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

Annotation of src/usr.bin/indent/indent.c, Revision 1.15

1.15    ! deraadt     1: /*     $OpenBSD: indent.c,v 1.14 2003/04/25 23:33:56 krw Exp $ */
1.3       deraadt     2:
1.1       deraadt     3: /*
1.9       pjanzen     4:  * Copyright (c) 1980, 1993
                      5:  *     The Regents of the University of California.
                      6:  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
1.1       deraadt     7:  * Copyright (c) 1985 Sun Microsystems, Inc.
                      8:  * All rights reserved.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
1.15    ! deraadt    18:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: #ifndef lint
                     36: char copyright[] =
                     37: "@(#) Copyright (c) 1985 Sun Microsystems, Inc.\n\
1.9       pjanzen    38:  @(#) Copyright (c) 1980, 1993\n\
                     39:         The Regents of the University of California.\n\
1.1       deraadt    40:  @(#) Copyright (c) 1976 Board of Trustees of the University of Illinois.\n\
                     41:  All rights reserved.\n";
                     42: #endif /* not lint */
                     43:
                     44: #ifndef lint
1.9       pjanzen    45: /*static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";*/
1.15    ! deraadt    46: static char rcsid[] = "$OpenBSD: indent.c,v 1.14 2003/04/25 23:33:56 krw Exp $";
1.1       deraadt    47: #endif /* not lint */
                     48:
                     49: #include <sys/param.h>
                     50: #include <fcntl.h>
                     51: #include <unistd.h>
                     52: #include <stdio.h>
                     53: #include <stdlib.h>
                     54: #include <string.h>
                     55: #include "indent_globs.h"
                     56: #include "indent_codes.h"
                     57: #include <ctype.h>
1.2       deraadt    58: #include <errno.h>
1.4       mickey     59: #include <err.h>
1.1       deraadt    60:
                     61: char       *in_name = "Standard Input";        /* will always point to name of input
                     62:                                         * file */
                     63: char       *out_name = "Standard Output";      /* will always point to name
                     64:                                                 * of output file */
                     65: char        bakfile[MAXPATHLEN] = "";
                     66:
1.4       mickey     67: void bakcopy();
                     68:
                     69: int
