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 handling from in...



details:   https://anonhg.NetBSD.org/src/rev/7bf3c58d2ace
branches:  trunk
changeset: 1026333:7bf3c58d2ace
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Nov 19 17:59:16 2021 +0000

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

No functional change.

diffstat:

 usr.bin/indent/indent.c |  43 +++++-----------------------------
 usr.bin/indent/indent.h |   5 +++-
 usr.bin/indent/io.c     |  60 +++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 69 insertions(+), 39 deletions(-)

diffs (203 lines):

diff -r 98b6a1a9f3fd -r 7bf3c58d2ace usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Fri Nov 19 17:42:45 2021 +0000
+++ b/usr.bin/indent/indent.c   Fri Nov 19 17:59:16 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.223 2021/11/19 17:42:45 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.224 2021/11/19 17:59:16 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.223 2021/11/19 17:42:45 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.224 2021/11/19 17:59:16 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -218,12 +218,7 @@
 static void
 search_stmt_newline(bool *force_nl)
 {
-    if (inbuf.save_com_e == NULL) {
-       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_inp("search_stmt_newline init");
-    }
+    inp_comment_init_newline();
     inp_comment_add_char('\n');
     debug_inp(__func__);
 
@@ -243,38 +238,14 @@
 static void
 search_stmt_comment(void)
 {
-    if (inbuf.save_com_e == NULL) {
-       /*
-        * Copy everything from the start of the line, because
-        * process_comment() will use that to calculate the original
-        * indentation of a boxed comment.
-        */
-       /*
-        * FIXME: This '4' needs an explanation. For example, in the snippet
-        * 'if(expr)/''*comment', the 'r)' of the code is not copied. If there
-        * is an additional line break before the ')', memcpy tries to copy
-        * (size_t)-1 bytes.
-        */
-       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.save_com_buf));
-       memcpy(inbuf.save_com_buf, inbuf.inp.buf, line_len);
-       inbuf.save_com_s = inbuf.save_com_buf + line_len;
-       inbuf.save_com_s[0] = inbuf.save_com_s[1] = ' ';
-       inbuf.save_com_e = &inbuf.save_com_s[2];
-       debug_vis_range("search_stmt_comment: before save_com is \"",
-           inbuf.save_com_buf, inbuf.save_com_s, "\"\n");
-       debug_vis_range("search_stmt_comment: save_com is \"",
-           inbuf.save_com_s, inbuf.save_com_e, "\"\n");
-    }
-
+    inp_comment_init_comment();
     inp_comment_add_range(token.s, token.e);
     if (token.e[-1] == '/') {
-       while (inbuf.inp.s[0] != '\n')
+       while (inp_peek() != '\n')
            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] == '/'))
+       while (!inp_comment_complete_block())
            inp_comment_add_char(inp_next());
        debug_inp("search_stmt_comment end block");
     }
@@ -288,7 +259,7 @@
      * this loop in order to avoid copying the token again.
      */
     if (inbuf.save_com_e != NULL && opt.brace_same_line) {
-       assert(inbuf.save_com_s[0] == ' ');     /* see search_stmt_comment */
+       assert(inbuf.save_com_s[0] == ' ');     /* inp_comment_init_newline */
        inbuf.save_com_s[0] = '{';
        /*
         * Originally the lbrace may have been alone on its own line, but it
diff -r 98b6a1a9f3fd -r 7bf3c58d2ace usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Fri Nov 19 17:42:45 2021 +0000
+++ b/usr.bin/indent/indent.h   Fri Nov 19 17:59:16 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.92 2021/11/19 17:42:45 rillig Exp $       */
+/*     $NetBSD: indent.h,v 1.93 2021/11/19 17:59:16 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -384,8 +384,11 @@
 void inp_skip(void);
 char inp_next(void);
 
+void inp_comment_init_newline(void);
+void inp_comment_init_comment(void);
 void inp_comment_add_char(char);
 void inp_comment_add_range(const char *, const char *);
+bool inp_comment_complete_block(void);
 
 void inp_from_comment(void);
 
diff -r 98b6a1a9f3fd -r 7bf3c58d2ace usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Fri Nov 19 17:42:45 2021 +0000
+++ b/usr.bin/indent/io.c       Fri Nov 19 17:59:16 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.120 2021/11/19 17:42:45 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.121 2021/11/19 17:59:16 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,11 +43,12 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.120 2021/11/19 17:42:45 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.121 2021/11/19 17:59:16 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
 
+#include <assert.h>
 #include <ctype.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -135,6 +136,54 @@
 }
 
 void
+inp_comment_init_newline(void)
+{
+    if (inbuf.save_com_e != NULL)
+       return;
+
+    inbuf.save_com_s = inbuf.save_com_buf;
+    inbuf.save_com_s[0] = ' '; /* see search_stmt_lbrace */
+    inbuf.save_com_s[1] = ' '; /* see search_stmt_lbrace */
+    inbuf.save_com_e = &inbuf.save_com_s[2];
+    debug_inp(__func__);
+}
+
+void
+inp_comment_init_comment(void)
+{
+    if (inbuf.save_com_e != NULL)
+       return;
+
+    /*
+     * Copy everything from the start of the line, because
+     * process_comment() will use that to calculate the original
+     * indentation of a boxed comment.
+     */
+    /*
+     * TODO: Don't store anything in the memory range [input.inp.buf,
+     * input.inp.s), as that data can easily get lost.
+     */
+    /*
+     * FIXME: This '4' needs an explanation. For example, in the snippet
+     * 'if(expr)/''*comment', the 'r)' of the code is not copied. If there
+     * is an additional line break before the ')', memcpy tries to copy
+     * (size_t)-1 bytes.
+     */
+    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.save_com_buf));
+    memcpy(inbuf.save_com_buf, inbuf.inp.buf, line_len);
+    inbuf.save_com_s = inbuf.save_com_buf + line_len;
+    inbuf.save_com_s[0] = ' '; /* see search_stmt_lbrace */
+    inbuf.save_com_s[1] = ' '; /* see search_stmt_lbrace */
+    inbuf.save_com_e = &inbuf.save_com_s[2];
+    debug_vis_range("search_stmt_comment: before save_com is \"",
+                   inbuf.save_com_buf, inbuf.save_com_s, "\"\n");
+    debug_vis_range("search_stmt_comment: save_com is \"",
+                   inbuf.save_com_s, inbuf.save_com_e, "\"\n");
+}
+
+void
 inp_comment_add_char(char ch)
 {
     inp_comment_check_size(1);
@@ -150,6 +199,12 @@
     inbuf.save_com_e += len;
 }
 
+bool
+inp_comment_complete_block(void)
+{
+    return inbuf.save_com_e[-2] == '*' && inbuf.save_com_e[-1] == '/';
+}
+
 void
 inp_from_comment(void)
 {
@@ -163,6 +218,7 @@
     debug_inp(__func__);
 }
 
+
 static void
 output_char(char ch)
 {



Home | Main Index | Thread Index | Old Index