diff options
author | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
commit | e0c6872cf40896c7be36b11dcc744620f10adf1d (patch) | |
tree | 60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/lgrind/source |
Initial commit
Diffstat (limited to 'support/lgrind/source')
-rw-r--r-- | support/lgrind/source/.cvsignore | 2 | ||||
-rw-r--r-- | support/lgrind/source/Makefile | 39 | ||||
-rw-r--r-- | support/lgrind/source/README | 10 | ||||
-rw-r--r-- | support/lgrind/source/lgrind.1 | 228 | ||||
-rw-r--r-- | support/lgrind/source/lgrind.c | 1769 | ||||
-rw-r--r-- | support/lgrind/source/lgrindef.5 | 248 | ||||
-rw-r--r-- | support/lgrind/source/lgrindef.c | 330 | ||||
-rw-r--r-- | support/lgrind/source/lgrindef.h | 12 | ||||
-rw-r--r-- | support/lgrind/source/lgutil.c | 323 | ||||
-rw-r--r-- | support/lgrind/source/makefile.dos | 34 | ||||
-rw-r--r-- | support/lgrind/source/makefile.emx | 55 | ||||
-rw-r--r-- | support/lgrind/source/regexp.c | 648 | ||||
-rw-r--r-- | support/lgrind/source/regexp.h | 13 | ||||
-rw-r--r-- | support/lgrind/source/retest.c | 75 | ||||
-rw-r--r-- | support/lgrind/source/v2lg.c | 78 |
15 files changed, 3864 insertions, 0 deletions
diff --git a/support/lgrind/source/.cvsignore b/support/lgrind/source/.cvsignore new file mode 100644 index 0000000000..7726bfbebb --- /dev/null +++ b/support/lgrind/source/.cvsignore @@ -0,0 +1,2 @@ +lgrind + diff --git a/support/lgrind/source/Makefile b/support/lgrind/source/Makefile new file mode 100644 index 0000000000..b785122aae --- /dev/null +++ b/support/lgrind/source/Makefile @@ -0,0 +1,39 @@ +# Makefile for lgrind, a LaTeX prettyprinter +# make binaries +# $Id: Makefile,v 1.4 1999/12/18 21:54:29 mike Exp $ + +BINDIR=$(BASEDIR)/bin +MANDIR=$(BASEDIR)/man + +CC=gcc +MANPAGES1=lgrind.1 +MANPAGES5=lgrindef.5 +OBJS=lgrind.o regexp.o retest.o lgrindef.o v2lg.o + +CFLAGS=-O2 +# CFLAGS=-Dpopen=fopen -Dpclose=fclose +# There are operating systems and compilers without these... + +.PHONY: all install clean distribution + +all: lgrind + +lgrind: lgrind.o lgrindef.o regexp.o + $(CC) $(CFLAGS) -o lgrind lgrind.o lgrindef.o regexp.o + +lgrind.o: lgrind.c lgutil.c + $(CC) $(CFLAGS) -DDEFSFILE=\"$(DEFSFILE)\" -DVERSION=\"$(VERSION)\" -c lgrind.c + +v2lg: v2lg.o + $(CC) $(CFLAGS) -o v2lg v2lg.o + +retest: retest.o regexp.o + $(CC) $(CFLAGS) -o retest retest.o regexp.o + +install: all + $(INSTALL) -s lgrind $(BINDIR)/lgrind + $(INSTALL) -m 644 $(MANPAGES1) $(MANDIR)/man1 + $(INSTALL) -m 644 $(MANPAGES5) $(MANDIR)/man5 + +clean: + rm -f $(OBJS) lgrind retest v2lg diff --git a/support/lgrind/source/README b/support/lgrind/source/README new file mode 100644 index 0000000000..6762cd342e --- /dev/null +++ b/support/lgrind/source/README @@ -0,0 +1,10 @@ +These sources should compile without any problems. + +The "access" function is declared in "io.h" or "unistd.h", depending on your +system. Replace "unistd" in "lgrind.c" with "io" if necessary. If your +compiler issues only a warning for missing prototypes you can delete the +#include completely. + + +The -d! option is tested and works well. It writes a patched version of the +executable, run it at your own risk. diff --git a/support/lgrind/source/lgrind.1 b/support/lgrind/source/lgrind.1 new file mode 100644 index 0000000000..8963aad647 --- /dev/null +++ b/support/lgrind/source/lgrind.1 @@ -0,0 +1,228 @@ +'\" t +.\" @(#)lgrind.1 3.62 25/05/99 MPi; from UCB 4.3 +.\" $Id: lgrind.1,v 1.5 2000/12/27 21:42:42 mike Exp $ +.\" This man page was reverse engineered by George V. Reilly from the +.\" preformatted man page written by Tuna Ertemalp for Jerry Leichter's +.\" version of tgrind. gvr remains wilfully ignorant of troff. +.dS C C\" \" Fixed-width font +.TH lgrind 1 "25 May 1999" "TeX/LaTeX" +.SH NAME +lgrind \- grind nice program listings using LaTeX +.SH NOTE +This man page is not yet much outdated, but might be soon except somebody +asks me to work on it. Consider the LaTeX docs the real docs. +.SH SYNOPSIS +\fBlgrind\fR [ \fB-e\fP ] [ \fB-i\fP ] [ \fB-\fP ] [ \fB-n\fP ] [ \fB-c\fP ] +[ \fB-t\fP \fI<width>\fP ] +[ \fB-h\fP \fI<header>\fP ] [ \fB-d\fP \fI<description file>\fP ] +[ \fB-l\fP\fI<language>\fP ] [ \fB-s\fP ] \fI<name>\fP ... +.PP +.SH DESCRIPTION +LGrind formats program sources in a nice style using LaTeX(1). +Comments are placed in roman, keywords in bold face, variables in +italics, and strings in typewriter font. Source file line numbers +appear in the right margin (every 10 lines). +.PP +LGrind processes its input file(s) and writes the result to standard +output. This output can be saved for later editting, inclusion in a +larger document, etc. +.PP +The options are: +.IP "\fB\-e\fP" +process a LaTeX file for embedded code. +.IP "\fB\-i\fP" +process source code file for inclusion in a LaTeX document. +.IP "\fB\-\fP" +take input from standard input. +.IP "\fB\-n\fP" +don't boldface keywords. +.IP "\fB\-c\fP" +don't treat @, etc. specially in comments. +.IP "\fB\-t\fP" +change tab width (default 8). +.IP "\fB\-h\fP" +specifies text to go into the header. +.IP "\fB\-d\fP" +specifies the language definitions file (default is +\fB/usr/lib/texmf/tex/latex/lgrind/lgrindef\fP). +.IP "\fB\-d!\fP" +same as above, but write patched executable. +.IP "\fB\-l\fP" +specifies the language to use. +.IP "\fB\-s\fP" +shows a list of currently known languages. +.PP +If LGrind is called without parameters, a help screen will be shown. +If neither \fB\-e\fP nor \fB\-i\fP are specified, a complete LaTeX +file is produced. When no language is specified, LGrind tries to find +out the language used itself; C is used when this fails. +.SH USAGE +For example, to include a C file named \fBfoo.c\fP +into your LaTeX document, first give the command: +.IP "\fC lgrind -i -lc foo.c > foo.tex\fP" +.PP +This will generate \fCfoo.tex\fP, which will have the +pretty-printed version of \fCfoo.c\fP with a lot of LaTeX commands. +.PP +Then include \fClgrind.sty\fP as you include any other style, +namely with the \fC\eusepackage{lgrind}\fP line at +the beginning of your LaTeX document. Having done this, +within the document you can include \fCfoo.tex\fP using one of the +following commands: +.PP +.IP "\fC\elgrindfile{foo.tex}\fP" +which will simply include the file at that point +of text, and will draw horizontal lines before and +after the listing. +.PP +.IP "\fC\elagrind[htbp]{foo.tex}{caption}{label}\fP" +which will put the listing also within a figure +environment, using the float options, caption and +label you gave. +.PP +To produce a standalone LaTeX file from, say, a Yacc file: +.IP "\fC lgrind -ly bary.y > bary.tex\fP" +This uses Piet van Oostrum's \fCfancyhdr.sty\fP to make the +headers and footers. +.PP +For a more detailed explanation of these commands, refer to +\fC/usr/TeX/texmf/doc/latex/lgrind.dvi\fP. +.PP +.SH "EMBEDDED PROGRAMS WITHIN A LaTeX FILE" +(From Jerry Leichter's notes.) +.PP +Within the text of your LaTeX file, you mark groups of lines as +either text- or display-style program code: +.PP +Text style: +.TS +l l. +.fT \*C + The expression + %( + a + 3 + %) + produces 10. +.fT +.TE +prints something like: "The expression \fIa\fP + 3 produces 10." +(with "a + 3" set as a program.) +.PP +The same effect can be achieved with inline @'s. +.TS +l l. +.fT \*C + The expression @a + 3@ produces 10. +.TE +.PP +Display style: +.TS +l l. +.fT \*C + The statement + %[ + a += 3; + %] + is an example of an incrementing operator. +.fT +.TE +prints something like: +.TS +l l. + The statement + a += 3; + is an example of an incrementing operator. +.TE +.PP +Important rules: +.IP +\fC%\fP and the following character must be the first two characters on +the line to be recognized. +.IP +Put \fInothing\fR on the line after the \fC%\fR and the key character. +If you do that, LGrind will provide a default environment that will +produce an \fC\ehbox\fP for \fC%(\fP \fC)%\fP, and a \fC\evbox\fP for +\fC%[\fP \fC-\fP \fC%]\fP. If you put stuff on the line, LGrind +assumes you want to control the format completely. Doing this +requires understanding \fIexactly\fP what the code LGrind produces is +doing. (Sometimes I'm not sure I do!) +.IP +\fC%)\fP and \fC%]\fP are, if I remember right, simply ignored outside +of a code group, but any extra \fC%(\fP or \fC%[\fP produces a +warning, so a missing \fC%)\fP or \fC%]\fP is usually caught. +.PP +You can insert your own code by using a line starting with \fC%=\fP in +the program text. Whatever you enter after that is left in the +output, exactly as you typed it. It will be executed in a strange +environment, so doing anything fancy is very tricky. A macro, \fC\eLine\fP, +is provided to help you do simple things. For example, +.TS +tab (/); +l. +.fT \*C +%[ +%=\eLine{________\evdots} + a = 1; +%] +.fT +.TE +produces: +.TS +tab (/); +l. +.fT \*C + \. + \. + \. + a = 1; +.fT +.TE +.PP +(Within the program text, \fC_\fP is active and expands to a +fixed-width space. A whole bunch of macros are also defined. If you +understand how LGrind sets lines up, you can replace the 8 \fC_\fP's +with a call to \fC\eTab\fP \(em but I'll let you hang yourself on that one.) +.PP +The output of LGrind always contains exactly one output line for each input +line. Hence, you can look up line numbers in TeX error messages in your +original file, rather than in the lgrind'ed (lground?) file. (Of course, if +the problem is in the LGrind output....) +.PP +Many things are controllable by re-defining various macros. You can +change what fonts LGrind will use for various kinds of things, how +much it indents the output, whether it adds line numbers, and if so at +what interval it prints them and whether it sticks them on the left or +right, and so on. This stuff is all described in \fClgrind.dvi\fP, +though probably not very well. The default settings produce output +that looks reasonable to me, though I can't say I'm ecstatic about it. +Doing a \fIreally\fP good job would require defining some special fonts. +.PP +.SH FILES +.IP "\fC/usr/bin/lgrind\fP" +Executable +.PP +.IP "\fC/usr/doc/lgrind/lgrind.dvi\fP" +Documentation +.PP +.IP "\fC/usr/lib/texmf/tex/latex/lgrind/lgrind.sty\fP" +LaTeX style file +.PP +.IP "\fC/usr/lib/texmf/tex/latex/lgrind/lgrindef\fP" +Language descriptions +.PP +.SH AUTHORS +Van Jacobson, Lawrence Berkeley Laboratory (based on +"vgrind" by Dave Presotto & William Joy of UC Berkeley), +wrote it for TeX. +.PP +Jerry Leichter of Yale University modified it for LaTeX. +.PP +George V. Reilly of Brown University changed the name to lgrind, +fixed up the man page, and added the program-text-within-comments +and @-within-LaTeX features. +.PP +Michael Piefel of Humboldt-University Berlin adapted it to +LaTeX2e and wrote decent documentation. +.PP +.SH "SEE ALSO" +\fBlatex\fR(1), \fBtex\fR(1), \fBvgrind\fR(1), \fBlgrindef\fR(5) diff --git a/support/lgrind/source/lgrind.c b/support/lgrind/source/lgrind.c new file mode 100644 index 0000000000..272bc164e0 --- /dev/null +++ b/support/lgrind/source/lgrind.c @@ -0,0 +1,1769 @@ +#ifndef lint +static char sccsid[] = "@(#)lgrind.c 3.66 (MPi) 5/10/99"; +static char rcsid[] = + "$Id: lgrind.c,v 1.14 2000/12/27 21:42:42 mike Exp $"; +#endif + +/* + * Copyright %%\copyright%% 1980 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + + +/* lgrind --- general purpose "pretty printer" for use with %%\LaTeX%%. + * A %%\LaTeX%% version of tgrind, which is a %%\TeX%% version + * of vgrind. Used to "grind nice program listings." + * + * Copyright %%\copyright%% 1985 by Van Jacobson, Lawrence Berkeley Laboratory + * This program may be freely used and copied but %%{\bf may not be sold}%% + * without the author's %%{\bf written permission}%%. This notice must remain + * in any copy or derivative. + * + * This program is an adaptation of "vfontedpr" v4.2 (12/11/84) from + * the 4.2bsd Unix distribution. Vfontedpr was written by Dave + * Presotto (based on an earlier program of the same name written by + * Bill Joy). + * + * I would welcome comments, enhancements, bug fixes, etc. Please + * mail them to: + * van@@lbl-rtsg.arpa (from arpanet, milnet, csnet, etc.) + * ..!ucbvax!lbl-csam!van (from Usenet/UUCP) + */ + +/*%%\par{\bf Thank you very much, Van. Due to the ``may not be sold'' clause%%*/ +/*%%this program has become non-free. And since the author of this licence%%*/ +/*%%cannot be contacted (address too old, apparently newer ones exist, but%%*/ +/*%%no answers from them), this cannot be changed.}\par%%*/ + +/* Copyright %%\copyright%% 1995-99 by Michael Piefel + * + * Modifications. + * -------------- + * 10 Feb 85 Van Written. + * 29 Mar 85 Chris Torek (chris@@maryland): Bug fixes for %|~|% and + * %|^L|% output. Most cpu-time eaters recoded + * to improve efficiency. + * 30 Mar 85 Chris & Van Fixed %|\C|% & %|\S|% (comment & string start + * indicators to really appear at the start of + * comments & strings. Changes for speeded-up + * @expmatch()@. + * 8 Oct 87 JSL Modified so as to compile on VMS. %|-i|% option + * Jan 88 JSL %|-e|% option for embedded code. + * Sep 91 George V Reilly Reformated and cleaned up code, including + * naughtiness with @NULL@. Added %|@|%, + * %|%%|%, %|%$|%, and %%\tt \%|%% features. + * Also the %|%<|%, %|%!|%, and %|%#|% features. + * Oct 94 Matthias Eckermann (%|ud311aa@@sunmail.lrz-muenchen.de|%) + * fixed a little bug, added: Internal_Help, + * Sep 95 Michael Piefel Modified for %%\LaTeXe%% + * Feb 96 Michael Piefel Restructured into ANSI C + * May 99 Michael Piefel Made it Y2K compliant + * Oct 99 Michael Piefel Space runs make tabstops + * Dec 99 Michael Piefel Version option, space allowed after '-l' + */ + +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <string.h> +#include <malloc.h> +#include <time.h> +/* One of the following two (depending on your system) */ +#include <unistd.h> +/* #include <io.h> */ +#include "lgrindef.h" +#include "regexp.h" + +#ifndef vms +# include <sys/types.h> +# include <sys/stat.h> +#else +# include <types.h> +# include <stat.h> +#endif + +#define STANDARD 0 +#define ALTERNATE 1 + +#define NOTCODE 0 /* the three states of @incode@ */ +#define INITCODE 1 +#define OTHERCODE 2 + +#define PNAMELEN 80 /* length of a function/procedure name */ +#define PSMAX 20 /* size of procedure name stacking */ + +#ifndef vms +# define OKEXIT 0 +# define BADEXIT 1 +# ifndef DEFSFILE +# define DEFSFILE "/usr/lib/texmf/tex/latex/lgrind/lgrindef" +# endif +#else +# define OKEXIT 1 +# define BADEXIT 0 +# define DEFSFILE "TEX$INPUTS:lgrindef.src" +#endif + +#ifndef unix +# define HELPOPTION "-?" +#else +# define HELPOPTION "--help" +#endif + +typedef struct varsubst { + char *var; + char *subst; + struct varsubst *next; +} varsubst; + +varsubst *varsubstlist; + +/* forward declarations */ + +int getredirection(int argc, char **argv); +void setlang(); +void Internal_Help(); +void Internal_Help_Language_List(); +void readfile(FILE *); +void putScp(char *); +boolean putKcp(char *, char *, boolean); +void putVcp(char *, char *); +void putstr(char *); +boolean (*isproc)(char *); +boolean isprocNorm(char *); +boolean isprocC(char *); +void outchar(int); +void parsechartab(void); +varsubst *parsevartab(char *); +void parsepreamble(char *); +void setpreambles(void); +void printpreamble(char *); +void writeDefsfileInExe(char *, char *); +int substvarname(char **, int); + +/* + * The state variables + */ + +boolean incomm; /* in a comment of the primary type */ +boolean instr; /* in a string constant */ +boolean inchr; /* in a character constant */ +int incode; /* in program text within a comment */ +int latexcode; /* in program text within %%\LaTeX%% */ +int latex_tt; /* in %|\texttt|% text within %%\LaTeX%% */ +boolean use_tt; /* use %|\texttt|% everywhere */ +boolean do_at; /* pay attention to %|@|%s in %%\LaTeX%% */ +boolean do_tt; /* pay attention to %|||%s in %%\LaTeX%% */ +boolean nokeyw = FALSE; /* no keywords being flagged */ +boolean prccont; /* continue last procedure */ +boolean code_cmnts = TRUE; /* Treat %|@|%, etc specially in comments */ +boolean code_latex = TRUE; /* Treat %|@|%, etc specially in %%\LaTeX%% */ +int lastout; /* (extended) last character to outchar */ +int comtype; /* type of comment */ +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 */ +int reclevel; /* current @record@ nesting level */ +int procedures_pending; /* proc starts w/o block starts */ +char fname[200]=""; /* File being read */ +int lineno; /* Line number in that file */ +int linenocode; /* Line number of the source code worked on */ +char pname[BUFFERSIZE+1]; /* Current procedure name */ + +/* + * The language specific globals + */ +char defsbuf[200]="DeFsBuF"DEFSFILE; +char *defsfile=&defsbuf[7]; /* name of language definitions file */ +char language[PNAMELEN]="mPi"; /* the language indicator */ +char the_buf[BUFFERSIZE+1]; /* general purpose buffer */ +char *buf = the_buf + 1; /* @buf-1@ must be valid */ +char *strings; /* store the keywords */ +char *defs; /* language definitions from lgrindef */ +char preamble[BUFFERSIZE/4]; /* first preamble */ +char postamble[BUFFERSIZE/4]; /* first preamble */ +char preamble2[BUFFERSIZE/4]; /* file preamble */ +char config[BUFFERSIZE/4]; /* redefinitions within lgrind-environment */ +char chartab[BUFFERSIZE/4]; /* buffer for chartab modifications */ +char *l_keywds[BUFFERSIZE/2]; /* keyword table address */ +char *l_prcbeg; /* regular expr for procedure begin */ +char *l_noproc; /* regexp for lines with NO procedure begin */ +char *l_combeg; /* regexp introducing a comment */ +char *l_comend; /* regexp ending a comment */ +char *l_acmbeg; /* regexp introducing a comment */ +char *l_acmend; /* regexp ending a comment */ +char *l_blkbeg; /* regexp beginning of a block */ +char *l_blkend; /* regexp ending a block */ +char *l_strbeg; /* regexp starting string constant */ +char *l_strend; /* regexp ending string constant */ +char *l_chrbeg; /* regexp starting character constant */ +char *l_chrend; /* regexp ending character constant */ +char *l_cdebeg; /* regexp starting prog text within comment */ +char s_cdebeg[BUFFERSIZE/8]; /* actual string corresponding to @l_cdebeg@ */ +char *l_cdeend; /* regexp ending prog text within comment */ +char *l_texbeg; /* regexp starting %%\TeX%% text in comment */ +char *l_texend; /* regexp ending %%\TeX%% text in comment */ +char *l_txmbeg; /* regexp starting %%\TeX%% math in comment */ +char *l_txmend; /* regexp ending %%\TeX%% math in comment */ +char *l_tt_beg; /* regexp starting verbatim text in comment */ +char *l_tt_end; /* regexp ending typewriter text in comment */ +char *l_at; /* regexp for %|@|% in %%\LaTeX%% text */ +char *l_tt; /* regexp for %|||% in %%\LaTeX%% text */ +char *l_pc; /* regexp for %|%|% in %%\LaTeX%% text */ +char *l_id; /* string containing valid identifier chars */ +char *l_record; /* start of lexical block outside functions */ +char l_escape; /* character used to escape characters */ +boolean l_toplex; /* procedures only defined at top lex level */ +boolean l_onecase; /* upper & lower case equivalent */ +boolean l_cfunctions; /* special for C function search */ +boolean embed = FALSE; /* -e seen --- do embedded code */ +boolean code = TRUE; /* Looking at code */ +int TabWidth = 8; /* Default width of a tab */ + +/* + * global variables also used by expmatch + */ + +extern boolean _escaped; /* if last character was an escape */ +extern char *_sstart; /* start of the current string */ + +extern int (*re_strncmp)(const char*, const char*, size_t); + /* function to do string compares */ + +int main(int argc, char **argv) +{ + struct stat stbuf; + boolean hseen = FALSE; /* -h seen */ + char *hstring=""; /* header string to use */ + char *programname; /* the zeroeth argument */ + boolean iseen = FALSE; /* -i seen */ + int files_done = 0; + + defs=(char*)malloc(2*BUFFERSIZE); + strings=(char*)malloc(2*BUFFERSIZE); + argc = getredirection(argc, argv); + + /* abuse of programname */ + programname=getenv("LGRINDEF"); + if (programname) strcpy(defsfile, programname); + + programname=argv[0]; + argc--, argv++; + + if (argc==0) { + Internal_Help(); + return OKEXIT; + } + + do { + struct tm modtime = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + + if (argc > 0) { + /* Help-facility */ + if (!strcmp(argv[0], HELPOPTION)) { + Internal_Help(); + return OKEXIT; + } + + if (!strcmp(argv[0], "-s")) { + Internal_Help_Language_List(); + return OKEXIT; + } + + if (!strcmp(argv[0], "--version")) { + printf("LGrind %s\n", VERSION); + return OKEXIT; + } + + + /* Header */ + if (!strcmp(argv[0], "-h")) { + hseen = TRUE; + if (argc == 1) { + argc--; + goto rest; + } + hstring = argv[1]; + argc--, argv++; + argc--, argv++; + if (argc > 0) + continue; + goto rest; + } + + /* take input from the standard place */ + if (!strcmp(argv[0], "-")) { + argc = 0; + goto rest; + } + + /* put output to non-standard place */ + if (!strcmp(argv[0], "-o")) { + if (freopen(argv[1], "w", stdout) == NULL) { + perror(argv[1]); + return BADEXIT; + } + argc--, argv++; + argc--, argv++; + continue; + } + + /* Process embedded text */ + if (!strcmp(argv[0], "-e")) { + embed = TRUE; + argc--, argv++; + continue; + } + + /* format for inclusion */ + if (!strcmp(argv[0], "-i")) { + iseen = TRUE; + argc--, argv++; + continue; + } + + /* indicate no keywords */ + if (!strcmp(argv[0], "-n")) { + nokeyw++; + argc--, argv++; + continue; + } + + /* Don't treat %|@|%, etc. specially in comments */ + if (!strcmp(argv[0], "-c")) { + code_cmnts = FALSE; + argc--, argv++; + continue; + } + + /* Do treat %|@|%, etc. specially in comments */ + if (!strcmp(argv[0], "+c")) { + code_cmnts = TRUE; + argc--, argv++; + continue; + } + + /* Don't treat %|@|%, etc. specially in + * %%\LaTeX%% text (@embed@ only) */ + if (!strcmp(argv[0], "-a")) { + code_latex = FALSE; + argc--, argv++; + continue; + } + + /* Do treat %|@|%, etc. specially in + * %%\LaTeX%% text (@embed@ only) */ + if (!strcmp(argv[0], "+a")) { + code_latex = TRUE; + argc--, argv++; + continue; + } + + /* Use %|\texttt|% for all fonts */ + if (!strcmp(argv[0], "-t")) { + sscanf(argv[1], "%d", &TabWidth); + argc--, argv++; + argc--, argv++; + continue; + } + + /* specify the language */ + if (!strncmp(argv[0], "-l", 2)) { + if (strlen(argv[0])==2) { + strcpy(language, argv[1]); + argc--, argv++; + argc--, argv++; + } else { + strcpy(language, argv[0]+2); + argc--, argv++; + continue; + } + } + + /* specify the language description file for permanent use */ + if (!strcmp(argv[0], "-d!")) { + writeDefsfileInExe(programname, argv[1]); + return OKEXIT; + } + + /* specify the language description file */ + if (!strncmp(argv[0], "-d", 2)) { /* TODO strncmp? */ + strcpy(defsfile, argv[1]); + argc--, argv++; + argc--, argv++; + continue; + } + + /* specify the file containing variable substitutions */ + if (!strcmp(argv[0], "-v")) { + varsubstlist=parsevartab(argv[1]); + argc--, argv++; + argc--, argv++; + continue; + } + + /* open the file for input */ + if (freopen(argv[0], "r", stdin) == NULL) { + perror(argv[0]); + return BADEXIT; + } + + strcpy(fname, argv[0]); + argc--, argv++; + } + rest: + + lineno = 0; + setpreambles(); + + /* get config data from %|lgrindef|% */ + if (tgetent(buf, "firstpreamble", defsfile)>0) + parsepreamble(preamble); + + if (tgetent(buf, "postamble", defsfile)>0) + parsepreamble(postamble); + + if (tgetent(buf, "filepreamble", defsfile)>0) + parsepreamble(preamble2); + + if (tgetent(buf, "configuration", defsfile)>0) + parsepreamble(config); + + if (tgetent(buf, "chartab", defsfile)>0) + parsechartab(); + + if (iseen && embed) { + fprintf(stderr, "-i makes no sense with -e; -e ignored\n"); + embed = FALSE; + } + + if (!iseen && !embed) { + if (files_done == 0) printpreamble(preamble); + printpreamble(preamble2); + printf("\\begin{lgrind}\n"); + printpreamble(config); + /* In case \BGfont was modified in config */ + printf("\\BGfont\n"); + } + + + if (embed) + printf("%% This document was generated automagically by lgrind. DO NOT EDIT.\n\n"); + if (iseen) + { + printf("%% Remember to use the lgrind style\n\n"); + printf("\\Head{"); + if (hseen) putstr(hstring); + printf("}\n"); + } + + setlang(); + + /* initialize the program */ + + incomm = instr = inchr = _escaped = FALSE; + incode = latexcode = latex_tt = NOTCODE; + do_at = do_tt = code_latex; + blklevel = 0; + reclevel = 0; + for (psptr = 0; psptr < PSMAX; psptr++) { + pstack[psptr][0] = '\0'; + plstack[psptr] = 0; + } + psptr = -1; + if (*fname=='\0') { + time_t tm; + strcpy(fname, "stdin"); + stbuf.st_size=0; + tm=time(NULL); + modtime = *localtime(&tm); + } + else { + stat(fname, &stbuf); + modtime = *localtime(&stbuf.st_mtime); + } + + if (!embed) { + printf("\\File{"); + putstr(fname); + printf("}{%d}{%d}{%d}{%d:%02d}{%ld}\n", modtime.tm_year+1900, modtime.tm_mon+1, + modtime.tm_mday, modtime.tm_hour, modtime.tm_min, stbuf.st_size); + } + + code = FALSE; + readfile(stdin); + files_done++; + + if (!iseen && !embed) { + printf("\\end{lgrind}\n"); + } + + if (code) + fprintf(stderr, "Reached EOF within code in file %s\n", fname); + } while (argc > 0); + + if (!iseen && !embed) { + printpreamble(postamble); + } + return OKEXIT; +} + + +/* + * @readfile()@ --- read and process a file + */ +void readfile(FILE *fp) +{ + register char *cp; + boolean LGinline=FALSE; /* Doing %( -- %) */ + char term; /* ']' or ')' */ + char *atptr; /* start of %|@|% within %%\LaTeX%% text */ + char *atendptr; /* end of %|@|% within %%\LaTeX%% text */ + char *pcptr; /* start of %|%|% within %%\LaTeX%% text */ + char *pcendptr; /* end of %|%|% within %%\LaTeX%% text */ + char temp[BUFFERSIZE]; + char fnamebak[200]; + int linenobak; + + while (fgets(buf, BUFFERSIZE, fp) != NULL) { + cp = &buf[strlen(buf)-1]; + if (*cp != '\n') {*(++cp) = '\n'; *(++cp) = '\0'; } + lineno++; + cp = buf; + lastout = '\0'; + if (embed) { + if (!code) { + if (buf[0] == '%' && + (buf[1] == '[' || buf[1] == '(' + || buf[1] == '#' || buf[1] == '<' || buf[1] == '!' + || buf[1] == '@' || buf[1] == '|')) { + + for (cp = buf + 2; *cp == ' ' || *cp == '\t'; cp++) + ; + + /* Change the language */ + if (buf[1] == '#') { + if (*cp == '\n') + fprintf(stderr, "no language seen after %%# in file\ + %s at line %d\n", fname, lineno); + else { + cp[strlen(cp) - 1] = '\0'; /* nuke the @'\n'@ */ + while ((*cp==' ') || (*cp=='\t')) cp++; + strcpy(language, cp); + printf("%% switching to %s\n", language); + setlang(); + } + continue; + } + + /* Turn %|@|% or %|||% processing within %%\LaTeX%% + * text on or off. */ + + if (buf[1] == '@' || buf[1] == '|') { + if (*cp == '\n') + fprintf(stderr, "no setting seen after %%%c in file\ + %s at line %d\n", buf[1], fname, lineno); + else { + int flag = (*cp == '1' || *cp == '+'); + if (buf[1] == '@') + do_at = flag; + else + do_tt = flag; + } + continue; + } + + code = TRUE; + + /* take input from another file or from a shell command */ + if (buf[1] == '<' || buf[1] == '!') { + FILE *fp; + char source = buf[1]; + + cp[strlen(cp) - 1] = '\0'; /* nuke the @'\n'@ */ + if (source == '<') + fp = fopen(cp, "r"); + else /* @source == '!'@ */ + fp = popen(cp, "r"); + + if (fp == NULL) { + sprintf(temp, "%%%c on `%s' failed", source, cp); + perror(temp); + } else { + strcpy(temp, cp); + printf("%% start of `%s'\n", temp); + /* printf("\\LGinlinefalse\\LGbegin\\lgrinde\n"); */ + /* the above was not %%all%% incorrect ... */ + printf("\\LGinlinefalse"); + printf("\\begin{lgrind}\n"); + printpreamble(config); + /* In case \BGfont was modified in config */ + printf("\\BGfont\n"); + embed = FALSE; + linenobak=lineno; strcpy(fnamebak, fname); + lineno=0; strcpy(fname, temp); + linenocode=0; + readfile(fp); + lineno=linenobak; strcpy(fname, fnamebak); + embed = TRUE; + printf("\\end{lgrind}\n"); + printf("%% end of `%s'\n", temp); + if (source == '<') + fclose(fp); + else + pclose(fp); + } + code = FALSE; + continue; + } + + /* embedded inline or displayed code */ + linenocode=0; + if (buf[1] == '(') { + LGinline = TRUE; + term = ')'; + printf("\\LGinlinetrue"); + } else { /* @buf[1] == '['@ */ + LGinline = FALSE; + term = ']'; + printf("\\LGinlinefalse"); + } + if (buf[2] == '\n') { + printf("\\LGbegin\\lgrinde\n"); + } else { + buf[strlen(buf)-1] = '\0'; + printf("%s\\lgrinde\n", &buf[2]); + } + continue; + + } /* @if (buf[0] == '%'@%%\dots%% (special comment) */ + else + { + while (do_at && (atendptr = expmatch(cp, l_at, &atptr, (char *) NULL)) != NULL + && ( (pcendptr = expmatch(cp, l_pc, &pcptr, (char *) NULL)) == NULL + || atptr < pcptr ) ) + { + putVcp(cp, atptr-1); + printf("\\LGinlinetrue\\LGbegin\\lgrinde"); + cp = atendptr; + atendptr = expmatch(cp, l_at, &atptr, (char *) NULL); + /* No %|@|%: implicit %|@|% just before NL */ + if (atptr == NULL) + atptr = atendptr = cp + strlen(cp) - 1; + strncpy(temp, cp, (size_t)(atptr-cp)); + temp[(int)(atptr-cp)] = '\0'; + cp = atendptr; + linenocode++; + putScp(temp); + printf("}}\\egroup\\endlgrinde\\LGend{}"); + } + fputs(cp, stdout); + continue; + } + } /* @if (!code)@ */ + + /* + * We're in embedded code. + */ + if (buf[0] == '%') { + if (buf[1] == '*') { + if (term != ']') + fprintf(stderr, "%%* only makes sense in display mode\ + in file %s at line %d\n", fname, lineno); + else { + printf("\\endlgrinde\\LGend\n"); + printf("\\LGinlinefalse\\LGbegin\\lgrinde[%d]\n", linenocode+1); + } + continue; + } else if (buf[1] == term) { + if (term == ')') printf("\\egroup"); + if (buf[2] == '\n') + printf("\\endlgrinde\\LGend\n"); + else + printf("\\endlgrinde%s", &buf[2]); + code = FALSE; + continue; + } else if (buf[1] == '=') { /* Send literal */ + fputs(&buf[2], stdout); + continue; + } else if (buf[1] == '[' || buf[1] == '(' + || buf[1] == ']' || buf[1] == ')') { + fprintf(stderr, + "Possible nested embedded code in file %s at line %d\n", + fname, lineno); + } + } + + /* + * Inline code --- suppress leading whitespace + */ + if (LGinline) { + while (isspace(*cp)) + cp++; + } + } /* @if (embed)@ */ + + if (*cp == '\f') { + printf("\\NewPage\n"); + cp++; + if (*cp == '\n') /* some people like ^Ls on their own line */ + continue; + } + prccont = FALSE; + linenocode++; + putScp(cp); + if (!embed && prccont && (psptr >= 0)) { + printf("\\ProcCont{"); + putstr(pstack[psptr]); + printf("}"); + } +#ifdef DEBUG + printf("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr); +#endif + } /* @while fgets()@ */ +} + + + +/* + * Set all of the language-dependent variables + */ +void setlang(void) +{ + char *p, *cp; + int i; + + /* + * get the language definition from the defs file + */ + if (strcmp(language, "mPi") == 0) /* none yet defined */ + { + i = tgetent(defs, "extensions", defsfile); + if (i == 0) strcpy(language, "c"); + else + { + cp = strrchr(fname, '.'); + if (cp !=NULL) + { + p=buf; cp = tgetstr(cp+1, &p); + } + if (cp != NULL) strcpy(language, cp); + else strcpy(language, "c"); + } + } + i = tgetent(defs, language, defsfile); + if (i == 0) { + fprintf(stderr, "no entry for language %s\n", language); + exit(BADEXIT); + } else if (i < 0) { + fprintf(stderr, "cannot find lgrindef file `%s'\n", defsfile); + exit(BADEXIT); + } + + p = strings; + if (tgetstr("kw", &p) == NULL) + 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 = NULL; + } + + p = buf; l_prcbeg = convexp(tgetstr("pb", &p)); + p = buf; l_noproc = convexp(tgetstr("np", &p)); + p = buf; l_combeg = convexp(tgetstr("cb", &p)); + p = buf; l_comend = convexp(tgetstr("ce", &p)); + p = buf; l_acmbeg = convexp(tgetstr("ab", &p)); + p = buf; l_acmend = convexp(tgetstr("ae", &p)); + p = buf; l_strbeg = convexp(tgetstr("sb", &p)); + p = buf; l_strend = convexp(tgetstr("se", &p)); + p = buf; l_blkbeg = convexp(tgetstr("bb", &p)); + p = buf; l_blkend = convexp(tgetstr("be", &p)); + p = buf; l_chrbeg = convexp(tgetstr("lb", &p)); + p = buf; l_chrend = convexp(tgetstr("le", &p)); + p = s_cdebeg; l_cdebeg = convexp(tgetstr("zb", &p)); + p = buf; l_cdeend = convexp(tgetstr("ze", &p)); + p = buf; l_texbeg = convexp(tgetstr("tb", &p)); + p = buf; l_texend = convexp(tgetstr("te", &p)); + p = buf; l_txmbeg = convexp(tgetstr("mb", &p)); + p = buf; l_txmend = convexp(tgetstr("me", &p)); + p = buf; l_tt_beg = convexp(tgetstr("vb", &p)); + p = buf; l_tt_end = convexp(tgetstr("ve", &p)); + p = buf; l_record = convexp(tgetstr("rb", &p)); + p = buf; cp = tgetstr("id", &p) ; + if (cp) + { + l_id = (char*)malloc(strlen(cp)); + strcpy(l_id, cp); + } + else l_id = "_"; + + l_tt = convexp("\\|"); + l_at = convexp("@"); + l_pc = convexp("%"); + + l_escape = '\\'; + l_cfunctions = tgetflag("cf"); + isproc = l_cfunctions ? isprocC : isprocNorm; + + l_onecase = tgetflag("oc"); + re_strncmp = l_onecase ? lc_strncmp : strncmp; + l_toplex = tgetflag("tl"); +} + + +/* + * Write out a line of TeX in comment verbatim + */ +void putVtc(char *stringstart, char *comacmptr) +{ + char *texptr, *texendptr; + char texline[BUFFERSIZE/8]; + texendptr = expmatch(stringstart, l_texend, &texptr, (char *) NULL); + if (texendptr == comacmptr) + { + strncpy(texline, stringstart, texptr-stringstart); + texline[texptr-stringstart]='\0'; + printf("%s\n", texline); + } +} + + +/* + * Write out a formatted line of program text + */ +void putScp(char *os) +{ + register char *s = os; /* pointer to unmatched string */ + register char *s1; /* temp. string */ + char *comptr; /* start of a comment delimiter */ + char *comendptr; /* end of a comment delimiter */ + char *acmptr; /* start of an alt. comment delimiter */ + char *acmendptr; /* end of an alt. comment delimiter */ + char *strptr; /* start of a string delimiter */ + char *strendptr; /* end of a string delimiter */ + char *chrptr; /* start of a char. const delimiter */ + char *chrendptr; /* end of a char. const delimiter */ + char *cdeptr; /* start of prog text delim within a comment */ + char *cdeendptr; /* end of prog text delim within a comment */ + char *cdbptr; /* start of prog text delim within a comment */ + char *cdbendptr; /* end of prog text delim within a comment */ + char *texptr; /* start of %%\TeX%% text delim within a comment */ + char *texendptr; /* end of %%\TeX%% text delim within a comment */ + char *txmptr; /* start of %%\TeX%% math delim within a comment */ + char *txmendptr; /* end of %%\TeX%% math delim within a comment */ + char *tt_ptr; /* start of typewriter delim within a comment */ + char *tt_endptr; /* end of typewriter delim within a comment */ + char *blksptr; /* start of a lexical block start */ + char *blksendptr; /* end of a lexical block start */ + char *recsptr; /* start of a lexical block outside functions start */ + char *recsendptr; /* end of a lexical block outside functions start */ + char *blkeptr; /* start of a lexical block end */ + char *blkeendptr; /* end of a lexical block end */ + + _sstart = os; /* remember the start for @expmatch()@ */ + _escaped = FALSE; + if (nokeyw || incomm || instr) + goto skip; + + /* check for complete comment TeX line */ + comendptr = expmatch(s, l_combeg, &comptr, (char *) NULL); + acmendptr = expmatch(s, l_acmbeg, &acmptr, (char *) NULL); + texendptr = expmatch(s, l_texbeg, &texptr, (char *) NULL); + if (texptr && comptr == s) + if (texptr == comendptr) + { + comendptr = expmatch(comendptr, l_comend, &comptr, (char *) NULL); + if (*comendptr=='\n') + { + putVtc(texendptr, comptr); + return; + } + } else if (texptr == acmendptr) + { + acmendptr = expmatch(acmendptr, l_acmend, &acmptr, (char *) NULL); + if (*acmendptr=='\n') + { + putVtc(texendptr, acmptr); + return; + } + } + + if ((*isproc)(s) && reclevel == 0) { + printf("\\index{"); putstr(pname); printf("}"); + printf("\\Proc{"); + if (strlen(pname)>30) + { + s1=pname+strlen(pname)-1; + while (s1>pname && (isalnum(*s1) || *s1=='_')) s1--; + if (s1!=pname) strcpy(pname, s1+1); + } + if (strlen(pname)>32) { + strcpy(pname, pname+strlen(pname)-28); + printf("\\dots "); } + putstr(pname); + printf("}"); + if (!embed && psptr < PSMAX-1) { + ++psptr; + strncpy(pstack[psptr], pname, PNAMELEN); + pstack[psptr][PNAMELEN] = '\0'; + plstack[psptr] = blklevel-1; + } + } + + skip: + printf("\\L{\\LB{"); + + do { + /* check for string, comment, blockstart, etc */ + if (!incomm && !instr && !inchr) { + + blkeendptr = expmatch(s, l_blkend, &blkeptr, (char *) NULL); + blksendptr = expmatch(s, l_blkbeg, &blksptr, (char *) NULL); + recsendptr = expmatch(s, l_record, &recsptr, (char *) NULL); + comendptr = expmatch(s, l_combeg, &comptr, (char *) NULL); + acmendptr = expmatch(s, l_acmbeg, &acmptr, (char *) NULL); + strendptr = expmatch(s, l_strbeg, &strptr, (char *) NULL); + chrendptr = expmatch(s, l_chrbeg, &chrptr, (char *) NULL); + + /* check for end of program text in comment */ + if (incode != NOTCODE) { + cdeendptr = expmatch(s, l_cdeend, &cdeptr, (char *) NULL); + + if ((cdeptr == NULL && (comptr != NULL || acmptr != NULL)) + || (cdeptr != NULL + && ((comptr != NULL && comptr < cdeptr) + || ((acmptr != NULL && acmptr < cdeptr))))) { + fprintf(stderr, "Comments may not be nested within\ + program text within comments in file %s at\ + line %d\n", fname, lineno); + s = (comptr != NULL) ? comptr : acmptr; + incode = NOTCODE; + continue; + } + + /* look to see if there's a \<zb> in the program text */ + cdbendptr = expmatch(s, l_cdebeg, &cdbptr, (char *) NULL); + if (cdeptr != NULL + && (strptr == NULL || cdbptr < strptr) + && (chrptr == NULL || cdbptr < chrptr) + && (blksptr == NULL || cdbptr < blksptr) + && (recsptr == NULL || cdbptr < recsptr) + && (blkeptr == NULL || cdbptr < blkeptr)) { + if (cdbptr > s && cdbptr[-1] == l_escape) { + putKcp(s, cdbptr-2, FALSE); + printf("\\C{}"); + putKcp(s_cdebeg, s_cdebeg + strlen(s_cdebeg) - 1, TRUE); + printf("\\CE{}"); + s = cdbendptr; + incode = OTHERCODE; + continue; + } + } + + comendptr = + expmatch(s, (comtype == STANDARD) ? l_comend : l_acmend, + &comptr, (char *) NULL); + + if ((cdeptr == NULL && comptr != NULL) + || (cdeptr != NULL && (comptr != NULL && comptr < cdeptr))) { + fprintf(stderr, "Unterminated program text within comment\ + in file %s at line %d\n", fname, lineno); + printf("\\C{}"); + s = comptr; + incode = NOTCODE; incomm = TRUE; + continue; + } + + + if (cdeendptr != NULL) { + if ((strptr == NULL || cdeptr < strptr) + && (chrptr == NULL || cdeptr < chrptr) + && (blksptr == NULL || cdeptr < blksptr) + && (recsptr == NULL || cdeptr < recsptr) + && (blkeptr == NULL || cdeptr < blkeptr)) { + if (incode == INITCODE && cdeptr == s) { + printf("\\C{}"); + putKcp(s_cdebeg, s_cdebeg + strlen(s_cdebeg) - 1, TRUE); + } else { + putKcp(s, cdeptr-1, FALSE); + printf("\\C{}"); + } + s = cdeendptr; + incode = NOTCODE; incomm = TRUE; + continue; + } + } else if (strptr == NULL && chrptr == NULL + && blksptr == NULL && recsptr == NULL + && blkeptr == NULL) { + cdeptr = s; + s += strlen(s); + putKcp(cdeptr, s-1, FALSE); + incode = OTHERCODE; + continue; + } /* else there is a string/char/block on this line */ + + incode = OTHERCODE; + } /* @if (incode)@ */ + + + /* start of a comment? */ + if (comptr != NULL + && (strptr == NULL || comptr < strptr) + && (acmptr == NULL || comptr < acmptr) + && (chrptr == NULL || comptr < chrptr) + && (blksptr == NULL || comptr < blksptr) + && (recsptr == NULL || comptr < recsptr) + && (blkeptr == NULL || comptr < blkeptr)) { + putKcp(s, comptr-1, FALSE); + printf("\\C{}"); + s = comendptr; + putKcp(comptr, comendptr-1, TRUE); + incomm = TRUE; + comtype = STANDARD; + continue; + } + + /* start of an alternate-form comment? */ + if (acmptr != NULL + && (strptr == NULL || acmptr < strptr) + && (chrptr == NULL || acmptr < chrptr) + && (blksptr == NULL || acmptr < blksptr) + && (recsptr == NULL || acmptr < recsptr) + && (blkeptr == NULL || acmptr < blkeptr)) { + putKcp(s, acmptr-1, FALSE); + printf("\\C{}"); + s = acmendptr; + putKcp(acmptr, acmendptr-1, TRUE); + incomm = TRUE; + comtype = ALTERNATE; + continue; + } + + /* start of a string? */ + if (strptr != NULL + && (chrptr == NULL || strptr < chrptr) + && (blksptr == NULL || strptr < blksptr) + && (recsptr == NULL || strptr < recsptr) + && (blkeptr == NULL || strptr < blkeptr)) { + putKcp(s, strptr-1, FALSE); + printf("\\S{}"); + s = strendptr; + putKcp(strptr, strendptr-1, FALSE); + instr = TRUE; + continue; + } + + /* start of a character string? */ + if (chrptr != NULL + && (blksptr == NULL || chrptr < blksptr) + && (recsptr == NULL || chrptr < recsptr) + && (blkeptr == NULL || chrptr < blkeptr)) { + putKcp(s, chrptr-1, FALSE); + printf("\\S{}"); + s = chrendptr; + putKcp(chrptr, chrendptr-1, FALSE); + inchr = TRUE; + continue; + } + + /* end of a lexical block */ + if (blkeptr != NULL) { + if ((blksptr == NULL || blkeptr < blksptr) && + (recsptr == NULL || blkeptr < recsptr)) { + putKcp(s, blkeendptr - 1, FALSE); + s = blkeendptr; + if (blklevel) blklevel--; + if (reclevel) reclevel--; + else if (psptr >= 0 && plstack[psptr] >= blklevel) { + + /* end of current procedure */ + blklevel = plstack[psptr]; + + /* see if we should print the last proc name */ + if (--psptr >= 0) + prccont = TRUE; + else + psptr = -1; + } + continue; + } + } + + /* start of a lexical block */ + if (blksptr != NULL) { + putKcp(s, blksendptr - 1, FALSE); + s = blksendptr; + if (procedures_pending) + procedures_pending--; + else + /* when C functions then not on top level */ + if (!l_cfunctions || blklevel) + blklevel++; + continue; + } + + /* start of a lexical block outside functions */ + if (recsptr != NULL) { + putKcp(s, recsendptr - 1, FALSE); + s = recsendptr; + if (procedures_pending) + procedures_pending--; + else + blklevel++; + reclevel++; + continue; + } + + /* check for end of comment */ + } else if (incomm) { + + cdeendptr = expmatch(s, l_cdebeg, &cdeptr, (char *) NULL); + texendptr = expmatch(s, l_texbeg, &texptr, (char *) NULL); + txmendptr = expmatch(s, l_txmbeg, &txmptr, (char *) NULL); + tt_endptr = expmatch(s, l_tt_beg, &tt_ptr, (char *) NULL); + + if (code_cmnts) { + + /* Check for program text within comment */ + if (cdeptr != NULL + && (texptr == NULL || cdeptr < texptr) + && (tt_ptr == NULL || cdeptr < tt_ptr) + && (txmptr == NULL || cdeptr < txmptr)) { + putKcp(s, cdeptr-1, TRUE); + printf("\\CE{}"); + s = cdeendptr; + incode = INITCODE; incomm = FALSE; + continue; + } + + /* Check for %%\TeX%% text within comment */ + if (texptr != NULL + && (tt_ptr == NULL || texptr < tt_ptr) + && (txmptr == NULL || texptr < txmptr)) { + putKcp(s, texptr-1, TRUE); + s = texendptr; + if ((texendptr = + expmatch(s, l_texend, &texptr, (char *) NULL)) != NULL) { + putchar('{'); putVcp(s, texptr-1); putchar('}'); + s = texendptr; + } else { + fprintf(stderr, "LaTeX text within a comment must all be\ + on one line (file %s at line %d)\n", fname, lineno); + s += strlen(s); + } + continue; + } + + /* Check for typewriter text within comment */ + if (tt_ptr != NULL + && (txmptr == NULL || tt_ptr < txmptr)) { + putKcp(s, tt_ptr-1, TRUE); + s = tt_endptr; + if ((tt_endptr = + expmatch(s, l_tt_end, &tt_ptr, (char *) NULL)) != NULL) { + printf("{\\TTfont "); + latex_tt=TRUE; + putKcp(s, tt_ptr-1, TRUE); + latex_tt=FALSE; + printf("}"); + s = tt_endptr; + } else { + fprintf(stderr, "typewriter text within a comment must all\ + be on one line (file %s at line %d)\n\t%s", + fname, lineno, s); + s += strlen(s); + } + continue; + } + + /* Check for %%\TeX%% math within comment */ + if (txmptr != NULL) { + putKcp(s, txmptr-1, TRUE); + s = txmendptr; + if ((txmendptr = + expmatch(s, l_txmend, &txmptr, (char *) NULL)) != NULL) { + putchar('$'); putVcp(s, txmptr-1); putchar('$'); + s = txmendptr; + } else { + fprintf(stderr, "TeX math within a comment must all be\ + on one line (file %s at line %d)\n", fname, lineno); + s += strlen(s); + } + continue; + } + } /* @if (code_cmnts)@ */ + + if ((comendptr = + expmatch(s, (comtype == STANDARD) ? l_comend : l_acmend, + (char **) NULL, (char *) NULL)) != NULL) { + putKcp(s, comendptr-1, TRUE); + s = comendptr; + incomm = FALSE; + printf("\\CE{}"); + } else { + comptr = s; + s += strlen(s); + putKcp(comptr, s-1, TRUE); + } + continue; + + /* check for end of string */ + } else if (instr) { + if ((strendptr = + expmatch(s, l_strend, (char **) NULL, (char *) NULL)) != NULL) { + putKcp(s, strendptr-1, TRUE); + s = strendptr; + instr = FALSE; + printf("\\SE{}"); + } else { + strptr = s; + s += strlen(s); + putKcp(strptr, s-1, TRUE); + } + continue; + + /* check for end of character string */ + } else if (inchr) { + if ((chrendptr = + expmatch(s, l_chrend, (char **) NULL, (char *) NULL)) != NULL) { + putKcp(s, chrendptr-1, TRUE); + s = chrendptr; + inchr = FALSE; + printf("\\SE{}"); + } else { + chrptr = s; + s += strlen(s); + putKcp(chrptr, s-1, TRUE); + } + continue; + } + + /* print out the line */ + chrptr = s; + s += strlen(s); + putKcp(chrptr, s-1, FALSE); + + } while (*s); +} + + +/* + * Output a %%\LaTeX%% command to tab to column "col" (see the documentation + * for a partial explanation of the bizarre brace arrangement). + */ +#define tabto(col) printf("}\\Tab{%d}{", col); + + +/* + * @islidchr()@ is @TRUE@ for things that can begin identifiers; + * @isidchr@ is @TRUE@ of identifier constituents. + */ +#define islidchr(c)(((isalpha(c) || (strchr(l_id, c))) && c!=0)) +#define isidchr(c)(((isalnum(c) || (strchr(l_id, c))) && c!=0)) + + +/* + * Get the identifier starting at @s@. It is assumed that @s@ may indeed + * start an identifier. Value is %$>0$% for a keyword --- the count of + * characters in the keyword --- and %$<0$% if not a keyword, in which + * case the value returned is the negative of the number of bytes in + * the matched identifier. + * + * This function checks @nokeyw@ and won't check to see if the + * identifier found is a keyword if it is @TRUE@. + */ +int getid(char *s) +{ + char **ss = l_keywds; + int i = 1; + char *cp = s; + int firstc = *s; + + while (++cp, isidchr(*cp)) + i++; + if (nokeyw) + return -i; + while ((cp = *ss++) != '\0') { + if (!l_onecase && firstc != *cp) + continue; + if ((*re_strncmp)(s, cp, i) == 0 && !isidchr(cp[i])) + return i; + } + return -i; +} + + +/* + * Calculate the width of a string, including tabs + */ +int width(register char *s, register char *os) +{ + register int i = 0, c; + + while (s < os) { + c = *s++; + if (c == '\t') { + /* i = (i + 8) &~ 7; */ + i = ((i + TabWidth) / TabWidth) * TabWidth; + continue; + } + if (c < ' ') + i += 2; + else + i++; + } + return i; +} + + +/* + * Write out a portion of the line + */ +boolean putKcp(char *start, char *end, boolean nix) +/* start Start of string to write + end End of string to write + nix Don't look for identifiers, numbers + returns whether we ate only as much as we ought to */ +{ + int i, c; + + while (start <= end) { + c = *start++; + /* + * take care of nice tab stops ... + * ... and of space runs + */ + if (c == '\t' || (c == ' ' && *start == ' ')) { + while (start <= end && ( *start == '\t' || *start == ' ' )) + start++; + tabto(width(_sstart, start)); + continue; + } + + /* + * First split off numbers. We have a rather ad hoc + * definition: A number is a digit followed by any number + * of digits or letters, and periods. + * (Additionally a number can start with %|$|% (e.g. hex numbers).) + * This produces meaningless parses --- %$.2$% is parsed as %|.|% + * followed by the number %|2|% --- but should produce decent + * results for most languages (outside of maybe FORTRAN and DCL). + */ + if (!nix) { + if (c == '#' || islidchr(c)) { + i = getid(--start); + if (i > 0) + printf("\\K{"); + else { + printf("\\V{"); + i = substvarname(&start, -i); + } + while (--i >= 0) { + c = *start++; + outchar(c); + } + putchar('}'); + continue; + } + else if (isdigit(c) || c == '$') { + printf("\\N{"); + do { + if (c == 'l') + printf("$\\ell$"); + else + outchar(c); + c = *start++; + } while (isalnum(c) || (c == '.' && *start != '.')); + putchar('}'); + start--; + continue; + } + } + outchar(c); + } + return (start-1)==end; +} + + + +/* + * Write out a portion of the line verbatim + */ +void putVcp(char *start, char *end) +/* start Start of string to write + end End of string to write */ +{ + for ( ; start <= end; start++) { + putchar(*start); + } +} + + +/* + * Output a string, escaping special characters + */ +void putstr(register char *cp) +{ + register int c; + + if (cp == NULL) + return; + while ((c = *cp++) != '\0') + outchar(c); +} + + +/* + * The following table converts ASCII characters to a printed + * representation, taking care of all the %%\LaTeX%% quoting. N.B.: all + * single-character strings are assumed to be equivalent to the + * character for that index (i.e., @printtab['c']@ can't be @"f"@). + * (This is purely for efficiency hacking.) + * + * Notes: + * Some pairs of characters are handled specially within @outchar()@; + * this table contains no indication when that happens. + * %|`|% is output as %|{`}|% to avoid various ligatures, such as %|!`|%. + * The %|''|% and %|--|% (and %|---|%) ligatures are not a problem + * because those characters are output as macros anyway. Using %|{`}|% + * is sufficient since nothing we put out will ever be given to the + * hyphenation algorithms anyway. + * + * @ttprinttab@ is the same for the emulated verbatim mode + */ + +char *printtab[256] = { + "\0x", "\\^A", "\\^B", "\\^C", "\\^D", "\\^E", "\\^F", "\\^G", + "\\^H", "\t", "}}\n", "\\^K", "\0x", "\\^M", "\\^N", "\\^O", + "\\^P", "\\^Q", "\\^R", "\\^S", "\\^T", "\\^U", "\\^V", "\\^W", + "\\^X", "\\^Y", "\\^Z", "\\^[", "\\^\\!","\\^]", "\\^\\^","\\^\\_", + " ", "!", "\\3", "\\#", "\\$", "\\%", "\\&", "{'}", + "(", ")", "*", "+", ",", "\\-", ".", "/", + "0", "1", "2", "3", "4", "5", "6", "7", + "8", "9", ":", ";", "\\<", "=", "\\>", "?", + "@", "A", "B", "C", "D", "E", "F", "G", + "H", "I", "J", "K", "L", "M", "N", "O", + "P", "Q", "R", "S", "T", "U", "V", "W", + "X", "Y", "Z", "[", "\\2", "]", "\\5", "\\_", + "{`}", "a", "b", "c", "d", "e", "f", "g", + "h", "i", "j", "k", "l", "m", "n", "o", + "p", "q", "r", "s", "t", "u", "v", "w", + "x", "y", "z", "\\{", "\\|", "\\}", "\\~{}", "\\^?", + "\200", "\201", "\202", "\203", "\204", "\205", "\206", "\207", + "\210", "\211", "\212", "\213", "\214", "\215", "\216", "\217", + "\220", "\221", "\222", "\223", "\224", "\225", "\226", "\227", + "\230", "\231", "\232", "\233", "\234", "\235", "\236", "\237", + "\240", "\241", "\242", "\243", "\244", "\245", "\246", "\247", + "\250", "\251", "\252", "\253", "\254", "\255", "\256", "\257", + "\260", "\261", "\262", "\263", "\264", "\265", "\266", "\267", + "\270", "\271", "\272", "\273", "\274", "\275", "\276", "\277", + "\300", "\301", "\302", "\303", "\304", "\305", "\306", "\307", + "\310", "\311", "\312", "\313", "\314", "\315", "\316", "\317", + "\320", "\321", "\322", "\323", "\324", "\325", "\326", "\327", + "\330", "\331", "\332", "\333", "\334", "\335", "\336", "\337", + "\340", "\341", "\342", "\343", "\344", "\345", "\346", "\347", + "\350", "\351", "\352", "\353", "\354", "\355", "\356", "\357", + "\360", "\361", "\362", "\363", "\364", "\365", "\366", "\367", + "\370", "\371", "\372", "\373", "\374", "\375", "\376", "\377", +}; + +char *ttprinttab[256] = { + "\0x", "\\^A", "\\^B", "\\^C", "\\^D", "\\^E", "\\^F", "\\^G", + "\\^H", "\t", "}}\n", "\\^K", "\0x", "\\^M", "\\^N", "\\^O", + "\\^P", "\\^Q", "\\^R", "\\^S", "\\^T", "\\^U", "\\^V", "\\^W", + "\\^X", "\\^Y", "\\^Z", "\\^[", "\\^\\!","\\^]", "\\^\\^","\\^\\_", + " ", "!", "{34}", "{35}", "{36}", "{37}", "{38}", "{39}", + "(", ")", "*", "+", ",", "{45}", ".", "/", + "0", "1", "2", "3", "4", "5", "6", "7", + "8", "9", ":", ";", "{60}", "=", "{62}", "?", + "@", "A", "B", "C", "D", "E", "F", "G", + "H", "I", "J", "K", "L", "M", "N", "O", + "P", "Q", "R", "S", "T", "U", "V", "W", + "X", "Y", "Z", "[", "{92}", "]", "{94}", "{95}", + "{96}", "a", "b", "c", "d", "e", "f", "g", + "h", "i", "j", "k", "l", "m", "n", "o", + "p", "q", "r", "s", "t", "u", "v", "w", + "x", "y", "z", "{123}", "{124}", "{125}", "{126}", "\\^?", + "\200", "\201", "\202", "\203", "\204", "\205", "\206", "\207", + "\210", "\211", "\212", "\213", "\214", "\215", "\216", "\217", + "\220", "\221", "\222", "\223", "\224", "\225", "\226", "\227", + "\230", "\231", "\232", "\233", "\234", "\235", "\236", "\237", + "\240", "\241", "\242", "\243", "\244", "\245", "\246", "\247", + "\250", "\251", "\252", "\253", "\254", "\255", "\256", "\257", + "\260", "\261", "\262", "\263", "\264", "\265", "\266", "\267", + "\270", "\271", "\272", "\273", "\274", "\275", "\276", "\277", + "\300", "\301", "\302", "\303", "\304", "\305", "\306", "\307", + "\310", "\311", "\312", "\313", "\314", "\315", "\316", "\317", + "\320", "\321", "\322", "\323", "\324", "\325", "\326", "\327", + "\330", "\331", "\332", "\333", "\334", "\335", "\336", "\337", + "\340", "\341", "\342", "\343", "\344", "\345", "\346", "\347", + "\350", "\351", "\352", "\353", "\354", "\355", "\356", "\357", + "\360", "\361", "\362", "\363", "\364", "\365", "\366", "\367", + "\370", "\371", "\372", "\373", "\374", "\375", "\376", "\377", +}; + + + +#define EMDASH (-1) +/* + * Output one character, fixed up as required. Since there is no call-back + * to tell @outchar()@ to flush what it has stored, it must always produce + * some output --- it can't simply retain state. This makes certain + * translations impossible, but we can live without them for now.... + */ +void outchar(int c) +{ + if (!latex_tt) + { + if (c == ' ') { + putchar('_'); + goto fin; + } + switch (lastout) { + case '.': + case '|': + if (c == lastout) + printf("\\,"); + break; + case ' ': +#if 0 /* gvr hates this trick */ + if (incomm && c == '-') { + printf("---"); + c = EMDASH; /* For future cleverness... */ + goto fin; + } +#endif + break; + case '[': + if (c == ']') + printf("\\,"); + break; + case '-': + if (c == '>') + printf("\\!"); + break; + case '<': + if (c == '-') + printf("\\!"); + break; + } + if (incomm && c == '-') { + putchar('-'); + goto fin; + } + printtab[(unsigned char)c][1] ? + printf("%s", printtab[(unsigned char)c]) : putchar(c); + fin: + lastout = c; + } + else + c==32 ? putchar('~') : + ttprinttab[(unsigned char)c][1] ? + printf("\\symbol%s", ttprinttab[(unsigned char)c]) : putchar(c); +} + + +/* + * Look for a procedure beginning on this line + */ +boolean isprocNorm(char *s) +{ + pname[0] = '\0'; + if ((!l_toplex || blklevel == 0) + && expmatch(s, l_prcbeg, (char **) NULL, pname) != NULL + && expmatch(s, l_noproc, (char **) NULL, (char *) NULL) == NULL) + { + procedures_pending++; + blklevel++; + return TRUE; + } + return FALSE; +} + +/* Special version of the above for C functions */ +boolean isprocC(char *s) +{ + char cheat[BUFFERSIZE]; + char *j=s, *i=cheat; + boolean comm=FALSE, string=FALSE, schar=FALSE; + int brack=0; + + if (blklevel!=0) return FALSE; + while (*j) + { + if (*j=='"' && !comm && !schar) string=(string+1)%2; + else if (*j=='\'' && !comm && !string) schar=(schar+1)%2; + else if (*j=='\\' && !comm) j++; + else if (!comm && !string && !schar && *j=='{') brack++; + else if (!comm && !string && !schar && *j=='}') brack--; + else if (!comm && !string && !schar && *j=='#') break; + else if (!comm && !string && !schar && *j=='/' && *(j+1)=='/') break; + else if (!comm && !string && !schar && *j=='/' && *(j+1)=='*') comm=TRUE; + else if ( !string && !schar && *j=='*' && *(j+1)=='/') + {comm=FALSE; j++;} + else if (!brack && !comm && !string && !schar) + *i++=*j; + j++; + } + *i = '\0'; + + return isprocNorm(cheat); +} + + +/* The VMS code below has been absolutely untested for the last few years. + * Don't ask me about it. Please. MPi + */ + +/* + * @getredirection()@ is intended to aid in porting C programs + * to VMS (Vax-11 C) which does not support %|>|% and %|<|% + * I/O redirection. With suitable modification, it may + * useful for other portability problems as well. + * + * Modified, 24-Jan-86 by Jerry Leichter + * When creating a new output file, force the maximum record size to + * 512; otherwise, it ends up as 0 (though the C I/O system won't write + * a record longer than 512 bytes anyway) which will cause problems if + * the file is later opened for @APPEND@ --- if the maximum record size + * is 0, C will use the length of the longest record written to the file + * for its buffer! + */ + +#ifdef vms +# include <stdio.h> +# include <errno.h> + + int +getredirection(argc, argv) + int argc; + char **argv; +/* + * Process VMS redirection args. Exit if any error is seen. + * If @getredirection()@ processes an argument, it is erased + * from the vector. @getredirection()@ returns a new @argc@ value. + * + * Warning: do not try to simplify the code for VMS. The code + * presupposes that @getredirection()@ is called before any data is + * read from @stdin@ or written to @stdout@. + * + * Normal usage is as follows: + * + *@ main(argc, argv) + * int argc; + * char *argv[]; + * { + * argc = getredirection(argc, argv); + * }@ + */ +{ + register char *ap; /* Argument pointer */ + int i; /* @argv[]@ index */ + int j; /* Output index */ + int file; /* File_descriptor */ + + for (j = i = 1; i < argc; i++) { /* Do all arguments */ + switch (*(ap = argv[i])) { + case '<': /* %|<file|% */ + if (freopen(++ap, "r", stdin) == NULL) { + perror(ap); /* Can't find file */ + exit(errno); /* Is a fatal error */ + } + break; + + case '>': /* %|>file|% or %|>>file|% */ + if (*++ap == '>') { /* %|>>file|% */ + /* + * If the file exists, and is writable by us, + * call @freopen()@ to append to the file (using the + * file's current attributes). Otherwise, create + * a new file with "vanilla" attributes as if + * the argument was given as %|>filename|%. + * @access(name, 2)@ is @TRUE@ if we can write on + * the specified file. + */ + if (access(++ap, 2) == 0) { + if (freopen(ap, "a", stdout) != NULL) + break; /* Exit @case@ statement */ + perror(ap); /* Error, can't append */ + exit(errno); /* After @access@ test */ + } /* If file accessable */ + } + /* + * On VMS, we want to create the file using "standard" + * record attributes. @create(...)@ creates the file + * using the caller's default protection mask and + * "variable length, implied carriage return" + * attributes. @dup2()@ associates the file with @stdout@. + */ + if ((file = creat(ap, 0, "rat=cr", "rfm=var", "mrs=512")) == -1 + || dup2(file, fileno(stdout)) == -1) { + perror(ap); /* Can't create file */ + exit(errno); /* is a fatal error */ + } /* If %|>|% creation */ + break; /* Exit @case@ test */ + + default: + argv[j++] = ap; /* Not a redirector */ + break; /* Exit @case@ test */ + } + } /* For all arguments */ + argv[j] = NULL; /* Terminate @argv[]@ */ + return j; /* Return new @argc@ */ +} + +#else /* @!vms@ */ + +int getredirection(int argc, char **argv) + /* + * Dummy routine. + */ +{ + return argc; +} + +#endif /* @!vms@ */ + +#include "lgutil.c" +/* Helper functions */ + diff --git a/support/lgrind/source/lgrindef.5 b/support/lgrind/source/lgrindef.5 new file mode 100644 index 0000000000..3606fd3666 --- /dev/null +++ b/support/lgrind/source/lgrindef.5 @@ -0,0 +1,248 @@ +.\" @(#)lgrindef.5 3.62 11/2/97 MPi; from UCB 4.3 +.\" Copyright (c) 1983 Regents of the University of California. +.\" All rights reserved. The Berkeley software License Agreement +.\" specifies the terms and conditions for redistribution. +.\" +.\" $Id: lgrindef.5,v 1.4 1999/10/05 17:41:35 mike Exp $ +.\" +.\" This is the standard SunOS vgrindefs(5) manpage, with a few +.\" additions to describe the extra capabilities needed by lgrind, +.\" viz.: zb, ze, tb, te, mb, me, vb, ve, cf, rb. +.\" +.tr || +.TH LGRINDEF 5 "11 February 1997" "TeX/LaTeX" +.SH NAME +lgrindef \- LGrind's language definition data base +.SH NOTE +This man page is not yet much outdated, but might be soon except somebody +asks me to work on it. Consider the LaTeX docs the real docs. +.SH SYNOPSIS +.B /usr/TeX/texmf/tex/latex/lgrind/lgrindef +.SH DESCRIPTION +.IX "lgrindef file" "" "\fLlgrindef\fP \(em LGrind language definitions" +.B lgrindef +contains all language definitions for +.BR lgrind (1). +The data base is very similar to +.BR vgrind (5) +and +.BR termcap (5), +and it is upward-compatible with that of +.BR vgrind (5). +Capabilities in +.B lgrindef +are of two types: +Boolean capabilities which indicate that the language has +some particular feature +and string +capabilities which give a regular expression or +keyword list. +Entries may continue onto multiple lines by giving a \e as the last +character of a line. +Lines starting with # are comments. +.SS Capabilities +The following table names and describes each capability. +.PP +.PD 0 +.ta \w'\fBName\fP 'u +.nr Xx \w'\fBName\fP \fBType\fP 'u +.TP \n(Xxu +.B +Name Type +.B Description +.TP +.B ab \fRstr +Regular expression for the start of an alternate form comment +.TP +.B ae \fRstr +Regular expression for the end of an alternate form comment +.TP +.B bb \fRstr +Regular expression for the start of a block +.TP +.B be \fRstr +Regular expression for the end of a lexical block +.TP +.B cb \fRstr +Regular expression for the start of a comment +.TP +.B ce \fRstr +Regular expression for the end of a comment +.TP +.B cf \fRbool +(Boolean) Use specialized C function detection +.TP +.B id \fRstr +String giving characters other than letters and digits +that may legally occur in identifiers (default `_') +.TP +.B kw \fRstr +A list of keywords separated by spaces +.TP +.B lb \fRstr +Regular expression for the start of a character constant +.TP +.B le \fRstr +Regular expression for the end of a character constant +.TP +.B mb \fRstr +Regular expression for the start of TeX math within a comment +.TP +.B me \fRstr +Regular expression for the end of TeX math within a comment +.TP +.B np \fRstr +Regular expression for a line +.B not +containing the start of a procedure +.TP +.B oc \fRbool +Present means upper and lower case are equivalent +.TP +.B pb \fRstr +Regular expression for start of a procedure +.TP +.B pl \fRbool +Procedure definitions are constrained to the lexical level +matched by the `px' capability +.TP +.B px \fRstr +A match for this regular expression indicates +that procedure definitions may occur at the next lexical level. +Useful for lisp-like languages in which procedure definitions +occur as subexpressions of defuns. +.TP +.B rb \fRstr +Regular expression for the start of a block outside the actual code +.TP +.B sb \fRstr +Regular expression for the start of a string +.TP +.B se \fRstr +Regular expression for the end of a string +.TP +.B rb \fRstr +Regular expression for the end of a block outside a funtion +(e. g. records in Pascal and Modula-2) +.TP +.B tb \fRstr +Regular expression for the start of TeX text within a comment +.TP +.B tc \fRstr +Use the named entry as a continuation of this one +.TP +.B te \fRstr +Regular expression for the end of TeX text within a comment +.TP +.B tl \fRbool +Present means procedures are only defined at the top lexical level +.TP +.B vb \fRstr +Regular expression for the start of typewriter text within a comment +.TP +.B ve \fRstr +Regular expression for the end of typewriter text within a comment +.TP +.B zb \fRstr +Regular expression for the start of program text within a comment +.TP +.B ze \fRstr +Regular expression for the end of program text within a comment +.PD +.DT +.SS "Regular Expressions" +.B lgrindef +uses regular expressions similar to those of +.BR ex (1) +and +.BR lex (1). +The characters `^', +`$', +`\^|', +`:', +and `\e' +are reserved characters and must be +`quoted' with a preceding \e if they +are to be included as normal characters. +The metasymbols and their meanings are: +.IP $ +The end of a line +.PD 0.2v +.IP ^ +The beginning of a line +.IP \ed +A delimiter (space, tab, newline, start of line) +.IP \ea +Matches any string of symbols +(like `.*' in lex) +.IP \ep +Matches any identifier. +In a procedure definition +(the `pb' capability) +the string that matches this symbol is used as the procedure name. +.IP (\^) +Grouping +.IP | +Alternation +.IP ? +Last item is optional +.IP \ee +Preceding any string means that the string will not match an +input string if the input string is preceded by an escape character (\e). +This is typically used for languages (like C) that can include the +string delimiter in a string by escaping it. +.PD +.PP +Unlike other regular expressions in the system, +these match words and not characters. +Hence something like `(tramp\^|\^steamer)flies?' +would match `tramp', +`steamer', +`trampflies', +or `steamerflies'. +Contrary to some forms of regular expressions, +.B lgrindef +alternation binds very tightly. +Grouping parentheses are likely to be necessary in expressions +involving alternation. +.SS "Keyword List" +The keyword list is just a list of keywords in the language separated +by spaces. +If the `oc' boolean is specified, +indicating that upper and lower case are equivalent, +then all the keywords should be specified in lower case. +.SH EXAMPLE +The following entry, +which describes the C language, +is typical of a language entry. +.IP +.ft B +.nf +C\^|\^the C programming language:\e + :pb=^\ed?*?\ed?\ep\ed?\(\ea?\):bb={:be=}:cb=/*:ce=*/:\e + :sb=":se=\ee":lb=':le=\ee':tl:\e + :zb=@:ze=@:tb=%%:te=%%:mb=%\e$:me=\e$%:vb=%\e\^|:ve=\e\^|%:\e + :kw=asm auto break case char continue default do double\e + else enum extern float for fortran goto if int long\e + register return short sizeof static struct switch typedef\e + union unsigned while #define #else #endif #if #ifdef\e + #ifndef #include #undef # define else endif if ifdef\e + ifndef include undef: +.fi +.ft +.PP +Note that the first field is just the language name +(and any variants of it). +Thus the C language could be specified to +.BR lgrind (1) +as `c' or `C', since case is not significant here. +.SH FILES +.ta \w'/usr/TeX/texmf/tex/latex/lgrind/lgrindef 'u +\fB/usr/TeX/texmf/tex/latex/lgrind/lgrindef\fR file containing terminal descriptions +.SH "SEE ALSO" +.BR latex (1), +.BR lgrind (1), +.BR vgrindefs (5), +For full documentation, refer to the package itself; it comes as a .dtx +containing both the documentation and the LaTeX-files. + diff --git a/support/lgrind/source/lgrindef.c b/support/lgrind/source/lgrindef.c new file mode 100644 index 0000000000..c096010a97 --- /dev/null +++ b/support/lgrind/source/lgrindef.c @@ -0,0 +1,330 @@ +#ifndef lint +static char sccsid[] = "@(#)lgrindef.c 4.3 (Berkeley) 11/12/84"; +static char rcsid[] = + "$Id: lgrindef.c,v 1.3 1999/05/28 11:00:16 mike Exp $"; +#endif + +/* Copyright %%\copyright%% 1979 Regents of the University of California + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#define MAXHOP 32 /* max number of %|tc=|% indirections */ + +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <string.h> +#include "lgrindef.h" + +/* + * grindcap --- routines for dealing with the language definitions data base + * (code stolen almost totally from termcap) + * + * BUG: Should use a "last" pointer in @tbuf@, so that searching + * for capabilities alphabetically would not be a %$n^2/2$% + * process when large numbers of capabilities are given. + * Note: If we add a last pointer now we will screw up the + * %|tc|% capability. We really should compile termcap. + * + * Essentially all the work here is scanning and decoding escapes + * in string capabilities. We don't use stdio because the editor + * doesn't, and because living w/o it is not hard. + */ + +static char *tbuf; +static char *filename; +static int hopcount = 0; /* detect infinite loops in termcap */ + +static char *tskip(char *); +static char *tdecode(char *, char **); + +/* + * @tnchktc@: check the last entry, see if it's %|tc=xxx|%. If so, + * recursively find %|xxx|% and append that entry (minus the names) + * to take the place of the %|tc=xxx|% entry. This allows termcap + * entries to say "like an HP2621 but doesn't turn on the labels". + * Note that this works because of the left to right scan. + */ +static int tnchktc(void) +{ + register char *p, *q; + char tcname[16]; /* name of similar terminal */ + char tcbuf[BUFFERSIZE]; + char *holdtbuf = tbuf; + int l; + + p = tbuf + strlen(tbuf) - 2; /* before the last colon */ + while (*--p != ':') + if (p < tbuf) { + /* Not very informative. Wrong BTW (for the configs). Shut up. */ + /* write(2, "Bad lgrind entry\n", 18); */ + return 0; + } + p++; + /* @p@ now points to beginning of last field */ + if (p[0] != 't' || p[1] != 'c') + return 1; + strcpy(tcname,p+3); + q = tcname; + while (q && *q != ':') + q++; + *q = '\0'; + if (++hopcount > MAXHOP) { + fprintf(stderr, "Infinite tc= loop\n"); + return 0; + } + if (tgetent(tcbuf, tcname, filename) != 1) + return 0; + for (q = tcbuf; *q != ':'; q++) + ; + l = (int)(p - holdtbuf + strlen(q)); + if (l > BUFFERSIZE) { + fprintf(stderr, "LGrind entry too long\n"); + q[(int)(BUFFERSIZE - (p-tbuf))] = '\0'; + } + strcpy(p, q+1); + tbuf = holdtbuf; + return 1; +} + + +/* + * @tnamatch@ deals with name matching. The first field of the termcap + * entry is a sequence of names separated by %|||%'s, so we compare + * against each such name. The normal %|:|% terminator after the last + * name (before the first field) stops us. + */ +static int tnamatch(char *np) +{ + register char *Np, *Bp; + + Bp = tbuf; + if (*Bp == '#') + return 0; + for (;;) { + for (Np = np; *Np && toupper(*Bp) == toupper(*Np); Bp++, Np++) + continue; + if (*Np == '\0' && (*Bp == '|' || *Bp == ':' || *Bp == '\0')) + return 1; + while (*Bp != '\0' && *Bp != ':' && *Bp != '|') + Bp++; + if (*Bp == '\0' || *Bp == ':') + return 0; + Bp++; + } +} + + +/* + * Skip to the next field. Notice that this is very dumb, not + * knowing about %|\:|% escapes or any such. If necessary, %|:|%'s can + * be put into the termcap file in octal. + */ +static char *tskip(register char *bp) +{ + while (*bp && *bp != ':') + bp++; + if (*bp == ':') + bp++; + return bp; +} + + +/* + * Return the (numeric) option id. + * Numeric options look like + * %|li#80|% + * i.e. the option string is separated from the numeric value by + * a %|#|% character. If the option is not found we return %$-1$%. + * Note that we handle octal numbers beginning with %$0$%. + */ +int tgetnum(char *id) +{ + register int i, base; + register char *bp = tbuf; + + for (;;) { + bp = tskip(bp); + if (*bp == '\0') + return -1; + if (*bp++ != id[0] || *bp == '\0' || *bp++ != id[1]) + continue; + if (*bp == '@') + return -1; + if (*bp != '#') + continue; + bp++; + base = 10; + if (*bp == '0') + base = 8; + i = 0; + while (isdigit(*bp)) + i *= base, i += *bp++ - '0'; + return i; + } +} + + + +/* + * Handle a flag option. + * Flag options are given "naked", i.e. followed by a %|:|% or the end + * of the buffer. Return 1 if we find the option, or 0 if it is + * not given. + */ +int tgetflag(char *id) +{ + register char *bp = tbuf; + + for (;;) { + bp = tskip(bp); + if (!*bp) + return 0; + if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) { + if (!*bp || *bp == ':') + return 1; + else if (*bp == '@') + return 0; + } + } +} + + +/* + * Get a string valued option. + * These are given as + * %|cl=^Z|% + * Much decoding is done on the strings, and the strings are + * placed in area, which is a ref parameter which is updated. + * No checking on area overflow. + */ +char *tgetstr(char *id, char **area) +{ + register char *bp = tbuf, *i; + register int eq; + + for (;;) { + bp = tskip(bp); + if (!*bp) + return 0; + eq=1; + for (i=id; *i;) + if (*i++!=*bp++) { eq=0; break; } + if (!eq) + continue; + if (*bp == '@') + return 0; + if (*bp != '=') + continue; + bp++; + return tdecode(bp, area); + } +} + + +/* + * @tdecode@ does the grunge work to decode the + * string capability escapes. + */ +static char *tdecode(register char *str, char **area) +{ + char *cp = *area, c; + + while ((c = *str++) != 0) { + if (c == ':') { + if (*(cp-1) == '\\') + cp--; + else + break; + } + *cp++ = c; + } + *cp++ = '\0'; + str = *area; + *area = cp; + return str; +} + +/* + * Get an entry for terminal name in buffer @bp@, + * from the termcap file. Parse is very rudimentary; + * we just notice escaped newlines. + */ +int tgetent(char *bp, char *name, char *file) +{ + register char *cp; + register char c; /* 30.7.96 -- tryal [sic] */ + register int i = 0, cnt = 0; + char ibuf[BUFFERSIZE]; + FILE *tf; + + tbuf = bp; + tf = 0; + filename = file; + tf = fopen(filename, "rt"); + if (tf == NULL) + return -1; + for (;;) { + cp = bp; + for (;;) { + if (i == cnt) { + cnt = fread(ibuf, 1, BUFFERSIZE, tf); + if (cnt <= 0) { + fclose(tf); + return 0; + } + i = 0; + } + c = ibuf[i++]; + if (c == '\n') { + if (cp > bp && cp[-1] == '\\'){ + cp--; + continue; + } + break; + } + if (cp >= bp+BUFFERSIZE) { + fprintf(stderr, "LGrind entry too long\n"); + break; + } else + *cp++ = c; + } + *cp = '\0'; + + /* + * The real work for the match. + */ + if (tnamatch(name)) { + fclose(tf); + return tnchktc(); + } + } +} diff --git a/support/lgrind/source/lgrindef.h b/support/lgrind/source/lgrindef.h new file mode 100644 index 0000000000..f213d9b948 --- /dev/null +++ b/support/lgrind/source/lgrindef.h @@ -0,0 +1,12 @@ +/* sccsid[] = "@(#)lgrindef.h 3.6 (MPi) 3/8/98"; + * rcsid[] = + * "$Id: lgrindef.h,v 1.3 1999/05/26 07:30:03 mike Exp $"; + * + * Created 19 February 1996 + */ + +#define BUFFERSIZE 8192 +int tgetent(char *bp, char *name, char *file); +int tgetnum(char *id); +int tgetflag(char *id); +char *tgetstr(char *id, char **area); diff --git a/support/lgrind/source/lgutil.c b/support/lgrind/source/lgutil.c new file mode 100644 index 0000000000..121ba366a0 --- /dev/null +++ b/support/lgrind/source/lgutil.c @@ -0,0 +1,323 @@ +#ifndef lint +#ifdef standalone_lgutil +static char sccsid[] = "@(#)lgutil.c 3.6 (MPi) 24/3/98"; +static char rcsid[] = + "$Id: lgutil.c,v 1.7 1999/12/18 21:54:29 mike Exp $"; +#endif /* standalone_lgutil */ +#endif + +/* lgutil.c + * Utility Functions for LGrind + * This file is @#include@d from lgrind.c (likely to change) + */ + +/* Copyright %%\copyright%% 1998,1999 Michael Piefel + * + * This file is something like GPL, though LGrind is BSD. + * I don't want to become a lawyer. + */ + +/* + * Internal_Help() --- show help + */ + +void Internal_Help() +{ + printf("lgrind -- general purpose \"pretty printer\" for use with LaTeX.\n"); + printf("usage: lgrind [options] <name> ...\n"); + printf("-e process embedded text in a LaTeX file\n"); + printf("-i format source code for inclusion in a LaTeX document.\n"); + printf("-n don't boldface keywords.\n"); + printf("-a don't treat @, etc. specially in LaTeX.\n"); + printf("-c don't treat @, etc. specially in comments.\n"); + printf("- take input from standard input.\n"); + printf("-o <file> write to file (instead of standard output).\n"); + printf("-t <width> change tab width (default 8).\n"); + printf("-h <header> specifies text in header (default: none).\n"); + printf("-v <varfile> take variable substitutions from file.\n"); + printf("-d <deffile> use a different language definitions file, default:\ +\n \"%s\"\n", defsfile); + printf("-l<language> choose the language to use.\n"); + printf("-s show a list of currently known languages.\n\n"); + printf("If neither -e nor -i are specified, a complete LaTeX-file is produced.\n\n"); + printf("Direct comments and questions to lgrind@gmx.net\n"); +} + + +typedef struct langRec { + char *name; + char *alternatives; + struct langRec *next; } langRec; + +void AddToLList(char *line, langRec **into, int *chainLength) +{ + char *token, *item, *alts; + + if (*line!='#' && *line!='\n') + { + item=strdup(line); + if ((token=strtok(item, "|:\\\n")) == NULL) return; + (**into).next=(langRec*)malloc(sizeof(langRec)); + *into=(**into).next; + (**into).name=strdup(token); + (*chainLength)++; + if ((token=strtok(NULL, "|:\\\n")) == NULL) + { + (**into).alternatives="\0"; + return; + } + (**into).alternatives=strdup(token); + while ((token=strtok(NULL, "|:\\\n")) != NULL) + { + alts=(**into).alternatives; + (**into).alternatives=(char*)malloc( + strlen((**into).alternatives)+strlen(token)+1); + /* This is extremely poor style and dangerous. I leave memory + * allocated here because after invocation of this help facility + * the program will stop anyway. In this context it is safe. */ + strcpy((**into).alternatives, alts); + strcat((**into).alternatives, ", "); + strcat((**into).alternatives, token); + } + } +} + +void Internal_Help_Language_List() +{ + FILE *tf; + int check, i, llch; + langRec *lch, *rch, *currlch; + /* @lch2@ was originally "language chain", but I guess + it's "left" as well */ + + printf("When specifying a language case is insignificant. You can use the\n"); + printf("name of the language, or, where available, one of the synonyms in\n"); + printf("parantheses. Thus the following are legal and mark Tcl/Tk, Pascal\n"); + printf("and Fortran input, respectively:\n"); + printf(" lgrind -ltcl/tk ...\n"); + printf(" lgrind -lpaSCAL ...\n"); + printf(" lgrind -lf ...\n"); + printf("The list of languages currently available in your lgrindef file:\n"); + tf = fopen(defsfile, "rt"); + if (tf == NULL) + { + fprintf(stderr, "cannot find lgrindef file `%s'\n", defsfile); + return; + } + + llch=0; + currlch=lch=(langRec*)malloc(sizeof(langRec)); + (*currlch).name=""; + *config='\n'; + do { + check=0; + if (*config=='\n' || config[strlen(config)-2]!='\\') + check=1; + if (fgets(config, BUFFERSIZE, tf)==NULL) break; + if (check!=0) AddToLList(config, &currlch, &llch); + } while (strcmp((*currlch).name, "EndOfLanguageDefinitions")!=0); + rch=lch; + for (check=0; check<llch/2; check++) + rch=(*rch).next; + for (check=0; check<llch/2; check++) + { + lch=(*lch).next; + rch=(*rch).next; + printf(" %s", (*lch).name); + if (*((*lch).alternatives)) printf(" (%s)", (*lch).alternatives); + else printf(" "); + for (i=strlen((*lch).name)+strlen((*lch).alternatives); + i<30; i++) putchar(' '); + if (rch!=currlch) + { + printf("%s", (*rch).name); + if (*((*rch).alternatives)) printf(" (%s)", (*rch).alternatives); + } + printf("\n"); + } +} + + +/* + * Redefine Chartab from lgrindef file + */ +void parsechartab(void) +{ + char *i=buf, *j=chartab, c; + int charnum=0; + + while (*i++!=':') {} + while ((c=*i++)!='\0') { + if (c==':') continue; + charnum=(c>='0' && c<='9') ? (int)c-48 : tolower(c)-87; + charnum=charnum << 4; c=*i++; + charnum+=(c>='0' && c<='9') ? (int)c-48 : tolower(c)-87; + i++; + + printtab[charnum]=j; + while (*i!=':') + if (*i=='\\') { + switch (*++i) { + case ':': *j++=':'; break; + case '\\': *j++='\\'; break; + }; i++; + } else *j++=*i++; + *j++='\0'; + } + +} + +/* + * Parses the %|lgrindef|% file for a preamble + */ +void parsepreamble(char *destination) +{ + char *i=buf, *j=destination; + + while (*i++!=':') ; + while (*i) { + if (*i=='\\') { + switch (*++i) { + case 'n': *j++='\n'; break; + case 't': *j++='\t'; break; + case 'f': *j++='\f'; break; + case '\\': *j++='\\'; break; + default: fprintf(stderr, "Bad preamble entry: incorrect '\\'\n"); + }; i++; + } else *j++=*i++; + *j='\0'; + } +} + +/* + * Sets the default preambles + */ +void setpreambles(void) +{ + strcpy(preamble, +"\\documentclass[a4paper]{article}\n\ +\\usepackage[procnames,noindent]{lgrind}\n\ +\\usepackage{fancyhdr}\n\ +\\pagestyle{fancy}\n\ +\\begin{document}\n"); + strcpy(postamble, "\\end{document}\n"); + strcpy(preamble2, +"\\lhead[\\fancyplain{}{\\bf\\thepage}]\ +{\\fancyplain{}{\\bf \f}}\n\ +\\rhead[\\fancyplain{}{\\bf \f}]\ +{\\fancyplain{}{\\bf\\thepage}}\n\ +\\cfoot{}\n"); + strcpy(config, ""); +} + +/* + * prints a preamble, substituting %|\f|% with the current + * file's name (sorry, no more form feeds) + */ +void printpreamble(char *string) +{ + char *i; + while (*string) + if (*string=='\f') + { i=fname-1; + while (*(++i)) outchar(*i); + string++; + } + else putchar(*string++); +} + +/* + * try to create an executable file to store the new lgrindef-file + */ +void writeDefsfileInExe(char *progname, char *newdefsfile) +{ + char *buffer, *i; + FILE *progfile, *newfile; + long pos=0; + int ret; + + buffer = (char*) malloc(30000); + /* So small? Why in chunks? Well, it's gotta run on machines with + only limited access to their resources (you know, the IBM PC) */ + progfile=fopen(progname, "rb"); + newfile=fopen("lgrind.new", "wb"); + + if (buffer && progfile && newfile) + { + fread(buffer, 1, 200, progfile); + do { + ret=fread(buffer+200, 1, 29500, progfile); + for(i=buffer; i<buffer+ret; i++) + if (*i=='D' && + *(i+1)=='e' && + *(i+2)=='F' && + *(i+3)=='s' && + *(i+4)=='B' && + *(i+5)=='u' && + *(i+6)=='F' && + strcmp(defsfile, i+7)==0) { + memcpy(i+7, newdefsfile, strlen(newdefsfile)+1); + } + fwrite(buffer, 1, ret==29500 ? ret : ret+200, newfile); + memcpy(buffer, buffer+29500, 200); + pos+=29500; + } while (ret==29500); + fclose(progfile); + fclose(newfile); + } + free(buffer); +} + +/* + * Reads a variable substitution table from a file + */ +varsubst *parsevartab(char* fname) +{ + FILE *f = fopen(fname, "rt"); + char linebuf[201], *cp; + varsubst *varsubsts=NULL, *substitem, *substlistpos; + + if (f==NULL) return; + while (!feof(f)) + { + fscanf(f, "%200[^\n]", linebuf); + fscanf(f, "\n"); + cp=strchr(linebuf, '='); + if (cp==NULL) continue; + *cp++='\0'; + substitem=(varsubst*)malloc(sizeof(varsubst)); + substitem->var=strdup(linebuf); + substitem->subst=strdup(cp); + substitem->next=NULL; + if (varsubsts==NULL) + { + varsubsts=substitem; + substlistpos=substitem; + } else { + substlistpos->next=substitem; + substlistpos=substitem; + } + } + fclose(f); + return varsubsts; +} + +/* + * Replaces a variable by alternate representation + */ +int substvarname(char **ident, int i) +{ + varsubst *substlistpos = varsubstlist; + + while (substlistpos!=NULL) + { + if (re_strncmp(*ident, substlistpos->var, i)==0) + { + *ident+=i; /* skip i characters in input */ + printf("%s", substlistpos->subst); + return 0; + } + substlistpos=substlistpos->next; + } + return i; +} diff --git a/support/lgrind/source/makefile.dos b/support/lgrind/source/makefile.dos new file mode 100644 index 0000000000..540e1337e7 --- /dev/null +++ b/support/lgrind/source/makefile.dos @@ -0,0 +1,34 @@ +# Makefile for lgrind, a LaTeX prettyprinter + +# $Id: makefile.dos,v 1.1.1.1 1999/05/13 13:02:55 mike Exp $ + +# You will possibly want to change this +DEFSFILE=$(EMTEXDIR:\\=\\\\)\\\\texinput\\\\lgrind\\\\lgrindef +# if you encounter problems with the last line, enter the path +# directly; if all else fails, modify lgrind.c + +CC=bcc +SOURCES=lgrind.c regexp.c regexp.h lgrindef.c lgrindef.h retest.c v2lg.c +TEXFILES=lgrind.dtx lgrind.sty lgrind.ins +CMDS=lgrind.exe +OBJS=lgrind.obj regexp.obj retest.obj lgrindef.obj v2lg.obj + +HELPOPTION=-? +# popen? pclose? This is DOS! +#CFLAGS=-v -mc -Dpopen=fopen -Dpclose=fclose +CFLAGS=-O2 -mc -Dpopen=fopen -Dpclose=fclose +# Well, the DEFSFILE is still (semi-)fixed in the EXE... + +.c.obj: + $(CC) $(CFLAGS) -DDEFSFILE="$(DEFSFILE)" -DHELPOPTION="$(HELPOPTION)" -c $< + +all: $(CMDS) + +lgrind.exe: lgrind.obj lgrindef.obj regexp.obj + $(CC) $(CFLAGS) lgrind.obj lgrindef.obj regexp.obj + +v2lg: v2lg.c + $(CC) $(CFLAGS) v2lg + +retest: retest.c regexp.c + $(CC) $(CFLAGS) retest regexp diff --git a/support/lgrind/source/makefile.emx b/support/lgrind/source/makefile.emx new file mode 100644 index 0000000000..3cbdbbb381 --- /dev/null +++ b/support/lgrind/source/makefile.emx @@ -0,0 +1,55 @@ +# Makefile for lgrind, a LaTeX prettyprinter +# for EMX+GCC with DMAKE, bound DOS-extender + +# $Id: makefile.emx,v 1.1.1.1 1999/05/13 13:02:55 mike Exp $ + +# You will almost certainly want to change these +DESTDIR=c:\\emtex\\bin +DEFSFILE=c:\\emtex\\texinput\\lgrind\\lgrindef +TEXINPUTS=c:\\emtex\\texinput\\lgrind + +SOURCES=lgrind.c regexp.c regexp.h lgrindef.c lgrindef.h retest.c v2lg.c +TEXFILES=lgrind.doc lgrind.sty lgrind.ins +MANPAGES=lgrind.1 lgrindef.5 +EXAMPLES=doc-lgrind.lg +CMDS=lgrind.exe +OBJS=lgrind.o regexp.o retest.o lgrindef.o v2lg.o + +CC=gcc +HELPOPTION=-? +CFLAGS=-O2 -DOS2 +# CFLAGS=-g +# CFLAGS=-O2 -Dpopen=fopen -Dpclose=fclose +# There are operating systems and compilers without these... + +.SUFFIXES: .o .c + +.c.o: + $(CC) -c $(CFLAGS) $< + +all: $(CMDS) + +lgrind.exe: lgrind.o lgrindef.o regexp.o + $(CC) $(CFLAGS) -o lgrind.exe lgrind.o lgrindef.o regexp.o -s +# alternatively you can bind the DOS-extender permanently: +# $(CC) $(CFLAGS) -o lgrind lgrind.o lgrindef.o regexp.o -s +# emxbind c:\emx\bin\emx.exe lgrind lgrind.exe -p + +lgrind.o: lgrind.c + $(CC) $(CFLAGS) -DDEFSFILE=\"${DEFSFILE}\" -DHELPOPTION=\"${HELPOPTION}\"\ + -c lgrind.c + +v2lg.exe: v2lg.o + $(CC) $(CFLAGS) -o v2lg.exe v2lg.o + +retest.exe: retest.o regexp.o + $(CC) $(CFLAGS) -o retest.exe retest.o regexp.o + +install: all + -copy lgrind.exe $(DESTDIR)\\lgrind.exe + -copy lgrindef $(DEFSFILE) + -copy lgrind.dtx $(TEXINPUTS)\\lgrind.dtx + -copy lgrind.sty $(TEXINPUTS)\\lgrind.sty + +clean: + -del $(CMDS) $(OBJS) retest.exe v2lg.exe diff --git a/support/lgrind/source/regexp.c b/support/lgrind/source/regexp.c new file mode 100644 index 0000000000..87b19433ce --- /dev/null +++ b/support/lgrind/source/regexp.c @@ -0,0 +1,648 @@ +#ifndef lint +static char sccsid[] = "@(#)regexp.c 1.2 (LBL) 12/4/85"; +static char rcsid[] = + "$Id: regexp.c,v 1.3 1999/05/27 16:17:43 mike Exp $"; +#endif + +/* + * Copyright %%\copyright%% 1980 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Regular expression matching routines for lgrind/tgrind/tfontedpr. + * + * These routines were written by Dave Presotto (I think) for vgrind. + * Minor mods & attempts to improve performance by Van Jacobson + * (van@@lbl-rtsg) and Chris Torek (chris@@maryland). + * + * Modifications. + * -------------- + * Sep 91 George V Reilly Fixed up some bogus uses of @NIL@ and @NULL@. + * 30 Mar 85 Van & Chris Changed @expmatch()@ to return pointer to + * start of what was matched in addition to + * pointer to match end. Several changes to + * improve performance (too numerous to mention). + * 11 Dec 84 Dave Presotto Written. + */ + + +#include <stdio.h> +#include <malloc.h> +#include <ctype.h> +#include <string.h> +#include "regexp.h" + +#define makelower(c) (isupper((c)) ? tolower((c)) : (c)) + +extern char *l_id; +static void expconv(void); /* forward declaration */ + +int (*re_strncmp)(const char *, const char *, size_t); + /* function used by @expmatch()@ to compare + * strings. The caller should make it point to + * @strncmp()@ if case is significant & + * @lc_strncmp()@ otherwise. + */ + + +/* @lc_strncmp()@ --- like @strncmp()@ except that we convert the + * first string to lower case before comparing. + */ +int lc_strncmp(register const char *s1, register const char *s2, register size_t len) +{ + while (len-- > 0) + if (*s2 - makelower(*s1)) + return 1; + else + s2++, s1++; + + return 0; +} + + +/* The following routine converts an irregular expression to + * internal format. + * + * Either meta symbols (%|\a|%, %|\d|%, or %|\p|%) or character strings + * or operations (alternation or parenthesizing) can be specified. + * Each starts with a descriptor byte. The descriptor byte + * has @STR@ set for strings, @META@ set for meta symbols, + * and @OPER@ set for operations. The descriptor byte can also have + * the @OPT@ bit set if the object defined is optional. Also @ALT@ + * can be set to indicate an alternation. + * + * For metasymbols, the byte following the descriptor byte identities + * the meta symbol (containing an ASCII @'a'@, @'d'@, @'p'@, @'|'@, + * or @'('@). For strings, the byte after the descriptor is a + * character count for the string: + *@ + * meta symbols := descriptor + * symbol + * + * strings := descriptor + * character count + * the string + * + * operations := descriptor + * symbol + * character count@ + */ + + +/* + * handy macros for accessing parts of match blocks + */ +#define MSYM(A) (*(A+1)) /* symbol in a meta symbol block */ +#define MNEXT(A) (A+2) /* character following a metasymbol block */ + +#define OSYM(A) (*(A+1)) /* symbol in an operation block */ +#define OCNT(A) (*(A+2)) /* character count */ +#define ONEXT(A) (A+3) /* next character after the operation */ +#define OPTR(A) (A+*(A+2)) /* place pointed to by the operator */ + +#define SCNT(A) (*(A+1)) /* byte count of a string */ +#define SSTR(A) (A+2) /* address of the string */ +#define SNEXT(A) (A+2+*(A+1)) /* character following the string */ + + +/* + * bit flags in the descriptor + */ +#define OPT 1 +#define STR 2 +#define META 4 +#define ALT 8 +#define OPER 16 + +char *ure; /* pointer current position in unconverted exp */ +char *ccre; /* pointer to current position in converted exp */ + + + +/* + * Convert an irregular expression to the internal form + */ +char *convexp(char *re) +/* re unconverted irregular expression */ +{ + register char *cre; /* pointer to converted regular expression */ + + /* allocate room for the converted expression */ + if (re == NULL) + return NULL; + if (*re == '\0') + return NULL; + cre = (char*)malloc(4 * strlen(re) + 3); + ccre = cre; + ure = re; + + /* start the conversion with a %|\a|% */ + *cre = META | OPT; + MSYM(cre) = 'a'; + ccre = MNEXT(cre); + + /* start the conversion (it's recursive) */ + expconv(); + *ccre = 0; + return cre; +} + + +/* + * Actually do the conversion + */ +static void expconv(void) +{ + register char *cs; /* pointer to current symbol in converted exp */ + register char c; /* character being processed */ + register char *acs; /* pointer to last alternate */ + register int temp; + + /* let the conversion begin */ + acs = NULL; + cs = NULL; + while (*ure != '\0') { + switch (c = *ure++) { + + case '\\': + switch (c = *ure++) { + + /* escaped characters are just characters */ + default: + if (cs == NULL || (*cs & STR) == 0) { + cs = ccre; + *cs = STR; + SCNT(cs) = 1; + ccre += 2; + } else + SCNT(cs)++; + *ccre++ = c; + break; + + /* normal(?) metacharacters */ + case 'a': + case 'd': + case 'e': + case 'p': + if (acs != NULL && acs != cs) { + do { + temp = OCNT(acs); + OCNT(acs) = ccre - acs; + acs -= temp; + } while (temp != 0); + acs = NULL; + } + cs = ccre; + *cs = META; + MSYM(cs) = c; + ccre = MNEXT(cs); + break; + } + break; + + /* just put the symbol in */ + case '^': + case '$': + if (acs != NULL && acs != cs) { + do { + temp = OCNT(acs); + OCNT(acs) = ccre - acs; + acs -= temp; + } while (temp != 0); + acs = NULL; + } + cs = ccre; + *cs = META; + MSYM(cs) = c; + ccre = MNEXT(cs); + break; + + /* mark the last match sequence as optional */ + case '?': + if (cs) + *cs = *cs | OPT; + break; + + /* recurse and define a subexpression */ + case '(': + if (acs != NULL && acs != cs) { + do { + temp = OCNT(acs); + OCNT(acs) = ccre - acs; + acs -= temp; + } while (temp != 0); + acs = NULL; + } + cs = ccre; + *cs = OPER; + OSYM(cs) = '('; + ccre = ONEXT(cs); + expconv(); + OCNT(cs) = ccre - cs; /* offset to next symbol */ + break; + + /* return from a recursion */ + case ')': + if (acs != NULL) { + do { + temp = OCNT(acs); + OCNT(acs) = ccre - acs; + acs -= temp; + } while (temp != 0); + acs = NULL; + } + cs = ccre; + *cs = META; + MSYM(cs) = c; + ccre = MNEXT(cs); + return; + + /* Mark the last match sequence as having an alternate. */ + /* The third byte will contain an offset to jump over the */ + /* alternate match in case the first did not fail */ + case '|': + if (acs != NULL && acs != cs) + OCNT(ccre) = ccre - acs; /* make a back pointer */ + else + OCNT(ccre) = 0; + *cs |= ALT; + cs = ccre; + *cs = OPER; + OSYM(cs) = '|'; + ccre = ONEXT(cs); + acs = cs; /* remember that the pointer is to be filled */ + break; + + /* if it's not a metasymbol, just build a character string */ + default: + if (cs == NULL || (*cs & STR) == 0) { + cs = ccre; + *cs = STR; + SCNT(cs) = 1; + ccre = SSTR(cs); + } else + SCNT(cs)++; + *ccre++ = c; + break; + } + } + if (acs != NULL) { + do { + temp = OCNT(acs); + OCNT(acs) = ccre - acs; + acs -= temp; + } while (temp != 0); + acs = NULL; + } + return; +} /* end of @expconv()@ */ + + +/* + * The following routine recognises an irregular expression + * with the following special characters: + * + * %|\?|% means last match was optional + * %|\a|% matches any number of characters + * %|\d|% matches any number of spaces and tabs + * %|\p|% matches any number of alphanumeric characters. + * The characters matched will be copied into + * the area pointed to by @mstring@. + * %|\||% alternation + * %|\( \)|% grouping used mostly for alternation and + * optionality + * + * The irregular expression must be translated to internal form + * prior to calling this routine + * + * The value returned is the pointer to the last character matched. + * If @strtptr@ is non-@NULL@, a pointer to the start of the string matched + * (excluding %|\a|% matches) will be returned at @*strtptr@. + * If @mstring@ is non-@NULL@, the string matched by a %|\p|% will be copied + * into @mstring@. + */ + +boolean _escaped; /* true if we are currently @_escaped@ */ +char *_sstart; /* start of string. Set in @putScp()@. */ + + +char *expmatch( + register char *s, /* string to check for a match in */ + register char *re, /* a converted irregular expression */ + char **strtptr, /* where to put ptr to start of match */ + char *mstring) /* where to put whatever matches a %|\p|% */ +{ + register char *cs; /* the current symbol */ + register char *ptr, *s1; /* temporary pointer */ + register char c; /* temporary */ + boolean matched; /* a temporary @boolean@ */ + + /* initial conditions */ + if (strtptr) + *strtptr = '\0'; + if (re == NULL) + return NULL; + cs = re; + matched = FALSE; + + /* loop till expression string is exhausted (or at least pretty tired) */ + while (*cs) { + switch (*cs & (OPER | STR | META)) { + + /* try to match a string */ + case STR: + matched = !((*re_strncmp)(s, SSTR(cs), SCNT(cs))); + if (matched) { + + /* hoorah: it matches */ + s += SCNT(cs); + cs = SNEXT(cs); + } else if (*cs & ALT) { + + /* alternation: skip to next expression */ + cs = SNEXT(cs); + } else if (*cs & OPT) { + + /* the match is optional */ + cs = SNEXT(cs); + matched = 1; /* indicate a successful match */ + } else { + + /* no match: error return */ + return NULL; + } + break; + + /* an operator: do something fancy */ + case OPER: + switch (OSYM(cs)) { + + /* this is an alternation */ + case '|': + if (matched) + + /* last thing in the alternation was a match: skip ahead */ + cs = OPTR(cs); + else + + /* no match: keep trying */ + cs = ONEXT(cs); + break; + + /* this is a grouping: recurse */ + case '(': + ptr = expmatch(s, ONEXT(cs), strtptr, mstring); + if (ptr != NULL) { + + /* the subexpression matched */ + matched = 1; + s = ptr; + } else if (*cs & ALT) { + + /* alternation: skip to next expression */ + matched = 0; + } else if (*cs & OPT) { + + /* the match is optional */ + matched = 1; /* indicate a successful match */ + } else { + + /* no match: error return */ + return NULL; + } + cs = OPTR(cs); + break; + } + break; + + /* try to match a metasymbol */ + case META: + switch (MSYM(cs)) { + + /* try to match anything and remember what was matched */ + case 'p': + /* + * This is really the same as trying the match the + * remaining parts of the expression to any subset + * of the string. + */ + s1 = s; + do { + ptr = expmatch(s1, MNEXT(cs), strtptr, mstring); + if (ptr != NULL && s1 != s) { + + /* we have a match: remember the match */ + if (mstring) { + strncpy(mstring, s, (size_t)(s1 - s)); + mstring[(int)(s1 - s)] = '\0'; + } + return ptr; + + } else if (ptr != NULL && (*cs & OPT)) { + + /* %|\p|% was optional, so no match is ok */ + return ptr; + + } else if (ptr != NULL) { + + /* not optional and we still matched */ + return NULL; + } + if (!isalnum(*s1) && !strchr(l_id, *s1)) + return NULL; + if (*s1 == '\\') + _escaped = _escaped ? FALSE : TRUE; + else + _escaped = FALSE; + } while (*s1++); + return NULL; + + /* try to match anything */ + case 'a': + /* + * This is really the same as trying the match the + * remaining parts of the expression to any subset + * of the string. + */ + s1 = s; + do { + /* + * Hack for an important special case: if the next thing + * in the pattern is a string, just gobble characters until + * we find something that matches that string (this saves + * the cost of a recursive call on @expmatch()@ while scanning + * for the start of comments or strings). Since many + * patterns end with a string, we also treat that as a + * special case. + */ + if(*(ptr = MNEXT(cs)) == STR) { + c = *SSTR(ptr); + while(*s1 && *s1 != c) + s1++; + + if (*s1 == 0) + return NULL; + + if (SNEXT(ptr) == 0 && (s1 != s || *cs & OPT)) { + /* next item is a string, it's the last item and + * the %|\a|% match is ok --- just loop to try & match + * the string. + */ + if (strtptr) + *strtptr = s1; + + cs = ptr; + s = s1; + break; + } + } + ptr = expmatch(s1, MNEXT(cs), strtptr, mstring); + if (ptr != NULL && (s1 != s || *cs & OPT)) { + + /* we have a match */ + if (strtptr) + *strtptr = s1; + + return ptr; + + } else if (ptr != NULL) { + + /* not optional and we still matched */ + return NULL; + } + if (*s1 == '\\') + _escaped = _escaped ? FALSE : TRUE; + else + _escaped = FALSE; + } while (*s1++); + return NULL; + + /* fail if we are currently @_escaped@ */ + case 'e': + if (_escaped) + return NULL; + cs = MNEXT(cs); + break; + + /* match any number of tabs and spaces */ + case 'd': + ptr = s; + while (*s == ' ' || *s == '\t') + s++; + + if (s != ptr || s == _sstart) { + + /* match: be happy */ + matched = 1; + cs = MNEXT(cs); + } else if (*s == '\n' || *s == '\0') { + + /* match: be happy */ + matched = 1; + cs = MNEXT(cs); + } else if (*cs & ALT) { + + /* try the next part */ + matched = 0; + cs = MNEXT(cs); + } else if (*cs & OPT) { + + /* doesn't matter */ + matched = 1; + cs = MNEXT(cs); + } else + + /* no match: error return */ + return NULL; + + break; + + /* check for end of line */ + case '$': + if (*s == '\0' || *s == '\n') { + + /* match: be happy */ + /* s++; was here + * removed; now $ is left in stream to be matched again, which + * is not bad (EOL can end several things at once. Also it + * leaves things like CE on the same line. + */ + matched = 1; + cs = MNEXT(cs); + } else if (*cs & ALT) { + + /* try the next part */ + matched = 0; + cs = MNEXT(cs); + } else if (*cs & OPT) { + + /* doesn't matter */ + matched = 1; + cs = MNEXT(cs); + } else + + /* no match: error return */ + return NULL; + break; + + /* check for start of line */ + case '^': + if (s == _sstart) { + + /* match: be happy */ + matched = 1; + cs = MNEXT(cs); + } else if (*cs & ALT) { + + /* try the next part */ + matched = 0; + cs = MNEXT(cs); + } else if (*cs & OPT) { + + /* doesn't matter */ + matched = 1; + cs = MNEXT(cs); + } else + + /* no match: error return */ + return NULL; + break; + + /* end of a subexpression: return success */ + case ')': + return s; + } + break; + } + } + return s; +} diff --git a/support/lgrind/source/regexp.h b/support/lgrind/source/regexp.h new file mode 100644 index 0000000000..048a2840e5 --- /dev/null +++ b/support/lgrind/source/regexp.h @@ -0,0 +1,13 @@ +/* sccsid[] = "@(#)regexp.h 3.6 (MPi) 3/8/98"; + * rcsid[] = + * "$Id: regexp.h,v 1.3 1999/05/26 07:30:03 mike Exp $"; + * + * Created 19 February 1996 + */ + +typedef int boolean; +#define TRUE 1 +#define FALSE 0 +int lc_strncmp(const char *s1, const char *s2, size_t len); +char *convexp(char *re); +char *expmatch(register char *s, register char *re, char **strtptr, char *mstring); diff --git a/support/lgrind/source/retest.c b/support/lgrind/source/retest.c new file mode 100644 index 0000000000..9ff3572532 --- /dev/null +++ b/support/lgrind/source/retest.c @@ -0,0 +1,75 @@ +/* Test program for the regular expression routines */ + +#ifndef lint +static char sccsid[] = "@(#)retest.c 1.1 (LBL) 29/3/85"; +static char rcsid[] = + "$Id: retest.c,v 1.3 1999/05/26 07:30:03 mike Exp $"; +#endif + +#include <stdio.h> +#include <ctype.h> + +int l_onecase = 0; +char *_start; +char *_escaped; +char *convexp(); +char *expmatch(); +char *l_id="_"; + + + +main() +{ + char reg[132]; + char *ireg; + char str[132]; + char *match; + char matstr[132]; + char c; + + for (;;) { + printf ("\nexpr: "); + scanf ("%s", reg); + ireg = convexp(reg); + match = ireg; + while(*match) { + switch (*match) { + + case '\\': + case '(': + case ')': + case '|': + printf ("%c", *match); + break; + + default: + if (isalnum(*match)) + printf("%c", *match); + else + printf ("<%03o>", *match); + break; + } + match++; + } + printf("\n"); + getchar(); + for (;;) { + printf ("string: "); + match = str; + while ((c = getchar()) != '\n') + *match++ = c; + *match = 0; + if (str[0] == '#') + break; + matstr[0] = 0; + _start = str; + _escaped = 0; + match = expmatch (str, ireg, matstr); + if (match == 0) + printf ("FAILED\n"); + else + printf ("match\nmatstr = %s\n", matstr); + } + + } +} diff --git a/support/lgrind/source/v2lg.c b/support/lgrind/source/v2lg.c new file mode 100644 index 0000000000..5aa5df206f --- /dev/null +++ b/support/lgrind/source/v2lg.c @@ -0,0 +1,78 @@ +/* + * Filter a LaTeX file into an lgrind file. Convert + * \begin{verbatim}-\end{verbatim} pairs into %[ - %] pairs. Tabify + * the former verbatim environments. Convert \verb|stuff| into @stuff@. + */ + +#ifndef lint +static char sccsid[] = "@(#)v2lg.c 1.1 20/3/85"; +static char rcsid[] = + "$Id: v2lg.c,v 1.2 1999/05/25 17:01:36 mike Exp $"; +#endif + + +#include <stdio.h> +#include <string.h> + +#define FALSE 0 +#define TRUE 1 + +#define STREQ(s,t) ((*(s) == *(t)) && (!*(s) || !strcmp((s),(t)))) + + +int main(int argc, char **argv) +{ + int in_verbatim = FALSE, col, start_col; + char in[256], out[256], *ic, *oc, *verb, delim; + + while (fgets(in, sizeof(in), stdin) != NULL) { + if (in_verbatim) { + if (STREQ(in, "\\end{verbatim}\n")) { + fputs("%]\n", stdout); + in_verbatim = FALSE; + continue; + } + for (col = 0, ic = in, oc = out; *ic != '\n'; ) { + if (*ic != ' ' && *ic != '\t') { + *oc++ = *ic++; col++; + } else { /* \t == ' ' in a verbatim environment */ + start_col = col; + while (*ic == ' ' || *ic == '\t') { + if (((++col) & 7) == 0) { + *oc++ = '\t'; start_col = col; + } + ic++; + } + if ((col & 7) != 0) + for ( ; start_col < col; start_col++) + *oc++ = ' '; + } + } + *oc++ = '\n'; *oc++ = '\0'; + fputs(out, stdout); + continue; + } + if (STREQ(in, "\\begin{verbatim}\n")) { + fputs("%[\n", stdout); + in_verbatim = TRUE; + continue; + } + for (ic = in; (verb = strstr(ic, "\\verb")) != NULL; ) { + for ( ; ic < verb; ic++) + putchar(*ic); + ic += 5; /* Skip over \verb */ + if (*ic == '*') /* \verb* => funny-looking spaces */ + ic++; + delim = *ic++; /* the delimiter char */ + putchar('@'); + while (*ic != delim) { + putchar(*ic); ic++; + } + ic++; /* Skip the other delimiter */ + putchar('@'); + } + fputs(ic, stdout); + } + return 0; +} + |