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