xref: /illumos-gate/usr/src/cmd/deroff/deroff.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*b5514887Smuffin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*b5514887Smuffin /*	  All Rights Reserved  	*/
29*b5514887Smuffin 
307c478bd9Sstevel@tonic-gate #include <assert.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <locale.h>
367c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * Deroff command -- strip troff, eqn, and Tbl sequences from a file.
407c478bd9Sstevel@tonic-gate  * Has three flags argument, -w, to cause output one word per line
417c478bd9Sstevel@tonic-gate  * rather than in the original format.
427c478bd9Sstevel@tonic-gate  * -mm (or -ms) causes the corresponding macro's to be interpreted
437c478bd9Sstevel@tonic-gate  * so that just sentences are output
447c478bd9Sstevel@tonic-gate  * -ml  also gets rid of lists.
457c478bd9Sstevel@tonic-gate  * -i causes deroff to ignore .so and .nx commands.
467c478bd9Sstevel@tonic-gate  * Deroff follows .so and .nx commands, removes contents of macro
477c478bd9Sstevel@tonic-gate  * definitions, equations (both .EQ ... .EN and $...$),
487c478bd9Sstevel@tonic-gate  * Tbl command sequences, and Troff backslash constructions.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * All input is through the C macro; the most recently read character
517c478bd9Sstevel@tonic-gate  * is in c.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	C	((c = getc(infile)) == EOF ? eof() : \
557c478bd9Sstevel@tonic-gate 		    ((c == ldelim) && (filesp == files) ? skeqn() : c))
567c478bd9Sstevel@tonic-gate #define	C1	((c = getc(infile)) == EOF ? eof() : c)
577c478bd9Sstevel@tonic-gate #define	SKIP	while (C != '\n')
587c478bd9Sstevel@tonic-gate #define	SKIP_TO_COM	SKIP; SKIP; pc = c; \
597c478bd9Sstevel@tonic-gate 			while ((C != '.') || (pc != '\n') || \
607c478bd9Sstevel@tonic-gate 			    (C > 'Z')) { \
617c478bd9Sstevel@tonic-gate 				pc = c; \
627c478bd9Sstevel@tonic-gate 			}
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	YES 1
657c478bd9Sstevel@tonic-gate #define	NO 0
667c478bd9Sstevel@tonic-gate #define	MS 0
677c478bd9Sstevel@tonic-gate #define	MM 1
687c478bd9Sstevel@tonic-gate #define	ONE 1
697c478bd9Sstevel@tonic-gate #define	TWO 2
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	NOCHAR -2
727c478bd9Sstevel@tonic-gate #define	SPECIAL 0
737c478bd9Sstevel@tonic-gate #define	APOS 1
747c478bd9Sstevel@tonic-gate #define	DIGIT 2
757c478bd9Sstevel@tonic-gate #define	LETTER 3
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #define	MAXLINESZ	512
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static int wordflag = NO;
807c478bd9Sstevel@tonic-gate static int msflag = NO;
817c478bd9Sstevel@tonic-gate static int iflag = NO;
827c478bd9Sstevel@tonic-gate static int mac = MM;
837c478bd9Sstevel@tonic-gate static int disp = 0;
847c478bd9Sstevel@tonic-gate static int inmacro = NO;
857c478bd9Sstevel@tonic-gate static int intable = NO;
867c478bd9Sstevel@tonic-gate static int lindx;
877c478bd9Sstevel@tonic-gate static size_t linesize = MAXLINESZ;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static char chars[128];  /* SPECIAL, APOS, DIGIT, or LETTER */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static char *line = NULL;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static char c;
947c478bd9Sstevel@tonic-gate static int pc;
957c478bd9Sstevel@tonic-gate static int ldelim	= NOCHAR;
967c478bd9Sstevel@tonic-gate static int rdelim	= NOCHAR;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static int argc;
997c478bd9Sstevel@tonic-gate static char **argv;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate extern int optind;
1027c478bd9Sstevel@tonic-gate extern char *optarg;
1037c478bd9Sstevel@tonic-gate static char fname[50];
1047c478bd9Sstevel@tonic-gate static FILE *files[15];
1057c478bd9Sstevel@tonic-gate static FILE **filesp;
1067c478bd9Sstevel@tonic-gate static FILE *infile;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate static void backsl(void);
1097c478bd9Sstevel@tonic-gate static void comline(void);
1107c478bd9Sstevel@tonic-gate static char *copys(char *);
1117c478bd9Sstevel@tonic-gate static int eof(void);
1127c478bd9Sstevel@tonic-gate static void eqn(void);
1137c478bd9Sstevel@tonic-gate static void fatal(const char *, ...);
1147c478bd9Sstevel@tonic-gate static void fatal_msg(char *);
1157c478bd9Sstevel@tonic-gate static void getfname(void);
1167c478bd9Sstevel@tonic-gate static void macro(void);
1177c478bd9Sstevel@tonic-gate static FILE *opn(char *);
1187c478bd9Sstevel@tonic-gate static void putmac(char *, int);
1197c478bd9Sstevel@tonic-gate static void putwords(int);
1207c478bd9Sstevel@tonic-gate static void regline(int, int);
1217c478bd9Sstevel@tonic-gate static void sce(void);
122*b5514887Smuffin static int skeqn(void);
1237c478bd9Sstevel@tonic-gate static void sdis(char, char);
1247c478bd9Sstevel@tonic-gate static void stbl(void);
1257c478bd9Sstevel@tonic-gate static void tbl(void);
1267c478bd9Sstevel@tonic-gate static void usage(void);
127*b5514887Smuffin static void work(void)	__NORETURN;
1287c478bd9Sstevel@tonic-gate 
129*b5514887Smuffin int
main(int ac,char ** av)1307c478bd9Sstevel@tonic-gate main(int ac, char **av)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	int i;
1337c478bd9Sstevel@tonic-gate 	int errflg = 0;
1347c478bd9Sstevel@tonic-gate 	int optchar;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1377c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1387c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1397c478bd9Sstevel@tonic-gate #endif
1407c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1417c478bd9Sstevel@tonic-gate 	argc = ac;
1427c478bd9Sstevel@tonic-gate 	argv = av;
1437c478bd9Sstevel@tonic-gate 	while ((optchar = getopt(argc, argv, "wim:")) != EOF) {
1447c478bd9Sstevel@tonic-gate 		switch (optchar) {
1457c478bd9Sstevel@tonic-gate 		case 'w':
1467c478bd9Sstevel@tonic-gate 			wordflag = YES;
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		case 'm':
1497c478bd9Sstevel@tonic-gate 			msflag = YES;
1507c478bd9Sstevel@tonic-gate 			if (*optarg == 'm')
1517c478bd9Sstevel@tonic-gate 				mac = MM;
1527c478bd9Sstevel@tonic-gate 			else if (*optarg == 's')
1537c478bd9Sstevel@tonic-gate 				mac = MS;
1547c478bd9Sstevel@tonic-gate 			else if (*optarg == 'l')
1557c478bd9Sstevel@tonic-gate 				disp = 1;
1567c478bd9Sstevel@tonic-gate 			else
1577c478bd9Sstevel@tonic-gate 				errflg++;
1587c478bd9Sstevel@tonic-gate 			break;
1597c478bd9Sstevel@tonic-gate 		case 'i':
1607c478bd9Sstevel@tonic-gate 			iflag = YES;
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 		case '?':
1637c478bd9Sstevel@tonic-gate 			errflg++;
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 	}
166*b5514887Smuffin 	if (errflg) {
1677c478bd9Sstevel@tonic-gate 		usage();
168*b5514887Smuffin 		return (1);
169*b5514887Smuffin 	}
1707c478bd9Sstevel@tonic-gate 	if (optind == argc)
1717c478bd9Sstevel@tonic-gate 		infile = stdin;
1727c478bd9Sstevel@tonic-gate 	else
1737c478bd9Sstevel@tonic-gate 		infile = opn(argv[optind++]);
1747c478bd9Sstevel@tonic-gate 	files[0] = infile;
1757c478bd9Sstevel@tonic-gate 	filesp = &files[0];
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	for (i = 'a'; i <= 'z'; ++i)
1787c478bd9Sstevel@tonic-gate 		chars[i] = LETTER;
1797c478bd9Sstevel@tonic-gate 	for (i = 'A'; i <= 'Z'; ++i)
1807c478bd9Sstevel@tonic-gate 		chars[i] = LETTER;
1817c478bd9Sstevel@tonic-gate 	for (i = '0'; i <= '9'; ++i)
1827c478bd9Sstevel@tonic-gate 		chars[i] = DIGIT;
1837c478bd9Sstevel@tonic-gate 	chars['\''] = APOS;
1847c478bd9Sstevel@tonic-gate 	chars['&'] = APOS;
1857c478bd9Sstevel@tonic-gate 	work();
186*b5514887Smuffin 	/* NOTREACHED */
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate static int
skeqn(void)191*b5514887Smuffin skeqn(void)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	while ((c = getc(infile)) != rdelim) {
1947c478bd9Sstevel@tonic-gate 		if (c == EOF) {
1957c478bd9Sstevel@tonic-gate 			c = eof();
1967c478bd9Sstevel@tonic-gate 		} else if (c == '"') {
1977c478bd9Sstevel@tonic-gate 			while ((c = getc(infile)) != '"') {
1987c478bd9Sstevel@tonic-gate 				if (c == EOF) {
1997c478bd9Sstevel@tonic-gate 					c = eof();
2007c478bd9Sstevel@tonic-gate 				} else if (c == '\\') {
2017c478bd9Sstevel@tonic-gate 					if ((c = getc(infile)) == EOF) {
2027c478bd9Sstevel@tonic-gate 						c = eof();
2037c478bd9Sstevel@tonic-gate 					}
2047c478bd9Sstevel@tonic-gate 				}
2057c478bd9Sstevel@tonic-gate 			}
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	if (msflag) {
2097c478bd9Sstevel@tonic-gate 		return (c = 'x');
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	return (c = ' ');
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /* Functions calling opn() should ensure 'p' is non-null */
2167c478bd9Sstevel@tonic-gate static FILE *
opn(char * p)2177c478bd9Sstevel@tonic-gate opn(char *p)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	FILE *fd;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	assert(p != NULL);
2227c478bd9Sstevel@tonic-gate 	if ((fd = fopen(p, "r")) == NULL)
2237c478bd9Sstevel@tonic-gate 		fatal(gettext("Cannot open file %s: %s\n"), p, strerror(errno));
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	return (fd);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static int
eof(void)2317c478bd9Sstevel@tonic-gate eof(void)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	if (infile != stdin)
2347c478bd9Sstevel@tonic-gate 		(void) fclose(infile);
2357c478bd9Sstevel@tonic-gate 	if (filesp > files) {
2367c478bd9Sstevel@tonic-gate 		infile = *--filesp;
2377c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
2387c478bd9Sstevel@tonic-gate 		infile = opn(argv[optind++]);
2397c478bd9Sstevel@tonic-gate 	} else {
2407c478bd9Sstevel@tonic-gate 		exit(0);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	return (C);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate static void
getfname(void)2497c478bd9Sstevel@tonic-gate getfname(void)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	char *p;
2527c478bd9Sstevel@tonic-gate 	struct chain {
2537c478bd9Sstevel@tonic-gate 		struct chain *nextp;
2547c478bd9Sstevel@tonic-gate 		char *datap;
2557c478bd9Sstevel@tonic-gate 	};
2567c478bd9Sstevel@tonic-gate 	struct chain *q;
2577c478bd9Sstevel@tonic-gate 	static struct chain *namechain = NULL;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	while (C == ' ')
2607c478bd9Sstevel@tonic-gate 		;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	for (p = fname; ((*p = c) != '\n') && (c != ' ') && (c != '\t') &&
2637c478bd9Sstevel@tonic-gate 	    (c != '\\'); ++p) {
2647c478bd9Sstevel@tonic-gate 		(void) C;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	*p = '\0';
2677c478bd9Sstevel@tonic-gate 	while (c != '\n') {
2687c478bd9Sstevel@tonic-gate 		(void) C;
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/* see if this name has already been used */
2727c478bd9Sstevel@tonic-gate 	for (q = namechain; q; q = q->nextp)
2737c478bd9Sstevel@tonic-gate 		if (strcmp(fname, q->datap) != 0) {
2747c478bd9Sstevel@tonic-gate 			fname[0] = '\0';
2757c478bd9Sstevel@tonic-gate 			return;
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	q = (struct chain *)calloc(1, sizeof (*namechain));
2797c478bd9Sstevel@tonic-gate 	q->nextp = namechain;
2807c478bd9Sstevel@tonic-gate 	q->datap = copys(fname);
2817c478bd9Sstevel@tonic-gate 	namechain = q;
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * Functions calling fatal() should ensure 'format' and
2877c478bd9Sstevel@tonic-gate  * arguments are non-null.
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate static void
fatal(const char * format,...)2907c478bd9Sstevel@tonic-gate fatal(const char *format, ...)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	va_list	alist;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	assert(format != NULL);
2957c478bd9Sstevel@tonic-gate 	(void) fputs(gettext("deroff: "), stderr);
2967c478bd9Sstevel@tonic-gate 	va_start(alist, format);
2977c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
2987c478bd9Sstevel@tonic-gate 	exit(1);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /* Functions calling fatal_msg() should ensure 's' is non-null */
3027c478bd9Sstevel@tonic-gate static void
fatal_msg(char * s)3037c478bd9Sstevel@tonic-gate fatal_msg(char *s)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate 	assert(s != NULL);
3067c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("deroff: %s\n"), s);
3077c478bd9Sstevel@tonic-gate 	exit(1);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate static void
usage(void)3117c478bd9Sstevel@tonic-gate usage(void)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	(void) fputs(gettext(
3147c478bd9Sstevel@tonic-gate 	    "usage: deroff [ -w ] [ -m (m s l) ] [ -i ] "
3157c478bd9Sstevel@tonic-gate 	    "[ file ] ... \n"), stderr);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static void
work(void)3197c478bd9Sstevel@tonic-gate work(void)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	for (;;) {
3237c478bd9Sstevel@tonic-gate 		if ((C == '.') || (c == '\''))
3247c478bd9Sstevel@tonic-gate 			comline();
3257c478bd9Sstevel@tonic-gate 		else
3267c478bd9Sstevel@tonic-gate 			regline(NO, TWO);
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate static void
regline(int macline,int cnst)3327c478bd9Sstevel@tonic-gate regline(int macline, int cnst)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (line == NULL) {
3367c478bd9Sstevel@tonic-gate 		if ((line = (char *)malloc(linesize * sizeof (char))) == NULL) {
3377c478bd9Sstevel@tonic-gate 			fatal_msg(gettext("Cannot allocate memory"));
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	lindx = 0;
3427c478bd9Sstevel@tonic-gate 	line[lindx] = c;
3437c478bd9Sstevel@tonic-gate 	for (;;) {
3447c478bd9Sstevel@tonic-gate 		if (c == '\\') {
3457c478bd9Sstevel@tonic-gate 			line[lindx] = ' ';
3467c478bd9Sstevel@tonic-gate 			backsl();
3477c478bd9Sstevel@tonic-gate 			if (c == '%') {	/* no blank for hyphenation char */
3487c478bd9Sstevel@tonic-gate 				lindx--;
3497c478bd9Sstevel@tonic-gate 			}
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 		if (c == '\n') {
3527c478bd9Sstevel@tonic-gate 			break;
3537c478bd9Sstevel@tonic-gate 		}
3547c478bd9Sstevel@tonic-gate 		/*
3557c478bd9Sstevel@tonic-gate 		 * We're just about to add another character to the line
3567c478bd9Sstevel@tonic-gate 		 * buffer so ensure we don't overrun it.
3577c478bd9Sstevel@tonic-gate 		 */
3587c478bd9Sstevel@tonic-gate 		if (++lindx >= linesize - 1) {
3597c478bd9Sstevel@tonic-gate 			linesize = linesize * 2;
3607c478bd9Sstevel@tonic-gate 			if ((line = (char *)realloc(line,
3617c478bd9Sstevel@tonic-gate 			    linesize * sizeof (char))) == NULL) {
3627c478bd9Sstevel@tonic-gate 				fatal_msg(gettext("Cannot allocate memory"));
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 		if (intable && (c == 'T')) {
3667c478bd9Sstevel@tonic-gate 			line[lindx] = C;
3677c478bd9Sstevel@tonic-gate 			if ((c == '{') || (c == '}')) {
3687c478bd9Sstevel@tonic-gate 				line[lindx - 1] = ' ';
3697c478bd9Sstevel@tonic-gate 				line[lindx] = C;
3707c478bd9Sstevel@tonic-gate 			}
3717c478bd9Sstevel@tonic-gate 		} else {
3727c478bd9Sstevel@tonic-gate 			line[lindx] = C;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	line[lindx] = '\0';
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	if (line[0] != '\0') {
3797c478bd9Sstevel@tonic-gate 		if (wordflag) {
3807c478bd9Sstevel@tonic-gate 			putwords(macline);
3817c478bd9Sstevel@tonic-gate 		} else if (macline) {
3827c478bd9Sstevel@tonic-gate 			putmac(line, cnst);
3837c478bd9Sstevel@tonic-gate 		} else {
3847c478bd9Sstevel@tonic-gate 			(void) puts(line);
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate static void
putmac(char * s,int cnst)3937c478bd9Sstevel@tonic-gate putmac(char *s, int cnst)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	char *t;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	while (*s) {
3987c478bd9Sstevel@tonic-gate 		while ((*s == ' ') || (*s == '\t')) {
3997c478bd9Sstevel@tonic-gate 			(void) putchar(*s++);
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 		for (t = s; (*t != ' ') && (*t != '\t') && (*t != '\0'); ++t)
4027c478bd9Sstevel@tonic-gate 			;
4037c478bd9Sstevel@tonic-gate 		if (*s == '\"')
4047c478bd9Sstevel@tonic-gate 			s++;
4057c478bd9Sstevel@tonic-gate 		if ((t > s + cnst) && (chars[s[0]] == LETTER) &&
4067c478bd9Sstevel@tonic-gate 		    (chars[s[1]] == LETTER)) {
4077c478bd9Sstevel@tonic-gate 			while (s < t) {
4087c478bd9Sstevel@tonic-gate 				if (*s == '\"')
4097c478bd9Sstevel@tonic-gate 					s++;
4107c478bd9Sstevel@tonic-gate 				else
4117c478bd9Sstevel@tonic-gate 					(void) putchar(*s++);
4127c478bd9Sstevel@tonic-gate 			}
4137c478bd9Sstevel@tonic-gate 		} else {
4147c478bd9Sstevel@tonic-gate 			s = t;
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate static void
putwords(int macline)4237c478bd9Sstevel@tonic-gate putwords(int macline)	/* break into words for -w option */
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	char *p, *p1;
4267c478bd9Sstevel@tonic-gate 	int i, nlet;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	for (p1 = line; ; ) {
4297c478bd9Sstevel@tonic-gate 		/* skip initial specials ampersands and apostrophes */
4307c478bd9Sstevel@tonic-gate 		while (chars[*p1] < DIGIT) {
4317c478bd9Sstevel@tonic-gate 			if (*p1++ == '\0')
4327c478bd9Sstevel@tonic-gate 				return;
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		nlet = 0;
4357c478bd9Sstevel@tonic-gate 		for (p = p1; (i = chars[*p]) != SPECIAL; ++p) {
4367c478bd9Sstevel@tonic-gate 			if (i == LETTER)
4377c478bd9Sstevel@tonic-gate 				++nlet;
4387c478bd9Sstevel@tonic-gate 		}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		if ((!macline && (nlet > 1)) /* MDM definition of word */ ||
4417c478bd9Sstevel@tonic-gate 		    (macline && (nlet > 2) && (chars[p1[0]] == LETTER) &&
4427c478bd9Sstevel@tonic-gate 		    (chars[p1[1]] == LETTER))) {
4437c478bd9Sstevel@tonic-gate 			/* delete trailing ampersands and apostrophes */
4447c478bd9Sstevel@tonic-gate 			while ((p[-1] == '\'') || (p[-1] == '&')) {
4457c478bd9Sstevel@tonic-gate 				--p;
4467c478bd9Sstevel@tonic-gate 			}
4477c478bd9Sstevel@tonic-gate 			while (p1 < p) {
4487c478bd9Sstevel@tonic-gate 				(void) putchar(*p1++);
4497c478bd9Sstevel@tonic-gate 			}
4507c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
4517c478bd9Sstevel@tonic-gate 		} else {
4527c478bd9Sstevel@tonic-gate 			p1 = p;
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate static void
comline(void)4607c478bd9Sstevel@tonic-gate comline(void)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	int c1, c2;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate com:
4657c478bd9Sstevel@tonic-gate 	while ((C == ' ') || (c == '\t'))
4667c478bd9Sstevel@tonic-gate 		;
4677c478bd9Sstevel@tonic-gate comx:
4687c478bd9Sstevel@tonic-gate 	if ((c1 = c) == '\n')
4697c478bd9Sstevel@tonic-gate 		return;
4707c478bd9Sstevel@tonic-gate 	c2 = C;
4717c478bd9Sstevel@tonic-gate 	if ((c1 == '.') && (c2 != '.'))
4727c478bd9Sstevel@tonic-gate 		inmacro = NO;
4737c478bd9Sstevel@tonic-gate 	if (c2 == '\n')
4747c478bd9Sstevel@tonic-gate 		return;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if ((c1 == 'E') && (c2 == 'Q') && (filesp == files)) {
4777c478bd9Sstevel@tonic-gate 		eqn();
4787c478bd9Sstevel@tonic-gate 	} else if ((c1 == 'T') && ((c2 == 'S') || (c2 == 'C') ||
4797c478bd9Sstevel@tonic-gate 	    (c2 == '&')) && (filesp == files)) {
4807c478bd9Sstevel@tonic-gate 		if (msflag) {
4817c478bd9Sstevel@tonic-gate 			stbl();
4827c478bd9Sstevel@tonic-gate 		} else {
4837c478bd9Sstevel@tonic-gate 			tbl();
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 	} else if ((c1 == 'T') && (c2 == 'E')) {
4867c478bd9Sstevel@tonic-gate 		intable = NO;
4877c478bd9Sstevel@tonic-gate 	} else if (!inmacro && (c1 == 'd') && (c2 == 'e')) {
4887c478bd9Sstevel@tonic-gate 		macro();
4897c478bd9Sstevel@tonic-gate 	} else if (!inmacro && (c1 == 'i') && (c2 == 'g')) {
4907c478bd9Sstevel@tonic-gate 		macro();
4917c478bd9Sstevel@tonic-gate 	} else if (!inmacro && (c1 == 'a') && (c2 == 'm')) {
4927c478bd9Sstevel@tonic-gate 		macro();
4937c478bd9Sstevel@tonic-gate 	} else if ((c1 == 's') && (c2 == 'o')) {
4947c478bd9Sstevel@tonic-gate 		if (iflag) {
4957c478bd9Sstevel@tonic-gate 			SKIP;
4967c478bd9Sstevel@tonic-gate 		} else {
4977c478bd9Sstevel@tonic-gate 			getfname();
4987c478bd9Sstevel@tonic-gate 			if (fname[0]) {
4997c478bd9Sstevel@tonic-gate 				infile = *++filesp = opn(fname);
5007c478bd9Sstevel@tonic-gate 			}
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 	} else if ((c1 == 'n') && (c2 == 'x')) {
5037c478bd9Sstevel@tonic-gate 		if (iflag) {
5047c478bd9Sstevel@tonic-gate 			SKIP;
5057c478bd9Sstevel@tonic-gate 		} else {
5067c478bd9Sstevel@tonic-gate 			getfname();
5077c478bd9Sstevel@tonic-gate 			if (fname[0] == '\0') {
5087c478bd9Sstevel@tonic-gate 				exit(0);
5097c478bd9Sstevel@tonic-gate 			}
5107c478bd9Sstevel@tonic-gate 			if (infile != stdin) {
5117c478bd9Sstevel@tonic-gate 				(void) fclose(infile);
5127c478bd9Sstevel@tonic-gate 			}
5137c478bd9Sstevel@tonic-gate 			infile = *filesp = opn(fname);
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 	} else if ((c1 == 'h') && (c2 == 'w')) {
5167c478bd9Sstevel@tonic-gate 		SKIP;
5177c478bd9Sstevel@tonic-gate 	} else if (msflag && (c1 == 'T') && (c2 == 'L')) {
5187c478bd9Sstevel@tonic-gate 		SKIP_TO_COM;
5197c478bd9Sstevel@tonic-gate 		goto comx;
5207c478bd9Sstevel@tonic-gate 	} else if (msflag && (c1 == 'N') && (c2 == 'R')) {
5217c478bd9Sstevel@tonic-gate 		SKIP;
5227c478bd9Sstevel@tonic-gate 	} else if (msflag && (c1 == 'A') && ((c2 == 'U') || (c2 == 'I'))) {
5237c478bd9Sstevel@tonic-gate 		if (mac == MM) {
5247c478bd9Sstevel@tonic-gate 			SKIP;
5257c478bd9Sstevel@tonic-gate 		} else {
5267c478bd9Sstevel@tonic-gate 			SKIP_TO_COM;
5277c478bd9Sstevel@tonic-gate 			goto comx;
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 	} else if (msflag && (c1 == 'F') && (c2 == 'S')) {
5307c478bd9Sstevel@tonic-gate 		SKIP_TO_COM;
5317c478bd9Sstevel@tonic-gate 		goto comx;
5327c478bd9Sstevel@tonic-gate 	} else if (msflag && (c1 == 'S') && (c2 == 'H')) {
5337c478bd9Sstevel@tonic-gate 		SKIP_TO_COM;
5347c478bd9Sstevel@tonic-gate 		goto comx;
5357c478bd9Sstevel@tonic-gate 	} else if (msflag && (c1 == 'N') && (c2 == 'H')) {
5367c478bd9Sstevel@tonic-gate 		SKIP_TO_COM;
5377c478bd9Sstevel@tonic-gate 		goto comx;
5387c478bd9Sstevel@tonic-gate 	} else if (msflag && (c1 == 'O') && (c2 == 'K')) {
5397c478bd9Sstevel@tonic-gate 		SKIP_TO_COM;
5407c478bd9Sstevel@tonic-gate 		goto comx;
5417c478bd9Sstevel@tonic-gate 	} else if (msflag && (c1 == 'N') && (c2 == 'D')) {
5427c478bd9Sstevel@tonic-gate 		SKIP;
5437c478bd9Sstevel@tonic-gate 	} else if (msflag && (mac == MM) && (c1 == 'H') &&
5447c478bd9Sstevel@tonic-gate 	    ((c2 == ' ') || (c2 == 'U'))) {
5457c478bd9Sstevel@tonic-gate 		SKIP;
5467c478bd9Sstevel@tonic-gate 	} else if (msflag && (mac == MM) && (c2 == 'L')) {
5477c478bd9Sstevel@tonic-gate 		if (disp || (c1 == 'R')) {
5487c478bd9Sstevel@tonic-gate 			sdis('L', 'E');
5497c478bd9Sstevel@tonic-gate 		} else {
5507c478bd9Sstevel@tonic-gate 			SKIP;
5517c478bd9Sstevel@tonic-gate 			(void) putchar('.');
5527c478bd9Sstevel@tonic-gate 		}
5537c478bd9Sstevel@tonic-gate 	} else if (msflag && ((c1 == 'D') || (c1 == 'N') ||
5547c478bd9Sstevel@tonic-gate 	    (c1 == 'K') || (c1 == 'P')) && (c2 == 'S')) {
5557c478bd9Sstevel@tonic-gate 		sdis(c1, 'E');		/* removed RS-RE */
5567c478bd9Sstevel@tonic-gate 	} else if (msflag && (c1 == 'K' && c2 == 'F')) {
5577c478bd9Sstevel@tonic-gate 		sdis(c1, 'E');
5587c478bd9Sstevel@tonic-gate 	} else if (msflag && (c1 == 'n') && (c2 == 'f')) {
5597c478bd9Sstevel@tonic-gate 		sdis('f', 'i');
5607c478bd9Sstevel@tonic-gate 	} else if (msflag && (c1 == 'c') && (c2 == 'e')) {
5617c478bd9Sstevel@tonic-gate 		sce();
5627c478bd9Sstevel@tonic-gate 	} else {
5637c478bd9Sstevel@tonic-gate 		if ((c1 == '.') && (c2 == '.')) {
5647c478bd9Sstevel@tonic-gate 			while (C == '.')
5657c478bd9Sstevel@tonic-gate 				;
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 		++inmacro;
5687c478bd9Sstevel@tonic-gate 		if ((c1 <= 'Z') && msflag) {
5697c478bd9Sstevel@tonic-gate 			regline(YES, ONE);
5707c478bd9Sstevel@tonic-gate 		} else {
5717c478bd9Sstevel@tonic-gate 			regline(YES, TWO);
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 		--inmacro;
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate static void
macro(void)5807c478bd9Sstevel@tonic-gate macro(void)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	if (msflag) {
5837c478bd9Sstevel@tonic-gate 		/* look for  .. */
5847c478bd9Sstevel@tonic-gate 		do {
5857c478bd9Sstevel@tonic-gate 			SKIP;
5867c478bd9Sstevel@tonic-gate 		} while ((C != '.') || (C != '.') || (C == '.'));
5877c478bd9Sstevel@tonic-gate 		if (c != '\n') {
5887c478bd9Sstevel@tonic-gate 			SKIP;
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 		return;
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	SKIP;
5937c478bd9Sstevel@tonic-gate 	inmacro = YES;
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate static void
sdis(char a1,char a2)6007c478bd9Sstevel@tonic-gate sdis(char a1, char a2)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate 	int c1, c2;
6037c478bd9Sstevel@tonic-gate 	int eqnf;
6047c478bd9Sstevel@tonic-gate 	int notdone = 1;
6057c478bd9Sstevel@tonic-gate 	eqnf = 1;
6067c478bd9Sstevel@tonic-gate 	SKIP;
6077c478bd9Sstevel@tonic-gate 	while (notdone) {
6087c478bd9Sstevel@tonic-gate 		while (C != '.')
6097c478bd9Sstevel@tonic-gate 			SKIP;
6107c478bd9Sstevel@tonic-gate 		if ((c1 = C) == '\n')
6117c478bd9Sstevel@tonic-gate 			continue;
6127c478bd9Sstevel@tonic-gate 		if ((c2 = C) == '\n')
6137c478bd9Sstevel@tonic-gate 			continue;
6147c478bd9Sstevel@tonic-gate 		if ((c1 == a1) && (c2 == a2)) {
6157c478bd9Sstevel@tonic-gate 			SKIP;
6167c478bd9Sstevel@tonic-gate 			if (eqnf)
6177c478bd9Sstevel@tonic-gate 				(void) putchar('.');
6187c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
6197c478bd9Sstevel@tonic-gate 			return;
6207c478bd9Sstevel@tonic-gate 		} else if ((a1 == 'D') && (c1 == 'E') && (c2 == 'Q')) {
6217c478bd9Sstevel@tonic-gate 			eqn();
6227c478bd9Sstevel@tonic-gate 			eqnf = 0;
6237c478bd9Sstevel@tonic-gate 		} else {
6247c478bd9Sstevel@tonic-gate 			SKIP;
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate static void
tbl(void)6307c478bd9Sstevel@tonic-gate tbl(void)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	while (C != '.')
6337c478bd9Sstevel@tonic-gate 		;
6347c478bd9Sstevel@tonic-gate 	SKIP;
6357c478bd9Sstevel@tonic-gate 	intable = YES;
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate static void
stbl(void)6397c478bd9Sstevel@tonic-gate stbl(void)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	while (C != '.')
6427c478bd9Sstevel@tonic-gate 		;
6437c478bd9Sstevel@tonic-gate 	SKIP_TO_COM;
6447c478bd9Sstevel@tonic-gate 	if ((c != 'T') || (C != 'E')) {
6457c478bd9Sstevel@tonic-gate 		SKIP;
6467c478bd9Sstevel@tonic-gate 		pc = c;
6477c478bd9Sstevel@tonic-gate 		while ((C != '.') || (pc != '\n') ||
6487c478bd9Sstevel@tonic-gate 		    (C != 'T') || (C != 'E')) {
6497c478bd9Sstevel@tonic-gate 			pc = c;
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate static void
eqn(void)6557c478bd9Sstevel@tonic-gate eqn(void)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	int c1, c2;
6587c478bd9Sstevel@tonic-gate 	int dflg;
6597c478bd9Sstevel@tonic-gate 	int last;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	last = 0;
6627c478bd9Sstevel@tonic-gate 	dflg = 1;
6637c478bd9Sstevel@tonic-gate 	SKIP;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	for (;;) {
6667c478bd9Sstevel@tonic-gate 		if ((C1 == '.') || (c == '\'')) {
6677c478bd9Sstevel@tonic-gate 			while ((C1 == ' ') || (c == '\t'))
6687c478bd9Sstevel@tonic-gate 				;
6697c478bd9Sstevel@tonic-gate 			if ((c == 'E') && (C1 == 'N')) {
6707c478bd9Sstevel@tonic-gate 				SKIP;
6717c478bd9Sstevel@tonic-gate 				if (msflag && dflg) {
6727c478bd9Sstevel@tonic-gate 					(void) putchar('x');
6737c478bd9Sstevel@tonic-gate 					(void) putchar(' ');
6747c478bd9Sstevel@tonic-gate 					if (last) {
6757c478bd9Sstevel@tonic-gate 						(void) putchar('.');
6767c478bd9Sstevel@tonic-gate 						(void) putchar(' ');
6777c478bd9Sstevel@tonic-gate 					}
6787c478bd9Sstevel@tonic-gate 				}
6797c478bd9Sstevel@tonic-gate 				return;
6807c478bd9Sstevel@tonic-gate 			}
6817c478bd9Sstevel@tonic-gate 		} else if (c == 'd') {	/* look for delim */
6827c478bd9Sstevel@tonic-gate 			if ((C1 == 'e') && (C1 == 'l')) {
6837c478bd9Sstevel@tonic-gate 				if ((C1 == 'i') && (C1 == 'm')) {
6847c478bd9Sstevel@tonic-gate 					while (C1 == ' ')
6857c478bd9Sstevel@tonic-gate 						;
6867c478bd9Sstevel@tonic-gate 					if (((c1 = c) == '\n') ||
6877c478bd9Sstevel@tonic-gate 					    ((c2 = C1) == '\n') ||
6887c478bd9Sstevel@tonic-gate 					    ((c1 == 'o') && (c2 == 'f') &&
6897c478bd9Sstevel@tonic-gate 					    (C1 == 'f'))) {
6907c478bd9Sstevel@tonic-gate 						ldelim = NOCHAR;
6917c478bd9Sstevel@tonic-gate 						rdelim = NOCHAR;
6927c478bd9Sstevel@tonic-gate 					} else {
6937c478bd9Sstevel@tonic-gate 						ldelim = c1;
6947c478bd9Sstevel@tonic-gate 						rdelim = c2;
6957c478bd9Sstevel@tonic-gate 					}
6967c478bd9Sstevel@tonic-gate 				}
6977c478bd9Sstevel@tonic-gate 				dflg = 0;
6987c478bd9Sstevel@tonic-gate 			}
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		if (c != '\n') {
7027c478bd9Sstevel@tonic-gate 			while (C1 != '\n') {
7037c478bd9Sstevel@tonic-gate 				if (c == '.') {
7047c478bd9Sstevel@tonic-gate 					last = 1;
7057c478bd9Sstevel@tonic-gate 				} else {
7067c478bd9Sstevel@tonic-gate 					last = 0;
7077c478bd9Sstevel@tonic-gate 				}
7087c478bd9Sstevel@tonic-gate 			}
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate static void
backsl(void)7167c478bd9Sstevel@tonic-gate backsl(void)	/* skip over a complete backslash construction */
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	int bdelim;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate sw:	switch (C) {
7217c478bd9Sstevel@tonic-gate 	case '"':
7227c478bd9Sstevel@tonic-gate 		SKIP;
7237c478bd9Sstevel@tonic-gate 		return;
7247c478bd9Sstevel@tonic-gate 	case 's':
7257c478bd9Sstevel@tonic-gate 		if (C == '\\') {
7267c478bd9Sstevel@tonic-gate 			backsl();
7277c478bd9Sstevel@tonic-gate 		} else {
7287c478bd9Sstevel@tonic-gate 			while ((C >= '0') && (c <= '9'))
7297c478bd9Sstevel@tonic-gate 				;
7307c478bd9Sstevel@tonic-gate 			(void) ungetc(c, infile);
7317c478bd9Sstevel@tonic-gate 			c = '0';
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 		lindx--;
7347c478bd9Sstevel@tonic-gate 		return;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	case 'f':
7377c478bd9Sstevel@tonic-gate 	case 'n':
7387c478bd9Sstevel@tonic-gate 	case '*':
7397c478bd9Sstevel@tonic-gate 		if (C != '(')
7407c478bd9Sstevel@tonic-gate 			return;
7417c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	case '(':
7447c478bd9Sstevel@tonic-gate 		if (C != '\n') {
7457c478bd9Sstevel@tonic-gate 			(void) C;
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 		return;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	case '$':
7507c478bd9Sstevel@tonic-gate 		(void) C;	/* discard argument number */
7517c478bd9Sstevel@tonic-gate 		return;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	case 'b':
7547c478bd9Sstevel@tonic-gate 	case 'x':
7557c478bd9Sstevel@tonic-gate 	case 'v':
7567c478bd9Sstevel@tonic-gate 	case 'h':
7577c478bd9Sstevel@tonic-gate 	case 'w':
7587c478bd9Sstevel@tonic-gate 	case 'o':
7597c478bd9Sstevel@tonic-gate 	case 'l':
7607c478bd9Sstevel@tonic-gate 	case 'L':
7617c478bd9Sstevel@tonic-gate 		if ((bdelim = C) == '\n')
7627c478bd9Sstevel@tonic-gate 			return;
7637c478bd9Sstevel@tonic-gate 		while ((C != '\n') && (c != bdelim))
7647c478bd9Sstevel@tonic-gate 			if (c == '\\')
7657c478bd9Sstevel@tonic-gate 				backsl();
7667c478bd9Sstevel@tonic-gate 		return;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	case '\\':
7697c478bd9Sstevel@tonic-gate 		if (inmacro)
7707c478bd9Sstevel@tonic-gate 			goto sw;
7717c478bd9Sstevel@tonic-gate 	default:
7727c478bd9Sstevel@tonic-gate 		return;
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate static char *
copys(char * s)7807c478bd9Sstevel@tonic-gate copys(char *s)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate 	char *t, *t0;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	if ((t0 = t = calloc((unsigned)(strlen(s) + 1), sizeof (*t))) == NULL)
7857c478bd9Sstevel@tonic-gate 		fatal_msg(gettext("Cannot allocate memory"));
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	while (*t++ = *s++)
7887c478bd9Sstevel@tonic-gate 		;
7897c478bd9Sstevel@tonic-gate 	return (t0);
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate static void
sce(void)7937c478bd9Sstevel@tonic-gate sce(void)
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	char *ap;
7967c478bd9Sstevel@tonic-gate 	int n, i;
7977c478bd9Sstevel@tonic-gate 	char a[10];
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	for (ap = a; C != '\n'; ap++) {
8007c478bd9Sstevel@tonic-gate 		*ap = c;
8017c478bd9Sstevel@tonic-gate 		if (ap == &a[9]) {
8027c478bd9Sstevel@tonic-gate 			SKIP;
8037c478bd9Sstevel@tonic-gate 			ap = a;
8047c478bd9Sstevel@tonic-gate 			break;
8057c478bd9Sstevel@tonic-gate 		}
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 	if (ap != a) {
8087c478bd9Sstevel@tonic-gate 		n = atoi(a);
8097c478bd9Sstevel@tonic-gate 	} else {
8107c478bd9Sstevel@tonic-gate 		n = 1;
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; ) {
8137c478bd9Sstevel@tonic-gate 		if (C == '.') {
8147c478bd9Sstevel@tonic-gate 			if (C == 'c') {
8157c478bd9Sstevel@tonic-gate 				if (C == 'e') {
8167c478bd9Sstevel@tonic-gate 					while (C == ' ')
8177c478bd9Sstevel@tonic-gate 						;
8187c478bd9Sstevel@tonic-gate 					if (c == '0') {
8197c478bd9Sstevel@tonic-gate 						break;
8207c478bd9Sstevel@tonic-gate 					} else {
8217c478bd9Sstevel@tonic-gate 						SKIP;
8227c478bd9Sstevel@tonic-gate 					}
8237c478bd9Sstevel@tonic-gate 				} else {
8247c478bd9Sstevel@tonic-gate 					SKIP;
8257c478bd9Sstevel@tonic-gate 				}
8267c478bd9Sstevel@tonic-gate 			} else {
8277c478bd9Sstevel@tonic-gate 				SKIP;
8287c478bd9Sstevel@tonic-gate 			}
8297c478bd9Sstevel@tonic-gate 		} else {
8307c478bd9Sstevel@tonic-gate 			SKIP;
8317c478bd9Sstevel@tonic-gate 			i++;
8327c478bd9Sstevel@tonic-gate 		}
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate }
835