date and time created 82/10/19 00:52:08 by mckusick
[unix-history] / usr / src / usr.bin / vgrind / vfontedpr.c
index a29c7b0..0b69dd9 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#define boolean int
+#define TRUE 1
+#define FALSE 0
+#define NIL 0
+#define STANDARD 0
+#define ALTERNATE 1
+
 /*
  * Vfontedpr.
  *
 /*
  * Vfontedpr.
  *
- * Bill Joy, Apr. 1979.
- * (made table driven - Dave Presotto 11/17/80)
+ * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
  *
  */
  *
  */
-#define NOLANG -1              /* indicates no language chosen */
-#define C 0
-#define PASCAL 1
-#define MODEL 2
-#define NLANG 3                        /* total number of languages */
 
 #define STRLEN 10              /* length of strings introducing things */
 #define PNAMELEN 40            /* length of a function/procedure name */
 
 #define STRLEN 10              /* length of strings introducing things */
 #define PNAMELEN 40            /* length of a function/procedure name */
+#define PSMAX 20               /* size of procedure name stacking */
 
 
-/* routines used by the different languages */
-
-int    cprbegin();
-int    cprend();
-int    pprbegin();
-int    pprend();
-int    mprbegin();
-int    mprend();
-
-/* keywords for the model language */
-
-char   *mkw[] = {
-       "abs",
-       "and",
-       "array",
-       "beginproc",
-       "boolean",
-       "by",
-       "case",
-       "cdnl",
-       "char",
-       "copied",
-       "dispose",
-       "div",
-       "do",
-       "dynamic",
-       "else",
-       "elsif",
-       "end",
-       "endproc",
-       "entry",
-       "external",
-       "f",
-       "FALSE",
-       "false",
-       "fi",
-       "file",
-       "for",
-       "formal",
-       "fortran",
-       "global",
-       "if",
-       "in",
-       "include",
-       "inline",
-       "is",
-       "lbnd",
-       "max",
-       "min",
-       "mod",
-       "new",
-       "NIL",
-       "nil",
-       "noresult",
-       "not",
-       "notin",
-       "od",
-       "of",
-       "or",
-       "procedure",
-       "public",
-       "read",
-       "readln",
-       "readonly",
-       "record",
-       "recursive",
-       "rem",
-       "rep",
-       "repeat",
-       "result",
-       "return",
-       "set",
-       "space",
-       "string",
-       "subscript",
-       "such",
-       "then",
-       "TRUE",
-       "true",
-       "type",
-       "ubnd",
-       "union",
-       "until",
-       "varies",
-       "while",
-       "width",
-       "write",
-       "writeln",
-       0,
-};
-
-/* keywords for the pascal language */
-
-char   *pkw[] = {
-       "and",
-       "array",
-       "assert",
-       "begin",
-       "case",
-       "const",
-       "div",
-       "do",
-       "downto",
-       "else",
-       "end",
-       "file",
-       "for",
-       "forward",
-       "function",
-       "goto",
-       "if",
-       "in",
-       "label",
-       "mod",
-       "nil",
-       "not",
-       "of",
-       "or",
-       "packed",
-       "procedure",
-       "program",
-       "record",
-       "repeat",
-       "set",
-       "then",
-       "to",
-       "type",
-       "until",
-       "var",
-       "while",
-       "with",
-       "oct",
-       "hex",
-       "external",
-       0,
-};
-
-/* keywords for the C language */
-
-char   *ckw[] = {
-       "asm",
-       "auto",
-       "break",
-       "case",
-       "char",
-       "continue",
-       "default",
-       "do",
-       "double",
-       "else",
-       "enum",
-       "extern",
-       "float",
-       "for",
-       "fortran",
-       "goto",
-       "if",
-       "int",
-       "long",
-       "register",
-       "return",
-       "short",
-       "sizeof",
-       "static",
-       "struct",
-       "switch",
-       "typedef",
-       "union",
-       "unsigned",
-       "while",
-       "#define",
-       "#else",
-       "#endif",
-       "#if",
-       "#ifdef",
-       "#ifndef",
-       "#include",
-       "#undef",
-       "#",
-       "define",
-       "else",
-       "endif",
-       "if",
-       "ifdef",
-       "ifndef",
-       "include",
-       "undef",
-       0,
-};
+/* regular expression routines */
 
 
-/*
- * the following structure defines a language
- */
+char   *expmatch();            /* match a string to an expression */
+char   *STRNCMP();             /* a different kindof strncmp */
+char   *convexp();             /* convert expression to internal form */
+
+boolean        isproc();
 
 
-struct langdef {
-       char    *option;        /* its option switch */
-       char    **kwd;          /* address of its keyword table */
-       int     (*isproc)();    /* test for procedure begin */
-       int     (*ispend)();    /* test for procedure end */
-       char    *combeg;        /* string introducing a comment */
-       char    *comend;        /* string ending a comment */
-       char    *comout;        /* string output in place of combeg string */
-       char    strdel;         /* delimiter for string constant */
-       char    chrdel;         /* delimiter for character constant */
-       int     onelncom;       /* comments do not continue on next line */
-       int     onelnstr;       /* string constants do not continue on next */
-};
-
-struct langdef ld[] = {
-       /* the C language */
-       "-c",
-       ckw,
-       cprbegin,
-       cprend,
-       "/*",
-       "*/",
-       "\\*(/*",
-       '"',
-       '\'',
-       0,
-       0,
-
-       /* the pascal language */
-       "-p",
-       pkw,
-       pprbegin,
-       pprend,
-       "{",
-       "}",
-       "{",
-       '\'',
-       0,
-       0,
-       0,
-
-       /* the model language */
-       "-m",
-       mkw,
-       mprbegin,
-       mprend,
-       "$",
-       "$",
-       "$",
-       '"',
-       '\'',
-       1,
-       0
-};
 
 char   *ctime();
 
 char   *ctime();
