Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: group variables for the input buffer



details:   https://anonhg.NetBSD.org/src/rev/cd1cf6b609b1
branches:  trunk
changeset: 1024026:cd1cf6b609b1
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Oct 07 23:15:15 2021 +0000

description:
indent: group variables for the input buffer

The input buffer follows the same concept as the intermediate buffers
for label, code, comment and token, so use the same type for it.

No functional change.

diffstat:

 usr.bin/indent/indent.c       |  65 ++++++++++++++---------------
 usr.bin/indent/indent_globs.h |  12 +---
 usr.bin/indent/io.c           |  38 ++++++++--------
 usr.bin/indent/lexi.c         |  94 +++++++++++++++++++++---------------------
 usr.bin/indent/pr_comment.c   |  46 ++++++++++----------
 5 files changed, 124 insertions(+), 131 deletions(-)

diffs (truncated from 693 to 300 lines):

diff -r 464ece66a91b -r cd1cf6b609b1 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Thu Oct 07 23:01:32 2021 +0000
+++ b/usr.bin/indent/indent.c   Thu Oct 07 23:15:15 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.123 2021/10/07 23:01:32 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.124 2021/10/07 23:15:15 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.123 2021/10/07 23:01:32 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.124 2021/10/07 23:15:15 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -92,10 +92,7 @@
 struct buffer com;
 struct buffer token;
 
-char *in_buffer;
-char *in_buffer_limit;
-char *buf_ptr;
-char *buf_end;
+struct buffer inp;
 
 char sc_buf[sc_size];
 char *save_com;
@@ -176,8 +173,8 @@
         * process_comment() will use that to calculate original indentation
         * of a boxed comment.
         */
-       memcpy(sc_buf, in_buffer, (size_t)(buf_ptr - in_buffer) - 4);
-       save_com = sc_buf + (buf_ptr - in_buffer - 4);
+       memcpy(sc_buf, inp.buf, (size_t)(inp.s - inp.buf) - 4);
+       save_com = sc_buf + (inp.s - inp.buf - 4);
        save_com[0] = save_com[1] = ' ';
        sc_end = &save_com[2];
     }
