Annotation of src/usr.bin/cvs/rcs.c, Revision 1.24
1.24 ! jfb 1: /* $OpenBSD: rcs.c,v 1.23 2005/01/24 20:43:12 jfb Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.15 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.15 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.15 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.15 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.15 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/param.h>
28: #include <sys/queue.h>
29: #include <sys/stat.h>
30:
31: #include <errno.h>
32: #include <stdio.h>
33: #include <ctype.h>
34: #include <stdlib.h>
35: #include <string.h>
36:
37: #include "rcs.h"
38: #include "log.h"
39:
1.18 jfb 40: #define RCS_BUFSIZE 16384
41: #define RCS_BUFEXTSIZE 8192
1.1 jfb 42:
43:
44: /* RCS token types */
45: #define RCS_TOK_ERR -1
46: #define RCS_TOK_EOF 0
47: #define RCS_TOK_NUM 1
48: #define RCS_TOK_ID 2
49: #define RCS_TOK_STRING 3
50: #define RCS_TOK_SCOLON 4
51: #define RCS_TOK_COLON 5
52:
53:
54: #define RCS_TOK_HEAD 8
55: #define RCS_TOK_BRANCH 9
56: #define RCS_TOK_ACCESS 10
57: #define RCS_TOK_SYMBOLS 11
58: #define RCS_TOK_LOCKS 12
59: #define RCS_TOK_COMMENT 13
60: #define RCS_TOK_EXPAND 14
61: #define RCS_TOK_DATE 15
62: #define RCS_TOK_AUTHOR 16
63: #define RCS_TOK_STATE 17
64: #define RCS_TOK_NEXT 18
65: #define RCS_TOK_BRANCHES 19
66: #define RCS_TOK_DESC 20
67: #define RCS_TOK_LOG 21
68: #define RCS_TOK_TEXT 22
69: #define RCS_TOK_STRICT 23
70:
71: #define RCS_ISKEY(t) (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
72:
73:
74: #define RCS_NOSCOL 0x01 /* no terminating semi-colon */
75: #define RCS_VOPT 0x02 /* value is optional */
76:
77:
78: /* opaque parse data */
79: struct rcs_pdata {
1.18 jfb 80: u_int rp_lines;
1.1 jfb 81:
82: char *rp_buf;
83: size_t rp_blen;
1.18 jfb 84: char *rp_bufend;
1.1 jfb 85:
86: /* pushback token buffer */
87: char rp_ptok[128];
88: int rp_pttype; /* token type, RCS_TOK_ERR if no token */
89:
90: FILE *rp_file;
91: };
92:
93:
94: struct rcs_line {
95: char *rl_line;
96: int rl_lineno;
97: TAILQ_ENTRY(rcs_line) rl_list;
98: };
1.5 vincent 99: TAILQ_HEAD(rcs_tqh, rcs_line);
1.1 jfb 100:
101: struct rcs_foo {
102: int rl_nblines;
103: char *rl_data;
1.5 vincent 104: struct rcs_tqh rl_lines;
1.1 jfb 105: };
106:
107: static int rcs_parse_admin (RCSFILE *);
108: static int rcs_parse_delta (RCSFILE *);
109: static int rcs_parse_deltatext (RCSFILE *);
110:
1.7 jfb 111: static int rcs_parse_access (RCSFILE *);
112: static int rcs_parse_symbols (RCSFILE *);
113: static int rcs_parse_locks (RCSFILE *);
114: static int rcs_parse_branches (RCSFILE *, struct rcs_delta *);
115: static void rcs_freedelta (struct rcs_delta *);
116: static void rcs_freepdata (struct rcs_pdata *);
117: static int rcs_gettok (RCSFILE *);
118: static int rcs_pushtok (RCSFILE *, const char *, int);
1.18 jfb 119: static int rcs_growbuf (RCSFILE *);
1.7 jfb 120: static int rcs_patch_lines (struct rcs_foo *, struct rcs_foo *);
121:
122: static struct rcs_delta* rcs_findrev (RCSFILE *, RCSNUM *);
123: static struct rcs_foo* rcs_splitlines (const char *);
124: static void rcs_freefoo (struct rcs_foo *);
1.1 jfb 125:
126: #define RCS_TOKSTR(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
127: #define RCS_TOKLEN(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_blen
128:
129:
1.20 jfb 130: static struct rcs_kfl {
131: char rk_char;
132: int rk_val;
133: } rcs_kflags[] = {
134: { 'k', RCS_KWEXP_NAME },
135: { 'v', RCS_KWEXP_VAL },
136: { 'l', RCS_KWEXP_LKR },
137: { 'o', RCS_KWEXP_OLD },
138: { 'b', RCS_KWEXP_NONE },
139: };
140:
1.1 jfb 141: static struct rcs_key {
142: char rk_str[16];
143: int rk_id;
144: int rk_val;
145: int rk_flags;
146: } rcs_keys[] = {
147: { "access", RCS_TOK_ACCESS, RCS_TOK_ID, RCS_VOPT },
148: { "author", RCS_TOK_AUTHOR, RCS_TOK_STRING, 0 },
149: { "branch", RCS_TOK_BRANCH, RCS_TOK_NUM, RCS_VOPT },
150: { "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM, RCS_VOPT },
151: { "comment", RCS_TOK_COMMENT, RCS_TOK_STRING, RCS_VOPT },
152: { "date", RCS_TOK_DATE, RCS_TOK_NUM, 0 },
153: { "desc", RCS_TOK_DESC, RCS_TOK_STRING, RCS_NOSCOL },
154: { "expand", RCS_TOK_EXPAND, RCS_TOK_STRING, RCS_VOPT },
155: { "head", RCS_TOK_HEAD, RCS_TOK_NUM, RCS_VOPT },
156: { "locks", RCS_TOK_LOCKS, RCS_TOK_ID, 0 },
157: { "log", RCS_TOK_LOG, RCS_TOK_STRING, RCS_NOSCOL },
158: { "next", RCS_TOK_NEXT, RCS_TOK_NUM, RCS_VOPT },
159: { "state", RCS_TOK_STATE, RCS_TOK_STRING, RCS_VOPT },
160: { "strict", RCS_TOK_STRICT, 0, 0, },
161: { "symbols", RCS_TOK_SYMBOLS, 0, 0 },
162: { "text", RCS_TOK_TEXT, RCS_TOK_STRING, RCS_NOSCOL },
163: };
164:
1.18 jfb 165: #define RCS_NKEYS (sizeof(rcs_keys)/sizeof(rcs_keys[0]))
1.1 jfb 166:
167:
168: /*
169: * rcs_open()
170: *
171: * Open a file containing RCS-formatted information. The file's path is
172: * given in <path>, and the opening mode is given in <mode>, which is either
173: * RCS_MODE_READ, RCS_MODE_WRITE, or RCS_MODE_RDWR. If the mode requests write
174: * access and the file does not exist, it will be created.
175: * The file isn't actually parsed by rcs_open(); parsing is delayed until the
176: * first operation that requires information from the file.
177: * Returns a handle to the opened file on success, or NULL on failure.
178: */
179: RCSFILE*
180: rcs_open(const char *path, u_int mode)
181: {
182: RCSFILE *rfp;
183: struct stat st;
184:
185: if ((stat(path, &st) == -1) && (errno == ENOENT) &&
186: !(mode & RCS_MODE_WRITE)) {
187: cvs_log(LP_ERRNO, "cannot open RCS file `%s'", path);
188: return (NULL);
189: }
190:
191: rfp = (RCSFILE *)malloc(sizeof(*rfp));
192: if (rfp == NULL) {
193: cvs_log(LP_ERRNO, "failed to allocate RCS file structure");
194: return (NULL);
195: }
196: memset(rfp, 0, sizeof(*rfp));
197:
198: rfp->rf_head = rcsnum_alloc();
199: if (rfp->rf_head == NULL) {
200: free(rfp);
201: return (NULL);
202: }
203:
1.11 joris 204: rfp->rf_branch = rcsnum_alloc();
205: if (rfp->rf_branch == NULL) {
206: rcs_close(rfp);
207: return (NULL);
208: }
209:
1.1 jfb 210: rfp->rf_path = strdup(path);
211: if (rfp->rf_path == NULL) {
212: cvs_log(LP_ERRNO, "failed to duplicate RCS file path");
213: rcs_close(rfp);
214: return (NULL);
215: }
216:
217: rcsnum_aton(RCS_HEAD_INIT, NULL, rfp->rf_head);
218:
219: rfp->rf_ref = 1;
220: rfp->rf_flags |= RCS_RF_SLOCK;
221: rfp->rf_mode = mode;
222:
223: TAILQ_INIT(&(rfp->rf_delta));
224: TAILQ_INIT(&(rfp->rf_symbols));
225: TAILQ_INIT(&(rfp->rf_locks));
226:
227: if (rcs_parse(rfp) < 0) {
228: rcs_close(rfp);
229: return (NULL);
230: }
231:
232: return (rfp);
233: }
234:
235:
236: /*
237: * rcs_close()
238: *
239: * Close an RCS file handle.
240: */
241: void
242: rcs_close(RCSFILE *rfp)
243: {
244: struct rcs_delta *rdp;
1.13 jfb 245: struct rcs_lock *rlp;
246: struct rcs_sym *rsp;
1.1 jfb 247:
248: if (rfp->rf_ref > 1) {
249: rfp->rf_ref--;
250: return;
251: }
252:
253: while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
254: rdp = TAILQ_FIRST(&(rfp->rf_delta));
255: TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
256: rcs_freedelta(rdp);
257: }
258:
1.13 jfb 259: while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
260: rsp = TAILQ_FIRST(&(rfp->rf_symbols));
261: TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
262: rcsnum_free(rsp->rs_num);
263: free(rsp->rs_name);
264: free(rsp);
265: }
266:
267: while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
268: rlp = TAILQ_FIRST(&(rfp->rf_locks));
269: TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
270: rcsnum_free(rlp->rl_num);
271: free(rlp);
272: }
273:
1.1 jfb 274: if (rfp->rf_head != NULL)
275: rcsnum_free(rfp->rf_head);
1.11 joris 276: if (rfp->rf_branch != NULL)
277: rcsnum_free(rfp->rf_branch);
1.1 jfb 278:
279: if (rfp->rf_path != NULL)
280: free(rfp->rf_path);
281: if (rfp->rf_comment != NULL)
282: free(rfp->rf_comment);
283: if (rfp->rf_expand != NULL)
284: free(rfp->rf_expand);
285: if (rfp->rf_desc != NULL)
286: free(rfp->rf_desc);
287: free(rfp);
288: }
289:
290:
291: /*
292: * rcs_write()
293: *
294: * Write the contents of the RCS file handle <rfp> to disk in the file whose
295: * path is in <rf_path>.
296: * Returns 0 on success, or -1 on failure.
297: */
298: int
299: rcs_write(RCSFILE *rfp)
300: {
301: FILE *fp;
1.7 jfb 302: char buf[1024], numbuf[64], *cp;
303: size_t rlen, len;
1.1 jfb 304: struct rcs_sym *symp;
305: struct rcs_delta *rdp;
306:
307: if (rfp->rf_flags & RCS_RF_SYNCED)
308: return (0);
309:
310: fp = fopen(rfp->rf_path, "w");
311: if (fp == NULL) {
312: cvs_log(LP_ERRNO, "failed to open RCS output file `%s'",
313: rfp->rf_path);
314: return (-1);
315: }
316:
317: rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
318: fprintf(fp, "head\t%s;\n", numbuf);
319: fprintf(fp, "access;\n");
320:
321: fprintf(fp, "symbols\n");
322: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
323: rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
324: snprintf(buf, sizeof(buf), "%s:%s", symp->rs_name, numbuf);
325: fprintf(fp, "\t%s", buf);
326: if (symp != TAILQ_LAST(&(rfp->rf_symbols), rcs_slist))
327: fputc('\n', fp);
328: }
329: fprintf(fp, ";\n");
330:
331: fprintf(fp, "locks;");
332:
333: if (rfp->rf_flags & RCS_RF_SLOCK)
334: fprintf(fp, " strict;");
335: fputc('\n', fp);
336:
337: if (rfp->rf_comment != NULL)
338: fprintf(fp, "comment\t@%s@;\n", rfp->rf_comment);
339:
340: if (rfp->rf_expand != NULL)
341: fprintf(fp, "expand @ %s @;\n", rfp->rf_expand);
342:
343: fprintf(fp, "\n\n");
344:
345: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
346: fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
347: sizeof(numbuf)));
348: fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
349: rdp->rd_date.tm_year, rdp->rd_date.tm_mon + 1,
350: rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
351: rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
352: fprintf(fp, "\tauthor %s;\tstate %s;\n",
353: rdp->rd_author, rdp->rd_state);
354: fprintf(fp, "branches;\n");
355: fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
356: numbuf, sizeof(numbuf)));
357: }
358:
359: fprintf(fp, "\ndesc\n@%s@\n\n", rfp->rf_desc);
360:
361: /* deltatexts */
362: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
363: fprintf(fp, "\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
364: sizeof(numbuf)));
1.7 jfb 365: fprintf(fp, "log\n@%s@\ntext\n@", rdp->rd_log);
366:
367: cp = rdp->rd_text;
368: do {
369: len = sizeof(buf);
370: rlen = rcs_stresc(1, cp, buf, &len);
371: fprintf(fp, "%s", buf);
372: cp += rlen;
373: } while (len != 0);
374: fprintf(fp, "@\n\n");
1.1 jfb 375: }
376: fclose(fp);
377:
378: rfp->rf_flags |= RCS_RF_SYNCED;
379:
380: return (0);
381: }
382:
383:
384: /*
385: * rcs_addsym()
386: *
387: * Add a symbol to the list of symbols for the RCS file <rfp>. The new symbol
388: * is named <sym> and is bound to the RCS revision <snum>.
389: * Returns 0 on success, or -1 on failure.
390: */
391: int
392: rcs_addsym(RCSFILE *rfp, const char *sym, RCSNUM *snum)
393: {
394: struct rcs_sym *symp;
395:
396: /* first look for duplication */
397: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
398: if (strcmp(symp->rs_name, sym) == 0) {
1.17 jfb 399: cvs_log(LP_ERR, "attempt to add duplicate symbol `%s'",
400: sym);
1.1 jfb 401: return (-1);
402: }
403: }
404:
405: symp = (struct rcs_sym *)malloc(sizeof(*symp));
406: if (symp == NULL) {
407: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
408: return (-1);
409: }
410:
411: symp->rs_name = strdup(sym);
1.10 joris 412: if (symp->rs_name == NULL) {
413: cvs_log(LP_ERRNO, "failed to duplicate symbol");
414: free(symp);
415: return (-1);
416: }
417:
1.1 jfb 418: symp->rs_num = rcsnum_alloc();
1.11 joris 419: if (symp->rs_num == NULL) {
1.17 jfb 420: free(symp->rs_name);
1.11 joris 421: free(symp);
422: return (-1);
423: }
1.1 jfb 424: rcsnum_cpy(snum, symp->rs_num, 0);
425:
426: TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
427:
428: /* not synced anymore */
429: rfp->rf_flags &= ~RCS_RF_SYNCED;
430:
431: return (0);
432: }
433:
434:
435: /*
436: * rcs_patch()
437: *
438: * Apply an RCS-format patch pointed to by <patch> to the file contents
439: * found in <data>.
440: * Returns 0 on success, or -1 on failure.
441: */
442: BUF*
443: rcs_patch(const char *data, const char *patch)
444: {
1.5 vincent 445: struct rcs_foo *dlines, *plines;
446: struct rcs_line *lp;
1.1 jfb 447: size_t len;
1.5 vincent 448: int lineno;
1.1 jfb 449: BUF *res;
450:
451: len = strlen(data);
452: res = cvs_buf_alloc(len, BUF_AUTOEXT);
453: if (res == NULL)
454: return (NULL);
455:
456: dlines = rcs_splitlines(data);
1.17 jfb 457: if (dlines == NULL) {
458: cvs_buf_free(res);
1.1 jfb 459: return (NULL);
1.17 jfb 460: }
1.5 vincent 461:
1.1 jfb 462: plines = rcs_splitlines(patch);
1.5 vincent 463: if (plines == NULL) {
1.17 jfb 464: cvs_buf_free(res);
1.5 vincent 465: rcs_freefoo(dlines);
1.1 jfb 466: return (NULL);
1.5 vincent 467: }
468:
469: if (rcs_patch_lines(dlines, plines) < 0) {
1.17 jfb 470: cvs_buf_free(res);
1.5 vincent 471: rcs_freefoo(plines);
472: rcs_freefoo(dlines);
473: return (NULL);
474: }
475:
476: lineno = 0;
477: TAILQ_FOREACH(lp, &dlines->rl_lines, rl_list) {
478: if (lineno != 0)
479: cvs_buf_fappend(res, "%s\n", lp->rl_line);
480: lineno++;
481: }
482:
483: rcs_freefoo(dlines);
484: rcs_freefoo(plines);
485: return (res);
486: }
487:
1.7 jfb 488: static int
1.5 vincent 489: rcs_patch_lines(struct rcs_foo *dlines, struct rcs_foo *plines)
490: {
491: char op, *ep;
492: struct rcs_line *lp, *dlp, *ndlp;
493: int i, lineno, nbln;
1.1 jfb 494:
495: dlp = TAILQ_FIRST(&(dlines->rl_lines));
496: lp = TAILQ_FIRST(&(plines->rl_lines));
497:
498: /* skip first bogus line */
499: for (lp = TAILQ_NEXT(lp, rl_list); lp != NULL;
500: lp = TAILQ_NEXT(lp, rl_list)) {
501: op = *(lp->rl_line);
502: lineno = (int)strtol((lp->rl_line + 1), &ep, 10);
503: if ((lineno > dlines->rl_nblines) || (lineno <= 0) ||
504: (*ep != ' ')) {
505: cvs_log(LP_ERR,
506: "invalid line specification in RCS patch");
507: return (NULL);
508: }
509: ep++;
510: nbln = (int)strtol(ep, &ep, 10);
511: if ((nbln <= 0) || (*ep != '\0')) {
512: cvs_log(LP_ERR,
513: "invalid line number specification in RCS patch");
514: return (NULL);
515: }
516:
517: /* find the appropriate line */
518: for (;;) {
519: if (dlp == NULL)
520: break;
521: if (dlp->rl_lineno == lineno)
522: break;
523: if (dlp->rl_lineno > lineno) {
524: dlp = TAILQ_PREV(dlp, rcs_tqh, rl_list);
1.14 deraadt 525: } else if (dlp->rl_lineno < lineno) {
1.1 jfb 526: ndlp = TAILQ_NEXT(dlp, rl_list);
527: if (ndlp->rl_lineno > lineno)
528: break;
529: dlp = ndlp;
530: }
531: }
532: if (dlp == NULL) {
533: cvs_log(LP_ERR,
534: "can't find referenced line in RCS patch");
535: return (NULL);
536: }
537:
538: if (op == 'd') {
539: for (i = 0; (i < nbln) && (dlp != NULL); i++) {
540: ndlp = TAILQ_NEXT(dlp, rl_list);
541: TAILQ_REMOVE(&(dlines->rl_lines), dlp, rl_list);
542: dlp = ndlp;
543: }
1.14 deraadt 544: } else if (op == 'a') {
1.1 jfb 545: for (i = 0; i < nbln; i++) {
546: ndlp = lp;
547: lp = TAILQ_NEXT(lp, rl_list);
548: if (lp == NULL) {
549: cvs_log(LP_ERR, "truncated RCS patch");
1.5 vincent 550: return (-1);
1.1 jfb 551: }
552: TAILQ_REMOVE(&(plines->rl_lines), lp, rl_list);
553: TAILQ_INSERT_AFTER(&(dlines->rl_lines), dlp,
554: lp, rl_list);
555: dlp = lp;
556:
557: /* we don't want lookup to block on those */
558: lp->rl_lineno = lineno;
559:
560: lp = ndlp;
561: }
1.14 deraadt 562: } else {
1.1 jfb 563: cvs_log(LP_ERR, "unknown RCS patch operation `%c'", op);
1.5 vincent 564: return (-1);
1.1 jfb 565: }
566:
567: /* last line of the patch, done */
568: if (lp->rl_lineno == plines->rl_nblines)
569: break;
570: }
571:
572: /* once we're done patching, rebuild the line numbers */
1.2 vincent 573: lineno = 0;
1.5 vincent 574: TAILQ_FOREACH(lp, &(dlines->rl_lines), rl_list)
1.1 jfb 575: lp->rl_lineno = lineno++;
576: dlines->rl_nblines = lineno - 1;
577:
1.5 vincent 578: return (0);
1.1 jfb 579: }
580:
581:
582: /*
583: * rcs_getrev()
584: *
585: * Get the whole contents of revision <rev> from the RCSFILE <rfp>. The
1.4 vincent 586: * returned buffer is dynamically allocated and should be released using
587: * cvs_buf_free() once the caller is done using it.
1.1 jfb 588: */
589: BUF*
590: rcs_getrev(RCSFILE *rfp, RCSNUM *rev)
591: {
592: int res;
593: size_t len;
594: void *bp;
595: RCSNUM *crev;
596: BUF *rbuf;
597: struct rcs_delta *rdp = NULL;
598:
599: res = rcsnum_cmp(rfp->rf_head, rev, 0);
600: if (res == 1) {
601: cvs_log(LP_ERR, "sorry, can't travel in the future yet");
602: return (NULL);
1.14 deraadt 603: } else {
1.1 jfb 604: rdp = rcs_findrev(rfp, rfp->rf_head);
605: if (rdp == NULL) {
606: cvs_log(LP_ERR, "failed to get RCS HEAD revision");
607: return (NULL);
608: }
609:
610: len = strlen(rdp->rd_text);
611: rbuf = cvs_buf_alloc(len, BUF_AUTOEXT);
612: if (rbuf == NULL)
613: return (NULL);
614: cvs_buf_append(rbuf, rdp->rd_text, len);
615:
616: if (res != 0) {
617: /* Apply patches backwards to get the right version.
618: * This will need some rework to support sub branches.
619: */
1.23 jfb 620: if ((crev = rcsnum_alloc()) == NULL) {
1.17 jfb 621: cvs_buf_free(rbuf);
1.11 joris 622: return (NULL);
1.17 jfb 623: }
1.1 jfb 624: rcsnum_cpy(rfp->rf_head, crev, 0);
625: do {
626: crev->rn_id[crev->rn_len - 1]--;
627: rdp = rcs_findrev(rfp, crev);
1.17 jfb 628: if (rdp == NULL) {
629: rcsnum_free(crev);
630: cvs_buf_free(rbuf);
1.1 jfb 631: return (NULL);
1.17 jfb 632: }
1.1 jfb 633:
1.23 jfb 634: if (cvs_buf_putc(rbuf, '\0') < 0) {
635: rcsnum_free(crev);
636: cvs_buf_free(rbuf);
637: return (NULL);
638: }
1.1 jfb 639: bp = cvs_buf_release(rbuf);
640: rbuf = rcs_patch((char *)bp, rdp->rd_text);
641: if (rbuf == NULL)
642: break;
643: } while (rcsnum_cmp(crev, rev, 0) != 0);
644:
645: rcsnum_free(crev);
646: }
647: }
648:
649: return (rbuf);
1.16 jfb 650: }
651:
652:
653: /*
654: * rcs_gethead()
655: *
656: * Get the head revision for the RCS file <rf>.
657: */
658: BUF*
659: rcs_gethead(RCSFILE *rf)
660: {
661: return rcs_getrev(rf, rf->rf_head);
1.1 jfb 662: }
663:
664:
665: /*
666: * rcs_getrevbydate()
667: *
668: * Get an RCS revision by a specific date.
669: */
670: RCSNUM*
671: rcs_getrevbydate(RCSFILE *rfp, struct tm *date)
672: {
673: return (NULL);
674: }
675:
676:
677: /*
678: * rcs_findrev()
679: *
680: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
681: * The revision number is given in <rev>.
682: * Returns a pointer to the delta on success, or NULL on failure.
683: */
684: static struct rcs_delta*
685: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
686: {
687: u_int cmplen;
688: struct rcs_delta *rdp;
689: struct rcs_dlist *hp;
1.6 vincent 690: int found;
691:
1.1 jfb 692: cmplen = 2;
693: hp = &(rfp->rf_delta);
694:
1.6 vincent 695: do {
696: found = 0;
697: TAILQ_FOREACH(rdp, hp, rd_list) {
698: if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
699: if (cmplen == rev->rn_len)
700: return (rdp);
1.1 jfb 701:
1.6 vincent 702: hp = &(rdp->rd_snodes);
703: cmplen += 2;
704: found = 1;
705: break;
706: }
1.1 jfb 707: }
1.6 vincent 708: } while (found && cmplen < rev->rn_len);
1.1 jfb 709:
710: return (NULL);
1.20 jfb 711: }
712:
713:
714: /*
715: * rcs_kflag_get()
716: *
717: * Get the keyword expansion mode from a set of character flags given in
718: * <flags> and return the appropriate flag mask. In case of an error, the
719: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
720: */
721: int
722: rcs_kflag_get(const char *flags)
723: {
724: int fl;
725: size_t len;
726: const char *fp;
727:
728: fl = 0;
729: len = strlen(flags);
730:
731: for (fp = flags; *fp != '\0'; fp++) {
732: if (*fp == 'k')
733: fl |= RCS_KWEXP_NAME;
734: else if (*fp == 'v')
735: fl |= RCS_KWEXP_VAL;
736: else if (*fp == 'l')
737: fl |= RCS_KWEXP_LKR;
738: else if (*fp == 'o') {
739: if (len != 1)
740: fl |= RCS_KWEXP_ERR;
741: fl |= RCS_KWEXP_OLD;
742: } else if (*fp == 'b') {
743: if (len != 1)
744: fl |= RCS_KWEXP_ERR;
745: } else /* unknown letter */
746: fl |= RCS_KWEXP_ERR;
747: }
748:
749: return (fl);
1.1 jfb 750: }
751:
1.21 jfb 752: void
753: rcs_kflag_usage(void)
754: {
755: fprintf(stderr, "Valid expansion modes include:\n"
1.22 jfb 756: "\t-kkv\tGenerate keywords using the default form.\n"
757: "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
758: "\t-kk\tGenerate only keyword names in keyword strings.\n"
759: "\t-kv\tGenerate only keyword values in keyword strings.\n"
760: "\t-ko\tGenerate old keyword string "
1.21 jfb 761: "(no changes from checked in file).\n"
1.22 jfb 762: "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21 jfb 763: }
1.1 jfb 764:
765: /*
766: * rcs_parse()
767: *
768: * Parse the contents of file <path>, which are in the RCS format.
769: * Returns 0 on success, or -1 on failure.
770: */
771: int
772: rcs_parse(RCSFILE *rfp)
773: {
774: int ret;
775: struct rcs_pdata *pdp;
776:
777: if (rfp->rf_flags & RCS_RF_PARSED)
778: return (0);
779:
780: pdp = (struct rcs_pdata *)malloc(sizeof(*pdp));
781: if (pdp == NULL) {
782: cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
783: return (-1);
784: }
785: memset(pdp, 0, sizeof(*pdp));
786:
1.18 jfb 787: pdp->rp_lines = 0;
1.1 jfb 788: pdp->rp_pttype = RCS_TOK_ERR;
789:
790: pdp->rp_file = fopen(rfp->rf_path, "r");
791: if (pdp->rp_file == NULL) {
792: cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
793: rcs_freepdata(pdp);
794: return (-1);
795: }
796:
797: pdp->rp_buf = (char *)malloc(RCS_BUFSIZE);
798: if (pdp->rp_buf == NULL) {
799: cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
800: rcs_freepdata(pdp);
801: return (-1);
802: }
803: pdp->rp_blen = RCS_BUFSIZE;
1.18 jfb 804: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1 jfb 805:
806: /* ditch the strict lock */
807: rfp->rf_flags &= ~RCS_RF_SLOCK;
808: rfp->rf_pdata = pdp;
809:
810: if (rcs_parse_admin(rfp) < 0) {
811: rcs_freepdata(pdp);
812: return (-1);
813: }
814:
815: for (;;) {
816: ret = rcs_parse_delta(rfp);
817: if (ret == 0)
818: break;
819: else if (ret == -1) {
820: rcs_freepdata(pdp);
821: return (-1);
822: }
823: }
824:
825: ret = rcs_gettok(rfp);
826: if (ret != RCS_TOK_DESC) {
827: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
828: RCS_TOKSTR(rfp));
829: rcs_freepdata(pdp);
830: return (-1);
831: }
832:
833: ret = rcs_gettok(rfp);
834: if (ret != RCS_TOK_STRING) {
835: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
836: RCS_TOKSTR(rfp));
837: rcs_freepdata(pdp);
838: return (-1);
839: }
840:
841: rfp->rf_desc = strdup(RCS_TOKSTR(rfp));
1.10 joris 842: if (rfp->rf_desc == NULL) {
843: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
844: rcs_freepdata(pdp);
845: return (-1);
846: }
1.1 jfb 847:
848: for (;;) {
849: ret = rcs_parse_deltatext(rfp);
850: if (ret == 0)
851: break;
852: else if (ret == -1) {
853: rcs_freepdata(pdp);
854: return (-1);
855: }
856: }
857:
858: cvs_log(LP_DEBUG, "RCS file `%s' parsed OK (%u lines)", rfp->rf_path,
1.18 jfb 859: pdp->rp_lines);
1.1 jfb 860:
861: rcs_freepdata(pdp);
862:
863: rfp->rf_pdata = NULL;
864: rfp->rf_flags |= RCS_RF_PARSED|RCS_RF_SYNCED;
865:
866: return (0);
867: }
868:
869:
870: /*
871: * rcs_parse_admin()
872: *
873: * Parse the administrative portion of an RCS file.
874: * Returns 0 on success, or -1 on failure.
875: */
876: static int
877: rcs_parse_admin(RCSFILE *rfp)
878: {
879: u_int i;
880: int tok, ntok, hmask;
881: struct rcs_key *rk;
882:
883: /* hmask is a mask of the headers already encountered */
884: hmask = 0;
885: for (;;) {
886: tok = rcs_gettok(rfp);
887: if (tok == RCS_TOK_ERR) {
888: cvs_log(LP_ERR, "parse error in RCS admin section");
889: return (-1);
1.14 deraadt 890: } else if (tok == RCS_TOK_NUM) {
1.1 jfb 891: /* assume this is the start of the first delta */
892: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
893: return (0);
894: }
895:
896: rk = NULL;
1.18 jfb 897: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 898: if (rcs_keys[i].rk_id == tok)
899: rk = &(rcs_keys[i]);
900:
901: if (hmask & (1 << tok)) {
902: cvs_log(LP_ERR, "duplicate RCS key");
903: return (-1);
904: }
905: hmask |= (1 << tok);
906:
907: switch (tok) {
908: case RCS_TOK_HEAD:
909: case RCS_TOK_BRANCH:
910: case RCS_TOK_COMMENT:
911: case RCS_TOK_EXPAND:
912: ntok = rcs_gettok(rfp);
913: if (ntok == RCS_TOK_SCOLON)
914: break;
915: if (ntok != rk->rk_val) {
916: cvs_log(LP_ERR,
917: "invalid value type for RCS key `%s'",
918: rk->rk_str);
919: }
920:
921: if (tok == RCS_TOK_HEAD) {
922: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
923: rfp->rf_head);
1.14 deraadt 924: } else if (tok == RCS_TOK_BRANCH) {
1.1 jfb 925: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
926: rfp->rf_branch);
1.14 deraadt 927: } else if (tok == RCS_TOK_COMMENT) {
1.1 jfb 928: rfp->rf_comment = strdup(RCS_TOKSTR(rfp));
1.10 joris 929: if (rfp->rf_comment == NULL) {
930: cvs_log(LP_ERRNO,
931: "failed to duplicate rcs token");
932: return (-1);
933: }
1.14 deraadt 934: } else if (tok == RCS_TOK_EXPAND) {
1.1 jfb 935: rfp->rf_expand = strdup(RCS_TOKSTR(rfp));
1.10 joris 936: if (rfp->rf_expand == NULL) {
937: cvs_log(LP_ERRNO,
938: "failed to duplicate rcs token");
939: return (-1);
940: }
1.1 jfb 941: }
942:
943: /* now get the expected semi-colon */
944: ntok = rcs_gettok(rfp);
945: if (ntok != RCS_TOK_SCOLON) {
946: cvs_log(LP_ERR,
947: "missing semi-colon after RCS `%s' key",
948: rk->rk_str);
949: return (-1);
950: }
951: break;
952: case RCS_TOK_ACCESS:
953: rcs_parse_access(rfp);
954: break;
955: case RCS_TOK_SYMBOLS:
956: rcs_parse_symbols(rfp);
957: break;
958: case RCS_TOK_LOCKS:
959: rcs_parse_locks(rfp);
960: break;
961: default:
962: cvs_log(LP_ERR,
963: "unexpected token `%s' in RCS admin section",
964: RCS_TOKSTR(rfp));
965: return (-1);
966: }
967: }
968:
969: return (0);
970: }
971:
972:
973: /*
974: * rcs_parse_delta()
975: *
976: * Parse an RCS delta section and allocate the structure to store that delta's
977: * information in the <rfp> delta list.
978: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
979: * -1 on error.
980: */
981: static int
982: rcs_parse_delta(RCSFILE *rfp)
983: {
984: int ret, tok, ntok, hmask;
985: u_int i;
986: char *tokstr;
1.3 vincent 987: RCSNUM *datenum;
1.1 jfb 988: struct rcs_delta *rdp;
989: struct rcs_key *rk;
990:
991: rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
992: if (rdp == NULL) {
993: cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
994: return (-1);
995: }
996: memset(rdp, 0, sizeof(*rdp));
997:
998: rdp->rd_num = rcsnum_alloc();
1.11 joris 999: if (rdp->rd_num == NULL) {
1000: rcs_freedelta(rdp);
1001: return (-1);
1002: }
1.1 jfb 1003: rdp->rd_next = rcsnum_alloc();
1.11 joris 1004: if (rdp->rd_next == NULL) {
1005: rcs_freedelta(rdp);
1006: return (-1);
1007: }
1.1 jfb 1008:
1009: TAILQ_INIT(&(rdp->rd_branches));
1010:
1011: tok = rcs_gettok(rfp);
1012: if (tok != RCS_TOK_NUM) {
1013: cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
1014: RCS_TOKSTR(rfp));
1015: rcs_freedelta(rdp);
1016: return (-1);
1017: }
1018: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1019:
1020: hmask = 0;
1021: ret = 0;
1022: tokstr = NULL;
1023:
1024: for (;;) {
1025: tok = rcs_gettok(rfp);
1026: if (tok == RCS_TOK_ERR) {
1027: cvs_log(LP_ERR, "parse error in RCS delta section");
1028: rcs_freedelta(rdp);
1029: return (-1);
1.14 deraadt 1030: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15 tedu 1031: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1 jfb 1032: ret = (tok == RCS_TOK_NUM ? 1 : 0);
1033: break;
1034: }
1035:
1036: rk = NULL;
1.18 jfb 1037: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1038: if (rcs_keys[i].rk_id == tok)
1039: rk = &(rcs_keys[i]);
1040:
1041: if (hmask & (1 << tok)) {
1042: cvs_log(LP_ERR, "duplicate RCS key");
1043: rcs_freedelta(rdp);
1044: return (-1);
1045: }
1046: hmask |= (1 << tok);
1047:
1048: switch (tok) {
1049: case RCS_TOK_DATE:
1050: case RCS_TOK_AUTHOR:
1051: case RCS_TOK_STATE:
1052: case RCS_TOK_NEXT:
1053: ntok = rcs_gettok(rfp);
1054: if (ntok == RCS_TOK_SCOLON) {
1055: if (rk->rk_flags & RCS_VOPT)
1056: break;
1057: else {
1058: cvs_log(LP_ERR, "missing mandatory "
1059: "value to RCS key `%s'",
1060: rk->rk_str);
1061: rcs_freedelta(rdp);
1062: return (-1);
1063: }
1064: }
1065:
1066: if (ntok != rk->rk_val) {
1067: cvs_log(LP_ERR,
1068: "invalid value type for RCS key `%s'",
1069: rk->rk_str);
1070: rcs_freedelta(rdp);
1071: return (-1);
1072: }
1073:
1074: if (tokstr != NULL)
1075: free(tokstr);
1076: tokstr = strdup(RCS_TOKSTR(rfp));
1.10 joris 1077: if (tokstr == NULL) {
1.15 tedu 1078: cvs_log(LP_ERRNO,
1.10 joris 1079: "failed to duplicate rcs token");
1080: rcs_freedelta(rdp);
1081: return (-1);
1082: }
1.1 jfb 1083:
1084: /* now get the expected semi-colon */
1085: ntok = rcs_gettok(rfp);
1086: if (ntok != RCS_TOK_SCOLON) {
1087: cvs_log(LP_ERR,
1088: "missing semi-colon after RCS `%s' key",
1089: rk->rk_str);
1090: rcs_freedelta(rdp);
1091: return (-1);
1092: }
1093:
1094: if (tok == RCS_TOK_DATE) {
1.3 vincent 1095: datenum = rcsnum_alloc();
1.11 joris 1096: if (datenum == NULL) {
1097: rcs_freedelta(rdp);
1098: return (-1);
1099: }
1.3 vincent 1100: rcsnum_aton(tokstr, NULL, datenum);
1101: if (datenum->rn_len != 6) {
1.1 jfb 1102: cvs_log(LP_ERR,
1103: "RCS date specification has %s "
1104: "fields",
1.3 vincent 1105: (datenum->rn_len > 6) ? "too many" :
1.1 jfb 1106: "missing");
1107: rcs_freedelta(rdp);
1108: }
1.3 vincent 1109: rdp->rd_date.tm_year = datenum->rn_id[0];
1.19 jfb 1110: if (rdp->rd_date.tm_year >= 1900)
1111: rdp->rd_date.tm_year -= 1900;
1.3 vincent 1112: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
1113: rdp->rd_date.tm_mday = datenum->rn_id[2];
1114: rdp->rd_date.tm_hour = datenum->rn_id[3];
1115: rdp->rd_date.tm_min = datenum->rn_id[4];
1116: rdp->rd_date.tm_sec = datenum->rn_id[5];
1117: rcsnum_free(datenum);
1.14 deraadt 1118: } else if (tok == RCS_TOK_AUTHOR) {
1.1 jfb 1119: rdp->rd_author = tokstr;
1120: tokstr = NULL;
1.14 deraadt 1121: } else if (tok == RCS_TOK_STATE) {
1.1 jfb 1122: rdp->rd_state = tokstr;
1123: tokstr = NULL;
1.14 deraadt 1124: } else if (tok == RCS_TOK_NEXT) {
1.1 jfb 1125: rcsnum_aton(tokstr, NULL, rdp->rd_next);
1126: }
1127: break;
1128: case RCS_TOK_BRANCHES:
1129: rcs_parse_branches(rfp, rdp);
1130: break;
1131: default:
1132: cvs_log(LP_ERR,
1133: "unexpected token `%s' in RCS delta",
1134: RCS_TOKSTR(rfp));
1135: rcs_freedelta(rdp);
1136: return (-1);
1137: }
1138: }
1139:
1.13 jfb 1140: if (tokstr != NULL)
1141: free(tokstr);
1142:
1.1 jfb 1143: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1144:
1145: return (ret);
1146: }
1147:
1148:
1149: /*
1150: * rcs_parse_deltatext()
1151: *
1152: * Parse an RCS delta text section and fill in the log and text field of the
1153: * appropriate delta section.
1154: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1155: * -1 on error.
1156: */
1157: static int
1158: rcs_parse_deltatext(RCSFILE *rfp)
1159: {
1160: int tok;
1161: RCSNUM *tnum;
1162: struct rcs_delta *rdp;
1163:
1164: tok = rcs_gettok(rfp);
1165: if (tok == RCS_TOK_EOF)
1166: return (0);
1167:
1168: if (tok != RCS_TOK_NUM) {
1169: cvs_log(LP_ERR,
1170: "unexpected token `%s' at start of RCS delta text",
1171: RCS_TOKSTR(rfp));
1172: return (-1);
1173: }
1.13 jfb 1174:
1175: tnum = rcsnum_alloc();
1176: if (tnum == NULL)
1177: return (-1);
1.1 jfb 1178: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
1179:
1180: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1181: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
1182: break;
1183: }
1.13 jfb 1184: rcsnum_free(tnum);
1185:
1.1 jfb 1186: if (rdp == NULL) {
1187: cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
1188: RCS_TOKSTR(rfp));
1189: return (-1);
1190: }
1191:
1192: tok = rcs_gettok(rfp);
1193: if (tok != RCS_TOK_LOG) {
1194: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1195: RCS_TOKSTR(rfp));
1196: return (-1);
1197: }
1198:
1199: tok = rcs_gettok(rfp);
1200: if (tok != RCS_TOK_STRING) {
1201: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1202: RCS_TOKSTR(rfp));
1203: return (-1);
1204: }
1205: rdp->rd_log = strdup(RCS_TOKSTR(rfp));
1206: if (rdp->rd_log == NULL) {
1207: cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
1208: return (-1);
1209: }
1210:
1211: tok = rcs_gettok(rfp);
1212: if (tok != RCS_TOK_TEXT) {
1213: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
1214: RCS_TOKSTR(rfp));
1215: return (-1);
1216: }
1217:
1218: tok = rcs_gettok(rfp);
1219: if (tok != RCS_TOK_STRING) {
1220: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
1221: RCS_TOKSTR(rfp));
1222: return (-1);
1223: }
1224:
1225: rdp->rd_text = strdup(RCS_TOKSTR(rfp));
1226: if (rdp->rd_text == NULL) {
1227: cvs_log(LP_ERRNO, "failed to copy RCS delta text");
1228: return (-1);
1229: }
1230:
1231: return (1);
1232: }
1233:
1234:
1235: /*
1236: * rcs_parse_access()
1237: *
1238: * Parse the access list given as value to the `access' keyword.
1239: * Returns 0 on success, or -1 on failure.
1240: */
1241: static int
1242: rcs_parse_access(RCSFILE *rfp)
1243: {
1244: int type;
1245:
1246: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
1247: if (type != RCS_TOK_ID) {
1248: cvs_log(LP_ERR, "unexpected token `%s' in access list",
1249: RCS_TOKSTR(rfp));
1250: return (-1);
1251: }
1252: }
1253:
1254: return (0);
1255: }
1256:
1257:
1258: /*
1259: * rcs_parse_symbols()
1260: *
1261: * Parse the symbol list given as value to the `symbols' keyword.
1262: * Returns 0 on success, or -1 on failure.
1263: */
1264: static int
1265: rcs_parse_symbols(RCSFILE *rfp)
1266: {
1267: int type;
1268: struct rcs_sym *symp;
1269:
1270: for (;;) {
1271: type = rcs_gettok(rfp);
1272: if (type == RCS_TOK_SCOLON)
1273: break;
1274:
1275: if (type != RCS_TOK_STRING) {
1276: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1277: RCS_TOKSTR(rfp));
1278: return (-1);
1279: }
1280:
1281: symp = (struct rcs_sym *)malloc(sizeof(*symp));
1282: if (symp == NULL) {
1283: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
1284: return (-1);
1285: }
1286: symp->rs_name = strdup(RCS_TOKSTR(rfp));
1.10 joris 1287: if (symp->rs_name == NULL) {
1288: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
1289: free(symp);
1290: return (-1);
1291: }
1292:
1.1 jfb 1293: symp->rs_num = rcsnum_alloc();
1.11 joris 1294: if (symp->rs_num == NULL) {
1295: cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
1296: free(symp);
1297: return (-1);
1298: }
1.1 jfb 1299:
1300: type = rcs_gettok(rfp);
1301: if (type != RCS_TOK_COLON) {
1302: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1303: RCS_TOKSTR(rfp));
1.11 joris 1304: rcsnum_free(symp->rs_num);
1.1 jfb 1305: free(symp->rs_name);
1306: free(symp);
1307: return (-1);
1308: }
1309:
1310: type = rcs_gettok(rfp);
1311: if (type != RCS_TOK_NUM) {
1312: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1313: RCS_TOKSTR(rfp));
1.11 joris 1314: rcsnum_free(symp->rs_num);
1.1 jfb 1315: free(symp->rs_name);
1316: free(symp);
1317: return (-1);
1318: }
1319:
1320: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
1321: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
1322: RCS_TOKSTR(rfp));
1.11 joris 1323: rcsnum_free(symp->rs_num);
1.1 jfb 1324: free(symp->rs_name);
1325: free(symp);
1326: return (-1);
1327: }
1328:
1329: TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
1330: }
1331:
1332: return (0);
1333: }
1334:
1335:
1336: /*
1337: * rcs_parse_locks()
1338: *
1339: * Parse the lock list given as value to the `locks' keyword.
1340: * Returns 0 on success, or -1 on failure.
1341: */
1342: static int
1343: rcs_parse_locks(RCSFILE *rfp)
1344: {
1345: int type;
1346: struct rcs_lock *lkp;
1347:
1348: for (;;) {
1349: type = rcs_gettok(rfp);
1350: if (type == RCS_TOK_SCOLON)
1351: break;
1352:
1353: if (type != RCS_TOK_ID) {
1354: cvs_log(LP_ERR, "unexpected token `%s' in lock list",
1355: RCS_TOKSTR(rfp));
1356: return (-1);
1357: }
1358:
1359: lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
1360: if (lkp == NULL) {
1361: cvs_log(LP_ERRNO, "failed to allocate RCS lock");
1362: return (-1);
1363: }
1364: lkp->rl_num = rcsnum_alloc();
1.11 joris 1365: if (lkp->rl_num == NULL) {
1366: free(lkp);
1367: return (-1);
1368: }
1.1 jfb 1369:
1370: type = rcs_gettok(rfp);
1371: if (type != RCS_TOK_COLON) {
1372: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1373: RCS_TOKSTR(rfp));
1374: free(lkp);
1375: return (-1);
1376: }
1377:
1378: type = rcs_gettok(rfp);
1379: if (type != RCS_TOK_NUM) {
1380: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1381: RCS_TOKSTR(rfp));
1382: free(lkp);
1383: return (-1);
1384: }
1385:
1386: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
1387: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
1388: RCS_TOKSTR(rfp));
1389: free(lkp);
1390: return (-1);
1391: }
1392:
1393: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
1394: }
1395:
1396: /* check if we have a `strict' */
1397: type = rcs_gettok(rfp);
1398: if (type != RCS_TOK_STRICT) {
1399: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14 deraadt 1400: } else {
1.1 jfb 1401: rfp->rf_flags |= RCS_RF_SLOCK;
1402:
1403: type = rcs_gettok(rfp);
1404: if (type != RCS_TOK_SCOLON) {
1405: cvs_log(LP_ERR,
1406: "missing semi-colon after `strict' keyword");
1407: return (-1);
1408: }
1409: }
1410:
1411: return (0);
1412: }
1413:
1414: /*
1415: * rcs_parse_branches()
1416: *
1417: * Parse the list of branches following a `branches' keyword in a delta.
1418: * Returns 0 on success, or -1 on failure.
1419: */
1420: static int
1421: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
1422: {
1423: int type;
1424: struct rcs_branch *brp;
1425:
1426: for (;;) {
1427: type = rcs_gettok(rfp);
1428: if (type == RCS_TOK_SCOLON)
1429: break;
1430:
1431: if (type != RCS_TOK_NUM) {
1432: cvs_log(LP_ERR,
1433: "unexpected token `%s' in list of branches",
1434: RCS_TOKSTR(rfp));
1435: return (-1);
1436: }
1437:
1438: brp = (struct rcs_branch *)malloc(sizeof(*brp));
1439: if (brp == NULL) {
1440: cvs_log(LP_ERRNO, "failed to allocate RCS branch");
1441: return (-1);
1442: }
1443: brp->rb_num = rcsnum_alloc();
1.11 joris 1444: if (brp->rb_num == NULL) {
1445: free(brp);
1446: return (-1);
1447: }
1448:
1.1 jfb 1449: rcsnum_aton(RCS_TOKSTR(rfp), NULL, brp->rb_num);
1450:
1451: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
1452: }
1453:
1454: return (0);
1455: }
1456:
1457:
1458: /*
1459: * rcs_freedelta()
1460: *
1461: * Free the contents of a delta structure.
1462: */
1.18 jfb 1463: static void
1.1 jfb 1464: rcs_freedelta(struct rcs_delta *rdp)
1465: {
1.12 jfb 1466: struct rcs_branch *rb;
1.1 jfb 1467: struct rcs_delta *crdp;
1468:
1.12 jfb 1469: if (rdp->rd_num != NULL)
1470: rcsnum_free(rdp->rd_num);
1471: if (rdp->rd_next != NULL)
1472: rcsnum_free(rdp->rd_next);
1473:
1.1 jfb 1474: if (rdp->rd_author != NULL)
1475: free(rdp->rd_author);
1476: if (rdp->rd_state != NULL)
1477: free(rdp->rd_state);
1478: if (rdp->rd_log != NULL)
1479: free(rdp->rd_log);
1480: if (rdp->rd_text != NULL)
1481: free(rdp->rd_text);
1.12 jfb 1482:
1483: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
1484: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
1485: rcsnum_free(rb->rb_num);
1486: free(rb);
1487: }
1.1 jfb 1488:
1489: while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
1490: TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
1491: rcs_freedelta(crdp);
1492: }
1493:
1494: free(rdp);
1495: }
1496:
1497:
1498: /*
1499: * rcs_freepdata()
1500: *
1501: * Free the contents of the parser data structure.
1502: */
1503: static void
1504: rcs_freepdata(struct rcs_pdata *pd)
1505: {
1506: if (pd->rp_file != NULL)
1507: (void)fclose(pd->rp_file);
1508: if (pd->rp_buf != NULL)
1509: free(pd->rp_buf);
1510: free(pd);
1511: }
1512:
1513:
1514: /*
1515: * rcs_gettok()
1516: *
1517: * Get the next RCS token from the string <str>.
1518: */
1519: static int
1520: rcs_gettok(RCSFILE *rfp)
1521: {
1522: u_int i;
1523: int ch, last, type;
1.18 jfb 1524: size_t len;
1525: char *bp;
1.1 jfb 1526: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
1527:
1528: type = RCS_TOK_ERR;
1529: bp = pdp->rp_buf;
1530: *bp = '\0';
1531:
1532: if (pdp->rp_pttype != RCS_TOK_ERR) {
1533: type = pdp->rp_pttype;
1534: strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
1535: pdp->rp_pttype = RCS_TOK_ERR;
1536: return (type);
1537: }
1538:
1539: /* skip leading whitespace */
1540: /* XXX we must skip backspace too for compatibility, should we? */
1541: do {
1542: ch = getc(pdp->rp_file);
1543: if (ch == '\n')
1.18 jfb 1544: pdp->rp_lines++;
1.1 jfb 1545: } while (isspace(ch));
1546:
1547: if (ch == EOF) {
1548: type = RCS_TOK_EOF;
1.14 deraadt 1549: } else if (ch == ';') {
1.1 jfb 1550: type = RCS_TOK_SCOLON;
1.14 deraadt 1551: } else if (ch == ':') {
1.1 jfb 1552: type = RCS_TOK_COLON;
1.14 deraadt 1553: } else if (isalpha(ch)) {
1.18 jfb 1554: type = RCS_TOK_STRING;
1.1 jfb 1555: *(bp++) = ch;
1.18 jfb 1556: for (;;) {
1.1 jfb 1557: ch = getc(pdp->rp_file);
1.11 joris 1558: if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1 jfb 1559: ungetc(ch, pdp->rp_file);
1560: break;
1561: }
1562: *(bp++) = ch;
1.18 jfb 1563: if (bp == pdp->rp_bufend - 1) {
1564: len = bp - pdp->rp_buf;
1565: if (rcs_growbuf(rfp) < 0) {
1566: type = RCS_TOK_ERR;
1567: break;
1568: }
1569: bp = pdp->rp_buf + len;
1570: }
1.1 jfb 1571: }
1572: *bp = '\0';
1573:
1.18 jfb 1574: if (type != RCS_TOK_ERR) {
1575: for (i = 0; i < RCS_NKEYS; i++) {
1576: if (strcmp(rcs_keys[i].rk_str,
1577: pdp->rp_buf) == 0) {
1578: type = rcs_keys[i].rk_id;
1579: break;
1580: }
1.1 jfb 1581: }
1582: }
1.14 deraadt 1583: } else if (ch == '@') {
1.1 jfb 1584: /* we have a string */
1.18 jfb 1585: type = RCS_TOK_STRING;
1.1 jfb 1586: for (;;) {
1587: ch = getc(pdp->rp_file);
1588: if (ch == '@') {
1589: ch = getc(pdp->rp_file);
1590: if (ch != '@') {
1591: ungetc(ch, pdp->rp_file);
1592: break;
1593: }
1.14 deraadt 1594: } else if (ch == '\n')
1.18 jfb 1595: pdp->rp_lines++;
1.1 jfb 1596:
1597: *(bp++) = ch;
1.18 jfb 1598: if (bp == pdp->rp_bufend - 1) {
1599: len = bp - pdp->rp_buf;
1600: if (rcs_growbuf(rfp) < 0) {
1601: type = RCS_TOK_ERR;
1602: break;
1603: }
1604: bp = pdp->rp_buf + len;
1605: }
1.1 jfb 1606: }
1607:
1608: *bp = '\0';
1.14 deraadt 1609: } else if (isdigit(ch)) {
1.1 jfb 1610: *(bp++) = ch;
1611: last = ch;
1612: type = RCS_TOK_NUM;
1613:
1614: for (;;) {
1615: ch = getc(pdp->rp_file);
1.18 jfb 1616: if (bp == pdp->rp_bufend)
1.1 jfb 1617: break;
1618: if (!isdigit(ch) && ch != '.') {
1619: ungetc(ch, pdp->rp_file);
1620: break;
1621: }
1622:
1623: if (last == '.' && ch == '.') {
1624: type = RCS_TOK_ERR;
1625: break;
1626: }
1627: last = ch;
1628: *(bp++) = ch;
1629: }
1.18 jfb 1630: *bp = '\0';
1.1 jfb 1631: }
1632:
1633: return (type);
1634: }
1635:
1636:
1637: /*
1638: * rcs_pushtok()
1639: *
1640: * Push a token back in the parser's token buffer.
1641: */
1642: static int
1643: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
1644: {
1645: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
1646:
1647: if (pdp->rp_pttype != RCS_TOK_ERR)
1648: return (-1);
1649:
1650: pdp->rp_pttype = type;
1651: strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
1652: return (0);
1653: }
1654:
1655:
1656: /*
1657: * rcs_stresc()
1658: *
1659: * Performs either escaping or unescaping of the string stored in <str>.
1660: * The operation is to escape special RCS characters if the <esc> argument
1661: * is 1, or unescape otherwise. The result is stored in the <buf> destination
1662: * buffer, and <blen> must originally point to the size of <buf>.
1663: * Returns the number of bytes which have been read from the source <str> and
1664: * operated on. The <blen> parameter will contain the number of bytes
1665: * actually copied in <buf>.
1666: */
1667: size_t
1668: rcs_stresc(int esc, const char *str, char *buf, size_t *blen)
1669: {
1670: size_t rlen;
1671: const char *sp;
1672: char *bp, *bep;
1673:
1674: rlen = 0;
1675: bp = buf;
1676: bep = buf + *blen - 1;
1677:
1678: for (sp = str; (*sp != '\0') && (bp <= (bep - 1)); sp++) {
1679: if (*sp == '@') {
1680: if (esc) {
1681: if (bp > (bep - 2))
1682: break;
1683: *(bp++) = '@';
1.14 deraadt 1684: } else {
1.1 jfb 1685: sp++;
1686: if (*sp != '@') {
1687: cvs_log(LP_WARN,
1688: "unknown escape character `%c' in "
1689: "RCS file", *sp);
1690: if (*sp == '\0')
1691: break;
1692: }
1693: }
1694: }
1695:
1696: *(bp++) = *sp;
1697: }
1698:
1699: *bp = '\0';
1700: *blen = (bp - buf);
1701: return (sp - str);
1702: }
1703:
1704:
1705: /*
1706: * rcs_splitlines()
1707: *
1708: * Split the contents of a file into a list of lines.
1709: */
1710: static struct rcs_foo*
1711: rcs_splitlines(const char *fcont)
1712: {
1713: char *dcp;
1714: struct rcs_foo *foo;
1715: struct rcs_line *lp;
1716:
1717: foo = (struct rcs_foo *)malloc(sizeof(*foo));
1718: if (foo == NULL) {
1719: cvs_log(LP_ERR, "failed to allocate line structure");
1720: return (NULL);
1721: }
1722: TAILQ_INIT(&(foo->rl_lines));
1723: foo->rl_nblines = 0;
1724: foo->rl_data = strdup(fcont);
1725: if (foo->rl_data == NULL) {
1726: cvs_log(LP_ERRNO, "failed to copy file contents");
1727: free(foo);
1728: return (NULL);
1729: }
1730:
1731: /*
1732: * Add a first bogus line with line number 0. This is used so we
1733: * can position the line pointer before 1 when changing the first line
1734: * in rcs_patch().
1735: */
1736: lp = (struct rcs_line *)malloc(sizeof(*lp));
1.5 vincent 1737: if (lp == NULL)
1.1 jfb 1738: return (NULL);
1.5 vincent 1739:
1.1 jfb 1740: lp->rl_line = NULL;
1741: lp->rl_lineno = 0;
1742: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
1743:
1744:
1745: for (dcp = foo->rl_data; *dcp != '\0';) {
1746: lp = (struct rcs_line *)malloc(sizeof(*lp));
1747: if (lp == NULL) {
1748: cvs_log(LP_ERR, "failed to allocate line entry");
1749: return (NULL);
1750: }
1751:
1752: lp->rl_line = dcp;
1753: lp->rl_lineno = ++(foo->rl_nblines);
1754: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
1755:
1756: dcp = strchr(dcp, '\n');
1757: if (dcp == NULL) {
1758: break;
1759: }
1760: *(dcp++) = '\0';
1761: }
1762:
1763: return (foo);
1.5 vincent 1764: }
1765:
1766: static void
1767: rcs_freefoo(struct rcs_foo *fp)
1768: {
1769: struct rcs_line *lp;
1770:
1771: while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
1772: TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
1773: free(lp);
1774: }
1775: free(fp->rl_data);
1776: free(fp);
1.18 jfb 1777: }
1778:
1779: /*
1780: * rcs_growbuf()
1781: *
1782: * Attempt to grow the internal parse buffer for the RCS file <rf> by
1783: * RCS_BUFEXTSIZE.
1784: * In case of failure, the original buffer is left unmodified.
1785: * Returns 0 on success, or -1 on failure.
1786: */
1787: static int
1788: rcs_growbuf(RCSFILE *rf)
1789: {
1790: void *tmp;
1791: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
1792:
1793: tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
1794: if (tmp == NULL) {
1795: cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
1796: return (-1);
1797: }
1798:
1799: pdp->rp_buf = (char *)tmp;
1800: pdp->rp_blen += RCS_BUFEXTSIZE;
1801: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1802:
1803: return (0);
1.1 jfb 1804: }