1.1       deraadt    70: main(argc, argv)
                     71:     int         argc;
                     72:     char      **argv;
                     73: {
                     74:
                     75:     extern int  found_err;     /* flag set in diag() on error */
                     76:     int         dec_ind;       /* current indentation for declarations */
                     77:     int         di_stack[20];  /* a stack of structure indentation levels */
                     78:     int         flushed_nl;    /* used when buffering up comments to remember
                     79:                                 * that a newline was passed over */
                     80:     int         force_nl;      /* when true, code must be broken */
                     81:     int         hd_type;       /* used to store type of stmt for if (...),
                     82:                                 * for (...), etc */
1.11      mpech      83:     int        i;              /* local loop counter */
1.1       deraadt    84:     int         scase;         /* set to true when we see a case, so we will
                     85:                                 * know what to do with the following colon */
                     86:     int         sp_sw;         /* when true, we are in the expressin of
                     87:                                 * if(...), while(...), etc. */
                     88:     int         squest;                /* when this is positive, we have seen a ?
                     89:                                 * without the matching : in a <c>?<s>:<s>
                     90:                                 * construct */
1.11      mpech      91:     char       *t_ptr;         /* used for copying tokens */
1.1       deraadt    92:     int         type_code;     /* the type of token, returned by lexi */
                     93:
                     94:     int         last_else = 0; /* true iff last keyword was an else */
                     95:
                     96:
                     97:     /*-----------------------------------------------*\
                     98:     |                INITIALIZATION                  |
                     99:     \*-----------------------------------------------*/
                    100:
                    101:
1.9       pjanzen   102:     hd_type = 0;
1.1       deraadt   103:     ps.p_stack[0] = stmt;      /* this is the parser's stack */
                    104:     ps.last_nl = true;         /* this is true if the last thing scanned was
                    105:                                 * a newline */
                    106:     ps.last_token = semicolon;
                    107:     combuf = (char *) malloc(bufsize);
                    108:     labbuf = (char *) malloc(bufsize);
                    109:     codebuf = (char *) malloc(bufsize);
                    110:     tokenbuf = (char *) malloc(bufsize);
1.9       pjanzen   111:     if (combuf == NULL || labbuf == NULL || codebuf == NULL ||
                    112:         tokenbuf == NULL)
1.10      pjanzen   113:            err(1, NULL);
1.1       deraadt   114:     l_com = combuf + bufsize - 5;
                    115:     l_lab = labbuf + bufsize - 5;
                    116:     l_code = codebuf + bufsize - 5;
                    117:     l_token = tokenbuf + bufsize - 5;
                    118:     combuf[0] = codebuf[0] = labbuf[0] = ' ';  /* set up code, label, and
                    119:                                                 * comment buffers */
                    120:     combuf[1] = codebuf[1] = labbuf[1] = '\0';
                    121:     ps.else_if = 1;            /* Default else-if special processing to on */
                    122:     s_lab = e_lab = labbuf + 1;
                    123:     s_code = e_code = codebuf + 1;
                    124:     s_com = e_com = combuf + 1;
                    125:     s_token = e_token = tokenbuf + 1;
                    126:
                    127:     in_buffer = (char *) malloc(10);
1.9       pjanzen   128:     if (in_buffer == NULL)
1.10      pjanzen   129:            err(1, NULL);
1.1       deraadt   130:     in_buffer_limit = in_buffer + 8;
                    131:     buf_ptr = buf_end = in_buffer;
                    132:     line_no = 1;
                    133:     had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
                    134:     sp_sw = force_nl = false;
                    135:     ps.in_or_st = false;
                    136:     ps.bl_line = true;
                    137:     dec_ind = 0;
                    138:     di_stack[ps.dec_nest = 0] = 0;
                    139:     ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
                    140:
                    141:
                    142:     scase = ps.pcase = false;
                    143:     squest = 0;
                    144:     sc_end = 0;
                    145:     bp_save = 0;
                    146:     be_save = 0;
                    147:
                    148:     output = 0;
                    149:
                    150:
                    151:
                    152:     /*--------------------------------------------------*\
                    153:     |                  COMMAND LINE SCAN                |
                    154:     \*--------------------------------------------------*/
                    155:
                    156: #ifdef undef
                    157:     max_col = 78;              /* -l78 */
                    158:     lineup_to_parens = 1;      /* -lp */
                    159:     ps.ljust_decl = 0;         /* -ndj */
                    160:     ps.com_ind = 33;           /* -c33 */
                    161:     star_comment_cont = 1;     /* -sc */
                    162:     ps.ind_size = 8;           /* -i8 */
                    163:     verbose = 0;
                    164:     ps.decl_indent = 16;       /* -di16 */
                    165:     ps.indent_parameters = 1;  /* -ip */
                    166:     ps.decl_com_ind = 0;       /* if this is not set to some positive value
                    167:                                 * by an arg, we will set this equal to
                    168:                                 * ps.com_ind */
                    169:     btype_2 = 1;               /* -br */
                    170:     cuddle_else = 1;           /* -ce */
                    171:     ps.unindent_displace = 0;  /* -d0 */
                    172:     ps.case_indent = 0;                /* -cli0 */
                    173:     format_col1_comments = 1;  /* -fc1 */
                    174:     procnames_start_line = 1;  /* -psl */
                    175:     proc_calls_space = 0;      /* -npcs */
                    176:     comment_delimiter_on_blankline = 1;        /* -cdb */
                    177:     ps.leave_comma = 1;                /* -nbc */
                    178: #endif
                    179:
                    180:     for (i = 1; i < argc; ++i)
                    181:        if (strcmp(argv[i], "-npro") == 0)
                    182:            break;
                    183:     set_defaults();
                    184:     if (i >= argc)
                    185:        set_profile();
                    186:
                    187:     for (i = 1; i < argc; ++i) {
                    188:
                    189:        /*
                    190:         * look thru args (if any) for changes to defaults
                    191:         */
                    192:        if (argv[i][0] != '-') {/* no flag on parameter */
                    193:            if (input == 0) {   /* we must have the input file */
                    194:                in_name = argv[i];      /* remember name of input file */
                    195:                input = fopen(in_name, "r");
1.4       mickey    196:                if (input == NULL)              /* check for open error */
1.7       millert   197:                        err(1, "%s", in_name);
1.1       deraadt   198:                continue;
                    199:            }
                    200:            else if (output == 0) {     /* we have the output file */
                    201:                out_name = argv[i];     /* remember name of output file */
1.4       mickey    202:                if (strcmp(in_name, out_name) == 0)     /* attempt to overwrite
1.1       deraadt   203:                                                         * the file */
1.5       deraadt   204:                        errx(1, "input and output files must be different");
1.1       deraadt   205:                output = fopen(out_name, "w");
1.4       mickey    206:                if (output == NULL)     /* check for create error */
1.7       millert   207:                        err(1, "%s", out_name);
1.1       deraadt   208:                continue;
                    209:            }
1.5       deraadt   210:            errx(1, "unknown parameter: %s", argv[i]);
1.1       deraadt   211:        }
                    212:        else
                    213:            set_option(argv[i]);
                    214:     }                          /* end of for */
1.6       alex      215:     if (input == NULL) {
                    216:        fprintf(stderr, "usage: indent file [ outfile ] [ options ]\n");
                    217:        exit(1);
                    218:     }
1.9       pjanzen   219:     if (output == NULL) {
1.1       deraadt   220:        if (troff)
                    221:            output = stdout;
                    222:        else {
                    223:            out_name = in_name;
                    224:            bakcopy();
                    225:        }
1.9       pjanzen   226:     }
1.1       deraadt   227:     if (ps.com_ind <= 1)
                    228:        ps.com_ind = 2;         /* dont put normal comments before column 2 */
                    229:     if (troff) {
                    230:        if (bodyf.font[0] == 0)
                    231:            parsefont(&bodyf, "R");
                    232:        if (scomf.font[0] == 0)
                    233:            parsefont(&scomf, "I");
                    234:        if (blkcomf.font[0] == 0)
                    235:            blkcomf = scomf, blkcomf.size += 2;
                    236:        if (boxcomf.font[0] == 0)
                    237:            boxcomf = blkcomf;
                    238:        if (stringf.font[0] == 0)
                    239:            parsefont(&stringf, "L");
                    240:        if (keywordf.font[0] == 0)
                    241:            parsefont(&keywordf, "B");
                    242:        writefdef(&bodyf, 'B');
                    243:        writefdef(&scomf, 'C');
                    244:        writefdef(&blkcomf, 'L');
                    245:        writefdef(&boxcomf, 'X');
                    246:        writefdef(&stringf, 'S');
                    247:        writefdef(&keywordf, 'K');
                    248:     }
                    249:     if (block_comment_max_col <= 0)
                    250:        block_comment_max_col = max_col;
                    251:     if (ps.decl_com_ind <= 0)  /* if not specified by user, set this */
                    252:        ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
                    253:     if (continuation_indent == 0)
                    254:        continuation_indent = ps.ind_size;
1.4       mickey    255:     fill_buffer();     /* get first batch of stuff into input buffer */
1.1       deraadt   256:
                    257:     parse(semicolon);
                    258:     {
1.9       pjanzen   259:        char *p = buf_ptr;
                    260:        int   col = 1;
1.1       deraadt   261:
                    262:        while (1) {
                    263:            if (*p == ' ')
                    264:                col++;
                    265:            else if (*p == '\t')
                    266:                col = ((col - 1) & ~7) + 9;
                    267:            else
                    268:                break;
                    269:            p++;
                    270:        }
                    271:        if (col > ps.ind_size)
                    272:            ps.ind_level = ps.i_l_follow = col / ps.ind_size;
                    273:     }
                    274:     if (troff) {
1.11      mpech     275:        char *p = in_name,
1.1       deraadt   276:                   *beg = in_name;
                    277:
                    278:        while (*p)
                    279:            if (*p++ == '/')
                    280:                beg = p;
                    281:        fprintf(output, ".Fn \"%s\"\n", beg);
                    282:     }
                    283:     /*
                    284:      * START OF MAIN LOOP
                    285:      */
                    286:
                    287:     while (1) {                        /* this is the main loop.  it will go until we
                    288:                                 * reach eof */
                    289:        int         is_procname;
                    290:
                    291:        type_code = lexi();     /* lexi reads one token.  The actual
                    292:                                 * characters read are stored in "token". lexi
                    293:                                 * returns a code indicating the type of token */
                    294:        is_procname = ps.procname[0];
                    295:
                    296:        /*
                    297:         * The following code moves everything following an if (), while (),
                    298:         * else, etc. up to the start of the following stmt to a buffer. This
                    299:         * allows proper handling of both kinds of brace placement.
                    300:         */
                    301:
                    302:        flushed_nl = false;
                    303:        while (ps.search_brace) {       /* if we scanned an if(), while(),
                    304:                                         * etc., we might need to copy stuff
                    305:                                         * into a buffer we must loop, copying
                    306:                                         * stuff into save_com, until we find
                    307:                                         * the start of the stmt which follows
                    308:                                         * the if, or whatever */
                    309:            switch (type_code) {
                    310:            case newline:
                    311:                ++line_no;
                    312:                flushed_nl = true;
                    313:            case form_feed:
                    314:                break;          /* form feeds and newlines found here will be
                    315:                                 * ignored */
                    316:
                    317:            case lbrace:        /* this is a brace that starts the compound
                    318:                                 * stmt */
                    319:                if (sc_end == 0) {      /* ignore buffering if a comment wasnt
                    320:                                         * stored up */
                    321:                    ps.search_brace = false;
                    322:                    goto check_type;
                    323:                }
                    324:                if (btype_2) {
                    325:                    save_com[0] = '{';  /* we either want to put the brace
                    326:                                         * right after the if */
                    327:                    goto sw_buffer;     /* go to common code to get out of
                    328:                                         * this loop */
                    329:                }
                    330:            case comment:       /* we have a comment, so we must copy it into
                    331:                                 * the buffer */
                    332:                if (!flushed_nl || sc_end != 0) {
                    333:                    if (sc_end == 0) {  /* if this is the first comment, we
                    334:                                         * must set up the buffer */
                    335:                        save_com[0] = save_com[1] = ' ';
                    336:                        sc_end = &(save_com[2]);
                    337:                    }
                    338:                    else {
                    339:                        *sc_end++ = '\n';       /* add newline between
                    340:                                                 * comments */
                    341:                        *sc_end++ = ' ';
                    342:                        --line_no;
                    343:                    }
                    344:                    *sc_end++ = '/';    /* copy in start of comment */
                    345:                    *sc_end++ = '*';
                    346:
                    347:                    for (;;) {  /* loop until we get to the end of the comment */
                    348:                        *sc_end = *buf_ptr++;
                    349:                        if (buf_ptr >= buf_end)
                    350:                            fill_buffer();
                    351:
                    352:                        if (*sc_end++ == '*' && *buf_ptr == '/')
                    353:                            break;      /* we are at end of comment */
                    354:
                    355:                        if (sc_end >= &(save_com[sc_size])) {   /* check for temp buffer
                    356:                                                                 * overflow */
                    357:                            diag(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever.");
                    358:                            fflush(output);
                    359:                            exit(1);
                    360:                        }
                    361:                    }
                    362:                    *sc_end++ = '/';    /* add ending slash */
                    363:                    if (++buf_ptr >= buf_end)   /* get past / in buffer */
                    364:                        fill_buffer();
                    365:                    break;
                    366:                }
                    367:            default:            /* it is the start of a normal statment */
                    368:                if (flushed_nl) /* if we flushed a newline, make sure it is
                    369:                                 * put back */
                    370:                    force_nl = true;
1.4       mickey    371:                if ((type_code == sp_paren && *token == 'i'
                    372:                     && last_else && ps.else_if) ||
                    373:                    (type_code == sp_nparen && *token == 'e'
                    374:                     && e_code != s_code && e_code[-1] == '}'))
                    375:                        force_nl = false;
1.1       deraadt   376:
                    377:                if (sc_end == 0) {      /* ignore buffering if comment wasnt
                    378:                                         * saved up */
                    379:                    ps.search_brace = false;
                    380:                    goto check_type;
                    381:                }
                    382:                if (force_nl) { /* if we should insert a nl here, put it into
                    383:                                 * the buffer */
                    384:                    force_nl = false;
                    385:                    --line_no;  /* this will be re-increased when the nl is
                    386:                                 * read from the buffer */
                    387:                    *sc_end++ = '\n';
                    388:                    *sc_end++ = ' ';
                    389:                    if (verbose && !flushed_nl) /* print error msg if the line
                    390:                                                 * was not already broken */
                    391:                        diag(0, "Line broken");
                    392:                    flushed_nl = false;
                    393:                }
                    394:                for (t_ptr = token; *t_ptr; ++t_ptr)
                    395:                    *sc_end++ = *t_ptr; /* copy token into temp buffer */
                    396:                ps.procname[0] = 0;
                    397:
                    398:        sw_buffer:
                    399:                ps.search_brace = false;        /* stop looking for start of
                    400:                                                 * stmt */
                    401:                bp_save = buf_ptr;      /* save current input buffer */
                    402:                be_save = buf_end;
                    403:                buf_ptr = save_com;     /* fix so that subsequent calls to
                    404:                                         * lexi will take tokens out of
                    405:                                         * save_com */
                    406:                *sc_end++ = ' ';/* add trailing blank, just in case */
                    407:                buf_end = sc_end;
                    408:                sc_end = 0;
                    409:                break;
                    410:            }                   /* end of switch */
                    411:            if (type_code != 0) /* we must make this check, just in case there
                    412:                                 * was an unexpected EOF */
                    413:                type_code = lexi();     /* read another token */
                    414:            /* if (ps.search_brace) ps.procname[0] = 0; */
                    415:            if ((is_procname = ps.procname[0]) && flushed_nl
                    416:                    && !procnames_start_line && ps.in_decl
                    417:                    && type_code == ident)
                    418:                flushed_nl = 0;
                    419:        }                       /* end of while (search_brace) */
                    420:        last_else = 0;
                    421: check_type:
                    422:        if (type_code == 0) {   /* we got eof */
                    423:            if (s_lab != e_lab || s_code != e_code
                    424:                    || s_com != e_com)  /* must dump end of line */
                    425:                dump_line();
                    426:            if (ps.tos > 1)     /* check for balanced braces */
1.12      vincent   427:                diag(1, "Missing braces at end of file.");
1.1       deraadt   428:
                    429:            if (verbose) {
                    430:                printf("There were %d output lines and %d comments\n",
                    431:                       ps.out_lines, ps.out_coms);
                    432:                printf("(Lines with comments)/(Lines with code): %6.3f\n",
                    433:                       (1.0 * ps.com_lines) / code_lines);
                    434:            }
                    435:            fflush(output);
                    436:            exit(found_err);
                    437:        }
                    438:        if (
                    439:                (type_code != comment) &&
                    440:                (type_code != newline) &&
                    441:                (type_code != preesc) &&
                    442:                (type_code != form_feed)) {
                    443:            if (force_nl &&
                    444:                    (type_code != semicolon) &&
                    445:                    (type_code != lbrace || !btype_2)) {
                    446:                /* we should force a broken line here */
                    447:                if (verbose && !flushed_nl)
                    448:                    diag(0, "Line broken");
                    449:                flushed_nl = false;
                    450:                dump_line();
                    451:                ps.want_blank = false;  /* dont insert blank at line start */
                    452:                force_nl = false;
                    453:            }
                    454:            ps.in_stmt = true;  /* turn on flag which causes an extra level of
                    455:                                 * indentation. this is turned off by a ; or
                    456:                                 * '}' */
                    457:            if (s_com != e_com) {       /* the turkey has embedded a comment
                    458:                                         * in a line. fix it */
                    459:                *e_code++ = ' ';
                    460:                for (t_ptr = s_com; *t_ptr; ++t_ptr) {
                    461:                    CHECK_SIZE_CODE;
                    462:                    *e_code++ = *t_ptr;
                    463:                }
                    464:                *e_code++ = ' ';
                    465:                *e_code = '\0'; /* null terminate code sect */
                    466:                ps.want_blank = false;
                    467:                e_com = s_com;
                    468:            }
                    469:        }
                    470:        else if (type_code != comment)  /* preserve force_nl thru a comment */
                    471:            force_nl = false;   /* cancel forced newline after newline, form
                    472:                                 * feed, etc */
                    473:
                    474:
                    475:
                    476:        /*-----------------------------------------------------*\
                    477:        |          do switch on type of token scanned           |
                    478:        \*-----------------------------------------------------*/
                    479:        CHECK_SIZE_CODE;
                    480:        switch (type_code) {    /* now, decide what to do with the token */
                    481:
                    482:        case form_feed: /* found a form feed in line */
                    483:            ps.use_ff = true;   /* a form feed is treated much like a newline */
                    484:            dump_line();
                    485:            ps.want_blank = false;
                    486:            break;
                    487:
                    488:        case newline:
                    489:            if (ps.last_token != comma || ps.p_l_follow > 0
                    490:                    || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
                    491:                dump_line();
                    492:                ps.want_blank = false;
                    493:            }
                    494:            ++line_no;          /* keep track of input line number */
                    495:            break;
                    496:
                    497:        case lparen:            /* got a '(' or '[' */
                    498:            ++ps.p_l_follow;    /* count parens to make Healy happy */
                    499:            if (ps.want_blank && *token != '[' &&
                    500:                    (ps.last_token != ident || proc_calls_space
                    501:              || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon))))
                    502:                *e_code++ = ' ';
                    503:            if (ps.in_decl && !ps.block_init)
                    504:                if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) {
                    505:                    ps.dumped_decl_indent = 1;
1.14      krw       506:                    snprintf(e_code, (l_code - e_code) + 5,
                    507:                        "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
1.1       deraadt   508:                    e_code += strlen(e_code);
1.14      krw       509:                    CHECK_SIZE_CODE;
1.1       deraadt   510:                }
                    511:                else {
                    512:                    while ((e_code - s_code) < dec_ind) {
                    513:                        CHECK_SIZE_CODE;
                    514:                        *e_code++ = ' ';
                    515:                    }
                    516:                    *e_code++ = token[0];
                    517:                }
                    518:            else
                    519:                *e_code++ = token[0];
                    520:            ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;
                    521:            if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
                    522:                    && ps.paren_indents[0] < 2 * ps.ind_size)
                    523:                ps.paren_indents[0] = 2 * ps.ind_size;
                    524:            ps.want_blank = false;
                    525:            if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
                    526:                /*
                    527:                 * this is a kluge to make sure that declarations will be
                    528:                 * aligned right if proc decl has an explicit type on it, i.e.
                    529:                 * "int a(x) {..."
                    530:                 */
                    531:                parse(semicolon);       /* I said this was a kluge... */
                    532:                ps.in_or_st = false;    /* turn off flag for structure decl or
                    533:                                         * initialization */
                    534:            }
                    535:            if (ps.sizeof_keyword)
                    536:                ps.sizeof_mask |= 1 << ps.p_l_follow;
                    537:            break;
                    538:
                    539:        case rparen:            /* got a ')' or ']' */
                    540:            rparen_count--;
                    541:            if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
                    542:                ps.last_u_d = true;
                    543:                ps.cast_mask &= (1 << ps.p_l_follow) - 1;
                    544:            }
                    545:            ps.sizeof_mask &= (1 << ps.p_l_follow) - 1;
                    546:            if (--ps.p_l_follow < 0) {
                    547:                ps.p_l_follow = 0;
                    548:                diag(0, "Extra %c", *token);
                    549:            }
                    550:            if (e_code == s_code)       /* if the paren starts the line */
                    551:                ps.paren_level = ps.p_l_follow; /* then indent it */
                    552:
                    553:            *e_code++ = token[0];
                    554:            ps.want_blank = true;
                    555:
                    556:            if (sp_sw && (ps.p_l_follow == 0)) {        /* check for end of if
                    557:                                                         * (...), or some such */
                    558:                sp_sw = false;
                    559:                force_nl = true;/* must force newline after if */
                    560:                ps.last_u_d = true;     /* inform lexi that a following
                    561:                                         * operator is unary */
                    562:                ps.in_stmt = false;     /* dont use stmt continuation
                    563:                                         * indentation */
                    564:
                    565:                parse(hd_type); /* let parser worry about if, or whatever */
                    566:            }
                    567:            ps.search_brace = btype_2;  /* this should insure that constructs
                    568:                                         * such as main(){...} and int[]{...}
                    569:                                         * have their braces put in the right
                    570:                                         * place */
                    571:            break;
                    572:
                    573:        case unary_op:          /* this could be any unary operation */
                    574:            if (ps.want_blank)
                    575:                *e_code++ = ' ';
                    576:
                    577:            if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) {
1.14      krw       578:                snprintf(e_code, (l_code - e_code) + 5,
                    579:                    "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
1.1       deraadt   580:                ps.dumped_decl_indent = 1;
                    581:                e_code += strlen(e_code);
1.14      krw       582:                CHECK_SIZE_CODE;
1.1       deraadt   583:            }
                    584:            else {
                    585:                char       *res = token;
                    586:
                    587:                if (ps.in_decl && !ps.block_init) {     /* if this is a unary op
                    588:                                                         * in a declaration, we
                    589:                                                         * should indent this
                    590:                                                         * token */
                    591:                    for (i = 0; token[i]; ++i); /* find length of token */
                    592:                    while ((e_code - s_code) < (dec_ind - i)) {
                    593:                        CHECK_SIZE_CODE;
                    594:                        *e_code++ = ' ';        /* pad it */
                    595:                    }
                    596:                }
                    597:                if (troff && token[0] == '-' && token[1] == '>')
                    598:                    res = "\\(->";
                    599:                for (t_ptr = res; *t_ptr; ++t_ptr) {
                    600:                    CHECK_SIZE_CODE;
                    601:                    *e_code++ = *t_ptr;
                    602:                }
                    603:            }
                    604:            ps.want_blank = false;
                    605:            break;
                    606:
                    607:        case binary_op: /* any binary operation */
                    608:            if (ps.want_blank)
                    609:                *e_code++ = ' ';
                    610:            {
                    611:                char       *res = token;
                    612:
                    613:                if (troff)
                    614:                    switch (token[0]) {
                    615:                    case '<':
                    616:                        if (token[1] == '=')
                    617:                            res = "\\(<=";
                    618:                        break;
                    619:                    case '>':
                    620:                        if (token[1] == '=')
                    621:                            res = "\\(>=";
                    622:                        break;
                    623:                    case '!':
                    624:                        if (token[1] == '=')
                    625:                            res = "\\(!=";
                    626:                        break;
                    627:                    case '|':
                    628:                        if (token[1] == '|')
                    629:                            res = "\\(br\\(br";
                    630:                        else if (token[1] == 0)
                    631:                            res = "\\(br";
                    632:                        break;
                    633:                    }
                    634:                for (t_ptr = res; *t_ptr; ++t_ptr) {
                    635:                    CHECK_SIZE_CODE;
                    636:                    *e_code++ = *t_ptr; /* move the operator */
                    637:                }
                    638:            }
                    639:            ps.want_blank = true;
                    640:            break;
                    641:
                    642:        case postop:            /* got a trailing ++ or -- */
                    643:            *e_code++ = token[0];
                    644:            *e_code++ = token[1];
                    645:            ps.want_blank = true;
                    646:            break;
                    647:
                    648:        case question:          /* got a ? */
                    649:            squest++;           /* this will be used when a later colon
                    650:                                 * appears so we can distinguish the
                    651:                                 * <c>?<n>:<n> construct */
                    652:            if (ps.want_blank)
                    653:                *e_code++ = ' ';
                    654:            *e_code++ = '?';
                    655:            ps.want_blank = true;
                    656:            break;
                    657:
                    658:        case casestmt:          /* got word 'case' or 'default' */
                    659:            scase = true;       /* so we can process the later colon properly */
                    660:            goto copy_id;
                    661:
                    662:        case colon:             /* got a ':' */
                    663:            if (squest > 0) {   /* it is part of the <c>?<n>: <n> construct */
                    664:                --squest;
                    665:                if (ps.want_blank)
                    666:                    *e_code++ = ' ';
                    667:                *e_code++ = ':';
                    668:                ps.want_blank = true;
                    669:                break;
                    670:            }
                    671:            if (ps.in_decl) {
                    672:                *e_code++ = ':';
                    673:                ps.want_blank = false;
                    674:                break;
                    675:            }
                    676:            ps.in_stmt = false; /* seeing a label does not imply we are in a
                    677:                                 * stmt */
                    678:            for (t_ptr = s_code; *t_ptr; ++t_ptr)
                    679:                *e_lab++ = *t_ptr;      /* turn everything so far into a label */
                    680:            e_code = s_code;
                    681:            *e_lab++ = ':';
                    682:            *e_lab++ = ' ';
                    683:            *e_lab = '\0';
                    684:
                    685:            force_nl = ps.pcase = scase;        /* ps.pcase will be used by
                    686:                                                 * dump_line to decide how to
                    687:                                                 * indent the label. force_nl
                    688:                                                 * will force a case n: to be
                    689:                                                 * on a line by itself */
                    690:            scase = false;
                    691:            ps.want_blank = false;
                    692:            break;
                    693:
                    694:        case semicolon: /* got a ';' */
                    695:            ps.in_or_st = false;/* we are not in an initialization or
                    696:                                 * structure declaration */
                    697:            scase = false;      /* these will only need resetting in a error */
                    698:            squest = 0;
                    699:            if (ps.last_token == rparen && rparen_count == 0)
                    700:                ps.in_parameter_declaration = 0;
                    701:            ps.cast_mask = 0;
                    702:            ps.sizeof_mask = 0;
                    703:            ps.block_init = 0;
                    704:            ps.block_init_level = 0;
                    705:            ps.just_saw_decl--;
                    706:
                    707:            if (ps.in_decl && s_code == e_code && !ps.block_init)
                    708:                while ((e_code - s_code) < (dec_ind - 1)) {
                    709:                    CHECK_SIZE_CODE;
                    710:                    *e_code++ = ' ';
                    711:                }
                    712:
                    713:            ps.in_decl = (ps.dec_nest > 0);     /* if we were in a first level
                    714:                                                 * structure declaration, we
                    715:                                                 * arent any more */
                    716:
                    717:            if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
                    718:
                    719:                /*
                    720:                 * This should be true iff there were unbalanced parens in the
                    721:                 * stmt.  It is a bit complicated, because the semicolon might
                    722:                 * be in a for stmt
                    723:                 */
                    724:                diag(1, "Unbalanced parens");
                    725:                ps.p_l_follow = 0;
                    726:                if (sp_sw) {    /* this is a check for a if, while, etc. with
                    727:                                 * unbalanced parens */
                    728:                    sp_sw = false;
                    729:                    parse(hd_type);     /* dont lose the if, or whatever */
                    730:                }
                    731:            }
                    732:            *e_code++ = ';';
                    733:            ps.want_blank = true;
                    734:            ps.in_stmt = (ps.p_l_follow > 0);   /* we are no longer in the
                    735:                                                 * middle of a stmt */
                    736:
                    737:            if (!sp_sw) {       /* if not if for (;;) */
                    738:                parse(semicolon);       /* let parser know about end of stmt */
                    739:                force_nl = true;/* force newline after a end of stmt */
                    740:            }
                    741:            break;
                    742:
                    743:        case lbrace:            /* got a '{' */
                    744:            ps.in_stmt = false; /* dont indent the {} */
                    745:            if (!ps.block_init)
                    746:                force_nl = true;/* force other stuff on same line as '{' onto
                    747:                                 * new line */
                    748:            else if (ps.block_init_level <= 0)
                    749:                ps.block_init_level = 1;
                    750:            else
                    751:                ps.block_init_level++;
                    752:
                    753:            if (s_code != e_code && !ps.block_init) {
                    754:                if (!btype_2) {
                    755:                    dump_line();
                    756:                    ps.want_blank = false;
                    757:                }
                    758:                else if (ps.in_parameter_declaration && !ps.in_or_st) {
                    759:                    ps.i_l_follow = 0;
                    760:                    dump_line();
                    761:                    ps.want_blank = false;
                    762:                }
                    763:            }
                    764:            if (ps.in_parameter_declaration)
                    765:                prefix_blankline_requested = 0;
                    766:
                    767:            if (ps.p_l_follow > 0) {    /* check for preceeding unbalanced
                    768:                                         * parens */
                    769:                diag(1, "Unbalanced parens");
                    770:                ps.p_l_follow = 0;
                    771:                if (sp_sw) {    /* check for unclosed if, for, etc. */
                    772:                    sp_sw = false;
                    773:                    parse(hd_type);
                    774:                    ps.ind_level = ps.i_l_follow;
                    775:                }
                    776:            }
                    777:            if (s_code == e_code)
                    778:                ps.ind_stmt = false;    /* dont put extra indentation on line
                    779:                                         * with '{' */
                    780:            if (ps.in_decl && ps.in_or_st) {    /* this is either a structure
                    781:                                                 * declaration or an init */
                    782:                di_stack[ps.dec_nest++] = dec_ind;
                    783:                /* ?            dec_ind = 0; */
                    784:            }
                    785:            else {
1.9       pjanzen   786:                ps.decl_on_line = false;
                    787:                /* we can't be in the middle of a declaration, so don't do
                    788:                 * special indentation of comments */
1.1       deraadt   789:                if (blanklines_after_declarations_at_proctop
                    790:                        && ps.in_parameter_declaration)
                    791:                    postfix_blankline_requested = 1;
                    792:                ps.in_parameter_declaration = 0;
                    793:            }
                    794:            dec_ind = 0;
                    795:            parse(lbrace);      /* let parser know about this */
                    796:            if (ps.want_blank)  /* put a blank before '{' if '{' is not at
                    797:                                 * start of line */
                    798:                *e_code++ = ' ';
                    799:            ps.want_blank = false;
                    800:            *e_code++ = '{';
                    801:            ps.just_saw_decl = 0;
                    802:            break;
                    803:
                    804:        case rbrace:            /* got a '}' */
                    805:            if (ps.p_stack[ps.tos] == decl && !ps.block_init)   /* semicolons can be
                    806:                                                                 * omitted in
                    807:                                                                 * declarations */
                    808:                parse(semicolon);
                    809:            if (ps.p_l_follow) {/* check for unclosed if, for, else. */
                    810:                diag(1, "Unbalanced parens");
                    811:                ps.p_l_follow = 0;
                    812:                sp_sw = false;
                    813:            }
                    814:            ps.just_saw_decl = 0;
                    815:            ps.block_init_level--;
                    816:            if (s_code != e_code && !ps.block_init) {   /* '}' must be first on
                    817:                                                         * line */
                    818:                if (verbose)
                    819:                    diag(0, "Line broken");
                    820:                dump_line();
                    821:            }
                    822:            *e_code++ = '}';
                    823:            ps.want_blank = true;
                    824:            ps.in_stmt = ps.ind_stmt = false;
                    825:            if (ps.dec_nest > 0) {      /* we are in multi-level structure
                    826:                                         * declaration */
                    827:                dec_ind = di_stack[--ps.dec_nest];
                    828:                if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
                    829:                    ps.just_saw_decl = 2;
                    830:                ps.in_decl = true;
                    831:            }
                    832:            prefix_blankline_requested = 0;
                    833:            parse(rbrace);      /* let parser know about this */
                    834:            ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
                    835:                && ps.il[ps.tos] >= ps.ind_level;
                    836:            if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
                    837:                postfix_blankline_requested = 1;
                    838:            break;
                    839:
                    840:        case swstmt:            /* got keyword "switch" */
                    841:            sp_sw = true;
                    842:            hd_type = swstmt;   /* keep this for when we have seen the
                    843:                                 * expression */
                    844:            goto copy_id;       /* go move the token into buffer */
                    845:
                    846:        case sp_paren:          /* token is if, while, for */
                    847:            sp_sw = true;       /* the interesting stuff is done after the
                    848:                                 * expression is scanned */
                    849:            hd_type = (*token == 'i' ? ifstmt :
                    850:                       (*token == 'w' ? whilestmt : forstmt));
                    851:
                    852:            /*
                    853:             * remember the type of header for later use by parser
                    854:             */
                    855:            goto copy_id;       /* copy the token into line */
                    856:
                    857:        case sp_nparen: /* got else, do */
                    858:            ps.in_stmt = false;
                    859:            if (*token == 'e') {
                    860:                if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
                    861:                    if (verbose)
                    862:                        diag(0, "Line broken");
                    863:                    dump_line();/* make sure this starts a line */
                    864:                    ps.want_blank = false;
                    865:                }
                    866:                force_nl = true;/* also, following stuff must go onto new line */
                    867:                last_else = 1;
                    868:                parse(elselit);
                    869:            }
                    870:            else {
                    871:                if (e_code != s_code) { /* make sure this starts a line */
                    872:                    if (verbose)
                    873:                        diag(0, "Line broken");
                    874:                    dump_line();
                    875:                    ps.want_blank = false;
                    876:                }
                    877:                force_nl = true;/* also, following stuff must go onto new line */
                    878:                last_else = 0;
                    879:                parse(dolit);
                    880:            }
                    881:            goto copy_id;       /* move the token into line */
                    882:
                    883:        case decl:              /* we have a declaration type (int, register,
                    884:                                 * etc.) */
                    885:            parse(decl);        /* let parser worry about indentation */
                    886:            if (ps.last_token == rparen && ps.tos <= 1) {
                    887:                ps.in_parameter_declaration = 1;
                    888:                if (s_code != e_code) {
                    889:                    dump_line();
                    890:                    ps.want_blank = 0;
                    891:                }
                    892:            }
                    893:            if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
                    894:                ps.ind_level = ps.i_l_follow = 1;
                    895:                ps.ind_stmt = 0;
                    896:            }
                    897:            ps.in_or_st = true; /* this might be a structure or initialization
                    898:                                 * declaration */
                    899:            ps.in_decl = ps.decl_on_line = true;
                    900:            if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
                    901:                ps.just_saw_decl = 2;
                    902:            prefix_blankline_requested = 0;
                    903:            for (i = 0; token[i++];);   /* get length of token */
                    904:
                    905:            /*
                    906:             * dec_ind = e_code - s_code + (ps.decl_indent>i ? ps.decl_indent
                    907:             * : i);
                    908:             */
                    909:            dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
                    910:            goto copy_id;
                    911:
                    912:        case ident:             /* got an identifier or constant */
                    913:            if (ps.in_decl) {   /* if we are in a declaration, we must indent
                    914:                                 * identifier */
                    915:                if (ps.want_blank)
                    916:                    *e_code++ = ' ';
                    917:                ps.want_blank = false;
                    918:                if (is_procname == 0 || !procnames_start_line) {
1.9       pjanzen   919:                    if (!ps.block_init) {
1.1       deraadt   920:                        if (troff && !ps.dumped_decl_indent) {
1.14      krw       921:                            snprintf(e_code, (l_code - e_code) + 5,
                    922:                                "\n.De %dp+\200p\n", dec_ind * 7);
1.1       deraadt   923:                            ps.dumped_decl_indent = 1;
                    924:                            e_code += strlen(e_code);
1.14      krw       925:                            CHECK_SIZE_CODE;
1.1       deraadt   926:                        }
                    927:                        else
                    928:                            while ((e_code - s_code) < dec_ind) {
                    929:                                CHECK_SIZE_CODE;
                    930:                                *e_code++ = ' ';
                    931:                            }
1.9       pjanzen   932:                    }
1.1       deraadt   933:                }
                    934:                else {
                    935:                    if (dec_ind && s_code != e_code)
                    936:                        dump_line();
                    937:                    dec_ind = 0;
                    938:                    ps.want_blank = false;
                    939:                }
                    940:            }
                    941:            else if (sp_sw && ps.p_l_follow == 0) {
                    942:                sp_sw = false;
                    943:                force_nl = true;
                    944:                ps.last_u_d = true;
                    945:                ps.in_stmt = false;
                    946:                parse(hd_type);
                    947:            }
                    948:     copy_id:
                    949:            if (ps.want_blank)
                    950:                *e_code++ = ' ';
                    951:            if (troff && ps.its_a_keyword) {
                    952:                e_code = chfont(&bodyf, &keywordf, e_code);
                    953:                for (t_ptr = token; *t_ptr; ++t_ptr) {
                    954:                    CHECK_SIZE_CODE;
                    955:                    *e_code++ = keywordf.allcaps && islower(*t_ptr)
                    956:                        ? toupper(*t_ptr) : *t_ptr;
                    957:                }
                    958:                e_code = chfont(&keywordf, &bodyf, e_code);
                    959:            }
                    960:            else
                    961:                for (t_ptr = token; *t_ptr; ++t_ptr) {
                    962:                    CHECK_SIZE_CODE;
                    963:                    *e_code++ = *t_ptr;
                    964:                }
                    965:            ps.want_blank = true;
                    966:            break;
                    967:
                    968:        case period:            /* treat a period kind of like a binary
                    969:                                 * operation */
                    970:            *e_code++ = '.';    /* move the period into line */
                    971:            ps.want_blank = false;      /* dont put a blank after a period */
                    972:            break;
                    973:
                    974:        case comma:
                    975:            ps.want_blank = (s_code != e_code); /* only put blank after comma
                    976:                                                 * if comma does not start the
                    977:                                                 * line */
                    978:            if (ps.in_decl && is_procname == 0 && !ps.block_init)
                    979:                while ((e_code - s_code) < (dec_ind - 1)) {
                    980:                    CHECK_SIZE_CODE;
                    981:                    *e_code++ = ' ';
                    982:                }
                    983:
                    984:            *e_code++ = ',';
                    985:            if (ps.p_l_follow == 0) {
                    986:                if (ps.block_init_level <= 0)
                    987:                    ps.block_init = 0;
                    988:                if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8))
                    989:                    force_nl = true;
                    990:            }
                    991:            break;
                    992:
                    993:        case preesc:            /* got the character '#' */
                    994:            if ((s_com != e_com) ||
                    995:                    (s_lab != e_lab) ||
                    996:                    (s_code != e_code))
                    997:                dump_line();
                    998:            *e_lab++ = '#';     /* move whole line to 'label' buffer */
                    999:            {
                   1000:                int         in_comment = 0;
                   1001:                int         com_start = 0;
                   1002:                char        quote = 0;
                   1003:                int         com_end = 0;
                   1004:
                   1005:                while (*buf_ptr == ' ' || *buf_ptr == '\t') {
                   1006:                    buf_ptr++;
                   1007:                    if (buf_ptr >= buf_end)
                   1008:                        fill_buffer();
                   1009:                }
1.8       espie    1010:                while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
1.1       deraadt  1011:                    CHECK_SIZE_LAB;
                   1012:                    *e_lab = *buf_ptr++;
                   1013:                    if (buf_ptr >= buf_end)
                   1014:                        fill_buffer();
                   1015:                    switch (*e_lab++) {
                   1016:                    case BACKSLASH:
                   1017:                        if (troff)
                   1018:                            *e_lab++ = BACKSLASH;
                   1019:                        if (!in_comment) {
                   1020:                            *e_lab++ = *buf_ptr++;
                   1021:                            if (buf_ptr >= buf_end)
                   1022:                                fill_buffer();
                   1023:                        }
                   1024:                        break;
                   1025:                    case '/':
                   1026:                        if (*buf_ptr == '*' && !in_comment && !quote) {
                   1027:                            in_comment = 1;
                   1028:                            *e_lab++ = *buf_ptr++;
                   1029:                            com_start = e_lab - s_lab - 2;
                   1030:                        }
                   1031:                        break;
                   1032:                    case '"':
                   1033:                        if (quote == '"')
                   1034:                            quote = 0;
                   1035:                        break;
                   1036:                    case '\'':
                   1037:                        if (quote == '\'')
                   1038:                            quote = 0;
                   1039:                        break;
                   1040:                    case '*':
                   1041:                        if (*buf_ptr == '/' && in_comment) {
                   1042:                            in_comment = 0;
                   1043:                            *e_lab++ = *buf_ptr++;
                   1044:                            com_end = e_lab - s_lab;
                   1045:                        }
                   1046:                        break;
                   1047:                    }
                   1048:                }
                   1049:
                   1050:                while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
                   1051:                    e_lab--;
                   1052:                if (e_lab - s_lab == com_end && bp_save == 0) { /* comment on
                   1053:                                                                 * preprocessor line */
                   1054:                    if (sc_end == 0)    /* if this is the first comment, we
                   1055:                                         * must set up the buffer */
                   1056:                        sc_end = &(save_com[0]);
                   1057:                    else {
                   1058:                        *sc_end++ = '\n';       /* add newline between
                   1059:                                                 * comments */
                   1060:                        *sc_end++ = ' ';
                   1061:                        --line_no;
                   1062:                    }
                   1063:                    bcopy(s_lab + com_start, sc_end, com_end - com_start);
                   1064:                    sc_end += com_end - com_start;
                   1065:                    if (sc_end >= &save_com[sc_size])
                   1066:                        abort();
                   1067:                    e_lab = s_lab + com_start;
                   1068:                    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
                   1069:                        e_lab--;
                   1070:                    bp_save = buf_ptr;  /* save current input buffer */
                   1071:                    be_save = buf_end;
                   1072:                    buf_ptr = save_com; /* fix so that subsequent calls to
                   1073:                                         * lexi will take tokens out of
                   1074:                                         * save_com */
                   1075:                    *sc_end++ = ' ';    /* add trailing blank, just in case */
                   1076:                    buf_end = sc_end;
                   1077:                    sc_end = 0;
                   1078:                }
                   1079:                *e_lab = '\0';  /* null terminate line */
                   1080:                ps.pcase = false;
                   1081:            }
                   1082:
                   1083:            if (strncmp(s_lab, "#if", 3) == 0) {
                   1084:                if (blanklines_around_conditional_compilation) {
1.9       pjanzen  1085:                    int    c;
1.1       deraadt  1086:                    prefix_blankline_requested++;
                   1087:                    while ((c = getc(input)) == '\n');
                   1088:                    ungetc(c, input);
                   1089:                }
                   1090:                if (ifdef_level < sizeof state_stack / sizeof state_stack[0]) {
                   1091:                    match_state[ifdef_level].tos = -1;
                   1092:                    state_stack[ifdef_level++] = ps;
                   1093:                }
                   1094:                else
                   1095:                    diag(1, "#if stack overflow");
                   1096:            }
                   1097:            else if (strncmp(s_lab, "#else", 5) == 0)
                   1098:                if (ifdef_level <= 0)
                   1099:                    diag(1, "Unmatched #else");
                   1100:                else {
                   1101:                    match_state[ifdef_level - 1] = ps;
                   1102:                    ps = state_stack[ifdef_level - 1];
                   1103:                }
                   1104:            else if (strncmp(s_lab, "#endif", 6) == 0) {
                   1105:                if (ifdef_level <= 0)
                   1106:                    diag(1, "Unmatched #endif");
                   1107:                else {
                   1108:                    ifdef_level--;
                   1109:
                   1110: #ifdef undef
                   1111:                    /*
                   1112:                     * This match needs to be more intelligent before the
                   1113:                     * message is useful
                   1114:                     */
                   1115:                    if (match_state[ifdef_level].tos >= 0
                   1116:                          && bcmp(&ps, &match_state[ifdef_level], sizeof ps))
1.13      deraadt  1117:                        diag(0, "Syntactically inconsistent #ifdef alternatives.");
1.1       deraadt  1118: #endif
                   1119:                }
                   1120:                if (blanklines_around_conditional_compilation) {
                   1121:                    postfix_blankline_requested++;
                   1122:                    n_real_blanklines = 0;
                   1123:                }
                   1124:            }
                   1125:            break;              /* subsequent processing of the newline
                   1126:                                 * character will cause the line to be printed */
                   1127:
