#include <ctype.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <locale.h>
#include <euc.h>
#include <stdlib.h>
#define boolean int
#define TRUE 1
#define FALSE 0
#define NIL 0
#define STANDARD 0
#define ALTERNATE 1
#define STRLEN 10
#define PNAMELEN 40
#define PSMAX 20
char *expmatch();
char *STRNCMP();
char *convexp();
char *tgetstr();
boolean isproc();
char *ctime();
char *strchr();
boolean incomm;
boolean instr;
boolean inchr;
boolean nokeyw = FALSE;
boolean doindex = FALSE;
boolean twocol = FALSE;
boolean filter = FALSE;
boolean pass = FALSE;
boolean prccont;
int comtype;
int margin;
int psptr;
char pstack[PSMAX][PNAMELEN+1];
int plstack[PSMAX];
int blklevel;
int prclevel;
char *defsfile = "/usr/lib/vgrindefs";
char pname[BUFSIZ+1];
char *language = "c";
char *l_keywds[BUFSIZ/2];
char *l_prcbeg;
char *l_combeg;
char *l_comend;
char *l_acmbeg;
char *l_acmend;
char *l_blkbeg;
char *l_blkend;
char *l_strbeg;
char *l_strend;
char *l_chrbeg;
char *l_chrend;
char *l_prcenable;
char l_escape;
boolean l_toplex;
boolean l_prclevel;
short tabsize = 8;
boolean _escaped;
char *Start;
boolean l_onecase;
char *l_idchars;
static void putcp(int c);
static int width(char *s, char *os);
static int tabs(char *s, char *os);
static void putKcp(char *start, char *end, boolean force);
static void putScp(char *os);
static int iskw(char *s);
int
main(int argc, char *argv[])
{
FILE *in;
char *fname;
struct stat stbuf;
char buf[BUFSIZ];
char idbuf[256];
char strings[2 * BUFSIZ];
char defs[2 * BUFSIZ];
int needbp = 0;
int i;
char *cp;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
argc--, argv++;
while (argc > 0 && *argv[0] == '-') {
switch (*(cp = argv[0] + 1)) {
case '\0':
goto flagsdone;
case '2':
twocol = 1;
printf("'nr =2 1\n");
break;
case 'd':
defsfile = argv[1];
argc--, argv++;
break;
case 'f':
filter = 1;
for (i = 0; i < argc - 1; i++)
argv[i] = argv[i + 1];
argv[argc - 1] = "-";
continue;
case 'h':
if (argc == 1) {
printf("'ds =H\n");
break;
}
printf("'ds =H %s\n", argv[1]);
argc--, argv++;
break;
case 'l':
language = cp + 1;
break;
case 'n':
nokeyw = 1;
break;
case 's':
i = 0;
cp++;
while (*cp)
i = i * 10 + (*cp++ - '0');
printf("'nr vP %d\n", i);
break;
case 't':
tabsize = 4;
break;
case 'x':
doindex = 1;
argv[0] = "-n";
continue;
}
argc--, argv++;
}
flagsdone:
i = tgetent (defs, language, defsfile);
if (i == 0) {
fprintf (stderr, gettext("no entry for language %s\n"), language);
exit (0);
} else if (i < 0) {
fprintf (stderr, gettext("cannot find vgrindefs file %s\n"), defsfile);
exit (0);
}
cp = strings;
if (tgetstr ("kw", &cp) == NIL)
nokeyw = TRUE;
else {
char **cpp;
cpp = l_keywds;
cp = strings;
while (*cp) {
while (*cp == ' ' || *cp =='\t')
*cp++ = '\0';
if (*cp)
*cpp++ = cp;
while (*cp != ' ' && *cp != '\t' && *cp)
cp++;
}
*cpp = NIL;
}
cp = buf;
l_prcbeg = convexp (tgetstr ("pb", &cp));
cp = buf;
l_combeg = convexp (tgetstr ("cb", &cp));
cp = buf;
l_comend = convexp (tgetstr ("ce", &cp));
cp = buf;
l_acmbeg = convexp (tgetstr ("ab", &cp));
cp = buf;
l_acmend = convexp (tgetstr ("ae", &cp));
cp = buf;
l_strbeg = convexp (tgetstr ("sb", &cp));
cp = buf;
l_strend = convexp (tgetstr ("se", &cp));
cp = buf;
l_blkbeg = convexp (tgetstr ("bb", &cp));
cp = buf;
l_blkend = convexp (tgetstr ("be", &cp));
cp = buf;
l_chrbeg = convexp (tgetstr ("lb", &cp));
cp = buf;
l_chrend = convexp (tgetstr ("le", &cp));
cp = buf;
l_prcenable = convexp (tgetstr ("px", &cp));
cp = idbuf;
l_idchars = tgetstr ("id", &cp);
if (l_idchars == NIL)
l_idchars = "_";
l_escape = '\\';
l_onecase = tgetflag ("oc");
l_toplex = tgetflag ("tl");
l_prclevel = tgetflag ("pl");
printf("'vI\n");
if (!filter)
printf("'vS\n");
if (doindex) {
if (twocol)
printf("'ta 2.5i 2.75i 4.0iR\n'in .25i\n");
else
printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
}
while (argc > 0) {
if (strcmp(argv[0], "-") == 0) {
in = fdopen(fileno(stdin), "r");
fname = "";
} else {
if ((in = fopen(argv[0], "r")) == NULL) {
perror(argv[0]);
exit(1);
}
fname = argv[0];
}
argc--, argv++;
incomm = FALSE;
instr = FALSE;
inchr = FALSE;
_escaped = FALSE;
blklevel = 0;
prclevel = -1;
for (psptr=0; psptr<PSMAX; psptr++) {
pstack[psptr][0] = '\0';
plstack[psptr] = 0;
}
psptr = -1;
printf("'-F\n");
if (!filter) {
char *cp;
printf(".ds =F %s\n", fname);
if (needbp) {
needbp = 0;
printf(".()\n");
printf(".bp\n");
}
fstat(fileno(in), &stbuf);
cp = ctime(&stbuf.st_mtime);
cp[16] = '\0';
cp[24] = '\0';
printf(".ds =M %s %s\n", cp+4, cp+20);
printf("'wh 0 vH\n");
printf("'wh -1i vF\n");
}
if (needbp && filter) {
needbp = 0;
printf(".()\n");
printf(".bp\n");
}
while (fgets(buf, sizeof buf, in) != 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))
printf("'FC %s\n", pstack[psptr]);
#ifdef DEBUG
printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
#endif
margin = 0;
}
needbp = 1;
(void) fclose(in);
}
if (!filter)
printf("'vE\n");
return (0);
}
#define isidchr(c) (isalnum(c) || ((c) != NIL && strchr(l_idchars, (c)) != NIL))
static void
putScp(char *os)
{
char *s = os;
char dummy[BUFSIZ];
char *comptr;
char *acmptr;
char *strptr;
char *chrptr;
char *blksptr;
char *blkeptr;
Start = os;
_escaped = FALSE;
if (nokeyw || incomm || instr)
goto skip;
if (isproc(s)) {
printf("'FN %s\n", pname);
if (psptr < PSMAX-1) {
++psptr;
strncpy (pstack[psptr], pname, PNAMELEN);
pstack[psptr][PNAMELEN] = '\0';
plstack[psptr] = blklevel;
}
}
if (l_prclevel && !incomm && !instr && !inchr) {
if (expmatch (s, l_prcenable, dummy) != NIL)
prclevel = blklevel + 1;
}
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)
printf ("\\c");
printf ("\\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)
printf ("\\c");
printf ("\\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) {
if (l_prclevel && prclevel == blklevel)
prclevel = -1;
putKcp (s, blkeptr - 1, FALSE);
s = blkeptr;
blklevel--;
if (psptr >= 0 && plstack[psptr] >= blklevel) {
if (s != os)
printf ("\\c");
printf ("\\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;
printf("\\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(char *start, char *end, boolean force)
{
int i;
int xfld = 0;
while (start <= end) {
if (doindex) {
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(Start, start) - margin / tabsize;
printf ("\\h'|%dn'",
i * (tabsize == 4 ? 5 : 10) + 1 - margin % tabsize);
continue;
}
if (!nokeyw && !force)
if ( (*start == '#' || isidchr(*start))
&& (start == Start || !isidchr(start[-1]))
) {
i = iskw(start);
if (i > 0) {
printf("\\*(+K");
do
putcp(*start++);
while (--i > 0);
printf("\\*(-K");
continue;
}
}
putcp (*start++);
}
}
static int
tabs(char *s, char *os)
{
return (width(s, os) / tabsize);
}
static int
width(char *s, char *os)
{
int i = 0;
unsigned char c;
int n;
while (s < os) {
if (*s == '\t') {
i = (i + tabsize) &~ (tabsize-1);
s++;
continue;
}
c = *(unsigned char *)s;
if (c < ' ')
i += 2, s++;
else if (c >= 0200) {
if ((n = mblen(s, MB_CUR_MAX)) > 0) {
i += csetcol(csetno(c));
s += n;
} else
s++;
} else
i++, s++;
}
return (i);
}
static void
putcp(int c)
{
switch(c) {
case 0:
break;
case '\f':
break;
case '{':
printf("\\*(+K{\\*(-K");
break;
case '}':
printf("\\*(+K}\\*(-K");
break;
case '\\':
printf("\\e");
break;
case '_':
printf("\\*_");
break;
case '-':
printf("\\*-");
break;
case '`':
if (incomm)
printf("`");
else
printf("\\`");
break;
case '\'':
if (incomm)
printf("'");
else
printf("\\'");
break;
case '.':
printf("\\&.");
break;
case '*':
if (instr || inchr)
printf("*");
else
printf("\\f2*\\fP");
break;
case '/':
if (instr || inchr)
printf("/");
else
printf("\\f2\\h'\\w' 'u-\\w'/'u'/\\fP");
break;
default:
if (c < 040)
putchar('^'), c |= '@';
case '\t':
case '\n':
putchar(c);
}
}
boolean
isproc(char *s)
{
pname[0] = '\0';
if (l_prclevel ? (prclevel == blklevel) : (!l_toplex || blklevel == 0))
if (expmatch (s, l_prcbeg, pname) != NIL) {
return (TRUE);
}
return (FALSE);
}
static int
iskw(char *s)
{
char **ss = l_keywds;
int i = 1;
char *cp = s;
while (++cp, isidchr(*cp))
i++;
while (cp = *ss++) {
if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))
return (i);
}
return (0);
}