lint cleanup
[unix-history] / usr / src / usr.bin / indent / io.c
CommitLineData
c0bc4ef7 1/*
30f48914
KB
2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980 The Regents of the University of California.
b0627149
KB
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
b36fc510
KB
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
30f48914
KB
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.
b36fc510
KB
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.
c0bc4ef7
DF
19 */
20
21#ifndef lint
95891db1 22static char sccsid[] = "@(#)io.c 5.9 (Berkeley) %G%";
b0627149 23#endif /* not lint */
87ba9e01 24
95891db1 25#include "indent_globs.h"
30f48914 26#include <ctype.h>
87ba9e01
KM
27
28
1009bf5e
KM
29int comment_open;
30static paren_target;
87ba9e01 31
1009bf5e
KM
32dump_line()
33{ /* dump_line is the routine that actually
30f48914
KB
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 */
1009bf5e
KM
38 register int cur_col,
39 temp_col,
40 target_col;
30f48914 41 static not_first_line;
87ba9e01 42
1009bf5e 43 if (ps.procname[0]) {
30f48914
KB
44 if (troff) {
45 if (comment_open) {
46 comment_open = 0;
47 fprintf(output, ".*/\n");
48 }
1009bf5e 49 fprintf(output, ".Pr \"%s\"\n", ps.procname);
30f48914 50 }
1009bf5e
KM
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) {
30f48914
KB
55 if (suppress_blanklines > 0)
56 suppress_blanklines--;
87ba9e01 57 else {
30f48914
KB
58 ps.bl_line = true;
59 n_real_blanklines++;
87ba9e01 60 }
87ba9e01 61 }
1009bf5e
KM
62 else if (!inhibit_formatting) {
63 suppress_blanklines = 0;
64 ps.bl_line = false;
30f48914 65 if (prefix_blankline_requested && not_first_line)
1009bf5e
KM
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
30f48914
KB
79 * additional statement indentation if we are
80 * at bracket level 0 */
1009bf5e
KM
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);
87ba9e01 96 }
1009bf5e
KM
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++)
30f48914 119 if (*p == (char) 0200)
1009bf5e
KM
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) {
30f48914 127 int all_here = 0;
1009bf5e
KM
128 register char *p;
129
130 if (e_com[-1] == '/' && e_com[-2] == '*')
30f48914 131 e_com -= 2, all_here++;
1009bf5e
KM
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] == '*')
30f48914 139 p += 2, all_here++;
1009bf5e
KM
140 else if (p[0] == '*')
141 p += p[1] == '/' ? 2 : 1;
142 while (*p == ' ')
143 p++;
144 if (*p == 0)
145 goto inhibit_newline;
30f48914
KB
146 if (comment_open < 2 && ps.box_com) {
147 comment_open = 0;
148 fprintf(output, ".*/\n");
149 }
150 if (comment_open == 0) {
1009bf5e
KM
151 if ('a' <= *p && *p <= 'z')
152 *p = *p + 'A' - 'a';
30f48914
KB
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);
1009bf5e 170 }
30f48914
KB
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);
1009bf5e 175 }
30f48914 176 comment_open = 1 + ps.box_com;
1009bf5e
KM
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;
30f48914
KB
188 while (*com_st == '\t')
189 com_st++, target += 8; /* ? */
1009bf5e 190 while (target <= 0)
30f48914
KB
191 if (*com_st == ' ')
192 target++, com_st++;
193 else if (*com_st == '\t')
194 target = ((target - 1) & ~7) + 9, com_st++;
1009bf5e
KM
195 else
196 target = 1;
30f48914
KB
197 if (cur_col > target) { /* if comment cant fit on this line,
198 * put it on next line */
1009bf5e
KM
199 putc('\n', output);
200 cur_col = 1;
201 ++ps.out_lines;
202 }
30f48914
KB
203 while (e_com > com_st && isspace(e_com[-1]))
204 e_com--;
1009bf5e
KM
205 cur_col = pad_output(cur_col, target);
206 if (!ps.box_com) {
30f48914
KB
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)
1009bf5e
KM
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);
222inhibit_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;
87ba9e01 231 }
1009bf5e 232 ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
30f48914
KB
233 * declaration, remember that fact for
234 * proper comment indentation */
1009bf5e
KM
235 ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
236 * indented if we have not
30f48914
KB
237 * completed this stmt and if
238 * we are not in the middle of
239 * a declaration */
1009bf5e
KM
240 ps.use_ff = false;
241 ps.dumped_decl_indent = 0;
242 *(e_lab = s_lab) = '\0'; /* reset buffers */
87ba9e01
KM
243 *(e_code = s_code) = '\0';
244 *(e_com = s_com) = '\0';
1009bf5e
KM
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];
30f48914 248 not_first_line = 1;
87ba9e01
KM
249 return;
250};
87ba9e01 251
30f48914
KB
252compute_code_target()
253{
1009bf5e 254 register target_col = ps.ind_size * ps.ind_level + 1;
87ba9e01 255
1009bf5e
KM
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
30f48914 264 && count_spaces(target_col, s_code) <= max_col) {
1009bf5e
KM
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}
87ba9e01 276
1009bf5e
KM
277compute_label_target()
278{
279 return
30f48914 280 ps.pcase ? (int) (case_ind * ps.ind_size) + 1
1009bf5e 281 : *s_lab == '#' ? 1
30f48914 282 : ps.ind_size * (ps.ind_level - label_offset) + 1;
1009bf5e 283}
87ba9e01 284
87ba9e01 285
1009bf5e 286/*
30f48914
KB
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 *
1009bf5e
KM
300 */\f
301int
302fill_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
30f48914 309 if (bp_save != 0) { /* there is a partly filled input buffer left */
1009bf5e 310 buf_ptr = bp_save; /* dont read anything, just switch buffers */
87ba9e01
KM
311 buf_end = be_save;
312 bp_save = be_save = 0;
313 if (buf_ptr < buf_end)
30f48914
KB
314 return; /* only return if there is really something in
315 * this buffer */
87ba9e01 316 }
1009bf5e
KM
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';
87ba9e01 323 had_eof = true;
87ba9e01 324 }
1009bf5e
KM
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'
30f48914 340 && p[4] == 'N' && p[5] == 'T') {
1009bf5e
KM
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 }
87ba9e01
KM
373 return;
374};
87ba9e01 375
1009bf5e 376/*
30f48914
KB
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 *
1009bf5e 389 * PARAMETERS: current integer The current column target
30f48914
KB
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 *
1009bf5e 403 */\f
30f48914
KB
404pad_output(current, target) /* writes tabs and blanks (if necessary) to
405 * get the current output position up to the
406 * target column */
1009bf5e
KM
407 int current; /* the current column value */
408 int target; /* position we want it at */
87ba9e01 409{
1009bf5e 410 register int curr; /* internal column pointer */
87ba9e01
KM
411 register int tcur;
412
1009bf5e
KM
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);
87ba9e01
KM
421 curr = tcur;
422 }
1009bf5e
KM
423 while (curr++ < target)
424 putc(' ', output); /* pad with final blanks */
87ba9e01 425 }
87ba9e01
KM
426 return (target);
427};
87ba9e01 428
1009bf5e 429/*
30f48914
KB
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 *
1009bf5e 437 * FUNCTION: Find out where printing of a given string will leave the current
30f48914
KB
438 * character position on output.
439 *
1009bf5e 440 * ALGORITHM: Run thru input string and add appropriate values to current
30f48914
KB
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 *
1009bf5e
KM
448 */\f
449int
450count_spaces(current, buffer)
1009bf5e
KM
451/*
452 * this routine figures out where the character position will be after
30f48914 453 * printing the text in buffer starting at column "current"
1009bf5e
KM
454 */
455 int current;
456 char *buffer;
87ba9e01 457{
1009bf5e
KM
458 register char *buf; /* used to look thru buffer */
459 register int cur; /* current character counter */
87ba9e01
KM
460
461 cur = current;
462
463 for (buf = buffer; *buf != '\0'; ++buf) {
464 switch (*buf) {
465
30f48914
KB
466 case '\n':
467 case 014: /* form feed */
468 cur = 1;
469 break;
87ba9e01 470
30f48914
KB
471 case '\t':
472 cur = ((cur - 1) & tabmask) + tabsize + 1;
473 break;
87ba9e01 474
30f48914
KB
475 case '\b': /* this is a backspace */
476 --cur;
477 break;
87ba9e01 478
30f48914
KB
479 default:
480 ++cur;
481 break;
1009bf5e
KM
482 } /* end of switch */
483 } /* end of for loop */
87ba9e01
KM
484 return (cur);
485};
87ba9e01 486
95891db1 487int found_err;
1009bf5e 488diag(level, msg, a, b)
87ba9e01 489{
95891db1
KB
490 if (level)
491 found_err = 1;
1009bf5e
KM
492 if (output == stdout) {
493 fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
494 fprintf(stdout, msg, a, b);
495 fprintf(stdout, " */\n");
87ba9e01 496 }
1009bf5e
KM
497 else {
498 fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
499 fprintf(stderr, msg, a, b);
500 fprintf(stderr, "\n");
87ba9e01 501 }
87ba9e01 502}
30f48914
KB
503
504writefdef(f, nm)
505 register struct fstate *f;
506{
507 fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
508 nm, f->font, nm, f->size);
509}
510
511char *
512chfont(of, nf, s)
513 register struct fstate *of,
514 *nf;
515 char *s;
516{
517 if (of->font[0] != nf->font[0]
518 || of->font[1] != nf->font[1]) {
519 *s++ = '\\';
520 *s++ = 'f';
521 if (nf->font[1]) {
522 *s++ = '(';
523 *s++ = nf->font[0];
524 *s++ = nf->font[1];
525 }
526 else
527 *s++ = nf->font[0];
528 }
529 if (nf->size != of->size) {
530 *s++ = '\\';
531 *s++ = 's';
532 if (nf->size < of->size) {
533 *s++ = '-';
534 *s++ = '0' + of->size - nf->size;
535 }
536 else {
537 *s++ = '+';
538 *s++ = '0' + nf->size - of->size;
539 }
540 }
541 return s;
542}
543
544
545setfont(f)
546 register struct fstate *f;
547{
548 if (f->font[0] != ps.cfont.font[0]
549 || f->font[1] != ps.cfont.font[1])
550 fprintf(output, f->font[1] ? "\\f(%s" : "\\f%s", f->font);
551 if (f->size != ps.cfont.size)
552 fprintf(output, "\\s%d", f->size);
553 ps.cfont = *f;
554}
555
556parsefont(f, s0)
557 register struct fstate *f;
558 char *s0;
559{
560 register char *s = s0;
561 int sizedelta = 0;
562 bzero(f, sizeof *f);
563 while (*s) {
564 if (isdigit(*s))
565 f->size = f->size * 10 + *s - '0';
566 else if (isupper(*s))
567 if (f->font[0])
568 f->font[1] = *s;
569 else
570 f->font[0] = *s;
571 else if (*s == 'c')
572 f->allcaps = 1;
573 else if (*s == '+')
574 sizedelta++;
575 else if (*s == '-')
576 sizedelta--;
577 else {
578 fprintf(stderr, "indent: bad font specification: %s\n", s0);
579 exit(1);
580 }
581 s++;
582 }
583 if (f->font[0] == 0)
584 f->font[0] = 'R';
585 if (bodyf.size == 0)
586 bodyf.size = 11;
587 if (f->size == 0)
588 f->size = bodyf.size + sizedelta;
589 else if (sizedelta > 0)
590 f->size += bodyf.size;
591 else
592 f->size = bodyf.size - f->size;
593}