121968Sdist /* 235500Sbostic * Copyright (c) 1985 Sun Microsystems, Inc. 335500Sbostic * Copyright (c) 1980 The Regents of the University of California. 433767Sbostic * Copyright (c) 1976 Board of Trustees of the University of Illinois. 533767Sbostic * All rights reserved. 633767Sbostic * 733767Sbostic * Redistribution and use in source and binary forms are permitted 834885Sbostic * provided that the above copyright notice and this paragraph are 934885Sbostic * duplicated in all such forms and that any documentation, 1034885Sbostic * advertising materials, and other materials related to such 1134885Sbostic * distribution and use acknowledge that the software was developed 1235500Sbostic * by the University of California, Berkeley, the University of Illinois, 1335500Sbostic * Urbana, and Sun Microsystems, Inc. The name of either University 1435500Sbostic * or Sun Microsystems may not be used to endorse or promote products 1535500Sbostic * derived from this software without specific prior written permission. 1634885Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1734885Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1834885Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1921968Sdist */ 208800Smckusick 2121968Sdist #ifndef lint 2221968Sdist char copyright[] = 2335500Sbostic "@(#) Copyright (c) 1985 Sun Microsystems, Inc.\n\ 2435500Sbostic @(#) Copyright (c) 1980 The Regents of the University of California.\n\ 2535500Sbostic @(#) Copyright (c) 1976 Board of Trustees of the University of Illinois.\n\ 2621968Sdist All rights reserved.\n"; 2733767Sbostic #endif /* not lint */ 2821968Sdist 2921968Sdist #ifndef lint 30*36970Sbostic static char sccsid[] = "@(#)indent.c 5.12 (Berkeley) 03/05/89"; 3133767Sbostic #endif /* not lint */ 3221968Sdist 33*36970Sbostic #include <sys/param.h> 3435505Sbostic #include "indent_globs.h" 3535505Sbostic #include "indent_codes.h" 3635500Sbostic #include <ctype.h> 378800Smckusick 3835500Sbostic char *in_name = "Standard Input"; /* will always point to name of input 3935500Sbostic * file */ 4035500Sbostic char *out_name = "Standard Output"; /* will always point to name 4135500Sbostic * of output file */ 4235500Sbostic char bakfile[MAXPATHLEN] = ""; 438800Smckusick 4424453Smckusick main(argc, argv) 4524453Smckusick int argc; 4624453Smckusick char **argv; 478800Smckusick { 488800Smckusick 4935505Sbostic extern int found_err; /* flag set in diag() on error */ 5024453Smckusick int dec_ind; /* current indentation for declarations */ 5124453Smckusick int di_stack[20]; /* a stack of structure indentation levels */ 5235500Sbostic int flushed_nl; /* used when buffering up comments to remember 5335500Sbostic * that a newline was passed over */ 5424453Smckusick int force_nl; /* when true, code must be broken */ 5535500Sbostic int hd_type; /* used to store type of stmt for if (...), 5635500Sbostic * for (...), etc */ 5724453Smckusick register int i; /* local loop counter */ 5835500Sbostic int scase; /* set to true when we see a case, so we will 5935500Sbostic * know what to do with the following colon */ 6024453Smckusick int sp_sw; /* when true, we are in the expressin of 6124453Smckusick * if(...), while(...), etc. */ 6224453Smckusick int squest; /* when this is positive, we have seen a ? 6324453Smckusick * without the matching : in a <c>?<s>:<s> 6424453Smckusick * construct */ 6524453Smckusick register char *t_ptr; /* used for copying tokens */ 6624453Smckusick int type_code; /* the type of token, returned by lexi */ 678800Smckusick 6824453Smckusick int last_else = 0; /* true iff last keyword was an else */ 698800Smckusick 708800Smckusick 7124453Smckusick /*-----------------------------------------------*\ 7224648Smckusick | INITIALIZATION | 7324648Smckusick \*-----------------------------------------------*/ 748800Smckusick 758800Smckusick 7624453Smckusick ps.p_stack[0] = stmt; /* this is the parser's stack */ 7735500Sbostic ps.last_nl = true; /* this is true if the last thing scanned was 7835500Sbostic * a newline */ 7924453Smckusick ps.last_token = semicolon; 8035500Sbostic combuf = (char *) malloc(bufsize); 8135500Sbostic labbuf = (char *) malloc(bufsize); 8235500Sbostic codebuf = (char *) malloc(bufsize); 8335500Sbostic l_com = combuf + bufsize - 5; 8435500Sbostic l_lab = labbuf + bufsize - 5; 8535500Sbostic l_code = codebuf + bufsize - 5; 8624453Smckusick combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and 8724453Smckusick * comment buffers */ 888800Smckusick combuf[1] = codebuf[1] = labbuf[1] = '\0'; 8935500Sbostic ps.else_if = 1; /* Default else-if special processing to on */ 908800Smckusick s_lab = e_lab = labbuf + 1; 918800Smckusick s_code = e_code = codebuf + 1; 928800Smckusick s_com = e_com = combuf + 1; 938800Smckusick 948800Smckusick buf_ptr = buf_end = in_buffer; 958800Smckusick line_no = 1; 9624453Smckusick had_eof = ps.in_decl = ps.decl_on_line = break_comma = false; 978800Smckusick sp_sw = force_nl = false; 9824453Smckusick ps.in_or_st = false; 9924453Smckusick ps.bl_line = true; 1008800Smckusick dec_ind = 0; 10124453Smckusick di_stack[ps.dec_nest = 0] = 0; 10224453Smckusick ps.want_blank = ps.in_stmt = ps.ind_stmt = false; 1038800Smckusick 1048800Smckusick 10524453Smckusick scase = ps.pcase = false; 1068800Smckusick squest = 0; 1078800Smckusick sc_end = 0; 1088800Smckusick bp_save = 0; 1098800Smckusick be_save = 0; 1108800Smckusick 11124453Smckusick output = 0; 1128800Smckusick 1138800Smckusick 1148800Smckusick 11524453Smckusick /*--------------------------------------------------*\ 11635500Sbostic | COMMAND LINE SCAN | 11724648Smckusick \*--------------------------------------------------*/ 1188800Smckusick 11935500Sbostic #ifdef undef 12035500Sbostic max_col = 78; /* -l78 */ 12135500Sbostic lineup_to_parens = 1; /* -lp */ 12235500Sbostic ps.ljust_decl = 0; /* -ndj */ 12335500Sbostic ps.com_ind = 33; /* -c33 */ 12435500Sbostic star_comment_cont = 1; /* -sc */ 12535500Sbostic ps.ind_size = 8; /* -i8 */ 12635500Sbostic verbose = 0; 12735500Sbostic ps.decl_indent = 16; /* -di16 */ 12835500Sbostic ps.indent_parameters = 1; /* -ip */ 12935500Sbostic ps.decl_com_ind = 0; /* if this is not set to some positive value 13035500Sbostic * by an arg, we will set this equal to 13135500Sbostic * ps.com_ind */ 13235500Sbostic btype_2 = 1; /* -br */ 13335500Sbostic cuddle_else = 1; /* -ce */ 13435500Sbostic ps.unindent_displace = 0; /* -d0 */ 13535500Sbostic ps.case_indent = 0; /* -cli0 */ 13635500Sbostic format_col1_comments = 1; /* -fc1 */ 13735500Sbostic procnames_start_line = 1; /* -psl */ 13835500Sbostic proc_calls_space = 0; /* -npcs */ 13935500Sbostic comment_delimiter_on_blankline = 1; /* -cdb */ 14035500Sbostic ps.leave_comma = 1; /* -nbc */ 14135500Sbostic #endif 1428800Smckusick 14324453Smckusick for (i = 1; i < argc; ++i) 14424453Smckusick if (strcmp(argv[i], "-npro") == 0) 14524453Smckusick break; 14635500Sbostic set_defaults(); 14724453Smckusick if (i >= argc) 14824453Smckusick set_profile(); 1498800Smckusick 1508800Smckusick for (i = 1; i < argc; ++i) { 15124453Smckusick 15224453Smckusick /* 15335500Sbostic * look thru args (if any) for changes to defaults 15424453Smckusick */ 1558800Smckusick if (argv[i][0] != '-') {/* no flag on parameter */ 15624453Smckusick if (input == 0) { /* we must have the input file */ 15724453Smckusick in_name = argv[i]; /* remember name of input file */ 15824453Smckusick input = fopen(in_name, "r"); 159*36970Sbostic if (input == 0) /* check for open error */ 160*36970Sbostic err(in_name); 1618800Smckusick continue; 16235500Sbostic } 16335500Sbostic else if (output == 0) { /* we have the output file */ 16424453Smckusick out_name = argv[i]; /* remember name of output file */ 16524453Smckusick if (strcmp(in_name, out_name) == 0) { /* attempt to overwrite 16624453Smckusick * the file */ 16724648Smckusick fprintf(stderr, "indent: input and output files must be different\n"); 16824648Smckusick exit(1); 16924453Smckusick } 17024453Smckusick output = fopen(out_name, "w"); 171*36970Sbostic if (output == 0) /* check for create error */ 172*36970Sbostic err(out_name); 17324453Smckusick continue; 1748800Smckusick } 17524648Smckusick fprintf(stderr, "indent: unknown parameter: %s\n", argv[i]); 17624648Smckusick exit(1); 17735500Sbostic } 17835500Sbostic else 17924453Smckusick set_option(argv[i]); 18024453Smckusick } /* end of for */ 18124453Smckusick if (input == 0) { 18235500Sbostic fprintf(stderr, "indent: usage: indent file [ outfile ] [ options ]\n"); 18324648Smckusick exit(1); 18424453Smckusick } 18524453Smckusick if (output == 0) 18624453Smckusick if (troff) 18724453Smckusick output = stdout; 18824453Smckusick else { 18924453Smckusick out_name = in_name; 19024453Smckusick bakcopy(); 1918800Smckusick } 19224453Smckusick if (ps.com_ind <= 1) 19335500Sbostic ps.com_ind = 2; /* dont put normal comments before column 2 */ 19435500Sbostic if (troff) { 19535500Sbostic if (bodyf.font[0] == 0) 19635500Sbostic parsefont(&bodyf, "R"); 19735500Sbostic if (scomf.font[0] == 0) 19835500Sbostic parsefont(&scomf, "I"); 19935500Sbostic if (blkcomf.font[0] == 0) 20035500Sbostic blkcomf = scomf, blkcomf.size += 2; 20135500Sbostic if (boxcomf.font[0] == 0) 20235500Sbostic boxcomf = blkcomf; 20335500Sbostic if (stringf.font[0] == 0) 20435500Sbostic parsefont(&stringf, "L"); 20535500Sbostic if (keywordf.font[0] == 0) 20635500Sbostic parsefont(&keywordf, "B"); 20735500Sbostic writefdef(&bodyf, 'B'); 20835500Sbostic writefdef(&scomf, 'C'); 20935500Sbostic writefdef(&blkcomf, 'L'); 21035500Sbostic writefdef(&boxcomf, 'X'); 21135500Sbostic writefdef(&stringf, 'S'); 21235500Sbostic writefdef(&keywordf, 'K'); 21335500Sbostic } 21424453Smckusick if (block_comment_max_col <= 0) 21524453Smckusick block_comment_max_col = max_col; 21624453Smckusick if (ps.decl_com_ind <= 0) /* if not specified by user, set this */ 21735500Sbostic ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind; 21824453Smckusick if (continuation_indent == 0) 21924453Smckusick continuation_indent = ps.ind_size; 22035500Sbostic fill_buffer(); /* get first batch of stuff into input buffer */ 2218800Smckusick 22224453Smckusick parse(semicolon); 22324453Smckusick { 22424453Smckusick register char *p = buf_ptr; 22524453Smckusick register col = 1; 2268800Smckusick 22724453Smckusick while (1) { 22824453Smckusick if (*p == ' ') 22924453Smckusick col++; 23024453Smckusick else if (*p == '\t') 23124453Smckusick col = ((col - 1) & ~7) + 9; 23224453Smckusick else 23324453Smckusick break; 23424453Smckusick p++; 235*36970Sbostic } 23624453Smckusick if (col > ps.ind_size) 23724453Smckusick ps.ind_level = ps.i_l_follow = col / ps.ind_size; 2388800Smckusick } 23924453Smckusick if (troff) { 24024453Smckusick register char *p = in_name, 24124453Smckusick *beg = in_name; 24224453Smckusick 24324453Smckusick while (*p) 24424453Smckusick if (*p++ == '/') 24524453Smckusick beg = p; 24624453Smckusick fprintf(output, ".Fn \"%s\"\n", beg); 2478800Smckusick } 24824453Smckusick /* 24935500Sbostic * START OF MAIN LOOP 25024453Smckusick */ 2518800Smckusick 25235500Sbostic while (1) { /* this is the main loop. it will go until we 25335500Sbostic * reach eof */ 25424453Smckusick int is_procname; 2558800Smckusick 25624453Smckusick type_code = lexi(); /* lexi reads one token. The actual 25735500Sbostic * characters read are stored in "token". lexi 25835500Sbostic * returns a code indicating the type of token */ 25924453Smckusick is_procname = ps.procname[0]; 2608800Smckusick 26124453Smckusick /* 26235500Sbostic * The following code moves everything following an if (), while (), 26335500Sbostic * else, etc. up to the start of the following stmt to a buffer. This 26435500Sbostic * allows proper handling of both kinds of brace placement. 26524453Smckusick */ 2668800Smckusick 2678800Smckusick flushed_nl = false; 26824453Smckusick while (ps.search_brace) { /* if we scanned an if(), while(), 26935500Sbostic * etc., we might need to copy stuff 27035500Sbostic * into a buffer we must loop, copying 27135500Sbostic * stuff into save_com, until we find 27235500Sbostic * the start of the stmt which follows 27324453Smckusick * the if, or whatever */ 2748800Smckusick switch (type_code) { 27535500Sbostic case newline: 27635500Sbostic ++line_no; 27735500Sbostic flushed_nl = true; 27835500Sbostic case form_feed: 27935500Sbostic break; /* form feeds and newlines found here will be 28035500Sbostic * ignored */ 2818800Smckusick 28235500Sbostic case lbrace: /* this is a brace that starts the compound 28335500Sbostic * stmt */ 28435500Sbostic if (sc_end == 0) { /* ignore buffering if a comment wasnt 28535500Sbostic * stored up */ 28635500Sbostic ps.search_brace = false; 28735500Sbostic goto check_type; 28835500Sbostic } 28935500Sbostic if (btype_2) { 29035500Sbostic save_com[0] = '{'; /* we either want to put the brace 29135500Sbostic * right after the if */ 29235500Sbostic goto sw_buffer; /* go to common code to get out of 29324453Smckusick * this loop */ 29435500Sbostic } 29535500Sbostic case comment: /* we have a comment, so we must copy it into 29635500Sbostic * the buffer */ 29735500Sbostic if (!flushed_nl || sc_end != 0) { 29835500Sbostic if (sc_end == 0) { /* if this is the first comment, we 29935500Sbostic * must set up the buffer */ 30035500Sbostic save_com[0] = save_com[1] = ' '; 30135500Sbostic sc_end = &(save_com[2]); 3028800Smckusick } 30335500Sbostic else { 30435500Sbostic *sc_end++ = '\n'; /* add newline between 30524453Smckusick * comments */ 30635500Sbostic *sc_end++ = ' '; 30735500Sbostic --line_no; 30835500Sbostic } 30935500Sbostic *sc_end++ = '/'; /* copy in start of comment */ 31035500Sbostic *sc_end++ = '*'; 3118800Smckusick 31235500Sbostic for (;;) { /* loop until we get to the end of the comment */ 31335500Sbostic *sc_end = *buf_ptr++; 31435500Sbostic if (buf_ptr >= buf_end) 31535500Sbostic fill_buffer(); 31624453Smckusick 31735500Sbostic if (*sc_end++ == '*' && *buf_ptr == '/') 31835500Sbostic break; /* we are at end of comment */ 31924453Smckusick 32035500Sbostic if (sc_end >= &(save_com[sc_size])) { /* check for temp buffer 32135500Sbostic * overflow */ 32235500Sbostic diag(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever."); 32335500Sbostic fflush(output); 32435500Sbostic exit(1); 32524453Smckusick } 32624453Smckusick } 32735500Sbostic *sc_end++ = '/'; /* add ending slash */ 32835500Sbostic if (++buf_ptr >= buf_end) /* get past / in buffer */ 32935500Sbostic fill_buffer(); 33035500Sbostic break; 33135500Sbostic } 33235500Sbostic default: /* it is the start of a normal statment */ 33335500Sbostic if (flushed_nl) /* if we flushed a newline, make sure it is 33435500Sbostic * put back */ 33535500Sbostic force_nl = true; 33635500Sbostic if (type_code == sp_paren && *token == 'i' 33724453Smckusick && last_else && ps.else_if 33824453Smckusick || type_code == sp_nparen && *token == 'e' 33924453Smckusick && e_code != s_code && e_code[-1] == '}') 34035500Sbostic force_nl = false; 3418800Smckusick 34235500Sbostic if (sc_end == 0) { /* ignore buffering if comment wasnt 34335500Sbostic * saved up */ 34435500Sbostic ps.search_brace = false; 34535500Sbostic goto check_type; 34635500Sbostic } 34735500Sbostic if (force_nl) { /* if we should insert a nl here, put it into 34835500Sbostic * the buffer */ 34935500Sbostic force_nl = false; 35035500Sbostic --line_no; /* this will be re-increased when the nl is 35135500Sbostic * read from the buffer */ 35235500Sbostic *sc_end++ = '\n'; 35335500Sbostic *sc_end++ = ' '; 35435500Sbostic if (verbose && !flushed_nl) /* print error msg if the line 35535500Sbostic * was not already broken */ 35635500Sbostic diag(0, "Line broken"); 35735500Sbostic flushed_nl = false; 35835500Sbostic } 35935500Sbostic for (t_ptr = token; *t_ptr; ++t_ptr) 36035500Sbostic *sc_end++ = *t_ptr; /* copy token into temp buffer */ 36135500Sbostic ps.procname[0] = 0; 3628800Smckusick 36335500Sbostic sw_buffer: 36435500Sbostic ps.search_brace = false; /* stop looking for start of 36535500Sbostic * stmt */ 36635500Sbostic bp_save = buf_ptr; /* save current input buffer */ 36735500Sbostic be_save = buf_end; 36835500Sbostic buf_ptr = save_com; /* fix so that subsequent calls to 36924453Smckusick * lexi will take tokens out of 37024453Smckusick * save_com */ 37135500Sbostic *sc_end++ = ' ';/* add trailing blank, just in case */ 37235500Sbostic buf_end = sc_end; 37335500Sbostic sc_end = 0; 37435500Sbostic break; 37524453Smckusick } /* end of switch */ 37635500Sbostic if (type_code != 0) /* we must make this check, just in case there 37735500Sbostic * was an unexpected EOF */ 37824453Smckusick type_code = lexi(); /* read another token */ 37935500Sbostic /* if (ps.search_brace) ps.procname[0] = 0; */ 38035500Sbostic if ((is_procname = ps.procname[0]) && flushed_nl 38135500Sbostic && !procnames_start_line && ps.in_decl 38235500Sbostic && type_code == ident) 38335500Sbostic flushed_nl = 0; 38435500Sbostic } /* end of while (search_brace) */ 38524453Smckusick last_else = 0; 38624453Smckusick check_type: 38724453Smckusick if (type_code == 0) { /* we got eof */ 3888800Smckusick if (s_lab != e_lab || s_code != e_code 38935500Sbostic || s_com != e_com) /* must dump end of line */ 39024453Smckusick dump_line(); 39124453Smckusick if (ps.tos > 1) /* check for balanced braces */ 39224453Smckusick diag(1, "Stuff missing from end of file."); 3938800Smckusick 3948800Smckusick if (verbose) { 39524453Smckusick printf("There were %d output lines and %d comments\n", 39624453Smckusick ps.out_lines, ps.out_coms); 39724453Smckusick printf("(Lines with comments)/(Lines with code): %6.3f\n", 39824453Smckusick (1.0 * ps.com_lines) / code_lines); 3998800Smckusick } 40024453Smckusick fflush(output); 40135505Sbostic exit(found_err); 4028800Smckusick } 4038800Smckusick if ( 40435500Sbostic (type_code != comment) && 40535500Sbostic (type_code != newline) && 40635500Sbostic (type_code != preesc) && 40735500Sbostic (type_code != form_feed)) { 40835500Sbostic if (force_nl && 40935500Sbostic (type_code != semicolon) && 41035500Sbostic (type_code != lbrace || !btype_2)) { 41135500Sbostic /* we should force a broken line here */ 4128800Smckusick if (verbose && !flushed_nl) 41324453Smckusick diag(0, "Line broken"); 4148800Smckusick flushed_nl = false; 41524453Smckusick dump_line(); 41624453Smckusick ps.want_blank = false; /* dont insert blank at line start */ 4178800Smckusick force_nl = false; 4188800Smckusick } 41935500Sbostic ps.in_stmt = true; /* turn on flag which causes an extra level of 42035500Sbostic * indentation. this is turned off by a ; or 42135500Sbostic * '}' */ 42235500Sbostic if (s_com != e_com) { /* the turkey has embedded a comment 42335500Sbostic * in a line. fix it */ 4248800Smckusick *e_code++ = ' '; 42535500Sbostic for (t_ptr = s_com; *t_ptr; ++t_ptr) { 42635500Sbostic check_size(code); 4278800Smckusick *e_code++ = *t_ptr; 42835500Sbostic } 4298800Smckusick *e_code++ = ' '; 43024453Smckusick *e_code = '\0'; /* null terminate code sect */ 43124453Smckusick ps.want_blank = false; 4328800Smckusick e_com = s_com; 4338800Smckusick } 43435500Sbostic } 43535500Sbostic else if (type_code != comment) /* preserve force_nl thru a comment */ 43635500Sbostic force_nl = false; /* cancel forced newline after newline, form 43735500Sbostic * feed, etc */ 4388800Smckusick 4398800Smckusick 4408800Smckusick 44135500Sbostic /*-----------------------------------------------------*\ 44235500Sbostic | do switch on type of token scanned | 44335500Sbostic \*-----------------------------------------------------*/ 44435500Sbostic check_size(code); 44524453Smckusick switch (type_code) { /* now, decide what to do with the token */ 44624453Smckusick 44735500Sbostic case form_feed: /* found a form feed in line */ 44835500Sbostic ps.use_ff = true; /* a form feed is treated much like a newline */ 44935500Sbostic dump_line(); 45035500Sbostic ps.want_blank = false; 45135500Sbostic break; 45235500Sbostic 45335500Sbostic case newline: 45435500Sbostic if (ps.last_token != comma || ps.p_l_follow > 0 45535500Sbostic || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) { 45624453Smckusick dump_line(); 45724453Smckusick ps.want_blank = false; 45835500Sbostic } 45935500Sbostic ++line_no; /* keep track of input line number */ 46035500Sbostic break; 4618800Smckusick 46235500Sbostic case lparen: /* got a '(' or '[' */ 46335500Sbostic ++ps.p_l_follow; /* count parens to make Healy happy */ 46435500Sbostic if (ps.want_blank && *token != '[' && 46535500Sbostic (ps.last_token != ident || proc_calls_space 46635500Sbostic || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon)))) 46735500Sbostic *e_code++ = ' '; 46835500Sbostic if (ps.in_decl && !ps.block_init) 46935500Sbostic if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) { 47035500Sbostic ps.dumped_decl_indent = 1; 47135500Sbostic sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token); 47235500Sbostic e_code += strlen(e_code); 47324453Smckusick } 47435500Sbostic else { 47535500Sbostic while ((e_code - s_code) < dec_ind) { 47635500Sbostic check_size(code); 47735500Sbostic *e_code++ = ' '; 47835500Sbostic } 47924453Smckusick *e_code++ = token[0]; 4808800Smckusick } 48135500Sbostic else 48235500Sbostic *e_code++ = token[0]; 48335500Sbostic ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code; 48435500Sbostic if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent 48535500Sbostic && ps.paren_indents[0] < 2 * ps.ind_size) 48635500Sbostic ps.paren_indents[0] = 2 * ps.ind_size; 48735500Sbostic ps.want_blank = false; 48835500Sbostic if (ps.in_or_st && *token == '(' && ps.tos <= 2) { 48935500Sbostic /* 49035500Sbostic * this is a kluge to make sure that declarations will be 49135500Sbostic * aligned right if proc decl has an explicit type on it, i.e. 49235500Sbostic * "int a(x) {..." 49335500Sbostic */ 49435500Sbostic parse(semicolon); /* I said this was a kluge... */ 49535500Sbostic ps.in_or_st = false; /* turn off flag for structure decl or 49635500Sbostic * initialization */ 49735500Sbostic } 49835500Sbostic if (ps.sizeof_keyword) 49935500Sbostic ps.sizeof_mask |= 1 << ps.p_l_follow; 50035500Sbostic break; 5018800Smckusick 50235500Sbostic case rparen: /* got a ')' or ']' */ 50335500Sbostic if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) { 50435500Sbostic ps.last_u_d = true; 50535500Sbostic ps.cast_mask &= (1 << ps.p_l_follow) - 1; 50635500Sbostic } 50735500Sbostic ps.sizeof_mask &= (1 << ps.p_l_follow) - 1; 50835500Sbostic if (--ps.p_l_follow < 0) { 50935500Sbostic ps.p_l_follow = 0; 51035500Sbostic diag(0, "Extra %c", *token); 51135500Sbostic } 51235500Sbostic if (e_code == s_code) /* if the paren starts the line */ 51335500Sbostic ps.paren_level = ps.p_l_follow; /* then indent it */ 5148800Smckusick 51535500Sbostic *e_code++ = token[0]; 51635500Sbostic ps.want_blank = true; 5178800Smckusick 51835500Sbostic if (sp_sw && (ps.p_l_follow == 0)) { /* check for end of if 51924453Smckusick * (...), or some such */ 52035500Sbostic sp_sw = false; 52135500Sbostic force_nl = true;/* must force newline after if */ 52235500Sbostic ps.last_u_d = true; /* inform lexi that a following 52324453Smckusick * operator is unary */ 52435500Sbostic ps.in_stmt = false; /* dont use stmt continuation 52524453Smckusick * indentation */ 5268800Smckusick 52735500Sbostic parse(hd_type); /* let parser worry about if, or whatever */ 52835500Sbostic } 52935500Sbostic ps.search_brace = btype_2; /* this should insure that constructs 53035500Sbostic * such as main(){...} and int[]{...} 53135500Sbostic * have their braces put in the right 53235500Sbostic * place */ 53335500Sbostic break; 5348800Smckusick 53535500Sbostic case unary_op: /* this could be any unary operation */ 53635500Sbostic if (ps.want_blank) 53735500Sbostic *e_code++ = ' '; 5388800Smckusick 53935500Sbostic if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) { 54035500Sbostic sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token); 54135500Sbostic ps.dumped_decl_indent = 1; 54235500Sbostic e_code += strlen(e_code); 54335500Sbostic } 54435500Sbostic else { 54535500Sbostic char *res = token; 54624453Smckusick 54735500Sbostic if (ps.in_decl && !ps.block_init) { /* if this is a unary op 54824453Smckusick * in a declaration, we 54924453Smckusick * should indent this 55024453Smckusick * token */ 55135500Sbostic for (i = 0; token[i]; ++i); /* find length of token */ 55235500Sbostic while ((e_code - s_code) < (dec_ind - i)) { 55335500Sbostic check_size(code); 55435500Sbostic *e_code++ = ' '; /* pad it */ 55524453Smckusick } 5568800Smckusick } 55735500Sbostic if (troff && token[0] == '-' && token[1] == '>') 55835500Sbostic res = "\\(->"; 55935500Sbostic for (t_ptr = res; *t_ptr; ++t_ptr) { 56035500Sbostic check_size(code); 56135500Sbostic *e_code++ = *t_ptr; 56235500Sbostic } 56335500Sbostic } 56435500Sbostic ps.want_blank = false; 56535500Sbostic break; 5668800Smckusick 56735500Sbostic case binary_op: /* any binary operation */ 56835500Sbostic if (ps.want_blank) 56935500Sbostic *e_code++ = ' '; 57035500Sbostic { 57135500Sbostic char *res = token; 57224453Smckusick 57335500Sbostic if (troff) 57435500Sbostic switch (token[0]) { 57535500Sbostic case '<': 57635500Sbostic if (token[1] == '=') 57735500Sbostic res = "\\(<="; 57835500Sbostic break; 57935500Sbostic case '>': 58035500Sbostic if (token[1] == '=') 58135500Sbostic res = "\\(>="; 58235500Sbostic break; 58335500Sbostic case '!': 58435500Sbostic if (token[1] == '=') 58535500Sbostic res = "\\(!="; 58635500Sbostic break; 58735500Sbostic case '|': 58835500Sbostic if (token[1] == '|') 58935500Sbostic res = "\\(br\\(br"; 59035500Sbostic else if (token[1] == 0) 59135500Sbostic res = "\\(br"; 59235500Sbostic break; 59335500Sbostic } 59435500Sbostic for (t_ptr = res; *t_ptr; ++t_ptr) { 59535500Sbostic check_size(code); 59635500Sbostic *e_code++ = *t_ptr; /* move the operator */ 59724453Smckusick } 59835500Sbostic } 59935500Sbostic ps.want_blank = true; 60035500Sbostic break; 6018800Smckusick 60235500Sbostic case postop: /* got a trailing ++ or -- */ 60335500Sbostic *e_code++ = token[0]; 60435500Sbostic *e_code++ = token[1]; 60535500Sbostic ps.want_blank = true; 60635500Sbostic break; 6078800Smckusick 60835500Sbostic case question: /* got a ? */ 60935500Sbostic squest++; /* this will be used when a later colon 61024453Smckusick * appears so we can distinguish the 61124453Smckusick * <c>?<n>:<n> construct */ 61235500Sbostic if (ps.want_blank) 61335500Sbostic *e_code++ = ' '; 61435500Sbostic *e_code++ = '?'; 61535500Sbostic ps.want_blank = true; 61635500Sbostic break; 61735500Sbostic 61835500Sbostic case casestmt: /* got word 'case' or 'default' */ 61935500Sbostic scase = true; /* so we can process the later colon properly */ 62035500Sbostic goto copy_id; 62135500Sbostic 62235500Sbostic case colon: /* got a ':' */ 62335500Sbostic if (squest > 0) { /* it is part of the <c>?<n>: <n> construct */ 62435500Sbostic --squest; 62524453Smckusick if (ps.want_blank) 6268800Smckusick *e_code++ = ' '; 62735500Sbostic *e_code++ = ':'; 62824453Smckusick ps.want_blank = true; 6298800Smckusick break; 63035500Sbostic } 63135500Sbostic if (ps.in_decl) { 63235500Sbostic *e_code++ = ':'; 63324453Smckusick ps.want_blank = false; 6348800Smckusick break; 63535500Sbostic } 63635500Sbostic ps.in_stmt = false; /* seeing a label does not imply we are in a 63735500Sbostic * stmt */ 63835500Sbostic for (t_ptr = s_code; *t_ptr; ++t_ptr) 63935500Sbostic *e_lab++ = *t_ptr; /* turn everything so far into a label */ 64035500Sbostic e_code = s_code; 64135500Sbostic *e_lab++ = ':'; 64235500Sbostic *e_lab++ = ' '; 64335500Sbostic *e_lab = '\0'; 6448800Smckusick 64535500Sbostic force_nl = ps.pcase = scase; /* ps.pcase will be used by 64635500Sbostic * dump_line to decide how to 64735500Sbostic * indent the label. force_nl 64835500Sbostic * will force a case n: to be 64935500Sbostic * on a line by itself */ 65035500Sbostic scase = false; 65135500Sbostic ps.want_blank = false; 65235500Sbostic break; 6538800Smckusick 65435500Sbostic case semicolon: /* got a ';' */ 65535500Sbostic ps.in_or_st = false;/* we are not in an initialization or 65635500Sbostic * structure declaration */ 65735500Sbostic scase = false; /* these will only need resetting in a error */ 65835500Sbostic squest = 0; 65935500Sbostic if (ps.last_token == rparen) 66035500Sbostic ps.in_parameter_declaration = 0; 66135500Sbostic ps.cast_mask = 0; 66235500Sbostic ps.sizeof_mask = 0; 66335500Sbostic ps.block_init = 0; 66435500Sbostic ps.block_init_level = 0; 66535500Sbostic ps.just_saw_decl--; 6668800Smckusick 66735500Sbostic if (ps.in_decl && s_code == e_code && !ps.block_init) 66835500Sbostic while ((e_code - s_code) < (dec_ind - 1)) { 66935500Sbostic check_size(code); 67035500Sbostic *e_code++ = ' '; 67135500Sbostic } 6728800Smckusick 67335500Sbostic ps.in_decl = (ps.dec_nest > 0); /* if we were in a first level 67435500Sbostic * structure declaration, we 67535500Sbostic * arent any more */ 67624453Smckusick 67735500Sbostic if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) { 6788800Smckusick 67935500Sbostic /* 68035500Sbostic * This should be true iff there were unbalanced parens in the 68135500Sbostic * stmt. It is a bit complicated, because the semicolon might 68235500Sbostic * be in a for stmt 68335500Sbostic */ 68435500Sbostic diag(1, "Unbalanced parens"); 68535500Sbostic ps.p_l_follow = 0; 68635500Sbostic if (sp_sw) { /* this is a check for a if, while, etc. with 68735500Sbostic * unbalanced parens */ 68835500Sbostic sp_sw = false; 68935500Sbostic parse(hd_type); /* dont lose the if, or whatever */ 6908800Smckusick } 69135500Sbostic } 69235500Sbostic *e_code++ = ';'; 69335500Sbostic ps.want_blank = true; 69435500Sbostic ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in the 69535500Sbostic * middle of a stmt */ 6968800Smckusick 69735500Sbostic if (!sp_sw) { /* if not if for (;;) */ 69835500Sbostic parse(semicolon); /* let parser know about end of stmt */ 69935500Sbostic force_nl = true;/* force newline after a end of stmt */ 70035500Sbostic } 70135500Sbostic break; 7028800Smckusick 70335500Sbostic case lbrace: /* got a '{' */ 70435500Sbostic ps.in_stmt = false; /* dont indent the {} */ 70535500Sbostic if (!ps.block_init) 70635500Sbostic force_nl = true;/* force other stuff on same line as '{' onto 70735500Sbostic * new line */ 70835500Sbostic else if (ps.block_init_level <= 0) 70935500Sbostic ps.block_init_level = 1; 71035500Sbostic else 71135500Sbostic ps.block_init_level++; 7128800Smckusick 71335500Sbostic if (s_code != e_code && !ps.block_init) { 71435500Sbostic if (!btype_2) { 71535500Sbostic dump_line(); 71635500Sbostic ps.want_blank = false; 7178800Smckusick } 71835500Sbostic else if (ps.in_parameter_declaration && !ps.in_or_st) { 71935500Sbostic ps.i_l_follow = 0; 72035500Sbostic dump_line(); 72135500Sbostic ps.want_blank = false; 7228800Smckusick } 72335500Sbostic } 72435500Sbostic if (ps.in_parameter_declaration) 72535500Sbostic prefix_blankline_requested = 0; 7268800Smckusick 72735500Sbostic if (ps.p_l_follow > 0) { /* check for preceeding unbalanced 72835500Sbostic * parens */ 72935500Sbostic diag(1, "Unbalanced parens"); 73035500Sbostic ps.p_l_follow = 0; 73135500Sbostic if (sp_sw) { /* check for unclosed if, for, etc. */ 7328800Smckusick sp_sw = false; 73335500Sbostic parse(hd_type); 73435500Sbostic ps.ind_level = ps.i_l_follow; 7358800Smckusick } 73635500Sbostic } 73735500Sbostic if (s_code == e_code) 73835500Sbostic ps.ind_stmt = false; /* dont put extra indentation on line 73935500Sbostic * with '{' */ 74035500Sbostic if (ps.in_decl && ps.in_or_st) { /* this is either a structure 74135500Sbostic * declaration or an init */ 74235500Sbostic di_stack[ps.dec_nest++] = dec_ind; 74335500Sbostic /* ? dec_ind = 0; */ 74435500Sbostic } 74535500Sbostic else { 74635500Sbostic ps.decl_on_line = false; /* we cant be in the middle of 74735500Sbostic * a declaration, so dont do 74835500Sbostic * special indentation of 74935500Sbostic * comments */ 75035500Sbostic if (blanklines_after_declarations_at_proctop 75135500Sbostic && ps.in_parameter_declaration) 75224453Smckusick postfix_blankline_requested = 1; 75335500Sbostic ps.in_parameter_declaration = 0; 75435500Sbostic } 75535500Sbostic dec_ind = 0; 75635500Sbostic parse(lbrace); /* let parser know about this */ 75735500Sbostic if (ps.want_blank) /* put a blank before '{' if '{' is not at 75835500Sbostic * start of line */ 75935500Sbostic *e_code++ = ' '; 76035500Sbostic ps.want_blank = false; 76135500Sbostic *e_code++ = '{'; 76235500Sbostic ps.just_saw_decl = 0; 76335500Sbostic break; 7648800Smckusick 76535500Sbostic case rbrace: /* got a '}' */ 76635500Sbostic if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be 76735500Sbostic * omitted in 76835500Sbostic * declarations */ 76935500Sbostic parse(semicolon); 77035500Sbostic if (ps.p_l_follow) {/* check for unclosed if, for, else. */ 77135500Sbostic diag(1, "Unbalanced parens"); 77235500Sbostic ps.p_l_follow = 0; 77335500Sbostic sp_sw = false; 77435500Sbostic } 77535500Sbostic ps.just_saw_decl = 0; 77635500Sbostic ps.block_init_level--; 77735500Sbostic if (s_code != e_code && !ps.block_init) { /* '}' must be first on 77835500Sbostic * line */ 77935500Sbostic if (verbose) 78035500Sbostic diag(0, "Line broken"); 78135500Sbostic dump_line(); 78235500Sbostic } 78335500Sbostic *e_code++ = '}'; 78435500Sbostic ps.want_blank = true; 78535500Sbostic ps.in_stmt = ps.ind_stmt = false; 78635500Sbostic if (ps.dec_nest > 0) { /* we are in multi-level structure 78735500Sbostic * declaration */ 78835500Sbostic dec_ind = di_stack[--ps.dec_nest]; 78935500Sbostic if (ps.dec_nest == 0 && !ps.in_parameter_declaration) 79035500Sbostic ps.just_saw_decl = 2; 79135500Sbostic ps.in_decl = true; 79235500Sbostic } 79335500Sbostic prefix_blankline_requested = 0; 79435500Sbostic parse(rbrace); /* let parser know about this */ 79535500Sbostic ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead 79635500Sbostic && ps.il[ps.tos] >= ps.ind_level; 79735500Sbostic if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0) 79835500Sbostic postfix_blankline_requested = 1; 79935500Sbostic break; 8008800Smckusick 80135500Sbostic case swstmt: /* got keyword "switch" */ 80235500Sbostic sp_sw = true; 80335500Sbostic hd_type = swstmt; /* keep this for when we have seen the 80435500Sbostic * expression */ 80535500Sbostic goto copy_id; /* go move the token into buffer */ 80635500Sbostic 80735500Sbostic case sp_paren: /* token is if, while, for */ 80835500Sbostic sp_sw = true; /* the interesting stuff is done after the 80924453Smckusick * expression is scanned */ 81035500Sbostic hd_type = (*token == 'i' ? ifstmt : 81135500Sbostic (*token == 'w' ? whilestmt : forstmt)); 8128800Smckusick 81335500Sbostic /* 81435500Sbostic * remember the type of header for later use by parser 81535500Sbostic */ 81635500Sbostic goto copy_id; /* copy the token into line */ 81724453Smckusick 81835500Sbostic case sp_nparen: /* got else, do */ 81935500Sbostic ps.in_stmt = false; 82035500Sbostic if (*token == 'e') { 82135500Sbostic if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) { 82235500Sbostic if (verbose) 82335500Sbostic diag(0, "Line broken"); 82435500Sbostic dump_line();/* make sure this starts a line */ 82535500Sbostic ps.want_blank = false; 8268800Smckusick } 82735500Sbostic force_nl = true;/* also, following stuff must go onto new line */ 82835500Sbostic last_else = 1; 82935500Sbostic parse(elselit); 83035500Sbostic } 83135500Sbostic else { 83235500Sbostic if (e_code != s_code) { /* make sure this starts a line */ 83335500Sbostic if (verbose) 83435500Sbostic diag(0, "Line broken"); 83535500Sbostic dump_line(); 83635500Sbostic ps.want_blank = false; 83735500Sbostic } 83835500Sbostic force_nl = true;/* also, following stuff must go onto new line */ 83935500Sbostic last_else = 0; 84035500Sbostic parse(dolit); 84135500Sbostic } 84235500Sbostic goto copy_id; /* move the token into line */ 8438800Smckusick 84435500Sbostic case decl: /* we have a declaration type (int, register, 84535500Sbostic * etc.) */ 84635500Sbostic parse(decl); /* let parser worry about indentation */ 84735500Sbostic if (ps.last_token == rparen && ps.tos <= 1) { 84835500Sbostic ps.in_parameter_declaration = 1; 84935500Sbostic if (s_code != e_code) { 85035500Sbostic dump_line(); 85135500Sbostic ps.want_blank = 0; 85224453Smckusick } 85335500Sbostic } 85435500Sbostic if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) { 85535500Sbostic ps.ind_level = ps.i_l_follow = 1; 85635500Sbostic ps.ind_stmt = 0; 85735500Sbostic } 85835500Sbostic ps.in_or_st = true; /* this might be a structure or initialization 85935500Sbostic * declaration */ 86035500Sbostic ps.in_decl = ps.decl_on_line = true; 86135500Sbostic if ( /* !ps.in_or_st && */ ps.dec_nest <= 0) 86235500Sbostic ps.just_saw_decl = 2; 86335500Sbostic prefix_blankline_requested = 0; 86435500Sbostic for (i = 0; token[i++];); /* get length of token */ 8658800Smckusick 86635500Sbostic /* 86735500Sbostic * dec_ind = e_code - s_code + (ps.decl_indent>i ? ps.decl_indent 86835500Sbostic * : i); 86935500Sbostic */ 87035500Sbostic dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i; 87135500Sbostic goto copy_id; 8728800Smckusick 87335500Sbostic case ident: /* got an identifier or constant */ 87435500Sbostic if (ps.in_decl) { /* if we are in a declaration, we must indent 87535500Sbostic * identifier */ 87624453Smckusick if (ps.want_blank) 8778800Smckusick *e_code++ = ' '; 87835500Sbostic ps.want_blank = false; 87935500Sbostic if (is_procname == 0 || !procnames_start_line) { 88035500Sbostic if (!ps.block_init) 88135500Sbostic if (troff && !ps.dumped_decl_indent) { 88235500Sbostic sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7); 88335500Sbostic ps.dumped_decl_indent = 1; 88435500Sbostic e_code += strlen(e_code); 88535500Sbostic } 88635500Sbostic else 88735500Sbostic while ((e_code - s_code) < dec_ind) { 88835500Sbostic check_size(code); 88935500Sbostic *e_code++ = ' '; 89035500Sbostic } 89124453Smckusick } 89235500Sbostic else { 89335500Sbostic if (dec_ind && s_code != e_code) 89435500Sbostic dump_line(); 89535500Sbostic dec_ind = 0; 89635500Sbostic ps.want_blank = false; 89735500Sbostic } 89835500Sbostic } 89935500Sbostic else if (sp_sw && ps.p_l_follow == 0) { 90035500Sbostic sp_sw = false; 90135500Sbostic force_nl = true; 90235500Sbostic ps.last_u_d = true; 90335500Sbostic ps.in_stmt = false; 90435500Sbostic parse(hd_type); 90535500Sbostic } 90635500Sbostic copy_id: 90735500Sbostic if (ps.want_blank) 90835500Sbostic *e_code++ = ' '; 90935500Sbostic if (troff && ps.its_a_keyword) { 91035500Sbostic e_code = chfont(&bodyf, &keywordf, e_code); 91135500Sbostic for (t_ptr = token; *t_ptr; ++t_ptr) { 91235500Sbostic check_size(code); 91335500Sbostic *e_code++ = keywordf.allcaps && islower(*t_ptr) 91435500Sbostic ? toupper(*t_ptr) : *t_ptr; 91535500Sbostic } 91635500Sbostic e_code = chfont(&keywordf, &bodyf, e_code); 91735500Sbostic } 91835500Sbostic else 91935500Sbostic for (t_ptr = token; *t_ptr; ++t_ptr) { 92035500Sbostic check_size(code); 9218800Smckusick *e_code++ = *t_ptr; 92224453Smckusick } 92335500Sbostic ps.want_blank = true; 92435500Sbostic break; 9258800Smckusick 92635500Sbostic case period: /* treat a period kind of like a binary 92724453Smckusick * operation */ 92835500Sbostic *e_code++ = '.'; /* move the period into line */ 92935500Sbostic ps.want_blank = false; /* dont put a blank after a period */ 93035500Sbostic break; 9318800Smckusick 93235500Sbostic case comma: 93335500Sbostic ps.want_blank = (s_code != e_code); /* only put blank after comma 93435500Sbostic * if comma does not start the 93535500Sbostic * line */ 93635500Sbostic if (ps.in_decl && is_procname == 0 && !ps.block_init) 93735500Sbostic while ((e_code - s_code) < (dec_ind - 1)) { 93835500Sbostic check_size(code); 93935500Sbostic *e_code++ = ' '; 94035500Sbostic } 9418800Smckusick 94235500Sbostic *e_code++ = ','; 94335500Sbostic if (ps.p_l_follow == 0) { 94435500Sbostic if (ps.block_init_level <= 0) 94524453Smckusick ps.block_init = 0; 94635500Sbostic if (break_comma && !ps.leave_comma) 94735500Sbostic force_nl = true; 94835500Sbostic } 94935500Sbostic break; 9508800Smckusick 95135500Sbostic case preesc: /* got the character '#' */ 95235500Sbostic if ((s_com != e_com) || 95324453Smckusick (s_lab != e_lab) || 95424453Smckusick (s_code != e_code)) 95535500Sbostic dump_line(); 95635500Sbostic *e_lab++ = '#'; /* move whole line to 'label' buffer */ 95735500Sbostic { 95835500Sbostic int in_comment = 0; 95935500Sbostic int com_start = 0; 96035500Sbostic char quote = 0; 96135500Sbostic int com_end = 0; 9628800Smckusick 96335500Sbostic while (*buf_ptr != '\n' || in_comment) { 96435500Sbostic check_size(lab); 96535500Sbostic *e_lab = *buf_ptr++; 96635500Sbostic if (buf_ptr >= buf_end) 96735500Sbostic fill_buffer(); 96835500Sbostic switch (*e_lab++) { 96935500Sbostic case BACKSLASH: 97035500Sbostic if (troff) 97135500Sbostic *e_lab++ = BACKSLASH; 97235500Sbostic if (!in_comment) { 97335500Sbostic *e_lab++ = *buf_ptr++; 97435500Sbostic if (buf_ptr >= buf_end) 97535500Sbostic fill_buffer(); 97624453Smckusick } 97735500Sbostic break; 97835500Sbostic case '/': 97935500Sbostic if (*buf_ptr == '*' && !in_comment && !quote) { 98035500Sbostic in_comment = 1; 98135500Sbostic *e_lab++ = *buf_ptr++; 98235500Sbostic com_start = e_lab - s_lab - 2; 98335500Sbostic } 98435500Sbostic break; 98535500Sbostic case '"': 98635500Sbostic if (quote == '"') 98735500Sbostic quote = 0; 98835500Sbostic break; 98935500Sbostic case '\'': 99035500Sbostic if (quote == '\'') 99135500Sbostic quote = 0; 99235500Sbostic break; 99335500Sbostic case '*': 99435500Sbostic if (*buf_ptr == '/' && in_comment) { 99535500Sbostic in_comment = 0; 99635500Sbostic *e_lab++ = *buf_ptr++; 99735500Sbostic com_end = e_lab - s_lab; 99835500Sbostic } 99935500Sbostic break; 10008800Smckusick } 100135500Sbostic } 100235500Sbostic 100335500Sbostic while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 100435500Sbostic e_lab--; 100535500Sbostic if (e_lab - s_lab == com_end && bp_save == 0) { /* comment on 100624453Smckusick * preprocessor line */ 100735500Sbostic if (sc_end == 0) /* if this is the first comment, we 100835500Sbostic * must set up the buffer */ 100935500Sbostic sc_end = &(save_com[0]); 101035500Sbostic else { 101135500Sbostic *sc_end++ = '\n'; /* add newline between 101224453Smckusick * comments */ 101335500Sbostic *sc_end++ = ' '; 101435500Sbostic --line_no; 101524453Smckusick } 101635500Sbostic bcopy(s_lab + com_start, sc_end, com_end - com_start); 101735500Sbostic sc_end += com_end - com_start; 101835500Sbostic if (sc_end >= &save_com[sc_size]) 101935500Sbostic abort(); 102035500Sbostic e_lab = s_lab + com_start; 102135500Sbostic while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 102235500Sbostic e_lab--; 102335500Sbostic bp_save = buf_ptr; /* save current input buffer */ 102435500Sbostic be_save = buf_end; 102535500Sbostic buf_ptr = save_com; /* fix so that subsequent calls to 102635500Sbostic * lexi will take tokens out of 102735500Sbostic * save_com */ 102835500Sbostic *sc_end++ = ' '; /* add trailing blank, just in case */ 102935500Sbostic buf_end = sc_end; 103035500Sbostic sc_end = 0; 10318800Smckusick } 103235500Sbostic *e_lab = '\0'; /* null terminate line */ 103335500Sbostic ps.pcase = false; 103435500Sbostic } 103535500Sbostic 103635500Sbostic if (strncmp(s_lab, "#if", 3) == 0) { 103735500Sbostic if (blanklines_around_conditional_compilation) { 103835500Sbostic register c; 103935500Sbostic prefix_blankline_requested++; 104035500Sbostic while ((c = getc(input)) == '\n'); 104135500Sbostic ungetc(c, input); 104235500Sbostic } 104335500Sbostic if (ifdef_level < sizeof state_stack / sizeof state_stack[0]) { 104435500Sbostic match_state[ifdef_level].tos = -1; 104535500Sbostic state_stack[ifdef_level++] = ps; 104635500Sbostic } 104735500Sbostic else 104835500Sbostic diag(1, "#if stack overflow"); 104935500Sbostic } 105035500Sbostic else if (strncmp(s_lab, "#else", 5) == 0) 105135500Sbostic if (ifdef_level <= 0) 105235500Sbostic diag(1, "Unmatched #else"); 105335500Sbostic else { 105435500Sbostic match_state[ifdef_level - 1] = ps; 105535500Sbostic ps = state_stack[ifdef_level - 1]; 105635500Sbostic } 105735500Sbostic else if (strncmp(s_lab, "#endif", 6) == 0) { 105835500Sbostic if (ifdef_level <= 0) 105935500Sbostic diag(1, "Unmatched #endif"); 106035500Sbostic else { 106135500Sbostic ifdef_level--; 106235500Sbostic 106324453Smckusick #ifdef undef 106435500Sbostic /* 106535500Sbostic * This match needs to be more intelligent before the 106635500Sbostic * message is useful 106735500Sbostic */ 106835500Sbostic if (match_state[ifdef_level].tos >= 0 106935500Sbostic && bcmp(&ps, &match_state[ifdef_level], sizeof ps)) 107035500Sbostic diag(0, "Syntactically inconsistant #ifdef alternatives."); 107124453Smckusick #endif 107235500Sbostic } 107335500Sbostic if (blanklines_around_conditional_compilation) { 107435500Sbostic postfix_blankline_requested++; 107535500Sbostic n_real_blanklines = 0; 107635500Sbostic } 107735500Sbostic } 107835500Sbostic break; /* subsequent processing of the newline 107935500Sbostic * character will cause the line to be printed */ 10808800Smckusick 108135500Sbostic case comment: /* we have gotten a /* this is a biggie */ 108235500Sbostic if (flushed_nl) { /* we should force a broken line here */ 108335500Sbostic flushed_nl = false; 108435500Sbostic dump_line(); 108535500Sbostic ps.want_blank = false; /* dont insert blank at line start */ 108635500Sbostic force_nl = false; 108735500Sbostic } 108835500Sbostic pr_comment(); 108935500Sbostic break; 109024453Smckusick } /* end of big switch stmt */ 109135500Sbostic 109235500Sbostic *e_code = '\0'; /* make sure code section is null terminated */ 109324453Smckusick if (type_code != comment && type_code != newline && type_code != preesc) 109424453Smckusick ps.last_token = type_code; 109524453Smckusick } /* end of main while (1) loop */ 1096*36970Sbostic } 10978800Smckusick 10988800Smckusick /* 109935500Sbostic * copy input file to backup file if in_name is /blah/blah/blah/file, then 110035500Sbostic * backup file will be ".Bfile" then make the backup file the input and 110135500Sbostic * original input file the output 11028800Smckusick */ 110324453Smckusick bakcopy() 110424453Smckusick { 110524453Smckusick int n, 110624453Smckusick bakchn; 110735500Sbostic char buff[8 * 1024]; 110824453Smckusick register char *p; 11098800Smckusick 111035500Sbostic /* construct file name .Bfile */ 111135500Sbostic for (p = in_name; *p; p++); /* skip to end of string */ 111235500Sbostic while (p > in_name && *p != '/') /* find last '/' */ 111335500Sbostic p--; 111435500Sbostic if (*p == '/') 11158800Smckusick p++; 111624453Smckusick sprintf(bakfile, "%s.BAK", p); 11178800Smckusick 111824453Smckusick /* copy in_name to backup file */ 111924453Smckusick bakchn = creat(bakfile, 0600); 1120*36970Sbostic if (bakchn < 0) 1121*36970Sbostic err(bakfile); 112235500Sbostic while (n = read(fileno(input), buff, sizeof buff)) 1123*36970Sbostic if (write(bakchn, buff, n) != n) 1124*36970Sbostic err(bakfile); 1125*36970Sbostic if (n < 0) 1126*36970Sbostic err(in_name); 112724453Smckusick close(bakchn); 112824453Smckusick fclose(input); 11298800Smckusick 113024453Smckusick /* re-open backup file as the input file */ 113124453Smckusick input = fopen(bakfile, "r"); 1132*36970Sbostic if (input == 0) 1133*36970Sbostic err(bakfile); 113424453Smckusick /* now the original input file will be the output */ 113524453Smckusick output = fopen(in_name, "w"); 113635500Sbostic if (output == 0) { 113724453Smckusick unlink(bakfile); 1138*36970Sbostic err(in_name); 11398800Smckusick } 11408800Smckusick } 1141*36970Sbostic 1142*36970Sbostic err(msg) 1143*36970Sbostic char *msg; 1144*36970Sbostic { 1145*36970Sbostic extern int errno; 1146*36970Sbostic char *strerror(); 1147*36970Sbostic 1148*36970Sbostic (void)fprintf(stderr, "indent: %s: %s\n", msg, strerror(errno)); 1149*36970Sbostic exit(1); 1150*36970Sbostic } 1151