1.4       mickey   1128:        case comment:           /* we have gotten a comment this is a biggie */
1.1       deraadt  1129:            if (flushed_nl) {   /* we should force a broken line here */
                   1130:                flushed_nl = false;
                   1131:                dump_line();
                   1132:                ps.want_blank = false;  /* dont insert blank at line start */
                   1133:                force_nl = false;
                   1134:            }
                   1135:            pr_comment();
                   1136:            break;
                   1137:        }                       /* end of big switch stmt */
                   1138:
                   1139:        *e_code = '\0';         /* make sure code section is null terminated */
                   1140:        if (type_code != comment && type_code != newline && type_code != preesc)
                   1141:            ps.last_token = type_code;
                   1142:     }                          /* end of main while (1) loop */
                   1143: }
                   1144:
                   1145: /*
                   1146:  * copy input file to backup file if in_name is /blah/blah/blah/file, then
                   1147:  * backup file will be ".Bfile" then make the backup file the input and
                   1148:  * original input file the output
                   1149:  */
1.4       mickey   1150: void
1.1       deraadt  1151: bakcopy()
                   1152: {
                   1153:     int         n,
                   1154:                 bakchn;
                   1155:     char        buff[8 * 1024];
1.9       pjanzen  1156:     char       *p;
1.1       deraadt  1157:
                   1158:     /* construct file name .Bfile */
                   1159:     for (p = in_name; *p; p++);        /* skip to end of string */
                   1160:     while (p > in_name && *p != '/')   /* find last '/' */
                   1161:        p--;
                   1162:     if (*p == '/')
                   1163:        p++;
1.9       pjanzen  1164:     if (snprintf(bakfile, MAXPATHLEN, "%s.BAK", p) >= MAXPATHLEN)
                   1165:            errx(1, "%s.BAK: %s", p, strerror(ENAMETOOLONG));
1.1       deraadt  1166:
                   1167:     /* copy in_name to backup file */
                   1168:     bakchn = creat(bakfile, 0600);
                   1169:     if (bakchn < 0)
1.7       millert  1170:        err(1, "%s", bakfile);
1.4       mickey   1171:     while ((n = read(fileno(input), buff, sizeof buff)) > 0)
1.1       deraadt  1172:        if (write(bakchn, buff, n) != n)
1.7       millert  1173:            err(1, "%s", bakfile);
1.1       deraadt  1174:     if (n < 0)
1.7       millert  1175:        err(1, "%s", in_name);
1.1       deraadt  1176:     close(bakchn);
                   1177:     fclose(input);
                   1178:
                   1179:     /* re-open backup file as the input file */
                   1180:     input = fopen(bakfile, "r");
1.4       mickey   1181:     if (input == NULL)
1.7       millert  1182:        err(1, "%s", bakfile);
1.1       deraadt  1183:     /* now the original input file will be the output */
                   1184:     output = fopen(in_name, "w");
1.4       mickey   1185:     if (output == NULL) {
1.1       deraadt  1186:        unlink(bakfile);
1.7       millert  1187:        err(1, "%s", in_name);
1.1       deraadt  1188:     }
                   1189: }