Home | History | Annotate | Line # | Download | only in soelim
soelim.c revision 1.5
      1  1.5  christos /*	$NetBSD: soelim.c,v 1.5 1997/12/21 17:04:16 christos Exp $	*/
      2  1.3       jtc 
      3  1.1       cgd /*
      4  1.3       jtc  * Copyright (c) 1980, 1993
      5  1.3       jtc  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1       cgd  * modification, are permitted provided that the following conditions
      9  1.1       cgd  * are met:
     10  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1       cgd  *    must display the following acknowledgement:
     17  1.1       cgd  *	This product includes software developed by the University of
     18  1.1       cgd  *	California, Berkeley and its contributors.
     19  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1       cgd  *    may be used to endorse or promote products derived from this software
     21  1.1       cgd  *    without specific prior written permission.
     22  1.1       cgd  *
     23  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1       cgd  * SUCH DAMAGE.
     34  1.1       cgd  */
     35  1.1       cgd 
     36  1.4     lukem #include <sys/cdefs.h>
     37  1.1       cgd #ifndef lint
     38  1.4     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     39  1.4     lukem 	The Regents of the University of California.  All rights reserved.\n");
     40  1.1       cgd #endif /* not lint */
     41  1.1       cgd 
     42  1.1       cgd #ifndef lint
     43  1.3       jtc #if 0
     44  1.3       jtc static char sccsid[] = "@(#)soelim.c	8.1 (Berkeley) 6/6/93";
     45  1.3       jtc #endif
     46  1.5  christos __RCSID("$NetBSD: soelim.c,v 1.5 1997/12/21 17:04:16 christos Exp $");
     47  1.1       cgd #endif /* not lint */
     48  1.1       cgd 
     49  1.1       cgd /*
     50  1.1       cgd  * soelim - a filter to process n/troff input eliminating .so's
     51  1.1       cgd  *
     52  1.1       cgd  * Author: Bill Joy UCB July 8, 1977
     53  1.1       cgd  *
     54  1.1       cgd  * This program eliminates .so's from a n/troff input stream.
     55  1.1       cgd  * It can be used to prepare safe input for submission to the
     56  1.1       cgd  * phototypesetter since the software supporting the operator
     57  1.1       cgd  * doesn't let him do chdir.
     58  1.1       cgd  *
     59  1.1       cgd  * This is a kludge and the operator should be given the
     60  1.1       cgd  * ability to do chdir.
     61  1.1       cgd  *
     62  1.1       cgd  * This program is more generally useful, it turns out, because
     63  1.1       cgd  * the program tbl doesn't understand ".so" directives.
     64  1.1       cgd  */
     65  1.5  christos #include <sys/param.h>
     66  1.5  christos #include <stdio.h>
     67  1.5  christos #include <string.h>
     68  1.5  christos #include <stdlib.h>
     69  1.5  christos #include <err.h>
     70  1.5  christos 
     71  1.1       cgd #define	STDIN_NAME	"-"
     72  1.1       cgd 
     73  1.5  christos struct path {
     74  1.5  christos 	char **list;
     75  1.5  christos 	size_t n, c;
     76  1.5  christos };
     77  1.5  christos 
     78  1.5  christos static int	 process __P((struct path *, char *));
     79  1.5  christos static void	 initpath __P((struct path *));
     80  1.5  christos static void	 addpath __P((struct path *,  const char *));
     81  1.5  christos static FILE	*openpath __P((struct path *, const char *, const char *));
     82  1.5  christos 
     83  1.4     lukem int	main __P((int, char **));
     84  1.5  christos 
     85  1.5  christos 
     86  1.5  christos static void
     87  1.5  christos initpath(p)
     88  1.5  christos 	struct path *p;
     89  1.5  christos {
     90  1.5  christos 	p->list = NULL;
     91  1.5  christos 	p->n = p->c = 0;
     92  1.5  christos }
     93  1.5  christos 
     94  1.5  christos static void
     95  1.5  christos addpath(p, dir)
     96  1.5  christos 	struct path *p;
     97  1.5  christos 	const char *dir;
     98  1.5  christos {
     99  1.5  christos 	if (p->list == NULL || p->n <= p->c - 2) {
    100  1.5  christos 		p->n += 10;
    101  1.5  christos 		p->list = realloc(p->list, p->n * sizeof(p->list[0]));
    102  1.5  christos 		if (p->list == NULL)
    103  1.5  christos 			err(1, "%s", "");
    104  1.5  christos 	}
    105  1.5  christos 
    106  1.5  christos 	if ((p->list[p->c++] = strdup(dir)) == NULL)
    107  1.5  christos 		err(1, "%s", "");
    108  1.5  christos 
    109  1.5  christos 	p->list[p->c] = NULL;
    110  1.5  christos }
    111  1.5  christos 
    112  1.5  christos static FILE *
    113  1.5  christos openpath(p, name, parm)
    114  1.5  christos 	struct path *p;
    115  1.5  christos 	const char *name;
    116  1.5  christos 	const char *parm;
    117  1.5  christos {
    118  1.5  christos 	char filename[MAXPATHLEN];
    119  1.5  christos 	const char *f;
    120  1.5  christos 	FILE *fp;
    121  1.5  christos 	size_t i;
    122  1.5  christos 
    123  1.5  christos 	if (*name == '/' || p->c == 0)
    124  1.5  christos 		return fopen(name, parm);
    125  1.5  christos 
    126  1.5  christos 	for (i = 0; i < p->c; i++) {
    127  1.5  christos 		if (p->list[i][0] == '\0')
    128  1.5  christos 			f = name;
    129  1.5  christos 		else {
    130  1.5  christos 			(void)snprintf(filename, sizeof(filename), "%s/%s",
    131  1.5  christos 			    p->list[i], name);
    132  1.5  christos 			f = filename;
    133  1.5  christos 		}
    134  1.5  christos 		if ((fp = fopen(f, parm)) != NULL)
    135  1.5  christos 			return fp;
    136  1.5  christos 	}
    137  1.5  christos 	return NULL;
    138  1.5  christos }
    139  1.4     lukem 
    140  1.4     lukem int
    141  1.1       cgd main(argc, argv)
    142  1.1       cgd 	int argc;
    143  1.1       cgd 	char *argv[];
    144  1.1       cgd {
    145  1.5  christos 	extern char *__progname;
    146  1.5  christos 	struct path p;
    147  1.5  christos 	int c;
    148  1.5  christos 
    149  1.5  christos 	initpath(&p);
    150  1.5  christos 	addpath(&p, ".");
    151  1.5  christos 
    152  1.5  christos 	while ((c = getopt(argc, argv, "I:")) != -1)
    153  1.5  christos 		switch (c) {
    154  1.5  christos 		case 'I':
    155  1.5  christos 			addpath(&p, optarg);
    156  1.5  christos 			break;
    157  1.5  christos 		default:
    158  1.5  christos 			(void)fprintf(stderr,
    159  1.5  christos 			    "Usage: %s [-I<dir>] [files...]\n", __progname);
    160  1.5  christos 			exit(1);
    161  1.5  christos 		}
    162  1.1       cgd 
    163  1.5  christos 	argc -= optind;
    164  1.5  christos 	argv += optind;
    165  1.5  christos 
    166  1.1       cgd 	if (argc == 0) {
    167  1.5  christos 		(void)process(&p, STDIN_NAME);
    168  1.1       cgd 		exit(0);
    169  1.1       cgd 	}
    170  1.1       cgd 	do {
    171  1.5  christos 		(void)process(&p, argv[0]);
    172  1.1       cgd 		argv++;
    173  1.1       cgd 		argc--;
    174  1.1       cgd 	} while (argc > 0);
    175  1.1       cgd 	exit(0);
    176  1.1       cgd }
    177  1.1       cgd 
    178  1.4     lukem int
    179  1.5  christos process(p, file)
    180  1.5  christos 	struct path *p;
    181  1.1       cgd 	char *file;
    182  1.1       cgd {
    183  1.4     lukem 	char *cp;
    184  1.4     lukem 	int c;
    185  1.1       cgd 	char fname[BUFSIZ];
    186  1.1       cgd 	FILE *soee;
    187  1.1       cgd 	int isfile;
    188  1.1       cgd 
    189  1.1       cgd 	if (!strcmp(file, STDIN_NAME)) {
    190  1.1       cgd 		soee = stdin;
    191  1.1       cgd 	} else {
    192  1.5  christos 		soee = openpath(p, file, "r");
    193  1.1       cgd 		if (soee == NULL) {
    194  1.5  christos 			warn("Cannot open `%s'", file);
    195  1.1       cgd 			return(-1);
    196  1.1       cgd 		}
    197  1.1       cgd 	}
    198  1.1       cgd 	for (;;) {
    199  1.1       cgd 		c = getc(soee);
    200  1.1       cgd 		if (c == EOF)
    201  1.1       cgd 			break;
    202  1.1       cgd 		if (c != '.')
    203  1.1       cgd 			goto simple;
    204  1.1       cgd 		c = getc(soee);
    205  1.1       cgd 		if (c != 's') {
    206  1.1       cgd 			putchar('.');
    207  1.1       cgd 			goto simple;
    208  1.1       cgd 		}
    209  1.1       cgd 		c = getc(soee);
    210  1.1       cgd 		if (c != 'o') {
    211  1.1       cgd 			printf(".s");
    212  1.1       cgd 			goto simple;
    213  1.1       cgd 		}
    214  1.1       cgd 		do
    215  1.1       cgd 			c = getc(soee);
    216  1.1       cgd 		while (c == ' ' || c == '\t');
    217  1.1       cgd 		cp = fname;
    218  1.1       cgd 		isfile = 0;
    219  1.1       cgd 		for (;;) {
    220  1.1       cgd 			switch (c) {
    221  1.1       cgd 
    222  1.1       cgd 			case ' ':
    223  1.1       cgd 			case '\t':
    224  1.1       cgd 			case '\n':
    225  1.1       cgd 			case EOF:
    226  1.1       cgd 				goto donename;
    227  1.1       cgd 
    228  1.1       cgd 			default:
    229  1.1       cgd 				*cp++ = c;
    230  1.1       cgd 				c = getc(soee);
    231  1.1       cgd 				isfile++;
    232  1.1       cgd 				continue;
    233  1.1       cgd 			}
    234  1.1       cgd 		}
    235  1.1       cgd donename:
    236  1.1       cgd 		if (cp == fname) {
    237  1.1       cgd 			printf(".so");
    238  1.1       cgd 			goto simple;
    239  1.1       cgd 		}
    240  1.1       cgd 		*cp = 0;
    241  1.5  christos 		if (process(p, fname) < 0)
    242  1.1       cgd 			if (isfile)
    243  1.1       cgd 				printf(".so %s\n", fname);
    244  1.1       cgd 		continue;
    245  1.1       cgd simple:
    246  1.1       cgd 		if (c == EOF)
    247  1.1       cgd 			break;
    248  1.1       cgd 		putchar(c);
    249  1.1       cgd 		if (c != '\n') {
    250  1.1       cgd 			c = getc(soee);
    251  1.1       cgd 			goto simple;
    252  1.1       cgd 		}
    253  1.1       cgd 	}
    254  1.1       cgd 	if (soee != stdin) {
    255  1.1       cgd 		fclose(soee);
    256  1.1       cgd 	}
    257  1.1       cgd 	return(0);
    258  1.1       cgd }
    259