2 * Copyright (c) 2015 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>
45 /* state condition to transit to next state */
47 DELIM_SEEN, /* letter */
48 KEYWORD, /* punctuation mark */
49 PUNC_SEEN, /* ':' -> _SVN; space -> TEXT */
50 PUNC_SEEN_SVN, /* space */
55 scan(FILE *fp, const char *name, bool quiet)
59 bool subversion = false;
60 analyzer_states state = INIT;
61 struct sbuf *id = sbuf_new_auto();
64 l = newlocale(LC_ALL_MASK, "C", NULL);
67 printf("%s:\n", name);
69 while ((c = fgetc(fp)) != EOF) {
73 /* Transit to DELIM_SEEN if we see $ */
76 /* Otherwise, stay in INIT state */
81 if (isalpha_l(c, l)) {
82 /* Transit to KEYWORD if we see letter */
89 } else if (c == '$') {
90 /* Or, stay in DELIM_SEEN if more $ */
93 /* Otherwise, transit back to INIT */
100 if (isalpha_l(c, l)) {
102 * Stay in KEYWORD if additional letter is seen
105 } else if (c == ':') {
107 * See ':' for the first time, transit to
112 } else if (c == '$') {
114 * Incomplete ident. Go back to DELIM_SEEN
115 * state because we see a '$' which could be
116 * the beginning of a keyword.
121 * Go back to INIT state otherwise.
133 * If we see '::' (seen : in PUNC_SEEN),
134 * activate subversion treatment and transit
135 * to PUNC_SEEN_SVN state.
137 * If more than two :'s were seen, the ident
138 * is invalid and we would therefore go back
141 if (state == PUNC_SEEN) {
142 state = PUNC_SEEN_SVN;
150 * A space after ':' or '::' indicates we are at the
151 * last component of potential ident.
156 /* All other characters are invalid */
164 if (iscntrl_l(c, l)) {
165 /* Control characters are not allowed in this state */
167 } else if (c == '$') {
170 * valid ident should end with a space.
172 * subversion extension uses '#' to indicate that
173 * the keyword expansion have exceeded the fixed
174 * width, so it is also permitted if we are in
175 * subversion mode. No length check is enforced
176 * because GNU RCS ident(1) does not do it either.
178 c = sbuf_data(id)[sbuf_len(id) - 2];
179 if (c == ' ' || (subversion && c == '#')) {
180 printf(" %s\n", sbuf_data(id));
185 /* Other characters: stay in the state */
194 fprintf(stderr, "%s warning: no id keywords in %s\n",
195 getprogname(), name ? name : "standard input");
197 return (EXIT_FAILURE);
200 return (EXIT_SUCCESS);
204 main(int argc, char **argv)
208 int ret = EXIT_SUCCESS;
212 while ((ch = getopt(argc, argv, "qV")) != -1) {
218 /* Do nothing, compat with GNU rcs's ident */
219 return (EXIT_SUCCESS);
221 errx(EXIT_FAILURE, "usage: %s [-q] [-V] [file...]",
229 if (caph_limit_stdio() < 0)
230 err(EXIT_FAILURE, "unable to limit stdio");
234 fds = malloc(sizeof(*fds));
236 err(EXIT_FAILURE, "unable to allocate fds array");
237 fds[0] = STDIN_FILENO;
240 fds = malloc(sizeof(*fds) * nfds);
242 err(EXIT_FAILURE, "unable to allocate fds array");
244 for (i = 0; i < argc; i++) {
245 fds[i] = fd = open(argv[i], O_RDONLY);
251 if (caph_limit_stream(fd, CAPH_READ) < 0)
253 "unable to limit fcntls/rights for %s",
258 /* Enter Capsicum sandbox. */
259 if (caph_enter() < 0)
260 err(EXIT_FAILURE, "unable to enter capability mode");
262 for (i = 0; i < (int)nfds; i++) {
266 fp = fdopen(fds[i], "r");
272 if (scan(fp, argc == 0 ? NULL : argv[i], quiet) != EXIT_SUCCESS)