Annotation of src/usr.bin/cvs/rcs.c, Revision 1.25
1.25 ! jfb 1: /* $OpenBSD: rcs.c,v 1.24 2005/02/16 19:45:59 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.25 ! jfb 1095: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.11 joris 1096: rcs_freedelta(rdp);
1097: return (-1);
1098: }
1.3 vincent 1099: if (datenum->rn_len != 6) {
1.1 jfb 1100: cvs_log(LP_ERR,
1101: "RCS date specification has %s "
1102: "fields",
1.3 vincent 1103: (datenum->rn_len > 6) ? "too many" :
1.1 jfb 1104: "missing");
1105: rcs_freedelta(rdp);
1106: }
1.3 vincent 1107: rdp->rd_date.tm_year = datenum->rn_id[0];
1.19 jfb 1108: if (rdp->rd_date.tm_year >= 1900)
1109: rdp->rd_date.tm_year -= 1900;
1.3 vincent 1110: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
1111: rdp->rd_date.tm_mday = datenum->rn_id[2];
1112: rdp->rd_date.tm_hour = datenum->rn_id[3];
1113: rdp->rd_date.tm_min = datenum->rn_id[4];
1114: rdp->rd_date.tm_sec = datenum->rn_id[5];
1115: rcsnum_free(datenum);
1.14 deraadt 1116: } else if (tok == RCS_TOK_AUTHOR) {
1.1 jfb 1117: rdp->rd_author = tokstr;
1118: tokstr = NULL;
1.14 deraadt 1119: } else if (tok == RCS_TOK_STATE) {
1.1 jfb 1120: rdp->rd_state = tokstr;
1121: tokstr = NULL;
1.14 deraadt 1122: } else if (tok == RCS_TOK_NEXT) {
1.1 jfb 1123: rcsnum_aton(tokstr, NULL, rdp->rd_next);
1124: }
1125: break;
1126: case RCS_TOK_BRANCHES:
1127: rcs_parse_branches(rfp, rdp);
1128: break;
1129: default:
1130: cvs_log(LP_ERR,
1131: "unexpected token `%s' in RCS delta",
1132: RCS_TOKSTR(rfp));
1133: rcs_freedelta(rdp);
1134: return (-1);
1135: }
1136: }
1137:
1.13 jfb 1138: if (tokstr != NULL)
1139: free(tokstr);
1140:
1.1 jfb 1141: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1142:
1143: return (ret);
1144: }
1145:
1146:
1147: /*
1148: * rcs_parse_deltatext()
1149: *
1150: * Parse an RCS delta text section and fill in the log and text field of the
1151: * appropriate delta section.
1152: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1153: * -1 on error.
1154: */
1155: static int
1156: rcs_parse_deltatext(RCSFILE *rfp)
1157: {
1158: int tok;
1159: RCSNUM *tnum;
1160: struct rcs_delta *rdp;
1161:
1162: tok = rcs_gettok(rfp);
1163: if (tok == RCS_TOK_EOF)
1164: return (0);
1165:
1166: if (tok != RCS_TOK_NUM) {
1167: cvs_log(LP_ERR,
1168: "unexpected token `%s' at start of RCS delta text",
1169: RCS_TOKSTR(rfp));
1170: return (-1);
1171: }
1.13 jfb 1172:
1173: tnum = rcsnum_alloc();
1174: if (tnum == NULL)
1175: return (-1);
1.1 jfb 1176: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
1177:
1178: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1179: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
1180: break;
1181: }
1.13 jfb 1182: rcsnum_free(tnum);
1183:
1.1 jfb 1184: if (rdp == NULL) {
1185: cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
1186: RCS_TOKSTR(rfp));
1187: return (-1);
1188: }
1189:
1190: tok = rcs_gettok(rfp);
1191: if (tok != RCS_TOK_LOG) {
1192: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1193: RCS_TOKSTR(rfp));
1194: return (-1);
1195: }
1196:
1197: tok = rcs_gettok(rfp);
1198: if (tok != RCS_TOK_STRING) {
1199: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1200: RCS_TOKSTR(rfp));
1201: return (-1);
1202: }
1203: rdp->rd_log = strdup(RCS_TOKSTR(rfp));
1204: if (rdp->rd_log == NULL) {
1205: cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
1206: return (-1);
1207: }
1208:
1209: tok = rcs_gettok(rfp);
1210: if (tok != RCS_TOK_TEXT) {
1211: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
1212: RCS_TOKSTR(rfp));
1213: return (-1);
1214: }
1215:
1216: tok = rcs_gettok(rfp);
1217: if (tok != RCS_TOK_STRING) {
1218: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
1219: RCS_TOKSTR(rfp));
1220: return (-1);
1221: }
1222:
1223: rdp->rd_text = strdup(RCS_TOKSTR(rfp));
1224: if (rdp->rd_text == NULL) {
1225: cvs_log(LP_ERRNO, "failed to copy RCS delta text");
1226: return (-1);
1227: }
1228:
1229: return (1);
1230: }
1231:
1232:
1233: /*
1234: * rcs_parse_access()
1235: *
1236: * Parse the access list given as value to the `access' keyword.
1237: * Returns 0 on success, or -1 on failure.
1238: */
1239: static int
1240: rcs_parse_access(RCSFILE *rfp)
1241: {
1242: int type;
1243:
1244: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
1245: if (type != RCS_TOK_ID) {
1246: cvs_log(LP_ERR, "unexpected token `%s' in access list",
1247: RCS_TOKSTR(rfp));
1248: return (-1);
1249: }
1250: }
1251:
1252: return (0);
1253: }
1254:
1255:
1256: /*
1257: * rcs_parse_symbols()
1258: *
1259: * Parse the symbol list given as value to the `symbols' keyword.
1260: * Returns 0 on success, or -1 on failure.
1261: */
1262: static int
1263: rcs_parse_symbols(RCSFILE *rfp)
1264: {
1265: int type;
1266: struct rcs_sym *symp;
1267:
1268: for (;;) {
1269: type = rcs_gettok(rfp);
1270: if (type == RCS_TOK_SCOLON)
1271: break;
1272:
1273: if (type != RCS_TOK_STRING) {
1274: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1275: RCS_TOKSTR(rfp));
1276: return (-1);
1277: }
1278:
1279: symp = (struct rcs_sym *)malloc(sizeof(*symp));
1280: if (symp == NULL) {
1281: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
1282: return (-1);
1283: }
1284: symp->rs_name = strdup(RCS_TOKSTR(rfp));
1.10 joris 1285: if (symp->rs_name == NULL) {
1286: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
1287: free(symp);
1288: return (-1);
1289: }
1290:
1.1 jfb 1291: symp->rs_num = rcsnum_alloc();
1.11 joris 1292: if (symp->rs_num == NULL) {
1293: cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
1294: free(symp);
1295: return (-1);
1296: }
1.1 jfb 1297:
1298: type = rcs_gettok(rfp);
1299: if (type != RCS_TOK_COLON) {
1300: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1301: RCS_TOKSTR(rfp));
1.11 joris 1302: rcsnum_free(symp->rs_num);
1.1 jfb 1303: free(symp->rs_name);
1304: free(symp);
1305: return (-1);
1306: }
1307:
1308: type = rcs_gettok(rfp);
1309: if (type != RCS_TOK_NUM) {
1310: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1311: RCS_TOKSTR(rfp));
1.11 joris 1312: rcsnum_free(symp->rs_num);
1.1 jfb 1313: free(symp->rs_name);
1314: free(symp);
1315: return (-1);
1316: }
1317:
1318: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
1319: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
1320: RCS_TOKSTR(rfp));
1.11 joris 1321: rcsnum_free(symp->rs_num);
1.1 jfb 1322: free(symp->rs_name);
1323: free(symp);
1324: return (-1);
1325: }
1326:
1327: TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
1328: }
1329:
1330: return (0);
1331: }
1332:
1333:
1334: /*
1335: * rcs_parse_locks()
1336: *
1337: * Parse the lock list given as value to the `locks' keyword.
1338: * Returns 0 on success, or -1 on failure.
1339: */
1340: static int
1341: rcs_parse_locks(RCSFILE *rfp)
1342: {
1343: int type;
1344: struct rcs_lock *lkp;
1345:
1346: for (;;) {
1347: type = rcs_gettok(rfp);
1348: if (type == RCS_TOK_SCOLON)
1349: break;
1350:
1351: if (type != RCS_TOK_ID) {
1352: cvs_log(LP_ERR, "unexpected token `%s' in lock list",
1353: RCS_TOKSTR(rfp));
1354: return (-1);
1355: }
1356:
1357: lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
1358: if (lkp == NULL) {
1359: cvs_log(LP_ERRNO, "failed to allocate RCS lock");
1360: return (-1);
1361: }
1362: lkp->rl_num = rcsnum_alloc();
1.11 joris 1363: if (lkp->rl_num == NULL) {
1364: free(lkp);
1365: return (-1);
1366: }
1.1 jfb 1367:
1368: type = rcs_gettok(rfp);
1369: if (type != RCS_TOK_COLON) {
1370: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1371: RCS_TOKSTR(rfp));
1372: free(lkp);
1373: return (-1);
1374: }
1375:
1376: type = rcs_gettok(rfp);
1377: if (type != RCS_TOK_NUM) {
1378: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1379: RCS_TOKSTR(rfp));
1380: free(lkp);
1381: return (-1);
1382: }
1383:
1384: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
1385: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
1386: RCS_TOKSTR(rfp));
1387: free(lkp);
1388: return (-1);
1389: }
1390:
1391: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
1392: }
1393:
1394: /* check if we have a `strict' */
1395: type = rcs_gettok(rfp);
1396: if (type != RCS_TOK_STRICT) {
1397: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14 deraadt 1398: } else {
1.1 jfb 1399: rfp->rf_flags |= RCS_RF_SLOCK;
1400:
1401: type = rcs_gettok(rfp);
1402: if (type != RCS_TOK_SCOLON) {
1403: cvs_log(LP_ERR,
1404: "missing semi-colon after `strict' keyword");
1405: return (-1);
1406: }
1407: }
1408:
1409: return (0);
1410: }
1411:
1412: /*
1413: * rcs_parse_branches()
1414: *
1415: * Parse the list of branches following a `branches' keyword in a delta.
1416: * Returns 0 on success, or -1 on failure.
1417: */
1418: static int
1419: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
1420: {
1421: int type;
1422: struct rcs_branch *brp;
1423:
1424: for (;;) {
1425: type = rcs_gettok(rfp);
1426: if (type == RCS_TOK_SCOLON)
1427: break;
1428:
1429: if (type != RCS_TOK_NUM) {
1430: cvs_log(LP_ERR,
1431: "unexpected token `%s' in list of branches",
1432: RCS_TOKSTR(rfp));
1433: return (-1);
1434: }
1435:
1436: brp = (struct rcs_branch *)malloc(sizeof(*brp));
1437: if (brp == NULL) {
1438: cvs_log(LP_ERRNO, "failed to allocate RCS branch");
1439: return (-1);
1440: }
1441: brp->rb_num = rcsnum_alloc();
1.11 joris 1442: if (brp->rb_num == NULL) {
1443: free(brp);
1444: return (-1);
1445: }
1446:
1.1 jfb 1447: rcsnum_aton(RCS_TOKSTR(rfp), NULL, brp->rb_num);
1448:
1449: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
1450: }
1451:
1452: return (0);
1453: }
1454:
1455:
1456: /*
1457: * rcs_freedelta()
1458: *
1459: * Free the contents of a delta structure.
1460: */
1.18 jfb 1461: static void
1.1 jfb 1462: rcs_freedelta(struct rcs_delta *rdp)
1463: {
1.12 jfb 1464: struct rcs_branch *rb;
1.1 jfb 1465: struct rcs_delta *crdp;
1466:
1.12 jfb 1467: if (rdp->rd_num != NULL)
1468: rcsnum_free(rdp->rd_num);
1469: if (rdp->rd_next != NULL)
1470: rcsnum_free(rdp->rd_next);
1471:
1.1 jfb 1472: if (rdp->rd_author != NULL)
1473: free(rdp->rd_author);
1474: if (rdp->rd_state != NULL)
1475: free(rdp->rd_state);
1476: if (rdp->rd_log != NULL)
1477: free(rdp->rd_log);
1478: if (rdp->rd_text != NULL)
1479: free(rdp->rd_text);
1.12 jfb 1480:
1481: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
1482: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
1483: rcsnum_free(rb->rb_num);
1484: free(rb);
1485: }
1.1 jfb 1486:
1487: while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
1488: TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
1489: rcs_freedelta(crdp);
1490: }
1491:
1492: free(rdp);
1493: }
1494:
1495:
1496: /*
1497: * rcs_freepdata()
1498: *
1499: * Free the contents of the parser data structure.
1500: */
1501: static void
1502: rcs_freepdata(struct rcs_pdata *pd)
1503: {
1504: if (pd->rp_file != NULL)
1505: (void)fclose(pd->rp_file);
1506: if (pd->rp_buf != NULL)
1507: free(pd->rp_buf);
1508: free(pd);
1509: }
1510:
1511:
1512: /*
1513: * rcs_gettok()
1514: *
1515: * Get the next RCS token from the string <str>.
1516: */
1517: static int
1518: rcs_gettok(RCSFILE *rfp)
1519: {
1520: u_int i;
1521: int ch, last, type;
1.18 jfb 1522: size_t len;
1523: char *bp;
1.1 jfb 1524: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
1525:
1526: type = RCS_TOK_ERR;
1527: bp = pdp->rp_buf;
1528: *bp = '\0';
1529:
1530: if (pdp->rp_pttype != RCS_TOK_ERR) {
1531: type = pdp->rp_pttype;
1532: strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
1533: pdp->rp_pttype = RCS_TOK_ERR;
1534: return (type);
1535: }
1536:
1537: /* skip leading whitespace */
1538: /* XXX we must skip backspace too for compatibility, should we? */
1539: do {
1540: ch = getc(pdp->rp_file);
1541: if (ch == '\n')
1.18 jfb 1542: pdp->rp_lines++;
1.1 jfb 1543: } while (isspace(ch));
1544:
1545: if (ch == EOF) {
1546: type = RCS_TOK_EOF;
1.14 deraadt 1547: } else if (ch == ';') {
1.1 jfb 1548: type = RCS_TOK_SCOLON;
1.14 deraadt 1549: } else if (ch == ':') {
1.1 jfb 1550: type = RCS_TOK_COLON;
1.14 deraadt 1551: } else if (isalpha(ch)) {
1.18 jfb 1552: type = RCS_TOK_STRING;
1.1 jfb 1553: *(bp++) = ch;
1.18 jfb 1554: for (;;) {
1.1 jfb 1555: ch = getc(pdp->rp_file);
1.11 joris 1556: if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1 jfb 1557: ungetc(ch, pdp->rp_file);
1558: break;
1559: }
1560: *(bp++) = ch;
1.18 jfb 1561: if (bp == pdp->rp_bufend - 1) {
1562: len = bp - pdp->rp_buf;
1563: if (rcs_growbuf(rfp) < 0) {
1564: type = RCS_TOK_ERR;
1565: break;
1566: }
1567: bp = pdp->rp_buf + len;
1568: }
1.1 jfb 1569: }
1570: *bp = '\0';
1571:
1.18 jfb 1572: if (type != RCS_TOK_ERR) {
1573: for (i = 0; i < RCS_NKEYS; i++) {
1574: if (strcmp(rcs_keys[i].rk_str,
1575: pdp->rp_buf) == 0) {
1576: type = rcs_keys[i].rk_id;
1577: break;
1578: }
1.1 jfb 1579: }
1580: }
1.14 deraadt 1581: } else if (ch == '@') {
1.1 jfb 1582: /* we have a string */
1.18 jfb 1583: type = RCS_TOK_STRING;
1.1 jfb 1584: for (;;) {
1585: ch = getc(pdp->rp_file);
1586: if (ch == '@') {
1587: ch = getc(pdp->rp_file);
1588: if (ch != '@') {
1589: ungetc(ch, pdp->rp_file);
1590: break;
1591: }
1.14 deraadt 1592: } else if (ch == '\n')
1.18 jfb 1593: pdp->rp_lines++;
1.1 jfb 1594:
1595: *(bp++) = ch;
1.18 jfb 1596: if (bp == pdp->rp_bufend - 1) {
1597: len = bp - pdp->rp_buf;
1598: if (rcs_growbuf(rfp) < 0) {
1599: type = RCS_TOK_ERR;
1600: break;
1601: }
1602: bp = pdp->rp_buf + len;
1603: }
1.1 jfb 1604: }
1605:
1606: *bp = '\0';
1.14 deraadt 1607: } else if (isdigit(ch)) {
1.1 jfb 1608: *(bp++) = ch;
1609: last = ch;
1610: type = RCS_TOK_NUM;
1611:
1612: for (;;) {
1613: ch = getc(pdp->rp_file);
1.18 jfb 1614: if (bp == pdp->rp_bufend)
1.1 jfb 1615: break;
1616: if (!isdigit(ch) && ch != '.') {
1617: ungetc(ch, pdp->rp_file);
1618: break;
1619: }
1620:
1621: if (last == '.' && ch == '.') {
1622: type = RCS_TOK_ERR;
1623: break;
1624: }
1625: last = ch;
1626: *(bp++) = ch;
1627: }
1.18 jfb 1628: *bp = '\0';
1.1 jfb 1629: }
1630:
1631: return (type);
1632: }
1633:
1634:
1635: /*
1636: * rcs_pushtok()
1637: *
1638: * Push a token back in the parser's token buffer.
1639: */
1640: static int
1641: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
1642: {
1643: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
1644:
1645: if (pdp->rp_pttype != RCS_TOK_ERR)
1646: return (-1);
1647:
1648: pdp->rp_pttype = type;
1649: strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
1650: return (0);
1651: }
1652:
1653:
1654: /*
1655: * rcs_stresc()
1656: *
1657: * Performs either escaping or unescaping of the string stored in <str>.
1658: * The operation is to escape special RCS characters if the <esc> argument
1659: * is 1, or unescape otherwise. The result is stored in the <buf> destination
1660: * buffer, and <blen> must originally point to the size of <buf>.
1661: * Returns the number of bytes which have been read from the source <str> and
1662: * operated on. The <blen> parameter will contain the number of bytes
1663: * actually copied in <buf>.
1664: */
1665: size_t
1666: rcs_stresc(int esc, const char *str, char *buf, size_t *blen)
1667: {
1668: size_t rlen;
1669: const char *sp;
1670: char *bp, *bep;
1671:
1672: rlen = 0;
1673: bp = buf;
1674: bep = buf + *blen - 1;
1675:
1676: for (sp = str; (*sp != '\0') && (bp <= (bep - 1)); sp++) {
1677: if (*sp == '@') {
1678: if (esc) {
1679: if (bp > (bep - 2))
1680: break;
1681: *(bp++) = '@';
1.14 deraadt 1682: } else {
1.1 jfb 1683: sp++;
1684: if (*sp != '@') {
1685: cvs_log(LP_WARN,
1686: "unknown escape character `%c' in "
1687: "RCS file", *sp);
1688: if (*sp == '\0')
1689: break;
1690: }
1691: }
1692: }
1693:
1694: *(bp++) = *sp;
1695: }
1696:
1697: *bp = '\0';
1698: *blen = (bp - buf);
1699: return (sp - str);
1700: }
1701:
1702:
1703: /*
1704: * rcs_splitlines()
1705: *
1706: * Split the contents of a file into a list of lines.
1707: */
1708: static struct rcs_foo*
1709: rcs_splitlines(const char *fcont)
1710: {
1711: char *dcp;
1712: struct rcs_foo *foo;
1713: struct rcs_line *lp;
1714:
1715: foo = (struct rcs_foo *)malloc(sizeof(*foo));
1716: if (foo == NULL) {
1717: cvs_log(LP_ERR, "failed to allocate line structure");
1718: return (NULL);
1719: }
1720: TAILQ_INIT(&(foo->rl_lines));
1721: foo->rl_nblines = 0;
1722: foo->rl_data = strdup(fcont);
1723: if (foo->rl_data == NULL) {
1724: cvs_log(LP_ERRNO, "failed to copy file contents");
1725: free(foo);
1726: return (NULL);
1727: }
1728:
1729: /*
1730: * Add a first bogus line with line number 0. This is used so we
1731: * can position the line pointer before 1 when changing the first line
1732: * in rcs_patch().
1733: */
1734: lp = (struct rcs_line *)malloc(sizeof(*lp));
1.5 vincent 1735: if (lp == NULL)
1.1 jfb 1736: return (NULL);
1.5 vincent 1737:
1.1 jfb 1738: lp->rl_line = NULL;
1739: lp->rl_lineno = 0;
1740: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
1741:
1742:
1743: for (dcp = foo->rl_data; *dcp != '\0';) {
1744: lp = (struct rcs_line *)malloc(sizeof(*lp));
1745: if (lp == NULL) {
1746: cvs_log(LP_ERR, "failed to allocate line entry");
1747: return (NULL);
1748: }
1749:
1750: lp->rl_line = dcp;
1751: lp->rl_lineno = ++(foo->rl_nblines);
1752: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
1753:
1754: dcp = strchr(dcp, '\n');
1755: if (dcp == NULL) {
1756: break;
1757: }
1758: *(dcp++) = '\0';
1759: }
1760:
1761: return (foo);
1.5 vincent 1762: }
1763:
1764: static void
1765: rcs_freefoo(struct rcs_foo *fp)
1766: {
1767: struct rcs_line *lp;
1768:
1769: while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
1770: TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
1771: free(lp);
1772: }
1773: free(fp->rl_data);
1774: free(fp);
1.18 jfb 1775: }
1776:
1777: /*
1778: * rcs_growbuf()
1779: *
1780: * Attempt to grow the internal parse buffer for the RCS file <rf> by
1781: * RCS_BUFEXTSIZE.
1782: * In case of failure, the original buffer is left unmodified.
1783: * Returns 0 on success, or -1 on failure.
1784: */
1785: static int
1786: rcs_growbuf(RCSFILE *rf)
1787: {
1788: void *tmp;
1789: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
1790:
1791: tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
1792: if (tmp == NULL) {
1793: cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
1794: return (-1);
1795: }
1796:
1797: pdp->rp_buf = (char *)tmp;
1798: pdp->rp_blen += RCS_BUFEXTSIZE;
1799: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1800:
1801: return (0);
1.1 jfb 1802: }