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