-int    incomm;
-int    instr;
-int    nokeyw;
-int    index;
+
+/*
+ *     The state variables
+ */
+
+boolean        incomm;                 /* in a comment of the primary type */
+boolean        instr;                  /* in a string constant */
+boolean        inchr;                  /* in a string constant */
+boolean        nokeyw = FALSE;         /* no keywords being flagged */
+boolean        index = FALSE;          /* form an index */
+boolean filter = FALSE;                /* act as a filter (like eqn) */
+boolean pass = FALSE;          /* when acting as a filter, pass indicates
+                                * whether we are currently processing
+                                * input.
+                                */
+boolean prccont;               /* continue last procedure */
+int    comtype;                /* type of comment */
 int    margin;
 int    margin;
-char   *comcol;                /* character position comment starts in */
-int    language = NOLANG;      /* the language indicator */
-char   **keywds;               /* keyword table address */
-int    (*isprbeg)();           /* test for beginning of procedure */
-int    (*isprend)();           /* test for end of procedure */
-char   *cstart;                /* start of comment string */
-char   *cstop;                 /* end of comment string */
-char   *cout;                  /* string to substitute for cstart */
-int    lcstart;                /* length of comment string starter */
-int    lcstop;                 /* length of comment string terminator */
-char   sdelim;                 /* string constant delimiter */
-char   cdelim;                 /* character constant delimiter */
-int    com1line;               /* one line comments */
-int    str1line;               /* one line strings */
-char   pname[PNAMELEN];
+int    psptr;                  /* the stack index of the current procedure */
+char   pstack[PSMAX][PNAMELEN+1];      /* the procedure name stack */
+int    plstack[PSMAX];         /* the procedure nesting level stack */
+int    blklevel;               /* current nesting level */
+char   *defsfile = "/usr/lib/vgrindefs";       /* name of language definitions file */
+char   pname[BUFSIZ+1];
+
+/*
+ *     The language specific globals
+ */
+
+char   *language = "c";        /* the language indicator */
+char   *l_keywds[BUFSIZ/2];    /* keyword table address */
+char   *l_prcbeg;              /* regular expr for procedure begin */
+char   *l_combeg;              /* string introducing a comment */
+char   *l_comend;              /* string ending a comment */
+char   *l_acmbeg;              /* string introducing a comment */
+char   *l_acmend;              /* string ending a comment */
+char   *l_blkbeg;              /* string begining of a block */
+char   *l_blkend;              /* string ending a block */
+char    *l_strbeg;             /* delimiter for string constant */
+char    *l_strend;             /* delimiter for string constant */
+char    *l_chrbeg;             /* delimiter for character constant */
+char    *l_chrend;             /* delimiter for character constant */
+char   l_escape;               /* character used to  escape characters */
+boolean        l_toplex;               /* procedures only defined at top lex level */
+
+/*
+ *  global variables also used by expmatch
+ */
+boolean _escaped;              /* if last character was an escape */
+char *_start;                  /* start of the current string */
+boolean        l_onecase;              /* upper and lower case are equivalent */
 
 #define        ps(x)   printf("%s", x)
 
 main(argc, argv)
 
 #define        ps(x)   printf("%s", x)
 
 main(argc, argv)
