xref: /csrg-svn/usr.bin/indent/io.c (revision 35500)
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.8 (Berkeley) 09/15/88";
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                 temp_col,
40                 target_col;
41     static      not_first_line;
42 
43     if (ps.procname[0]) {
44 	if (troff) {
45 	    if (comment_open) {
46 		comment_open = 0;
47 		fprintf(output, ".*/\n");
48 	    }
49 	    fprintf(output, ".Pr \"%s\"\n", ps.procname);
50 	}
51 	ps.ind_level = 0;
52 	ps.procname[0] = 0;
53     }
54     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
55 	if (suppress_blanklines > 0)
56 	    suppress_blanklines--;
57 	else {
58 	    ps.bl_line = true;
59 	    n_real_blanklines++;
60 	}
61     }
62     else if (!inhibit_formatting) {
63 	suppress_blanklines = 0;
64 	ps.bl_line = false;
65 	if (prefix_blankline_requested && not_first_line)
66 	    if (swallow_optional_blanklines) {
67 		if (n_real_blanklines == 1)
68 		    n_real_blanklines = 0;
69 	    }
70 	    else {
71 		if (n_real_blanklines == 0)
72 		    n_real_blanklines = 1;
73 	    }
74 	while (--n_real_blanklines >= 0)
75 	    putc('\n', output);
76 	n_real_blanklines = 0;
77 	if (ps.ind_level == 0)
78 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
79 				 * additional statement indentation if we are
80 				 * at bracket level 0 */
81 
82 	if (e_lab != s_lab || e_code != s_code)
83 	    ++code_lines;	/* keep count of lines with code */
84 
85 
86 	if (e_lab != s_lab) {	/* print lab, if any */
87 	    if (comment_open) {
88 		comment_open = 0;
89 		fprintf(output, ".*/\n");
90 	    }
91 	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
92 		e_lab--;
93 	    cur_col = pad_output(1, compute_label_target());
94 	    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     int         count;
305     register char *p;
306     register int i;
307     register FILE *f = input;
308 
309     if (bp_save != 0) {		/* there is a partly filled input buffer left */
310 	buf_ptr = bp_save;	/* dont read anything, just switch buffers */
311 	buf_end = be_save;
312 	bp_save = be_save = 0;
313 	if (buf_ptr < buf_end)
314 	    return;		/* only return if there is really something in
315 				 * this buffer */
316     }
317     p = in_buffer;
318     buf_ptr = p;
319     while ((*p++ = i = getc(f)) != EOF && i != '\n');
320     if (i == EOF) {
321 	p[-1] = ' ';
322 	*p++ = '\n';
323 	had_eof = true;
324     }
325     buf_end = p;
326     if (p[-2] == '/' && p[-3] == '*') {
327 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
328 	    fill_buffer();	/* flush indent error message */
329 	else {
330 	    int         com = 0;
331 
332 	    p = in_buffer;
333 	    while (*p == ' ' || *p == '\t')
334 		p++;
335 	    if (*p == '/' && p[1] == '*') {
336 		p += 2;
337 		while (*p == ' ' || *p == '\t')
338 		    p++;
339 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
340 			&& p[4] == 'N' && p[5] == 'T') {
341 		    p += 6;
342 		    while (*p == ' ' || *p == '\t')
343 			p++;
344 		    if (*p == '*')
345 			com = 1;
346 		    else if (*p == 'O')
347 			if (*++p == 'N')
348 			    p++, com = 1;
349 			else if (*p == 'F' && *++p == 'F')
350 			    p++, com = 2;
351 		    while (*p == ' ' || *p == '\t')
352 			p++;
353 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
354 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
355 			    dump_line();
356 			if (!(inhibit_formatting = com - 1)) {
357 			    n_real_blanklines = 0;
358 			    postfix_blankline_requested = 0;
359 			    prefix_blankline_requested = 0;
360 			    suppress_blanklines = 1;
361 			}
362 		    }
363 		}
364 	    }
365 	}
366     }
367     if (inhibit_formatting) {
368 	p = in_buffer;
369 	do
370 	    putc(*p, output);
371 	while (*p++ != '\n');
372     }
373     return;
374 };
375 
376 /*
377  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
378  *
379  * All rights reserved
380  *
381  *
382  * NAME: pad_output
383  *
384  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
385  * position.
386  *
387  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
388  *
389  * PARAMETERS: current		integer		The current column target
390  * nteger		The desired column
391  *
392  * RETURNS: Integer value of the new column.  (If current >= target, no action is
393  * taken, and current is returned.
394  *
395  * GLOBALS: None
396  *
397  * CALLS: write (sys)
398  *
399  * CALLED BY: dump_line
400  *
401  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
402  *
403  */
404 pad_output(current, target)	/* writes tabs and blanks (if necessary) to
405 				 * get the current output position up to the
406 				 * target column */
407     int         current;	/* the current column value */
408     int         target;		/* position we want it at */
409 {
410     register int curr;		/* internal column pointer */
411     register int tcur;
412 
413     if (troff)
414 	fprintf(output, "\\h'|%dp'", (target - 1) * 7);
415     else {
416 	if (current >= target)
417 	    return (current);	/* line is already long enough */
418 	curr = current;
419 	while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
420 	    putc('\t', output);
421 	    curr = tcur;
422 	}
423 	while (curr++ < target)
424 	    putc(' ', output);	/* pad with final blanks */
425     }
426     return (target);
427 };
428 
429 /*
430  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
431  *
432  * All rights reserved
433  *
434  *
435  * NAME: count_spaces
436  *
437  * FUNCTION: Find out where printing of a given string will leave the current
438  * character position on output.
439  *
440  * ALGORITHM: Run thru input string and add appropriate values to current
441  * position.
442  *
443  * RETURNS: Integer value of position after printing "buffer" starting in column
444  * "current".
445  *
446  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
447  *
448  */
449 int
450 count_spaces(current, buffer)
451 /*
452  * this routine figures out where the character position will be after
453  * printing the text in buffer starting at column "current"
454  */
455     int         current;
456     char       *buffer;
457 {
458     register char *buf;		/* used to look thru buffer */
459     register int cur;		/* current character counter */
460 
461     cur = current;
462 
463     for (buf = buffer; *buf != '\0'; ++buf) {
464 	switch (*buf) {
465 
466 	case '\n':
467 	case 014:		/* form feed */
468 	    cur = 1;
469 	    break;
470 
471 	case '\t':
472 	    cur = ((cur - 1) & tabmask) + tabsize + 1;
473 	    break;
474 
475 	case '':		/* this is a backspace */
476 	    --cur;
477 	    break;
478 
479 	default:
480 	    ++cur;
481 	    break;
482 	}			/* end of switch */
483     }				/* end of for loop */
484     return (cur);
485 };
486 
487 diag(level, msg, a, b)
488 {
489     if (output == stdout) {
490 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
491 	fprintf(stdout, msg, a, b);
492 	fprintf(stdout, " */\n");
493     }
494     else {
495 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
496 	fprintf(stderr, msg, a, b);
497 	fprintf(stderr, "\n");
498     }
499 }
500 
501 writefdef(f, nm)
502     register struct fstate *f;
503 {
504     fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
505 	    nm, f->font, nm, f->size);
506 }
507 
508 char       *
509 chfont(of, nf, s)
510     register struct fstate *of,
511                *nf;
512     char       *s;
513 {
514     if (of->font[0] != nf->font[0]
515 	    || of->font[1] != nf->font[1]) {
516 	*s++ = '\\';
517 	*s++ = 'f';
518 	if (nf->font[1]) {
519 	    *s++ = '(';
520 	    *s++ = nf->font[0];
521 	    *s++ = nf->font[1];
522 	}
523 	else
524 	    *s++ = nf->font[0];
525     }
526     if (nf->size != of->size) {
527 	*s++ = '\\';
528 	*s++ = 's';
529 	if (nf->size < of->size) {
530 	    *s++ = '-';
531 	    *s++ = '0' + of->size - nf->size;
532 	}
533 	else {
534 	    *s++ = '+';
535 	    *s++ = '0' + nf->size - of->size;
536 	}
537     }
538     return s;
539 }
540 
541 
542 setfont(f)
543     register struct fstate *f;
544 {
545     if (f->font[0] != ps.cfont.font[0]
546 	    || f->font[1] != ps.cfont.font[1])
547 	fprintf(output, f->font[1] ? "\\f(%s" : "\\f%s", f->font);
548     if (f->size != ps.cfont.size)
549 	fprintf(output, "\\s%d", f->size);
550     ps.cfont = *f;
551 }
552 
553 parsefont(f, s0)
554     register struct fstate *f;
555     char       *s0;
556 {
557     register char *s = s0;
558     int         sizedelta = 0;
559     bzero(f, sizeof *f);
560     while (*s) {
561 	if (isdigit(*s))
562 	    f->size = f->size * 10 + *s - '0';
563 	else if (isupper(*s))
564 	    if (f->font[0])
565 		f->font[1] = *s;
566 	    else
567 		f->font[0] = *s;
568 	else if (*s == 'c')
569 	    f->allcaps = 1;
570 	else if (*s == '+')
571 	    sizedelta++;
572 	else if (*s == '-')
573 	    sizedelta--;
574 	else {
575 	    fprintf(stderr, "indent: bad font specification: %s\n", s0);
576 	    exit(1);
577 	}
578 	s++;
579     }
580     if (f->font[0] == 0)
581 	f->font[0] = 'R';
582     if (bodyf.size == 0)
583 	bodyf.size = 11;
584     if (f->size == 0)
585 	f->size = bodyf.size + sizedelta;
586     else if (sizedelta > 0)
587 	f->size += bodyf.size;
588     else
589 	f->size = bodyf.size - f->size;
590 }
591