Annotation of src/usr.bin/cvs/rcs.c, Revision 1.41
1.41 ! jfb 1: /* $OpenBSD: rcs.c,v 1.40 2005/04/06 19:12:08 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>
1.26 jfb 35: #include <stdarg.h>
1.1 jfb 36: #include <string.h>
37:
38: #include "rcs.h"
39: #include "log.h"
1.39 joris 40: #include "strtab.h"
1.1 jfb 41:
1.26 jfb 42: #define RCS_BUFSIZE 16384
1.18 jfb 43: #define RCS_BUFEXTSIZE 8192
1.1 jfb 44:
45:
46: /* RCS token types */
47: #define RCS_TOK_ERR -1
48: #define RCS_TOK_EOF 0
49: #define RCS_TOK_NUM 1
50: #define RCS_TOK_ID 2
51: #define RCS_TOK_STRING 3
52: #define RCS_TOK_SCOLON 4
53: #define RCS_TOK_COLON 5
54:
55:
56: #define RCS_TOK_HEAD 8
57: #define RCS_TOK_BRANCH 9
58: #define RCS_TOK_ACCESS 10
59: #define RCS_TOK_SYMBOLS 11
60: #define RCS_TOK_LOCKS 12
61: #define RCS_TOK_COMMENT 13
62: #define RCS_TOK_EXPAND 14
63: #define RCS_TOK_DATE 15
64: #define RCS_TOK_AUTHOR 16
65: #define RCS_TOK_STATE 17
66: #define RCS_TOK_NEXT 18
67: #define RCS_TOK_BRANCHES 19
68: #define RCS_TOK_DESC 20
69: #define RCS_TOK_LOG 21
70: #define RCS_TOK_TEXT 22
71: #define RCS_TOK_STRICT 23
72:
73: #define RCS_ISKEY(t) (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
74:
75:
76: #define RCS_NOSCOL 0x01 /* no terminating semi-colon */
77: #define RCS_VOPT 0x02 /* value is optional */
78:
79:
80: /* opaque parse data */
81: struct rcs_pdata {
1.18 jfb 82: u_int rp_lines;
1.1 jfb 83:
84: char *rp_buf;
85: size_t rp_blen;
1.18 jfb 86: char *rp_bufend;
1.1 jfb 87:
88: /* pushback token buffer */
89: char rp_ptok[128];
90: int rp_pttype; /* token type, RCS_TOK_ERR if no token */
91:
92: FILE *rp_file;
93: };
94:
95:
96: struct rcs_line {
97: char *rl_line;
98: int rl_lineno;
99: TAILQ_ENTRY(rcs_line) rl_list;
100: };
1.5 vincent 101: TAILQ_HEAD(rcs_tqh, rcs_line);
1.1 jfb 102:
103: struct rcs_foo {
104: int rl_nblines;
105: char *rl_data;
1.5 vincent 106: struct rcs_tqh rl_lines;
1.1 jfb 107: };
108:
109: #define RCS_TOKSTR(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
110: #define RCS_TOKLEN(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_blen
111:
112:
1.33 jfb 113: #ifdef notyet
1.20 jfb 114: static struct rcs_kfl {
115: char rk_char;
116: int rk_val;
117: } rcs_kflags[] = {
118: { 'k', RCS_KWEXP_NAME },
119: { 'v', RCS_KWEXP_VAL },
120: { 'l', RCS_KWEXP_LKR },
121: { 'o', RCS_KWEXP_OLD },
122: { 'b', RCS_KWEXP_NONE },
123: };
1.33 jfb 124: #endif
1.20 jfb 125:
1.1 jfb 126: static struct rcs_key {
127: char rk_str[16];
128: int rk_id;
129: int rk_val;
130: int rk_flags;
131: } rcs_keys[] = {
132: { "access", RCS_TOK_ACCESS, RCS_TOK_ID, RCS_VOPT },
1.41 ! jfb 133: { "author", RCS_TOK_AUTHOR, RCS_TOK_ID, 0 },
1.1 jfb 134: { "branch", RCS_TOK_BRANCH, RCS_TOK_NUM, RCS_VOPT },
135: { "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM, RCS_VOPT },
136: { "comment", RCS_TOK_COMMENT, RCS_TOK_STRING, RCS_VOPT },
137: { "date", RCS_TOK_DATE, RCS_TOK_NUM, 0 },
138: { "desc", RCS_TOK_DESC, RCS_TOK_STRING, RCS_NOSCOL },
139: { "expand", RCS_TOK_EXPAND, RCS_TOK_STRING, RCS_VOPT },
140: { "head", RCS_TOK_HEAD, RCS_TOK_NUM, RCS_VOPT },
141: { "locks", RCS_TOK_LOCKS, RCS_TOK_ID, 0 },
142: { "log", RCS_TOK_LOG, RCS_TOK_STRING, RCS_NOSCOL },
143: { "next", RCS_TOK_NEXT, RCS_TOK_NUM, RCS_VOPT },
1.41 ! jfb 144: { "state", RCS_TOK_STATE, RCS_TOK_ID, RCS_VOPT },
1.1 jfb 145: { "strict", RCS_TOK_STRICT, 0, 0, },
146: { "symbols", RCS_TOK_SYMBOLS, 0, 0 },
147: { "text", RCS_TOK_TEXT, RCS_TOK_STRING, RCS_NOSCOL },
148: };
149:
1.18 jfb 150: #define RCS_NKEYS (sizeof(rcs_keys)/sizeof(rcs_keys[0]))
1.1 jfb 151:
1.33 jfb 152: #ifdef notyet
153: /*
154: * Keyword expansion table
155: */
156: static struct rcs_kw {
157: char kw_str[16];
158: } rcs_expkw[] = {
159: { "Author" },
160: { "Date" },
161: { "Header" },
162: { "Id" },
163: { "Log" },
164: { "Name" },
165: { "RCSfile" },
166: { "Revision" },
167: { "Source" },
168: { "State" }
169: };
170: #endif
171:
1.32 jfb 172: static const char *rcs_errstrs[] = {
173: "No error",
174: "No such entry",
175: "Duplicate entry found",
176: "Bad RCS number",
177: };
178:
179: #define RCS_NERR (sizeof(rcs_errstrs)/sizeof(rcs_errstrs[0]))
180:
181:
182: int rcs_errno = RCS_ERR_NOERR;
183:
1.1 jfb 184:
1.27 jfb 185: static int rcs_write (RCSFILE *);
1.26 jfb 186: static int rcs_parse (RCSFILE *);
187: static int rcs_parse_admin (RCSFILE *);
188: static int rcs_parse_delta (RCSFILE *);
189: static int rcs_parse_deltatext (RCSFILE *);
190:
191: static int rcs_parse_access (RCSFILE *);
192: static int rcs_parse_symbols (RCSFILE *);
193: static int rcs_parse_locks (RCSFILE *);
194: static int rcs_parse_branches (RCSFILE *, struct rcs_delta *);
195: static void rcs_freedelta (struct rcs_delta *);
196: static void rcs_freepdata (struct rcs_pdata *);
197: static int rcs_gettok (RCSFILE *);
198: static int rcs_pushtok (RCSFILE *, const char *, int);
199: static int rcs_growbuf (RCSFILE *);
200: static int rcs_patch_lines (struct rcs_foo *, struct rcs_foo *);
201:
202: static struct rcs_delta* rcs_findrev (RCSFILE *, RCSNUM *);
203: static struct rcs_foo* rcs_splitlines (const char *);
204: static void rcs_freefoo (struct rcs_foo *);
205:
206:
1.1 jfb 207: /*
208: * rcs_open()
209: *
210: * Open a file containing RCS-formatted information. The file's path is
1.26 jfb 211: * given in <path>, and the opening flags are given in <flags>, which is either
212: * RCS_READ, RCS_WRITE, or RCS_RDWR. If the open requests write access and
213: * the file does not exist, the RCS_CREATE flag must also be given, in which
214: * case it will be created with the mode specified in a third argument of
215: * type mode_t. If the file exists and RCS_CREATE is passed, the open will
216: * fail.
1.1 jfb 217: * Returns a handle to the opened file on success, or NULL on failure.
218: */
219: RCSFILE*
1.26 jfb 220: rcs_open(const char *path, int flags, ...)
1.1 jfb 221: {
1.26 jfb 222: int ret;
223: mode_t fmode;
1.1 jfb 224: RCSFILE *rfp;
225: struct stat st;
1.26 jfb 226: va_list vap;
227:
228: fmode = 0;
229: flags &= 0xffff; /* ditch any internal flags */
1.1 jfb 230:
1.26 jfb 231: if (((ret = stat(path, &st)) == -1) && (errno == ENOENT)) {
232: if (flags & RCS_CREATE) {
233: va_start(vap, flags);
234: fmode = va_arg(vap, mode_t);
235: va_end(vap);
236: } else {
237: cvs_log(LP_ERR, "RCS file `%s' does not exist", path);
238: return (NULL);
239: }
240: } else if ((ret == 0) && (flags & RCS_CREATE)) {
241: cvs_log(LP_ERR, "RCS file `%s' exists", path);
1.1 jfb 242: return (NULL);
243: }
244:
1.26 jfb 245: if ((rfp = (RCSFILE *)malloc(sizeof(*rfp))) == NULL) {
1.1 jfb 246: cvs_log(LP_ERRNO, "failed to allocate RCS file structure");
247: return (NULL);
248: }
249: memset(rfp, 0, sizeof(*rfp));
250:
1.26 jfb 251: if ((rfp->rf_path = strdup(path)) == NULL) {
1.1 jfb 252: cvs_log(LP_ERRNO, "failed to duplicate RCS file path");
1.26 jfb 253: free(rfp);
1.1 jfb 254: return (NULL);
255: }
256:
257: rfp->rf_ref = 1;
1.26 jfb 258: rfp->rf_flags = flags | RCS_SLOCK;
259: rfp->rf_mode = fmode;
1.1 jfb 260:
261: TAILQ_INIT(&(rfp->rf_delta));
1.29 jfb 262: TAILQ_INIT(&(rfp->rf_access));
1.1 jfb 263: TAILQ_INIT(&(rfp->rf_symbols));
264: TAILQ_INIT(&(rfp->rf_locks));
265:
1.26 jfb 266: if (rfp->rf_flags & RCS_CREATE) {
267: } else if (rcs_parse(rfp) < 0) {
1.1 jfb 268: rcs_close(rfp);
269: return (NULL);
270: }
271:
272: return (rfp);
273: }
274:
275: /*
276: * rcs_close()
277: *
278: * Close an RCS file handle.
279: */
280: void
281: rcs_close(RCSFILE *rfp)
282: {
283: struct rcs_delta *rdp;
1.13 jfb 284: struct rcs_lock *rlp;
285: struct rcs_sym *rsp;
1.1 jfb 286:
287: if (rfp->rf_ref > 1) {
288: rfp->rf_ref--;
289: return;
290: }
291:
1.26 jfb 292: if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
293: rcs_write(rfp);
294:
1.1 jfb 295: while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
296: rdp = TAILQ_FIRST(&(rfp->rf_delta));
297: TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
298: rcs_freedelta(rdp);
299: }
300:
1.13 jfb 301: while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
302: rsp = TAILQ_FIRST(&(rfp->rf_symbols));
303: TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
304: rcsnum_free(rsp->rs_num);
1.39 joris 305: cvs_strfree(rsp->rs_name);
1.13 jfb 306: free(rsp);
307: }
308:
309: while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
310: rlp = TAILQ_FIRST(&(rfp->rf_locks));
311: TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
312: rcsnum_free(rlp->rl_num);
313: free(rlp);
314: }
315:
1.1 jfb 316: if (rfp->rf_head != NULL)
317: rcsnum_free(rfp->rf_head);
1.11 joris 318: if (rfp->rf_branch != NULL)
319: rcsnum_free(rfp->rf_branch);
1.1 jfb 320:
321: if (rfp->rf_path != NULL)
322: free(rfp->rf_path);
323: if (rfp->rf_comment != NULL)
1.39 joris 324: cvs_strfree(rfp->rf_comment);
1.1 jfb 325: if (rfp->rf_expand != NULL)
1.39 joris 326: cvs_strfree(rfp->rf_expand);
1.1 jfb 327: if (rfp->rf_desc != NULL)
1.39 joris 328: cvs_strfree(rfp->rf_desc);
1.1 jfb 329: free(rfp);
330: }
331:
332: /*
333: * rcs_write()
334: *
335: * Write the contents of the RCS file handle <rfp> to disk in the file whose
336: * path is in <rf_path>.
337: * Returns 0 on success, or -1 on failure.
338: */
1.27 jfb 339: static int
1.1 jfb 340: rcs_write(RCSFILE *rfp)
341: {
342: FILE *fp;
1.7 jfb 343: char buf[1024], numbuf[64], *cp;
344: size_t rlen, len;
1.29 jfb 345: struct rcs_access *ap;
1.1 jfb 346: struct rcs_sym *symp;
347: struct rcs_delta *rdp;
348:
1.28 jfb 349: if (rfp->rf_flags & RCS_SYNCED)
1.1 jfb 350: return (0);
351:
1.29 jfb 352: if ((fp = fopen(rfp->rf_path, "w")) == NULL) {
1.1 jfb 353: cvs_log(LP_ERRNO, "failed to open RCS output file `%s'",
354: rfp->rf_path);
355: return (-1);
356: }
357:
1.28 jfb 358: if (rfp->rf_head != NULL)
359: rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
360: else
361: numbuf[0] = '\0';
362:
1.1 jfb 363: fprintf(fp, "head\t%s;\n", numbuf);
1.35 jfb 364:
365: if (rfp->rf_branch != NULL) {
366: rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
367: fprintf(fp, "branch\t%s;\n", numbuf);
368: }
369:
1.29 jfb 370: fputs("access", fp);
371: TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
372: fprintf(fp, "\n\t%s", ap->ra_name);
373: }
374: fputs(";\n", fp);
1.1 jfb 375:
376: fprintf(fp, "symbols\n");
377: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
378: rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
379: snprintf(buf, sizeof(buf), "%s:%s", symp->rs_name, numbuf);
380: fprintf(fp, "\t%s", buf);
381: if (symp != TAILQ_LAST(&(rfp->rf_symbols), rcs_slist))
382: fputc('\n', fp);
383: }
384: fprintf(fp, ";\n");
385:
386: fprintf(fp, "locks;");
387:
1.26 jfb 388: if (rfp->rf_flags & RCS_SLOCK)
1.1 jfb 389: fprintf(fp, " strict;");
390: fputc('\n', fp);
391:
392: if (rfp->rf_comment != NULL)
393: fprintf(fp, "comment\t@%s@;\n", rfp->rf_comment);
394:
395: if (rfp->rf_expand != NULL)
396: fprintf(fp, "expand @ %s @;\n", rfp->rf_expand);
397:
398: fprintf(fp, "\n\n");
399:
400: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
401: fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
402: sizeof(numbuf)));
403: fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
404: rdp->rd_date.tm_year, rdp->rd_date.tm_mon + 1,
405: rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
406: rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
407: fprintf(fp, "\tauthor %s;\tstate %s;\n",
408: rdp->rd_author, rdp->rd_state);
409: fprintf(fp, "branches;\n");
410: fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
411: numbuf, sizeof(numbuf)));
412: }
413:
1.26 jfb 414: fprintf(fp, "\ndesc\n@%s@\n\n",
415: (rfp->rf_desc == NULL) ? "" : rfp->rf_desc);
1.1 jfb 416:
417: /* deltatexts */
418: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
419: fprintf(fp, "\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
420: sizeof(numbuf)));
1.7 jfb 421: fprintf(fp, "log\n@%s@\ntext\n@", rdp->rd_log);
422:
423: cp = rdp->rd_text;
424: do {
425: len = sizeof(buf);
426: rlen = rcs_stresc(1, cp, buf, &len);
427: fprintf(fp, "%s", buf);
428: cp += rlen;
429: } while (len != 0);
430: fprintf(fp, "@\n\n");
1.1 jfb 431: }
432: fclose(fp);
433:
1.26 jfb 434: rfp->rf_flags |= RCS_SYNCED;
1.1 jfb 435:
436: return (0);
437: }
438:
439: /*
1.35 jfb 440: * rcs_branch_get()
441: *
442: * Retrieve the default branch number for the RCS file <file>.
443: * Returns the number on success. If NULL is returned, then there is no
444: * default branch for this file.
445: */
446: const RCSNUM*
447: rcs_branch_get(RCSFILE *file)
448: {
449: return (file->rf_branch);
450: }
451:
452: /*
453: * rcs_branch_set()
454: *
455: * Set the default branch for the RCS file <file> to <bnum>.
456: * Returns 0 on success, -1 on failure.
457: */
458: int
459: rcs_branch_set(RCSFILE *file, const RCSNUM *bnum)
460: {
461: if ((file->rf_branch == NULL) &&
462: ((file->rf_branch = rcsnum_alloc()) == NULL))
463: return (-1);
464:
465: if (rcsnum_cpy(bnum, file->rf_branch, 0) < 0) {
466: rcsnum_free(file->rf_branch);
467: file->rf_branch = NULL;
468: return (-1);
469: }
470:
471: return (0);
472: }
473:
474: /*
1.29 jfb 475: * rcs_access_add()
476: *
477: * Add the login name <login> to the access list for the RCS file <file>.
478: * Returns 0 on success, or -1 on failure.
479: */
480: int
481: rcs_access_add(RCSFILE *file, const char *login)
482: {
483: struct rcs_access *ap;
484:
485: /* first look for duplication */
486: TAILQ_FOREACH(ap, &(file->rf_access), ra_list) {
487: if (strcmp(ap->ra_name, login) == 0) {
1.32 jfb 488: rcs_errno = RCS_ERR_DUPENT;
1.29 jfb 489: return (-1);
490: }
491: }
492:
493: ap = (struct rcs_access *)malloc(sizeof(*ap));
494: if (ap == NULL) {
495: cvs_log(LP_ERRNO, "failed to allocate RCS access entry");
496: return (-1);
497: }
498:
1.39 joris 499: ap->ra_name = cvs_strdup(login);
1.29 jfb 500: if (ap->ra_name == NULL) {
501: cvs_log(LP_ERRNO, "failed to duplicate user name");
502: free(ap);
503: return (-1);
504: }
505:
506: TAILQ_INSERT_TAIL(&(file->rf_access), ap, ra_list);
507:
508: /* not synced anymore */
509: file->rf_flags &= ~RCS_SYNCED;
510: return (0);
511: }
512:
513: /*
514: * rcs_access_remove()
515: *
516: * Remove an entry with login name <login> from the access list of the RCS
517: * file <file>.
518: * Returns 0 on success, or -1 on failure.
519: */
520: int
521: rcs_access_remove(RCSFILE *file, const char *login)
522: {
523: struct rcs_access *ap;
524:
525: TAILQ_FOREACH(ap, &(file->rf_access), ra_list)
526: if (strcmp(ap->ra_name, login) == 0)
527: break;
528:
529: if (ap == NULL) {
1.32 jfb 530: rcs_errno = RCS_ERR_NOENT;
1.29 jfb 531: return (-1);
532: }
533:
534: TAILQ_REMOVE(&(file->rf_access), ap, ra_list);
1.39 joris 535: cvs_strfree(ap->ra_name);
1.29 jfb 536: free(ap);
537:
538: /* not synced anymore */
539: file->rf_flags &= ~RCS_SYNCED;
540: return (0);
541: }
542:
543: /*
1.26 jfb 544: * rcs_sym_add()
1.1 jfb 545: *
546: * Add a symbol to the list of symbols for the RCS file <rfp>. The new symbol
547: * is named <sym> and is bound to the RCS revision <snum>.
548: * Returns 0 on success, or -1 on failure.
549: */
550: int
1.26 jfb 551: rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
1.1 jfb 552: {
553: struct rcs_sym *symp;
554:
555: /* first look for duplication */
556: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
557: if (strcmp(symp->rs_name, sym) == 0) {
1.32 jfb 558: rcs_errno = RCS_ERR_DUPENT;
1.1 jfb 559: return (-1);
560: }
561: }
562:
563: symp = (struct rcs_sym *)malloc(sizeof(*symp));
564: if (symp == NULL) {
565: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
566: return (-1);
567: }
568:
1.39 joris 569: symp->rs_name = cvs_strdup(sym);
1.10 joris 570: if (symp->rs_name == NULL) {
571: cvs_log(LP_ERRNO, "failed to duplicate symbol");
572: free(symp);
573: return (-1);
574: }
575:
1.1 jfb 576: symp->rs_num = rcsnum_alloc();
1.11 joris 577: if (symp->rs_num == NULL) {
1.39 joris 578: cvs_strfree(symp->rs_name);
1.11 joris 579: free(symp);
580: return (-1);
581: }
1.1 jfb 582: rcsnum_cpy(snum, symp->rs_num, 0);
583:
584: TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
585:
586: /* not synced anymore */
1.26 jfb 587: rfp->rf_flags &= ~RCS_SYNCED;
1.1 jfb 588: return (0);
589: }
590:
591: /*
1.27 jfb 592: * rcs_sym_remove()
593: *
594: * Remove the symbol with name <sym> from the symbol list for the RCS file
595: * <file>. If no such symbol is found, the call fails and returns with an
596: * error.
597: * Returns 0 on success, or -1 on failure.
598: */
599: int
600: rcs_sym_remove(RCSFILE *file, const char *sym)
601: {
602: struct rcs_sym *symp;
603:
604: TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
605: if (strcmp(symp->rs_name, sym) == 0)
606: break;
607:
608: if (symp == NULL) {
1.32 jfb 609: rcs_errno = RCS_ERR_NOENT;
1.27 jfb 610: return (-1);
611: }
612:
613: TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
1.39 joris 614: cvs_strfree(symp->rs_name);
1.27 jfb 615: rcsnum_free(symp->rs_num);
616: free(symp);
617:
618: /* not synced anymore */
619: file->rf_flags &= ~RCS_SYNCED;
620: return (0);
621: }
622:
623: /*
624: * rcs_sym_getrev()
625: *
626: * Retrieve the RCS revision number associated with the symbol <sym> for the
627: * RCS file <file>. The returned value is a dynamically-allocated copy and
628: * should be freed by the caller once they are done with it.
629: * Returns the RCSNUM on success, or NULL on failure.
630: */
631: RCSNUM*
632: rcs_sym_getrev(RCSFILE *file, const char *sym)
633: {
634: RCSNUM *num;
635: struct rcs_sym *symp;
636:
637: num = NULL;
638: TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
639: if (strcmp(symp->rs_name, sym) == 0)
640: break;
641:
1.36 jfb 642: if (symp == NULL)
643: rcs_errno = RCS_ERR_NOENT;
644: else if (((num = rcsnum_alloc()) != NULL) &&
1.27 jfb 645: (rcsnum_cpy(symp->rs_num, num, 0) < 0)) {
646: rcsnum_free(num);
647: num = NULL;
648: }
649:
650: return (num);
1.30 jfb 651: }
652:
653: /*
654: * rcs_lock_getmode()
655: *
656: * Retrieve the locking mode of the RCS file <file>.
657: */
658: int
659: rcs_lock_getmode(RCSFILE *file)
660: {
661: return (file->rf_flags & RCS_SLOCK) ? RCS_LOCK_STRICT : RCS_LOCK_LOOSE;
662: }
663:
664: /*
665: * rcs_lock_setmode()
666: *
667: * Set the locking mode of the RCS file <file> to <mode>, which must either
668: * be RCS_LOCK_LOOSE or RCS_LOCK_STRICT.
669: * Returns the previous mode on success, or -1 on failure.
670: */
671: int
672: rcs_lock_setmode(RCSFILE *file, int mode)
673: {
674: int pmode;
675: pmode = rcs_lock_getmode(file);
676:
677: if (mode == RCS_LOCK_STRICT)
678: file->rf_flags |= RCS_SLOCK;
679: else if (mode == RCS_LOCK_LOOSE)
680: file->rf_flags &= ~RCS_SLOCK;
681: else {
682: cvs_log(LP_ERRNO, "invalid lock mode %d", mode);
683: return (-1);
684: }
685:
686: return (pmode);
1.27 jfb 687: }
688:
689: /*
1.40 jfb 690: * rcs_lock_add()
691: *
692: * Add an RCS lock for the user <user> on revision <rev>.
693: * Returns 0 on success, or -1 on failure.
694: */
695: int
696: rcs_lock_add(RCSFILE *file, const char *user, RCSNUM *rev)
697: {
698: struct rcs_lock *lkp;
699:
700: /* first look for duplication */
701: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
702: if (strcmp(lkp->rl_name, user) == 0) {
703: rcs_errno = RCS_ERR_DUPENT;
704: return (-1);
705: }
706: }
707:
708: lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
709: if (lkp == NULL) {
710: cvs_log(LP_ERRNO, "failed to allocate RCS lock");
711: return (-1);
712: }
713:
714: lkp->rl_name = cvs_strdup(user);
715: if (lkp->rl_name == NULL) {
716: cvs_log(LP_ERRNO, "failed to duplicate user name");
717: free(lkp);
718: return (-1);
719: }
720:
721: TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
722:
723: /* not synced anymore */
724: file->rf_flags &= ~RCS_SYNCED;
725: return (0);
726:
727:
728: }
729:
730:
731: /*
732: * rcs_lock_remove()
733: *
734: * Remove the RCS lock on revision <rev>.
735: * Returns 0 on success, or -1 on failure.
736: */
737: int
738: rcs_lock_remove(RCSFILE *file, const RCSNUM *rev)
739: {
740: struct rcs_lock *lkp;
741:
742: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list)
743: if (rcsnum_cmp(lkp->rl_num, rev, 0) == 0)
744: break;
745:
746: if (lkp == NULL) {
747: rcs_errno = RCS_ERR_NOENT;
748: return (-1);
749: }
750:
751: TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
752: rcsnum_free(lkp->rl_num);
753: cvs_strfree(lkp->rl_name);
754: free(lkp);
755:
756: /* not synced anymore */
757: file->rf_flags &= ~RCS_SYNCED;
758: return (0);
759: }
760:
761: /*
1.27 jfb 762: * rcs_desc_get()
763: *
764: * Retrieve the description for the RCS file <file>.
765: */
766: const char*
767: rcs_desc_get(RCSFILE *file)
768: {
769: return (file->rf_desc);
770: }
771:
772: /*
773: * rcs_desc_set()
774: *
775: * Set the description for the RCS file <file>.
776: * Returns 0 on success, or -1 on failure.
777: */
778: int
779: rcs_desc_set(RCSFILE *file, const char *desc)
780: {
781: char *tmp;
782:
1.39 joris 783: if ((tmp = cvs_strdup(desc)) == NULL)
1.27 jfb 784: return (-1);
785:
786: if (file->rf_desc != NULL)
1.39 joris 787: cvs_strfree(file->rf_desc);
1.27 jfb 788: file->rf_desc = tmp;
789: file->rf_flags &= ~RCS_SYNCED;
790:
791: return (0);
792: }
793:
1.33 jfb 794: /*
795: * rcs_comment_get()
796: *
797: * Retrieve the comment leader for the RCS file <file>.
798: */
799: const char*
800: rcs_comment_get(RCSFILE *file)
801: {
802: return (file->rf_comment);
803: }
804:
805: /*
806: * rcs_comment_set()
807: *
808: * Set the comment leader for the RCS file <file>.
809: * Returns 0 on success, or -1 on failure.
810: */
811: int
812: rcs_comment_set(RCSFILE *file, const char *comment)
813: {
814: char *tmp;
815:
1.39 joris 816: if ((tmp = cvs_strdup(comment)) == NULL)
1.33 jfb 817: return (-1);
818:
819: if (file->rf_comment != NULL)
1.39 joris 820: cvs_strfree(file->rf_comment);
1.33 jfb 821: file->rf_comment = tmp;
822: file->rf_flags &= ~RCS_SYNCED;
823:
824: return (0);
825: }
1.40 jfb 826:
827: /*
828: * rcs_tag_resolve()
829: *
830: * Retrieve the revision number corresponding to the tag <tag> for the RCS
831: * file <file>.
832: */
833: RCSNUM*
834: rcs_tag_resolve(RCSFILE *file, const char *tag)
835: {
836: RCSNUM *num;
837:
838: if ((num = rcsnum_parse(tag)) == NULL) {
839: num = rcs_sym_getrev(file, tag);
840: }
841:
842: return (num);
843: }
844:
1.27 jfb 845:
846: /*
1.1 jfb 847: * rcs_patch()
848: *
849: * Apply an RCS-format patch pointed to by <patch> to the file contents
850: * found in <data>.
851: * Returns 0 on success, or -1 on failure.
852: */
853: BUF*
854: rcs_patch(const char *data, const char *patch)
855: {
1.5 vincent 856: struct rcs_foo *dlines, *plines;
857: struct rcs_line *lp;
1.1 jfb 858: size_t len;
1.5 vincent 859: int lineno;
1.1 jfb 860: BUF *res;
861:
862: len = strlen(data);
863: res = cvs_buf_alloc(len, BUF_AUTOEXT);
864: if (res == NULL)
865: return (NULL);
866:
867: dlines = rcs_splitlines(data);
1.17 jfb 868: if (dlines == NULL) {
869: cvs_buf_free(res);
1.1 jfb 870: return (NULL);
1.17 jfb 871: }
1.5 vincent 872:
1.1 jfb 873: plines = rcs_splitlines(patch);
1.5 vincent 874: if (plines == NULL) {
1.17 jfb 875: cvs_buf_free(res);
1.5 vincent 876: rcs_freefoo(dlines);
1.1 jfb 877: return (NULL);
1.5 vincent 878: }
879:
880: if (rcs_patch_lines(dlines, plines) < 0) {
1.17 jfb 881: cvs_buf_free(res);
1.5 vincent 882: rcs_freefoo(plines);
883: rcs_freefoo(dlines);
884: return (NULL);
885: }
886:
887: lineno = 0;
888: TAILQ_FOREACH(lp, &dlines->rl_lines, rl_list) {
889: if (lineno != 0)
890: cvs_buf_fappend(res, "%s\n", lp->rl_line);
891: lineno++;
892: }
893:
894: rcs_freefoo(dlines);
895: rcs_freefoo(plines);
896: return (res);
897: }
898:
1.7 jfb 899: static int
1.5 vincent 900: rcs_patch_lines(struct rcs_foo *dlines, struct rcs_foo *plines)
901: {
902: char op, *ep;
903: struct rcs_line *lp, *dlp, *ndlp;
904: int i, lineno, nbln;
1.1 jfb 905:
906: dlp = TAILQ_FIRST(&(dlines->rl_lines));
907: lp = TAILQ_FIRST(&(plines->rl_lines));
908:
909: /* skip first bogus line */
910: for (lp = TAILQ_NEXT(lp, rl_list); lp != NULL;
911: lp = TAILQ_NEXT(lp, rl_list)) {
912: op = *(lp->rl_line);
913: lineno = (int)strtol((lp->rl_line + 1), &ep, 10);
914: if ((lineno > dlines->rl_nblines) || (lineno <= 0) ||
915: (*ep != ' ')) {
916: cvs_log(LP_ERR,
917: "invalid line specification in RCS patch");
918: return (NULL);
919: }
920: ep++;
921: nbln = (int)strtol(ep, &ep, 10);
922: if ((nbln <= 0) || (*ep != '\0')) {
923: cvs_log(LP_ERR,
924: "invalid line number specification in RCS patch");
925: return (NULL);
926: }
927:
928: /* find the appropriate line */
929: for (;;) {
930: if (dlp == NULL)
931: break;
932: if (dlp->rl_lineno == lineno)
933: break;
934: if (dlp->rl_lineno > lineno) {
935: dlp = TAILQ_PREV(dlp, rcs_tqh, rl_list);
1.14 deraadt 936: } else if (dlp->rl_lineno < lineno) {
1.1 jfb 937: ndlp = TAILQ_NEXT(dlp, rl_list);
938: if (ndlp->rl_lineno > lineno)
939: break;
940: dlp = ndlp;
941: }
942: }
943: if (dlp == NULL) {
944: cvs_log(LP_ERR,
945: "can't find referenced line in RCS patch");
946: return (NULL);
947: }
948:
949: if (op == 'd') {
950: for (i = 0; (i < nbln) && (dlp != NULL); i++) {
951: ndlp = TAILQ_NEXT(dlp, rl_list);
952: TAILQ_REMOVE(&(dlines->rl_lines), dlp, rl_list);
953: dlp = ndlp;
954: }
1.14 deraadt 955: } else if (op == 'a') {
1.1 jfb 956: for (i = 0; i < nbln; i++) {
957: ndlp = lp;
958: lp = TAILQ_NEXT(lp, rl_list);
959: if (lp == NULL) {
960: cvs_log(LP_ERR, "truncated RCS patch");
1.5 vincent 961: return (-1);
1.1 jfb 962: }
963: TAILQ_REMOVE(&(plines->rl_lines), lp, rl_list);
964: TAILQ_INSERT_AFTER(&(dlines->rl_lines), dlp,
965: lp, rl_list);
966: dlp = lp;
967:
968: /* we don't want lookup to block on those */
969: lp->rl_lineno = lineno;
970:
971: lp = ndlp;
972: }
1.14 deraadt 973: } else {
1.1 jfb 974: cvs_log(LP_ERR, "unknown RCS patch operation `%c'", op);
1.5 vincent 975: return (-1);
1.1 jfb 976: }
977:
978: /* last line of the patch, done */
979: if (lp->rl_lineno == plines->rl_nblines)
980: break;
981: }
982:
983: /* once we're done patching, rebuild the line numbers */
1.2 vincent 984: lineno = 0;
1.5 vincent 985: TAILQ_FOREACH(lp, &(dlines->rl_lines), rl_list)
1.1 jfb 986: lp->rl_lineno = lineno++;
987: dlines->rl_nblines = lineno - 1;
988:
1.5 vincent 989: return (0);
1.1 jfb 990: }
991:
992: /*
993: * rcs_getrev()
994: *
995: * Get the whole contents of revision <rev> from the RCSFILE <rfp>. The
1.4 vincent 996: * returned buffer is dynamically allocated and should be released using
997: * cvs_buf_free() once the caller is done using it.
1.1 jfb 998: */
999: BUF*
1000: rcs_getrev(RCSFILE *rfp, RCSNUM *rev)
1001: {
1002: int res;
1003: size_t len;
1004: void *bp;
1005: RCSNUM *crev;
1006: BUF *rbuf;
1007: struct rcs_delta *rdp = NULL;
1008:
1.28 jfb 1009: if (rfp->rf_head == NULL)
1010: return (NULL);
1011:
1.1 jfb 1012: res = rcsnum_cmp(rfp->rf_head, rev, 0);
1013: if (res == 1) {
1.32 jfb 1014: rcs_errno = RCS_ERR_NOENT;
1.1 jfb 1015: return (NULL);
1.26 jfb 1016: }
1017:
1018: rdp = rcs_findrev(rfp, rfp->rf_head);
1019: if (rdp == NULL) {
1020: cvs_log(LP_ERR, "failed to get RCS HEAD revision");
1021: return (NULL);
1022: }
1023:
1024: len = strlen(rdp->rd_text);
1025: if ((rbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
1026: return (NULL);
1027:
1028: cvs_buf_append(rbuf, rdp->rd_text, len);
1029:
1030: if (res != 0) {
1031: /* Apply patches backwards to get the right version.
1032: * This will need some rework to support sub branches.
1033: */
1034: if ((crev = rcsnum_alloc()) == NULL) {
1035: cvs_buf_free(rbuf);
1.1 jfb 1036: return (NULL);
1037: }
1.26 jfb 1038: rcsnum_cpy(rfp->rf_head, crev, 0);
1039: do {
1040: crev->rn_id[crev->rn_len - 1]--;
1041: rdp = rcs_findrev(rfp, crev);
1042: if (rdp == NULL) {
1043: rcsnum_free(crev);
1044: cvs_buf_free(rbuf);
1045: return (NULL);
1046: }
1.1 jfb 1047:
1.26 jfb 1048: if (cvs_buf_putc(rbuf, '\0') < 0) {
1049: rcsnum_free(crev);
1.17 jfb 1050: cvs_buf_free(rbuf);
1.11 joris 1051: return (NULL);
1.17 jfb 1052: }
1.26 jfb 1053: bp = cvs_buf_release(rbuf);
1054: rbuf = rcs_patch((char *)bp, rdp->rd_text);
1055: if (rbuf == NULL)
1056: break;
1057: } while (rcsnum_cmp(crev, rev, 0) != 0);
1.1 jfb 1058:
1.26 jfb 1059: rcsnum_free(crev);
1.1 jfb 1060: }
1061:
1062: return (rbuf);
1.16 jfb 1063: }
1064:
1065: /*
1066: * rcs_gethead()
1067: *
1068: * Get the head revision for the RCS file <rf>.
1069: */
1070: BUF*
1071: rcs_gethead(RCSFILE *rf)
1072: {
1073: return rcs_getrev(rf, rf->rf_head);
1.1 jfb 1074: }
1075:
1076: /*
1077: * rcs_getrevbydate()
1078: *
1079: * Get an RCS revision by a specific date.
1080: */
1081: RCSNUM*
1082: rcs_getrevbydate(RCSFILE *rfp, struct tm *date)
1083: {
1084: return (NULL);
1085: }
1086:
1087: /*
1088: * rcs_findrev()
1089: *
1090: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1091: * The revision number is given in <rev>.
1092: * Returns a pointer to the delta on success, or NULL on failure.
1093: */
1094: static struct rcs_delta*
1095: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
1096: {
1097: u_int cmplen;
1098: struct rcs_delta *rdp;
1099: struct rcs_dlist *hp;
1.6 vincent 1100: int found;
1.26 jfb 1101:
1.1 jfb 1102: cmplen = 2;
1103: hp = &(rfp->rf_delta);
1104:
1.6 vincent 1105: do {
1106: found = 0;
1107: TAILQ_FOREACH(rdp, hp, rd_list) {
1108: if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
1109: if (cmplen == rev->rn_len)
1110: return (rdp);
1.1 jfb 1111:
1.6 vincent 1112: hp = &(rdp->rd_snodes);
1113: cmplen += 2;
1114: found = 1;
1115: break;
1116: }
1.1 jfb 1117: }
1.6 vincent 1118: } while (found && cmplen < rev->rn_len);
1.1 jfb 1119:
1120: return (NULL);
1.20 jfb 1121: }
1122:
1123: /*
1.26 jfb 1124: * rcs_kwexp_set()
1125: *
1126: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1127: * Returns 0 on success, or -1 on failure.
1128: */
1129: int
1130: rcs_kwexp_set(RCSFILE *file, int mode)
1131: {
1132: int i;
1133: char *tmp, buf[8] = "";
1134:
1135: if (RCS_KWEXP_INVAL(mode))
1136: return (-1);
1137:
1138: i = 0;
1139: if (mode == RCS_KWEXP_NONE)
1140: buf[0] = 'b';
1141: else if (mode == RCS_KWEXP_OLD)
1142: buf[0] = 'o';
1143: else {
1144: if (mode & RCS_KWEXP_NAME)
1145: buf[i++] = 'k';
1146: if (mode & RCS_KWEXP_VAL)
1147: buf[i++] = 'v';
1148: if (mode & RCS_KWEXP_LKR)
1149: buf[i++] = 'l';
1150: }
1151:
1.39 joris 1152: if ((tmp = cvs_strdup(buf)) == NULL) {
1.26 jfb 1153: cvs_log(LP_ERRNO, "%s: failed to copy expansion mode",
1154: file->rf_path);
1155: return (-1);
1156: }
1157:
1.27 jfb 1158: if (file->rf_expand != NULL)
1.39 joris 1159: cvs_strfree(file->rf_expand);
1.26 jfb 1160: file->rf_expand = tmp;
1161:
1162: return (0);
1163: }
1164:
1165: /*
1166: * rcs_kwexp_get()
1167: *
1168: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1169: */
1170: int
1171: rcs_kwexp_get(RCSFILE *file)
1172: {
1173: return rcs_kflag_get(file->rf_expand);
1174: }
1175:
1176: /*
1.20 jfb 1177: * rcs_kflag_get()
1178: *
1179: * Get the keyword expansion mode from a set of character flags given in
1180: * <flags> and return the appropriate flag mask. In case of an error, the
1181: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1182: */
1183: int
1184: rcs_kflag_get(const char *flags)
1185: {
1186: int fl;
1187: size_t len;
1188: const char *fp;
1189:
1190: fl = 0;
1191: len = strlen(flags);
1192:
1193: for (fp = flags; *fp != '\0'; fp++) {
1194: if (*fp == 'k')
1195: fl |= RCS_KWEXP_NAME;
1196: else if (*fp == 'v')
1197: fl |= RCS_KWEXP_VAL;
1198: else if (*fp == 'l')
1199: fl |= RCS_KWEXP_LKR;
1200: else if (*fp == 'o') {
1201: if (len != 1)
1202: fl |= RCS_KWEXP_ERR;
1203: fl |= RCS_KWEXP_OLD;
1204: } else if (*fp == 'b') {
1205: if (len != 1)
1206: fl |= RCS_KWEXP_ERR;
1207: } else /* unknown letter */
1208: fl |= RCS_KWEXP_ERR;
1209: }
1210:
1211: return (fl);
1.32 jfb 1212: }
1213:
1214: /*
1215: * rcs_errstr()
1216: *
1217: * Get the error string matching the RCS error code <code>.
1218: */
1219: const char*
1220: rcs_errstr(int code)
1221: {
1.37 tedu 1222: if ((code < 0) || (code >= (int)RCS_NERR))
1.32 jfb 1223: return (NULL);
1224: return (rcs_errstrs[code]);
1.1 jfb 1225: }
1226:
1.21 jfb 1227: void
1228: rcs_kflag_usage(void)
1229: {
1230: fprintf(stderr, "Valid expansion modes include:\n"
1.22 jfb 1231: "\t-kkv\tGenerate keywords using the default form.\n"
1232: "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
1233: "\t-kk\tGenerate only keyword names in keyword strings.\n"
1234: "\t-kv\tGenerate only keyword values in keyword strings.\n"
1235: "\t-ko\tGenerate old keyword string "
1.21 jfb 1236: "(no changes from checked in file).\n"
1.22 jfb 1237: "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21 jfb 1238: }
1.1 jfb 1239:
1240: /*
1241: * rcs_parse()
1242: *
1243: * Parse the contents of file <path>, which are in the RCS format.
1244: * Returns 0 on success, or -1 on failure.
1245: */
1.26 jfb 1246: static int
1.1 jfb 1247: rcs_parse(RCSFILE *rfp)
1248: {
1249: int ret;
1250: struct rcs_pdata *pdp;
1251:
1.26 jfb 1252: if (rfp->rf_flags & RCS_PARSED)
1.1 jfb 1253: return (0);
1254:
1.26 jfb 1255: if ((pdp = (struct rcs_pdata *)malloc(sizeof(*pdp))) == NULL) {
1.1 jfb 1256: cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
1257: return (-1);
1258: }
1259: memset(pdp, 0, sizeof(*pdp));
1260:
1.18 jfb 1261: pdp->rp_lines = 0;
1.1 jfb 1262: pdp->rp_pttype = RCS_TOK_ERR;
1263:
1264: pdp->rp_file = fopen(rfp->rf_path, "r");
1265: if (pdp->rp_file == NULL) {
1266: cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
1267: rcs_freepdata(pdp);
1268: return (-1);
1269: }
1270:
1271: pdp->rp_buf = (char *)malloc(RCS_BUFSIZE);
1272: if (pdp->rp_buf == NULL) {
1273: cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
1274: rcs_freepdata(pdp);
1275: return (-1);
1276: }
1277: pdp->rp_blen = RCS_BUFSIZE;
1.18 jfb 1278: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1 jfb 1279:
1280: /* ditch the strict lock */
1.26 jfb 1281: rfp->rf_flags &= ~RCS_SLOCK;
1.1 jfb 1282: rfp->rf_pdata = pdp;
1283:
1.31 jfb 1284: if ((ret = rcs_parse_admin(rfp)) < 0) {
1.1 jfb 1285: rcs_freepdata(pdp);
1286: return (-1);
1.31 jfb 1287: } else if (ret == RCS_TOK_NUM) {
1288: for (;;) {
1289: ret = rcs_parse_delta(rfp);
1290: if (ret == 0)
1291: break;
1292: else if (ret == -1) {
1293: rcs_freepdata(pdp);
1294: return (-1);
1295: }
1.1 jfb 1296: }
1297: }
1298:
1299: ret = rcs_gettok(rfp);
1300: if (ret != RCS_TOK_DESC) {
1301: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1302: RCS_TOKSTR(rfp));
1303: rcs_freepdata(pdp);
1304: return (-1);
1305: }
1306:
1307: ret = rcs_gettok(rfp);
1308: if (ret != RCS_TOK_STRING) {
1309: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1310: RCS_TOKSTR(rfp));
1311: rcs_freepdata(pdp);
1312: return (-1);
1313: }
1314:
1.39 joris 1315: rfp->rf_desc = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1316: if (rfp->rf_desc == NULL) {
1317: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
1318: rcs_freepdata(pdp);
1319: return (-1);
1320: }
1.1 jfb 1321:
1322: for (;;) {
1323: ret = rcs_parse_deltatext(rfp);
1324: if (ret == 0)
1325: break;
1326: else if (ret == -1) {
1327: rcs_freepdata(pdp);
1328: return (-1);
1329: }
1330: }
1331:
1332: cvs_log(LP_DEBUG, "RCS file `%s' parsed OK (%u lines)", rfp->rf_path,
1.18 jfb 1333: pdp->rp_lines);
1.1 jfb 1334:
1335: rcs_freepdata(pdp);
1336:
1337: rfp->rf_pdata = NULL;
1.26 jfb 1338: rfp->rf_flags |= RCS_PARSED | RCS_SYNCED;
1.1 jfb 1339:
1340: return (0);
1341: }
1342:
1343: /*
1344: * rcs_parse_admin()
1345: *
1346: * Parse the administrative portion of an RCS file.
1.31 jfb 1347: * Returns the type of the first token found after the admin section on
1348: * success, or -1 on failure.
1.1 jfb 1349: */
1350: static int
1351: rcs_parse_admin(RCSFILE *rfp)
1352: {
1353: u_int i;
1354: int tok, ntok, hmask;
1355: struct rcs_key *rk;
1356:
1357: /* hmask is a mask of the headers already encountered */
1358: hmask = 0;
1359: for (;;) {
1360: tok = rcs_gettok(rfp);
1361: if (tok == RCS_TOK_ERR) {
1362: cvs_log(LP_ERR, "parse error in RCS admin section");
1363: return (-1);
1.31 jfb 1364: } else if ((tok == RCS_TOK_NUM) || (tok == RCS_TOK_DESC)) {
1365: /*
1366: * Assume this is the start of the first delta or
1367: * that we are dealing with an empty RCS file and
1368: * we just found the description.
1369: */
1.1 jfb 1370: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31 jfb 1371: return (tok);
1.1 jfb 1372: }
1373:
1374: rk = NULL;
1.18 jfb 1375: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1376: if (rcs_keys[i].rk_id == tok)
1377: rk = &(rcs_keys[i]);
1378:
1379: if (hmask & (1 << tok)) {
1380: cvs_log(LP_ERR, "duplicate RCS key");
1381: return (-1);
1382: }
1383: hmask |= (1 << tok);
1384:
1385: switch (tok) {
1386: case RCS_TOK_HEAD:
1387: case RCS_TOK_BRANCH:
1388: case RCS_TOK_COMMENT:
1389: case RCS_TOK_EXPAND:
1390: ntok = rcs_gettok(rfp);
1391: if (ntok == RCS_TOK_SCOLON)
1392: break;
1393: if (ntok != rk->rk_val) {
1394: cvs_log(LP_ERR,
1395: "invalid value type for RCS key `%s'",
1396: rk->rk_str);
1397: }
1398:
1399: if (tok == RCS_TOK_HEAD) {
1.28 jfb 1400: if (rfp->rf_head == NULL) {
1401: rfp->rf_head = rcsnum_alloc();
1402: if (rfp->rf_head == NULL)
1403: return (-1);
1404: }
1.1 jfb 1405: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1406: rfp->rf_head);
1.14 deraadt 1407: } else if (tok == RCS_TOK_BRANCH) {
1.35 jfb 1408: if (rfp->rf_branch == NULL) {
1409: rfp->rf_branch = rcsnum_alloc();
1410: if (rfp->rf_branch == NULL)
1411: return (-1);
1412: }
1413: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1414: rfp->rf_branch) < 0)
1415: return (-1);
1.14 deraadt 1416: } else if (tok == RCS_TOK_COMMENT) {
1.39 joris 1417: rfp->rf_comment = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1418: if (rfp->rf_comment == NULL) {
1419: cvs_log(LP_ERRNO,
1420: "failed to duplicate rcs token");
1421: return (-1);
1422: }
1.14 deraadt 1423: } else if (tok == RCS_TOK_EXPAND) {
1.39 joris 1424: rfp->rf_expand = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1425: if (rfp->rf_expand == NULL) {
1426: cvs_log(LP_ERRNO,
1427: "failed to duplicate rcs token");
1428: return (-1);
1429: }
1.1 jfb 1430: }
1431:
1432: /* now get the expected semi-colon */
1433: ntok = rcs_gettok(rfp);
1434: if (ntok != RCS_TOK_SCOLON) {
1435: cvs_log(LP_ERR,
1436: "missing semi-colon after RCS `%s' key",
1.26 jfb 1437: rk->rk_str);
1.1 jfb 1438: return (-1);
1439: }
1440: break;
1441: case RCS_TOK_ACCESS:
1.29 jfb 1442: if (rcs_parse_access(rfp) < 0)
1443: return (-1);
1.1 jfb 1444: break;
1445: case RCS_TOK_SYMBOLS:
1.29 jfb 1446: if (rcs_parse_symbols(rfp) < 0)
1447: return (-1);
1.1 jfb 1448: break;
1449: case RCS_TOK_LOCKS:
1.29 jfb 1450: if (rcs_parse_locks(rfp) < 0)
1451: return (-1);
1.1 jfb 1452: break;
1453: default:
1454: cvs_log(LP_ERR,
1455: "unexpected token `%s' in RCS admin section",
1456: RCS_TOKSTR(rfp));
1457: return (-1);
1458: }
1459: }
1460:
1461: return (0);
1462: }
1463:
1464: /*
1465: * rcs_parse_delta()
1466: *
1467: * Parse an RCS delta section and allocate the structure to store that delta's
1468: * information in the <rfp> delta list.
1469: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1470: * -1 on error.
1471: */
1472: static int
1473: rcs_parse_delta(RCSFILE *rfp)
1474: {
1475: int ret, tok, ntok, hmask;
1476: u_int i;
1477: char *tokstr;
1.3 vincent 1478: RCSNUM *datenum;
1.1 jfb 1479: struct rcs_delta *rdp;
1480: struct rcs_key *rk;
1481:
1482: rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
1483: if (rdp == NULL) {
1484: cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
1485: return (-1);
1486: }
1487: memset(rdp, 0, sizeof(*rdp));
1488:
1489: rdp->rd_num = rcsnum_alloc();
1.11 joris 1490: if (rdp->rd_num == NULL) {
1491: rcs_freedelta(rdp);
1492: return (-1);
1493: }
1.1 jfb 1494: rdp->rd_next = rcsnum_alloc();
1.11 joris 1495: if (rdp->rd_next == NULL) {
1496: rcs_freedelta(rdp);
1497: return (-1);
1498: }
1.1 jfb 1499:
1500: TAILQ_INIT(&(rdp->rd_branches));
1501:
1502: tok = rcs_gettok(rfp);
1503: if (tok != RCS_TOK_NUM) {
1504: cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
1505: RCS_TOKSTR(rfp));
1506: rcs_freedelta(rdp);
1507: return (-1);
1508: }
1509: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1510:
1511: hmask = 0;
1512: ret = 0;
1513: tokstr = NULL;
1514:
1515: for (;;) {
1516: tok = rcs_gettok(rfp);
1517: if (tok == RCS_TOK_ERR) {
1518: cvs_log(LP_ERR, "parse error in RCS delta section");
1519: rcs_freedelta(rdp);
1520: return (-1);
1.14 deraadt 1521: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15 tedu 1522: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1 jfb 1523: ret = (tok == RCS_TOK_NUM ? 1 : 0);
1524: break;
1525: }
1526:
1527: rk = NULL;
1.18 jfb 1528: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1529: if (rcs_keys[i].rk_id == tok)
1530: rk = &(rcs_keys[i]);
1531:
1532: if (hmask & (1 << tok)) {
1533: cvs_log(LP_ERR, "duplicate RCS key");
1534: rcs_freedelta(rdp);
1535: return (-1);
1536: }
1537: hmask |= (1 << tok);
1538:
1539: switch (tok) {
1540: case RCS_TOK_DATE:
1541: case RCS_TOK_AUTHOR:
1542: case RCS_TOK_STATE:
1543: case RCS_TOK_NEXT:
1544: ntok = rcs_gettok(rfp);
1545: if (ntok == RCS_TOK_SCOLON) {
1546: if (rk->rk_flags & RCS_VOPT)
1547: break;
1548: else {
1549: cvs_log(LP_ERR, "missing mandatory "
1550: "value to RCS key `%s'",
1551: rk->rk_str);
1552: rcs_freedelta(rdp);
1553: return (-1);
1554: }
1555: }
1556:
1557: if (ntok != rk->rk_val) {
1558: cvs_log(LP_ERR,
1559: "invalid value type for RCS key `%s'",
1560: rk->rk_str);
1561: rcs_freedelta(rdp);
1562: return (-1);
1563: }
1564:
1565: if (tokstr != NULL)
1.41 ! jfb 1566: cvs_strfree(tokstr);
1.39 joris 1567: tokstr = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1568: if (tokstr == NULL) {
1.15 tedu 1569: cvs_log(LP_ERRNO,
1.10 joris 1570: "failed to duplicate rcs token");
1571: rcs_freedelta(rdp);
1572: return (-1);
1573: }
1.1 jfb 1574:
1575: /* now get the expected semi-colon */
1576: ntok = rcs_gettok(rfp);
1577: if (ntok != RCS_TOK_SCOLON) {
1578: cvs_log(LP_ERR,
1579: "missing semi-colon after RCS `%s' key",
1.26 jfb 1580: rk->rk_str);
1.41 ! jfb 1581: cvs_strfree(tokstr);
1.1 jfb 1582: rcs_freedelta(rdp);
1583: return (-1);
1584: }
1585:
1586: if (tok == RCS_TOK_DATE) {
1.25 jfb 1587: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.41 ! jfb 1588: cvs_strfree(tokstr);
1.11 joris 1589: rcs_freedelta(rdp);
1590: return (-1);
1591: }
1.3 vincent 1592: if (datenum->rn_len != 6) {
1.1 jfb 1593: cvs_log(LP_ERR,
1594: "RCS date specification has %s "
1595: "fields",
1.3 vincent 1596: (datenum->rn_len > 6) ? "too many" :
1.1 jfb 1597: "missing");
1.41 ! jfb 1598: cvs_strfree(tokstr);
1.1 jfb 1599: rcs_freedelta(rdp);
1.37 tedu 1600: rcsnum_free(datenum);
1601: return (-1);
1.1 jfb 1602: }
1.3 vincent 1603: rdp->rd_date.tm_year = datenum->rn_id[0];
1.19 jfb 1604: if (rdp->rd_date.tm_year >= 1900)
1605: rdp->rd_date.tm_year -= 1900;
1.3 vincent 1606: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
1607: rdp->rd_date.tm_mday = datenum->rn_id[2];
1608: rdp->rd_date.tm_hour = datenum->rn_id[3];
1609: rdp->rd_date.tm_min = datenum->rn_id[4];
1610: rdp->rd_date.tm_sec = datenum->rn_id[5];
1611: rcsnum_free(datenum);
1.14 deraadt 1612: } else if (tok == RCS_TOK_AUTHOR) {
1.1 jfb 1613: rdp->rd_author = tokstr;
1614: tokstr = NULL;
1.14 deraadt 1615: } else if (tok == RCS_TOK_STATE) {
1.1 jfb 1616: rdp->rd_state = tokstr;
1617: tokstr = NULL;
1.14 deraadt 1618: } else if (tok == RCS_TOK_NEXT) {
1.1 jfb 1619: rcsnum_aton(tokstr, NULL, rdp->rd_next);
1620: }
1621: break;
1622: case RCS_TOK_BRANCHES:
1623: rcs_parse_branches(rfp, rdp);
1624: break;
1625: default:
1626: cvs_log(LP_ERR,
1627: "unexpected token `%s' in RCS delta",
1628: RCS_TOKSTR(rfp));
1629: rcs_freedelta(rdp);
1630: return (-1);
1631: }
1632: }
1633:
1.13 jfb 1634: if (tokstr != NULL)
1.39 joris 1635: cvs_strfree(tokstr);
1.13 jfb 1636:
1.1 jfb 1637: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26 jfb 1638: rfp->rf_ndelta++;
1.1 jfb 1639:
1640: return (ret);
1641: }
1642:
1643: /*
1644: * rcs_parse_deltatext()
1645: *
1646: * Parse an RCS delta text section and fill in the log and text field of the
1647: * appropriate delta section.
1648: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1649: * -1 on error.
1650: */
1651: static int
1652: rcs_parse_deltatext(RCSFILE *rfp)
1653: {
1654: int tok;
1655: RCSNUM *tnum;
1656: struct rcs_delta *rdp;
1657:
1658: tok = rcs_gettok(rfp);
1659: if (tok == RCS_TOK_EOF)
1660: return (0);
1661:
1662: if (tok != RCS_TOK_NUM) {
1663: cvs_log(LP_ERR,
1664: "unexpected token `%s' at start of RCS delta text",
1665: RCS_TOKSTR(rfp));
1666: return (-1);
1667: }
1.13 jfb 1668:
1669: tnum = rcsnum_alloc();
1670: if (tnum == NULL)
1671: return (-1);
1.1 jfb 1672: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
1673:
1674: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1675: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
1676: break;
1677: }
1.13 jfb 1678: rcsnum_free(tnum);
1679:
1.1 jfb 1680: if (rdp == NULL) {
1681: cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
1682: RCS_TOKSTR(rfp));
1683: return (-1);
1684: }
1685:
1686: tok = rcs_gettok(rfp);
1687: if (tok != RCS_TOK_LOG) {
1688: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1689: RCS_TOKSTR(rfp));
1690: return (-1);
1691: }
1692:
1693: tok = rcs_gettok(rfp);
1694: if (tok != RCS_TOK_STRING) {
1695: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1696: RCS_TOKSTR(rfp));
1697: return (-1);
1698: }
1.39 joris 1699: rdp->rd_log = cvs_strdup(RCS_TOKSTR(rfp));
1.1 jfb 1700: if (rdp->rd_log == NULL) {
1701: cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
1702: return (-1);
1703: }
1704:
1705: tok = rcs_gettok(rfp);
1706: if (tok != RCS_TOK_TEXT) {
1707: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
1708: RCS_TOKSTR(rfp));
1709: return (-1);
1710: }
1711:
1712: tok = rcs_gettok(rfp);
1713: if (tok != RCS_TOK_STRING) {
1714: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
1715: RCS_TOKSTR(rfp));
1716: return (-1);
1717: }
1718:
1.39 joris 1719: rdp->rd_text = cvs_strdup(RCS_TOKSTR(rfp));
1.1 jfb 1720: if (rdp->rd_text == NULL) {
1721: cvs_log(LP_ERRNO, "failed to copy RCS delta text");
1722: return (-1);
1723: }
1724:
1725: return (1);
1726: }
1727:
1728: /*
1729: * rcs_parse_access()
1730: *
1731: * Parse the access list given as value to the `access' keyword.
1732: * Returns 0 on success, or -1 on failure.
1733: */
1734: static int
1735: rcs_parse_access(RCSFILE *rfp)
1736: {
1737: int type;
1738:
1739: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
1740: if (type != RCS_TOK_ID) {
1741: cvs_log(LP_ERR, "unexpected token `%s' in access list",
1742: RCS_TOKSTR(rfp));
1743: return (-1);
1744: }
1.29 jfb 1745:
1746: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
1747: return (-1);
1.1 jfb 1748: }
1749:
1750: return (0);
1751: }
1752:
1753: /*
1754: * rcs_parse_symbols()
1755: *
1756: * Parse the symbol list given as value to the `symbols' keyword.
1757: * Returns 0 on success, or -1 on failure.
1758: */
1759: static int
1760: rcs_parse_symbols(RCSFILE *rfp)
1761: {
1762: int type;
1763: struct rcs_sym *symp;
1764:
1765: for (;;) {
1766: type = rcs_gettok(rfp);
1767: if (type == RCS_TOK_SCOLON)
1768: break;
1769:
1.41 ! jfb 1770: if (type != RCS_TOK_ID) {
1.1 jfb 1771: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1772: RCS_TOKSTR(rfp));
1773: return (-1);
1774: }
1775:
1776: symp = (struct rcs_sym *)malloc(sizeof(*symp));
1777: if (symp == NULL) {
1778: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
1779: return (-1);
1780: }
1.39 joris 1781: symp->rs_name = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1782: if (symp->rs_name == NULL) {
1783: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
1784: free(symp);
1785: return (-1);
1786: }
1787:
1.1 jfb 1788: symp->rs_num = rcsnum_alloc();
1.11 joris 1789: if (symp->rs_num == NULL) {
1790: cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
1.39 joris 1791: cvs_strfree(symp->rs_name);
1.11 joris 1792: free(symp);
1793: return (-1);
1794: }
1.1 jfb 1795:
1796: type = rcs_gettok(rfp);
1797: if (type != RCS_TOK_COLON) {
1798: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1799: RCS_TOKSTR(rfp));
1.11 joris 1800: rcsnum_free(symp->rs_num);
1.39 joris 1801: cvs_strfree(symp->rs_name);
1.1 jfb 1802: free(symp);
1803: return (-1);
1804: }
1805:
1806: type = rcs_gettok(rfp);
1807: if (type != RCS_TOK_NUM) {
1808: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1809: RCS_TOKSTR(rfp));
1.11 joris 1810: rcsnum_free(symp->rs_num);
1.39 joris 1811: cvs_strfree(symp->rs_name);
1.1 jfb 1812: free(symp);
1813: return (-1);
1814: }
1815:
1816: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
1817: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
1818: RCS_TOKSTR(rfp));
1.11 joris 1819: rcsnum_free(symp->rs_num);
1.39 joris 1820: cvs_strfree(symp->rs_name);
1.1 jfb 1821: free(symp);
1822: return (-1);
1823: }
1824:
1825: TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
1826: }
1827:
1828: return (0);
1829: }
1830:
1831: /*
1832: * rcs_parse_locks()
1833: *
1834: * Parse the lock list given as value to the `locks' keyword.
1835: * Returns 0 on success, or -1 on failure.
1836: */
1837: static int
1838: rcs_parse_locks(RCSFILE *rfp)
1839: {
1840: int type;
1841: struct rcs_lock *lkp;
1842:
1843: for (;;) {
1844: type = rcs_gettok(rfp);
1845: if (type == RCS_TOK_SCOLON)
1846: break;
1847:
1848: if (type != RCS_TOK_ID) {
1849: cvs_log(LP_ERR, "unexpected token `%s' in lock list",
1850: RCS_TOKSTR(rfp));
1851: return (-1);
1852: }
1853:
1854: lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
1855: if (lkp == NULL) {
1856: cvs_log(LP_ERRNO, "failed to allocate RCS lock");
1857: return (-1);
1858: }
1859: lkp->rl_num = rcsnum_alloc();
1.11 joris 1860: if (lkp->rl_num == NULL) {
1861: free(lkp);
1862: return (-1);
1863: }
1.1 jfb 1864:
1865: type = rcs_gettok(rfp);
1866: if (type != RCS_TOK_COLON) {
1867: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1868: RCS_TOKSTR(rfp));
1.37 tedu 1869: rcsnum_free(lkp->rl_num);
1.1 jfb 1870: free(lkp);
1871: return (-1);
1872: }
1873:
1874: type = rcs_gettok(rfp);
1875: if (type != RCS_TOK_NUM) {
1876: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
1877: RCS_TOKSTR(rfp));
1.37 tedu 1878: rcsnum_free(lkp->rl_num);
1.1 jfb 1879: free(lkp);
1880: return (-1);
1881: }
1882:
1883: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
1884: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
1885: RCS_TOKSTR(rfp));
1.37 tedu 1886: rcsnum_free(lkp->rl_num);
1.1 jfb 1887: free(lkp);
1888: return (-1);
1889: }
1890:
1891: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
1892: }
1893:
1894: /* check if we have a `strict' */
1895: type = rcs_gettok(rfp);
1896: if (type != RCS_TOK_STRICT) {
1897: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14 deraadt 1898: } else {
1.26 jfb 1899: rfp->rf_flags |= RCS_SLOCK;
1.1 jfb 1900:
1901: type = rcs_gettok(rfp);
1902: if (type != RCS_TOK_SCOLON) {
1903: cvs_log(LP_ERR,
1904: "missing semi-colon after `strict' keyword");
1905: return (-1);
1906: }
1907: }
1908:
1909: return (0);
1910: }
1911:
1912: /*
1913: * rcs_parse_branches()
1914: *
1915: * Parse the list of branches following a `branches' keyword in a delta.
1916: * Returns 0 on success, or -1 on failure.
1917: */
1918: static int
1919: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
1920: {
1921: int type;
1922: struct rcs_branch *brp;
1923:
1924: for (;;) {
1925: type = rcs_gettok(rfp);
1926: if (type == RCS_TOK_SCOLON)
1927: break;
1928:
1929: if (type != RCS_TOK_NUM) {
1930: cvs_log(LP_ERR,
1931: "unexpected token `%s' in list of branches",
1932: RCS_TOKSTR(rfp));
1933: return (-1);
1934: }
1935:
1936: brp = (struct rcs_branch *)malloc(sizeof(*brp));
1937: if (brp == NULL) {
1938: cvs_log(LP_ERRNO, "failed to allocate RCS branch");
1939: return (-1);
1940: }
1941: brp->rb_num = rcsnum_alloc();
1.11 joris 1942: if (brp->rb_num == NULL) {
1943: free(brp);
1944: return (-1);
1945: }
1946:
1.1 jfb 1947: rcsnum_aton(RCS_TOKSTR(rfp), NULL, brp->rb_num);
1948:
1949: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
1950: }
1951:
1952: return (0);
1953: }
1954:
1955: /*
1956: * rcs_freedelta()
1957: *
1958: * Free the contents of a delta structure.
1959: */
1.18 jfb 1960: static void
1.1 jfb 1961: rcs_freedelta(struct rcs_delta *rdp)
1962: {
1.12 jfb 1963: struct rcs_branch *rb;
1.1 jfb 1964: struct rcs_delta *crdp;
1965:
1.12 jfb 1966: if (rdp->rd_num != NULL)
1967: rcsnum_free(rdp->rd_num);
1968: if (rdp->rd_next != NULL)
1969: rcsnum_free(rdp->rd_next);
1970:
1.1 jfb 1971: if (rdp->rd_author != NULL)
1.39 joris 1972: cvs_strfree(rdp->rd_author);
1.1 jfb 1973: if (rdp->rd_state != NULL)
1.39 joris 1974: cvs_strfree(rdp->rd_state);
1.1 jfb 1975: if (rdp->rd_log != NULL)
1.39 joris 1976: cvs_strfree(rdp->rd_log);
1.1 jfb 1977: if (rdp->rd_text != NULL)
1.39 joris 1978: cvs_strfree(rdp->rd_text);
1.12 jfb 1979:
1980: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
1981: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
1982: rcsnum_free(rb->rb_num);
1983: free(rb);
1984: }
1.1 jfb 1985:
1986: while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
1987: TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
1988: rcs_freedelta(crdp);
1989: }
1990:
1991: free(rdp);
1992: }
1993:
1994: /*
1995: * rcs_freepdata()
1996: *
1997: * Free the contents of the parser data structure.
1998: */
1999: static void
2000: rcs_freepdata(struct rcs_pdata *pd)
2001: {
2002: if (pd->rp_file != NULL)
2003: (void)fclose(pd->rp_file);
2004: if (pd->rp_buf != NULL)
2005: free(pd->rp_buf);
2006: free(pd);
2007: }
2008:
2009: /*
2010: * rcs_gettok()
2011: *
2012: * Get the next RCS token from the string <str>.
2013: */
2014: static int
2015: rcs_gettok(RCSFILE *rfp)
2016: {
2017: u_int i;
2018: int ch, last, type;
1.18 jfb 2019: size_t len;
2020: char *bp;
1.1 jfb 2021: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2022:
2023: type = RCS_TOK_ERR;
2024: bp = pdp->rp_buf;
2025: *bp = '\0';
2026:
2027: if (pdp->rp_pttype != RCS_TOK_ERR) {
2028: type = pdp->rp_pttype;
2029: strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
2030: pdp->rp_pttype = RCS_TOK_ERR;
2031: return (type);
2032: }
2033:
2034: /* skip leading whitespace */
2035: /* XXX we must skip backspace too for compatibility, should we? */
2036: do {
2037: ch = getc(pdp->rp_file);
2038: if (ch == '\n')
1.18 jfb 2039: pdp->rp_lines++;
1.1 jfb 2040: } while (isspace(ch));
2041:
2042: if (ch == EOF) {
2043: type = RCS_TOK_EOF;
1.14 deraadt 2044: } else if (ch == ';') {
1.1 jfb 2045: type = RCS_TOK_SCOLON;
1.14 deraadt 2046: } else if (ch == ':') {
1.1 jfb 2047: type = RCS_TOK_COLON;
1.14 deraadt 2048: } else if (isalpha(ch)) {
1.31 jfb 2049: type = RCS_TOK_ID;
1.1 jfb 2050: *(bp++) = ch;
1.18 jfb 2051: for (;;) {
1.1 jfb 2052: ch = getc(pdp->rp_file);
1.11 joris 2053: if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1 jfb 2054: ungetc(ch, pdp->rp_file);
2055: break;
2056: }
2057: *(bp++) = ch;
1.18 jfb 2058: if (bp == pdp->rp_bufend - 1) {
2059: len = bp - pdp->rp_buf;
2060: if (rcs_growbuf(rfp) < 0) {
2061: type = RCS_TOK_ERR;
2062: break;
2063: }
2064: bp = pdp->rp_buf + len;
2065: }
1.1 jfb 2066: }
2067: *bp = '\0';
2068:
1.18 jfb 2069: if (type != RCS_TOK_ERR) {
2070: for (i = 0; i < RCS_NKEYS; i++) {
2071: if (strcmp(rcs_keys[i].rk_str,
2072: pdp->rp_buf) == 0) {
2073: type = rcs_keys[i].rk_id;
2074: break;
2075: }
1.1 jfb 2076: }
2077: }
1.14 deraadt 2078: } else if (ch == '@') {
1.1 jfb 2079: /* we have a string */
1.18 jfb 2080: type = RCS_TOK_STRING;
1.1 jfb 2081: for (;;) {
2082: ch = getc(pdp->rp_file);
2083: if (ch == '@') {
2084: ch = getc(pdp->rp_file);
2085: if (ch != '@') {
2086: ungetc(ch, pdp->rp_file);
2087: break;
2088: }
1.14 deraadt 2089: } else if (ch == '\n')
1.18 jfb 2090: pdp->rp_lines++;
1.1 jfb 2091:
2092: *(bp++) = ch;
1.18 jfb 2093: if (bp == pdp->rp_bufend - 1) {
2094: len = bp - pdp->rp_buf;
2095: if (rcs_growbuf(rfp) < 0) {
2096: type = RCS_TOK_ERR;
2097: break;
2098: }
2099: bp = pdp->rp_buf + len;
2100: }
1.1 jfb 2101: }
2102:
2103: *bp = '\0';
1.14 deraadt 2104: } else if (isdigit(ch)) {
1.1 jfb 2105: *(bp++) = ch;
2106: last = ch;
2107: type = RCS_TOK_NUM;
2108:
2109: for (;;) {
2110: ch = getc(pdp->rp_file);
1.18 jfb 2111: if (bp == pdp->rp_bufend)
1.1 jfb 2112: break;
2113: if (!isdigit(ch) && ch != '.') {
2114: ungetc(ch, pdp->rp_file);
2115: break;
2116: }
2117:
2118: if (last == '.' && ch == '.') {
2119: type = RCS_TOK_ERR;
2120: break;
2121: }
2122: last = ch;
2123: *(bp++) = ch;
2124: }
1.18 jfb 2125: *bp = '\0';
1.1 jfb 2126: }
2127:
2128: return (type);
2129: }
2130:
2131: /*
2132: * rcs_pushtok()
2133: *
2134: * Push a token back in the parser's token buffer.
2135: */
2136: static int
2137: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2138: {
2139: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2140:
2141: if (pdp->rp_pttype != RCS_TOK_ERR)
2142: return (-1);
2143:
2144: pdp->rp_pttype = type;
2145: strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
2146: return (0);
2147: }
2148:
2149: /*
2150: * rcs_stresc()
2151: *
2152: * Performs either escaping or unescaping of the string stored in <str>.
2153: * The operation is to escape special RCS characters if the <esc> argument
2154: * is 1, or unescape otherwise. The result is stored in the <buf> destination
2155: * buffer, and <blen> must originally point to the size of <buf>.
2156: * Returns the number of bytes which have been read from the source <str> and
2157: * operated on. The <blen> parameter will contain the number of bytes
2158: * actually copied in <buf>.
2159: */
2160: size_t
2161: rcs_stresc(int esc, const char *str, char *buf, size_t *blen)
2162: {
2163: size_t rlen;
2164: const char *sp;
2165: char *bp, *bep;
2166:
2167: rlen = 0;
2168: bp = buf;
2169: bep = buf + *blen - 1;
2170:
2171: for (sp = str; (*sp != '\0') && (bp <= (bep - 1)); sp++) {
2172: if (*sp == '@') {
2173: if (esc) {
2174: if (bp > (bep - 2))
2175: break;
2176: *(bp++) = '@';
1.14 deraadt 2177: } else {
1.1 jfb 2178: sp++;
2179: if (*sp != '@') {
2180: cvs_log(LP_WARN,
2181: "unknown escape character `%c' in "
2182: "RCS file", *sp);
2183: if (*sp == '\0')
2184: break;
2185: }
2186: }
2187: }
2188:
2189: *(bp++) = *sp;
2190: }
2191:
2192: *bp = '\0';
2193: *blen = (bp - buf);
2194: return (sp - str);
2195: }
2196:
2197: /*
2198: * rcs_splitlines()
2199: *
2200: * Split the contents of a file into a list of lines.
2201: */
2202: static struct rcs_foo*
2203: rcs_splitlines(const char *fcont)
2204: {
2205: char *dcp;
2206: struct rcs_foo *foo;
2207: struct rcs_line *lp;
2208:
2209: foo = (struct rcs_foo *)malloc(sizeof(*foo));
2210: if (foo == NULL) {
2211: cvs_log(LP_ERR, "failed to allocate line structure");
2212: return (NULL);
2213: }
2214: TAILQ_INIT(&(foo->rl_lines));
2215: foo->rl_nblines = 0;
2216: foo->rl_data = strdup(fcont);
2217: if (foo->rl_data == NULL) {
2218: cvs_log(LP_ERRNO, "failed to copy file contents");
2219: free(foo);
2220: return (NULL);
2221: }
2222:
2223: /*
2224: * Add a first bogus line with line number 0. This is used so we
2225: * can position the line pointer before 1 when changing the first line
2226: * in rcs_patch().
2227: */
2228: lp = (struct rcs_line *)malloc(sizeof(*lp));
1.38 joris 2229: if (lp == NULL) {
2230: rcs_freefoo(foo);
1.1 jfb 2231: return (NULL);
1.38 joris 2232: }
1.5 vincent 2233:
1.1 jfb 2234: lp->rl_line = NULL;
2235: lp->rl_lineno = 0;
2236: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2237:
2238:
2239: for (dcp = foo->rl_data; *dcp != '\0';) {
2240: lp = (struct rcs_line *)malloc(sizeof(*lp));
2241: if (lp == NULL) {
1.38 joris 2242: rcs_freefoo(foo);
1.1 jfb 2243: cvs_log(LP_ERR, "failed to allocate line entry");
2244: return (NULL);
2245: }
2246:
2247: lp->rl_line = dcp;
2248: lp->rl_lineno = ++(foo->rl_nblines);
2249: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2250:
2251: dcp = strchr(dcp, '\n');
2252: if (dcp == NULL) {
2253: break;
2254: }
2255: *(dcp++) = '\0';
2256: }
2257:
2258: return (foo);
1.5 vincent 2259: }
2260:
2261: static void
2262: rcs_freefoo(struct rcs_foo *fp)
2263: {
2264: struct rcs_line *lp;
2265:
2266: while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
2267: TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
2268: free(lp);
2269: }
2270: free(fp->rl_data);
2271: free(fp);
1.18 jfb 2272: }
2273:
2274: /*
2275: * rcs_growbuf()
2276: *
2277: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2278: * RCS_BUFEXTSIZE.
2279: * In case of failure, the original buffer is left unmodified.
2280: * Returns 0 on success, or -1 on failure.
2281: */
2282: static int
2283: rcs_growbuf(RCSFILE *rf)
2284: {
2285: void *tmp;
2286: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2287:
2288: tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
2289: if (tmp == NULL) {
2290: cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
2291: return (-1);
2292: }
2293:
2294: pdp->rp_buf = (char *)tmp;
2295: pdp->rp_blen += RCS_BUFEXTSIZE;
2296: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
2297:
2298: return (0);
1.1 jfb 2299: }