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: move character input from indent.c to...



details:   https://anonhg.NetBSD.org/src/rev/98b6a1a9f3fd
branches:  trunk
changeset: 1026332:98b6a1a9f3fd
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Nov 19 17:42:45 2021 +0000

description:
indent: move character input from indent.c to io.c

No functional change.

diffstat:

 usr.bin/indent/indent.c |  107 +++++++++++------------------------------------
 usr.bin/indent/indent.h |   13 +++++-
 usr.bin/indent/io.c     |   67 +++++++++++++++++++++++++++++-
 3 files changed, 102 insertions(+), 85 deletions(-)

diffs (truncated from 336 to 300 lines):

diff -r 423c0906730e -r 98b6a1a9f3fd usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Fri Nov 19 17:30:10 2021 +0000
+++ b/usr.bin/indent/indent.c   Fri Nov 19 17:42:45 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.222 2021/11/19 17:11:46 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.223 2021/11/19 17:42:45 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.222 2021/11/19 17:11:46 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.223 2021/11/19 17:42:45 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -88,8 +88,6 @@
 
 struct parser_state ps;
 
-struct input_buffer inbuf;
-
 struct buffer token;
 
 struct buffer lab;
@@ -217,53 +215,6 @@
     va_end(ap);
 }
 
-#ifdef debug
-static void
-debug_inbuf(const char *prefix)
-{
-    debug_printf("%s:", prefix);
-    debug_vis_range(" inp \"", inbuf.inp.s, inbuf.inp.e, "\"");
-    if (inbuf.save_com_s != NULL)
-       debug_vis_range(" save_com \"",
-           inbuf.save_com_s, inbuf.save_com_e, "\"");
-    if (inbuf.saved_inp_s != NULL)
-       debug_vis_range(" saved_inp \"",
-           inbuf.saved_inp_s, inbuf.saved_inp_e, "\"");
-    debug_printf("\n");
-}
-#else
-#define debug_inbuf(prefix) do { } while (false)
-#endif
-
-static void
-save_com_check_size(size_t n)
-{
-    if ((size_t)(inbuf.save_com_e - inbuf.save_com_buf) + n <=
-           array_length(inbuf.save_com_buf))
-       return;
-
-    diag(1, "Internal buffer overflow - "
-       "Move big comment from right after if, while, or whatever");
-    fflush(output);
-    exit(1);
-}
-
-static void
-save_com_add_char(char ch)
-{
-    save_com_check_size(1);
-    *inbuf.save_com_e++ = ch;
-}
-
-static void
-save_com_add_range(const char *s, const char *e)
-{
-    size_t len = (size_t)(e - s);
-    save_com_check_size(len);
-    memcpy(inbuf.save_com_e, s, len);
-    inbuf.save_com_e += len;
-}
-
 static void
 search_stmt_newline(bool *force_nl)
 {
@@ -271,10 +222,10 @@
        inbuf.save_com_s = inbuf.save_com_buf;
        inbuf.save_com_s[0] = inbuf.save_com_s[1] = ' ';
        inbuf.save_com_e = &inbuf.save_com_s[2];
-       debug_inbuf("search_stmt_newline init");
+       debug_inp("search_stmt_newline init");
     }
-    save_com_add_char('\n');
-    debug_inbuf(__func__);
+    inp_comment_add_char('\n');
+    debug_inp(__func__);
 
     line_no++;
 
@@ -317,15 +268,15 @@
            inbuf.save_com_s, inbuf.save_com_e, "\"\n");
     }
 
-    save_com_add_range(token.s, token.e);
+    inp_comment_add_range(token.s, token.e);
     if (token.e[-1] == '/') {
        while (inbuf.inp.s[0] != '\n')
-           save_com_add_char(inp_next());
-       debug_inbuf("search_stmt_comment end C99");
+           inp_comment_add_char(inp_next());
+       debug_inp("search_stmt_comment end C99");
     } else {
        while (!(inbuf.save_com_e[-2] == '*' && inbuf.save_com_e[-1] == '/'))
-           save_com_add_char(inp_next());
-       debug_inbuf("search_stmt_comment end block");
+           inp_comment_add_char(inp_next());
+       debug_inp("search_stmt_comment end block");
     }
 }
 
@@ -349,7 +300,7 @@
            if (inp_peek() == '\n')
                break;
        }
-       debug_inbuf(__func__);
+       debug_inp(__func__);
        return true;
     }
     return false;
@@ -375,7 +326,7 @@
        return false;
     }
 
-    debug_inbuf(__func__);
+    debug_inp(__func__);
     while (inbuf.save_com_e > inbuf.save_com_s && ch_isblank(inbuf.save_com_e[-1]))
        inbuf.save_com_e--;
 
@@ -391,15 +342,15 @@
        *force_nl = false;
        --line_no;              /* this will be re-increased when the newline
                                 * is read from the buffer */
-       save_com_add_char('\n');
-       save_com_add_char(' ');
+       inp_comment_add_char('\n');
+       inp_comment_add_char(' ');
        if (opt.verbose)        /* warn if the line was not already broken */
            diag(0, "Line broken");
     }
 
     for (const char *t_ptr = token.s; *t_ptr != '\0'; ++t_ptr)
