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