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