From: VZDMZA::IN%"HUBER%DM0MPI53.BITNET@vm.gmd.de" "Joseph Huber, MPI Muenchen" 19-FEB-1992 15:32:36.07 To: IN%"WIEHL@VKCMZD.Chemie.Uni-Mainz.de" CC: Subj: INDENT PART 2 of 5 Received: from vm.gmd.de by VzdmzA.ZDV.Uni-Mainz.DE (PMDF #12046) id <01GGPHW91RZ48WW2GB@VzdmzA.ZDV.Uni-Mainz.DE>; Wed, 19 Feb 1992 15:23 GMT +0100 Received: from DM0MPI11 by vm.gmd.de (IBM VM SMTP R1.2.2MX) with BSMTP id 3096; Wed, 19 Feb 92 15:25:05 CET Received: from DM0MPI53.BITNET (DM0MPI53@DM0MPI12) by DM0MPI11 (Mailer R2.08) with BSMTP id 9552; Wed, 19 Feb 92 15:25:46 GMT Date: Wed, 19 Feb 92 15:19 +0200 From: "Joseph Huber, MPI Muenchen" Subject: INDENT PART 2 of 5 To: WIEHL@VKCMZD.Chemie.Uni-Mainz.de Message-id: <01GGPHW91RZ48WW2GB@VzdmzA.ZDV.Uni-Mainz.DE> Organization: Max-Planck-Institut fuer Physik, Muenchen X-Envelope-to: WIEHL@VKCMZD.Chemie.Uni-Mainz.de X-Delivery-Notice: SMTP MAIL FROM does not correspond to sender. X-Original-To: RSCS%"WIEHL@VKCMZD.CHEMIE.UNI-MAINZ.DE" Comments: This is gatewayed mail. WARNING: Mail may not necessarily be returnable thru this path. -+-+-+-+-+-+-+-+ START OF PART 2 -+-+-+-+-+-+-+-+ X`09case unary_op:`09`09/* this could be any unary operation */ X`09 if (ps.want_blank) X`09`09*e_code++ = ' '; X X`09 if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) `7 VB X`09`09sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token); X`09`09ps.dumped_decl_indent = 1; X`09`09e_code += strlen(e_code); X`09 `7D X`09 else `7B X`09`09char *res = token; X X`09`09if (ps.in_decl && !ps.block_init) `7B`09/* if this is a unary op X`09`09`09`09`09`09`09 * in a declaration, we X`09`09`09`09`09`09`09 * should indent this X`09`09`09`09`09`09`09 * token */ X`09`09 for (i = 0; token`5Bi`5D; ++i);`09/* find length of token */ X`09`09 while ((e_code - s_code) < (dec_ind - i)) `7B X`09`09`09CHECK_SIZE_CODE; X`09`09`09*e_code++ = ' ';`09/* pad it */ X`09`09 `7D X`09`09`7D X`09`09if (troff && token`5B0`5D == '-' && token`5B1`5D == '>') X`09`09 res = "\\(->"; X`09`09for (t_ptr = res; *t_ptr; ++t_ptr) `7B X`09`09 CHECK_SIZE_CODE; X`09`09 *e_code++ = *t_ptr; X`09`09`7D X`09 `7D X`09 ps.want_blank = false; X`09 break; X X`09case binary_op:`09/* any binary operation */ X`09 if (ps.want_blank) X`09`09*e_code++ = ' '; X`09 `7B X`09`09char *res = token; X X`09`09if (troff) X`09`09 switch (token`5B0`5D) `7B X`09`09 case '<': X`09`09`09if (token`5B1`5D == '=') X`09`09`09 res = "\\(<="; X`09`09`09break; X`09`09 case '>': X`09`09`09if (token`5B1`5D == '=') X`09`09`09 res = "\\(>="; X`09`09`09break; X`09`09 case '!': X`09`09`09if (token`5B1`5D == '=') X`09`09`09 res = "\\(!="; X`09`09`09break; X`09`09 case '`7C': X`09`09`09if (token`5B1`5D == '`7C') X`09`09`09 res = "\\(br\\(br"; X`09`09`09else if (token`5B1`5D == 0) X`09`09`09 res = "\\(br"; X`09`09`09break; X`09`09 `7D X`09`09for (t_ptr = res; *t_ptr; ++t_ptr) `7B X`09`09 CHECK_SIZE_CODE; X`09`09 *e_code++ = *t_ptr;`09/* move the operator */ X`09`09`7D X`09 `7D X`09 ps.want_blank = true; X`09 break; X X`09case postop:`09`09/* got a trailing ++ or -- */ X`09 *e_code++ = token`5B0`5D; X`09 *e_code++ = token`5B1`5D; X`09 ps.want_blank = true; X`09 break; X X`09case question:`09`09/* got a ? */ X`09 squest++;`09`09/* this will be used when a later colon X`09`09`09`09 * appears so we can distinguish the X`09`09`09`09 * ?: construct */ X`09 if (ps.want_blank) X`09`09*e_code++ = ' '; X`09 *e_code++ = '?'; X`09 ps.want_blank = true; X`09 break; X X`09case casestmt:`09`09/* got word 'case' or 'default' */ X`09 scase = true;`09/* so we can process the later colon properly */ X`09 goto copy_id; X X`09case colon:`09`09/* got a ':' */ X`09 if (squest > 0) `7B`09/* it is part of the ?: construct */ X`09`09--squest; X`09`09if (ps.want_blank) X`09`09 *e_code++ = ' '; X`09`09*e_code++ = ':'; X`09`09ps.want_blank = true; X`09`09break; X`09 `7D X`09 if (ps.in_decl) `7B X`09`09*e_code++ = ':'; X`09`09ps.want_blank = false; X`09`09break; X`09 `7D X`09 ps.in_stmt = false;`09/* seeing a label does not imply we are in a X`09`09`09`09 * stmt */ X`09 for (t_ptr = s_code; *t_ptr; ++t_ptr) X`09`09*e_lab++ = *t_ptr;`09/* turn everything so far into a label */ X`09 e_code = s_code; X`09 *e_lab++ = ':'; X`09 *e_lab++ = ' '; X`09 *e_lab = '\0'; X X`09 force_nl = ps.pcase = scase;`09/* ps.pcase will be used by X`09`09`09`09`09`09 * dump_line to decide how to X`09`09`09`09`09`09 * indent the label. force_nl X`09`09`09`09`09`09 * will force a case n: to be X`09`09`09`09`09`09 * on a line by itself */ X`09 scase = false; X`09 ps.want_blank = false; X`09 break; X X`09case semicolon:`09/* got a ';' */ X`09 ps.in_or_st = false;/* we are not in an initialization or X`09`09`09`09 * structure declaration */ X`09 scase = false;`09/* these will only need resetting in a error */ X`09 squest = 0; X`09 if (ps.last_token == rparen && rparen_count == 0) X`09`09ps.in_parameter_declaration = 0; X`09 ps.cast_mask = 0; X`09 ps.sizeof_mask = 0; X`09 if (ps.block_init && !ps.ind_level && ps.ind_stmt) X`09 ps.block_init_level = -1; X`09`09else X`09 ps.block_init_level = 0; X`09 ps.block_init = 0; X`09 ps.just_saw_decl--; X X`09 if (ps.in_decl && s_code == e_code && !ps.block_init) X`09`09while ((e_code - s_code) < (dec_ind - 1)) `7B X`09`09 CHECK_SIZE_CODE; X`09`09 *e_code++ = ' '; X`09`09`7D X X`09 ps.in_decl = (ps.dec_nest > 0);`09/* if we were in a first level X`09`09`09`09`09`09 * structure declaration, we X`09`09`09`09`09`09 * arent any more */ X X`09 if ((!sp_sw `7C`7C hd_type != forstmt) && ps.p_l_follow > 0) `7B X X`09`09/* X`09`09 * This should be true iff there were unbalanced parens in the X`09`09 * stmt. It is a bit complicated, because the semicolon might X`09`09 * be in a for stmt X`09`09 */ X`09`09diag(1, "Unbalanced parens"); X`09`09ps.p_l_follow = 0; X`09`09if (sp_sw) `7B`09/* this is a check for a if, while, etc. with X`09`09`09`09 * unbalanced parens */ X`09`09 sp_sw = false; X`09`09 parse(hd_type);`09/* dont lose the if, or whatever */ X`09`09`7D X`09 `7D X`09 *e_code++ = ';'; X`09 ps.want_blank = true; X`09 ps.in_stmt = (ps.p_l_follow > 0);`09/* we are no longer in the X`09`09`09`09`09`09 * middle of a stmt */ X X`09 if (!sp_sw) `7B`09/* if not if for (;;) */ X`09`09parse(semicolon);`09/* let parser know about end of stmt */ X`09`09force_nl = true;/* force newline after a end of stmt */ X`09 `7D X`09 break; X X`09case lbrace:`09`09/* got a '`7B' */ X`09 ps.in_stmt = false;`09/* dont indent the `7B`7D */ X`09 if (!ps.block_init) X`09`09force_nl = true;/* force other stuff on same line as '`7B' onto X`09`09`09`09 * new line */ X`09 else if (ps.block_init_level <= 0) X`09`09ps.block_init_level = 1; X`09 else X`09`09ps.block_init_level++; X X`09 if (s_code != e_code && !ps.block_init) `7B X`09`09if (!btype_2) `7B X`09`09 dump_line(); X`09`09 ps.want_blank = false; X`09`09`7D X`09`09else if (ps.in_parameter_declaration && !ps.in_or_st) `7B X`09`09 ps.i_l_follow = 0; X`09`09 dump_line(); X`09`09 ps.want_blank = false; X`09`09`7D X`09 `7D X`09 if (ps.in_parameter_declaration) X`09`09prefix_blankline_requested = 0; X X`09 if (ps.p_l_follow > 0) `7B`09/* check for preceeding unbalanced X`09`09`09`09`09 * parens */ X`09`09diag(1, "Unbalanced parens"); X`09`09ps.p_l_follow = 0; X`09`09if (sp_sw) `7B`09/* check for unclosed if, for, etc. */ X`09`09 sp_sw = false; X`09`09 parse(hd_type); X`09`09 if (!ps.block_init) ps.ind_level = ps.i_l_follow; X`09`09`7D X`09 `7D X`09 if (s_code == e_code && !ps.block_init) X`09`09ps.ind_stmt = false;`09/* dont put extra indentation on line X`09`09`09`09`09 * with '`7B' */ X`09 if (ps.in_decl && ps.in_or_st) `7B`09/* this is either a structure X`09`09`09`09`09`09 * declaration or an init */ X`09`09di_stack`5Bps.dec_nest++`5D = dec_ind; X`09`09/* ?`09`09dec_ind = 0; */ X`09 `7D X`09 else `7B X`09`09ps.decl_on_line = false;`09/* we cant be in the middle of X`09`09`09`09`09`09 * a declaration, so dont do X`09`09`09`09`09`09 * special indentation of X`09`09`09`09`09`09 * comments */ X`09`09if (blank_declarations_at_proctop X`09`09`09&& ps.in_parameter_declaration) X`09`09 postfix_blankline_requested = 1; X`09`09ps.in_parameter_declaration = 0; X`09 `7D X`09 dec_ind = 0; X`09 parse(lbrace);`09/* let parser know about this */ X`09 if (ps.want_blank)`09/* put a blank before '`7B' if '`7B' is not at X`09`09`09`09 * start of line */ X`09`09*e_code++ = ' '; X`09 ps.want_blank = false; X`09 *e_code++ = '`7B'; X`09 ps.just_saw_decl = 0; X`09 break; X X`09case rbrace:`09`09/* got a '`7D' */ X`09 if (ps.p_stack`5Bps.tos`5D == decl && !ps.block_init)`09/* semicolons V can be X`09`09`09`09`09`09`09`09 * omitted in X`09`09`09`09`09`09`09`09 * declarations */ X`09`09parse(semicolon); X`09 if (ps.p_l_follow) `7B/* check for unclosed if, for, else. */ X`09`09diag(1, "Unbalanced parens"); X`09`09ps.p_l_follow = 0; X`09`09sp_sw = false; X`09 `7D X`09 ps.just_saw_decl = 0; X`09 ps.block_init_level--; X`09 if (s_code != e_code && !ps.block_init) `7B`09/* '`7D' must be first V on X`09`09`09`09`09`09`09 * line */ X`09`09if (verbose) X`09`09 diag(0, "Line broken"); X`09`09dump_line(); X`09 `7D X`09 *e_code++ = '`7D'; X`09 ps.want_blank = true; X`09 ps.in_stmt = false; X`09 if (!ps.block_init) X`09 ps.ind_stmt = false; X`09 if (ps.dec_nest > 0) `7B`09/* we are in multi-level structure X`09`09`09`09`09 * declaration */ X`09`09dec_ind = di_stack`5B--ps.dec_nest`5D; X`09`09if (ps.dec_nest == 0 && !ps.in_parameter_declaration) X`09`09 ps.just_saw_decl = 2; X`09`09ps.in_decl = true; X`09 `7D X`09 prefix_blankline_requested = 0; X`09 parse(rbrace);`09/* let parser know about this */ X`09 ps.search_brace = cuddle_else && ps.p_stack`5Bps.tos`5D == ifhead X`09`09&& ps.il`5Bps.tos`5D >= ps.ind_level; X`09 if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0) X`09`09postfix_blankline_requested = 1; X`09 break; X X`09case swstmt:`09`09/* got keyword "switch" */ X`09 sp_sw = true; X`09 hd_type = swstmt;`09/* keep this for when we have seen the X`09`09`09`09 * expression */ X`09 goto copy_id;`09/* go move the token into buffer */ X X`09case sp_paren:`09`09/* token is if, while, for */ X`09 sp_sw = true;`09/* the interesting stuff is done after the X`09`09`09`09 * expression is scanned */ X`09 hd_type = (*token == 'i' ? ifstmt : X`09`09 (*token == 'w' ? whilestmt : forstmt)); X X`09 /* X`09 * remember the type of header for later use by parser X`09 */ X`09 goto copy_id;`09/* copy the token into line */ X X`09case sp_nparen:`09/* got else, do */ X`09 ps.in_stmt = false; X`09 if (*token == 'e') `7B X`09`09if (e_code != s_code && (!cuddle_else `7C`7C e_code`5B-1`5D != '`7D')) V `7B X`09`09 if (verbose) X`09`09`09diag(0, "Line broken"); X`09`09 dump_line();/* make sure this starts a line */ X`09`09 ps.want_blank = false; X`09`09`7D X`09`09force_nl = true;/* also, following stuff must go onto new line */ X`09`09last_else = 1; X`09`09parse(elselit); X`09 `7D X`09 else `7B X`09`09if (e_code != s_code) `7B`09/* make sure this starts a line */ X`09`09 if (verbose) X`09`09`09diag(0, "Line broken"); X`09`09 dump_line(); X`09`09 ps.want_blank = false; X`09`09`7D X`09`09force_nl = true;/* also, following stuff must go onto new line */ X`09`09last_else = 0; X`09`09parse(dolit); X`09 `7D X`09 goto copy_id;`09/* move the token into line */ X X`09case decl:`09`09/* we have a declaration type (int, register, X`09`09`09`09 * etc.) */ X`09 parse(decl);`09/* let parser worry about indentation */ X`09 if (ps.last_token == rparen && ps.tos <= 1) `7B X`09`09ps.in_parameter_declaration = 1; X`09`09if (s_code != e_code) `7B X`09`09 dump_line(); X`09`09 ps.want_blank = 0; X`09`09`7D X`09 `7D X`09 if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nes Vt == 0) `7B X`09`09ps.ind_level = ps.i_l_follow = 1; X`09`09ps.ind_stmt = 0; X`09 `7D X`09 ps.in_or_st = true;`09/* this might be a structure or initialization X`09`09`09`09 * declaration */ X`09 ps.in_decl = ps.decl_on_line = true; X`09 if ( /* !ps.in_or_st && */ ps.dec_nest <= 0) X`09`09ps.just_saw_decl = 2; X`09 prefix_blankline_requested = 0; X`09 for (i = 0; token`5Bi++`5D;);`09/* get length of token */ X X`09 /* X`09 * dec_ind = e_code - s_code + (ps.decl_indent>i ? ps.decl_indent X`09 * : i); X`09 */ X/*`09 dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i; */ X`09 dec_ind = ps.decl_indent; X`09 goto copy_id; X X`09case ident:`09`09/* got an identifier or constant */ X`09 if (ps.in_decl) `7B`09/* if we are in a declaration, we must indent X`09`09`09`09 * identifier */ X`09`09if (ps.want_blank) X`09`09 *e_code++ = ' '; X`09`09ps.want_blank = false; X`09`09if (is_procname == 0 `7C`7C !procnames_start_line) `7B X`09`09 if (!ps.block_init) X`09`09`09if (troff && !ps.dumped_decl_indent) `7B X`09`09`09 sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7); X`09`09`09 ps.dumped_decl_indent = 1; X`09`09`09 e_code += strlen(e_code); X`09`09`09`7D X`09`09`09else X`09`09`09 while ((e_code - s_code) < dec_ind) `7B X`09`09`09`09CHECK_SIZE_CODE; X`09`09`09`09*e_code++ = ' '; X`09`09`09 `7D X`09`09`7D X`09`09else `7B X`09`09 if (dec_ind && s_code != e_code) X`09`09`09dump_line(); X`09`09 dec_ind = 0; X`09`09 ps.want_blank = false; X`09`09`7D X`09 `7D X`09 else if (sp_sw && ps.p_l_follow == 0) `7B X`09`09sp_sw = false; X`09`09force_nl = true; X`09`09ps.last_u_d = true; X`09`09ps.in_stmt = false; X`09`09parse(hd_type); X`09 `7D X copy_id: X`09 if (ps.want_blank) X`09`09*e_code++ = ' '; X`09 if (troff && ps.its_a_keyword) `7B X`09`09e_code = chfont(&bodyf, &keywordf, e_code); X`09`09for (t_ptr = token; *t_ptr; ++t_ptr) `7B X`09`09 CHECK_SIZE_CODE; X`09`09 *e_code++ = keywordf.allcaps && islower(*t_ptr) X`09`09`09? toupper(*t_ptr) : *t_ptr; X`09`09`7D X`09`09e_code = chfont(&keywordf, &bodyf, e_code); X`09 `7D X`09 else X`09`09for (t_ptr = token; *t_ptr; ++t_ptr) `7B X`09`09 CHECK_SIZE_CODE; X`09`09 *e_code++ = *t_ptr; X`09`09`7D X`09 if (e_code`5B-1`5D != ' ' && e_code`5B-1`5D != '\t') ps.want_blank = V true; X`09 break; X X`09case period:`09`09/* treat a period kind of like a binary X`09`09`09`09 * operation */ X`09 *e_code++ = '.';`09/* move the period into line */ X`09 ps.want_blank = false;`09/* dont put a blank after a period */ X`09 break; X X`09case comma: X`09 ps.want_blank = (s_code != e_code) /* only put blank after comma X`09`09`09`09`09 `09* if comma does not start the X`09`09`09`09`09`09* line */ X`09`09`09 & spaces_after_comma; /* -nspc */ X`09 if (ps.in_decl && is_procname == 0 && !ps.block_init) X`09`09while ((e_code - s_code) < (dec_ind - 1)) `7B X`09`09 CHECK_SIZE_CODE; X`09`09 *e_code++ = ' '; X`09`09`7D X X`09 *e_code++ = ','; X`09 if (ps.p_l_follow == 0) `7B X`09`09if (ps.block_init_level <= 0) X`09`09 ps.block_init = 0; X`09`09if (break_comma && (!ps.leave_comma `7C`7C compute_code_target() + (e_ Vcode - s_code) > max_col - 8)) X`09`09 force_nl = true; X`09 `7D X`09 break; X X`09case preesc:`09`09/* got the character '#' */ X`09 if ((s_com != e_com) `7C`7C X`09`09 (s_lab != e_lab) `7C`7C X`09`09 (s_code != e_code)) X`09`09dump_line(); X`09 *e_lab++ = '#';`09/* move whole line to 'label' buffer */ X`09 `7B X`09`09int in_comment = 0; X`09`09int com_start = 0; X`09`09char quote = 0; X`09`09int com_end = 0; X X`09`09while (*buf_ptr == ' ' `7C`7C *buf_ptr == '\t') `7B X`09`09 buf_ptr++; X`09`09 if (buf_ptr >= buf_end) X`09`09`09fill_buffer(); X`09`09`7D X`09`09while (*buf_ptr != '\n' `7C`7C in_comment) `7B X`09`09 CHECK_SIZE_LAB; X`09`09 *e_lab = *buf_ptr++; X`09`09 if (buf_ptr >= buf_end) X`09`09`09fill_buffer(); X`09`09 switch (*e_lab++) `7B X`09`09 case BACKSLASH: X`09`09`09if (troff) X`09`09`09 *e_lab++ = BACKSLASH; X`09`09`09if (!in_comment) `7B X`09`09`09 *e_lab++ = *buf_ptr++; X`09`09`09 if (buf_ptr >= buf_end) X`09`09`09`09fill_buffer(); X`09`09`09`7D X`09`09`09break; X`09`09 case '/': X`09`09`09if (*buf_ptr == '*' && !in_comment && !quote) `7B X`09`09`09 in_comment = 1; X`09`09`09 *e_lab++ = *buf_ptr++; X`09`09`09 com_start = e_lab - s_lab - 2; X`09`09`09`7D X`09`09`09break; X`09`09 case '"': X`09`09`09if (quote == '"') X`09`09`09 quote = 0; X`09`09`09break; X`09`09 case '\'': X`09`09`09if (quote == '\'') X`09`09`09 quote = 0; X`09`09`09break; X`09`09 case '*': X`09`09`09if (*buf_ptr == '/' && in_comment) `7B X`09`09`09 in_comment = 0; X`09`09`09 *e_lab++ = *buf_ptr++; X`09`09`09 com_end = e_lab - s_lab; X`09`09`09`7D X`09`09`09break; X`09`09 `7D X`09`09`7D X X`09`09while (e_lab > s_lab && (e_lab`5B-1`5D == ' ' `7C`7C e_lab`5B-1`5D == V '\t')) X`09`09 e_lab--; X`09`09if (e_lab - s_lab == com_end && bp_save == 0) `7B`09/* comment on X`09`09`09`09`09`09`09`09 * preprocessor line */ X`09`09 if (sc_end == 0)`09/* if this is the first comment, we X`09`09`09`09`09 * must set up the buffer */ X`09`09`09sc_end = &(save_com`5B0`5D); X`09`09 else `7B X`09`09`09*sc_end++ = '\n';`09/* add newline between X`09`09`09`09`09`09 * comments */ X`09`09`09*sc_end++ = ' '; X`09`09`09--line_no; X`09`09 `7D X`09`09 memcpy(sc_end,s_lab + com_start,com_end - com_start); X`09`09 sc_end += com_end - com_start; X`09`09 if (sc_end >= &save_com`5Bsc_size`5D) X`09`09`09abort(); X`09`09 e_lab = s_lab + com_start; X`09`09 while (e_lab > s_lab && (e_lab`5B-1`5D == ' ' `7C`7C e_lab`5B-1`5D V == '\t')) X`09`09`09e_lab--; X`09`09 bp_save = buf_ptr;`09/* save current input buffer */ X`09`09 be_save = buf_end; X`09`09 buf_ptr = save_com;`09/* fix so that subsequent calls to X`09`09`09`09`09 * lexi will take tokens out of X`09`09`09`09`09 * save_com */ X`09`09 *sc_end++ = ' ';`09/* add trailing blank, just in case */ X`09`09 buf_end = sc_end; X`09`09 sc_end = 0; X`09`09`7D X`09`09*e_lab = '\0';`09/* null terminate line */ X`09`09ps.pcase = false; X`09 `7D X X`09 if (strncmp(s_lab, "#if", 3) == 0) `7B X`09`09if (blank_conditional_compilation) `7B X`09`09 register c; X`09`09 prefix_blankline_requested++; X`09`09 while ((c = getc(input)) == '\n'); X`09`09 ungetc(c, input); X`09`09`7D X`09`09if (ifdef_level < sizeof state_stack / sizeof state_stack`5B0`5D) `7B X`09`09 match_state`5Bifdef_level`5D.tos = -1; X`09`09 state_stack`5Bifdef_level++`5D = ps; X`09`09`7D X`09`09else X`09`09 diag(1, "#if stack overflow"); X`09 `7D X`09 else if (strncmp(s_lab, "#else", 5) == 0) X`09`09if (ifdef_level <= 0) X`09`09 diag(1, "Unmatched #else"); X`09`09else `7B X`09`09 match_state`5Bifdef_level - 1`5D = ps; X`09`09 ps = state_stack`5Bifdef_level - 1`5D; X`09`09`7D X`09 else if (strncmp(s_lab, "#endif", 6) == 0) `7B X`09`09if (ifdef_level <= 0) X`09`09 diag(1, "Unmatched #endif"); X`09`09else `7B X`09`09 ifdef_level--; X X#ifdef undef X`09`09 /* X`09`09 * This match needs to be more intelligent before the X`09`09 * message is useful X`09`09 */ X`09`09 if (match_state`5Bifdef_level`5D.tos >= 0 X`09`09`09 && bcmp(&ps, &match_state`5Bifdef_level`5D, sizeof ps)) X`09`09`09diag(0, "Syntactically inconsistant #ifdef alternatives."); X#endif X`09`09`7D X`09`09if (blank_conditional_compilation) `7B X`09`09 postfix_blankline_requested++; X`09`09 n_real_blanklines = 0; X`09`09`7D X`09 `7D X`09 break;`09`09/* subsequent processing of the newline X`09`09`09`09 * character will cause the line to be printed */ X X`09case comment:`09`09/* we have gotten a /* this is a biggie */ X`09 if (flushed_nl) `7B`09/* we should force a broken line here */ X`09`09flushed_nl = false; X`09`09dump_line(); X`09`09ps.want_blank = false;`09/* dont insert blank at line start */ X`09`09force_nl = false; X`09 `7D X `09 com_col_start = count_spaces(1,in_buffer,buf_ptr-2); X`09 pr_comment(); X`09 break; X`09`7D`09`09`09/* end of big switch stmt */ X X`09*e_code = '\0';`09`09/* make sure code section is null terminated */ X`09if (type_code != comment && type_code != newline && type_code != preesc) X`09 ps.last_token = type_code; X `7D`09`09`09`09/* end of main while (1) loop */ X`7D X X/* X * copy input file to backup file if in_name is /blah/blah/blah/file, then X * backup file will be ".Bfile" then make the backup file the input and X * original input file the output X */ Xbakcopy() X`7B X int n, X bakchn; X char buff`5B8 * 1024`5D; X register char *p; X X /* construct file name .Bfile */ X for (p = in_name; *p; p++);`09/* skip to end of string */ X while (p > in_name && *p != '`5D' && *p != '.')`09/* find last '.' */ X`09p--; X if (*p == '.') X`09*p = 0; X sprintf(bakfile, "%s.BAK", in_name); X if (*p == 0) X`09*p = '.'; X X /* copy in_name to backup file */ X bakchn = creat(bakfile, 0600); X if (bakchn < 0) X`09err(bakfile); X while (n = read(fileno(input), buff, sizeof buff)) X`09if (write(bakchn, buff, n) != n) X`09 err(bakfile); X if (n < 0) X`09err(in_name); X close(bakchn); X fclose(input); X X /* re-open backup file as the input file */ X input = fopen(bakfile, "r"); X if (input == 0) X`09err(bakfile); X /* now the original input file will be the output */ X output = fopen(in_name, "w"); X if (output == 0) `7B X`09delete(bakfile); X`09err(in_name); X `7D X`7D X X#include errno Xerr(msg) X`09char *msg; X`7B X`09char *strerror(); X X`09(void)fprintf(stderr, "indent: %s: %s\n", msg, strerror(errno)); X`09exit(1); X`7D $ CALL UNPACK INDENT.C;36 24465157 $ create 'f' X/* X * Copyright (c) 1985 Sun Microsystems, Inc. Copyright (c) 1980 The Regents X * of the University of California. Copyright (c) 1976 Board of Trustees of X * the University of Illinois. All rights reserved. X *`20 X * Redistribution and use in source and binary forms are permitted provided X * that: (1) source distributions retain this entire copyright notice and X * comment, and (2) distributions including binaries display the following X * acknowledgement: `60`60This product includes software developed by the X * University of California, Berkeley and its contributors'' in the X * documentation or other materials provided with the distribution and in al Vl X * advertising materials mentioning features or use of this software. Neithe Vr X * the name of the University nor the names of its contributors may be used X * to endorse or promote products derived from this software without specifi Vc X * prior written permission. THIS SOFTWARE IS PROVIDED `60`60AS IS'' AND WIT VHOUT X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR X * PURPOSE. X */ X X#ifndef lint Xstatic char sccsid`5B`5D = "@(#)io.c`095.14 (Berkeley) 6/1/90"; X#endif X X#include "indent_globs.h" X#include X X Xint comment_open; Xstatic paren_target; X Xdump_line() X`7B`09`09`09`09/* dump_line is the routine that actually X`09`09`09`09 * effects the printing of the new source. It X`09`09`09`09 * prints the label section, followed by the X`09`09`09`09 * code section with the appropriate nesting X`09`09`09`09 * level, followed by any comments */ X`09register int cur_col, target_col; X`09static not_first_line; X X`09if (ps.procname`5B0`5D) `7B X`09`09if (troff) `7B X`09`09`09if (comment_open) `7B X`09`09`09`09comment_open = 0; X`09`09`09`09fprintf(output, ".*/\n"); X`09`09`09`7D X`09`09`09fprintf(output, ".Pr \"%s\"\n", ps.procname); X`09`09`7D X`09`09ps.ind_level = 0; X`09`09ps.procname`5B0`5D = 0; X`09`7D X`09if (s_code == e_code && s_lab == e_lab && s_com == e_com) `7B X`09`09if (suppress_blanklines > 0) X`09`09`09suppress_blanklines--; X`09`09else `7B X`09`09`09ps.bl_line = true; X`09`09`09n_real_blanklines++; X`09`09`7D X`09`7D else if (!inhibit_formatting) `7B X`09`09suppress_blanklines = 0; X`09`09ps.bl_line = false; X`09`09if (prefix_blankline_requested && not_first_line) X`09`09`09if (swallow_optional_blanklines) `7B X`09`09`09`09if (n_real_blanklines == 1) X`09`09`09`09`09n_real_blanklines = 0; X`09`09`09`7D else `7B X`09`09`09`09if (n_real_blanklines == 0) X`09`09`09`09`09n_real_blanklines = 1; X`09`09`09`7D X`09`09while (--n_real_blanklines >= 0) X`09`09`09putc('\n', output); X`09`09n_real_blanklines = 0; X`09`09if (ps.block_init_level == -1) X`09`09 if (e_code == s_code) ps.block_init_level = 0; X`09`09else if (!ps.block_init && ps.ind_level == 0) X`09`09`09ps.ind_stmt = 0;`09/* this is a class A kludge. X`09`09`09`09`09`09 * dont do additional X`09`09`09`09`09`09 * statement indentation if X`09`09`09`09`09`09 * we are at bracket level 0 */ X X`09`09if (e_lab != s_lab `7C`7C e_code != s_code) X`09`09`09++code_lines;`09/* keep count of lines with code */ X X X`09`09if (e_lab != s_lab) `7B`09/* print lab, if any */ X`09`09`09if (comment_open) `7B X`09`09`09`09comment_open = 0; X`09`09`09`09fprintf(output, ".*/\n"); X`09`09`09`7D X`09`09`09while (e_lab > s_lab && (e_lab`5B-1`5D == ' ' `7C`7C e_lab`5B-1`5D V == '\t')) X`09`09`09`09e_lab--; X`09`09`09cur_col = pad_output(1, compute_label_target()); X`09`09`09if (s_lab`5B0`5D == '#' && (strncmp(s_lab, "#else", 5) == 0 X`09`09`09`09 `7C`7C strncmp(s_lab, "#endif", 6) == 0)) `7B X`09`09`09`09register char *s = s_lab; X`09`09`09`09if (e_lab`5B-1`5D == '\n') X`09`09`09`09`09e_lab--; X`09`09`09`09do X`09`09`09`09`09putc(*s++, output); X`09`09`09`09while (s < e_lab && 'a' <= *s && *s <= 'z'); X`09`09`09`09while ((*s == ' ' `7C`7C *s == '\t') && s < e_lab) X`09`09`09`09`09s++; X`09`09`09`09if (s < e_lab) X`09`09`09`09`09fprintf(output, s`5B0`5D == '/' && s`5B1`5D == '*' ? "\t%.*s" V : "\t/* %.*s */", X`09`09`09`09`09`09e_lab - s, s); X`09`09`09`7D else X`09`09`09`09fprintf(output, "%.*s", e_lab - s_lab, s_lab); X`09`09`09cur_col = count_spaces(cur_col, s_lab); X`09`09`7D else X`09`09`09cur_col = 1;`09/* there is no label section */ X X`09`09ps.pcase = false; X X`09`09if (s_code != e_code) `7B`09/* print code section, if any */ X`09`09`09register char *p; X X`09`09`09if (comment_open) `7B X`09`09`09`09comment_open = 0; X`09`09`09`09fprintf(output, ".*/\n"); X`09`09`09`7D X`09`09`09target_col = compute_code_target(); X`09`09`09`7B X`09`09`09`09register i; X X`09`09`09`09for (i = 0; i < ps.p_l_follow; i++) X`09`09`09`09`09if (ps.paren_indents`5Bi`5D >= 0) X`09`09`09`09`09`09ps.paren_indents`5Bi`5D = -(ps.paren_indents`5Bi`5D + targ Vet_col); X`09`09`09`7D X`09`09`09cur_col = pad_output(cur_col, target_col); X`09`09`09for (p = s_code; p < e_code; p++) X`09`09`09`09if (*p == (char) 0200) X`09`09`09`09`09fprintf(output, "%d", target_col * 7); X`09`09`09`09else X`09`09`09`09`09putc(*p, output); X`09`09`09cur_col = count_spaces(cur_col, s_code); X`09`09`7D X`09`09if (s_com != e_com) X`09`09`09if (troff) `7B X`09`09`09`09int all_here = 0; X`09`09`09`09register char *p; X X`09`09`09`09if (e_com`5B-1`5D == '/' && e_com`5B-2`5D == '*') X`09`09`09`09`09e_com -= 2, all_here++; X`09`09`09`09while (e_com > s_com && e_com`5B-1`5D == ' ') X`09`09`09`09`09e_com--; X`09`09`09`09*e_com = 0; X`09`09`09`09p = s_com; X`09`09`09`09while (*p == ' ') X`09`09`09`09`09p++; X`09`09`09`09if (p`5B0`5D == '/' && p`5B1`5D == '*') X`09`09`09`09`09p += 2, all_here++; X`09`09`09`09else if (p`5B0`5D == '*') X`09`09`09`09`09p += p`5B1`5D == '/' ? 2 : 1; X`09`09`09`09while (*p == ' ') X`09`09`09`09`09p++; X`09`09`09`09if (*p == 0) X`09`09`09`09`09goto inhibit_newline; X`09`09`09`09if (comment_open < 2 && ps.box_com) `7B X`09`09`09`09`09comment_open = 0; X`09`09`09`09`09fprintf(output, ".*/\n"); X`09`09`09`09`7D X`09`09`09`09if (comment_open == 0) `7B X`09`09`09`09`09if ('a' <= *p && *p <= 'z') X`09`09`09`09`09`09*p = *p + 'A' - 'a'; X`09`09`09`09`09if (e_com - p < 50 && all_here == 2) `7B X`09`09`09`09`09`09register char *follow = p; X`09`09`09`09`09`09fprintf(output, "\n.nr C! \\w\1"); X`09`09`09`09`09`09while (follow < e_com) `7B X`09`09`09`09`09`09`09switch (*follow) `7B X`09`09`09`09`09`09`09case '\n': X`09`09`09`09`09`09`09`09putc(' ', output); X`09`09`09`09`09`09`09case 1: X`09`09`09`09`09`09`09`09break; X`09`09`09`09`09`09`09case '\\': X`09`09`09`09`09`09`09`09putc('\\', output); X`09`09`09`09`09`09`09default: X`09`09`09`09`09`09`09`09putc(*follow, output); X`09`09`09`09`09`09`09`7D X`09`09`09`09`09`09`09follow++; X`09`09`09`09`09`09`7D X`09`09`09`09`09`09putc(1, output); X`09`09`09`09`09`7D X`09`09`09`09`09fprintf(output, "\n./* %dp %d %dp\n", X`09`09`09`09`09`09ps.com_col * 7, X`09`09`09`09`09`09(s_code != e_code `7C`7C s_lab != e_lab) - ps.box_com, X`09`09`09`09`09`09target_col * 7); X`09`09`09`09`7D X`09`09`09`09comment_open = 1 + ps.box_com; X`09`09`09`09while (*p) `7B X`09`09`09`09`09if (*p == BACKSLASH) X`09`09`09`09`09`09putc(BACKSLASH, output); X`09`09`09`09`09putc(*p++, output); X`09`09`09`09`7D X`09`09`09`7D else `7B/* print comment, if any */ X`09`09`09`09register target = ps.com_col; X`09`09`09`09register char *com_st = s_com; X X`09`09`09`09target += ps.comment_delta; X`09`09`09`09if (ps.block_init && s_code != e_code) target = cur_col; X`09`09`09`09while (*com_st == '\t') X`09`09`09`09`09com_st++, target += 8;`09/* ? */ X`09`09`09`09while (target <= 0) X`09`09`09`09`09if (*com_st == ' ') X`09`09`09`09`09`09target++, com_st++; X`09`09`09`09`09else if (*com_st == '\t') X`09`09`09`09`09`09target = ((target - 1) & `7E7) + 9, com_st++; X`09`09`09`09`09else X`09`09`09`09`09`09target = 1; X`09`09`09`09if (cur_col > target) `7B`09/* if comment cant fit X`09`09`09`09`09`09`09 * on this line, put it X`09`09`09`09`09`09`09 * on next line */ X`09`09`09`09`09putc('\n', output); X`09`09`09`09`09cur_col = 1; X`09`09`09`09`09++ps.out_lines; X`09`09`09`09`7D X`09`09`09`09while (e_com > com_st && isspace(e_com`5B-1`5D)) X`09`09`09`09`09e_com--; X`09`09`09`09cur_col = pad_output(cur_col, target); X`09`09`09`09if (com_st`5B0`5D == '/' && com_st`5B1`5D == '*' X`09`09`09`09 && e_com - com_st > 1) com_col_actual = cur_col; X`09`09`09`09if (!ps.box_com) `7B X`09`09`09`09`09if (star_comment_cont && (com_st`5B1`5D != '*' `7C`7C e_com < V= com_st + 1)) X`09`09`09`09`09`09if (com_st`5B1`5D == ' ' && com_st`5B0`5D == ' ' && e_com V > com_st + 1) X`09`09`09`09`09`09`09com_st`5B1`5D = '*'; X`09`09`09`09`09`09else X`09`09`09`09`09`09`09fwrite(" * ", com_st`5B0`5D == '\t' ? 2 : com_st`5B0`5D V == '*' ? 1 : 3, 1, output); X`09`09`09`09`7D X`09`09`09`09fwrite(com_st, e_com - com_st, 1, output); X`09`09`09`09ps.comment_delta = ps.n_comment_delta; X`09`09`09`09cur_col = count_spaces(cur_col, com_st); X`09`09`09`09++ps.com_lines;`09/* count lines with comments */ X`09`09`09`7D X`09`09if (ps.use_ff) X`09`09`09putc('\014', output); X`09`09else X`09`09`09putc('\n', output); Xinhibit_newline: X`09`09++ps.out_lines; X`09`09if (ps.just_saw_decl == 1 && blanklines_after_declarations) `7B X`09`09`09prefix_blankline_requested = 1; X`09`09`09ps.just_saw_decl = 0; X`09`09`7D else X`09`09`09prefix_blankline_requested = postfix_blankline_requested; X`09`09postfix_blankline_requested = 0; X`09`7D X`09ps.decl_on_line = ps.in_decl;`09/* if we are in the middle of a X`09`09`09`09`09 * declaration, remember that fact X`09`09`09`09`09 * for proper comment indentation */ X/*`09ps.ind_stmt = ps.in_stmt & `7Eps.in_decl;`09 * next line should be X`09`09`09`09`09`09 * indented if we have not X`09`09`09`09`09`09 * completed this stmt and if X`09`09`09`09`09`09 * we are not in the middle X`09`09`09`09`09`09 * of a declaration */ X`09ps.ind_stmt = ps.in_stmt; X`09ps.ind_stmt = ps.ind_stmt `7C ps.block_init; X`09ps.use_ff = false; X`09ps.dumped_decl_indent = 0; X`09*(e_lab = s_lab) = '\0';/* reset buffers */ X`09*(e_code = s_code) = '\0'; X`09*(e_com = s_com) = '\0'; X`09if (!ps.block_init) ps.ind_level = ps.i_l_follow; X`09ps.paren_level = ps.p_l_follow; X`09paren_target = -ps.paren_indents`5Bps.paren_level - 1`5D; X`09not_first_line = 1; X`09return; X`7D X Xcompute_code_target() X`7B X`09register target_col = ps.ind_size * ps.ind_level + 1; X X`09if (ps.paren_level) X`09`09if (!lineup_to_parens) X`09`09`09target_col += continuation_indent * ps.paren_level; X`09`09else `7B X`09`09`09register w; X`09`09`09register t = paren_target; X X`09`09`09if ((w = count_spaces(t, s_code) - max_col) > 0 X`09`09`09 && count_spaces(target_col, s_code) <= max_col) `7B X`09`09`09`09t -= w + 1; X`09`09`09`09if (t > target_col) X`09`09`09`09`09target_col = t; X`09`09`09`7D else X`09`09`09`09target_col = t; X`09`09`7D X`09else if (ps.ind_stmt `7C`7C ps.block_init_level == -1) `7B X`09`09int i = this_indent - prev_indent; X`09`09target_col += i `7C`7C ps.block_init `7C`7C ps.block_init_level == -1` V20 X`09`09`09 ? i : continuation_indent; X`09`09if (ps.block_init_level == -1) ps.block_init_level = 0; X`09`7D X`09return target_col; X`7D X Xcompute_label_target() X`7B X`09return X`09`09ps.pcase ? (int) (case_ind * ps.ind_size) + 1 X`09`09: *s_lab == '#' ? 1 X`09`09: ps.ind_size * (ps.ind_level - label_offset) + 1; X`7D X X X/* X * Copyright (C) 1976 by the Board of Trustees of the University of Illinois X *`20 X * All rights reserved X *`20 X *`20 X * NAME: fill_buffer X *`20 X * FUNCTION: Reads one block of input into input_buffer X *`20 X * HISTORY: initial coding `09November 1976`09D A Willcox of CAC 1/7/77 A X * Willcox of CAC`09Added check for switch back to partly full input X * buffer from temporary buffer X *`20 X */ Xint Xfill_buffer() X`7B`09`09`09`09/* this routine reads stuff from the input */ X`09register char *p; X`09register int i; X`09register FILE *f = input; X X`09if (bp_save != 0) `7B`09/* there is a partly filled input buffer left */ X`09`09buf_ptr = bp_save;`09/* dont read anything, just switch X`09`09`09`09`09 * buffers */ X`09`09buf_end = be_save; X`09`09bp_save = be_save = 0; X`09`09if (buf_ptr < buf_end) X`09`09`09return;`09/* only return if there is really something X`09`09`09`09 * in this buffer */ X`09`7D X`09for (p = in_buffer;;) `7B X`09`09if (p >= in_buffer_limit) `7B X`09`09`09register size = (in_buffer_limit - in_buffer) * 2 + 10; X`09`09`09register offset = p - in_buffer; X`09`09`09in_buffer = (char *) realloc(in_buffer, size); X`09`09`09if (in_buffer == 0) X`09`09`09`09err("input line too long"); +-+-+-+-+-+-+-+- END OF PART 2 +-+-+-+-+-+-+-+-