X-Git-Url: https://git.subgeniuskitty.com/unix-history/.git/blobdiff_plain/ea136191f5bef8ea31d1566dd9b59967cc853517..0c8ee79d5a121a8130d3c5b55c9f17e4b053dd45:/usr/src/usr.bin/indent/indent.c diff --git a/usr/src/usr.bin/indent/indent.c b/usr/src/usr.bin/indent/indent.c index 6acfb6cbbb..c1103fbadf 100644 --- a/usr/src/usr.bin/indent/indent.c +++ b/usr/src/usr.bin/indent/indent.c @@ -27,12 +27,12 @@ char copyright[] = #endif /* not lint */ #ifndef lint -static char sccsid[] = "@(#)indent.c 5.10 (Berkeley) %G%"; +static char sccsid[] = "@(#)indent.c 5.13 (Berkeley) %G%"; #endif /* not lint */ +#include #include "indent_globs.h" #include "indent_codes.h" -#include #include char *in_name = "Standard Input"; /* will always point to name of input @@ -55,7 +55,6 @@ main(argc, argv) int hd_type; /* used to store type of stmt for if (...), * for (...), etc */ register int i; /* local loop counter */ - register int j; /* local loop counter */ int scase; /* set to true when we see a case, so we will * know what to do with the following colon */ int sp_sw; /* when true, we are in the expressin of @@ -81,9 +80,11 @@ main(argc, argv) 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_token = tokenbuf + bufsize - 5; combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and * comment buffers */ combuf[1] = codebuf[1] = labbuf[1] = '\0'; @@ -91,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_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; @@ -157,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) { /* 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 */ @@ -171,10 +173,8 @@ main(argc, argv) 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]); @@ -237,7 +237,7 @@ main(argc, argv) else break; p++; - }; + } if (col > ps.ind_size) ps.ind_level = ps.i_l_follow = col / ps.ind_size; } @@ -505,6 +505,7 @@ check_type: 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; @@ -570,7 +571,6 @@ check_type: break; case binary_op: /* any binary operation */ - do_binary: if (ps.want_blank) *e_code++ = ' '; { @@ -662,7 +662,7 @@ check_type: * 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; @@ -949,7 +949,7 @@ check_type: 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; @@ -966,6 +966,11 @@ check_type: 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++; @@ -1085,7 +1090,6 @@ check_type: * 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(); @@ -1100,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 */ -}; +} /* * copy input file to backup file if in_name is /blah/blah/blah/file, then @@ -1124,34 +1128,34 @@ bakcopy() /* 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)) - 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"); - 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) { - fprintf(stderr, "indent: can't create %s\n", in_name); 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); +}