xref: /csrg-svn/usr.bin/vgrind/vfontedpr.c (revision 62423)
122065Sdist /*
2*62423Sbostic  * Copyright (c) 1980, 1993
3*62423Sbostic  *	The Regents of the University of California.  All rights reserved.
436167Sbostic  *
556224Sralph  * %sccs.include.redist.c%
622065Sdist  */
78760Smckusick 
822065Sdist #ifndef lint
956272Selan static char copyright[] =
10*62423Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*62423Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1236167Sbostic #endif /* not lint */
1322065Sdist 
1436167Sbostic #ifndef lint
15*62423Sbostic static char sccsid[] = "@(#)vfontedpr.c	8.1 (Berkeley) 06/06/93";
1636167Sbostic #endif /* not lint */
1736167Sbostic 
1837561Sbostic #include <sys/types.h>
1937561Sbostic #include <sys/stat.h>
2056143Selan #include <time.h>
211841Sroot #include <ctype.h>
2256143Selan #include <stdlib.h>
2356143Selan #include <string.h>
241841Sroot #include <stdio.h>
2537561Sbostic #include "pathnames.h"
2656143Selan #include "extern.h"
271841Sroot 
282199Sroot #define FALSE 0
2956143Selan #define TRUE !(FALSE)
302199Sroot #define NIL 0
313882Spresott #define STANDARD 0
323882Spresott #define ALTERNATE 1
332199Sroot 
341841Sroot /*
351841Sroot  * Vfontedpr.
361841Sroot  *
372199Sroot  * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
381862Sroot  *
391841Sroot  */
401862Sroot 
411862Sroot #define STRLEN 10		/* length of strings introducing things */
421862Sroot #define PNAMELEN 40		/* length of a function/procedure name */
432199Sroot #define PSMAX 20		/* size of procedure name stacking */
441862Sroot 
4556143Selan static int       iskw __P((char *));
4656143Selan static boolean   isproc __P((char *));
4756143Selan static void      putKcp __P((char *, char *, boolean));
4856143Selan static void      putScp __P((char *));
4956143Selan static void      putcp __P((int));
5056143Selan static int       tabs __P((char *, char *));
5156143Selan static int       width __P((char *, char *));
521862Sroot 
532199Sroot /*
542199Sroot  *	The state variables
552199Sroot  */
561862Sroot 
5756143Selan static boolean  filter = FALSE;	/* act as a filter (like eqn) */
5856143Selan static boolean	inchr;		/* in a string constant */
5956143Selan static boolean	incomm;		/* in a comment of the primary type */
6056143Selan static boolean	idx = FALSE;	/* form an index */
6156143Selan static boolean	instr;		/* in a string constant */
6256143Selan static boolean	nokeyw = FALSE;	/* no keywords being flagged */
6356143Selan static boolean  pass = FALSE;	/*
6456143Selan 				 * when acting as a filter, pass indicates
652199Sroot 				 * whether we are currently processing
662199Sroot 				 * input.
672199Sroot 				 */
681862Sroot 
6956224Sralph static int	blklevel;	/* current nesting level */
7056224Sralph static int	comtype;	/* type of comment */
7156143Selan static char    *defsfile[2] = { _PATH_VGRINDEFS, 0 };
7256143Selan 				/* name of language definitions file */
7356224Sralph static int	margin;
7456224Sralph static int	plstack[PSMAX];	/* the procedure nesting level stack */
7556143Selan static char	pname[BUFSIZ+1];
7656143Selan static boolean  prccont;	/* continue last procedure */
7756224Sralph static int	psptr;		/* the stack index of the current procedure */
7856143Selan static char	pstack[PSMAX][PNAMELEN+1];	/* the procedure name stack */
7956143Selan 
802199Sroot /*
812199Sroot  *	The language specific globals
822199Sroot  */
831862Sroot 
843882Spresott char	*l_acmbeg;		/* string introducing a comment */
853882Spresott char	*l_acmend;		/* string ending a comment */
862199Sroot char	*l_blkbeg;		/* string begining of a block */
872199Sroot char	*l_blkend;		/* string ending a block */
8856143Selan char    *l_chrbeg;		/* delimiter for character constant */
8956143Selan char    *l_chrend;		/* delimiter for character constant */
9056143Selan char	*l_combeg;		/* string introducing a comment */
9156143Selan char	*l_comend;		/* string ending a comment */
9256143Selan char	 l_escape;		/* character used to  escape characters */
9356143Selan char	*l_keywds[BUFSIZ/2];	/* keyword table address */
9456143Selan char	*l_prcbeg;		/* regular expr for procedure begin */
952199Sroot char    *l_strbeg;		/* delimiter for string constant */
962199Sroot char    *l_strend;		/* delimiter for string constant */
9756143Selan boolean	 l_toplex;		/* procedures only defined at top lex level */
9856143Selan char	*language = "c";	/* the language indicator */
991878Sroot 
1002199Sroot #define	ps(x)	printf("%s", x)
1011862Sroot 
10256143Selan void
main(argc,argv)1032199Sroot main(argc, argv)
1042199Sroot     int argc;
1052199Sroot     char *argv[];
1062199Sroot {
1072199Sroot     char *fname = "";
1082199Sroot     struct stat stbuf;
1092199Sroot     char buf[BUFSIZ];
11056143Selan     char *defs;
1112199Sroot     int needbp = 0;
1121862Sroot 
1132199Sroot     argc--, argv++;
1142199Sroot     do {
1152199Sroot 	char *cp;
1162199Sroot 	int i;
1171878Sroot 
1182199Sroot 	if (argc > 0) {
1192199Sroot 	    if (!strcmp(argv[0], "-h")) {
1202199Sroot 		if (argc == 1) {
1212199Sroot 		    printf("'ds =H\n");
1222199Sroot 		    argc = 0;
1232199Sroot 		    goto rest;
1242199Sroot 		}
1252199Sroot 		printf("'ds =H %s\n", argv[1]);
1263393Spresott 		argc--, argv++;
1273393Spresott 		argc--, argv++;
1282199Sroot 		if (argc > 0)
1292199Sroot 		    continue;
1302199Sroot 		goto rest;
1312199Sroot 	    }
1321862Sroot 
1332199Sroot 	    /* act as a filter like eqn */
1342199Sroot 	    if (!strcmp(argv[0], "-f")) {
1352199Sroot 		filter++;
1362199Sroot 		argv[0] = argv[argc-1];
1372199Sroot 		argv[argc-1] = "-";
1382199Sroot 		continue;
1392199Sroot 	    }
1401862Sroot 
1412199Sroot 	    /* take input from the standard place */
1422199Sroot 	    if (!strcmp(argv[0], "-")) {
1432199Sroot 		argc = 0;
1442199Sroot 		goto rest;
1452199Sroot 	    }
1461878Sroot 
1472199Sroot 	    /* build an index */
1482199Sroot 	    if (!strcmp(argv[0], "-x")) {
14956143Selan 		idx++;
1502199Sroot 		argv[0] = "-n";
1512199Sroot 	    }
1521841Sroot 
1532199Sroot 	    /* indicate no keywords */
1542199Sroot 	    if (!strcmp(argv[0], "-n")) {
1552199Sroot 		nokeyw++;
1562199Sroot 		argc--, argv++;
1572199Sroot 		continue;
1582199Sroot 	    }
1591862Sroot 
1602199Sroot 	    /* specify the font size */
1612199Sroot 	    if (!strncmp(argv[0], "-s", 2)) {
1622199Sroot 		i = 0;
1632199Sroot 		cp = argv[0] + 2;
1642199Sroot 		while (*cp)
1652199Sroot 		    i = i * 10 + (*cp++ - '0');
1662199Sroot 		printf("'ps %d\n'vs %d\n", i, i+1);
1672199Sroot 		argc--, argv++;
1682199Sroot 		continue;
1692199Sroot 	    }
1701841Sroot 
1712199Sroot 	    /* specify the language */
1722199Sroot 	    if (!strncmp(argv[0], "-l", 2)) {
1732199Sroot 		language = argv[0]+2;
1742199Sroot 		argc--, argv++;
1752199Sroot 		continue;
1762199Sroot 	    }
1771841Sroot 
1782199Sroot 	    /* specify the language description file */
1792199Sroot 	    if (!strncmp(argv[0], "-d", 2)) {
18056143Selan 		defsfile[0] = argv[1];
1812199Sroot 		argc--, argv++;
1822199Sroot 		argc--, argv++;
1832199Sroot 		continue;
1842199Sroot 	    }
1851862Sroot 
1862199Sroot 	    /* open the file for input */
1872199Sroot 	    if (freopen(argv[0], "r", stdin) == NULL) {
1882199Sroot 		perror(argv[0]);
1892199Sroot 		exit(1);
1902199Sroot 	    }
19156143Selan 	    if (idx)
1922199Sroot 		printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
1932199Sroot 	    fname = argv[0];
1942199Sroot 	    argc--, argv++;
1952199Sroot 	}
1962199Sroot     rest:
1971862Sroot 
1982199Sroot 	/*
1992199Sroot 	 *  get the  language definition from the defs file
2002199Sroot 	 */
20156143Selan 	i = cgetent(&defs, defsfile, language);
20256143Selan 	if (i == -1) {
2032199Sroot 	    fprintf (stderr, "no entry for language %s\n", language);
2042199Sroot 	    exit (0);
20556143Selan 	} else  if (i == -2) { fprintf(stderr,
20656143Selan 	    "cannot find vgrindefs file %s\n", defsfile[0]);
2072199Sroot 	    exit (0);
20856143Selan 	} else if (i == -3) { fprintf(stderr,
20956143Selan 	    "potential reference loop detected in vgrindefs file %s\n",
21056143Selan             defsfile[0]);
21156143Selan 	    exit(0);
2122199Sroot 	}
21356143Selan 	if (cgetustr(defs, "kw", &cp) == -1)
2142199Sroot 	    nokeyw = TRUE;
2152199Sroot 	else  {
2162199Sroot 	    char **cpp;
2171878Sroot 
2182199Sroot 	    cpp = l_keywds;
2192199Sroot 	    while (*cp) {
2202199Sroot 		while (*cp == ' ' || *cp =='\t')
2212199Sroot 		    *cp++ = NULL;
2222199Sroot 		if (*cp)
2232199Sroot 		    *cpp++ = cp;
2242199Sroot 		while (*cp != ' ' && *cp  != '\t' && *cp)
2252199Sroot 		    cp++;
2262199Sroot 	    }
2272199Sroot 	    *cpp = NIL;
2282199Sroot 	}
22956143Selan 	cgetustr(defs, "pb", &cp);
23056143Selan 	l_prcbeg = convexp(cp);
23156143Selan 	cgetustr(defs, "cb", &cp);
23256143Selan 	l_combeg = convexp(cp);
23356143Selan 	cgetustr(defs, "ce", &cp);
23456143Selan 	l_comend = convexp(cp);
23556143Selan 	cgetustr(defs, "ab", &cp);
23656143Selan 	l_acmbeg = convexp(cp);
23756143Selan 	cgetustr(defs, "ae", &cp);
23856143Selan 	l_acmend = convexp(cp);
23956143Selan 	cgetustr(defs, "sb", &cp);
24056143Selan 	l_strbeg = convexp(cp);
24156143Selan 	cgetustr(defs, "se", &cp);
24256143Selan 	l_strend = convexp(cp);
24356143Selan 	cgetustr(defs, "bb", &cp);
24456143Selan 	l_blkbeg = convexp(cp);
24556143Selan 	cgetustr(defs, "be", &cp);
24656143Selan 	l_blkend = convexp(cp);
24756143Selan 	cgetustr(defs, "lb", &cp);
24856143Selan 	l_chrbeg = convexp(cp);
24956143Selan 	cgetustr(defs, "le", &cp);
25056143Selan 	l_chrend = convexp(cp);
2512199Sroot 	l_escape = '\\';
25256143Selan 	l_onecase = (cgetcap(defs, "oc", ':') != NULL);
25356143Selan 	l_toplex = (cgetcap(defs, "tl", ':') != NULL);
2543882Spresott 
2552199Sroot 	/* initialize the program */
2561878Sroot 
2572199Sroot 	incomm = FALSE;
2582199Sroot 	instr = FALSE;
2592199Sroot 	inchr = FALSE;
2602199Sroot 	_escaped = FALSE;
2612199Sroot 	blklevel = 0;
2622199Sroot 	for (psptr=0; psptr<PSMAX; psptr++) {
2632199Sroot 	    pstack[psptr][0] = NULL;
2642199Sroot 	    plstack[psptr] = 0;
2652199Sroot 	}
2662199Sroot 	psptr = -1;
2672199Sroot 	ps("'-F\n");
2682199Sroot 	if (!filter) {
2692199Sroot 	    printf(".ds =F %s\n", fname);
2703393Spresott 	    ps("'wh 0 vH\n");
2713393Spresott 	    ps("'wh -1i vF\n");
2722199Sroot 	}
2732199Sroot 	if (needbp) {
2742199Sroot 	    needbp = 0;
2752199Sroot 	    printf(".()\n");
2762199Sroot 	    printf(".bp\n");
2772199Sroot 	}
27829570Sdonn 	if (!filter) {
27929570Sdonn 	    fstat(fileno(stdin), &stbuf);
28029570Sdonn 	    cp = ctime(&stbuf.st_mtime);
28129570Sdonn 	    cp[16] = '\0';
28229570Sdonn 	    cp[24] = '\0';
28329570Sdonn 	    printf(".ds =M %s %s\n", cp+4, cp+20);
28429570Sdonn 	}
2851878Sroot 
2862199Sroot 	/*
2872199Sroot 	 *	MAIN LOOP!!!
2882199Sroot 	 */
2892199Sroot 	while (fgets(buf, sizeof buf, stdin) != NULL) {
2902199Sroot 	    if (buf[0] == '\f') {
2912199Sroot 		printf(".bp\n");
2922199Sroot 	    }
2932199Sroot 	    if (buf[0] == '.') {
2942199Sroot 		printf("%s", buf);
2952199Sroot 		if (!strncmp (buf+1, "vS", 2))
2962199Sroot 		    pass = TRUE;
2972199Sroot 		if (!strncmp (buf+1, "vE", 2))
2982199Sroot 		    pass = FALSE;
2992199Sroot 		continue;
3002199Sroot 	    }
3012199Sroot 	    prccont = FALSE;
3022199Sroot 	    if (!filter || pass)
3032199Sroot 		putScp(buf);
3042199Sroot 	    else
3052199Sroot 		printf("%s", buf);
3062199Sroot 	    if (prccont && (psptr >= 0)) {
3072199Sroot 		ps("'FC ");
3082199Sroot 		ps(pstack[psptr]);
3092199Sroot 		ps("\n");
3102199Sroot 	    }
3112199Sroot #ifdef DEBUG
3122199Sroot 	    printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
3132199Sroot #endif
3142199Sroot 	    margin = 0;
3152199Sroot 	}
3162199Sroot 	needbp = 1;
3172199Sroot     } while (argc > 0);
3182199Sroot     exit(0);
3191841Sroot }
3201841Sroot 
3211841Sroot #define isidchr(c) (isalnum(c) || (c) == '_')
3221841Sroot 
32356143Selan static void
putScp(os)3241841Sroot putScp(os)
3252199Sroot     char *os;
3261841Sroot {
3272199Sroot     register char *s = os;		/* pointer to unmatched string */
3282199Sroot     char dummy[BUFSIZ];			/* dummy to be used by expmatch */
3292199Sroot     char *comptr;			/* end of a comment delimiter */
3303882Spresott     char *acmptr;			/* end of a comment delimiter */
3312199Sroot     char *strptr;			/* end of a string delimiter */
3322199Sroot     char *chrptr;			/* end of a character const delimiter */
3332199Sroot     char *blksptr;			/* end of a lexical block start */
3342199Sroot     char *blkeptr;			/* end of a lexical block end */
3351841Sroot 
3362199Sroot     _start = os;			/* remember the start for expmatch */
3372199Sroot     _escaped = FALSE;
3382199Sroot     if (nokeyw || incomm || instr)
3392199Sroot 	goto skip;
3402199Sroot     if (isproc(s)) {
3412199Sroot 	ps("'FN ");
3422199Sroot 	ps(pname);
3432293Sroot         ps("\n");
3442199Sroot 	if (psptr < PSMAX) {
3452199Sroot 	    ++psptr;
3462199Sroot 	    strncpy (pstack[psptr], pname, PNAMELEN);
3472199Sroot 	    pstack[psptr][PNAMELEN] = NULL;
3482199Sroot 	    plstack[psptr] = blklevel;
3492199Sroot 	}
3502199Sroot     }
3511841Sroot skip:
3522199Sroot     do {
3532199Sroot 	/* check for string, comment, blockstart, etc */
3542199Sroot 	if (!incomm && !instr && !inchr) {
3552199Sroot 
3562199Sroot 	    blkeptr = expmatch (s, l_blkend, dummy);
3572199Sroot 	    blksptr = expmatch (s, l_blkbeg, dummy);
3582199Sroot 	    comptr = expmatch (s, l_combeg, dummy);
3593882Spresott 	    acmptr = expmatch (s, l_acmbeg, dummy);
3602199Sroot 	    strptr = expmatch (s, l_strbeg, dummy);
3612199Sroot 	    chrptr = expmatch (s, l_chrbeg, dummy);
3622199Sroot 
3632199Sroot 	    /* start of a comment? */
3642199Sroot 	    if (comptr != NIL)
3652199Sroot 		if ((comptr < strptr || strptr == NIL)
3663882Spresott 		  && (comptr < acmptr || acmptr == NIL)
3672199Sroot 		  && (comptr < chrptr || chrptr == NIL)
3682199Sroot 		  && (comptr < blksptr || blksptr == NIL)
3692199Sroot 		  && (comptr < blkeptr || blkeptr == NIL)) {
3702199Sroot 		    putKcp (s, comptr-1, FALSE);
3712199Sroot 		    s = comptr;
3722199Sroot 		    incomm = TRUE;
3733882Spresott 		    comtype = STANDARD;
3742199Sroot 		    if (s != os)
3752199Sroot 			ps ("\\c");
3762199Sroot 		    ps ("\\c\n'+C\n");
3772199Sroot 		    continue;
3781841Sroot 		}
3791878Sroot 
3803882Spresott 	    /* start of a comment? */
3813882Spresott 	    if (acmptr != NIL)
3823882Spresott 		if ((acmptr < strptr || strptr == NIL)
3833882Spresott 		  && (acmptr < chrptr || chrptr == NIL)
3843882Spresott 		  && (acmptr < blksptr || blksptr == NIL)
3853882Spresott 		  && (acmptr < blkeptr || blkeptr == NIL)) {
3863882Spresott 		    putKcp (s, acmptr-1, FALSE);
3873882Spresott 		    s = acmptr;
3883882Spresott 		    incomm = TRUE;
3893882Spresott 		    comtype = ALTERNATE;
3903882Spresott 		    if (s != os)
3913882Spresott 			ps ("\\c");
3923882Spresott 		    ps ("\\c\n'+C\n");
3933882Spresott 		    continue;
3943882Spresott 		}
3953882Spresott 
3962199Sroot 	    /* start of a string? */
3972199Sroot 	    if (strptr != NIL)
3982199Sroot 		if ((strptr < chrptr || chrptr == NIL)
3992212Sroot 		  && (strptr < blksptr || blksptr == NIL)
4002212Sroot 		  && (strptr < blkeptr || blkeptr == NIL)) {
4012199Sroot 		    putKcp (s, strptr-1, FALSE);
4022199Sroot 		    s = strptr;
4032199Sroot 		    instr = TRUE;
4042199Sroot 		    continue;
4052199Sroot 		}
4061878Sroot 
4072199Sroot 	    /* start of a character string? */
4082199Sroot 	    if (chrptr != NIL)
4092212Sroot 		if ((chrptr < blksptr || blksptr == NIL)
4102212Sroot 		  && (chrptr < blkeptr || blkeptr == NIL)) {
4112199Sroot 		    putKcp (s, chrptr-1, FALSE);
4122199Sroot 		    s = chrptr;
4132199Sroot 		    inchr = TRUE;
4142199Sroot 		    continue;
4151841Sroot 		}
4161878Sroot 
4172199Sroot 	    /* end of a lexical block */
4182199Sroot 	    if (blkeptr != NIL) {
4192199Sroot 		if (blkeptr < blksptr || blksptr == NIL) {
4202199Sroot 		    putKcp (s, blkeptr - 1, FALSE);
4212199Sroot 		    s = blkeptr;
4222199Sroot 		    blklevel--;
4232199Sroot 		    if (psptr >= 0 && plstack[psptr] >= blklevel) {
4241878Sroot 
4252199Sroot 			/* end of current procedure */
4261841Sroot 			if (s != os)
4272199Sroot 			    ps ("\\c");
4282199Sroot 			ps ("\\c\n'-F\n");
4292199Sroot 			blklevel = plstack[psptr];
4301878Sroot 
4312199Sroot 			/* see if we should print the last proc name */
4322199Sroot 			if (--psptr >= 0)
4332199Sroot 			    prccont = TRUE;
4342199Sroot 			else
4352199Sroot 			    psptr = -1;
4362199Sroot 		    }
4372199Sroot 		    continue;
4381841Sroot 		}
4392199Sroot 	    }
4401878Sroot 
4412199Sroot 	    /* start of a lexical block */
4422199Sroot 	    if (blksptr != NIL) {
4432199Sroot 		putKcp (s, blksptr - 1, FALSE);
4442199Sroot 		s = blksptr;
4452199Sroot 		blklevel++;
4462199Sroot 		continue;
4472199Sroot 	    }
4482199Sroot 
4492199Sroot 	/* check for end of comment */
4502199Sroot 	} else if (incomm) {
4513882Spresott 	    comptr = expmatch (s, l_comend, dummy);
4523882Spresott 	    acmptr = expmatch (s, l_acmend, dummy);
4533882Spresott 	    if (((comtype == STANDARD) && (comptr != NIL)) ||
4543882Spresott 	        ((comtype == ALTERNATE) && (acmptr != NIL))) {
4553882Spresott 		if (comtype == STANDARD) {
4563882Spresott 		    putKcp (s, comptr-1, TRUE);
4573882Spresott 		    s = comptr;
4583882Spresott 		} else {
4593882Spresott 		    putKcp (s, acmptr-1, TRUE);
4603882Spresott 		    s = acmptr;
4613882Spresott 		}
4622199Sroot 		incomm = FALSE;
4632199Sroot 		ps("\\c\n'-C\n");
4642199Sroot 		continue;
4652199Sroot 	    } else {
46617500Sralph 		putKcp (s, s + strlen(s) -1, TRUE);
4672199Sroot 		s = s + strlen(s);
4682199Sroot 		continue;
4692199Sroot 	    }
4702199Sroot 
4712199Sroot 	/* check for end of string */
4722199Sroot 	} else if (instr) {
4732199Sroot 	    if ((strptr = expmatch (s, l_strend, dummy)) != NIL) {
4742199Sroot 		putKcp (s, strptr-1, TRUE);
4752199Sroot 		s = strptr;
4762199Sroot 		instr = FALSE;
4772199Sroot 		continue;
4782199Sroot 	    } else {
4792199Sroot 		putKcp (s, s+strlen(s)-1, TRUE);
4802199Sroot 		s = s + strlen(s);
4812199Sroot 		continue;
4822199Sroot 	    }
4832199Sroot 
4842199Sroot 	/* check for end of character string */
4852199Sroot 	} else if (inchr) {
4862199Sroot 	    if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) {
4872199Sroot 		putKcp (s, chrptr-1, TRUE);
4882199Sroot 		s = chrptr;
4892199Sroot 		inchr = FALSE;
4902199Sroot 		continue;
4912199Sroot 	    } else {
4922199Sroot 		putKcp (s, s+strlen(s)-1, TRUE);
4932199Sroot 		s = s + strlen(s);
4942199Sroot 		continue;
4952199Sroot 	    }
4961841Sroot 	}
4972199Sroot 
4982199Sroot 	/* print out the line */
4992199Sroot 	putKcp (s, s + strlen(s) -1, FALSE);
5002199Sroot 	s = s + strlen(s);
5012199Sroot     } while (*s);
5021841Sroot }
5031841Sroot 
50456143Selan static void
putKcp(start,end,force)5052199Sroot putKcp (start, end, force)
5062199Sroot     char	*start;		/* start of string to write */
5072199Sroot     char	*end;		/* end of string to write */
5082199Sroot     boolean	force;		/* true if we should force nokeyw */
5092199Sroot {
5102199Sroot     int i;
5112320Sroot     int xfld = 0;
5122199Sroot 
5132199Sroot     while (start <= end) {
51456143Selan 	if (idx) {
5152320Sroot 	    if (*start == ' ' || *start == '\t') {
5162320Sroot 		if (xfld == 0)
5172320Sroot 		    printf("");
5182320Sroot 		printf("\t");
5192320Sroot 		xfld = 1;
5202320Sroot 		while (*start == ' ' || *start == '\t')
5212320Sroot 		    start++;
5222320Sroot 		continue;
5232320Sroot 	    }
5242320Sroot 	}
5252199Sroot 
5262199Sroot 	/* take care of nice tab stops */
5272199Sroot 	if (*start == '\t') {
5282199Sroot 	    while (*start == '\t')
5292199Sroot 		start++;
5302199Sroot 	    i = tabs(_start, start) - margin / 8;
5312199Sroot 	    printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
5322199Sroot 	    continue;
5332199Sroot 	}
5342199Sroot 
5352199Sroot 	if (!nokeyw && !force)
5362199Sroot 	    if ((*start == '#' || isidchr(*start))
5372199Sroot 	    && (start == _start || !isidchr(start[-1]))) {
5382199Sroot 		i = iskw(start);
5392199Sroot 		if (i > 0) {
5402199Sroot 		    ps("\\*(+K");
5412199Sroot 		    do
5422199Sroot 			putcp(*start++);
5432199Sroot 		    while (--i > 0);
5442199Sroot 		    ps("\\*(-K");
5452199Sroot 		    continue;
5462199Sroot 		}
5472199Sroot 	    }
5482199Sroot 
5492199Sroot 	putcp (*start++);
5502199Sroot     }
5512199Sroot }
5522199Sroot 
5532199Sroot 
55456143Selan static int
tabs(s,os)5551841Sroot tabs(s, os)
5562199Sroot     char *s, *os;
5571841Sroot {
5581841Sroot 
5592199Sroot     return (width(s, os) / 8);
5601841Sroot }
5611841Sroot 
56256143Selan static int
width(s,os)5631841Sroot width(s, os)
5641841Sroot 	register char *s, *os;
5651841Sroot {
5661841Sroot 	register int i = 0;
5671841Sroot 
5681841Sroot 	while (s < os) {
5691841Sroot 		if (*s == '\t') {
5701841Sroot 			i = (i + 8) &~ 7;
5711841Sroot 			s++;
5721841Sroot 			continue;
5731841Sroot 		}
5741841Sroot 		if (*s < ' ')
5751841Sroot 			i += 2;
5761841Sroot 		else
5771841Sroot 			i++;
5781841Sroot 		s++;
5791841Sroot 	}
5801841Sroot 	return (i);
5811841Sroot }
5821841Sroot 
58356143Selan static void
putcp(c)5841841Sroot putcp(c)
5851841Sroot 	register int c;
5861841Sroot {
5871841Sroot 
5881841Sroot 	switch(c) {
5891841Sroot 
5902199Sroot 	case 0:
5912199Sroot 		break;
5922199Sroot 
5932199Sroot 	case '\f':
5942199Sroot 		break;
5952199Sroot 
5961841Sroot 	case '{':
5971841Sroot 		ps("\\*(+K{\\*(-K");
5981841Sroot 		break;
5991841Sroot 
6001841Sroot 	case '}':
6011841Sroot 		ps("\\*(+K}\\*(-K");
6021841Sroot 		break;
6031841Sroot 
6041841Sroot 	case '\\':
6051841Sroot 		ps("\\e");
6061841Sroot 		break;
6071841Sroot 
6081841Sroot 	case '_':
6091841Sroot 		ps("\\*_");
6101841Sroot 		break;
6111841Sroot 
6121841Sroot 	case '-':
6131841Sroot 		ps("\\*-");
6141841Sroot 		break;
6151841Sroot 
6161841Sroot 	case '`':
6171841Sroot 		ps("\\`");
6181841Sroot 		break;
6191841Sroot 
6201841Sroot 	case '\'':
6211841Sroot 		ps("\\'");
6221841Sroot 		break;
6231841Sroot 
6241841Sroot 	case '.':
6251841Sroot 		ps("\\&.");
6261841Sroot 		break;
6271841Sroot 
6282418Sroot 	case '*':
6292418Sroot 		ps("\\fI*\\fP");
6302418Sroot 		break;
6312418Sroot 
6322199Sroot 	case '/':
6332418Sroot 		ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
6342199Sroot 		break;
6352199Sroot 
6361841Sroot 	default:
6371841Sroot 		if (c < 040)
6381841Sroot 			putchar('^'), c |= '@';
6391841Sroot 	case '\t':
6401841Sroot 	case '\n':
6411841Sroot 		putchar(c);
6421841Sroot 	}
6431841Sroot }
6441841Sroot 
6452199Sroot /*
6462199Sroot  *	look for a process beginning on this line
6471878Sroot  */
64856143Selan static boolean
isproc(s)6492199Sroot isproc(s)
6502199Sroot     char *s;
6511878Sroot {
6522199Sroot     pname[0] = NULL;
6532199Sroot     if (!l_toplex || blklevel == 0)
6542199Sroot 	if (expmatch (s, l_prcbeg, pname) != NIL) {
6552199Sroot 	    return (TRUE);
6562199Sroot 	}
6572199Sroot     return (FALSE);
6581878Sroot }
6591878Sroot 
6602199Sroot 
6611878Sroot /*  iskw -	check to see if the next word is a keyword
6621878Sroot  */
6631878Sroot 
66456143Selan static int
iskw(s)6651841Sroot iskw(s)
6661841Sroot 	register char *s;
6671841Sroot {
6682199Sroot 	register char **ss = l_keywds;
6691841Sroot 	register int i = 1;
6701841Sroot 	register char *cp = s;
6711841Sroot 
6721841Sroot 	while (++cp, isidchr(*cp))
6731841Sroot 		i++;
6741841Sroot 	while (cp = *ss++)
6752199Sroot 		if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))
6761841Sroot 			return (i);
6771841Sroot 	return (0);
6781841Sroot }
67956143Selan 
680