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