[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.2

1.2     ! deraadt     1: /*     $OpenBSD: main.c,v 1.3 1995/09/02 06:15:37 jtc 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.2     ! deraadt    47: static char rcsid[] = "$OpenBSD: main.c,v 1.3 1995/09/02 06:15:37 jtc 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>
                     56: #include "error.h"
                     57: #include "pathnames.h"
                     58:
                     59: int    nerrors = 0;
                     60: Eptr   er_head;
                     61: Eptr   *errors;
                     62:
                     63: int    nfiles = 0;
                     64: Eptr   **files;        /* array of pointers into errors*/
                     65: int    language = INCC;
                     66:
                     67: char   *currentfilename = "????";
                     68: char   *processname;
                     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();
                     80: /*
                     81:  *     error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
                     82:  *
                     83:  *     -T:     terse output
                     84:  *
                     85:  *     -I:     the following name, `ignorename' contains a list of
                     86:  *             function names that are not to be treated as hard errors.
                     87:  *             Default: ~/.errorsrc
                     88:  *
                     89:  *     -n:     don't touch ANY files!
                     90:  *
                     91:  *     -q:     The user is to be queried before touching each
                     92:  *             file; if not specified, all files with hard, non
                     93:  *             ignorable errors are touched (assuming they can be).
                     94:  *
                     95:  *     -t:     touch only files ending with the list of suffices, each
                     96:  *             suffix preceded by a dot.
                     97:  *             eg, -t .c.y.l
                     98:  *             will touch only files ending with .c, .y or .l
                     99:  *
                    100:  *     -s:     print a summary of the error's categories.
                    101:  *
                    102:  *     -v:     after touching all files, overlay vi(1), ex(1) or ed(1)
                    103:  *             on top of error, entered in the first file with
                    104:  *             an error in it, with the appropriate editor
                    105:  *             set up to use the "next" command to get the other
                    106:  *             files containing errors.
                    107:  *
                    108:  *     -p:     (obsolete: for older versions of pi without bug
                    109:  *             fix regarding printing out the name of the main file
                    110:  *             with an error in it)
                    111:  *             Take the following argument and use it as the name of
                    112:  *             the pascal source file, suffix .p
                    113:  *
                    114:  *     -E:     show the errors in sorted order; intended for
                    115:  *             debugging.
                    116:  *
                    117:  *     -S:     show the errors in unsorted order
                    118:  *             (as they come from the error file)
                    119:  *
                    120:  *     infile: The error messages come from this file.
                    121:  *             Default: stdin
                    122:  */
                    123: main(argc, argv)
                    124:        int     argc;
                    125:        char    *argv[];
                    126: {
                    127:        char    *cp;
                    128:        char    *ignorename = 0;
                    129:        int     ed_argc;
                    130:        char    **ed_argv;              /*return from touchfiles*/
                    131:        boolean show_errors = FALSE;
                    132:        boolean Show_Errors = FALSE;
                    133:        boolean pr_summary = FALSE;
                    134:        boolean edit_files = FALSE;
                    135:
                    136:        processname = argv[0];
                    137:
                    138:        errorfile = stdin;
                    139:        if (argc > 1) for(; (argc > 1) && (argv[1][0] == '-'); argc--, argv++){
                    140:                for (cp = argv[1] + 1; *cp; cp++) switch(*cp){
                    141:                default:
                    142:                        fprintf(stderr, "%s: -%c: Unknown flag\n",
                    143:                                processname, *cp);
                    144:                        break;
                    145:
                    146:                case 'n':       notouch = TRUE; break;
                    147:                case 'q':       query = TRUE;   break;
                    148:                case 'S':       Show_Errors = TRUE;     break;
                    149:                case 's':       pr_summary = TRUE;      break;
                    150:                case 'v':       edit_files = TRUE;      break;
                    151:                case 'T':       terse = TRUE;   break;
                    152:                case 't':
                    153:                        *cp-- = 0; argv++; argc--;
                    154:                        if (argc > 1){
                    155:                                suffixlist = argv[1];
                    156:                        }
                    157:                        break;
                    158:                case 'I':       /*ignore file name*/
                    159:                        *cp-- = 0; argv++; argc--;
                    160:                        if (argc > 1)
                    161:                                ignorename = argv[1];
                    162:                        break;
                    163:                }
                    164:        }
                    165:        if (notouch)
                    166:                suffixlist = 0;
                    167:        if (argc > 1){
                    168:                if (argc > 3){
                    169:                        fprintf(stderr, "%s: Only takes 0 or 1 arguments\n",
                    170:                                processname);
                    171:                        exit(3);
                    172:                }
                    173:                if ( (errorfile = fopen(argv[1], "r")) == NULL){
                    174:                        fprintf(stderr, "%s: %s: No such file or directory for reading errors.\n",
                    175:                                processname, argv[1]);
                    176:                        exit(4);
                    177:                }
                    178:        }
                    179:        if ( (queryfile = fopen(im_on, "r")) == NULL){
                    180:                if (query){
                    181:                        fprintf(stderr,
                    182:                                "%s: Can't open \"%s\" to query the user.\n",
                    183:                                processname, im_on);
                    184:                        exit(9);
                    185:                }
                    186:        }
                    187:        if (signal(SIGINT, onintr) == SIG_IGN)
                    188:                signal(SIGINT, SIG_IGN);
                    189:        if (signal(SIGTERM, onintr) == SIG_IGN)
                    190:                signal(SIGTERM, SIG_IGN);
                    191:        getignored(ignorename);
                    192:        eaterrors(&nerrors, &errors);
                    193:        if (Show_Errors)
                    194:                printerrors(TRUE, nerrors, errors);
                    195:        qsort(errors, nerrors, sizeof(Eptr), errorsort);
                    196:        if (show_errors)
                    197:                printerrors(FALSE, nerrors, errors);
                    198:        findfiles(nerrors, errors, &nfiles, &files);
                    199: #define P(msg, arg) fprintf(stdout, msg, arg)
                    200:        if (pr_summary){
                    201:            if (nunknown)
                    202:              P("%d Errors are unclassifiable.\n", nunknown);
                    203:            if (nignore)
                    204:              P("%d Errors are classifiable, but totally discarded.\n",nignore);
                    205:            if (nsyncerrors)
                    206:              P("%d Errors are synchronization errors.\n", nsyncerrors);
                    207:            if (nignore)
                    208:              P("%d Errors are discarded because they refer to sacrosinct files.\n", ndiscard);
                    209:            if (nnulled)
                    210:              P("%d Errors are nulled because they refer to specific functions.\n", nnulled);
                    211:            if (nnonspec)
                    212:              P("%d Errors are not specific to any file.\n", nnonspec);
                    213:            if (nthisfile)
                    214:              P("%d Errors are specific to a given file, but not to a line.\n", nthisfile);
                    215:            if (ntrue)
                    216:              P("%d Errors are true errors, and can be inserted into the files.\n", ntrue);
                    217:        }
                    218:        filenames(nfiles, files);
                    219:        fflush(stdout);
                    220:        if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
                    221:                forkvi(ed_argc, ed_argv);
                    222: }
                    223:
                    224: forkvi(argc, argv)
                    225:        int     argc;
                    226:        char    **argv;
                    227: {
                    228:        if (query){
                    229:                switch(inquire(terse
                    230:                    ? "Edit? "
                    231:                    : "Do you still want to edit the files you touched? ")){
                    232:                case Q_NO:
                    233:                case Q_no:
                    234:                        return;
                    235:                default:
                    236:                        break;
                    237:                }
                    238:        }
                    239:        /*
                    240:         *      ed_agument's first argument is
                    241:         *      a vi/ex compatabile search argument
                    242:         *      to find the first occurance of ###
                    243:         */
                    244:        try("vi", argc, argv);
                    245:        try("ex", argc, argv);
                    246:        try("ed", argc-1, argv+1);
                    247:        fprintf(stdout, "Can't find any editors.\n");
                    248: }
                    249:
                    250: try(name, argc, argv)
                    251:        char    *name;
                    252:        int     argc;
                    253:        char    **argv;
                    254: {
                    255:        argv[0] = name;
                    256:        wordvprint(stdout, argc, argv);
                    257:        fprintf(stdout, "\n");
                    258:        fflush(stderr);
                    259:        fflush(stdout);
                    260:        sleep(2);
                    261:        if (freopen(im_on, "r", stdin) == NULL)
                    262:                return;
                    263:        if (freopen(im_on, "w", stdout) == NULL)
                    264:                return;
                    265:        execvp(name, argv);
                    266: }
                    267:
                    268: int errorsort(epp1, epp2)
                    269:                Eptr    *epp1, *epp2;
                    270: {
                    271:        reg     Eptr    ep1, ep2;
                    272:                int     order;
                    273:        /*
                    274:         *      Sort by:
                    275:         *      1)      synchronization, non specific, discarded errors first;
                    276:         *      2)      nulled and true errors last
                    277:         *              a)      grouped by similar file names
                    278:         *                      1)      grouped in ascending line number
                    279:         */
                    280:        ep1 = *epp1; ep2 = *epp2;
                    281:        if (ep1 == 0 || ep2 == 0)
                    282:                return(0);
                    283:        if ( (NOTSORTABLE(ep1->error_e_class)) ^ (NOTSORTABLE(ep2->error_e_class))){
                    284:                return(NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
                    285:        }
                    286:        if (NOTSORTABLE(ep1->error_e_class))    /* then both are */
                    287:                return(ep1->error_no - ep2->error_no);
                    288:        order = strcmp(ep1->error_text[0], ep2->error_text[0]);
                    289:        if (order == 0){
                    290:                return(ep1->error_line - ep2->error_line);
                    291:        }
                    292:        return(order);
                    293: }