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