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