@@ -188,7 +185,7 @@
 
     for (;;) {                 /* loop until the end of the comment */
        *sc_end++ = inbuf_next();
-       if (sc_end[-1] == '*' && *buf_ptr == '/')
+       if (sc_end[-1] == '*' && *inp.s == '/')
            break;              /* we are at end of comment */
        if (sc_end >= &save_com[sc_size]) {     /* check for temp buffer
                                                 * overflow */
@@ -216,9 +213,9 @@
         * 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)*buf_ptr)) {
+       while (isspace((unsigned char)*inp.s)) {
            inbuf_skip();
-           if (*buf_ptr == '\n')
+           if (*inp.s == '\n')
                break;
        }
        return true;
@@ -279,14 +276,14 @@
 switch_buffer(void)
 {
     ps.search_brace = false;   /* stop looking for start of stmt */
-    bp_save = buf_ptr;         /* save current input buffer */
-    be_save = buf_end;
-    buf_ptr = save_com;                /* fix so that subsequent calls to lexi will
+    bp_save = inp.s;           /* save current input buffer */
+    be_save = inp.e;
+    inp.s = save_com;          /* fix so that subsequent calls to lexi will
                                 * take tokens out of save_com */
     *sc_end++ = ' ';           /* add trailing blank, just in case */
-    buf_end = sc_end;
+    inp.e = sc_end;
     sc_end = NULL;
-    debug_println("switched buf_ptr to save_com");
+    debug_println("switched inp.s to save_com");
 }
 
 static void
@@ -314,12 +311,12 @@
      * into the buffer so that the later lexi() call will read them.
      */
     if (sc_end != NULL) {
-       while (is_hspace(*buf_ptr)) {
-           *sc_end++ = *buf_ptr++;
+       while (is_hspace(*inp.s)) {
+           *sc_end++ = *inp.s++;
            if (sc_end >= &save_com[sc_size])
                errx(1, "input too long");
        }
-       if (buf_ptr >= buf_end)
+       if (inp.s >= inp.e)
            fill_buffer();
     }
 
@@ -443,9 +440,9 @@
 
     opt.else_if = true;                /* XXX: redundant? */
 
-    in_buffer = xmalloc(10);
-    in_buffer_limit = in_buffer + 8;
-    buf_ptr = buf_end = in_buffer;
+    inp.buf = xmalloc(10);
+    inp.l = inp.buf + 8;
+    inp.s = inp.e = inp.buf;
     line_no = 1;
     had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
 
@@ -536,7 +533,7 @@
 
     parse(semicolon);
 
-    char *p = buf_ptr;
+    char *p = inp.s;
     int ind = 0;
 
     for (;;) {
@@ -1124,10 +1121,10 @@
     int com_start = 0, com_end = 0;
     char quote = '\0';
 
-    while (is_hspace(*buf_ptr))
+    while (is_hspace(*inp.s))
        inbuf_skip();
 
-    while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
+    while (*inp.s != '\n' || (in_comment && !had_eof)) {
        buf_reserve(&lab, 2);
        *lab.e++ = inbuf_next();
        switch (lab.e[-1]) {
@@ -1136,9 +1133,9 @@
                *lab.e++ = inbuf_next();
            break;
        case '/':
-           if (*buf_ptr == '*' && !in_comment && quote == '\0') {
+           if (*inp.s == '*' && !in_comment && quote == '\0') {
                in_comment = true;
-               *lab.e++ = *buf_ptr++;
+               *lab.e++ = *inp.s++;
                com_start = (int)buf_len(&lab) - 2;
            }
            break;
@@ -1155,9 +1152,9 @@
                quote = '\'';
            break;
        case '*':
-           if (*buf_ptr == '/' && in_comment) {
+           if (*inp.s == '/' && in_comment) {
                in_comment = false;
-               *lab.e++ = *buf_ptr++;
+               *lab.e++ = *inp.s++;
                com_end = (int)buf_len(&lab);
            }
            break;
@@ -1184,14 +1181,14 @@
        lab.e = lab.s + com_start;
        while (lab.e > lab.s && is_hspace(lab.e[-1]))
            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
+       bp_save = inp.s;        /* save current input buffer */
+       be_save = inp.e;
+       inp.s = save_com;       /* fix so that subsequent calls to lexi will
                                 * take tokens out of save_com */
        *sc_end++ = ' ';        /* add trailing blank, just in case */
-       buf_end = sc_end;
+       inp.e = sc_end;
        sc_end = NULL;
-       debug_println("switched buf_ptr to save_com");
+       debug_println("switched inp.s to save_com");
     }
     buf_terminate(&lab);
 }
diff -r 464ece66a91b -r cd1cf6b609b1 usr.bin/indent/indent_globs.h
--- a/usr.bin/indent/indent_globs.h     Thu Oct 07 23:01:32 2021 +0000
+++ b/usr.bin/indent/indent_globs.h     Thu Oct 07 23:15:15 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent_globs.h,v 1.46 2021/10/07 23:01:32 rillig Exp $ */
+/*     $NetBSD: indent_globs.h,v 1.47 2021/10/07 23:15:15 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -60,19 +60,15 @@
 extern struct buffer com;              /* comment */
 extern struct buffer token;            /* the last token scanned */
 
-extern char       *in_buffer;          /* input buffer */
-extern char       *in_buffer_limit;    /* the end of the input buffer */
-extern char       *buf_ptr;            /* ptr to next character to be taken from
-                                * in_buffer */
-extern char       *buf_end;            /* ptr to first after last char in in_buffer */
+extern struct buffer inp;
 
 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       *bp_save;            /* saved value of buf_ptr when taking input
+extern char       *bp_save;            /* saved value of inp.s when taking input
                                 * from save_com */
-extern char       *be_save;            /* similarly saved value of buf_end */
+extern char       *be_save;            /* similarly saved value of inp.e */
 
 
 extern struct options {
diff -r 464ece66a91b -r cd1cf6b609b1 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Thu Oct 07 23:01:32 2021 +0000
+++ b/usr.bin/indent/io.c       Thu Oct 07 23:15:15 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.78 2021/10/07 21:57:21 rillig Exp $   */
+/*     $NetBSD: io.c,v 1.79 2021/10/07 23:15:15 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.78 2021/10/07 21:57:21 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.79 2021/10/07 23:15:15 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -362,7 +362,7 @@
 {
     bool on_off;
 
-    const char *p = in_buffer;
+    const char *p = inp.buf;
 
     skip_hspace(&p);
     if (!skip_string(&p, "/*"))
@@ -411,22 +411,22 @@
     FILE *f = input;
 
     if (bp_save != NULL) {     /* there is a partly filled input buffer left */
-       buf_ptr = bp_save;      /* do not read anything, just switch buffers */
-       buf_end = be_save;
+       inp.s = bp_save;        /* do not read anything, just switch buffers */
+       inp.e = be_save;
        bp_save = be_save = NULL;
-       debug_println("switched buf_ptr back to bp_save");
-       if (buf_ptr < buf_end)
+       debug_println("switched inp.s back to bp_save");
+       if (inp.s < inp.e)
            return;             /* only return if there is really something in
                                 * this buffer */
     }
 
-    for (p = in_buffer;;) {
-       if (p >= in_buffer_limit) {
-           size_t size = (size_t)(in_buffer_limit - in_buffer) * 2 + 10;
-           size_t offset = (size_t)(p - in_buffer);
-           in_buffer = xrealloc(in_buffer, size);
-           p = in_buffer + offset;
-           in_buffer_limit = in_buffer + size - 2;
+    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;
        }
 
        if ((ch = getc(f)) == EOF) {
@@ -442,18 +442,18 @@
            break;
     }
 
-    buf_ptr = in_buffer;
-    buf_end = p;
+    inp.s = inp.buf;
+    inp.e = p;
 
-    if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
-       if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
+    if (p - inp.buf > 2 && p[-2] == '/' && p[-3] == '*') {
+       if (inp.buf[3] == 'I' && strncmp(inp.buf, "/**INDENT**", 11) == 0)
            fill_buffer();      /* flush indent error message */
        else
            parse_indent_comment();
     }
 
     if (inhibit_formatting) {
-       p = in_buffer;
+       p = inp.buf;
        do {
            output_char(*p);
        } while (*p++ != '\n');
diff -r 464ece66a91b -r cd1cf6b609b1 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Thu Oct 07 23:01:32 2021 +0000
+++ b/usr.bin/indent/lexi.c     Thu Oct 07 23:15:15 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.77 2021/10/07 22:52:13 rillig Exp $ */
+/*     $NetBSD: lexi.c,v 1.78 2021/10/07 23:15:15 rillig Exp $ */



Home | Main Index | Thread Index | Old Index