2 * cawf - a C version of Henry Spencer's awf(1), the Amazingly
3 * Workable (text) Formatter
5 * V. Abell, Purdue University Computing Center
9 * Copyright (c) 1991 Purdue University Research Foundation,
10 * West Lafayette, Indiana 47907. All rights reserved.
12 * Written by Victor A. Abell <abe@mace.cc.purdue.edu>, Purdue
13 * University Computing Center. Not derived from licensed software;
14 * derived from awf(1) by Henry Spencer of the University of Toronto.
16 * Permission is granted to anyone to use this software for any
17 * purpose on any computer system, and to alter it and redistribute
18 * it freely, subject to the following restrictions:
20 * 1. The author is not responsible for any consequences of use of
21 * this software, even if they arise from flaws in it.
23 * 2. The origin of this software must not be misrepresented, either
24 * by explicit claim or by omission. Credits must appear in the
27 * 3. Altered versions must be plainly marked as such, and must not
28 * be misrepresented as being the original software. Credits must
29 * appear in the documentation.
31 * 4. This notice may not be removed or altered.
34 static char Version
[] = "4.0";
43 #include <sys\types.h>
52 char *ep
; /* environment pointer */
53 int fff
= 0; /* final form feed status */
54 char **files
; /* file names */
55 int help
= 0; /* help status */
56 int i
; /* temporary index */
57 size_t l
; /* length */
58 char *lib
= CAWFLIB
; /* library path */
59 int libl
; /* library path length */
60 int mac
= 0; /* macro specification status */
61 int nf
= 0; /* number of files */
62 char *np
; /* name pointer */
63 int pc
; /* prolog count */
64 struct stat sbuf
; /* stat buffer */
68 if ((Pname
= strrchr(argv
[0], '\\')) != NULL
)
70 else if ((Pname
= strrchr(argv
[0], '/')) != NULL
)
75 * Set error file stream pointer.
81 if ((np
= getenv("CAWFLIB")) != NULL
)
85 * Get device file name.
87 for (ep
= getenv("TERM");; ep
= NULL
) {
88 if (ep
== NULL
|| *ep
== '\0')
90 l
= libl
+ 1 + strlen(ep
) + strlen(".dev") + 1;
91 if ((np
= malloc(l
)) == NULL
)
93 " no string space for device file: ", ep
);
94 (void) sprintf(np
, "%s/%s.dev", lib
, ep
);
95 if (stat(np
, &sbuf
) == 0)
97 if (strcmp(ep
, "dumb") == 0)
98 Error(FATAL
, NOLINE
, " no dumb.dev file in ", lib
);
101 if ((files
= malloc((argc
+ 2) * sizeof(files
[0]))) == NULL
)
102 Error(FATAL
, NOLINE
, " no space for file list",
106 * Get common text file name.
108 l
= libl
+ 1 + strlen("common") + 1;
109 if ((np
= malloc(l
)) == NULL
)
110 Error(FATAL
, NOLINE
, " no string space for common file name",
112 (void) sprintf(np
, "%s/common", lib
);
117 while ((i
= getopt(argc
, argv
, "c:d:ef:hm:")) != EOF
) {
120 * -c<device_configuration_file_path>>
126 * -d<output_device_name> -- define output device name
128 * The default output device name is NORMAL -- i.e., a device that
129 * does bold face with backspace and overprinting and italic face with
130 * underscore. NORMAL is usually a terminal device.
132 * There is a built-in device, named ANSI, that does bold face with
133 * the ANSI shadow mode and italic face with the ANSI underscore mode.
134 * ANSI is normally a terminal device that supports the ANSI shadow
135 * and underscore modes.
137 * There is a built-in output device, named NONE, that does nothing
138 * at all for the bold or italic faces. This is usually a terminal
141 * All other device names must match a stanza in the device
142 * configuration file.
148 * -e -- eject: issue final form feed
154 * -f<output_device_font_name> -- define font name for the output
155 * device (from device configuration
162 * -h -- display help (usage)
168 * -m<macro_file_name>
170 * Special support is provided for -man, -me and -ms.
175 "multiple macro file declaration",
179 l
= libl
+ 2 + strlen(optarg
) + strlen(".mac") + 1;
180 if ((np
= malloc(l
)) == NULL
)
181 Error(FATAL
, NOLINE
, " no string space for ",
183 (void) sprintf(np
, "%s/m%s.mac", lib
, optarg
);
185 if (strcmp(optarg
, "an") == 0)
187 else if (strcmp(optarg
, "s") == 0
188 || strcmp(optarg
, "e") == 0)
193 * Option not recognized by getopt().
202 (void) fprintf(stderr
,
203 "%s %s usage: [-c<c>] [-d<d>] [-e] [-f<f>] [-h] [-m<m>] file...\n",
205 (void) fprintf(stderr
,
206 "\t-c<c> <c> is the device configuration file path\n");
207 (void) fprintf(stderr
,
208 "\t-d<d> <d> is the output device name\n");
209 (void) fprintf(stderr
,
210 "\t (default = NORMAL, using \\b for bold and italic)\n");
211 (void) fprintf(stderr
,
212 "\t (built-ins = ANSI, NONE and NORMAL)\n");
213 (void) fprintf(stderr
,
214 "\t-e issue eject after last page\n");
215 (void) fprintf(stderr
,
216 "\t-f<f> <f> is the output device font name\n");
217 (void) fprintf(stderr
,
218 "\t-h display help (this output)\n");
219 (void) fprintf(stderr
,
220 "\t-m<m> m<m> is the macro file name\n");
221 (void) fprintf(stderr
,
222 "\tfile ... source file names\n");
228 * No macroes - enable Bold, Italic, Roman and Courier fonts.
230 for (i
= 0; Fcode
[i
].nm
; i
++) {
231 switch (Fcode
[i
].nm
) {
236 Fcode
[i
].status
= '1';
241 * Add user-supplied file names.
244 if (optind
>= argc
) {
245 files
[nf
++] = NULL
; /* STDIN */
247 while (optind
< argc
)
248 files
[nf
++] = argv
[optind
++];
251 * Make sure all input files are accessible.
253 for (i
= 0; i
< nf
; i
++) {
254 if (files
[i
] != NULL
) {
255 if (stat(files
[i
], &sbuf
) != 0)
256 Error(WARN
, NOLINE
, " can't find ", files
[i
]);
262 * Miscellaneous initialization.
266 if (Pat
[i
].re
== NULL
)
268 if ((Pat
[i
].pat
= regcomp(Pat
[i
].re
)) == NULL
)
269 Error(WARN
, NOLINE
, Pat
[i
].re
, " regcomp failure");
271 if ((i
= Findscale((int)'n', 0.0, 0)) < 0)
272 Error(WARN
, NOLINE
, " can't find Scale['n']", NULL
);
273 Scalen
= Scale
[i
].val
;
274 if ((i
= Findscale((int)'u', 0.0, 0)) < 0)
275 Error(WARN
, NOLINE
, " can't find Scale['u']", NULL
);
276 Scaleu
= Scale
[i
].val
;
277 if ((i
= Findscale((int)'v', 0.0, 0)) < 0)
278 Error(WARN
, NOLINE
, " can't find Scale['v']", NULL
);
279 Scalev
= Scale
[i
].val
;
280 (void) Findstr((unsigned char *)"CH", (unsigned char *)"= % -", 1);
281 Cont
= Newstr((unsigned char *)" ");
283 if ((Trtbl
= (unsigned char *)malloc(256)) == NULL
)
284 Error(WARN
, NOLINE
, " can't allocate translate table space",
288 for (i
= 1; i
< 256; i
++)
289 Trtbl
[i
] = (unsigned char) i
;
294 * Here begins pass1 of awf - reading input lines and expanding macros.
301 for (i
= 0; i
< Fstr
.il
; i
++) {
302 Charput((int)Fstr
.i
[i
]);
305 Macro((unsigned char *)".^x");
306 Macro((unsigned char *)".^b");
307 Macro((unsigned char *)".^# 1 <prolog>");
311 for (i
= 0; i
< nf
; i
++) {
313 if (files
[i
] == NULL
) {
318 if ((Ifs
= fopen(files
[i
], "r")) == NULL
)
320 if ((Ifs
= fopen(files
[i
], "rt")) == NULL
)
322 Error(FATAL
, NOLINE
, " can't open ", files
[i
]);
326 (void) sprintf((char *)Line
, ".^# 1 %s", np
);
332 while (fgets((char *)Line
, MAXLINE
, Ifs
) != NULL
) {
334 if ((np
= strrchr((char *)Line
, '\n')) != NULL
)
337 Line
[MAXLINE
-1] = '\0';
341 Macro((unsigned char *)".^e");
346 Inname
= Inn_stk
[Fsp
-1];
348 Ifs
= Ifs_stk
[Fsp
-1];
360 * Macro(inp) - process a possible macro statement
361 * pass non-macros and macros alike to pass 2
366 unsigned char *inp
; /* possible macro statement pointer */
368 unsigned char c
[2]; /* characters */
369 int endm
; /* end of macro status */
370 FILE *fs
; /* temporary file stream */
371 int i
, j
, k
; /* temporary indexes */
372 int mx
; /* Macrotab[] index */
373 int req
; /* request character status */
374 unsigned char *s1
, *s2
; /* temporary string pointers */
380 req
= (*inp
== '.' || *inp
== '\'') ? 1 : 0;
382 * Check for file name designator.
384 if (req
&& inp
[1] == '^' && inp
[2] == '#') {
386 Inname
= Field(3, inp
, 1);
392 * Check for source command - "^[.']so".
394 if (req
&& inp
[1] == 's' && inp
[2] == 'o') {
395 if ((s1
= Field(2, inp
, 1)) == NULL
) {
396 Error(WARN
, LINE
, " no file specified", NULL
);
399 if ((fs
= fopen((char *)s1
, "r")) == NULL
) {
400 Error(WARN
, LINE
, " can't open", NULL
);
403 if (Fsp
>= MAXFSTK
) {
405 Error(WARN
, LINE
, " nesting too deep", NULL
);
410 Inn_stk
[Fsp
] = Inname
;
420 if (req
&& inp
[1] == 'i' && inp
[2] == 'g') {
421 while (fgets((char *)inp
, MAXLINE
, Ifs
) != NULL
) {
423 if (inp
[0] == '.' && inp
[1] == '.') break;
428 * Check for start of macro definition.
430 if (req
&& inp
[1] == 'd' && inp
[2] == 'e') {
431 if (inp
[3] != ' ' || inp
[4] == '\0') {
432 Error(WARN
, LINE
, " illegal macro definition", NULL
);
437 Curmx
= Findmacro(c
, 1);
441 * Check for macro text. Remove double backslashes.
443 if (req
&& (inp
[1] == '\0' || (inp
[2] == '\0' && inp
[0] == inp
[1])))
447 if (Curmx
>= 0 && !endm
) {
449 Error(FATAL
, LINE
, " out of macro text space", NULL
);
450 if ((s1
= (unsigned char *)strchr((char *)inp
, '\\')) == NULL
)
451 Macrotxt
[Mtx
] = Newstr(inp
);
453 for (s1
= Pass1ln
, s2
= inp
;; s1
++) {
454 if ((*s1
= *s2
++) == '\0')
456 if (*s1
== '\\' && *s2
== '\\')
459 Macrotxt
[Mtx
] = Newstr(Pass1ln
);
461 if (Macrotab
[Curmx
].bx
== -1)
462 Macrotab
[Curmx
].bx
= Mtx
;
464 Macrotab
[Curmx
].ct
++;
468 * Check for end of macro.
470 if (Curmx
>= 0 && endm
) {
472 (void) sprintf((char *)Pass1ln
, ".^# %d %s", NR
, Inname
);
477 * Check for conditionals and macro expansions.
480 && (((mx
= Findmacro(inp
+1, 0)) != -1) || regexec(Pat
[0].pat
, inp
))) {
485 * None of the above: forward the line.