Module Name: src Committed By: rillig Date: Fri Sep 24 18:47:29 UTC 2021
Modified Files: src/usr.bin/indent: indent.c indent_globs.h io.c lexi.c pr_comment.c Log Message: indent: group global variables for label buffer into struct No functional change. To generate a diff of this commit: cvs rdiff -u -r1.62 -r1.63 src/usr.bin/indent/indent.c cvs rdiff -u -r1.22 -r1.23 src/usr.bin/indent/indent_globs.h cvs rdiff -u -r1.53 -r1.54 src/usr.bin/indent/io.c cvs rdiff -u -r1.45 -r1.46 src/usr.bin/indent/lexi.c cvs rdiff -u -r1.36 -r1.37 src/usr.bin/indent/pr_comment.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.62 src/usr.bin/indent/indent.c:1.63 --- src/usr.bin/indent/indent.c:1.62 Fri Sep 24 18:14:06 2021 +++ src/usr.bin/indent/indent.c Fri Sep 24 18:47:29 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.62 2021/09/24 18:14:06 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.63 2021/09/24 18:47:29 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -46,7 +46,7 @@ static char sccsid[] = "@(#)indent.c 5.1 #include <sys/cdefs.h> #ifndef lint #if defined(__NetBSD__) -__RCSID("$NetBSD: indent.c,v 1.62 2021/09/24 18:14:06 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.63 2021/09/24 18:47:29 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); #endif @@ -71,17 +71,14 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/ struct options opt; struct parser_state ps; -char *labbuf; -char *s_lab; -char *e_lab; -char *l_lab; +struct buffer lab; char *codebuf; char *s_code; char *e_code; char *l_code; -struct comment_buffer com; +struct buffer com; char *tokenbuf; char *s_token; @@ -149,17 +146,17 @@ check_size_code(size_t desired_size) static void check_size_label(size_t desired_size) { - if (e_lab + (desired_size) < l_lab) + if (lab.e + (desired_size) < lab.l) return; - size_t nsize = l_lab - s_lab + 400 + desired_size; - size_t label_len = e_lab - s_lab; - labbuf = realloc(labbuf, nsize); - if (labbuf == NULL) + size_t nsize = lab.l - lab.s + 400 + desired_size; + size_t label_len = lab.e - lab.s; + lab.buf = realloc(lab.buf, nsize); + if (lab.buf == NULL) err(1, NULL); - e_lab = labbuf + label_len + 1; - l_lab = labbuf + nsize - 5; - s_lab = labbuf + 1; + lab.e = lab.buf + label_len + 1; + lab.l = lab.buf + nsize - 5; + lab.s = lab.buf + 1; } #if HAVE_CAPSICUM @@ -377,8 +374,8 @@ main_init_globals(void) com.buf = malloc(bufsize); if (com.buf == NULL) err(1, NULL); - labbuf = malloc(bufsize); - if (labbuf == NULL) + lab.buf = malloc(bufsize); + if (lab.buf == NULL) err(1, NULL); codebuf = malloc(bufsize); if (codebuf == NULL) @@ -389,14 +386,14 @@ main_init_globals(void) alloc_typenames(); init_constant_tt(); com.l = com.buf + bufsize - 5; - l_lab = labbuf + bufsize - 5; + lab.l = lab.buf + bufsize - 5; l_code = codebuf + bufsize - 5; l_token = tokenbuf + bufsize - 5; - com.buf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and + com.buf[0] = codebuf[0] = lab.buf[0] = ' '; /* set up code, label, and * comment buffers */ - com.buf[1] = codebuf[1] = labbuf[1] = tokenbuf[1] = '\0'; + com.buf[1] = codebuf[1] = lab.buf[1] = tokenbuf[1] = '\0'; opt.else_if = 1; /* Default else-if special processing to on */ - s_lab = e_lab = labbuf + 1; + lab.s = lab.e = lab.buf + 1; s_code = e_code = codebuf + 1; com.s = com.e = com.buf + 1; s_token = e_token = tokenbuf + 1; @@ -546,7 +543,7 @@ main_prepare_parsing(void) static void __attribute__((__noreturn__)) process_end_of_file(void) { - if (s_lab != e_lab || s_code != e_code || com.s != com.e) + if (lab.s != lab.e || s_code != e_code || com.s != com.e) dump_line(); if (ps.tos > 1) /* check for balanced braces */ @@ -790,10 +787,10 @@ process_colon(int *inout_squest, int *in size_t len = e_code - s_code; check_size_label(len + 3); - memcpy(e_lab, s_code, len); - e_lab += len; - *e_lab++ = ':'; - *e_lab = '\0'; + memcpy(lab.e, s_code, len); + lab.e += len; + *lab.e++ = ':'; + *lab.e = '\0'; e_code = s_code; } *inout_force_nl = ps.pcase = *inout_scase; /* ps.pcase will be used by @@ -1120,10 +1117,10 @@ process_comma(int dec_ind, int tabs_to_v static void process_preprocessing(void) { - if (com.s != com.e || s_lab != e_lab || s_code != e_code) + if (com.s != com.e || lab.s != lab.e || s_code != e_code) dump_line(); check_size_label(1); - *e_lab++ = '#'; /* move whole line to 'label' buffer */ + *lab.e++ = '#'; /* move whole line to 'label' buffer */ { int in_comment = 0; @@ -1138,13 +1135,13 @@ process_preprocessing(void) } while (*buf_ptr != '\n' || (in_comment && !had_eof)) { check_size_label(2); - *e_lab = *buf_ptr++; + *lab.e = *buf_ptr++; if (buf_ptr >= buf_end) fill_buffer(); - switch (*e_lab++) { + switch (*lab.e++) { case '\\': if (!in_comment) { - *e_lab++ = *buf_ptr++; + *lab.e++ = *buf_ptr++; if (buf_ptr >= buf_end) fill_buffer(); } @@ -1152,8 +1149,8 @@ process_preprocessing(void) case '/': if (*buf_ptr == '*' && !in_comment && quote == '\0') { in_comment = 1; - *e_lab++ = *buf_ptr++; - com_start = (int)(e_lab - s_lab) - 2; + *lab.e++ = *buf_ptr++; + com_start = (int)(lab.e - lab.s) - 2; } break; case '"': @@ -1171,16 +1168,16 @@ process_preprocessing(void) case '*': if (*buf_ptr == '/' && in_comment) { in_comment = 0; - *e_lab++ = *buf_ptr++; - com_end = (int)(e_lab - s_lab); + *lab.e++ = *buf_ptr++; + com_end = (int)(lab.e - lab.s); } break; } } - while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) - e_lab--; - if (e_lab - s_lab == com_end && bp_save == NULL) { + while (lab.e > lab.s && (lab.e[-1] == ' ' || lab.e[-1] == '\t')) + lab.e--; + if (lab.e - lab.s == com_end && bp_save == NULL) { /* comment on preprocessor line */ if (sc_end == NULL) { /* if this is the first comment, * we must set up the buffer */ @@ -1194,11 +1191,11 @@ process_preprocessing(void) } if (sc_end - save_com + com_end - com_start > sc_size) errx(1, "input too long"); - memmove(sc_end, s_lab + com_start, (size_t)(com_end - com_start)); + memmove(sc_end, lab.s + com_start, (size_t)(com_end - com_start)); sc_end += com_end - com_start; - e_lab = s_lab + com_start; - while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) - e_lab--; + lab.e = lab.s + com_start; + while (lab.e > lab.s && (lab.e[-1] == ' ' || lab.e[-1] == '\t')) + lab.e--; bp_save = buf_ptr; /* save current input buffer */ be_save = buf_end; buf_ptr = save_com; /* fix so that subsequent calls to lexi will @@ -1209,35 +1206,35 @@ process_preprocessing(void) debug_println("switched buf_ptr to save_com"); } check_size_label(1); - *e_lab = '\0'; /* null terminate line */ + *lab.e = '\0'; /* null terminate line */ ps.pcase = false; } - if (strncmp(s_lab, "#if", 3) == 0) { /* also ifdef, ifndef */ + if (strncmp(lab.s, "#if", 3) == 0) { /* also ifdef, ifndef */ if ((size_t)ifdef_level < nitems(state_stack)) { match_state[ifdef_level].tos = -1; state_stack[ifdef_level++] = ps; } else diag(1, "#if stack overflow"); - } else if (strncmp(s_lab, "#el", 3) == 0) { /* else, elif */ + } else if (strncmp(lab.s, "#el", 3) == 0) { /* else, elif */ if (ifdef_level <= 0) - diag(1, s_lab[3] == 'i' ? "Unmatched #elif" : "Unmatched #else"); + diag(1, lab.s[3] == 'i' ? "Unmatched #elif" : "Unmatched #else"); else { match_state[ifdef_level - 1] = ps; ps = state_stack[ifdef_level - 1]; } - } else if (strncmp(s_lab, "#endif", 6) == 0) { + } else if (strncmp(lab.s, "#endif", 6) == 0) { if (ifdef_level <= 0) diag(1, "Unmatched #endif"); else ifdef_level--; } else { - if (strncmp(s_lab + 1, "pragma", 6) != 0 && - strncmp(s_lab + 1, "error", 5) != 0 && - strncmp(s_lab + 1, "line", 4) != 0 && - strncmp(s_lab + 1, "undef", 5) != 0 && - strncmp(s_lab + 1, "define", 6) != 0 && - strncmp(s_lab + 1, "include", 7) != 0) { + if (strncmp(lab.s + 1, "pragma", 6) != 0 && + strncmp(lab.s + 1, "error", 5) != 0 && + strncmp(lab.s + 1, "line", 4) != 0 && + strncmp(lab.s + 1, "undef", 5) != 0 && + strncmp(lab.s + 1, "define", 6) != 0 && + strncmp(lab.s + 1, "include", 7) != 0) { diag(1, "Unrecognized cpp directive"); return; } Index: src/usr.bin/indent/indent_globs.h diff -u src/usr.bin/indent/indent_globs.h:1.22 src/usr.bin/indent/indent_globs.h:1.23 --- src/usr.bin/indent/indent_globs.h:1.22 Fri Sep 24 18:14:06 2021 +++ src/usr.bin/indent/indent_globs.h Fri Sep 24 18:47:29 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: indent_globs.h,v 1.22 2021/09/24 18:14:06 rillig Exp $ */ +/* $NetBSD: indent_globs.h,v 1.23 2021/09/24 18:47:29 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -49,26 +49,24 @@ #define false 0 #define true 1 +struct buffer { + char *buf; /* buffer */ + char *s; /* start */ + char *e; /* end */ + char *l; /* limit */ +}; extern FILE *input; /* the fid for the input file */ extern FILE *output; /* the output file */ -extern char *labbuf; /* buffer for label */ -extern char *s_lab; /* start ... */ -extern char *e_lab; /* .. and end of stored label */ -extern char *l_lab; /* limit of label buffer */ +extern struct buffer lab; /* buffer for label */ extern char *codebuf; /* buffer for code section */ extern char *s_code; /* start ... */ extern char *e_code; /* .. and end of stored code */ extern char *l_code; /* limit of code section */ -extern struct comment_buffer { - char *buf; /* buffer for comments */ - char *s; /* start ... */ - char *e; /* ... and end of stored comments */ - char *l; /* limit of comment buffer */ -} com; +extern struct buffer com; /* comment */ #define token s_token extern char *tokenbuf; /* the last token scanned */ Index: src/usr.bin/indent/io.c diff -u src/usr.bin/indent/io.c:1.53 src/usr.bin/indent/io.c:1.54 --- src/usr.bin/indent/io.c:1.53 Fri Sep 24 18:37:03 2021 +++ src/usr.bin/indent/io.c Fri Sep 24 18:47:29 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: io.c,v 1.53 2021/09/24 18:37:03 rillig Exp $ */ +/* $NetBSD: io.c,v 1.54 2021/09/24 18:47:29 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -46,7 +46,7 @@ static char sccsid[] = "@(#)io.c 8.1 (Be #include <sys/cdefs.h> #ifndef lint #if defined(__NetBSD__) -__RCSID("$NetBSD: io.c,v 1.53 2021/09/24 18:37:03 rillig Exp $"); +__RCSID("$NetBSD: io.c,v 1.54 2021/09/24 18:47:29 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $"); #endif @@ -123,7 +123,7 @@ dump_line(void) ps.procname[0] = 0; } - if (s_code == e_code && s_lab == e_lab && com.s == com.e) { + if (s_code == e_code && lab.s == lab.e && com.s == com.e) { if (suppress_blanklines > 0) suppress_blanklines--; else { @@ -150,41 +150,41 @@ dump_line(void) * additional statement indentation if we are * at bracket level 0 */ - if (e_lab != s_lab || e_code != s_code) + if (lab.e != lab.s || e_code != s_code) ++code_lines; /* keep count of lines with code */ - if (e_lab != s_lab) { /* print lab, if any */ + if (lab.e != lab.s) { /* print lab, if any */ if (comment_open) { comment_open = 0; output_string(".*/\n"); } - while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) - e_lab--; - *e_lab = '\0'; + while (lab.e > lab.s && (lab.e[-1] == ' ' || lab.e[-1] == '\t')) + lab.e--; + *lab.e = '\0'; cur_col = 1 + output_indent(0, compute_label_indent()); - if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0 - || strncmp(s_lab, "#endif", 6) == 0)) { - char *s = s_lab; - if (e_lab[-1] == '\n') e_lab--; + if (lab.s[0] == '#' && (strncmp(lab.s, "#else", 5) == 0 + || strncmp(lab.s, "#endif", 6) == 0)) { + char *s = lab.s; + if (lab.e[-1] == '\n') lab.e--; do { output_char(*s++); - } while (s < e_lab && 'a' <= *s && *s <= 'z'); - while ((*s == ' ' || *s == '\t') && s < e_lab) + } while (s < lab.e && 'a' <= *s && *s <= 'z'); + while ((*s == ' ' || *s == '\t') && s < lab.e) s++; - if (s < e_lab) { + if (s < lab.e) { if (s[0] == '/' && s[1] == '*') { output_char('\t'); - output_range(s, e_lab); + output_range(s, lab.e); } else { output_string("\t/* "); - output_range(s, e_lab); + output_range(s, lab.e); output_string(" */"); } } } else - output_range(s_lab, e_lab); - cur_col = 1 + indentation_after(cur_col - 1, s_lab); + output_range(lab.s, lab.e); + cur_col = 1 + indentation_after(cur_col - 1, lab.s); } else cur_col = 1; /* there is no label section */ @@ -272,7 +272,7 @@ dump_line(void) * are not in the middle of a declaration */ ps.use_ff = false; ps.dumped_decl_indent = 0; - *(e_lab = s_lab) = '\0'; /* reset buffers */ + *(lab.e = lab.s) = '\0'; /* reset buffers */ *(e_code = s_code) = '\0'; *(com.e = com.s = com.buf + 1) = '\0'; ps.ind_level = ps.i_l_follow; @@ -325,7 +325,7 @@ compute_label_indent(void) { if (ps.pcase) return (int) (case_ind * opt.indent_size); - if (s_lab[0] == '#') + if (lab.s[0] == '#') return 0; return opt.indent_size * (ps.ind_level - label_offset); } @@ -375,7 +375,7 @@ parse_indent_comment(void) if (!(p[0] == '*' && p[1] == '/' && p[2] == '\n')) return; - if (com.s != com.e || s_lab != e_lab || s_code != e_code) + if (com.s != com.e || lab.s != lab.e || s_code != e_code) dump_line(); if (!(inhibit_formatting = on_off - 1)) { Index: src/usr.bin/indent/lexi.c diff -u src/usr.bin/indent/lexi.c:1.45 src/usr.bin/indent/lexi.c:1.46 --- src/usr.bin/indent/lexi.c:1.45 Fri Sep 24 18:14:06 2021 +++ src/usr.bin/indent/lexi.c Fri Sep 24 18:47:29 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: lexi.c,v 1.45 2021/09/24 18:14:06 rillig Exp $ */ +/* $NetBSD: lexi.c,v 1.46 2021/09/24 18:47:29 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -46,7 +46,7 @@ static char sccsid[] = "@(#)lexi.c 8.1 ( #include <sys/cdefs.h> #ifndef lint #if defined(__NetBSD__) -__RCSID("$NetBSD: lexi.c,v 1.45 2021/09/24 18:14:06 rillig Exp $"); +__RCSID("$NetBSD: lexi.c,v 1.46 2021/09/24 18:47:29 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $"); #endif @@ -273,7 +273,7 @@ lexi_end(token_type code) debug_printf("in line %d, lexi returns '%s'", line_no, token_type_name(code)); print_buf("token", s_token, e_token); - print_buf("label", s_lab, e_lab); + print_buf("label", lab.s, lab.e); print_buf("code", s_code, e_code); print_buf("comment", com.s, com.e); debug_printf("\n"); Index: src/usr.bin/indent/pr_comment.c diff -u src/usr.bin/indent/pr_comment.c:1.36 src/usr.bin/indent/pr_comment.c:1.37 --- src/usr.bin/indent/pr_comment.c:1.36 Fri Sep 24 18:14:06 2021 +++ src/usr.bin/indent/pr_comment.c Fri Sep 24 18:47:29 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: pr_comment.c,v 1.36 2021/09/24 18:14:06 rillig Exp $ */ +/* $NetBSD: pr_comment.c,v 1.37 2021/09/24 18:47:29 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -46,7 +46,7 @@ static char sccsid[] = "@(#)pr_comment.c #include <sys/cdefs.h> #ifndef lint #if defined(__NetBSD__) -__RCSID("$NetBSD: pr_comment.c,v 1.36 2021/09/24 18:14:06 rillig Exp $"); +__RCSID("$NetBSD: pr_comment.c,v 1.37 2021/09/24 18:47:29 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $"); #endif @@ -130,7 +130,7 @@ process_comment(void) * is nonzero (the default). */ break_delim = false; } - if ( /* ps.bl_line && */ s_lab == e_lab && s_code == e_code) { + if ( /* ps.bl_line && */ lab.s == lab.e && s_code == e_code) { /* klg: check only if this line is blank */ /* * If this (*and previous lines are*) blank, dont put comment way @@ -146,8 +146,8 @@ process_comment(void) int target_col; if (s_code != e_code) target_col = 1 + indentation_after(compute_code_indent(), s_code); - else if (s_lab != e_lab) - target_col = 1 + indentation_after(compute_label_indent(), s_lab); + else if (lab.s != lab.e) + target_col = 1 + indentation_after(compute_label_indent(), lab.s); else target_col = 1;