ANSI
[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 *
6ecf3d85 7 * %sccs.include.redist.c%
c0bc4ef7
DF
8 */
9
10#ifndef lint
2b8540ff 11static char sccsid[] = "@(#)io.c 5.15 (Berkeley) %G%";
b0627149 12#endif /* not lint */
87ba9e01 13
2b8540ff 14#include <stdio.h>
30f48914 15#include <ctype.h>
2b8540ff
KB
16#include <stdlib.h>
17#include <string.h>
18#include "indent_globs.h"
87ba9e01
KM
19
20
1009bf5e
KM
21int comment_open;
22static paren_target;
87ba9e01 23
1009bf5e
KM
24dump_line()
25{ /* dump_line is the routine that actually
30f48914
KB
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 */
1009bf5e 30 register int cur_col,
1009bf5e 31 target_col;
30f48914 32 static not_first_line;
87ba9e01 33
1009bf5e 34 if (ps.procname[0]) {
30f48914
KB
35 if (troff) {
36 if (comment_open) {
37 comment_open = 0;
38 fprintf(output, ".*/\n");
39 }
1009bf5e 40 fprintf(output, ".Pr \"%s\"\n", ps.procname);
30f48914 41 }
1009bf5e
KM
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) {
30f48914
KB
46 if (suppress_blanklines > 0)
47 suppress_blanklines--;
87ba9e01 48 else {
30f48914
KB
49 ps.bl_line = true;
50 n_real_blanklines++;
87ba9e01 51 }
87ba9e01 52 }
1009bf5e
KM
53 else if (!inhibit_formatting) {
54 suppress_blanklines = 0;
55 ps.bl_line = false;
30f48914 56 if (prefix_blankline_requested && not_first_line)
1009bf5e
KM
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
30f48914
KB
70 * additional statement indentation if we are
71 * at bracket level 0 */
1009bf5e
KM
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());
0c8ee79d
KB
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);
1009bf5e 98 cur_col = count_spaces(cur_col, s_lab);
87ba9e01 99 }
1009bf5e
KM
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++)
30f48914 122 if (*p == (char) 0200)
1009bf5e
KM
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) {
30f48914 130 int all_here = 0;
1009bf5e
KM
131 register char *p;
132
133 if (e_com[-1] == '/' && e_com[-2] == '*')
30f48914 134 e_com -= 2, all_here++;
1009bf5e
KM
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] == '*')
30f48914 142 p += 2, all_here++;
1009bf5e
KM
143 else if (p[0] == '*')
144 p += p[1] == '/' ? 2 : 1;
145 while (*p == ' ')
146 p++;
147 if (*p == 0)
148 goto inhibit_newline;
30f48914
KB
149 if (comment_open < 2 && ps.box_com) {
150 comment_open = 0;
151 fprintf(output, ".*/\n");
152 }
153 if (comment_open == 0) {
1009bf5e
KM
154 if ('a' <= *p && *p <= 'z')
155 *p = *p + 'A' - 'a';
30f48914
KB
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);
1009bf5e 173 }
30f48914
KB
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);
1009bf5e 178 }
30f48914 179 comment_open = 1 + ps.box_com;
1009bf5e
KM
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;
30f48914
KB
191 while (*com_st == '\t')
192 com_st++, target += 8; /* ? */
1009bf5e 193 while (target <= 0)
30f48914
KB
194 if (*com_st == ' ')
195 target++, com_st++;
196 else if (*com_st == '\t')
197 target = ((target - 1) & ~7) + 9, com_st++;
1009bf5e
KM
198 else
199 target = 1;
30f48914
KB
200 if (cur_col > target) { /* if comment cant fit on this line,
201 * put it on next line */
1009bf5e
KM
202 putc('\n', output);
203 cur_col = 1;
204 ++ps.out_lines;
205 }
30f48914
KB
206 while (e_com > com_st && isspace(e_com[-1]))
207 e_com--;
1009bf5e
KM
208 cur_col = pad_output(cur_col, target);
209 if (!ps.box_com) {
30f48914
KB
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)
1009bf5e
KM
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);
225inhibit_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;
87ba9e01 234 }
1009bf5e 235 ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
30f48914
KB
236 * declaration, remember that fact for
237 * proper comment indentation */
1009bf5e
KM
238 ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
239 * indented if we have not
30f48914
KB
240 * completed this stmt and if
241 * we are not in the middle of
242 * a declaration */
1009bf5e
KM
243 ps.use_ff = false;
244 ps.dumped_decl_indent = 0;
245 *(e_lab = s_lab) = '\0'; /* reset buffers */
87ba9e01
KM
246 *(e_code = s_code) = '\0';
247 *(e_com = s_com) = '\0';
1009bf5e
KM
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];
30f48914 251 not_first_line = 1;
87ba9e01 252 return;
1a43f1ba 253}
87ba9e01 254
30f48914
KB
255compute_code_target()
256{
1009bf5e 257 register target_col = ps.ind_size * ps.ind_level + 1;
87ba9e01 258
1009bf5e
KM
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
30f48914 267 && count_spaces(target_col, s_code) <= max_col) {
1009bf5e
KM
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}
87ba9e01 279
1009bf5e
KM
280compute_label_target()
281{
282 return
30f48914 283 ps.pcase ? (int) (case_ind * ps.ind_size) + 1
1009bf5e 284 : *s_lab == '#' ? 1
30f48914 285 : ps.ind_size * (ps.ind_level - label_offset) + 1;
1009bf5e 286}
87ba9e01 287
87ba9e01 288
1009bf5e 289/*
30f48914
KB
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 *
0c8ee79d 303 */
1009bf5e
KM
304int
305fill_buffer()
306{ /* this routine reads stuff from the input */
1009bf5e
KM
307 register char *p;
308 register int i;
309 register FILE *f = input;
310
30f48914 311 if (bp_save != 0) { /* there is a partly filled input buffer left */
1009bf5e 312 buf_ptr = bp_save; /* dont read anything, just switch buffers */
87ba9e01
KM
313 buf_end = be_save;
314 bp_save = be_save = 0;
315 if (buf_ptr < buf_end)
30f48914
KB
316 return; /* only return if there is really something in
317 * this buffer */
87ba9e01 318 }
0c8ee79d
KB
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 }
86591296
KB
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;
87ba9e01 338 }
0c8ee79d 339 buf_ptr = in_buffer;
1009bf5e 340 buf_end = p;
1a43f1ba 341 if (p[-2] == '/' && p[-3] == '*') {
1009bf5e
KM
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'
30f48914 355 && p[4] == 'N' && p[5] == 'T') {
1009bf5e
KM
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 }
87ba9e01 388 return;
1a43f1ba 389}
87ba9e01 390
1009bf5e 391/*
30f48914
KB
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 *
1009bf5e 404 * PARAMETERS: current integer The current column target
30f48914
KB
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 *
0c8ee79d 418 */
30f48914
KB
419pad_output(current, target) /* writes tabs and blanks (if necessary) to
420 * get the current output position up to the
421 * target column */
1009bf5e
KM
422 int current; /* the current column value */
423 int target; /* position we want it at */
87ba9e01 424{
1009bf5e 425 register int curr; /* internal column pointer */
87ba9e01
KM
426 register int tcur;
427
1009bf5e
KM
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);
87ba9e01
KM
436 curr = tcur;
437 }
1009bf5e
KM
438 while (curr++ < target)
439 putc(' ', output); /* pad with final blanks */
87ba9e01 440 }
87ba9e01 441 return (target);
1a43f1ba 442}
87ba9e01 443
1009bf5e 444/*
30f48914
KB
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 *
1009bf5e 452 * FUNCTION: Find out where printing of a given string will leave the current
30f48914
KB
453 * character position on output.
454 *
1009bf5e 455 * ALGORITHM: Run thru input string and add appropriate values to current
30f48914
KB
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 *
0c8ee79d 463 */
1009bf5e
KM
464int
465count_spaces(current, buffer)
1009bf5e
KM
466/*
467 * this routine figures out where the character position will be after
30f48914 468 * printing the text in buffer starting at column "current"
1009bf5e
KM
469 */
470 int current;
471 char *buffer;
87ba9e01 472{
1009bf5e
KM
473 register char *buf; /* used to look thru buffer */
474 register int cur; /* current character counter */
87ba9e01
KM
475
476 cur = current;
477
478 for (buf = buffer; *buf != '\0'; ++buf) {
479 switch (*buf) {
480
30f48914
KB
481 case '\n':
482 case 014: /* form feed */
483 cur = 1;
484 break;
87ba9e01 485
30f48914
KB
486 case '\t':
487 cur = ((cur - 1) & tabmask) + tabsize + 1;
488 break;
87ba9e01 489
1a43f1ba 490 case 010: /* backspace */
30f48914
KB
491 --cur;
492 break;
87ba9e01 493
30f48914
KB
494 default:
495 ++cur;
496 break;
1009bf5e
KM
497 } /* end of switch */
498 } /* end of for loop */
87ba9e01 499 return (cur);
1a43f1ba 500}
87ba9e01 501
95891db1 502int found_err;
1a43f1ba 503/* VARARGS2 */
1009bf5e 504diag(level, msg, a, b)
2b8540ff 505 char *msg;
87ba9e01 506{
95891db1
KB
507 if (level)
508 found_err = 1;
1009bf5e
KM
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");
87ba9e01 513 }
1009bf5e
KM
514 else {
515 fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
516 fprintf(stderr, msg, a, b);
517 fprintf(stderr, "\n");
87ba9e01 518 }
87ba9e01 519}
30f48914
KB
520
521writefdef(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
528char *
529chfont(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
30f48914
KB
562parsefont(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}