1 /* Extract RCS keyword string values from working files. */
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.
7 This file is part of RCS.
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)
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.
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.
24 Report problems and direct all questions to:
26 rcs-bugs@cs.purdue.edu
31 * Revision 5.10 1995/06/16 06:19:24 eggert
34 * Revision 5.9 1995/06/01 16:23:43 eggert
35 * (getoldkeys): Don't panic if a Name: is empty.
37 * Revision 5.8 1994/03/17 14:05:48 eggert
40 * Revision 5.7 1993/11/09 17:40:15 eggert
41 * Use simpler timezone parsing strategy now that we're using ISO 8601 format.
43 * Revision 5.6 1993/11/03 17:42:27 eggert
44 * Scan for Name keyword. Improve quality of diagnostics.
46 * Revision 5.5 1992/07/28 16:12:44 eggert
47 * Statement macro names now end in _.
49 * Revision 5.4 1991/08/19 03:13:55 eggert
52 * Revision 5.3 1991/04/21 11:58:25 eggert
53 * Shorten names to keep them distinct on shortname hosts.
55 * Revision 5.2 1990/10/04 06:30:20 eggert
56 * Parse time zone offsets; future RCS versions may output them.
58 * Revision 5.1 1990/09/20 02:38:56 eggert
59 * ci -k now checks dates more thoroughly.
61 * Revision 5.0 1990/08/22 08:12:53 eggert
62 * Retrieve old log message if there is one.
63 * Don't require final newline.
64 * Remove compile-time limits; use malloc instead. Tune.
65 * Permit dates past 1999/12/31. Ansify and Posixate.
67 * Revision 4.6 89/05/01 15:12:56 narten
68 * changed copyright header to reflect current distribution rules
70 * Revision 4.5 88/08/09 19:13:03 eggert
71 * Remove lint and speed up by making FILE *fp local, not global.
73 * Revision 4.4 87/12/18 11:44:21 narten
74 * more lint cleanups (Guy Harris)
76 * Revision 4.3 87/10/18 10:35:50 narten
77 * Updating version numbers. Changes relative to 1.1 actually relative
80 * Revision 1.3 87/09/24 14:00:00 narten
81 * Sources now pass through lint (if you ignore printf/sprintf/fprintf
84 * Revision 1.2 87/03/27 14:22:29 jenkins
87 * Revision 4.1 83/05/10 16:26:44 wft
88 * Added new markers Id and RCSfile; extraction added.
89 * Marker matching with trymatch().
91 * Revision 3.2 82/12/24 12:08:26 wft
92 * added missing #endif.
94 * Revision 3.1 82/12/04 13:22:41 wft
101 libId(keepId, "$FreeBSD$")
103 static int badly_terminated P((void));
104 static int checknum P((char const*));
105 static int get0val P((int,RILE*,struct buf*,int));
106 static int getval P((RILE*,struct buf*,int));
107 static int keepdate P((RILE*));
108 static int keepid P((int,RILE*,struct buf*));
109 static int keeprev P((RILE*));
112 struct buf prevauthor, prevdate, prevname, prevrev, prevstate;
117 /* Function: Tries to read keyword values for author, date,
118 * revision number, and state out of the file fp.
119 * If fp is null, workname is opened and closed instead of using fp.
120 * The results are placed into
121 * prevauthor, prevdate, prevname, prevrev, prevstate.
122 * Aborts immediately if it finds an error and returns false.
123 * If it returns true, it doesn't mean that any of the
124 * values were found; instead, check to see whether the corresponding arrays
125 * contain the empty string.
129 char keyword[keylength+1];
137 needs_closing = false;
139 if (!(fp = Iopen(workname, FOPEN_R_WORK, (struct stat*)0))) {
143 needs_closing = true;
146 /* initialize to empty */
147 bufscpy(&prevauthor, "");
148 bufscpy(&prevdate, "");
149 bufscpy(&prevname, ""); prevname_found = 0;
150 bufscpy(&prevrev, "");
151 bufscpy(&prevstate, "");
153 c = '\0'; /* anything but KDELIM */
157 /* try to get keyword */
160 Igeteof_(fp, c, goto ok;)
163 if (keyword+keylength <= tp)
168 case '\n': case KDELIM: case VDELIM:
174 if (c!=VDELIM) continue;
176 Igeteof_(fp, c, break;)
178 case ' ': case '\t': break;
182 switch (trymatch(keyword)) {
184 if (!keepid(0, fp, &prevauthor))
189 if (!(c = keepdate(fp)))
196 getval(fp, (struct buf*)0, false) &&
198 (c = keepdate(fp)) &&
199 keepid(c, fp, &prevauthor) &&
200 keepid(0, fp, &prevstate)
203 /* Skip either ``who'' (new form) or ``Locker: who'' (old). */
204 if (getval(fp, (struct buf*)0, true) &&
205 getval(fp, (struct buf*)0, true))
213 (void) getval(fp, (struct buf*)0, false);
219 if (!getval(fp, (struct buf*)0, false))
224 if (getval(fp, &prevname, false)) {
225 if (*prevname.string)
226 checkssym(prevname.string);
237 if (!keepid(0, fp, &prevstate))
245 Igeteof_(fp, c, c=0;)
247 workerror("closing %c missing on keyword", KDELIM);
250 if (prevname_found &&
251 *prevauthor.string && *prevdate.string &&
252 *prevrev.string && *prevstate.string
256 Igeteof_(fp, c, break;)
271 workerror("badly terminated keyword value");
276 getval(fp, target, optional)
280 /* Reads a keyword value from FP into TARGET.
281 * Returns true if one is found, false otherwise.
282 * Does not modify target if it is 0.
283 * Do not report an error if OPTIONAL is set and KDELIM is found instead.
287 Igeteof_(fp, c, return badly_terminated();)
288 return get0val(c, fp, target, optional);
292 get0val(c, fp, target, optional)
297 /* Reads a keyword value from C+FP into TARGET, perhaps OPTIONALly.
298 * Same as getval, except C is the lookahead character.
300 { register char * tp;
307 tlim = tp + target->size;
318 tp = bufenlarge(target, &tlim);
327 VOID printf("getval: %s\n", target);
333 if (!got1 && optional)
338 return badly_terminated();
340 Igeteof_(fp, c, return badly_terminated();)
348 /* Function: reads a date prevdate; checks format
349 * Return 0 on error, lookahead character otherwise.
352 struct buf prevday, prevtime;
356 bufautobegin(&prevday);
357 if (getval(fp,&prevday,false)) {
358 bufautobegin(&prevtime);
359 if (getval(fp,&prevtime,false)) {
360 Igeteof_(fp, c, c=0;)
362 register char const *d = prevday.string, *t = prevtime.string;
363 bufalloc(&prevdate, strlen(d) + strlen(t) + 9);
364 VOID sprintf(prevdate.string, "%s%s %s%s",
365 /* Parse dates put out by old versions of RCS. */
366 isdigit(d[0]) && isdigit(d[1]) && !isdigit(d[2])
369 strchr(t,'-') || strchr(t,'+') ? "" : "+0000"
373 bufautoend(&prevtime);
375 bufautoend(&prevday);
384 /* Get previous identifier from C+FP into B. */
387 Igeteof_(fp, c, return false;)
388 if (!get0val(c, fp, b, false))
397 /* Get previous revision from FP into prevrev. */
399 return getval(fp,&prevrev,false) && checknum(prevrev.string);
407 register char const *sp;
408 register int dotcount = 0;
428 workerror("%s is not a revision number", s);
436 /* Print the keyword values found. */
438 char const cmdid[] ="keeptest";
442 int argc; char *argv[];
446 getoldkeys((RILE*)0);
447 VOID printf("%s: revision: %s, date: %s, author: %s, name: %s, state: %s\n",
448 *argv, prevrev.string, prevdate.string, prevauthor.string, prevname.string, prevstate.string);
450 exitmain(EXIT_SUCCESS);