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/types.h>
43 /* state condition to transit to next state */
45 DELIM_SEEN, /* letter */
46 KEYWORD, /* punctuation mark */
47 PUNC_SEEN, /* ':' -> _SVN; space -> TEXT */
48 PUNC_SEEN_SVN, /* space */
53 scan(FILE *fp, const char *name, bool quiet)
57 bool subversion = false;
58 analyzer_states state = INIT;
59 struct sbuf *id = sbuf_new_auto();
62 l = newlocale(LC_ALL_MASK, "C", NULL);
65 printf("%s:\n", name);
67 while ((c = fgetc(fp)) != EOF) {
71 /* Transit to DELIM_SEEN if we see $ */
74 /* Otherwise, stay in INIT state */
79 if (isalpha_l(c, l)) {
80 /* Transit to KEYWORD if we see letter */
87 } else if (c == '$') {
88 /* Or, stay in DELIM_SEEN if more $ */
91 /* Otherwise, transit back to INIT */
98 if (isalpha_l(c, l)) {
100 * Stay in KEYWORD if additional letter is seen
103 } else if (c == ':') {
105 * See ':' for the first time, transit to
110 } else if (c == '$') {
112 * Incomplete ident. Go back to DELIM_SEEN
113 * state because we see a '$' which could be
114 * the beginning of a keyword.
119 * Go back to INIT state otherwise.
131 * If we see '::' (seen : in PUNC_SEEN),
132 * activate subversion treatment and transit
133 * to PUNC_SEEN_SVN state.
135 * If more than two :'s were seen, the ident
136 * is invalid and we would therefore go back
139 if (state == PUNC_SEEN) {
140 state = PUNC_SEEN_SVN;
148 * A space after ':' or '::' indicates we are at the
149 * last component of potential ident.
154 /* All other characters are invalid */
162 if (iscntrl_l(c, l)) {
163 /* Control characters are not allowed in this state */
165 } else if (c == '$') {
168 * valid ident should end with a space.
170 * subversion extension uses '#' to indicate that
171 * the keyword expansion have exceeded the fixed
172 * width, so it is also permitted if we are in
173 * subversion mode. No length check is enforced
174 * because GNU RCS ident(1) does not do it either.
176 c = sbuf_data(id)[sbuf_len(id) - 2];
177 if (c == ' ' || (subversion && c == '#')) {
178 printf(" %s\n", sbuf_data(id));
183 /* Other characters: stay in the state */
192 fprintf(stderr, "%s warning: no id keywords in %s\n",
193 getprogname(), name ? name : "standard input");
195 return (EXIT_FAILURE);
198 return (EXIT_SUCCESS);
202 main(int argc, char **argv)
206 int ret = EXIT_SUCCESS;
209 while ((ch = getopt(argc, argv, "qV")) != -1) {
215 /* Do nothing, compat with GNU rcs's ident */
216 return (EXIT_SUCCESS);
218 errx(EXIT_FAILURE, "usage: %s [-q] [-V] [file...]",
227 return (scan(stdin, NULL, quiet));
229 for (i = 0; i < argc; i++) {
230 fp = fopen(argv[i], "r");
236 if (scan(fp, argv[i], quiet) != EXIT_SUCCESS)