]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/soelim/soelim.c
pmap: move the smp_targeted_tlb_shutdown pointer stuff to amd64 pmap.h
[FreeBSD/FreeBSD.git] / usr.bin / soelim / soelim.c
1 /*-
2  * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/types.h>
28
29 #include <ctype.h>
30 #include <err.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stringlist.h>
36 #include <unistd.h>
37
38 #define C_OPTION 0x1
39
40 static StringList *includes;
41
42 static void
43 usage(void)
44 {
45
46         fprintf(stderr, "usage: soelim [-Crtv] [-I dir] [files]\n");
47
48         exit(EXIT_FAILURE);
49 }
50
51 static FILE *
52 soelim_fopen(const char *name)
53 {
54         FILE *f;
55         char path[PATH_MAX];
56         size_t i;
57
58         if (strcmp(name, "-") == 0)
59                 return (stdin);
60
61         if ((f = fopen(name, "r")) != NULL)
62                 return (f);
63
64         if (*name == '/') {
65                 warn("can't open '%s'", name);
66                 return (NULL);
67         }
68
69         for (i = 0; i < includes->sl_cur; i++) {
70                 snprintf(path, sizeof(path), "%s/%s", includes->sl_str[i],
71                     name);
72                 if ((f = fopen(path, "r")) != NULL)
73                         return (f);
74         }
75
76         warn("can't open '%s'", name);
77
78         return (f);
79 }
80
81 static int
82 soelim_file(FILE *f, int flag)
83 {
84         char *line = NULL;
85         char *walk, *cp;
86         size_t linecap = 0;
87         ssize_t linelen;
88
89         if (f == NULL)
90                 return (1);
91
92         while ((linelen = getline(&line, &linecap, f)) > 0) {
93                 if (strncmp(line, ".so", 3) != 0) {
94                         printf("%s", line);
95                         continue;
96                 }
97
98                 walk = line + 3;
99                 if (!isspace(*walk) && ((flag & C_OPTION) == 0)) {
100                         printf("%s", line);
101                         continue;
102                 }
103
104                 while (isspace(*walk))
105                         walk++;
106
107                 cp = walk;
108                 while (*cp != '\0' && !isspace(*cp))
109                         cp++;
110                 *cp = 0;
111                 if (cp < line + linelen)
112                         cp++;
113
114                 if (*walk == '\0') {
115                         printf("%s", line);
116                         continue;
117                 }
118                 if (soelim_file(soelim_fopen(walk), flag) == 1) {
119                         free(line);
120                         return (1);
121                 }
122                 if (*cp != '\0')
123                         printf("%s", cp);
124         }
125
126         free(line);
127         fclose(f);
128
129         return (0);
130 }
131
132 int
133 main(int argc, char **argv)
134 {
135         int ch, i;
136         int ret = 0;
137         int flags = 0;
138
139         includes = sl_init();
140         if (includes == NULL)
141                 err(EXIT_FAILURE, "sl_init()");
142
143         while ((ch = getopt(argc, argv, "CrtvI:")) != -1) {
144                 switch (ch) {
145                 case 'C':
146                         flags |= C_OPTION;
147                         break;
148                 case 'r':
149                 case 'v':
150                 case 't':
151                         /* stub compatibility with groff's soelim */
152                         break;
153                 case 'I':
154                         sl_add(includes, optarg);
155                         break;
156                 default:
157                         sl_free(includes, 0);
158                         usage();
159                 }
160         }
161
162         argc -= optind;
163         argv += optind;
164
165         if (argc == 0)
166                 ret = soelim_file(stdin, flags);
167
168         for (i = 0; i < argc; i++)
169                 ret = soelim_file(soelim_fopen(argv[i]), flags);
170
171         sl_free(includes, 0);
172
173         return (ret);
174 }