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