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