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