1 /* $Id: soelim.c,v 1.5 2015/11/07 14:22:29 schwarze Exp $ */
3 * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
40 #include <stringlist.h>
42 #include "compat_stringlist.h"
48 static StringList *includes;
54 fprintf(stderr, "usage: soelim [-Crtv] [-I dir] [files]\n");
60 soelim_fopen(const char *name)
66 if (strcmp(name, "-") == 0)
69 if ((f = fopen(name, "r")) != NULL)
73 warn("can't open '%s'", name);
77 for (i = 0; i < includes->sl_cur; i++) {
78 snprintf(path, sizeof(path), "%s/%s", includes->sl_str[i],
80 if ((f = fopen(path, "r")) != NULL)
84 warn("can't open '%s'", name);
90 soelim_file(FILE *f, int flag)
100 while ((linelen = getline(&line, &linecap, f)) > 0) {
101 if (strncmp(line, ".so", 3) != 0) {
107 if (!isspace(*walk) && ((flag & C_OPTION) == 0)) {
112 while (isspace(*walk))
116 while (*cp != '\0' && !isspace(*cp))
119 if (cp < line + linelen)
126 if (soelim_file(soelim_fopen(walk), flag) == 1) {
141 main(int argc, char **argv)
147 includes = sl_init();
148 if (includes == NULL)
149 err(EXIT_FAILURE, "sl_init()");
151 while ((ch = getopt(argc, argv, "CrtvI:")) != -1) {
159 /* stub compatibility with groff's soelim */
162 sl_add(includes, optarg);
165 sl_free(includes, 0);
174 ret = soelim_file(stdin, flags);
176 for (i = 0; i < argc; i++)
177 ret = soelim_file(soelim_fopen(argv[i]), flags);
179 sl_free(includes, 0);