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