[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / error

Annotation of src/usr.bin/error/main.c, Revision 1.7

1.7     ! mpech       1: /*     $OpenBSD: main.c,v 1.6 2001/07/12 05:17:02 deraadt Exp $        */
1.1       deraadt     2: /*     $NetBSD: main.c,v 1.3 1995/09/02 06:15:37 jtc Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1980, 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)main.c     8.1 (Berkeley) 6/6/93";
                     46: #endif
1.7     ! mpech      47: static char rcsid[] = "$OpenBSD: main.c,v 1.6 2001/07/12 05:17:02 deraadt Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: #include <signal.h>
                     51: #include <unistd.h>
                     52: #include <stdio.h>
                     53: #include <ctype.h>
                     54: #include <stdlib.h>
                     55: #include <string.h>
1.6       deraadt    56: #include <err.h>
1.1       deraadt    57: #include "error.h"
                     58: #include "pathnames.h"
                     59:
                     60: int    nerrors = 0;
                     61: Eptr   er_head;
                     62: Eptr   *errors;
                     63:
                     64: int    nfiles = 0;
                     65: Eptr   **files;        /* array of pointers into errors*/
                     66: int    language = INCC;
                     67:
                     68: char   *currentfilename = "????";
                     69: char   im_on[] = _PATH_TTY;    /* my tty name */
                     70:
                     71: boolean        query = FALSE;          /* query the operator if touch files */
                     72: boolean        notouch = FALSE;        /* don't touch ANY files */
                     73: boolean        piflag  = FALSE;        /* this is not pi */
                     74: boolean        terse   = FALSE;        /* Terse output */
                     75:
                     76: char   *suffixlist = ".*";     /* initially, can touch any file */
                     77:
                     78: int    errorsort();
                     79: void   onintr();
1.5       aaron      80: void   usage();
                     81:
1.1       deraadt    82: /*
                     83:  *     error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
                     84:  *
                     85:  *     -T:     terse output
                     86:  *
                     87:  *     -I:     the following name, `ignorename' contains a list of
                     88:  *             function names that are not to be treated as hard errors.
                     89:  *             Default: ~/.errorsrc
                     90:  *
                     91:  *     -n:     don't touch ANY files!
                     92:  *
                     93:  *     -q:     The user is to be queried before touching each
                     94:  *             file; if not specified, all files with hard, non
                     95:  *             ignorable errors are touched (assuming they can be).
                     96:  *
                     97:  *     -t:     touch only files ending with the list of suffices, each
                     98:  *             suffix preceded by a dot.
                     99:  *             eg, -t .c.y.l
                    100:  *             will touch only files ending with .c, .y or .l
                    101:  *
                    102:  *     -s:     print a summary of the error's categories.
                    103:  *
                    104:  *     -v:     after touching all files, overlay vi(1), ex(1) or ed(1)
                    105:  *             on top of error, entered in the first file with
                    106:  *             an error in it, with the appropriate editor
                    107:  *             set up to use the "next" command to get the other
                    108:  *             files containing errors.
                    109:  *
                    110:  *     -p:     (obsolete: for older versions of pi without bug
                    111:  *             fix regarding printing out the name of the main file
                    112:  *             with an error in it)
                    113:  *             Take the following argument and use it as the name of
                    114:  *             the pascal source file, suffix .p
                    115:  *
                    116:  *     -S:     show the errors in unsorted order
                    117:  *             (as they come from the error file)
                    118:  *
                    119:  *     infile: The error messages come from this file.
                    120:  *             Default: stdin
                    121:  */
1.5       aaron     122: int
1.1       deraadt   123: main(argc, argv)
1.5       aaron     124:        int argc;
                    125:        char *argv[];
1.1       deraadt   126: {
1.5       aaron     127:        char *ignorename = 0;
                    128:        char **ed_argv;         /* return from touchfiles */
                    129:        int ch, ed_argc;
1.1       deraadt   130:        boolean show_errors = FALSE;
                    131:        boolean Show_Errors = FALSE;
                    132:        boolean pr_summary = FALSE;
                    133:        boolean edit_files = FALSE;
                    134:
                    135:        errorfile = stdin;
1.5       aaron     136:        while ((ch = getopt(argc, argv, "STnqsvI:t:")) != -1)
                    137:                switch (ch) {
                    138:                case 'S':
                    139:                        Show_Errors = TRUE;
                    140:                        break;
                    141:                case 'T':
                    142:                        terse = TRUE;
                    143:                        break;
                    144:                case 'n':
                    145:                        notouch = TRUE;
                    146:                        break;
                    147:                case 'q':
                    148:                        query = TRUE;
                    149:                        break;
                    150:                case 's':
                    151:                        pr_summary = TRUE;
                    152:                        break;
                    153:                case 'v':
                    154:                        edit_files = TRUE;
                    155:                        break;
                    156:                case 'I':
                    157:                        ignorename = optarg;
1.1       deraadt   158:                        break;
                    159:                case 't':
1.5       aaron     160:                        suffixlist = optarg;
1.1       deraadt   161:                        break;
1.5       aaron     162:                default:
                    163:                        usage();
1.1       deraadt   164:                }
1.5       aaron     165:
                    166:        argc -= optind;
                    167:        argv += optind;
                    168:
1.1       deraadt   169:        if (notouch)
1.5       aaron     170:                suffixlist = NULL;
                    171:
                    172:        if (argc > 1) {
1.3       mickey    173:                if (argc > 3)
1.7     ! mpech     174:                        errx(3, "Only takes 0 or 1 arguments.");
1.5       aaron     175:                if ((errorfile = fopen(argv[1], "r")) == NULL)
1.4       millert   176:                        err(4, "%s", argv[1]);
1.1       deraadt   177:        }
1.5       aaron     178:
                    179:        if ((queryfile = fopen(im_on, "r")) == NULL) {
                    180:                if (query) {
1.3       mickey    181:                        errx(9, "Can't open \"%s\" to query the user.", im_on);
1.1       deraadt   182:                        exit(9);
                    183:                }
                    184:        }
1.5       aaron     185:
1.1       deraadt   186:        if (signal(SIGINT, onintr) == SIG_IGN)
                    187:                signal(SIGINT, SIG_IGN);
                    188:        if (signal(SIGTERM, onintr) == SIG_IGN)
                    189:                signal(SIGTERM, SIG_IGN);
1.5       aaron     190:
1.1       deraadt   191:        getignored(ignorename);
                    192:        eaterrors(&nerrors, &errors);
1.5       aaron     193:
1.1       deraadt   194:        if (Show_Errors)
                    195:                printerrors(TRUE, nerrors, errors);
1.5       aaron     196:
1.1       deraadt   197:        qsort(errors, nerrors, sizeof(Eptr), errorsort);
1.5       aaron     198:
1.1       deraadt   199:        if (show_errors)
                    200:                printerrors(FALSE, nerrors, errors);
1.5       aaron     201:
1.1       deraadt   202:        findfiles(nerrors, errors, &nfiles, &files);
1.5       aaron     203:
1.1       deraadt   204: #define P(msg, arg) fprintf(stdout, msg, arg)
1.5       aaron     205:
                    206:        if (pr_summary) {
                    207:                if (nunknown)
                    208:                        P("%d Errors are unclassifiable.\n", nunknown);
                    209:                if (nignore)
                    210:                        P("%d Errors are classifiable, but totally discarded.\n",nignore);
                    211:                if (nsyncerrors)
                    212:                        P("%d Errors are synchronization errors.\n", nsyncerrors);
                    213:                if (nignore)
                    214:                        P("%d Errors are discarded because they refer to sacrosinct files.\n", ndiscard);
                    215:                if (nnulled)
                    216:                        P("%d Errors are nulled because they refer to specific functions.\n", nnulled);
                    217:                if (nnonspec)
                    218:                        P("%d Errors are not specific to any file.\n", nnonspec);
                    219:                if (nthisfile)
                    220:                        P("%d Errors are specific to a given file, but not to a line.\n", nthisfile);
                    221:                if (ntrue)
                    222:                        P("%d Errors are true errors, and can be inserted into the files.\n", ntrue);
1.1       deraadt   223:        }
1.5       aaron     224:
1.1       deraadt   225:        filenames(nfiles, files);
                    226:        fflush(stdout);
1.5       aaron     227:
1.1       deraadt   228:        if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
                    229:                forkvi(ed_argc, ed_argv);
1.6       deraadt   230:        exit(0);
1.1       deraadt   231: }
                    232:
1.6       deraadt   233: void
1.1       deraadt   234: forkvi(argc, argv)
                    235:        int     argc;
                    236:        char    **argv;
                    237: {
1.5       aaron     238:        if (query) {
                    239:                switch (inquire(terse ? "Edit? " :
                    240:                    "Do you still want to edit the files you touched? ")) {
1.1       deraadt   241:                case Q_NO:
                    242:                case Q_no:
                    243:                        return;
                    244:                default:
                    245:                        break;
                    246:                }
                    247:        }
1.5       aaron     248:
1.1       deraadt   249:        /*
                    250:         *      ed_agument's first argument is
                    251:         *      a vi/ex compatabile search argument
                    252:         *      to find the first occurance of ###
                    253:         */
                    254:        try("vi", argc, argv);
                    255:        try("ex", argc, argv);
1.5       aaron     256:        try("ed", argc - 1, argv + 1);
1.1       deraadt   257:        fprintf(stdout, "Can't find any editors.\n");
                    258: }
                    259:
1.6       deraadt   260: void
1.1       deraadt   261: try(name, argc, argv)
1.5       aaron     262:        char *name;
                    263:        int argc;
                    264:        char **argv;
1.1       deraadt   265: {
                    266:        argv[0] = name;
                    267:        wordvprint(stdout, argc, argv);
                    268:        fprintf(stdout, "\n");
                    269:        fflush(stderr);
                    270:        fflush(stdout);
                    271:        sleep(2);
1.5       aaron     272:
1.1       deraadt   273:        if (freopen(im_on, "r", stdin) == NULL)
                    274:                return;
                    275:        if (freopen(im_on, "w", stdout) == NULL)
                    276:                return;
1.5       aaron     277:
1.1       deraadt   278:        execvp(name, argv);
                    279: }
                    280:
                    281: int errorsort(epp1, epp2)
1.5       aaron     282:        Eptr *epp1, *epp2;
1.1       deraadt   283: {
1.5       aaron     284:        reg Eptr ep1, ep2;
                    285:        int order;
                    286:
1.1       deraadt   287:        /*
                    288:         *      Sort by:
1.5       aaron     289:         *      1) synchronization, non specific, discarded errors first;
                    290:         *      2) nulled and true errors last
                    291:         *          a) grouped by similar file names
                    292:         *          b) grouped in ascending line number
1.1       deraadt   293:         */
                    294:        ep1 = *epp1; ep2 = *epp2;
                    295:        if (ep1 == 0 || ep2 == 0)
1.5       aaron     296:                return (0);
                    297:
                    298:        if ((NOTSORTABLE(ep1->error_e_class)) ^
                    299:            (NOTSORTABLE(ep2->error_e_class))) {
                    300:                return (NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
1.1       deraadt   301:        }
1.5       aaron     302:
1.1       deraadt   303:        if (NOTSORTABLE(ep1->error_e_class))    /* then both are */
1.5       aaron     304:                return (ep1->error_no - ep2->error_no);
                    305:
1.1       deraadt   306:        order = strcmp(ep1->error_text[0], ep2->error_text[0]);
1.5       aaron     307:        if (order == 0)
                    308:                return (ep1->error_line - ep2->error_line);
                    309:
                    310:        return (order);
                    311: }
                    312:
                    313: void
                    314: usage()
                    315: {
                    316:        extern char *__progname;
                    317:
                    318:        (void)fprintf(stderr,
                    319:            "usage: %s [-STnsqv] [-I ignorefile] [-t suffixlist] [name]\n",
                    320:            __progname);
                    321:        exit(1);
1.1       deraadt   322: }