Module Name: src Committed By: rillig Date: Fri Nov 19 15:28:32 UTC 2021
Modified Files: src/usr.bin/indent: indent.c indent.h io.c lexi.c pr_comment.c Log Message: indent: group variables for input handling No functional change. To generate a diff of this commit: cvs rdiff -u -r1.218 -r1.219 src/usr.bin/indent/indent.c cvs rdiff -u -r1.86 -r1.87 src/usr.bin/indent/indent.h cvs rdiff -u -r1.116 -r1.117 src/usr.bin/indent/io.c \ src/usr.bin/indent/pr_comment.c cvs rdiff -u -r1.139 -r1.140 src/usr.bin/indent/lexi.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.218 src/usr.bin/indent/indent.c:1.219 --- src/usr.bin/indent/indent.c:1.218 Sun Nov 7 19:18:56 2021 +++ src/usr.bin/indent/indent.c Fri Nov 19 15:28:32 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.218 2021/11/07 19:18:56 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.219 2021/11/19 15:28:32 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c 5.1 #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: indent.c,v 1.218 2021/11/07 19:18:56 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.219 2021/11/19 15:28:32 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); #endif @@ -88,7 +88,7 @@ struct options opt = { struct parser_state ps; -struct buffer inp; +struct input_buffer inbuf; struct buffer token; @@ -96,13 +96,6 @@ struct buffer lab; struct buffer code; struct buffer com; -char sc_buf[sc_size]; -char *save_com; -static char *sc_end; /* pointer into save_com buffer */ - -char *saved_inp_s; -char *saved_inp_e; - bool found_err; int blank_lines_to_output; bool blank_line_before; @@ -229,7 +222,7 @@ static void debug_save_com(const char *prefix) { debug_printf("%s: save_com is ", prefix); - debug_vis_range("\"", save_com, sc_end, "\"\n"); + debug_vis_range("\"", inbuf.save_com, inbuf.sc_end, "\"\n"); } #else #define debug_save_com(prefix) do { } while (false) @@ -238,7 +231,7 @@ debug_save_com(const char *prefix) static void sc_check_size(size_t n) { - if ((size_t)(sc_end - sc_buf) + n <= sc_size) + if ((size_t)(inbuf.sc_end - inbuf.sc_buf) + n <= sc_size) return; diag(1, "Internal buffer overflow - " @@ -251,7 +244,7 @@ static void sc_add_char(char ch) { sc_check_size(1); - *sc_end++ = ch; + *inbuf.sc_end++ = ch; } static void @@ -259,17 +252,17 @@ sc_add_range(const char *s, const char * { size_t len = (size_t)(e - s); sc_check_size(len); - memcpy(sc_end, s, len); - sc_end += len; + memcpy(inbuf.sc_end, s, len); + inbuf.sc_end += len; } static void search_stmt_newline(bool *force_nl) { - if (sc_end == NULL) { - save_com = sc_buf; - save_com[0] = save_com[1] = ' '; - sc_end = &save_com[2]; + if (inbuf.sc_end == NULL) { + inbuf.save_com = inbuf.sc_buf; + inbuf.save_com[0] = inbuf.save_com[1] = ' '; + inbuf.sc_end = &inbuf.save_com[2]; debug_save_com("search_stmt_newline init"); } sc_add_char('\n'); @@ -291,7 +284,7 @@ search_stmt_newline(bool *force_nl) static void search_stmt_comment(void) { - if (sc_end == NULL) { + if (inbuf.sc_end == NULL) { /* * Copy everything from the start of the line, because * process_comment() will use that to calculate the original @@ -303,26 +296,26 @@ search_stmt_comment(void) * is an additional line break before the ')', memcpy tries to copy * (size_t)-1 bytes. */ - assert((size_t)(inp.s - inp.buf) >= 4); - size_t line_len = (size_t)(inp.s - inp.buf) - 4; - assert(line_len < array_length(sc_buf)); - memcpy(sc_buf, inp.buf, line_len); - save_com = sc_buf + line_len; - save_com[0] = save_com[1] = ' '; - sc_end = &save_com[2]; + assert((size_t)(inbuf.inp.s - inbuf.inp.buf) >= 4); + size_t line_len = (size_t)(inbuf.inp.s - inbuf.inp.buf) - 4; + assert(line_len < array_length(inbuf.sc_buf)); + memcpy(inbuf.sc_buf, inbuf.inp.buf, line_len); + inbuf.save_com = inbuf.sc_buf + line_len; + inbuf.save_com[0] = inbuf.save_com[1] = ' '; + inbuf.sc_end = &inbuf.save_com[2]; debug_vis_range("search_stmt_comment: before save_com is \"", - sc_buf, save_com, "\"\n"); + inbuf.sc_buf, inbuf.save_com, "\"\n"); debug_vis_range("search_stmt_comment: save_com is \"", - save_com, sc_end, "\"\n"); + inbuf.save_com, inbuf.sc_end, "\"\n"); } sc_add_range(token.s, token.e); if (token.e[-1] == '/') { - while (inp.s[0] != '\n') + while (inbuf.inp.s[0] != '\n') sc_add_char(inp_next()); debug_save_com("search_stmt_comment end C99"); } else { - while (!(sc_end[-2] == '*' && sc_end[-1] == '/')) + while (!(inbuf.sc_end[-2] == '*' && inbuf.sc_end[-1] == '/')) sc_add_char(inp_next()); debug_save_com("search_stmt_comment end block"); } @@ -335,17 +328,17 @@ search_stmt_lbrace(void) * Put KNF-style lbraces before the buffered up tokens and jump out of * this loop in order to avoid copying the token again. */ - if (sc_end != NULL && opt.brace_same_line) { - assert(save_com[0] == ' '); /* see search_stmt_comment */ - save_com[0] = '{'; + if (inbuf.sc_end != NULL && opt.brace_same_line) { + assert(inbuf.save_com[0] == ' '); /* see search_stmt_comment */ + inbuf.save_com[0] = '{'; /* * Originally the lbrace may have been alone on its own line, but it * will be moved into "the else's line", so if there was a newline * resulting from the "{" before, it must be scanned now and ignored. */ - while (isspace((unsigned char)*inp.s)) { + while (isspace((unsigned char)*inbuf.inp.s)) { inp_skip(); - if (*inp.s == '\n') + if (*inbuf.inp.s == '\n') break; } debug_save_com(__func__); @@ -368,21 +361,21 @@ search_stmt_other(lexer_symbol lsym, boo if (remove_newlines) *force_nl = false; - if (sc_end == NULL) { /* ignore buffering if comment wasn't saved + if (inbuf.sc_end == NULL) { /* ignore buffering if comment wasn't saved * up */ ps.search_stmt = false; return false; } debug_save_com(__func__); - while (sc_end > save_com && ch_isblank(sc_end[-1])) - sc_end--; + while (inbuf.sc_end > inbuf.save_com && ch_isblank(inbuf.sc_end[-1])) + inbuf.sc_end--; if (opt.swallow_optional_blanklines || (!comment_buffered && remove_newlines)) { *force_nl = !remove_newlines; - while (sc_end > save_com && sc_end[-1] == '\n') - sc_end--; + while (inbuf.sc_end > inbuf.save_com && inbuf.sc_end[-1] == '\n') + inbuf.sc_end--; } if (*force_nl) { /* if we should insert a nl here, put it into @@ -409,12 +402,12 @@ switch_buffer(void) sc_add_char(' '); /* add trailing blank, just in case */ debug_save_com(__func__); - saved_inp_s = inp.s; - saved_inp_e = inp.e; + inbuf.saved_inp_s = inbuf.inp.s; + inbuf.saved_inp_e = inbuf.inp.e; - inp.s = save_com; /* redirect lexi input to save_com */ - inp.e = sc_end; - sc_end = NULL; + inbuf.inp.s = inbuf.save_com; /* redirect lexi input to save_com */ + inbuf.inp.e = inbuf.sc_end; + inbuf.sc_end = NULL; debug_println("switched inp.s to save_com"); } @@ -441,8 +434,8 @@ search_stmt_lookahead(lexer_symbol *lsym * Work around the latter problem by copying all whitespace characters * into the buffer so that the later lexi() call will read them. */ - if (sc_end != NULL) { - while (ch_isblank(*inp.s)) + if (inbuf.sc_end != NULL) { + while (ch_isblank(*inbuf.inp.s)) sc_add_char(inp_next()); debug_save_com(__func__); } @@ -500,10 +493,10 @@ search_stmt(lexer_symbol *lsym, bool *fo static void main_init_globals(void) { - inp.buf = xmalloc(10); - inp.l = inp.buf + 8; - inp.s = inp.buf; - inp.e = inp.buf; + inbuf.inp.buf = xmalloc(10); + inbuf.inp.l = inbuf.inp.buf + 8; + inbuf.inp.s = inbuf.inp.buf; + inbuf.inp.e = inbuf.inp.buf; buf_init(&token); @@ -630,7 +623,7 @@ main_prepare_parsing(void) inp_read_line(); int ind = 0; - for (const char *p = inp.s;; p++) { + for (const char *p = inbuf.inp.s;; p++) { if (*p == ' ') ind++; else if (*p == '\t') @@ -1245,10 +1238,10 @@ read_preprocessing_line(void) state = PLAIN; int com_start = 0, com_end = 0; - while (ch_isblank(*inp.s)) + while (ch_isblank(*inbuf.inp.s)) inp_skip(); - while (*inp.s != '\n' || (state == COMM && !had_eof)) { + while (*inbuf.inp.s != '\n' || (state == COMM && !had_eof)) { buf_reserve(&lab, 2); *lab.e++ = inp_next(); switch (lab.e[-1]) { @@ -1257,9 +1250,9 @@ read_preprocessing_line(void) *lab.e++ = inp_next(); break; case '/': - if (*inp.s == '*' && state == PLAIN) { + if (*inbuf.inp.s == '*' && state == PLAIN) { state = COMM; - *lab.e++ = *inp.s++; + *lab.e++ = *inbuf.inp.s++; com_start = (int)buf_len(&lab) - 2; } break; @@ -1276,9 +1269,9 @@ read_preprocessing_line(void) state = CHR; break; case '*': - if (*inp.s == '/' && state == COMM) { + if (*inbuf.inp.s == '/' && state == COMM) { state = PLAIN; - *lab.e++ = *inp.s++; + *lab.e++ = *inbuf.inp.s++; com_end = (int)buf_len(&lab); } break; @@ -1287,12 +1280,12 @@ read_preprocessing_line(void) while (lab.e > lab.s && ch_isblank(lab.e[-1])) lab.e--; - if (lab.e - lab.s == com_end && saved_inp_s == NULL) { + if (lab.e - lab.s == com_end && inbuf.saved_inp_s == NULL) { /* comment on preprocessor line */ - if (sc_end == NULL) { /* if this is the first comment, we must set + if (inbuf.sc_end == NULL) { /* if this is the first comment, we must set * up the buffer */ - save_com = sc_buf; - sc_end = save_com; + inbuf.save_com = inbuf.sc_buf; + inbuf.sc_end = inbuf.save_com; } else { sc_add_char('\n'); /* add newline between comments */ sc_add_char(' '); @@ -1302,14 +1295,14 @@ read_preprocessing_line(void) lab.e = lab.s + com_start; while (lab.e > lab.s && ch_isblank(lab.e[-1])) lab.e--; - saved_inp_s = inp.s; /* save current input buffer */ - saved_inp_e = inp.e; - inp.s = save_com; /* fix so that subsequent calls to lexi will + inbuf.saved_inp_s = inbuf.inp.s; /* save current input buffer */ + inbuf.saved_inp_e = inbuf.inp.e; + inbuf.inp.s = inbuf.save_com; /* fix so that subsequent calls to lexi will * take tokens out of save_com */ sc_add_char(' '); /* add trailing blank, just in case */ debug_save_com(__func__); - inp.e = sc_end; - sc_end = NULL; + inbuf.inp.e = inbuf.sc_end; + inbuf.sc_end = NULL; debug_println("switched inp.s to save_com"); } buf_terminate(&lab); Index: src/usr.bin/indent/indent.h diff -u src/usr.bin/indent/indent.h:1.86 src/usr.bin/indent/indent.h:1.87 --- src/usr.bin/indent/indent.h:1.86 Sun Nov 7 18:26:17 2021 +++ src/usr.bin/indent/indent.h Fri Nov 19 15:28:32 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.86 2021/11/07 18:26:17 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.87 2021/11/19 15:28:32 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -146,12 +146,22 @@ struct buffer { char *l; /* end of the allocated memory */ }; -extern FILE *input; -extern FILE *output; - -extern struct buffer inp; /* one line of input, ready to be split into +extern struct input_buffer { + struct buffer inp; /* one line of input, ready to be split into * tokens; occasionally this buffer switches * to sc_buf */ + char sc_buf[sc_size]; /* input text is saved here when looking for + * the brace after an if, while, etc */ + char *save_com; /* start of the comment stored in sc_buf */ + char *sc_end; /* pointer into save_com buffer */ + + char *saved_inp_s; /* saved value of inp.s when taking input from + * save_com */ + char *saved_inp_e; /* similarly saved value of inp.e */ +} inbuf; + +extern FILE *input; +extern FILE *output; extern struct buffer token; /* the current token to be processed, is * typically copied to the buffer 'code', @@ -164,15 +174,6 @@ extern struct buffer com; /* the trailin * while in process_comment, a single line of * a multi-line comment */ -extern char sc_buf[sc_size]; /* input text is saved here when looking for - * the brace after an if, while, etc */ -extern char *save_com; /* start of the comment stored in sc_buf */ - -extern char *saved_inp_s; /* saved value of inp.s when taking input from - * save_com */ -extern char *saved_inp_e; /* similarly saved value of inp.e */ - - extern struct options { bool blanklines_around_conditional_compilation; bool blanklines_after_decl_at_top; /* this is vaguely similar to Index: src/usr.bin/indent/io.c diff -u src/usr.bin/indent/io.c:1.116 src/usr.bin/indent/io.c:1.117 --- src/usr.bin/indent/io.c:1.116 Sun Nov 7 07:06:00 2021 +++ src/usr.bin/indent/io.c Fri Nov 19 15:28:32 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: io.c,v 1.116 2021/11/07 07:06:00 rillig Exp $ */ +/* $NetBSD: io.c,v 1.117 2021/11/19 15:28:32 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)io.c 8.1 (Be #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: io.c,v 1.116 2021/11/07 07:06:00 rillig Exp $"); +__RCSID("$NetBSD: io.c,v 1.117 2021/11/19 15:28:32 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $"); #endif @@ -367,7 +367,7 @@ parse_indent_comment(void) { bool on; - const char *p = inp.buf; + const char *p = inbuf.inp.buf; skip_blank(&p); if (!skip_string(&p, "/*")) @@ -412,23 +412,23 @@ inp_read_line(void) int ch; FILE *f = input; - if (saved_inp_s != NULL) { /* there is a partly filled input buffer left */ - inp.s = saved_inp_s; /* do not read anything, just switch buffers */ - inp.e = saved_inp_e; - saved_inp_s = saved_inp_e = NULL; + if (inbuf.saved_inp_s != NULL) { /* there is a partly filled input buffer left */ + inbuf.inp.s = inbuf.saved_inp_s; /* do not read anything, just switch buffers */ + inbuf.inp.e = inbuf.saved_inp_e; + inbuf.saved_inp_s = inbuf.saved_inp_e = NULL; debug_println("switched inp.s back to saved_inp_s"); - if (inp.s < inp.e) + if (inbuf.inp.s < inbuf.inp.e) return; /* only return if there is really something in * this buffer */ } - for (p = inp.buf;;) { - if (p >= inp.l) { - size_t size = (size_t)(inp.l - inp.buf) * 2 + 10; - size_t offset = (size_t)(p - inp.buf); - inp.buf = xrealloc(inp.buf, size); - p = inp.buf + offset; - inp.l = inp.buf + size - 2; + for (p = inbuf.inp.buf;;) { + if (p >= inbuf.inp.l) { + size_t size = (size_t)(inbuf.inp.l - inbuf.inp.buf) * 2 + 10; + size_t offset = (size_t)(p - inbuf.inp.buf); + inbuf.inp.buf = xrealloc(inbuf.inp.buf, size); + p = inbuf.inp.buf + offset; + inbuf.inp.l = inbuf.inp.buf + size - 2; } if ((ch = getc(f)) == EOF) { @@ -446,14 +446,14 @@ inp_read_line(void) break; } - inp.s = inp.buf; - inp.e = p; + inbuf.inp.s = inbuf.inp.buf; + inbuf.inp.e = p; - if (p - inp.s >= 3 && p[-3] == '*' && p[-2] == '/') + if (p - inbuf.inp.s >= 3 && p[-3] == '*' && p[-2] == '/') parse_indent_comment(); if (inhibit_formatting) - output_range(inp.s, inp.e); + output_range(inbuf.inp.s, inbuf.inp.e); } int Index: src/usr.bin/indent/pr_comment.c diff -u src/usr.bin/indent/pr_comment.c:1.116 src/usr.bin/indent/pr_comment.c:1.117 --- src/usr.bin/indent/pr_comment.c:1.116 Sun Nov 7 18:26:17 2021 +++ src/usr.bin/indent/pr_comment.c Fri Nov 19 15:28:32 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: pr_comment.c,v 1.116 2021/11/07 18:26:17 rillig Exp $ */ +/* $NetBSD: pr_comment.c,v 1.117 2021/11/19 15:28:32 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)pr_comment.c #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: pr_comment.c,v 1.116 2021/11/07 18:26:17 rillig Exp $"); +__RCSID("$NetBSD: pr_comment.c,v 1.117 2021/11/19 15:28:32 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $"); #endif @@ -86,13 +86,13 @@ com_terminate(void) static bool fits_in_one_line(int max_line_length) { - for (const char *p = inp.s; *p != '\n'; p++) { + for (const char *p = inbuf.inp.s; *p != '\n'; p++) { assert(*p != '\0'); - assert(inp.e - p >= 2); + assert(inbuf.inp.e - p >= 2); if (!(p[0] == '*' && p[1] == '/')) continue; - int len = ind_add(ps.com_ind + 3, inp.s, p); + int len = ind_add(ps.com_ind + 3, inbuf.inp.s, p); len += ch_isblank(p[-1]) ? 2 : 3; return len <= max_line_length; } @@ -117,8 +117,9 @@ analyze_comment(bool *p_may_wrap, bool * com_ind = 0; } else { - if (*inp.s == '-' || *inp.s == '*' || token.e[-1] == '/' || - (*inp.s == '\n' && !opt.format_block_comments)) { + if (*inbuf.inp.s == '-' || *inbuf.inp.s == '*' || + token.e[-1] == '/' || + (*inbuf.inp.s == '\n' && !opt.format_block_comments)) { may_wrap = false; break_delim = false; } @@ -158,13 +159,14 @@ analyze_comment(bool *p_may_wrap, bool * * XXX: ordered comparison between pointers from different objects * invokes undefined behavior (C99 6.5.8). */ - const char *start = inp.s >= sc_buf && inp.s < sc_buf + sc_size - ? sc_buf : inp.buf; - ps.n_comment_delta = -ind_add(0, start, inp.s - 2); + const char *start = inbuf.inp.s >= inbuf.sc_buf && + inbuf.inp.s < inbuf.sc_buf + sc_size + ? inbuf.sc_buf : inbuf.inp.buf; + ps.n_comment_delta = -ind_add(0, start, inbuf.inp.s - 2); } else { ps.n_comment_delta = 0; - while (ch_isblank(*inp.s)) - inp.s++; + while (ch_isblank(*inbuf.inp.s)) + inbuf.inp.s++; } ps.comment_delta = 0; @@ -172,7 +174,7 @@ analyze_comment(bool *p_may_wrap, bool * com_add_char(token.e[-1]); /* either '*' or '/' */ /* TODO: Maybe preserve a single '\t' as well. */ - if (*inp.s != ' ' && may_wrap) + if (*inbuf.inp.s != ' ' && may_wrap) com_add_char(' '); if (break_delim && fits_in_one_line(adj_max_line_length)) @@ -205,14 +207,14 @@ copy_comment_wrap(int adj_max_line_lengt ssize_t last_blank = -1; /* index of the last blank in com.buf */ for (;;) { - switch (*inp.s) { + switch (*inbuf.inp.s) { case '\f': dump_line_ff(); last_blank = -1; com_add_delim(); - inp.s++; - while (ch_isblank(*inp.s)) - inp.s++; + inbuf.inp.s++; + while (ch_isblank(*inbuf.inp.s)) + inbuf.inp.s++; break; case '\n': @@ -245,19 +247,19 @@ copy_comment_wrap(int adj_max_line_lengt do { /* flush any blanks and/or tabs at start of * next line */ inp_skip(); - if (*inp.s == '*' && skip_asterisk) { + if (*inbuf.inp.s == '*' && skip_asterisk) { skip_asterisk = false; inp_skip(); - if (*inp.s == '/') + if (*inbuf.inp.s == '/') goto end_of_comment; } - } while (ch_isblank(*inp.s)); + } while (ch_isblank(*inbuf.inp.s)); break; /* end of case for newline */ case '*': inp_skip(); - if (*inp.s == '/') { + if (*inbuf.inp.s == '/') { end_of_comment: inp_skip(); @@ -289,7 +291,7 @@ copy_comment_wrap(int adj_max_line_lengt last_blank = com.e - com.buf; com_add_char(ch); now_len++; - if (memchr("*\n\r\b\t", *inp.s, 6) != NULL) + if (memchr("*\n\r\b\t", *inbuf.inp.s, 6) != NULL) break; if (now_len >= adj_max_line_length && last_blank != -1) break; @@ -325,7 +327,7 @@ static void copy_comment_nowrap(void) { for (;;) { - if (*inp.s == '\n') { + if (*inbuf.inp.s == '\n') { if (token.e[-1] == '/') goto finish; Index: src/usr.bin/indent/lexi.c diff -u src/usr.bin/indent/lexi.c:1.139 src/usr.bin/indent/lexi.c:1.140 --- src/usr.bin/indent/lexi.c:1.139 Thu Nov 18 23:26:58 2021 +++ src/usr.bin/indent/lexi.c Fri Nov 19 15:28:32 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: lexi.c,v 1.139 2021/11/18 23:26:58 rillig Exp $ */ +/* $NetBSD: lexi.c,v 1.140 2021/11/19 15:28:32 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c 8.1 ( #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: lexi.c,v 1.139 2021/11/18 23:26:58 rillig Exp $"); +__RCSID("$NetBSD: lexi.c,v 1.140 2021/11/19 15:28:32 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $"); #endif @@ -181,14 +181,14 @@ static const unsigned char lex_number_ro static char inp_peek(void) { - return *inp.s; + return *inbuf.inp.s; } void inp_skip(void) { - inp.s++; - if (inp.s >= inp.e) + inbuf.inp.s++; + if (inbuf.inp.s >= inbuf.inp.e) inp_read_line(); } @@ -352,7 +352,7 @@ static void lex_number(void) { for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) { - unsigned char ch = (unsigned char)*inp.s; + unsigned char ch = (unsigned char)*inbuf.inp.s; if (ch >= array_length(lex_number_row) || lex_number_row[ch] == 0) break; @@ -373,14 +373,14 @@ lex_number(void) static void lex_word(void) { - while (isalnum((unsigned char)*inp.s) || - *inp.s == '\\' || - *inp.s == '_' || *inp.s == '$') { - - if (*inp.s == '\\') { - if (inp.s[1] == '\n') { - inp.s += 2; - if (inp.s >= inp.e) + while (isalnum((unsigned char)*inbuf.inp.s) || + *inbuf.inp.s == '\\' || + *inbuf.inp.s == '_' || *inbuf.inp.s == '$') { + + if (*inbuf.inp.s == '\\') { + if (inbuf.inp.s[1] == '\n') { + inbuf.inp.s += 2; + if (inbuf.inp.s >= inbuf.inp.e) inp_read_line(); } else break; @@ -394,7 +394,7 @@ static void lex_char_or_string(void) { for (char delim = token.e[-1];;) { - if (*inp.s == '\n') { + if (*inbuf.inp.s == '\n') { diag(1, "Unterminated literal"); return; } @@ -404,7 +404,7 @@ lex_char_or_string(void) return; if (token.e[-1] == '\\') { - if (*inp.s == '\n') + if (*inbuf.inp.s == '\n') ++line_no; token_add_char(inp_next()); } @@ -417,9 +417,9 @@ probably_typename(void) { if (ps.block_init || ps.in_stmt) return false; - if (inp.s[0] == '*' && inp.s[1] != '=') + if (inbuf.inp.s[0] == '*' && inbuf.inp.s[1] != '=') goto maybe; - if (isalpha((unsigned char)*inp.s)) + if (isalpha((unsigned char)*inbuf.inp.s)) goto maybe; return false; maybe: @@ -468,11 +468,11 @@ cmp_keyword_by_name(const void *key, con static lexer_symbol lexi_alnum(void) { - if (isdigit((unsigned char)*inp.s) || - (inp.s[0] == '.' && isdigit((unsigned char)inp.s[1]))) { + if (isdigit((unsigned char)*inbuf.inp.s) || + (inbuf.inp.s[0] == '.' && isdigit((unsigned char)inbuf.inp.s[1]))) { lex_number(); - } else if (isalnum((unsigned char)*inp.s) || - *inp.s == '_' || *inp.s == '$') { + } else if (isalnum((unsigned char)*inbuf.inp.s) || + *inbuf.inp.s == '_' || *inbuf.inp.s == '$') { lex_word(); } else return lsym_eof; /* just as a placeholder */ @@ -480,7 +480,7 @@ lexi_alnum(void) *token.e = '\0'; if (token.s[0] == 'L' && token.s[1] == '\0' && - (*inp.s == '"' || *inp.s == '\'')) + (*inbuf.inp.s == '"' || *inbuf.inp.s == '\'')) return lsym_string_prefix; while (ch_isblank(inp_peek())) @@ -523,10 +523,10 @@ found_typename: } } - if (*inp.s == '(' && ps.tos <= 1 && ps.ind_level == 0 && + if (*inbuf.inp.s == '(' && ps.tos <= 1 && ps.ind_level == 0 && !ps.in_parameter_declaration && !ps.block_init) { - for (const char *p = inp.s; p < inp.e;) + for (const char *p = inbuf.inp.s; p < inbuf.inp.e;) if (*p++ == ')' && (*p == ';' || *p == ',')) goto no_function_definition; @@ -552,7 +552,7 @@ lexi(void) ps.curr_col_1 = ps.next_col_1; ps.next_col_1 = false; - while (ch_isblank(*inp.s)) { + while (ch_isblank(*inbuf.inp.s)) { ps.curr_col_1 = false; inp_skip(); } @@ -647,19 +647,19 @@ lexi(void) lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op; unary_delim = true; - if (*inp.s == token.e[-1]) { /* ++, -- */ - *token.e++ = *inp.s++; + if (*inbuf.inp.s == token.e[-1]) { /* ++, -- */ + *token.e++ = *inbuf.inp.s++; if (ps.prev_token == lsym_word || ps.prev_token == lsym_rparen_or_rbracket) { lsym = ps.next_unary ? lsym_unary_op : lsym_postfix_op; unary_delim = false; } - } else if (*inp.s == '=') { /* += */ - *token.e++ = *inp.s++; + } else if (*inbuf.inp.s == '=') { /* += */ + *token.e++ = *inbuf.inp.s++; - } else if (*inp.s == '>') { /* -> */ - *token.e++ = *inp.s++; + } else if (*inbuf.inp.s == '>') { /* -> */ + *token.e++ = *inbuf.inp.s++; unary_delim = false; lsym = lsym_unary_op; ps.want_blank = false; @@ -669,8 +669,8 @@ lexi(void) case '=': if (ps.init_or_struct) ps.block_init = true; - if (*inp.s == '=') { /* == */ - *token.e++ = *inp.s++; + if (*inbuf.inp.s == '=') { /* == */ + *token.e++ = *inbuf.inp.s++; *token.e = '\0'; } lsym = lsym_binary_op; @@ -680,10 +680,10 @@ lexi(void) case '>': case '<': case '!': /* ops like <, <<, <=, !=, etc */ - if (*inp.s == '>' || *inp.s == '<' || *inp.s == '=') + if (*inbuf.inp.s == '>' || *inbuf.inp.s == '<' || *inbuf.inp.s == '=') *token.e++ = inp_next(); - if (*inp.s == '=') - *token.e++ = *inp.s++; + if (*inbuf.inp.s == '=') + *token.e++ = *inbuf.inp.s++; lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op; unary_delim = true; break; @@ -691,27 +691,27 @@ lexi(void) case '*': unary_delim = true; if (!ps.next_unary) { - if (*inp.s == '=') - *token.e++ = *inp.s++; + if (*inbuf.inp.s == '=') + *token.e++ = *inbuf.inp.s++; lsym = lsym_binary_op; break; } - while (*inp.s == '*' || isspace((unsigned char)*inp.s)) { - if (*inp.s == '*') + while (*inbuf.inp.s == '*' || isspace((unsigned char)*inbuf.inp.s)) { + if (*inbuf.inp.s == '*') token_add_char('*'); inp_skip(); } if (ps.in_decl) { - char *tp = inp.s; + char *tp = inbuf.inp.s; while (isalpha((unsigned char)*tp) || isspace((unsigned char)*tp)) { - if (++tp >= inp.e) { - const char *s_before = inp.s; + if (++tp >= inbuf.inp.e) { + const char *s_before = inbuf.inp.s; inp_read_line(); - if (inp.s != s_before) + if (inbuf.inp.s != s_before) abort(); } } @@ -723,7 +723,7 @@ lexi(void) break; default: - if (token.e[-1] == '/' && (*inp.s == '*' || *inp.s == '/')) { + if (token.e[-1] == '/' && (*inbuf.inp.s == '*' || *inbuf.inp.s == '/')) { *token.e++ = inp_next(); lsym = lsym_comment; unary_delim = ps.next_unary; @@ -731,14 +731,14 @@ lexi(void) } /* handle '||', '&&', etc., and also things as in 'int *****i' */ - while (token.e[-1] == *inp.s || *inp.s == '=') + while (token.e[-1] == *inbuf.inp.s || *inbuf.inp.s == '=') token_add_char(inp_next()); lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op; unary_delim = true; } - if (inp.s >= inp.e) + if (inbuf.inp.s >= inbuf.inp.e) inp_read_line(); ps.next_unary = unary_delim;