-       int argc;
-       char *argv[];
+    int argc;
+    char *argv[];
 {
 {
-       int lineno;
-       char *fname = "";
-       struct stat stbuf;
-       char buf[BUFSIZ];
-       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 -= 2;
-                               argv += 2;
-                               if (argc > 0)
-                                       continue;
-                               goto rest;
-                       }
-                       if (!strcmp(argv[0], "-x")) {
-                               index++;
-                               argv[0] = "-n";
-                       }
-                       if (!strcmp(argv[0], "-n")) {
-                               nokeyw++;
-                               argc--, argv++;
-                               continue;
-                       }
-                       for (i = 0; i < NLANG; i++)
-                               if (!strcmp(argv[0], ld[i].option)) {
-                                       language = i;
-                                       break;
-                               }
-                       if (i != NLANG) {
-                               argc--, argv++;
-                               continue;
-                       }
-                       if (freopen(argv[0], "r", stdin) == NULL) {
-                               perror(argv[0]);
-                               exit(1);
-                       }
-                       if (index)
-                               printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
-                       fname = argv[0];
-                       argc--, argv++;
-               }
-rest:
-               if (language == NOLANG)
-                       language = C;           /* C is the default */
-
-               /* initialize for the appropriate language */
-
-               keywds = ld[language].kwd;
-               isprbeg = ld[language].isproc;
-               isprend = ld[language].ispend;
-               cstart = ld[language].combeg;
-               cstop = ld[language].comend;
-               cout = ld[language].comout;
-               lcstart = strlen (cstart);
-               lcstop = strlen (cstop);
-               sdelim = ld[language].strdel;
-               cdelim = ld[language].chrdel;
-               com1line = ld[language].onelncom;
-               str1line = ld[language].onelnstr;
-               pname[0] = 0;
-
-               /* initialize the program */
-
-               incomm = 0;
-               instr = 0;
-               printf(".ds =F %s\n", fname);
-               fstat(fileno(stdin), &stbuf);
-               cp = ctime(&stbuf.st_mtime);
-               cp[16] = '\0';
-               cp[24] = '\0';
-               printf(".ds =M %s %s\n", cp+4, cp+20);
-               if (needbp) {
-                       needbp = 0;
-                       printf(".()\n");
-                       printf(".bp\n");
-               }
-               while (fgets(buf, sizeof buf, stdin) != NULL) {
-                       if (buf[0] == '\f' && buf[1] == '\n') {
-                               printf(".bp\n");
-                               continue;
-                       }
-                       if (com1line && incomm) {
-                               incomm = 0;
-                               ps("\\c\n'-C\n");
-                       }
-                       if (str1line)
-                               instr = 0;
-                       comcol = NULL;
-                       putScp(buf);
-                       if (buf[strlen(buf) - 2] != '\\')
-                               instr = 0;
-                       margin = 0;
+    int lineno;
+    char *fname = "";
+    char *ptr;
+    struct stat stbuf;
+    char buf[BUFSIZ];
+    char strings[2 * BUFSIZ];
+    char defs[2 * BUFSIZ];
+    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;
                }
                }
