io.c revision 1.5 1 1.5 lukem /* $NetBSD: io.c,v 1.5 1997/10/19 03:17:23 lukem 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.5 lukem __RCSID("$NetBSD: io.c,v 1.5 1997/10/19 03:17:23 lukem 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.1 cgd #include <stdio.h>
51 1.1 cgd #include <stdlib.h>
52 1.1 cgd #include <string.h>
53 1.1 cgd #include "indent_globs.h"
54 1.1 cgd
55 1.1 cgd
56 1.5 lukem int comment_open;
57 1.5 lukem static paren_target;
58 1.1 cgd
59 1.5 lukem void
60 1.1 cgd dump_line()
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.5 lukem 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.5 lukem while (--n_real_blanklines >= 0)
101 1.5 lukem putc('\n', output);
102 1.5 lukem n_real_blanklines = 0;
103 1.5 lukem if (ps.ind_level == 0)
104 1.5 lukem ps.ind_stmt = 0; /* this is a class A
105 1.5 lukem * kludge. dont do
106 1.5 lukem * additional statement
107 1.5 lukem * indentation if we are
108 1.5 lukem * at bracket level 0 */
109 1.5 lukem
110 1.5 lukem if (e_lab != s_lab || e_code != s_code)
111 1.5 lukem ++code_lines; /* keep count of lines with
112 1.5 lukem * code */
113 1.5 lukem
114 1.5 lukem
115 1.5 lukem if (e_lab != s_lab) { /* print lab, if any */
116 1.5 lukem if (comment_open) {
117 1.5 lukem comment_open = 0;
118 1.5 lukem fprintf(output, ".*/\n");
119 1.5 lukem }
120 1.5 lukem while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
121 1.5 lukem e_lab--;
122 1.5 lukem cur_col = pad_output(1, compute_label_target());
123 1.5 lukem if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
124 1.5 lukem || strncmp(s_lab, "#endif", 6) == 0)) {
125 1.5 lukem char *s = s_lab;
126 1.5 lukem if (e_lab[-1] == '\n')
127 1.5 lukem e_lab--;
128 1.5 lukem do
129 1.5 lukem putc(*s++, output);
130 1.5 lukem while (s < e_lab && 'a' <= *s && *s <= 'z');
131 1.5 lukem while ((*s == ' ' || *s == '\t') && s < e_lab)
132 1.5 lukem s++;
133 1.5 lukem if (s < e_lab)
134 1.5 lukem fprintf(output, s[0] == '/' && s[1] == '*' ? "\t%.*s" : "\t/* %.*s */",
135 1.5 lukem e_lab - s, s);
136 1.5 lukem } else
137 1.5 lukem fprintf(output, "%.*s", e_lab - s_lab, s_lab);
138 1.5 lukem cur_col = count_spaces(cur_col, s_lab);
139 1.5 lukem } else
140 1.5 lukem cur_col = 1; /* there is no label section */
141 1.5 lukem
142 1.5 lukem ps.pcase = false;
143 1.5 lukem
144 1.5 lukem if (s_code != e_code) { /* print code section, if any */
145 1.5 lukem char *p;
146 1.5 lukem
147 1.5 lukem if (comment_open) {
148 1.5 lukem comment_open = 0;
149 1.5 lukem fprintf(output, ".*/\n");
150 1.5 lukem }
151 1.5 lukem target_col = compute_code_target();
152 1.5 lukem {
153 1.5 lukem int i;
154 1.5 lukem
155 1.5 lukem for (i = 0; i < ps.p_l_follow; i++)
156 1.5 lukem if (ps.paren_indents[i] >= 0)
157 1.5 lukem ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
158 1.5 lukem }
159 1.5 lukem cur_col = pad_output(cur_col, target_col);
160 1.5 lukem for (p = s_code; p < e_code; p++)
161 1.5 lukem if (*p == (char) 0200)
162 1.5 lukem fprintf(output, "%d", target_col * 7);
163 1.5 lukem else
164 1.5 lukem putc(*p, output);
165 1.5 lukem cur_col = count_spaces(cur_col, s_code);
166 1.1 cgd }
167 1.5 lukem if (s_com != e_com)
168 1.5 lukem if (troff) {
169 1.5 lukem int all_here = 0;
170 1.5 lukem char *p;
171 1.5 lukem
172 1.5 lukem if (e_com[-1] == '/' && e_com[-2] == '*')
173 1.5 lukem e_com -= 2, all_here++;
174 1.5 lukem while (e_com > s_com && e_com[-1] == ' ')
175 1.5 lukem e_com--;
176 1.5 lukem *e_com = 0;
177 1.5 lukem p = s_com;
178 1.5 lukem while (*p == ' ')
179 1.5 lukem p++;
180 1.5 lukem if (p[0] == '/' && p[1] == '*')
181 1.5 lukem p += 2, all_here++;
182 1.5 lukem else
183 1.5 lukem if (p[0] == '*')
184 1.5 lukem p += p[1] == '/' ? 2 : 1;
185 1.5 lukem while (*p == ' ')
186 1.5 lukem p++;
187 1.5 lukem if (*p == 0)
188 1.5 lukem goto inhibit_newline;
189 1.5 lukem if (comment_open < 2 && ps.box_com) {
190 1.5 lukem comment_open = 0;
191 1.5 lukem fprintf(output, ".*/\n");
192 1.5 lukem }
193 1.5 lukem if (comment_open == 0) {
194 1.5 lukem if ('a' <= *p && *p <= 'z')
195 1.5 lukem *p = *p + 'A' - 'a';
196 1.5 lukem if (e_com - p < 50 && all_here == 2) {
197 1.5 lukem char *follow = p;
198 1.5 lukem fprintf(output, "\n.nr C! \\w\1");
199 1.5 lukem while (follow < e_com) {
200 1.5 lukem switch (*follow) {
201 1.5 lukem case '\n':
202 1.5 lukem putc(' ', output);
203 1.5 lukem case 1:
204 1.5 lukem break;
205 1.5 lukem case '\\':
206 1.5 lukem putc('\\', output);
207 1.5 lukem default:
208 1.5 lukem putc(*follow, output);
209 1.5 lukem }
210 1.5 lukem follow++;
211 1.5 lukem }
212 1.5 lukem putc(1, output);
213 1.5 lukem }
214 1.5 lukem fprintf(output, "\n./* %dp %d %dp\n",
215 1.5 lukem ps.com_col * 7,
216 1.5 lukem (s_code != e_code || s_lab != e_lab) - ps.box_com,
217 1.5 lukem target_col * 7);
218 1.5 lukem }
219 1.5 lukem comment_open = 1 + ps.box_com;
220 1.5 lukem while (*p) {
221 1.5 lukem if (*p == BACKSLASH)
222 1.5 lukem putc(BACKSLASH, output);
223 1.5 lukem putc(*p++, output);
224 1.5 lukem }
225 1.5 lukem } else { /* print comment, if any */
226 1.5 lukem int target = ps.com_col;
227 1.5 lukem char *com_st = s_com;
228 1.5 lukem
229 1.5 lukem target += ps.comment_delta;
230 1.5 lukem while (*com_st == '\t')
231 1.5 lukem com_st++, target += 8; /* ? */
232 1.5 lukem while (target <= 0)
233 1.5 lukem if (*com_st == ' ')
234 1.5 lukem target++, com_st++;
235 1.5 lukem else
236 1.5 lukem if (*com_st == '\t')
237 1.5 lukem target = ((target - 1) & ~7) + 9, com_st++;
238 1.5 lukem else
239 1.5 lukem target = 1;
240 1.5 lukem if (cur_col > target) { /* if comment cant fit
241 1.5 lukem * on this line, put it
242 1.5 lukem * on next line */
243 1.5 lukem putc('\n', output);
244 1.5 lukem cur_col = 1;
245 1.5 lukem ++ps.out_lines;
246 1.5 lukem }
247 1.5 lukem while (e_com > com_st && isspace(e_com[-1]))
248 1.5 lukem e_com--;
249 1.5 lukem cur_col = pad_output(cur_col, target);
250 1.5 lukem if (!ps.box_com) {
251 1.5 lukem if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1))
252 1.5 lukem if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
253 1.5 lukem com_st[1] = '*';
254 1.5 lukem else
255 1.5 lukem fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
256 1.5 lukem }
257 1.5 lukem fwrite(com_st, e_com - com_st, 1, output);
258 1.5 lukem ps.comment_delta = ps.n_comment_delta;
259 1.5 lukem cur_col = count_spaces(cur_col, com_st);
260 1.5 lukem ++ps.com_lines; /* count lines with
261 1.5 lukem * comments */
262 1.5 lukem }
263 1.5 lukem if (ps.use_ff)
264 1.5 lukem putc('\014', output);
265 1.1 cgd else
266 1.5 lukem putc('\n', output);
267 1.5 lukem inhibit_newline:
268 1.5 lukem ++ps.out_lines;
269 1.5 lukem if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
270 1.5 lukem prefix_blankline_requested = 1;
271 1.5 lukem ps.just_saw_decl = 0;
272 1.5 lukem } else
273 1.5 lukem prefix_blankline_requested = postfix_blankline_requested;
274 1.5 lukem postfix_blankline_requested = 0;
275 1.1 cgd }
276 1.5 lukem ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
277 1.1 cgd * declaration, remember that fact for
278 1.1 cgd * proper comment indentation */
279 1.5 lukem ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
280 1.1 cgd * indented if we have not
281 1.1 cgd * completed this stmt and if
282 1.1 cgd * we are not in the middle of
283 1.1 cgd * a declaration */
284 1.5 lukem ps.use_ff = false;
285 1.5 lukem ps.dumped_decl_indent = 0;
286 1.5 lukem *(e_lab = s_lab) = '\0';/* reset buffers */
287 1.5 lukem *(e_code = s_code) = '\0';
288 1.5 lukem *(e_com = s_com) = '\0';
289 1.5 lukem ps.ind_level = ps.i_l_follow;
290 1.5 lukem ps.paren_level = ps.p_l_follow;
291 1.5 lukem paren_target = -ps.paren_indents[ps.paren_level - 1];
292 1.5 lukem not_first_line = 1;
293 1.1 cgd }
294 1.1 cgd
295 1.5 lukem int
296 1.1 cgd compute_code_target()
297 1.1 cgd {
298 1.5 lukem int target_col = ps.ind_size * ps.ind_level + 1;
299 1.1 cgd
300 1.5 lukem if (ps.paren_level)
301 1.5 lukem if (!lineup_to_parens)
302 1.5 lukem target_col += continuation_indent * ps.paren_level;
303 1.5 lukem else {
304 1.5 lukem int w;
305 1.5 lukem int t = paren_target;
306 1.5 lukem
307 1.5 lukem if ((w = count_spaces(t, s_code) - max_col) > 0
308 1.5 lukem && count_spaces(target_col, s_code) <= max_col) {
309 1.5 lukem t -= w + 1;
310 1.5 lukem if (t > target_col)
311 1.5 lukem target_col = t;
312 1.5 lukem } else
313 1.5 lukem target_col = t;
314 1.5 lukem }
315 1.5 lukem else
316 1.5 lukem if (ps.ind_stmt)
317 1.5 lukem target_col += continuation_indent;
318 1.5 lukem return target_col;
319 1.1 cgd }
320 1.1 cgd
321 1.5 lukem int
322 1.1 cgd compute_label_target()
323 1.1 cgd {
324 1.5 lukem return
325 1.1 cgd ps.pcase ? (int) (case_ind * ps.ind_size) + 1
326 1.1 cgd : *s_lab == '#' ? 1
327 1.1 cgd : ps.ind_size * (ps.ind_level - label_offset) + 1;
328 1.1 cgd }
329 1.1 cgd
330 1.1 cgd
331 1.1 cgd /*
332 1.1 cgd * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
333 1.5 lukem *
334 1.1 cgd * All rights reserved
335 1.5 lukem *
336 1.5 lukem *
337 1.1 cgd * NAME: fill_buffer
338 1.5 lukem *
339 1.1 cgd * FUNCTION: Reads one block of input into input_buffer
340 1.5 lukem *
341 1.1 cgd * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A
342 1.1 cgd * Willcox of CAC Added check for switch back to partly full input
343 1.1 cgd * buffer from temporary buffer
344 1.5 lukem *
345 1.1 cgd */
346 1.5 lukem void
347 1.1 cgd fill_buffer()
348 1.1 cgd { /* this routine reads stuff from the input */
349 1.5 lukem char *p;
350 1.5 lukem int i;
351 1.5 lukem FILE *f = input;
352 1.5 lukem
353 1.5 lukem if (bp_save != 0) { /* there is a partly filled input buffer left */
354 1.5 lukem buf_ptr = bp_save; /* dont read anything, just switch
355 1.5 lukem * buffers */
356 1.5 lukem buf_end = be_save;
357 1.5 lukem bp_save = be_save = 0;
358 1.5 lukem if (buf_ptr < buf_end)
359 1.5 lukem return; /* only return if there is really something in
360 1.1 cgd * this buffer */
361 1.1 cgd }
362 1.5 lukem for (p = in_buffer;;) {
363 1.5 lukem if (p >= in_buffer_limit) {
364 1.5 lukem int size = (in_buffer_limit - in_buffer) * 2 + 10;
365 1.5 lukem int offset = p - in_buffer;
366 1.5 lukem in_buffer = (char *) realloc(in_buffer, size);
367 1.5 lukem if (in_buffer == 0)
368 1.5 lukem errx(1, "input line too long");
369 1.5 lukem p = in_buffer + offset;
370 1.5 lukem in_buffer_limit = in_buffer + size - 2;
371 1.5 lukem }
372 1.5 lukem if ((i = getc(f)) == EOF) {
373 1.5 lukem *p++ = ' ';
374 1.5 lukem *p++ = '\n';
375 1.5 lukem had_eof = true;
376 1.5 lukem break;
377 1.5 lukem }
378 1.5 lukem *p++ = i;
379 1.5 lukem if (i == '\n')
380 1.5 lukem break;
381 1.5 lukem }
382 1.5 lukem buf_ptr = in_buffer;
383 1.5 lukem buf_end = p;
384 1.5 lukem if (p[-2] == '/' && p[-3] == '*') {
385 1.5 lukem if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
386 1.5 lukem fill_buffer(); /* flush indent error message */
387 1.5 lukem else {
388 1.5 lukem int com = 0;
389 1.5 lukem
390 1.5 lukem p = in_buffer;
391 1.5 lukem while (*p == ' ' || *p == '\t')
392 1.5 lukem p++;
393 1.5 lukem if (*p == '/' && p[1] == '*') {
394 1.5 lukem p += 2;
395 1.5 lukem while (*p == ' ' || *p == '\t')
396 1.5 lukem p++;
397 1.5 lukem if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
398 1.5 lukem && p[4] == 'N' && p[5] == 'T') {
399 1.5 lukem p += 6;
400 1.5 lukem while (*p == ' ' || *p == '\t')
401 1.5 lukem p++;
402 1.5 lukem if (*p == '*')
403 1.5 lukem com = 1;
404 1.5 lukem else
405 1.5 lukem if (*p == 'O')
406 1.5 lukem if (*++p == 'N')
407 1.5 lukem p++, com = 1;
408 1.5 lukem else
409 1.5 lukem if (*p == 'F' && *++p == 'F')
410 1.5 lukem p++, com = 2;
411 1.5 lukem while (*p == ' ' || *p == '\t')
412 1.5 lukem p++;
413 1.5 lukem if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
414 1.5 lukem if (s_com != e_com || s_lab != e_lab || s_code != e_code)
415 1.5 lukem dump_line();
416 1.5 lukem if (!(inhibit_formatting = com - 1)) {
417 1.5 lukem n_real_blanklines = 0;
418 1.5 lukem postfix_blankline_requested = 0;
419 1.5 lukem prefix_blankline_requested = 0;
420 1.5 lukem suppress_blanklines = 1;
421 1.5 lukem }
422 1.5 lukem }
423 1.5 lukem }
424 1.1 cgd }
425 1.1 cgd }
426 1.1 cgd }
427 1.5 lukem if (inhibit_formatting) {
428 1.5 lukem p = in_buffer;
429 1.5 lukem do
430 1.5 lukem putc(*p, output);
431 1.5 lukem while (*p++ != '\n');
432 1.5 lukem }
433 1.1 cgd }
434 1.1 cgd /*
435 1.1 cgd * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
436 1.5 lukem *
437 1.1 cgd * All rights reserved
438 1.5 lukem *
439 1.5 lukem *
440 1.1 cgd * NAME: pad_output
441 1.5 lukem *
442 1.1 cgd * FUNCTION: Writes tabs and spaces to move the current column up to the desired
443 1.1 cgd * position.
444 1.5 lukem *
445 1.1 cgd * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
446 1.5 lukem *
447 1.1 cgd * PARAMETERS: current integer The current column target
448 1.1 cgd * nteger The desired column
449 1.5 lukem *
450 1.1 cgd * RETURNS: Integer value of the new column. (If current >= target, no action is
451 1.1 cgd * taken, and current is returned.
452 1.5 lukem *
453 1.1 cgd * GLOBALS: None
454 1.5 lukem *
455 1.1 cgd * CALLS: write (sys)
456 1.5 lukem *
457 1.1 cgd * CALLED BY: dump_line
458 1.5 lukem *
459 1.1 cgd * HISTORY: initial coding November 1976 D A Willcox of CAC
460 1.5 lukem *
461 1.1 cgd */
462 1.5 lukem int
463 1.1 cgd pad_output(current, target) /* writes tabs and blanks (if necessary) to
464 1.1 cgd * get the current output position up to the
465 1.1 cgd * target column */
466 1.5 lukem int current; /* the current column value */
467 1.5 lukem int target; /* position we want it at */
468 1.1 cgd {
469 1.5 lukem int curr; /* internal column pointer */
470 1.5 lukem int tcur;
471 1.1 cgd
472 1.5 lukem if (troff)
473 1.5 lukem fprintf(output, "\\h'|%dp'", (target - 1) * 7);
474 1.5 lukem else {
475 1.5 lukem if (current >= target)
476 1.5 lukem return (current); /* line is already long enough */
477 1.5 lukem curr = current;
478 1.5 lukem while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
479 1.5 lukem putc('\t', output);
480 1.5 lukem curr = tcur;
481 1.5 lukem }
482 1.5 lukem while (curr++ < target)
483 1.5 lukem putc(' ', output); /* pad with final blanks */
484 1.1 cgd }
485 1.5 lukem return (target);
486 1.1 cgd }
487 1.1 cgd /*
488 1.1 cgd * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
489 1.5 lukem *
490 1.1 cgd * All rights reserved
491 1.5 lukem *
492 1.5 lukem *
493 1.1 cgd * NAME: count_spaces
494 1.5 lukem *
495 1.1 cgd * FUNCTION: Find out where printing of a given string will leave the current
496 1.1 cgd * character position on output.
497 1.5 lukem *
498 1.1 cgd * ALGORITHM: Run thru input string and add appropriate values to current
499 1.1 cgd * position.
500 1.5 lukem *
501 1.1 cgd * RETURNS: Integer value of position after printing "buffer" starting in column
502 1.1 cgd * "current".
503 1.5 lukem *
504 1.1 cgd * HISTORY: initial coding November 1976 D A Willcox of CAC
505 1.5 lukem *
506 1.1 cgd */
507 1.1 cgd int
508 1.1 cgd count_spaces(current, buffer)
509 1.1 cgd /*
510 1.1 cgd * this routine figures out where the character position will be after
511 1.1 cgd * printing the text in buffer starting at column "current"
512 1.1 cgd */
513 1.5 lukem int current;
514 1.5 lukem char *buffer;
515 1.1 cgd {
516 1.5 lukem char *buf; /* used to look thru buffer */
517 1.5 lukem int cur; /* current character counter */
518 1.1 cgd
519 1.5 lukem cur = current;
520 1.1 cgd
521 1.5 lukem for (buf = buffer; *buf != '\0'; ++buf) {
522 1.5 lukem switch (*buf) {
523 1.1 cgd
524 1.5 lukem case '\n':
525 1.5 lukem case 014: /* form feed */
526 1.5 lukem cur = 1;
527 1.5 lukem break;
528 1.5 lukem
529 1.5 lukem case '\t':
530 1.5 lukem cur = ((cur - 1) & tabmask) + tabsize + 1;
531 1.5 lukem break;
532 1.5 lukem
533 1.5 lukem case 010: /* backspace */
534 1.5 lukem --cur;
535 1.5 lukem break;
536 1.5 lukem
537 1.5 lukem default:
538 1.5 lukem ++cur;
539 1.5 lukem break;
540 1.5 lukem } /* end of switch */
541 1.5 lukem } /* end of for loop */
542 1.5 lukem return (cur);
543 1.1 cgd }
544 1.1 cgd
545 1.5 lukem
546 1.5 lukem #if __STDC__
547 1.5 lukem #include <stdarg.h>
548 1.5 lukem #else
549 1.5 lukem #include <varargs.h>
550 1.5 lukem #endif
551 1.5 lukem
552 1.5 lukem int found_err;
553 1.5 lukem
554 1.5 lukem void
555 1.5 lukem #if __STDC__
556 1.5 lukem diag(int level, char *msg,...)
557 1.5 lukem #else
558 1.5 lukem diag(level, msg, va_alist)
559 1.5 lukem int level
560 1.5 lukem char *msg;
561 1.5 lukem va_dcl
562 1.5 lukem #endif
563 1.1 cgd {
564 1.5 lukem va_list ap;
565 1.5 lukem #if __STDC__
566 1.5 lukem va_start(ap, msg);
567 1.5 lukem #else
568 1.5 lukem va_start(ap);
569 1.5 lukem #endif
570 1.5 lukem
571 1.5 lukem if (level)
572 1.5 lukem found_err = 1;
573 1.5 lukem if (output == stdout) {
574 1.5 lukem fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
575 1.5 lukem vfprintf(stdout, msg, ap);
576 1.5 lukem fprintf(stdout, " */\n");
577 1.5 lukem } else {
578 1.5 lukem fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
579 1.5 lukem vfprintf(stdout, msg, ap);
580 1.5 lukem fprintf(stderr, "\n");
581 1.5 lukem }
582 1.5 lukem va_end(ap);
583 1.1 cgd }
584 1.1 cgd
585 1.5 lukem void
586 1.1 cgd writefdef(f, nm)
587 1.5 lukem struct fstate *f;
588 1.5 lukem int nm;
589 1.1 cgd {
590 1.5 lukem fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
591 1.1 cgd nm, f->font, nm, f->size);
592 1.1 cgd }
593 1.1 cgd
594 1.5 lukem char *
595 1.1 cgd chfont(of, nf, s)
596 1.5 lukem struct fstate *of, *nf;
597 1.5 lukem char *s;
598 1.1 cgd {
599 1.5 lukem if (of->font[0] != nf->font[0]
600 1.1 cgd || of->font[1] != nf->font[1]) {
601 1.5 lukem *s++ = '\\';
602 1.5 lukem *s++ = 'f';
603 1.5 lukem if (nf->font[1]) {
604 1.5 lukem *s++ = '(';
605 1.5 lukem *s++ = nf->font[0];
606 1.5 lukem *s++ = nf->font[1];
607 1.5 lukem } else
608 1.5 lukem *s++ = nf->font[0];
609 1.5 lukem }
610 1.5 lukem if (nf->size != of->size) {
611 1.5 lukem *s++ = '\\';
612 1.5 lukem *s++ = 's';
613 1.5 lukem if (nf->size < of->size) {
614 1.5 lukem *s++ = '-';
615 1.5 lukem *s++ = '0' + of->size - nf->size;
616 1.5 lukem } else {
617 1.5 lukem *s++ = '+';
618 1.5 lukem *s++ = '0' + nf->size - of->size;
619 1.5 lukem }
620 1.1 cgd }
621 1.5 lukem return s;
622 1.1 cgd }
623 1.1 cgd
624 1.1 cgd
625 1.5 lukem void
626 1.1 cgd parsefont(f, s0)
627 1.5 lukem struct fstate *f;
628 1.5 lukem char *s0;
629 1.1 cgd {
630 1.5 lukem char *s = s0;
631 1.5 lukem int sizedelta = 0;
632 1.5 lukem memset(f, 0, sizeof *f);
633 1.5 lukem while (*s) {
634 1.5 lukem if (isdigit(*s))
635 1.5 lukem f->size = f->size * 10 + *s - '0';
636 1.5 lukem else
637 1.5 lukem if (isupper(*s))
638 1.5 lukem if (f->font[0])
639 1.5 lukem f->font[1] = *s;
640 1.5 lukem else
641 1.5 lukem f->font[0] = *s;
642 1.5 lukem else
643 1.5 lukem if (*s == 'c')
644 1.5 lukem f->allcaps = 1;
645 1.5 lukem else
646 1.5 lukem if (*s == '+')
647 1.5 lukem sizedelta++;
648 1.5 lukem else
649 1.5 lukem if (*s == '-')
650 1.5 lukem sizedelta--;
651 1.5 lukem else {
652 1.5 lukem fprintf(stderr, "indent: bad font specification: %s\n", s0);
653 1.5 lukem exit(1);
654 1.5 lukem }
655 1.5 lukem s++;
656 1.5 lukem }
657 1.5 lukem if (f->font[0] == 0)
658 1.5 lukem f->font[0] = 'R';
659 1.5 lukem if (bodyf.size == 0)
660 1.5 lukem bodyf.size = 11;
661 1.5 lukem if (f->size == 0)
662 1.5 lukem f->size = bodyf.size + sizedelta;
663 1.5 lukem else
664 1.5 lukem if (sizedelta > 0)
665 1.5 lukem f->size += bodyf.size;
666 1.5 lukem else
667 1.5 lukem f->size = bodyf.size - f->size;
668 1.1 cgd }
669