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