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: split inp_read_line into smaller func...



details:   https://anonhg.NetBSD.org/src/rev/b6c7801988d3
branches:  trunk
changeset: 1026488:b6c7801988d3
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Nov 26 14:48:03 2021 +0000

description:
indent: split inp_read_line into smaller functions

No functional change.

diffstat:

 usr.bin/indent/io.c |  88 +++++++++++++++++++++++++---------------------------
 1 files changed, 43 insertions(+), 45 deletions(-)

diffs (130 lines):

diff -r caad9af6a638 -r b6c7801988d3 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Fri Nov 26 14:33:12 2021 +0000
+++ b/usr.bin/indent/io.c       Fri Nov 26 14:48:03 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.136 2021/11/26 14:33:12 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.137 2021/11/26 14:48:03 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.136 2021/11/26 14:33:12 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.137 2021/11/26 14:48:03 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -306,6 +306,45 @@
     inbuf.save_com_s[0] = '{';
 }
 
+static char *
+inp_enlarge(char *p)
+{
+    size_t new_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, new_size);
+    inbuf.inp.l = inbuf.inp.buf + new_size - 2;
+    return inbuf.inp.buf + offset;
+}
+
+static void
+inp_read_next_line(FILE *f)
+{
+    char *p;
+    int ch;
+
+    for (p = inbuf.inp.buf;;) {
+       if (p >= inbuf.inp.l)
+           p = inp_enlarge(p);
+
+       if ((ch = getc(f)) == EOF) {
+           if (!inhibit_formatting) {
+               *p++ = ' ';
+               *p++ = '\n';
+           }
+           had_eof = true;
+           break;
+       }
+
+       if (ch != '\0')
+           *p++ = (char)ch;
+       if (ch == '\n')
+           break;
+    }
+
+    inbuf.inp.s = inbuf.inp.buf;
+    inbuf.inp.e = p;
+}
+
 static void
 output_char(char ch)
 {
@@ -320,12 +359,6 @@
     debug_vis_range("output_range \"", s, e, "\"\n");
 }
 
-static inline void
-output_string(const char *s)
-{
-    output_range(s, s + strlen(s));
-}
-
 static int
 output_indent(int old_ind, int new_ind)
 {
@@ -622,50 +655,15 @@
     }
 }
 
-/*
- * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
- *
- * All rights reserved
- */
 void
 inp_read_line(void)
 {
-    char *p;
-    int ch;
-    FILE *f = input;
-
     if (inp_from_file())
        return;
 
-    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;
-       }
+    inp_read_next_line(input);
 
-       if ((ch = getc(f)) == EOF) {
-           if (!inhibit_formatting) {
-               *p++ = ' ';
-               *p++ = '\n';
-           }
-           had_eof = true;
-           break;
-       }
-
-       if (ch != '\0')
-           *p++ = (char)ch;
-       if (ch == '\n')
-           break;
-    }
-
-    inbuf.inp.s = inbuf.inp.buf;
-    inbuf.inp.e = p;
-
-    if (p - inbuf.inp.s >= 3 && p[-3] == '*' && p[-2] == '/')
-       parse_indent_comment();
+    parse_indent_comment();
 
     if (inhibit_formatting)
        output_range(inbuf.inp.s, inbuf.inp.e);



Home | Main Index | Thread Index | Old Index