xref: /original-bsd/usr.bin/indent/io.c (revision 36a8a07a)
1 /*
2  * Copyright (c) 1985 Sun Microsystems, Inc.
3  * Copyright (c) 1980, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  * All rights reserved.
6  *
7  * %sccs.include.redist.c%
8  */
9 
10 #ifndef lint
11 static char sccsid[] = "@(#)io.c	8.1 (Berkeley) 06/06/93";
12 #endif /* not lint */
13 
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "indent_globs.h"
19 
20 
21 int         comment_open;
22 static      paren_target;
23 
dump_line()24 dump_line()
25 {				/* dump_line is the routine that actually
26 				 * effects the printing of the new source. It
27 				 * prints the label section, followed by the
28 				 * code section with the appropriate nesting
29 				 * level, followed by any comments */
30     register int cur_col,
31                 target_col;
32     static      not_first_line;
33 
34     if (ps.procname[0]) {
35 	if (troff) {
36 	    if (comment_open) {
37 		comment_open = 0;
38 		fprintf(output, ".*/\n");
39 	    }
40 	    fprintf(output, ".Pr \"%s\"\n", ps.procname);
41 	}
42 	ps.ind_level = 0;
43 	ps.procname[0] = 0;
44     }
45     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
46 	if (suppress_blanklines > 0)
47 	    suppress_blanklines--;
48 	else {
49 	    ps.bl_line = true;
50 	    n_real_blanklines++;
51 	}
52     }
53     else if (!inhibit_formatting) {
54 	suppress_blanklines = 0;
55 	ps.bl_line = false;
56 	if (prefix_blankline_requested && not_first_line)
57 	    if (swallow_optional_blanklines) {
58 		if (n_real_blanklines == 1)
59 		    n_real_blanklines = 0;
60 	    }
61 	    else {
62 		if (n_real_blanklines == 0)
63 		    n_real_blanklines = 1;
64 	    }
65 	while (--n_real_blanklines >= 0)
66 	    putc('\n', output);
67 	n_real_blanklines = 0;
68 	if (ps.ind_level == 0)
69 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
70 				 * additional statement indentation if we are
71 				 * at bracket level 0 */
72 
73 	if (e_lab != s_lab || e_code != s_code)
74 	    ++code_lines;	/* keep count of lines with code */
75 
76 
77 	if (e_lab != s_lab) {	/* print lab, if any */
78 	    if (comment_open) {
79 		comment_open = 0;
80 		fprintf(output, ".*/\n");
81 	    }
82 	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
83 		e_lab--;
84 	    cur_col = pad_output(1, compute_label_target());
85 	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
86 				    || strncmp(s_lab, "#endif", 6) == 0)) {
87 		register char *s = s_lab;
88 		if (e_lab[-1] == '\n') e_lab--;
89 		do putc(*s++, output);
90 		while (s < e_lab && 'a' <= *s && *s<='z');
91 		while ((*s == ' ' || *s == '\t') && s < e_lab)
92 		    s++;
93 		if (s < e_lab)
94 		    fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
95 			    e_lab - s, s);
96 	    }
97 	    else fprintf(output, "%.*s", e_lab - s_lab, s_lab);
98 	    cur_col = count_spaces(cur_col, s_lab);
99 	}
100 	else
101 	    cur_col = 1;	/* there is no label section */
102 
103 	ps.pcase = false;
104 
105 	if (s_code != e_code) {	/* print code section, if any */
106 	    register char *p;
107 
108 	    if (comment_open) {
109 		comment_open = 0;
110 		fprintf(output, ".*/\n");
111 	    }
112 	    target_col = compute_code_target();
113 	    {
114 		register    i;
115 
116 		for (i = 0; i < ps.p_l_follow; i++)
117 		    if (ps.paren_indents[i] >= 0)
118 			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
119 	    }
120 	    cur_col = pad_output(cur_col, target_col);
121 	    for (p = s_code; p < e_code; p++)
122 		if (*p == (char) 0200)
123 		    fprintf(output, "%d", target_col * 7);
124 		else
125 		    putc(*p, output);
126 	    cur_col = count_spaces(cur_col, s_code);
127 	}
128 	if (s_com != e_com)
129 	    if (troff) {
130 		int         all_here = 0;
131 		register char *p;
132 
133 		if (e_com[-1] == '/' && e_com[-2] == '*')
134 		    e_com -= 2, all_here++;
135 		while (e_com > s_com && e_com[-1] == ' ')
136 		    e_com--;
137 		*e_com = 0;
138 		p = s_com;
139 		while (*p == ' ')
140 		    p++;
141 		if (p[0] == '/' && p[1] == '*')
142 		    p += 2, all_here++;
143 		else if (p[0] == '*')
144 		    p += p[1] == '/' ? 2 : 1;
145 		while (*p == ' ')
146 		    p++;
147 		if (*p == 0)
148 		    goto inhibit_newline;
149 		if (comment_open < 2 && ps.box_com) {
150 		    comment_open = 0;
151 		    fprintf(output, ".*/\n");
152 		}
153 		if (comment_open == 0) {
154 		    if ('a' <= *p && *p <= 'z')
155 			*p = *p + 'A' - 'a';
156 		    if (e_com - p < 50 && all_here == 2) {
157 			register char *follow = p;
158 			fprintf(output, "\n.nr C! \\w\1");
159 			while (follow < e_com) {
160 			    switch (*follow) {
161 			    case '\n':
162 				putc(' ', output);
163 			    case 1:
164 				break;
165 			    case '\\':
166 				putc('\\', output);
167 			    default:
168 				putc(*follow, output);
169 			    }
170 			    follow++;
171 			}
172 			putc(1, output);
173 		    }
174 		    fprintf(output, "\n./* %dp %d %dp\n",
175 			    ps.com_col * 7,
176 			    (s_code != e_code || s_lab != e_lab) - ps.box_com,
177 			    target_col * 7);
178 		}
179 		comment_open = 1 + ps.box_com;
180 		while (*p) {
181 		    if (*p == BACKSLASH)
182 			putc(BACKSLASH, output);
183 		    putc(*p++, output);
184 		}
185 	    }
186 	    else {		/* print comment, if any */
187 		register    target = ps.com_col;
188 		register char *com_st = s_com;
189 
190 		target += ps.comment_delta;
191 		while (*com_st == '\t')
192 		    com_st++, target += 8;	/* ? */
193 		while (target <= 0)
194 		    if (*com_st == ' ')
195 			target++, com_st++;
196 		    else if (*com_st == '\t')
197 			target = ((target - 1) & ~7) + 9, com_st++;
198 		    else
199 			target = 1;
200 		if (cur_col > target) {	/* if comment cant fit on this line,
201 					 * put it on next line */
202 		    putc('\n', output);
203 		    cur_col = 1;
204 		    ++ps.out_lines;
205 		}
206 		while (e_com > com_st && isspace(e_com[-1]))
207 		    e_com--;
208 		cur_col = pad_output(cur_col, target);
209 		if (!ps.box_com) {
210 		    if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1))
211 			if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
212 			    com_st[1] = '*';
213 			else
214 			    fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
215 		}
216 		fwrite(com_st, e_com - com_st, 1, output);
217 		ps.comment_delta = ps.n_comment_delta;
218 		cur_col = count_spaces(cur_col, com_st);
219 		++ps.com_lines;	/* count lines with comments */
220 	    }
221 	if (ps.use_ff)
222 	    putc('\014', output);
223 	else
224 	    putc('\n', output);
225 inhibit_newline:
226 	++ps.out_lines;
227 	if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
228 	    prefix_blankline_requested = 1;
229 	    ps.just_saw_decl = 0;
230 	}
231 	else
232 	    prefix_blankline_requested = postfix_blankline_requested;
233 	postfix_blankline_requested = 0;
234     }
235     ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
236 					 * declaration, remember that fact for
237 					 * proper comment indentation */
238     ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
239 						 * indented if we have not
240 						 * completed this stmt and if
241 						 * we are not in the middle of
242 						 * a declaration */
243     ps.use_ff = false;
244     ps.dumped_decl_indent = 0;
245     *(e_lab = s_lab) = '\0';	/* reset buffers */
246     *(e_code = s_code) = '\0';
247     *(e_com = s_com) = '\0';
248     ps.ind_level = ps.i_l_follow;
249     ps.paren_level = ps.p_l_follow;
250     paren_target = -ps.paren_indents[ps.paren_level - 1];
251     not_first_line = 1;
252     return;
253 }
254 
compute_code_target()255 compute_code_target()
256 {
257     register    target_col = ps.ind_size * ps.ind_level + 1;
258 
259     if (ps.paren_level)
260 	if (!lineup_to_parens)
261 	    target_col += continuation_indent * ps.paren_level;
262 	else {
263 	    register    w;
264 	    register    t = paren_target;
265 
266 	    if ((w = count_spaces(t, s_code) - max_col) > 0
267 		    && count_spaces(target_col, s_code) <= max_col) {
268 		t -= w + 1;
269 		if (t > target_col)
270 		    target_col = t;
271 	    }
272 	    else
273 		target_col = t;
274 	}
275     else if (ps.ind_stmt)
276 	target_col += continuation_indent;
277     return target_col;
278 }
279 
compute_label_target()280 compute_label_target()
281 {
282     return
283 	ps.pcase ? (int) (case_ind * ps.ind_size) + 1
284 	: *s_lab == '#' ? 1
285 	: ps.ind_size * (ps.ind_level - label_offset) + 1;
286 }
287 
288 
289 /*
290  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
291  *
292  * All rights reserved
293  *
294  *
295  * NAME: fill_buffer
296  *
297  * FUNCTION: Reads one block of input into input_buffer
298  *
299  * HISTORY: initial coding 	November 1976	D A Willcox of CAC 1/7/77 A
300  * Willcox of CAC	Added check for switch back to partly full input
301  * buffer from temporary buffer
302  *
303  */
304 int
fill_buffer()305 fill_buffer()
306 {				/* this routine reads stuff from the input */
307     register char *p;
308     register int i;
309     register FILE *f = input;
310 
311     if (bp_save != 0) {		/* there is a partly filled input buffer left */
312 	buf_ptr = bp_save;	/* dont read anything, just switch buffers */
313 	buf_end = be_save;
314 	bp_save = be_save = 0;
315 	if (buf_ptr < buf_end)
316 	    return;		/* only return if there is really something in
317 				 * this buffer */
318     }
319     for (p = in_buffer;;) {
320 	if (p >= in_buffer_limit) {
321 	    register size = (in_buffer_limit - in_buffer) * 2 + 10;
322 	    register offset = p - in_buffer;
323 	    in_buffer = (char *) realloc(in_buffer, size);
324 	    if (in_buffer == 0)
325 		err("input line too long");
326 	    p = in_buffer + offset;
327 	    in_buffer_limit = in_buffer + size - 2;
328 	}
329 	if ((i = getc(f)) == EOF) {
330 		*p++ = ' ';
331 		*p++ = '\n';
332 		had_eof = true;
333 		break;
334 	}
335 	*p++ = i;
336 	if (i == '\n')
337 		break;
338     }
339     buf_ptr = in_buffer;
340     buf_end = p;
341     if (p[-2] == '/' && p[-3] == '*') {
342 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
343 	    fill_buffer();	/* flush indent error message */
344 	else {
345 	    int         com = 0;
346 
347 	    p = in_buffer;
348 	    while (*p == ' ' || *p == '\t')
349 		p++;
350 	    if (*p == '/' && p[1] == '*') {
351 		p += 2;
352 		while (*p == ' ' || *p == '\t')
353 		    p++;
354 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
355 			&& p[4] == 'N' && p[5] == 'T') {
356 		    p += 6;
357 		    while (*p == ' ' || *p == '\t')
358 			p++;
359 		    if (*p == '*')
360 			com = 1;
361 		    else if (*p == 'O')
362 			if (*++p == 'N')
363 			    p++, com = 1;
364 			else if (*p == 'F' && *++p == 'F')
365 			    p++, com = 2;
366 		    while (*p == ' ' || *p == '\t')
367 			p++;
368 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
369 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
370 			    dump_line();
371 			if (!(inhibit_formatting = com - 1)) {
372 			    n_real_blanklines = 0;
373 			    postfix_blankline_requested = 0;
374 			    prefix_blankline_requested = 0;
375 			    suppress_blanklines = 1;
376 			}
377 		    }
378 		}
379 	    }
380 	}
381     }
382     if (inhibit_formatting) {
383 	p = in_buffer;
384 	do
385 	    putc(*p, output);
386 	while (*p++ != '\n');
387     }
388     return;
389 }
390 
391 /*
392  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
393  *
394  * All rights reserved
395  *
396  *
397  * NAME: pad_output
398  *
399  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
400  * position.
401  *
402  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
403  *
404  * PARAMETERS: current		integer		The current column target
405  * nteger		The desired column
406  *
407  * RETURNS: Integer value of the new column.  (If current >= target, no action is
408  * taken, and current is returned.
409  *
410  * GLOBALS: None
411  *
412  * CALLS: write (sys)
413  *
414  * CALLED BY: dump_line
415  *
416  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
417  *
418  */
pad_output(current,target)419 pad_output(current, target)	/* writes tabs and blanks (if necessary) to
420 				 * get the current output position up to the
421 				 * target column */
422     int         current;	/* the current column value */
423     int         target;		/* position we want it at */
424 {
425     register int curr;		/* internal column pointer */
426     register int tcur;
427 
428     if (troff)
429 	fprintf(output, "\\h'|%dp'", (target - 1) * 7);
430     else {
431 	if (current >= target)
432 	    return (current);	/* line is already long enough */
433 	curr = current;
434 	while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
435 	    putc('\t', output);
436 	    curr = tcur;
437 	}
438 	while (curr++ < target)
439 	    putc(' ', output);	/* pad with final blanks */
440     }
441     return (target);
442 }
443 
444 /*
445  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
446  *
447  * All rights reserved
448  *
449  *
450  * NAME: count_spaces
451  *
452  * FUNCTION: Find out where printing of a given string will leave the current
453  * character position on output.
454  *
455  * ALGORITHM: Run thru input string and add appropriate values to current
456  * position.
457  *
458  * RETURNS: Integer value of position after printing "buffer" starting in column
459  * "current".
460  *
461  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
462  *
463  */
464 int
count_spaces(current,buffer)465 count_spaces(current, buffer)
466 /*
467  * this routine figures out where the character position will be after
468  * printing the text in buffer starting at column "current"
469  */
470     int         current;
471     char       *buffer;
472 {
473     register char *buf;		/* used to look thru buffer */
474     register int cur;		/* current character counter */
475 
476     cur = current;
477 
478     for (buf = buffer; *buf != '\0'; ++buf) {
479 	switch (*buf) {
480 
481 	case '\n':
482 	case 014:		/* form feed */
483 	    cur = 1;
484 	    break;
485 
486 	case '\t':
487 	    cur = ((cur - 1) & tabmask) + tabsize + 1;
488 	    break;
489 
490 	case 010:		/* backspace */
491 	    --cur;
492 	    break;
493 
494 	default:
495 	    ++cur;
496 	    break;
497 	}			/* end of switch */
498     }				/* end of for loop */
499     return (cur);
500 }
501 
502 int	found_err;
503 /* VARARGS2 */
diag(level,msg,a,b)504 diag(level, msg, a, b)
505 	char *msg;
506 {
507     if (level)
508 	found_err = 1;
509     if (output == stdout) {
510 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
511 	fprintf(stdout, msg, a, b);
512 	fprintf(stdout, " */\n");
513     }
514     else {
515 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
516 	fprintf(stderr, msg, a, b);
517 	fprintf(stderr, "\n");
518     }
519 }
520 
writefdef(f,nm)521 writefdef(f, nm)
522     register struct fstate *f;
523 {
524     fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
525 	    nm, f->font, nm, f->size);
526 }
527 
528 char       *
chfont(of,nf,s)529 chfont(of, nf, s)
530     register struct fstate *of,
531                *nf;
532     char       *s;
533 {
534     if (of->font[0] != nf->font[0]
535 	    || of->font[1] != nf->font[1]) {
536 	*s++ = '\\';
537 	*s++ = 'f';
538 	if (nf->font[1]) {
539 	    *s++ = '(';
540 	    *s++ = nf->font[0];
541 	    *s++ = nf->font[1];
542 	}
543 	else
544 	    *s++ = nf->font[0];
545     }
546     if (nf->size != of->size) {
547 	*s++ = '\\';
548 	*s++ = 's';
549 	if (nf->size < of->size) {
550 	    *s++ = '-';
551 	    *s++ = '0' + of->size - nf->size;
552 	}
553 	else {
554 	    *s++ = '+';
555 	    *s++ = '0' + nf->size - of->size;
556 	}
557     }
558     return s;
559 }
560 
561 
parsefont(f,s0)562 parsefont(f, s0)
563     register struct fstate *f;
564     char       *s0;
565 {
566     register char *s = s0;
567     int         sizedelta = 0;
568     bzero(f, sizeof *f);
569     while (*s) {
570 	if (isdigit(*s))
571 	    f->size = f->size * 10 + *s - '0';
572 	else if (isupper(*s))
573 	    if (f->font[0])
574 		f->font[1] = *s;
575 	    else
576 		f->font[0] = *s;
577 	else if (*s == 'c')
578 	    f->allcaps = 1;
579 	else if (*s == '+')
580 	    sizedelta++;
581 	else if (*s == '-')
582 	    sizedelta--;
583 	else {
584 	    fprintf(stderr, "indent: bad font specification: %s\n", s0);
585 	    exit(1);
586 	}
587 	s++;
588     }
589     if (f->font[0] == 0)
590 	f->font[0] = 'R';
591     if (bodyf.size == 0)
592 	bodyf.size = 11;
593     if (f->size == 0)
594 	f->size = bodyf.size + sizedelta;
595     else if (sizedelta > 0)
596 	f->size += bodyf.size;
597     else
598 	f->size = bodyf.size - f->size;
599 }
600