-               needbp = 1;
-       } while (argc > 0);
-       exit(0);
+               printf("'ds =H %s\n", argv[1]);
+               argc--, argv++;
+               argc--, argv++;
+               if (argc > 0)
+                   continue;
+               goto rest;
+           }
+
+           /* act as a filter like eqn */
+           if (!strcmp(argv[0], "-f")) {
+               filter++;
+               argv[0] = argv[argc-1];
+               argv[argc-1] = "-";
+               continue;
+           }
+
+           /* take input from the standard place */
+           if (!strcmp(argv[0], "-")) {
+               argc = 0;
+               goto rest;
+           }
+
+           /* build an index */
+           if (!strcmp(argv[0], "-x")) {
+               index++;
+               argv[0] = "-n";
+           }
+
+           /* indicate no keywords */
+           if (!strcmp(argv[0], "-n")) {
+               nokeyw++;
+               argc--, argv++;
+               continue;
+           }
+
+           /* specify the font size */
+           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;
+           }
+
+           /* specify the language */
+           if (!strncmp(argv[0], "-l", 2)) {
+               language = argv[0]+2;
+               argc--, argv++;
+               continue;
+           }
+
+           /* specify the language description file */
+           if (!strncmp(argv[0], "-d", 2)) {
+               defsfile = argv[1];
+               argc--, argv++;
+               argc--, argv++;
+               continue;
+           }
+
+           /* open the file for input */
+           if (freopen(argv[0], "r", stdin) == NULL) {
+               perror(argv[0]);
+               exit(1);
+           }
+           if (index)
+               printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
+           fname = argv[0];
+           argc--, argv++;
+       }
+    rest:
+
+       /*
+        *  get the  language definition from the defs file
+        */
+       i = tgetent (defs, language, defsfile);
+       if (i == 0) {
+           fprintf (stderr, "no entry for language %s\n", language);
+           exit (0);
+       } else  if (i < 0) {
+           fprintf (stderr,  "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++ = NULL;
+               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));
+       l_escape = '\\';
+       l_onecase = tgetflag ("oc");
+       l_toplex = tgetflag ("tl");
+
+       /* initialize the program */
+
+       incomm = FALSE;
+       instr = FALSE;
+       inchr = FALSE;
+       _escaped = FALSE;
+       blklevel = 0;
+       for (psptr=0; psptr<PSMAX; psptr++) {
+           pstack[psptr][0] = NULL;
+           plstack[psptr] = 0;
+       }
+       psptr = -1;
+       ps("'-F\n");
+       if (!filter) {
+           printf(".ds =F %s\n", fname);
+           fstat(fileno(stdin), &stbuf);
+           cp = ctime(&stbuf.st_mtime);
+           cp[16] = '\0';
+           cp[24] = '\0';
+           printf(".ds =M %s %s\n", cp+4, cp+20);
+           ps("'wh 0 vH\n");
+           ps("'wh -1i vF\n");
+       }
+       if (needbp) {
+           needbp = 0;
+           printf(".()\n");
+           printf(".bp\n");
+       }
+
+       /*
+        *      MAIN LOOP!!!
+        */
+       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) == '_')
 
 putScp(os)
 }
 
 #define isidchr(c) (isalnum(c) || (c) == '_')
 
 putScp(os)
-       char *os;
+    char *os;
 {
 {
-       register char *s = os;
-       register int i;
-       int xfld = 0;
-
-       if (nokeyw || incomm || instr)
-               goto skip;
-       if ((*isprbeg)(s)) {
-               ps("'FN ");
-               ps(pname);
-               ps("\n");
-       } else if ((*isprend)(s))
-               ps("'-F\n");
+    register char *s = os;             /* pointer to unmatched string */
+    char dummy[BUFSIZ];                        /* dummy to be used by expmatch */
+    char *comptr;                      /* end of a comment delimiter */
+    char *acmptr;                      /* end of a comment delimiter */
+    char *strptr;                      /* end of a string delimiter */
+    char *chrptr;                      /* end of a character const delimiter */
+    char *blksptr;                     /* end of a lexical block start */
+    char *blkeptr;                     /* end of a lexical block end */
+
+    _start = os;                       /* remember the start for expmatch */
+    _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] = NULL;
+           plstack[psptr] = blklevel;
+       }
+    } 
 skip:
 skip:
