2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980, 1993
4 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34static char sccsid[] =
"@(#)io.c 8.1 (Berkeley) 6/6/93";
62{
/* dump_line is the routine that actually
63 * effects the printing of the new source. It
64 * prints the label section, followed by the
65 * code section with the appropriate nesting
66 * level, followed by any comments */
69 static int not_first_line;
100 ps.
ind_stmt = 0;
/* this is a class A kludge. dont do
101 * additional statement indentation if we are
102 * at bracket level 0 */
105 ++
code_lines;
/* keep count of lines with code */
117 if (
s_lab[0] ==
'#' && (strncmp(
s_lab,
"#else", 5) == 0
118 || strncmp(
s_lab,
"#endif", 6) == 0)) {
122 while (s <
e_lab &&
'a' <= *s && *s<=
'z');
123 while ((*s ==
' ' || *s ==
'\t') && s <
e_lab)
126 fprintf(
output, s[0]==
'/' && s[1]==
'*' ?
"\t%.*s" :
"\t/* %.*s */",
127 (
int)(
e_lab - s), s);
133 cur_col = 1;
/* there is no label section */
137 if (
s_code !=
e_code) {
/* print code section, if any */
154 if (*p == (
char) 0200)
160 if (
s_com !=
e_com) {
/* print comment, if any */
162 char *com_st =
s_com;
165 while (*com_st ==
'\t')
/* consider original indentation in
166 * case this is a box comment */
171 else if (*com_st ==
'\t')
175 if (cur_col > target) {
/* if comment can't fit on this line,
176 * put it on next line */
181 while (
e_com > com_st && isspace((
unsigned char)
e_com[-1]))
202 * declaration, remember that fact for
203 * proper comment indentation */
204 /* next line should be indented if we have not completed this stmt, and
205 * either we are not in a declaration or we are in an initialization
206 * assignment; but not if we're within braces in an initialization,
207 * because that scenario is handled by other rules. */
261 * Read data ahead of what has been collected into in_buffer.
263 * Successive calls get further and further ahead, until we hit EOF.
264 * Call lookahead_reset() to rescan from just beyond in_buffer.
266 * Lookahead is automatically reset whenever fill_buffer() reads beyond
267 * the lookahead buffer, i.e., you can't use this for "look behind".
269 * The standard pattern for potentially multi-line lookahead is to call
270 * lookahead_reset(), then enter a loop that scans forward from buf_ptr
271 * to buf_end, then (if necessary) calls lookahead() to read additional
272 * characters from beyond the end of the current line.
277 /* First read whatever's in bp_save area */
280 /* Else, we have to examine and probably fill the main lookahead buffer */
287 continue;
/* fill_buffer drops nulls, and so do we */
290 /* Need to allocate or enlarge lookahead_buf */
302 errx(1,
"too much lookahead required");
316 * Reset so that lookahead() will again scan from just beyond what's in
322 /* Reset the main lookahead buffer */
324 /* If bp_save isn't NULL, we need to scan that first */
329 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
331 * All rights reserved
336 * FUNCTION: Reads one line of input into in_buffer,
337 * sets up buf_ptr and buf_end to point to the line's start and end+1.
338 * (Note that the buffer does not get null-terminated.)
340 * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A
341 * Willcox of CAC Added check for switch back to partly full input
342 * buffer from temporary buffer
347{
/* this routine reads stuff from the input */
352 if (
bp_save != NULL) {
/* there is a partly filled input buffer left */
353 buf_ptr =
bp_save;
/* do not read anything, just switch buffers */
358 return;
/* only return if there is really something in
367 errx(1,
"input line too long");
375 if ((
i = getc(f)) == EOF) {
389 if (p -
in_buffer > 2 && p[-2] ==
'/' && p[-3] ==
'*') {
396 while (*p ==
' ' || *p ==
'\t')
398 if (*p ==
'/' && p[1] ==
'*') {
400 while (*p ==
' ' || *p ==
'\t')
402 if (p[0] ==
'I' && p[1] ==
'N' && p[2] ==
'D' && p[3] ==
'E'
403 && p[4] ==
'N' && p[5] ==
'T') {
405 while (*p ==
' ' || *p ==
'\t')
409 else if (*p ==
'O') {
412 else if (*p ==
'F' && *++p ==
'F')
415 while (*p ==
' ' || *p ==
'\t')
417 if (p[0] ==
'*' && p[1] ==
'/' && p[2] ==
'\n' && com) {
435 while (*p++ !=
'\n');
440 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
442 * All rights reserved
447 * FUNCTION: Writes tabs and spaces to move the current column up to the desired
450 * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
452 * PARAMETERS: current integer The current column
453 * target integer The desired column
455 * RETURNS: Integer value of the new column. (If current >= target, no action is
456 * taken, and current is returned.
462 * CALLED BY: dump_line
464 * HISTORY: initial coding November 1976 D A Willcox of CAC
469 /* writes tabs and blanks (if necessary) to
470 * get the current output position up to the
472 /* current: the current column value */
473 /* target: position we want it at */
475 int curr;
/* internal column pointer */
477 if (current >= target)
478 return (current);
/* line is already long enough */
483 while ((tcur =
tabsize * (1 + (curr - 1) /
tabsize) + 1) <= target) {
490 while (curr++ < target)
491 putc(
' ',
output);
/* pad with final blanks */
497 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
499 * All rights reserved
504 * FUNCTION: Find out where printing of a given string will leave the current
505 * character position on output.
507 * ALGORITHM: Run thru input string and add appropriate values to current
510 * RETURNS: Integer value of position after printing "buffer" starting in column
513 * HISTORY: initial coding November 1976 D A Willcox of CAC
519 * this routine figures out where the character position will be after
520 * printing the text in buffer starting at column "current"
523 char *
buf;
/* used to look thru buffer */
529 case 014:
/* form feed */
537 case 010:
/* backspace */
544 }
/* end of switch */
545 }
/* end of for loop */
556 diag4(
int level,
const char *msg,
int a,
int b)
566 fprintf(stderr,
"%s@%d: ", level == 0 ?
"Warning" :
"Error",
line_no);
583 fprintf(stderr,
"%s@%d: ", level == 0 ?
"Warning" :
"Error",
line_no);
600 fprintf(stderr,
"%s@%d: ", level == 0 ?
"Warning" :
"Error",
line_no);
#define fprintf(file, fmt, msg)
void errx(int eval, const char *fmt,...)
int blanklines_after_declarations
int postfix_blankline_requested
int prefix_blankline_requested
int swallow_optional_blanklines
int lineup_to_parens_always
static char * lookahead_buf_end
static char * lookahead_bp_save
int compute_code_target(void)
void diag3(int level, const char *msg, int a)
int compute_label_target(void)
int count_spaces(int cur, char *buffer)
int count_spaces_until(int cur, char *buffer, char *end)
static char * lookahead_start
static char * lookahead_buf
void diag2(int level, const char *msg)
static int pad_output(int current, int target)
void lookahead_reset(void)
void diag4(int level, const char *msg, int a, int b)
static char * lookahead_ptr
static char * lookahead_end