#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 <sys/param.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
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
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';
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;
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 */
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]);
else
break;
p++;
- };
+ }
if (col > ps.ind_size)
ps.ind_level = ps.i_l_follow = col / ps.ind_size;
}
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;
break;
case binary_op: /* any binary operation */
- do_binary:
if (ps.want_blank)
*e_code++ = ' ';
{
* 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;
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;
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++;
* 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 (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 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);
+}