-       save_com_add_char(*t_ptr);
-    debug_inbuf("search_stmt_other end");
+       inp_comment_add_char(*t_ptr);
+    debug_inp("search_stmt_other end");
     return true;
 }
 
@@ -407,16 +358,8 @@
 switch_buffer(void)
 {
     ps.search_stmt = false;
-    save_com_add_char(' ');            /* add trailing blank, just in case */
-    debug_inbuf(__func__);
-
-    inbuf.saved_inp_s = inbuf.inp.s;
-    inbuf.saved_inp_e = inbuf.inp.e;
-
-    inbuf.inp.s = inbuf.save_com_s;    /* redirect lexi input to save_com_s */
-    inbuf.inp.e = inbuf.save_com_e;
-    inbuf.save_com_e = NULL;
-    debug_println("switched inp.s to save_com_s");
+    inp_comment_add_char(' ');         /* add trailing blank, just in case */
+    inp_from_comment();
 }
 
 static void
@@ -444,8 +387,8 @@
      */
     if (inbuf.save_com_e != NULL) {
        while (ch_isblank(inp_peek()))
-           save_com_add_char(inp_next());
-       debug_inbuf(__func__);
+           inp_comment_add_char(inp_next());
+       debug_inp(__func__);
     }
 
     struct parser_state backup_ps = ps;
@@ -1295,11 +1238,11 @@
            inbuf.save_com_s = inbuf.save_com_buf;
            inbuf.save_com_e = inbuf.save_com_s;
        } else {
-           save_com_add_char('\n');    /* add newline between comments */
-           save_com_add_char(' ');
+           inp_comment_add_char('\n'); /* add newline between comments */
+           inp_comment_add_char(' ');
            --line_no;
        }
-       save_com_add_range(lab.s + com_start, lab.s + com_end);
+       inp_comment_add_range(lab.s + com_start, lab.s + com_end);
        lab.e = lab.s + com_start;
        while (lab.e > lab.s && ch_isblank(lab.e[-1]))
            lab.e--;
@@ -1307,8 +1250,8 @@
        inbuf.saved_inp_e = inbuf.inp.e;
        inbuf.inp.s = inbuf.save_com_s; /* fix so that subsequent calls to lexi will
                                 * take tokens out of save_com */
-       save_com_add_char(' '); /* add trailing blank, just in case */
-       debug_inbuf(__func__);
+       inp_comment_add_char(' ');      /* add trailing blank, just in case */
+       debug_inp(__func__);
        inbuf.inp.e = inbuf.save_com_e;
        inbuf.save_com_e = NULL;
        debug_println("switched inbuf to save_com");
diff -r 423c0906730e -r 98b6a1a9f3fd usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Fri Nov 19 17:30:10 2021 +0000
+++ b/usr.bin/indent/indent.h   Fri Nov 19 17:42:45 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.91 2021/11/19 17:30:10 rillig Exp $       */
+/*     $NetBSD: indent.h,v 1.92 2021/11/19 17:42:45 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -384,6 +384,17 @@
 void inp_skip(void);
 char inp_next(void);
 
+void inp_comment_add_char(char);
+void inp_comment_add_range(const char *, const char *);
+
+void inp_from_comment(void);
+
+#ifdef debug
+void debug_inp(const char *);
+#else
+#define debug_inp(prefix) do { } while (false)
+#endif
+
 lexer_symbol lexi(void);
 void diag(int, const char *, ...)__printflike(2, 3);
 void dump_line(void);
diff -r 423c0906730e -r 98b6a1a9f3fd usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Fri Nov 19 17:30:10 2021 +0000
+++ b/usr.bin/indent/io.c       Fri Nov 19 17:42:45 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.119 2021/11/19 17:30:10 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.120 2021/11/19 17:42:45 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.119 2021/11/19 17:30:10 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.120 2021/11/19 17:42:45 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -51,10 +51,13 @@
 #include <ctype.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "indent.h"
 
+struct input_buffer inbuf;
+
 static int paren_indent;
 static bool suppress_blanklines;
 
@@ -99,6 +102,66 @@
     return ch;
 }
 
+#ifdef debug
+void
+debug_inp(const char *prefix)
+{
+    debug_printf("%s:", prefix);
+    debug_vis_range(" inp \"", inbuf.inp.s, inbuf.inp.e, "\"");
+    if (inbuf.save_com_s != NULL)
+       debug_vis_range(" save_com \"",
+                       inbuf.save_com_s, inbuf.save_com_e, "\"");
+    if (inbuf.saved_inp_s != NULL)
+       debug_vis_range(" saved_inp \"",
+                       inbuf.saved_inp_s, inbuf.saved_inp_e, "\"");
+    debug_printf("\n");
+}
+#else
+#define debug_inp(prefix) do { } while (false)
+#endif
+
+
+static void
+inp_comment_check_size(size_t n)
+{
+    if ((size_t)(inbuf.save_com_e - inbuf.save_com_buf) + n <=
+       array_length(inbuf.save_com_buf))
+       return;
+
+    diag(1, "Internal buffer overflow - "



Home | Main Index | Thread Index | Old Index