2 * Copyright (c) 2015-2021 Baptiste Daroussin <bapt@FreeBSD.org>
3 * Copyright (c) 2015 Xin LI <delphij@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/capsicum.h>
31 #include <sys/types.h>
34 #include <capsicum_helpers.h>
46 /* state condition to transit to next state */
48 DELIM_SEEN, /* letter */
49 KEYWORD, /* punctuation mark */
50 PUNC_SEEN, /* ':' -> _SVN; space -> TEXT */
51 PUNC_SEEN_SVN, /* space */
56 scan(FILE *fp, const char *name, bool quiet)
60 bool subversion = false;
61 analyzer_states state = INIT;
67 l = newlocale(LC_ALL_MASK, "C", NULL);
70 buffp = open_memstream(&buf, &sz);
72 err(EXIT_FAILURE, "open_memstream()");
75 printf("%s:\n", name);
77 while ((c = fgetc(fp)) != EOF) {
81 /* Transit to DELIM_SEEN if we see $ */
84 /* Otherwise, stay in INIT state */
89 if (isalpha_l(c, l)) {
90 /* Transit to KEYWORD if we see letter */
99 } else if (c == '$') {
100 /* Or, stay in DELIM_SEEN if more $ */
103 /* Otherwise, transit back to INIT */
110 if (isalpha_l(c, l)) {
112 * Stay in KEYWORD if additional letter is seen
115 } else if (c == ':') {
117 * See ':' for the first time, transit to
122 } else if (c == '$') {
124 * Incomplete ident. Go back to DELIM_SEEN
125 * state because we see a '$' which could be
126 * the beginning of a keyword.
131 * Go back to INIT state otherwise.
143 * If we see '::' (seen : in PUNC_SEEN),
144 * activate subversion treatment and transit
145 * to PUNC_SEEN_SVN state.
147 * If more than two :'s were seen, the ident
148 * is invalid and we would therefore go back
151 if (state == PUNC_SEEN) {
152 state = PUNC_SEEN_SVN;
160 * A space after ':' or '::' indicates we are at the
161 * last component of potential ident.
166 /* All other characters are invalid */
174 if (iscntrl_l(c, l)) {
175 /* Control characters are not allowed in this state */
177 } else if (c == '$') {
180 * valid ident should end with a space.
182 * subversion extension uses '#' to indicate that
183 * the keyword expansion have exceeded the fixed
184 * width, so it is also permitted if we are in
185 * subversion mode. No length check is enforced
186 * because GNU RCS ident(1) does not do it either.
188 c = buf[strlen(buf) -2 ];
189 if (c == ' ' || (subversion && c == '#')) {
190 printf(" %s\n", buf);
195 /* Other characters: stay in the state */
205 fprintf(stderr, "%s warning: no id keywords in %s\n",
206 getprogname(), name ? name : "standard input");
208 return (EXIT_FAILURE);
211 return (EXIT_SUCCESS);
215 main(int argc, char **argv)
219 int ret = EXIT_SUCCESS;
223 while ((ch = getopt(argc, argv, "qV")) != -1) {
229 /* Do nothing, compat with GNU rcs's ident */
230 return (EXIT_SUCCESS);
232 errx(EXIT_FAILURE, "usage: %s [-q] [-V] [file...]",
240 if (caph_limit_stdio() < 0)
241 err(EXIT_FAILURE, "unable to limit stdio");
245 fds = malloc(sizeof(*fds));
247 err(EXIT_FAILURE, "unable to allocate fds array");
248 fds[0] = STDIN_FILENO;
251 fds = malloc(sizeof(*fds) * nfds);
253 err(EXIT_FAILURE, "unable to allocate fds array");
255 for (i = 0; i < argc; i++) {
256 fds[i] = fd = open(argv[i], O_RDONLY);
262 if (caph_limit_stream(fd, CAPH_READ) < 0)
264 "unable to limit fcntls/rights for %s",
269 /* Enter Capsicum sandbox. */
270 if (caph_enter() < 0)
271 err(EXIT_FAILURE, "unable to enter capability mode");
273 for (i = 0; i < (int)nfds; i++) {
277 fp = fdopen(fds[i], "r");
283 if (scan(fp, argc == 0 ? NULL : argv[i], quiet) != EXIT_SUCCESS)