2 * Copyright (c) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
3 * Copyright (c) 2015 Xin LI <delphij@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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/capsicum.h>
32 #include <sys/types.h>
35 #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;
62 struct sbuf *id = sbuf_new_auto();
65 l = newlocale(LC_ALL_MASK, "C", NULL);
68 printf("%s:\n", name);
70 while ((c = fgetc(fp)) != EOF) {
74 /* Transit to DELIM_SEEN if we see $ */
77 /* Otherwise, stay in INIT state */
82 if (isalpha_l(c, l)) {
83 /* Transit to KEYWORD if we see letter */
90 } else if (c == '$') {
91 /* Or, stay in DELIM_SEEN if more $ */
94 /* Otherwise, transit back to INIT */
101 if (isalpha_l(c, l)) {
103 * Stay in KEYWORD if additional letter is seen
106 } else if (c == ':') {
108 * See ':' for the first time, transit to
113 } else if (c == '$') {
115 * Incomplete ident. Go back to DELIM_SEEN
116 * state because we see a '$' which could be
117 * the beginning of a keyword.
122 * Go back to INIT state otherwise.
134 * If we see '::' (seen : in PUNC_SEEN),
135 * activate subversion treatment and transit
136 * to PUNC_SEEN_SVN state.
138 * If more than two :'s were seen, the ident
139 * is invalid and we would therefore go back
142 if (state == PUNC_SEEN) {
143 state = PUNC_SEEN_SVN;
151 * A space after ':' or '::' indicates we are at the
152 * last component of potential ident.
157 /* All other characters are invalid */
165 if (iscntrl_l(c, l)) {
166 /* Control characters are not allowed in this state */
168 } else if (c == '$') {
171 * valid ident should end with a space.
173 * subversion extension uses '#' to indicate that
174 * the keyword expansion have exceeded the fixed
175 * width, so it is also permitted if we are in
176 * subversion mode. No length check is enforced
177 * because GNU RCS ident(1) does not do it either.
179 c = sbuf_data(id)[sbuf_len(id) - 2];
180 if (c == ' ' || (subversion && c == '#')) {
181 printf(" %s\n", sbuf_data(id));
186 /* Other characters: stay in the state */
195 fprintf(stderr, "%s warning: no id keywords in %s\n",
196 getprogname(), name ? name : "standard input");
198 return (EXIT_FAILURE);
201 return (EXIT_SUCCESS);
205 main(int argc, char **argv)
209 int ret = EXIT_SUCCESS;
213 while ((ch = getopt(argc, argv, "qV")) != -1) {
219 /* Do nothing, compat with GNU rcs's ident */
220 return (EXIT_SUCCESS);
222 errx(EXIT_FAILURE, "usage: %s [-q] [-V] [file...]",
230 if (caph_limit_stdio() < 0)
231 err(EXIT_FAILURE, "unable to limit stdio");
235 fds = malloc(sizeof(*fds));
237 err(EXIT_FAILURE, "unable to allocate fds array");
238 fds[0] = STDIN_FILENO;
241 fds = malloc(sizeof(*fds) * nfds);
243 err(EXIT_FAILURE, "unable to allocate fds array");
245 for (i = 0; i < argc; i++) {
246 fds[i] = fd = open(argv[i], O_RDONLY);
252 if (caph_limit_stream(fd, CAPH_READ) < 0)
254 "unable to limit fcntls/rights for %s",
259 /* Enter Capsicum sandbox. */
260 if (cap_enter() < 0 && errno != ENOSYS)
261 err(EXIT_FAILURE, "unable to enter capability mode");
263 for (i = 0; i < (int)nfds; i++) {
267 fp = fdopen(fds[i], "r");
273 if (scan(fp, argc == 0 ? NULL : argv[i], quiet) != EXIT_SUCCESS)