1 /* Identify RCS keyword strings in files. */
2
3 /* Copyright 1982, 1988, 1989 Walter Tichy
4 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Paul Eggert
5 Distributed under license by the Free Software Foundation, Inc.
6
7 This file is part of RCS.
8
9 RCS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 RCS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with RCS; see the file COPYING.
21 If not, write to the Free Software Foundation,
22 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 Report problems and direct all questions to:
25
26 rcs-bugs@cs.purdue.edu
27
28 */
29
30 /*
31 * Revision 5.9 1995/06/16 06:19:24 eggert
32 * Update FSF address.
33 *
34 * Revision 5.8 1995/06/01 16:23:43 eggert
35 * (exiterr, reportError): New functions, needed for DOS and OS/2 ports.
36 * (scanfile): Use them.
37 *
38 * Revision 5.7 1994/03/20 04:52:58 eggert
39 * Remove `exiting' from identExit.
40 *
41 * Revision 5.6 1993/11/09 17:40:15 eggert
42 * Add -V.
43 *
44 * Revision 5.5 1993/11/03 17:42:27 eggert
45 * Test for char == EOF, not char < 0.
46 *
47 * Revision 5.4 1992/01/24 18:44:19 eggert
48 * lint -> RCS_lint
49 *
50 * Revision 5.3 1991/09/10 22:15:46 eggert
51 * Open files with FOPEN_R, not FOPEN_R_WORK,
52 * because they might be executables, not working files.
53 *
54 * Revision 5.2 1991/08/19 03:13:55 eggert
55 * Report read errors immediately.
56 *
57 * Revision 5.1 1991/02/25 07:12:37 eggert
58 * Don't report empty keywords. Check for I/O errors.
59 *
60 * Revision 5.0 1990/08/22 08:12:37 eggert
61 * Don't limit output to known keywords.
62 * Remove arbitrary limits and lint. Ansify and Posixate.
63 *
64 * Revision 4.5 89/05/01 15:11:54 narten
65 * changed copyright header to reflect current distribution rules
66 *
67 * Revision 4.4 87/10/23 17:09:57 narten
68 * added exit(0) so exit return code would be non random
69 *
70 * Revision 4.3 87/10/18 10:23:55 narten
71 * Updating version numbers. Changes relative to 1.1 are actually relative
72 * to 4.1
73 *
74 * Revision 1.3 87/07/09 09:20:52 trinkle
75 * Added check to make sure there is at least one arg before comparing argv[1]
76 * with "-q". This necessary on machines that don't allow dereferncing null
77 * pointers (i.e. Suns).
78 *
79 * Revision 1.2 87/03/27 14:21:47 jenkins
80 * Port to suns
81 *
82 * Revision 4.1 83/05/10 16:31:02 wft
83 * Added option -q and input from reading stdin.
84 * Marker matching is now done with trymatch() (independent of keywords).
85 *
86 * Revision 3.4 83/02/18 17:37:49 wft
87 * removed printing of new line after last file.
88 *
89 * Revision 3.3 82/12/04 12:48:55 wft
90 * Added LOCKER.
91 *
92 * Revision 3.2 82/11/28 18:24:17 wft
93 * removed Suffix; added ungetc to avoid skipping over trailing KDELIM.
94 *
95 * Revision 3.1 82/10/13 15:58:51 wft
96 * fixed type of variables receiving from getc() (char-->int).
97 */
98
99 #include "rcsbase.h"
100
101 static int match P((FILE*));
102 static int scanfile P((FILE*,char const*,int));
103 static void reportError P((char const*));
104
105 mainProg(identId, "ident", "$FreeBSD: stable/9/gnu/usr.bin/rcs/ident/ident.c 87625 2001-12-10 20:44:31Z peter $")
106 /* Ident searches the named files for all occurrences
107 * of the pattern $@: text $ where @ is a keyword.
108 */
109
110 {
111 FILE *fp;
112 int quiet = 0;
113 int status = EXIT_SUCCESS;
114 char const *a;
115
116 while ((a = *++argv) && *a=='-')
117 while (*++a)
118 switch (*a) {
119 case 'q':
120 quiet = 1;
121 break;
122
123 case 'V':
124 VOID printf("RCS version %s\n", RCS_version_string);
125 quiet = -1;
126 break;
127
128 default:
129 VOID fprintf(stderr,
130 "ident: usage: ident -{qV} [file...]\n"
131 );
132 exitmain(EXIT_FAILURE);
133 break;
134 }
135
136 if (0 <= quiet)
137 if (!a)
138 VOID scanfile(stdin, (char*)0, quiet);
139 else
140 do {
141 if (!(fp = fopen(a, FOPEN_RB))) {
142 reportError(a);
143 status = EXIT_FAILURE;
144 } else if (
145 scanfile(fp, a, quiet) != 0
146 || (argv[1] && putchar('\n') == EOF)
147 )
148 break;
149 } while ((a = *++argv));
150
151 if (ferror(stdout) || fclose(stdout)!=0) {
152 reportError("standard output");
153 status = EXIT_FAILURE;
154 }
155 exitmain(status);
156 }
157
158 #if RCS_lint
159 # define exiterr identExit
160 #endif
161 void
exiterr()162 exiterr()
163 {
164 _exit(EXIT_FAILURE);
165 }
166
167 static void
reportError(s)168 reportError(s)
169 char const *s;
170 {
171 int e = errno;
172 VOID fprintf(stderr, "%s error: ", cmdid);
173 errno = e;
174 perror(s);
175 }
176
177
178 static int
scanfile(file,name,quiet)179 scanfile(file, name, quiet)
180 register FILE *file;
181 char const *name;
182 int quiet;
183 /* Function: scan an open file with descriptor file for keywords.
184 * Return -1 if there's a write error; exit immediately on a read error.
185 */
186 {
187 register int c;
188
189 if (name) {
190 VOID printf("%s:\n", name);
191 if (ferror(stdout))
192 return -1;
193 } else
194 name = "standard input";
195 c = 0;
196 while (c != EOF || ! (feof(file)|ferror(file))) {
197 if (c == KDELIM) {
198 if ((c = match(file)))
199 continue;
200 if (ferror(stdout))
201 return -1;
202 quiet = true;
203 }
204 c = getc(file);
205 }
206 if (ferror(file) || fclose(file) != 0) {
207 reportError(name);
208 /*
209 * The following is equivalent to exit(EXIT_FAILURE), but we invoke
210 * exiterr to keep lint happy. The DOS and OS/2 ports need exiterr.
211 */
212 VOID fflush(stderr);
213 VOID fflush(stdout);
214 exiterr();
215 }
216 if (!quiet)
217 VOID fprintf(stderr, "%s warning: no id keywords in %s\n", cmdid, name);
218 return 0;
219 }
220
221
222
223 static int
match(fp)224 match(fp) /* group substring between two KDELIM's; then do pattern match */
225 register FILE *fp;
226 {
227 char line[BUFSIZ];
228 register int c;
229 register char * tp;
230
231 tp = line;
232 while ((c = getc(fp)) != VDELIM) {
233 if (c == EOF && feof(fp) | ferror(fp))
234 return c;
235 switch (ctab[c]) {
236 case LETTER: case Letter: case DIGIT:
237 *tp++ = c;
238 if (tp < line+sizeof(line)-4)
239 break;
240 /* fall into */
241 default:
242 return c ? c : '\n'/* anything but 0 or KDELIM or EOF */;
243 }
244 }
245 if (tp == line)
246 return c;
247 *tp++ = c;
248 if ((c = getc(fp)) != ' ')
249 return c ? c : '\n';
250 *tp++ = c;
251 while( (c = getc(fp)) != KDELIM ) {
252 if (c == EOF && feof(fp) | ferror(fp))
253 return c;
254 switch (ctab[c]) {
255 default:
256 *tp++ = c;
257 if (tp < line+sizeof(line)-2)
258 break;
259 /* fall into */
260 case NEWLN: case UNKN:
261 return c ? c : '\n';
262 }
263 }
264 if (tp[-1] != ' ')
265 return c;
266 *tp++ = c; /*append trailing KDELIM*/
267 *tp = '\0';
268 VOID printf(" %c%s\n", KDELIM, line);
269 return 0;
270 }
271