Annotation of mandoc/soelim.c, Revision 1.4
1.4 ! schwarze 1: /* $Id: soelim.c,v 1.3 2015/05/20 22:57:22 schwarze Exp $ */
1.2 schwarze 2: /*
1.1 schwarze 3: * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
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.
15: *
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.
26: */
1.4 ! schwarze 27: #include "config.h"
1.1 schwarze 28:
1.2 schwarze 29: #include <sys/types.h>
1.1 schwarze 30:
1.2 schwarze 31: #include <ctype.h>
32: #include <err.h>
1.3 schwarze 33: #include <limits.h>
1.1 schwarze 34: #include <stdio.h>
35: #include <stdlib.h>
36: #include <string.h>
1.2 schwarze 37: #if HAVE_STRINGLIST
1.1 schwarze 38: #include <stringlist.h>
1.2 schwarze 39: #else
40: #include "compat_stringlist.h"
41: #endif
42: #include <unistd.h>
1.1 schwarze 43:
44: #define C_OPTION 0x1
45:
46: static StringList *includes;
47:
48: static void
49: usage(void)
50: {
51:
52: fprintf(stderr, "usage: soelim [-Crtv] [-I dir] [files]\n");
53:
54: exit(EXIT_FAILURE);
55: }
56:
57: static FILE *
58: soelim_fopen(const char *name)
59: {
60: FILE *f;
1.3 schwarze 61: char path[PATH_MAX];
1.1 schwarze 62: size_t i;
63:
64: if (strcmp(name, "-") == 0)
65: return (stdin);
66:
67: if ((f = fopen(name, "r")) != NULL)
68: return (f);
69:
70: if (*name == '/') {
71: warn("can't open '%s'", name);
72: return (NULL);
73: }
74:
75: for (i = 0; i < includes->sl_cur; i++) {
76: snprintf(path, sizeof(path), "%s/%s", includes->sl_str[i],
77: name);
78: if ((f = fopen(path, "r")) != NULL)
79: return (f);
80: }
81:
82: warn("can't open '%s'", name);
83:
84: return (f);
85: }
86:
87: static int
88: soelim_file(FILE *f, int flag)
89: {
90: char *line = NULL;
91: char *walk, *cp;
92: size_t linecap = 0;
93: ssize_t linelen;
94:
95: if (f == NULL)
96: return (1);
97:
98: while ((linelen = getline(&line, &linecap, f)) > 0) {
99: if (strncmp(line, ".so", 3) != 0) {
100: printf("%s", line);
101: continue;
102: }
103:
104: walk = line + 3;
105: if (!isspace(*walk) && ((flag & C_OPTION) == 0)) {
106: printf("%s", line);
107: continue;
108: }
109:
110: while (isspace(*walk))
111: walk++;
112:
113: cp = walk;
114: while (*cp != '\0' && !isspace(*cp))
115: cp++;
116: *cp = 0;
117: if (cp < line + linelen)
118: cp++;
119:
120: if (*walk == '\0') {
121: printf("%s", line);
122: continue;
123: }
124: if (soelim_file(soelim_fopen(walk), flag) == 1) {
125: free(line);
126: return (1);
127: }
128: if (*cp != '\0')
129: printf("%s", cp);
130: }
131:
132: free(line);
133: fclose(f);
134:
135: return (0);
136: }
137:
138: int
139: main(int argc, char **argv)
140: {
141: int ch, i;
142: int ret = 0;
143: int flags = 0;
144:
145: includes = sl_init();
146: if (includes == NULL)
147: err(EXIT_FAILURE, "sl_init()");
148:
149: while ((ch = getopt(argc, argv, "CrtvI:")) != -1) {
150: switch (ch) {
151: case 'C':
152: flags |= C_OPTION;
153: break;
154: case 'r':
155: case 'v':
156: case 't':
157: /* stub compatibility with groff's soelim */
158: break;
159: case 'I':
160: sl_add(includes, optarg);
161: break;
162: default:
163: sl_free(includes, 0);
164: usage();
165: }
166: }
167:
168: argc -= optind;
169: argv += optind;
170:
171: if (argc == 0)
172: ret = soelim_file(stdin, flags);
173:
174: for (i = 0; i < argc; i++)
175: ret = soelim_file(soelim_fopen(argv[i]), flags);
176:
177: sl_free(includes, 0);
178:
179: return (ret);
180: }
CVSweb