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: convert big macros to functions



details:   https://anonhg.NetBSD.org/src/rev/ded518adff65
branches:  trunk
changeset: 953428:ded518adff65
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Mar 08 20:15:42 2021 +0000

description:
indent: convert big macros to functions

Each of these buffers is only modified in a single file.  This makes it
unnecessary to declare the macros in the global header.

diffstat:

 usr.bin/indent/indent.c       |  60 ++++++++++++++++++++++++++++++++----------
 usr.bin/indent/indent_globs.h |  54 +-------------------------------------
 usr.bin/indent/lexi.c         |  33 +++++++++++++++++------
 usr.bin/indent/pr_comment.c   |  36 ++++++++++++++++++++-----
 4 files changed, 99 insertions(+), 84 deletions(-)

diffs (truncated from 426 to 300 lines):

diff -r ff86672035bd -r ded518adff65 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Mon Mar 08 20:12:04 2021 +0000
+++ b/usr.bin/indent/indent.c   Mon Mar 08 20:15:42 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.33 2021/03/08 19:06:48 rillig Exp $       */
+/*     $NetBSD: indent.c,v 1.34 2021/03/08 20:15:42 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.33 2021/03/08 19:06:48 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.34 2021/03/08 20:15:42 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -133,6 +133,36 @@
                                                 * files */
 char        bakfile[MAXPATHLEN] = "";
 
