#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)vfontedpr.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: vfontedpr.c,v 1.6 1997/10/20 03:01:27 lukem Exp $");
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "pathnames.h"
#include "extern.h"
#define FALSE 0
#define TRUE !(FALSE)
#define NIL 0
#define STANDARD 0
#define ALTERNATE 1
#define STRLEN 10
#define PNAMELEN 40
#define PSMAX 20
static int iskw __P((char *));
static boolean isproc __P((char *));
static void putKcp __P((char *, char *, boolean));
static void putScp __P((char *));
static void putcp __P((int));
static int tabs __P((char *, char *));
static int width __P((char *, char *));
static boolean filter = FALSE;
static boolean inchr;
static boolean incomm;
static boolean idx = FALSE;
static boolean instr;
static boolean nokeyw = FALSE;
static boolean pass = FALSE;
static int blklevel;
static int comtype;
static char *defsfile[2] = { _PATH_VGRINDEFS, 0 };
static int margin;
static int plstack[PSMAX];
static char pname[BUFSIZ+1];
static boolean prccont;
static int psptr;
static char pstack[PSMAX][PNAMELEN+1];
char *l_acmbeg;
char *l_acmend;
char *l_blkbeg;
char *l_blkend;
char *l_chrbeg;
char *l_chrend;
char *l_combeg;
char *l_comend;
char l_escape;
char *l_keywds[BUFSIZ/2];
char *l_prcbeg;
char *l_strbeg;
char *l_strend;
boolean l_toplex;
char *language = "c";
int main __P((int, char **));
#define ps(x) printf("%s", x)
int
main(argc, argv)
int argc;
char *argv[];
{
char *fname = "";
struct stat stbuf;
char buf[BUFSIZ];
char *defs;
int needbp = 0;
argc--, argv++;
do {
char *cp;
int i;
if (argc > 0) {
if (!strcmp(argv[0], "-h")) {
if (argc == 1) {
printf("'ds =H\n");
argc = 0;
goto rest;
}
printf("'ds =H %s\n", argv[1]);
argc--, argv++;
argc--, argv++;
if (argc > 0)
continue;
goto rest;
}
if (!strcmp(argv[0], "-f")) {
filter++;
argv[0] = argv[argc-1];
argv[argc-1] = "-";
continue;
}
if (!strcmp(argv[0], "-")) {
argc = 0;
goto rest;
}
if (!strcmp(argv[0], "-x")) {
idx++;
argv[0] = "-n";
}
if (!strcmp(argv[0], "-n")) {
nokeyw++;
argc--, argv++;
continue;
}
if (!strncmp(argv[0], "-s", 2)) {
i = 0;
cp = argv[0] + 2;
while (*cp)
i = i * 10 + (*cp++ - '0');
printf("'ps %d\n'vs %d\n", i, i+1);
argc--, argv++;
continue;
}
if (!strncmp(argv[0], "-l", 2)) {
language = argv[0]+2;
argc--, argv++;
continue;
}
if (!strncmp(argv[0], "-d", 2)) {
defsfile[0] = argv[1];
argc--, argv++;
argc--, argv++;
continue;
}
if (freopen(argv[0], "r", stdin) == NULL) {
perror(argv[0]);
exit(1);
}
if (idx)
printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
fname = argv[0];
argc--, argv++;
}
rest:
i = cgetent(&defs, defsfile, language);
if (i == -1) {
fprintf (stderr, "no entry for language %s\n", language);
exit (0);
} else if (i == -2) { fprintf(stderr,
"cannot find vgrindefs file %s\n", defsfile[0]);
exit (0);
} else if (i == -3) { fprintf(stderr,
"potential reference loop detected in vgrindefs file %s\n",
defsfile[0]);
exit(0);
}
if (cgetustr(defs, "kw", &cp) == -1)
nokeyw = TRUE;
else {
char **cpp;
cpp = l_keywds;
while (*cp) {
while (*cp == ' ' || *cp =='\t')
*cp++ = '\0';
if (*cp)
*cpp++ = cp;
while (*cp != ' ' && *cp != '\t' && *cp)
cp++;
}
*cpp = NIL;
}
cgetustr(defs, "pb", &cp);
l_prcbeg = convexp(cp);
cgetustr(defs, "cb", &cp);
l_combeg = convexp(cp);
cgetustr(defs, "ce", &cp);
l_comend = convexp(cp);
cgetustr(defs, "ab", &cp);
l_acmbeg = convexp(cp);
cgetustr(defs, "ae", &cp);
l_acmend = convexp(cp);
cgetustr(defs, "sb", &cp);
l_strbeg = convexp(cp);
cgetustr(defs, "se", &cp);
l_strend = convexp(cp);
cgetustr(defs, "bb", &cp);
l_blkbeg = convexp(cp);
cgetustr(defs, "be", &cp);
l_blkend = convexp(cp);
cgetustr(defs, "lb", &cp);
l_chrbeg = convexp(cp);
cgetustr(defs, "le", &cp);
l_chrend = convexp(cp);
l_escape = '\\';
l_onecase = (cgetcap(defs, "oc", ':') != NULL);
l_toplex = (cgetcap(defs, "tl", ':') != NULL);
incomm = FALSE;
instr = FALSE;
inchr = FALSE;
_escaped = FALSE;
blklevel = 0;
for (psptr=0; psptr<PSMAX; psptr++) {
pstack[psptr][0] = '\0';
plstack[psptr] = 0;
}
psptr = -1;
ps("'-F\n");
if (!filter) {
printf(".ds =F %s\n", fname);
ps("'wh 0 vH\n");
ps("'wh -1i vF\n");
}
if (needbp) {
needbp = 0;
printf(".()\n");
printf(".bp\n");
}
if (!filter) {
fstat(fileno(stdin), &stbuf);
cp = ctime((const time_t *)&stbuf.st_mtime);
cp[16] = '\0';
cp[24] = '\0';
printf(".ds =M %s %s\n", cp+4, cp+20);
}
while (fgets(buf, sizeof buf, stdin) != NULL) {
if (buf[0] == '\f') {
printf(".bp\n");
}
if (buf[0] == '.') {
printf("%s", buf);
if (!strncmp (buf+1, "vS", 2))
pass = TRUE;
if (!strncmp (buf+1, "vE", 2))
pass = FALSE;
continue;
}
prccont = FALSE;
if (!filter || pass)
putScp(buf);
else
printf("%s", buf);
if (prccont && (psptr >= 0)) {
ps("'FC ");
ps(pstack[psptr]);
ps("\n");
}
#ifdef DEBUG
printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
#endif
margin = 0;
}
needbp = 1;
} while (argc > 0);
exit(0);
}
#define isidchr(c) (isalnum(c) || (c) == '_')
static void
putScp(os)
char *os;
{
char *s = os;
char dummy[BUFSIZ];
char *comptr;
char *acmptr;
char *strptr;
char *chrptr;
char *blksptr;
char *blkeptr;
vf_start = os;
_escaped = FALSE;
if (nokeyw || incomm || instr)
goto skip;
if (isproc(s)) {
ps("'FN ");
ps(pname);
ps("\n");
if (psptr < PSMAX) {
++psptr;
strncpy (pstack[psptr], pname, PNAMELEN);
pstack[psptr][PNAMELEN] = '\0';
plstack[psptr] = blklevel;
}
}
skip:
do {
if (!incomm && !instr && !inchr) {
blkeptr = expmatch (s, l_blkend, dummy);
blksptr = expmatch (s, l_blkbeg, dummy);
comptr = expmatch (s, l_combeg, dummy);
acmptr = expmatch (s, l_acmbeg, dummy);
strptr = expmatch (s, l_strbeg, dummy);
chrptr = expmatch (s, l_chrbeg, dummy);
if (comptr != NIL)
if ((comptr < strptr || strptr == NIL)
&& (comptr < acmptr || acmptr == NIL)
&& (comptr < chrptr || chrptr == NIL)
&& (comptr < blksptr || blksptr == NIL)
&& (comptr < blkeptr || blkeptr == NIL)) {
putKcp (s, comptr-1, FALSE);
s = comptr;
incomm = TRUE;
comtype = STANDARD;
if (s != os)
ps ("\\c");
ps ("\\c\n'+C\n");
continue;
}
if (acmptr != NIL)
if ((acmptr < strptr || strptr == NIL)
&& (acmptr < chrptr || chrptr == NIL)
&& (acmptr < blksptr || blksptr == NIL)
&& (acmptr < blkeptr || blkeptr == NIL)) {
putKcp (s, acmptr-1, FALSE);
s = acmptr;
incomm = TRUE;
comtype = ALTERNATE;
if (s != os)
ps ("\\c");
ps ("\\c\n'+C\n");
continue;
}
if (strptr != NIL)
if ((strptr < chrptr || chrptr == NIL)
&& (strptr < blksptr || blksptr == NIL)
&& (strptr < blkeptr || blkeptr == NIL)) {
putKcp (s, strptr-1, FALSE);
s = strptr;
instr = TRUE;
continue;
}
if (chrptr != NIL)
if ((chrptr < blksptr || blksptr == NIL)
&& (chrptr < blkeptr || blkeptr == NIL)) {
putKcp (s, chrptr-1, FALSE);
s = chrptr;
inchr = TRUE;
continue;
}
if (blkeptr != NIL) {
if (blkeptr < blksptr || blksptr == NIL) {
putKcp (s, blkeptr - 1, FALSE);
s = blkeptr;
blklevel--;
if (psptr >= 0 && plstack[psptr] >= blklevel) {
if (s != os)
ps ("\\c");
ps ("\\c\n'-F\n");
blklevel = plstack[psptr];
if (--psptr >= 0)
prccont = TRUE;
else
psptr = -1;
}
continue;
}
}
if (blksptr != NIL) {
putKcp (s, blksptr - 1, FALSE);
s = blksptr;
blklevel++;
continue;
}
} else if (incomm) {
comptr = expmatch (s, l_comend, dummy);
acmptr = expmatch (s, l_acmend, dummy);
if (((comtype == STANDARD) && (comptr != NIL)) ||
((comtype == ALTERNATE) && (acmptr != NIL))) {
if (comtype == STANDARD) {
putKcp (s, comptr-1, TRUE);
s = comptr;
} else {
putKcp (s, acmptr-1, TRUE);
s = acmptr;
}
incomm = FALSE;
ps("\\c\n'-C\n");
continue;
} else {
putKcp (s, s + strlen(s) -1, TRUE);
s = s + strlen(s);
continue;
}
} else if (instr) {
if ((strptr = expmatch (s, l_strend, dummy)) != NIL) {
putKcp (s, strptr-1, TRUE);
s = strptr;
instr = FALSE;
continue;
} else {
putKcp (s, s+strlen(s)-1, TRUE);
s = s + strlen(s);
continue;
}
} else if (inchr) {
if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) {
putKcp (s, chrptr-1, TRUE);
s = chrptr;
inchr = FALSE;
continue;
} else {
putKcp (s, s+strlen(s)-1, TRUE);
s = s + strlen(s);
continue;
}
}
putKcp (s, s + strlen(s) -1, FALSE);
s = s + strlen(s);
} while (*s);
}
static void
putKcp (start, end, force)
char *start;
char *end;
boolean force;
{
int i;
int xfld = 0;
while (start <= end) {
if (idx) {
if (*start == ' ' || *start == '\t') {
if (xfld == 0)
printf("");
printf("\t");
xfld = 1;
while (*start == ' ' || *start == '\t')
start++;
continue;
}
}
if (*start == '\t') {
while (*start == '\t')
start++;
i = tabs(vf_start, start) - margin / 8;
printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
continue;
}
if (!nokeyw && !force)
if ((*start == '#' || isidchr(*start))
&& (start == vf_start || !isidchr(start[-1]))) {
i = iskw(start);
if (i > 0) {
ps("\\*(+K");
do
putcp(*start++);
while (--i > 0);
ps("\\*(-K");
continue;
}
}
putcp (*start++);
}
}
static int
tabs(s, os)
char *s, *os;
{
return (width(s, os) / 8);
}
static int
width(s, os)
char *s, *os;
{
int i = 0;
while (s < os) {
if (*s == '\t') {
i = (i + 8) &~ 7;
s++;
continue;
}
if (*s < ' ')
i += 2;
else
i++;
s++;
}
return (i);
}
static void
putcp(c)
int c;
{
switch(c) {
case 0:
break;
case '\f':
break;
case '{':
ps("\\*(+K{\\*(-K");
break;
case '}':
ps("\\*(+K}\\*(-K");
break;
case '\\':
ps("\\e");
break;
case '_':
ps("\\*_");
break;
case '-':
ps("\\*-");
break;
case '`':
ps("\\`");
break;
case '\'':
ps("\\'");
break;
case '.':
ps("\\&.");
break;
case '*':
ps("\\fI*\\fP");
break;
case '/':
ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
break;
default:
if (c < 040)
putchar('^'), c |= '@';
case '\t':
case '\n':
putchar(c);
}
}
static boolean
isproc(s)
char *s;
{
pname[0] = '\0';
if (!l_toplex || blklevel == 0)
if (expmatch (s, l_prcbeg, pname) != NIL) {
return (TRUE);
}
return (FALSE);
}
static int
iskw(s)
char *s;
{
char **ss = l_keywds;
int i = 1;
char *cp = s;
while (++cp, isidchr(*cp))
i++;
while ((cp = *ss++) != NULL)
if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))
return (i);
return (0);
}