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