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

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