-       while (*s) {
-               if (*s == '\f') {
-                       ps("\n.bp\n");
-                       s++;
-                       continue;
+    do {
+       /* check for string, comment, blockstart, etc */
+       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);
+
+           /* start of a comment? */
+           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 (index) {
-                       if (*s == ' ' || *s == '\t') {
-                               if (xfld == 0)  
-                                       printf("\ 1");
-                               printf("\t");
-                               xfld = 1;
-                               while (*s == ' ' || *s == '\t')
-                                       s++;
-                               continue;
-                       }
-               }
-               if (!nokeyw && !incomm && *s == sdelim) {
-                       if (instr) {
-                               if (s[-1] != '\\')
-                                       instr = 0;
-                       } else
-                               if (s[-1] != cdelim)
-                                       instr = 1;
+
+           /* start of a comment? */
+           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 (incomm && comcol != s-1 && s - os >= lcstop && !strncmp(cstop, s - lcstop, lcstop)) {
-                       incomm = 0;
-                       ps("\\c\n'-C\n");
-               } else if (!instr && !nokeyw && !incomm && !strncmp(cstart, s, lcstart)) {
-                       comcol = s;
-                       incomm = 1;
-                       if (s != os)
-                               ps("\\c");
-                       ps("\\c\n'+C\n");
-                       margin = width(os, s);
-                       ps(cout);
-                       s += lcstart;
-                       continue;
+
+           /* start of a string? */
+           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 (*s == '\t') {
-                       while (*s == '\t')
-                               s++;
-                       i = tabs(os, s) - margin / 8;
-                       printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
-                       continue;
+
+           /* start of a character string? */
+           if (chrptr != NIL)
+               if ((chrptr < blksptr || blksptr == NIL)
+                 && (chrptr < blkeptr || blkeptr == NIL)) {
+                   putKcp (s, chrptr-1, FALSE);
+                   s = chrptr;
+                   inchr = TRUE;
+                   continue;
                }
                }
-/*
-               if (*s == '-' && s[1] == '>') {
-                       s += 2;
-                       ps("\\(->");
-                       continue;
+
+           /* end of a lexical block */
+           if (blkeptr != NIL) {
+               if (blkeptr < blksptr || blksptr == NIL) {
+                   putKcp (s, blkeptr - 1, FALSE);
+                   s = blkeptr;
+                   blklevel--;
+                   if (psptr >= 0 && plstack[psptr] >= blklevel) {
+
+                       /* end of current procedure */
+                       if (s != os)
+                           ps ("\\c");
+                       ps ("\\c\n'-F\n");
+                       blklevel = plstack[psptr];
+
+                       /* see if we should print the last proc name */
+                       if (--psptr >= 0)
+                           prccont = TRUE;
+                       else
+                           psptr = -1;
+                   }
+                   continue;
                }
                }
-*/
-               if (!incomm && !nokeyw && !instr && (*s == '#' || isalpha(*s)) && (s == os || !isidchr(s[-1]))) {
-                       i = iskw(s);
-                       if (i > 0) {
-                               ps("\\*(+K");
-                               do 
-                                       putcp(*s++);
-                               while (--i > 0);
-                               ps("\\*(-K");
-                               continue;
-                       }
+           }
+
+           /* start of a lexical block */
+           if (blksptr != NIL) {
+               putKcp (s, blksptr - 1, FALSE);
+               s = blksptr;
+               blklevel++;
+               continue;
+           }
+
+       /* check for end of comment */
+       } 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;
                }
                }
-               putcp(*s++);
+               incomm = FALSE;
+               ps("\\c\n'-C\n");
+               continue;
+           } else {
+               putKcp (s, s + strlen(s) -1);
+               s = s + strlen(s);
+               continue;
+           }
+
+       /* check for end of string */
+       } 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;
+           }
+
+       /* check for end of character string */
+       } 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;
+           }
        }
        }
+
+       /* print out the line */
+       putKcp (s, s + strlen(s) -1, FALSE);
+       s = s + strlen(s);
+    } while (*s);
+}
+
+putKcp (start, end, force)
+    char       *start;         /* start of string to write */
+    char       *end;           /* end of string to write */
+    boolean    force;          /* true if we should force nokeyw */
+{
+    int i;
+    int xfld = 0;
+
+    while (start <= end) {
+       if (index) {
+           if (*start == ' ' || *start == '\t') {
+               if (xfld == 0)  
+                   printf("\ 1");
+               printf("\t");
+               xfld = 1;
+               while (*start == ' ' || *start == '\t')
+                   start++;
+               continue;
+           }
+       }
+
+       /* take care of nice tab stops */
+       if (*start == '\t') {
+           while (*start == '\t')
+               start++;
+           i = tabs(_start, start) - margin / 8;
+           printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
+           continue;
+       }
+
+       if (!nokeyw && !force)
+           if ((*start == '#' || isidchr(*start)) 
+           && (start == _start || !isidchr(start[-1]))) {
+               i = iskw(start);
+               if (i > 0) {
+                   ps("\\*(+K");
+                   do 
+                       putcp(*start++);
+                   while (--i > 0);
+                   ps("\\*(-K");
+                   continue;
+               }
+           }
+
+       putcp (*start++);
+    }
 }
 
 }
 
