new version from James Gosling including various bug fixes
[unix-history] / usr / src / usr.bin / indent / io.c
index a006d99..73cf90a 100644 (file)
@@ -19,7 +19,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)io.c       5.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)io.c       5.13 (Berkeley) %G%";
 #endif /* not lint */
 
 #include "indent_globs.h"
 #endif /* not lint */
 
 #include "indent_globs.h"
@@ -90,7 +90,19 @@ dump_line()
            while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
                e_lab--;
            cur_col = pad_output(1, compute_label_target());
            while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
                e_lab--;
            cur_col = pad_output(1, compute_label_target());
-           fprintf(output, "%.*s", e_lab - s_lab, s_lab);
+           if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
+                                   || strncmp(s_lab, "#endif", 6) == 0)) {
+               register char *s = s_lab;
+               if (e_lab[-1] == '\n') e_lab--;
+               do putc(*s++, output);
+               while (s < e_lab && 'a' <= *s && *s<='z');
+               while ((*s == ' ' || *s == '\t') && s < e_lab)
+                   s++;
+               if (s < e_lab)
+                   fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
+                           e_lab - s, s);
+           }
+           else fprintf(output, "%.*s", e_lab - s_lab, s_lab);
            cur_col = count_spaces(cur_col, s_lab);
        }
        else
            cur_col = count_spaces(cur_col, s_lab);
        }
        else
@@ -244,11 +256,9 @@ inhibit_newline:
     ps.ind_level = ps.i_l_follow;
     ps.paren_level = ps.p_l_follow;
     paren_target = -ps.paren_indents[ps.paren_level - 1];
     ps.ind_level = ps.i_l_follow;
     ps.paren_level = ps.p_l_follow;
     paren_target = -ps.paren_indents[ps.paren_level - 1];
-    paren_target = (ps.paren_level > 0 ? -ps.paren_indents[ps.paren_level - 1]
-                   : 0);
     not_first_line = 1;
     return;
     not_first_line = 1;
     return;
-};
+}
 
 compute_code_target()
 {
 
 compute_code_target()
 {
@@ -298,7 +308,7 @@ compute_label_target()
  * Willcox of CAC      Added check for switch back to partly full input
  * buffer from temporary buffer
  * 
  * Willcox of CAC      Added check for switch back to partly full input
  * buffer from temporary buffer
  * 
- */\f
+ */
 int
 fill_buffer()
 {                              /* this routine reads stuff from the input */
 int
 fill_buffer()
 {                              /* this routine reads stuff from the input */
@@ -314,7 +324,16 @@ fill_buffer()
            return;             /* only return if there is really something in
                                 * this buffer */
     }
            return;             /* only return if there is really something in
                                 * this buffer */
     }
-    for (p = buf_ptr = in_buffer;;) {
+    for (p = in_buffer;;) {
+       if (p >= in_buffer_limit) {
+           register size = (in_buffer_limit - in_buffer) * 2 + 10;
+           register offset = p - in_buffer;
+           in_buffer = (char *) realloc(in_buffer, size);
+           if (in_buffer == 0)
+               err("input line too long");
+           p = in_buffer + offset;
+           in_buffer_limit = in_buffer + size - 2;
+       }
        if ((i = getc(f)) == EOF) {
                *p++ = ' ';
                *p++ = '\n';
        if ((i = getc(f)) == EOF) {
                *p++ = ' ';
                *p++ = '\n';
@@ -325,8 +344,9 @@ fill_buffer()
        if (i == '\n')
                break;
     }
        if (i == '\n')
                break;
     }
+    buf_ptr = in_buffer;
     buf_end = p;
     buf_end = p;
-    if (p - in_buffer > 3 && p[-2] == '/' && p[-3] == '*') {
+    if (p[-2] == '/' && p[-3] == '*') {
        if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
            fill_buffer();      /* flush indent error message */
        else {
        if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
            fill_buffer();      /* flush indent error message */
        else {
@@ -374,7 +394,7 @@ fill_buffer()
        while (*p++ != '\n');
     }
     return;
        while (*p++ != '\n');
     }
     return;
-};
+}
 
 /*
  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
 
 /*
  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
@@ -403,7 +423,7 @@ fill_buffer()
  * 
  * HISTORY: initial coding     November 1976   D A Willcox of CAC
  * 
  * 
  * HISTORY: initial coding     November 1976   D A Willcox of CAC
  * 
- */\f
+ */
 pad_output(current, target)    /* writes tabs and blanks (if necessary) to
                                 * get the current output position up to the
                                 * target column */
 pad_output(current, target)    /* writes tabs and blanks (if necessary) to
                                 * get the current output position up to the
                                 * target column */
@@ -427,7 +447,7 @@ pad_output(current, target) /* writes tabs and blanks (if necessary) to
            putc(' ', output);  /* pad with final blanks */
     }
     return (target);
            putc(' ', output);  /* pad with final blanks */
     }
     return (target);
-};
+}
 
 /*
  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
 
 /*
  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
@@ -448,7 +468,7 @@ pad_output(current, target) /* writes tabs and blanks (if necessary) to
  * 
  * HISTORY: initial coding     November 1976   D A Willcox of CAC
  * 
  * 
  * HISTORY: initial coding     November 1976   D A Willcox of CAC
  * 
- */\f
+ */
 int
 count_spaces(current, buffer)
 /*
 int
 count_spaces(current, buffer)
 /*
@@ -475,7 +495,7 @@ count_spaces(current, buffer)
            cur = ((cur - 1) & tabmask) + tabsize + 1;
            break;
 
            cur = ((cur - 1) & tabmask) + tabsize + 1;
            break;
 
-       case '\b':               /* this is a backspace */
+       case 010:               /* backspace */
            --cur;
            break;
 
            --cur;
            break;
 
@@ -485,9 +505,10 @@ count_spaces(current, buffer)
        }                       /* end of switch */
     }                          /* end of for loop */
     return (cur);
        }                       /* end of switch */
     }                          /* end of for loop */
     return (cur);
-};
+}
 
 int    found_err;
 
 int    found_err;
+/* VARARGS2 */
 diag(level, msg, a, b)
 {
     if (level)
 diag(level, msg, a, b)
 {
     if (level)