new version from James Gosling including various bug fixes
[unix-history] / usr / src / usr.bin / indent / indent.c
index b56aa94..c1103fb 100644 (file)
@@ -27,12 +27,12 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)indent.c   5.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)indent.c   5.13 (Berkeley) %G%";
 #endif /* not lint */
 
 #endif /* not lint */
 
+#include <sys/param.h>
 #include "indent_globs.h"
 #include "indent_codes.h"
 #include "indent_globs.h"
 #include "indent_codes.h"
-#include <sys/param.h>
 #include <ctype.h>
 
 char       *in_name = "Standard Input";        /* will always point to name of input
 #include <ctype.h>
 
 char       *in_name = "Standard Input";        /* will always point to name of input
@@ -80,9 +80,11 @@ main(argc, argv)
     combuf = (char *) malloc(bufsize);
     labbuf = (char *) malloc(bufsize);
     codebuf = (char *) malloc(bufsize);
     combuf = (char *) malloc(bufsize);
     labbuf = (char *) malloc(bufsize);
     codebuf = (char *) malloc(bufsize);
+    tokenbuf = (char *) malloc(bufsize);
     l_com = combuf + bufsize - 5;
     l_lab = labbuf + bufsize - 5;
     l_code = codebuf + bufsize - 5;
     l_com = combuf + bufsize - 5;
     l_lab = labbuf + bufsize - 5;
     l_code = codebuf + bufsize - 5;
+    l_token = tokenbuf + bufsize - 5;
     combuf[0] = codebuf[0] = labbuf[0] = ' ';  /* set up code, label, and
                                                 * comment buffers */
     combuf[1] = codebuf[1] = labbuf[1] = '\0';
     combuf[0] = codebuf[0] = labbuf[0] = ' ';  /* set up code, label, and
                                                 * comment buffers */
     combuf[1] = codebuf[1] = labbuf[1] = '\0';
@@ -90,7 +92,10 @@ main(argc, argv)
     s_lab = e_lab = labbuf + 1;
     s_code = e_code = codebuf + 1;
     s_com = e_com = combuf + 1;
     s_lab = e_lab = labbuf + 1;
     s_code = e_code = codebuf + 1;
     s_com = e_com = combuf + 1;
+    s_token = e_token = tokenbuf + 1;
 
 
+    in_buffer = (char *) malloc(10);
+    in_buffer_limit = in_buffer + 8;
     buf_ptr = buf_end = in_buffer;
     line_no = 1;
     had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
     buf_ptr = buf_end = in_buffer;
     line_no = 1;
     had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
@@ -156,10 +161,8 @@ main(argc, argv)
            if (input == 0) {   /* we must have the input file */
                in_name = argv[i];      /* remember name of input file */
                input = fopen(in_name, "r");
            if (input == 0) {   /* we must have the input file */
                in_name = argv[i];      /* remember name of input file */
                input = fopen(in_name, "r");
-               if (input == 0) {       /* check for open error */
-                   fprintf(stderr, "indent: can't open %s\n", argv[i]);
-                   exit(1);
-               }
+               if (input == 0)         /* check for open error */
+                       err(in_name);
                continue;
            }
            else if (output == 0) {     /* we have the output file */
                continue;
            }
            else if (output == 0) {     /* we have the output file */
@@ -170,10 +173,8 @@ main(argc, argv)
                    exit(1);
                }
                output = fopen(out_name, "w");
                    exit(1);
                }
                output = fopen(out_name, "w");
-               if (output == 0) {      /* check for create error */
-                   fprintf(stderr, "indent: can't create %s\n", argv[i]);
-                   exit(1);
-               }
+               if (output == 0)        /* check for create error */
+                       err(out_name);
                continue;
            }
            fprintf(stderr, "indent: unknown parameter: %s\n", argv[i]);
                continue;
            }
            fprintf(stderr, "indent: unknown parameter: %s\n", argv[i]);
@@ -236,7 +237,7 @@ main(argc, argv)
            else
                break;
            p++;
            else
                break;
            p++;
-       };
+       }
        if (col > ps.ind_size)
            ps.ind_level = ps.i_l_follow = col / ps.ind_size;
     }
        if (col > ps.ind_size)
            ps.ind_level = ps.i_l_follow = col / ps.ind_size;
     }
@@ -504,6 +505,7 @@ check_type:
            break;
 
        case rparen:            /* got a ')' or ']' */
            break;
 
        case rparen:            /* got a ')' or ']' */
+           rparen_count--;
            if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
                ps.last_u_d = true;
                ps.cast_mask &= (1 << ps.p_l_follow) - 1;
            if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
                ps.last_u_d = true;
                ps.cast_mask &= (1 << ps.p_l_follow) - 1;
@@ -569,7 +571,6 @@ check_type:
            break;
 
        case binary_op: /* any binary operation */
            break;
 
        case binary_op: /* any binary operation */