+static void
+check_size_code(size_t desired_size)
+{
+    if (e_code + (desired_size) >= l_code) {
+       int nsize = l_code - s_code + 400 + desired_size;
+       int code_len = e_code - s_code;
+       codebuf = (char *)realloc(codebuf, nsize);
+       if (codebuf == NULL)
+           err(1, NULL);
+       e_code = codebuf + code_len + 1;
+       l_code = codebuf + nsize - 5;
+       s_code = codebuf + 1;
+    }
+}
+
+static void
+check_size_label(size_t desired_size)
+{
+    if (e_lab + (desired_size) >= l_lab) {
+       int nsize = l_lab - s_lab + 400 + desired_size;
+       int label_len = e_lab - s_lab;
+       labbuf = (char *)realloc(labbuf, nsize);
+       if (labbuf == NULL)
+           err(1, NULL);
+       e_lab = labbuf + label_len + 1;
+       l_lab = labbuf + nsize - 5;
+       s_lab = labbuf + 1;
+    }
+}
+
 int
 main(int argc, char **argv)
 {
@@ -584,7 +614,7 @@
                                         * in a line. fix it */
                int len = e_com - s_com;
 
-               CHECK_SIZE_CODE(len + 3);
+               check_size_code(len + 3);
                *e_code++ = ' ';
                memcpy(e_code, s_com, len);
                e_code += len;
@@ -603,8 +633,8 @@
        /*-----------------------------------------------------*\
        |          do switch on type of token scanned           |
        \*-----------------------------------------------------*/
-       CHECK_SIZE_CODE(3);     /* maximum number of increments of e_code
-                                * before the next CHECK_SIZE_CODE or
+       check_size_code(3);     /* maximum number of increments of e_code
+                                * before the next check_size_code or
                                 * dump_line() is 2. After that there's the
                                 * final increment for the null character. */
        switch (type_code) {    /* now, decide what to do with the token */
@@ -720,7 +750,7 @@
            {
                int len = e_token - s_token;
 
-               CHECK_SIZE_CODE(len);
+               check_size_code(len);
                memcpy(e_code, token, len);
                e_code += len;
            }
@@ -731,7 +761,7 @@
            {
                int len = e_token - s_token;
 
-               CHECK_SIZE_CODE(len + 1);
+               check_size_code(len + 1);
                if (ps.want_blank)
                    *e_code++ = ' ';
                memcpy(e_code, token, len);
@@ -782,7 +812,7 @@
            {
                int len = e_code - s_code;
 
-               CHECK_SIZE_LAB(len + 3);
+               check_size_label(len + 3);
                memcpy(e_lab, s_code, len);
                e_lab += len;
                *e_lab++ = ':';
@@ -1073,7 +1103,7 @@
            {
                int len = e_token - s_token;
 
-               CHECK_SIZE_CODE(len + 1);
+               check_size_code(len + 1);
                if (ps.want_blank)
                    *e_code++ = ' ';
                memcpy(e_code, s_token, len);
@@ -1087,7 +1117,7 @@
            {
                int len = e_token - s_token;
 
-               CHECK_SIZE_CODE(len + 1);
+               check_size_code(len + 1);
                if (ps.want_blank)
                    *e_code++ = ' ';
                memcpy(e_code, token, len);
@@ -1128,7 +1158,7 @@
                    (s_lab != e_lab) ||
                    (s_code != e_code))
                dump_line();
-           CHECK_SIZE_LAB(1);
+           check_size_label(1);
            *e_lab++ = '#';     /* move whole line to 'label' buffer */
            {
                int         in_comment = 0;
@@ -1142,7 +1172,7 @@
                        fill_buffer();
                }
                while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
-                   CHECK_SIZE_LAB(2);
+                   check_size_label(2);
                    *e_lab = *buf_ptr++;
                    if (buf_ptr >= buf_end)
                        fill_buffer();
@@ -1210,7 +1240,7 @@
                    buf_end = sc_end;
                    sc_end = NULL;
                }
-               CHECK_SIZE_LAB(1);
+               check_size_label(1);
                *e_lab = '\0';  /* null terminate line */
                ps.pcase = false;
            }
@@ -1343,13 +1373,13 @@
     if (tabs_to_var) {
        int tpos;
 
-       CHECK_SIZE_CODE(cur_dec_ind / opt.tabsize);
+       check_size_code(cur_dec_ind / opt.tabsize);
        while ((tpos = opt.tabsize * (1 + pos / opt.tabsize)) <= cur_dec_ind) {
            *e_code++ = '\t';
            pos = tpos;
        }
     }
-    CHECK_SIZE_CODE(cur_dec_ind - pos + 1);
+    check_size_code(cur_dec_ind - pos + 1);
     while (pos < cur_dec_ind) {
        *e_code++ = ' ';
        pos++;
diff -r ff86672035bd -r ded518adff65 usr.bin/indent/indent_globs.h
--- a/usr.bin/indent/indent_globs.h     Mon Mar 08 20:12:04 2021 +0000
+++ b/usr.bin/indent/indent_globs.h     Mon Mar 08 20:15:42 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent_globs.h,v 1.15 2021/03/07 20:40:18 rillig Exp $ */
+/*     $NetBSD: indent_globs.h,v 1.16 2021/03/08 20:15:42 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -54,58 +54,6 @@
 extern FILE       *input;              /* the fid for the input file */
 extern FILE       *output;             /* the output file */
 
-#define CHECK_SIZE_CODE(desired_size) \
-       if (e_code + (desired_size) >= l_code) { \
-           int nsize = l_code-s_code + 400 + desired_size; \
-           int code_len = e_code-s_code; \
-           codebuf = (char *) realloc(codebuf, nsize); \
-           if (codebuf == NULL) \
-               err(1, NULL); \
-           e_code = codebuf + code_len + 1; \
-           l_code = codebuf + nsize - 5; \
-           s_code = codebuf + 1; \
-       }
-#define CHECK_SIZE_COM(desired_size) \
-       if (e_com + (desired_size) >= l_com) { \
-           int nsize = l_com-s_com + 400 + desired_size; \
-           int com_len = e_com - s_com; \
-           int blank_pos; \
-           if (last_bl != NULL) \
-               blank_pos = last_bl - combuf; \
-           else \
-               blank_pos = -1; \
-           combuf = (char *) realloc(combuf, nsize); \
-           if (combuf == NULL) \
-               err(1, NULL); \
-           e_com = combuf + com_len + 1; \
-           if (blank_pos > 0) \
-               last_bl = combuf + blank_pos; \
-           l_com = combuf + nsize - 5; \
-           s_com = combuf + 1; \
-       }
-#define CHECK_SIZE_LAB(desired_size) \
-       if (e_lab + (desired_size) >= l_lab) { \
-           int nsize = l_lab-s_lab + 400 + desired_size; \
-           int label_len = e_lab - s_lab; \
-           labbuf = (char *) realloc(labbuf, nsize); \
-           if (labbuf == NULL) \
-               err(1, NULL); \
-           e_lab = labbuf + label_len + 1; \
-           l_lab = labbuf + nsize - 5; \
-           s_lab = labbuf + 1; \
-       }
-#define CHECK_SIZE_TOKEN(desired_size) \
-       if (e_token + (desired_size) >= l_token) { \
-           int nsize = l_token-s_token + 400 + desired_size; \
-           int token_len = e_token - s_token; \
-           tokenbuf = (char *) realloc(tokenbuf, nsize); \
-           if (tokenbuf == NULL) \
-               err(1, NULL); \
-           e_token = tokenbuf + token_len + 1; \
-           l_token = tokenbuf + nsize - 5; \
-           s_token = tokenbuf + 1; \
-       }
-
 extern char       *labbuf;             /* buffer for label */
 extern char       *s_lab;              /* start ... */
 extern char       *e_lab;              /* .. and end of stored label */
diff -r ff86672035bd -r ded518adff65 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Mon Mar 08 20:12:04 2021 +0000
+++ b/usr.bin/indent/lexi.c     Mon Mar 08 20:15:42 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.24 2021/03/07 22:11:01 rillig Exp $ */
+/*     $NetBSD: lexi.c,v 1.25 2021/03/08 20:15:42 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.24 2021/03/07 22:11:01 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.25 2021/03/08 20:15:42 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -171,6 +171,21 @@
     [0]   = "uuiifuufiuuiiuiiiiiuiuuuuu",
 };
 
+static void
+check_size_token(size_t desired_size)
+{
+    if (e_token + (desired_size) >= l_token) {
+       int nsize = l_token - s_token + 400 + desired_size;
+       int token_len = e_token - s_token;
+       tokenbuf = (char *)realloc(tokenbuf, nsize);
+       if (tokenbuf == NULL)
+           err(1, NULL);
+       e_token = tokenbuf + token_len + 1;
+       l_token = tokenbuf + nsize - 5;
+       s_token = tokenbuf + 1;
+    }
+}
+
 static int
 strcmp_type(const void *e1, const void *e2)
 {
@@ -277,7 +292,7 @@
                    break;
                }
                s = table[i][s - 'A'];
-               CHECK_SIZE_TOKEN(1);
+               check_size_token(1);
                *e_token++ = *buf_ptr++;
                if (buf_ptr >= buf_end)
                    fill_buffer();
@@ -297,7 +312,7 @@
                        } else
                            break;
                }
-               CHECK_SIZE_TOKEN(1);
+               check_size_token(1);
                /* copy it over */
                *e_token++ = *buf_ptr++;
                if (buf_ptr >= buf_end)
@@ -420,7 +435,7 @@
 
     /* Scan a non-alphanumeric token */
 
-    CHECK_SIZE_TOKEN(3);               /* things like "<<=" */
+    check_size_token(3);               /* things like "<<=" */
     *e_token++ = *buf_ptr;             /* if it is only a one-character token, it is
                                 * moved here */
     *e_token = '\0';
@@ -448,7 +463,7 @@
                    diag(1, "Unterminated literal");



Home | Main Index | Thread Index | Old Index