+
 tabs(s, os)
 tabs(s, os)
-       char *s, *os;
+    char *s, *os;
 {
 
 {
 
-       return (width(s, os) / 8);
+    return (width(s, os) / 8);
 }
 
 width(s, os)
 }
 
 width(s, os)
@@ -524,6 +566,12 @@ putcp(c)
 
        switch(c) {
 
 
        switch(c) {
 
+       case 0:
+               break;
+
+       case '\f':
+               break;
+
        case '{':
                ps("\\*(+K{\\*(-K");
                break;
        case '{':
                ps("\\*(+K{\\*(-K");
                break;
@@ -556,6 +604,14 @@ putcp(c)
                ps("\\&.");
                break;
 
                ps("\\&.");
                break;
 
+       case '*':
+               ps("\\fI*\\fP");
+               break;
+
+       case '/':
+               ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
+               break;
+
        default:
                if (c < 040)
                        putchar('^'), c |= '@';
        default:
                if (c < 040)
                        putchar('^'), c |= '@';
@@ -565,100 +621,36 @@ putcp(c)
        }
 }
 
        }
 }
 
+/*
+ *     look for a process beginning on this line
+ */
+boolean
+isproc(s)
+    char *s;
+{
+    pname[0] = NULL;
+    if (!l_toplex || blklevel == 0)
+       if (expmatch (s, l_prcbeg, pname) != NIL) {
+           return (TRUE);
+       }
+    return (FALSE);
+}
+
+
+/*  iskw -     check to see if the next word is a keyword
+ */
+
 iskw(s)
        register char *s;
 {
 iskw(s)
        register char *s;
 {
-       register char **ss = keywds;
+       register char **ss = l_keywds;
        register int i = 1;
        register char *cp = s;
 
        while (++cp, isidchr(*cp))
                i++;
        while (cp = *ss++)
        register int i = 1;
        register char *cp = s;
 
        while (++cp, isidchr(*cp))
                i++;
        while (cp = *ss++)
-               if (*s == *cp && !strncmp(s, cp, i) && !isidchr(cp[i]))
+               if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))
                        return (i);
        return (0);
 }
                        return (i);
        return (0);
 }
-
-cprbegin(s)
-       register char *s;
-{
-       register char *p;
-
-       p = pname;
-       if ((*s == '_' || isalpha(*s)) && s[strlen(s) - 2] == ')') {
-               while (isidchr(*s))
-                       *p++ = *s++;
-               *p = 0;
-               return (1);
-       }
-       return (0);
-}
-
-cprend(s)
-       register char *s;
-{
-       if (!strcmp(s, "}\n"))
-               return (1);
-       else
-               return (0);
-}
-
-pprbegin(s)
-       register char *s;
-{
-       register char *p;
-
-       p = pname;
-       while ((*s == ' ') || (*s == '\t'))
-               s++;
-       if (strncmp(s, "procedure", 9) == 0)
-               s += 9;
-       else if (strncmp(s, "function", 8) ==0)
-               s += 8;
-       else
-               return (0);
-       while ((*s == ' ') || (*s == '\t'))
-               s++;
-       while ((*s != ' ') && (*s != '\t') && (*s != '(') && (*s != ';'))
-               *p++ = *s++;
-       *p = 0;
-       return (1);
-}
-
-pprend(s)
-       register char *s;
-{
-       if (strncmp (s, "end", 3) == 0)
-               return (1);
-       else
-               return (0);
-}
-
-mprbegin(s)
-       register char *s;
-{
-       register char *p;
-
-       p = pname;
-       if (strcmp(&s[strlen(s) - 10], "beginproc\n") == 0) {
-
-               while ((*s == ' ') || (*s == '\t'))
-                       s++;
-
-               while (*s != ' ')
-                       *p++ = *s++;
-               *p = 0;
-               return (1);
-       } else
-               return (0);
-}
-
-mprend(s)
-       register char *s;
-{
-       if (!strcmp(&s[strlen(s) - 9], "endproc;\n"))
-               return (1);
-       else
-               return (0);
-}