-    do_binary:
            if (ps.want_blank)
                *e_code++ = ' ';
            {
            if (ps.want_blank)
                *e_code++ = ' ';
            {
@@ -661,7 +662,7 @@ check_type:
                                 * structure declaration */
            scase = false;      /* these will only need resetting in a error */
            squest = 0;
                                 * structure declaration */
            scase = false;      /* these will only need resetting in a error */
            squest = 0;
-           if (ps.last_token == rparen)
+           if (ps.last_token == rparen && rparen_count == 0)
                ps.in_parameter_declaration = 0;
            ps.cast_mask = 0;
            ps.sizeof_mask = 0;
                ps.in_parameter_declaration = 0;
            ps.cast_mask = 0;
            ps.sizeof_mask = 0;
@@ -948,7 +949,7 @@ check_type:
            if (ps.p_l_follow == 0) {
                if (ps.block_init_level <= 0)
                    ps.block_init = 0;
            if (ps.p_l_follow == 0) {
                if (ps.block_init_level <= 0)
                    ps.block_init = 0;
-               if (break_comma && !ps.leave_comma)
+               if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8))
                    force_nl = true;
            }
            break;
                    force_nl = true;
            }
            break;
@@ -965,6 +966,11 @@ check_type:
                char        quote = 0;
                int         com_end = 0;
 
                char        quote = 0;
                int         com_end = 0;
 
+               while (*buf_ptr == ' ' || *buf_ptr == '\t') {
+                   buf_ptr++;
+                   if (buf_ptr >= buf_end)
+                       fill_buffer();
+               }
                while (*buf_ptr != '\n' || in_comment) {
                    check_size(lab);
                    *e_lab = *buf_ptr++;
                while (*buf_ptr != '\n' || in_comment) {
                    check_size(lab);
                    *e_lab = *buf_ptr++;
@@ -1084,7 +1090,6 @@ check_type:
                                 * character will cause the line to be printed */
 
        case comment:           /* we have gotten a /*  this is a biggie */
                                 * character will cause the line to be printed */
 
        case comment:           /* we have gotten a /*  this is a biggie */
-    proc_comment:
            if (flushed_nl) {   /* we should force a broken line here */
                flushed_nl = false;
                dump_line();
            if (flushed_nl) {   /* we should force a broken line here */
                flushed_nl = false;
                dump_line();
@@ -1099,7 +1104,7 @@ check_type:
        if (type_code != comment && type_code != newline && type_code != preesc)
            ps.last_token = type_code;
     }                          /* end of main while (1) loop */
        if (type_code != comment && type_code != newline && type_code != preesc)
            ps.last_token = type_code;
     }                          /* end of main while (1) loop */
-};
+}
 
 /*
  * copy input file to backup file if in_name is /blah/blah/blah/file, then
 
 /*
  * copy input file to backup file if in_name is /blah/blah/blah/file, then
@@ -1123,34 +1128,34 @@ bakcopy()
 
     /* copy in_name to backup file */
     bakchn = creat(bakfile, 0600);
 
     /* copy in_name to backup file */
     bakchn = creat(bakfile, 0600);
-    if (bakchn < 0) {
-       fprintf(stderr, "indent: can't create backup file \"%s\"\n", bakfile);
-       exit(1);
-    }
+    if (bakchn < 0)
+       err(bakfile);
     while (n = read(fileno(input), buff, sizeof buff))
     while (n = read(fileno(input), buff, sizeof buff))
-       if (write(bakchn, buff, n) != n) {
-           fprintf(stderr, "indent: error writing backup file \"%s\"\n",
-                   bakfile);
-           exit(1);
-       }
-    if (n < 0) {
-       fprintf(stderr, "indent: error reading input file \"%s\"\n", in_name);
-       exit(1);
-    }
+       if (write(bakchn, buff, n) != n)
+           err(bakfile);
+    if (n < 0)
+       err(in_name);
     close(bakchn);
     fclose(input);
 
     /* re-open backup file as the input file */
     input = fopen(bakfile, "r");
     close(bakchn);
     fclose(input);
 
     /* re-open backup file as the input file */
     input = fopen(bakfile, "r");
-    if (input == 0) {
-       fprintf(stderr, "indent: can't re-open backup file\n");
-       exit(1);
-    }
+    if (input == 0)
+       err(bakfile);
     /* now the original input file will be the output */
     output = fopen(in_name, "w");
     if (output == 0) {
     /* now the original input file will be the output */
     output = fopen(in_name, "w");
     if (output == 0) {
-       fprintf(stderr, "indent: can't create %s\n", in_name);
        unlink(bakfile);
        unlink(bakfile);
-       exit(1);
+       err(in_name);
     }
 }
     }
 }
+
+err(msg)
+       char *msg;
+{
+       extern int errno;
+       char *strerror();
+
+       (void)fprintf(stderr, "indent: %s: %s\n", msg, strerror(errno));
+       exit(1);
+}