*/
#ifndef lint
-static char sccsid[] = "@(#)args.c 5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)args.c 5.8 (Berkeley) %G%";
#endif /* not lint */
/*
#define STDIN 3 /* use stdin */
#define KEY 4 /* type (keyword) */
+char *option_source = "?";
+
/*
* N.B.: because of the way the table here is scanned, options whose names are
* substrings of other options must occur later; that is, with -lp vs -l, -lp
static char prof[] = ".indent.pro";
sprintf(fname, "%s/%s", getenv("HOME"), prof);
- if ((f = fopen(fname, "r")) != NULL) {
+ if ((f = fopen(option_source = fname, "r")) != NULL) {
scan_profile(f);
(void) fclose(f);
}
- if ((f = fopen(prof, "r")) != NULL) {
+ if ((f = fopen(option_source = prof, "r")) != NULL) {
scan_profile(f);
(void) fclose(f);
}
+ option_source = "Command line";
}
scan_profile(f)
for (p = pro; p->p_name; p++)
if (*p->p_name == *arg && eqin(p->p_name, arg))
goto found;
- fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1);
+ fprintf(stderr, "indent: %s: unknown parameter \"%s\"\n", option_source, arg - 1);
exit(1);
found:
switch (p->p_type) {
break;
case PRO_INT:
- if (*param_start == 0) {
+ if (!isdigit(*param_start)) {
need_param:
- fprintf(stderr, "indent: ``%s'' requires a parameter\n",
- arg - 1);
+ fprintf(stderr, "indent: %s: ``%s'' requires a parameter\n",
+ option_source, arg - 1);
exit(1);
}
*p->p_obj = atoi(param_start);
#endif /* not lint */
#ifndef lint
-static char sccsid[] = "@(#)indent.c 5.12 (Berkeley) %G%";
+static char sccsid[] = "@(#)indent.c 5.13 (Berkeley) %G%";
#endif /* not lint */
#include <sys/param.h>
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;
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;
* 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++;
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
- * @(#)indent_globs.h 5.7 (Berkeley) %G%
+ * @(#)indent_globs.h 5.8 (Berkeley) %G%
*/
#include <stdio.h>
#define BACKSLASH '\\'
#define bufsize 200 /* size of internal buffers */
-#define inp_bufs 600 /* size of input buffer */
#define sc_size 5000 /* size of save_com buffer */
#define label_offset 2 /* number of levels a label is placed to left
* of code */
char *e_com; /* ... and end of stored comments */
char *l_com; /* limit of comment buffer */
-char in_buffer[inp_bufs];/* input buffer */
+#define token s_token
+char *tokenbuf; /* the last token scanned */
+char *s_token;
+char *e_token;
+char *l_token;
+
+char *in_buffer; /* input buffer */
+char *in_buffer_limit; /* the end of the input buffer */
char *buf_ptr; /* ptr to next character to be taken from
* in_buffer */
char *buf_end; /* ptr to first after last char in in_buffer */
* from save_com */
char *be_save; /* similarly saved value of buf_end */
-char token[bufsize]; /* the last token scanned */
-
int pointer_as_binop;
int blanklines_after_declarations;
} ps;
int ifdef_level;
+int rparen_count;
struct parser_state state_stack[5];
struct parser_state match_state[5];
*/
#ifndef lint
-static char sccsid[] = "@(#)io.c 5.12 (Berkeley) %G%";
+static char sccsid[] = "@(#)io.c 5.13 (Berkeley) %G%";
#endif /* not lint */
#include "indent_globs.h"
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
* 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 */
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 == '\n')
break;
}
+ buf_ptr = in_buffer;
buf_end = p;
if (p[-2] == '/' && p[-3] == '*') {
if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
*
* 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 */
*
* HISTORY: initial coding November 1976 D A Willcox of CAC
*
- */\f
+ */
int
count_spaces(current, buffer)
/*
*/
#ifndef lint
-static char sccsid[] = "@(#)lexi.c 5.12 (Berkeley) %G%";
+static char sccsid[] = "@(#)lexi.c 5.13 (Berkeley) %G%";
#endif /* not lint */
/*
int
lexi()
{
- register char *tok; /* local pointer to next char in token */
int unary_delim; /* this is set to 1 if the current token
*
* forces a following operator to be unary */
int code; /* internal code to be returned */
char qchar; /* the delimiter character for a string */
- tok = token; /* point to start of place to save token */
+ e_token = s_token; /* point to start of place to save token */
unary_delim = false;
ps.col_1 = ps.last_nl; /* tell world that this token started in
* column 1 iff the last thing scanned was nl */
seenexp = 0;
if (*buf_ptr == '0' &&
(buf_ptr[1] == 'x' || buf_ptr[1] == 'X')) {
- *tok++ = *buf_ptr++;
- *tok++ = *buf_ptr++;
- while (isxdigit(*buf_ptr))
- *tok++ = *buf_ptr++;
+ *e_token++ = *buf_ptr++;
+ *e_token++ = *buf_ptr++;
+ while (isxdigit(*buf_ptr)) {
+ check_size(token);
+ *e_token++ = *buf_ptr++;
+ }
}
else
while (1) {
break;
else
seendot++;
- *tok++ = *buf_ptr++;
+ check_size(token);
+ *e_token++ = *buf_ptr++;
if (!isdigit(*buf_ptr) && *buf_ptr != '.')
if ((*buf_ptr != 'E' && *buf_ptr != 'e') || seenexp)
break;
else {
seenexp++;
seendot++;
- *tok++ = *buf_ptr++;
+ check_size(token);
+ *e_token++ = *buf_ptr++;
if (*buf_ptr == '+' || *buf_ptr == '-')
- *tok++ = *buf_ptr++;
+ *e_token++ = *buf_ptr++;
}
}
if (*buf_ptr == 'L' || *buf_ptr == 'l')
- *tok++ = *buf_ptr++;
+ *e_token++ = *buf_ptr++;
}
else
while (chartype[*buf_ptr] == alphanum) { /* copy it over */
- *tok++ = *buf_ptr++;
+ check_size(token);
+ *e_token++ = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
}
- *tok++ = '\0';
+ *e_token++ = '\0';
while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
if (++buf_ptr >= buf_end)
fill_buffer();
* This loop will check if the token is a keyword.
*/
for (p = specials; (j = p->rwd) != 0; p++) {
- tok = token; /* point at scanned token */
- if (*j++ != *tok++ || *j++ != *tok++)
+ register char *p = s_token; /* point at scanned token */
+ if (*j++ != *p++ || *j++ != *p++)
continue; /* This test depends on the fact that
* identifiers are always at least 1 character
* long (ie. the first two bytes of the
* identifier are always meaningful) */
- if (tok[-1] == 0)
+ if (p[-1] == 0)
break; /* If its a one-character identifier */
- while (*tok++ == *j)
+ while (*p++ == *j)
if (*j++ == 0)
goto found_keyword; /* I wish that C had a multi-level
* break... */
if (*buf_ptr == '(' && ps.tos <= 1 && ps.ind_level == 0) {
register char *tp = buf_ptr;
while (tp < buf_end)
- if (*tp++ == ')' && *tp == ';')
+ if (*tp++ == ')' && (*tp == ';' || *tp == ','))
goto not_proc;
strncpy(ps.procname, token, sizeof ps.procname - 1);
ps.in_parameter_declaration = 1;
+ rparen_count = 1;
not_proc:;
}
/*
last_code = ident;
return (ident); /* the ident is not in the list */
} /* end of procesing for alpanum character */
- /* l l l Scan a non-alphanumeric token */
- *tok++ = *buf_ptr; /* if it is only a one-character token, it is
+ /* Scan a non-alphanumeric token */
+
+ *e_token++ = *buf_ptr; /* if it is only a one-character token, it is
* moved here */
- *tok = '\0';
+ *e_token = '\0';
if (++buf_ptr >= buf_end)
fill_buffer();
case '"': /* start of string */
qchar = *token;
if (troff) {
- tok[-1] = '`';
+ e_token[-1] = '`';
if (qchar == '"')
- *tok++ = '`';
- tok = chfont(&bodyf, &stringf, tok);
+ *e_token++ = '`';
+ e_token = chfont(&bodyf, &stringf, e_token);
}
do { /* copy the string */
while (1) { /* move one character or [/<char>]<char> */
printf("%d: Unterminated literal\n", line_no);
goto stop_lit;
}
- *tok = *buf_ptr++;
+ check_size(token); /* Only have to do this once in this loop,
+ * since check_size guarantees that there
+ * are at least 5 entries left */
+ *e_token = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
- if (had_eof || ((tok - token) > (bufsize - 2))) {
- printf("Unterminated literal\n");
- ++tok;
- goto stop_lit;
- /* get outof literal copying loop */
- }
- if (*tok == BACKSLASH) { /* if escape, copy extra char */
+ if (*e_token == BACKSLASH) { /* if escape, copy extra char */
if (*buf_ptr == '\n') /* check for escaped newline */
++line_no;
if (troff) {
- *++tok = BACKSLASH;
+ *++e_token = BACKSLASH;
if (*buf_ptr == BACKSLASH)
- *++tok = BACKSLASH;
+ *++e_token = BACKSLASH;
}
- *++tok = *buf_ptr++;
- ++tok; /* we must increment this again because we
+ *++e_token = *buf_ptr++;
+ ++e_token; /* we must increment this again because we
* copied two chars */
if (buf_ptr >= buf_end)
fill_buffer();
else
break; /* we copied one character */
} /* end of while (1) */
- } while (*tok++ != qchar);
+ } while (*e_token++ != qchar);
if (troff) {
- tok = chfont(&stringf, &bodyf, tok - 1);
+ e_token = chfont(&stringf, &bodyf, e_token - 1);
if (qchar == '"')
- *tok++ = '\'';
+ *e_token++ = '\'';
}
stop_lit:
code = ident;
if (*buf_ptr == token[0]) {
/* check for doubled character */
- *tok++ = *buf_ptr++;
+ *e_token++ = *buf_ptr++;
/* buffer overflow will be checked at end of loop */
if (last_code == ident || last_code == rparen) {
code = (ps.last_u_d ? unary_op : postop);
}
else if (*buf_ptr == '=')
/* check for operator += */
- *tok++ = *buf_ptr++;
+ *e_token++ = *buf_ptr++;
else if (*buf_ptr == '>') {
/* check for operator -> */
- *tok++ = *buf_ptr++;
+ *e_token++ = *buf_ptr++;
if (!pointer_as_binop) {
unary_delim = false;
code = unary_op;
ps.block_init = 1;
#ifdef undef
if (chartype[*buf_ptr] == opchar) { /* we have two char assignment */
- tok[-1] = *buf_ptr++;
- if ((tok[-1] == '<' || tok[-1] == '>') && tok[-1] == *buf_ptr)
- *tok++ = *buf_ptr++;
- *tok++ = '='; /* Flip =+ to += */
- *tok = 0;
+ e_token[-1] = *buf_ptr++;
+ if ((e_token[-1] == '<' || e_token[-1] == '>') && e_token[-1] == *buf_ptr)
+ *e_token++ = *buf_ptr++;
+ *e_token++ = '='; /* Flip =+ to += */
+ *e_token = 0;
}
#else
if (*buf_ptr == '=') {/* == */
- *tok++ = '='; /* Flip =+ to += */
+ *e_token++ = '='; /* Flip =+ to += */
buf_ptr++;
- *tok = 0;
+ *e_token = 0;
}
#endif
code = binary_op;
case '<':
case '!': /* ops like <, <<, <=, !=, etc */
if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
- *tok++ = *buf_ptr;
+ *e_token++ = *buf_ptr;
if (++buf_ptr >= buf_end)
fill_buffer();
}
if (*buf_ptr == '=')
- *tok++ = *buf_ptr++;
+ *e_token++ = *buf_ptr++;
code = (ps.last_u_d ? unary_op : binary_op);
unary_delim = true;
break;
default:
if (token[0] == '/' && *buf_ptr == '*') {
/* it is start of comment */
- *tok++ = '*';
+ *e_token++ = '*';
if (++buf_ptr >= buf_end)
fill_buffer();
unary_delim = ps.last_u_d;
break;
}
- while (*(tok - 1) == *buf_ptr || *buf_ptr == '=') {
+ while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
/*
* handle ||, &&, etc, and also things as in int *****i
*/
- *tok++ = *buf_ptr;
+ *e_token++ = *buf_ptr;
if (++buf_ptr >= buf_end)
fill_buffer();
}
if (buf_ptr >= buf_end) /* check for input buffer empty */
fill_buffer();
ps.last_u_d = unary_delim;
- *tok = '\0'; /* null terminate the token */
+ *e_token = '\0'; /* null terminate the token */
return (code);
}