Annotation of src/usr.bin/cvs/rcs.c, Revision 1.68
1.68 ! moritz 1: /* $OpenBSD: rcs.c,v 1.67 2005/09/29 15:39:41 niallo 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/stat.h>
29:
1.55 xsa 30: #include <ctype.h>
31: #include <errno.h>
1.63 joris 32: #include <libgen.h>
1.52 jfb 33: #include <pwd.h>
1.55 xsa 34: #include <stdarg.h>
1.1 jfb 35: #include <stdio.h>
36: #include <stdlib.h>
37: #include <string.h>
1.55 xsa 38: #include <unistd.h>
1.1 jfb 39:
1.55 xsa 40: #include "log.h"
1.1 jfb 41: #include "rcs.h"
1.39 joris 42: #include "strtab.h"
1.1 jfb 43:
1.57 xsa 44: #define RCS_BUFSIZE 16384
45: #define RCS_BUFEXTSIZE 8192
1.1 jfb 46:
47:
48: /* RCS token types */
1.57 xsa 49: #define RCS_TOK_ERR -1
50: #define RCS_TOK_EOF 0
51: #define RCS_TOK_NUM 1
52: #define RCS_TOK_ID 2
53: #define RCS_TOK_STRING 3
54: #define RCS_TOK_SCOLON 4
55: #define RCS_TOK_COLON 5
56:
57:
58: #define RCS_TOK_HEAD 8
59: #define RCS_TOK_BRANCH 9
60: #define RCS_TOK_ACCESS 10
61: #define RCS_TOK_SYMBOLS 11
62: #define RCS_TOK_LOCKS 12
63: #define RCS_TOK_COMMENT 13
64: #define RCS_TOK_EXPAND 14
65: #define RCS_TOK_DATE 15
66: #define RCS_TOK_AUTHOR 16
67: #define RCS_TOK_STATE 17
68: #define RCS_TOK_NEXT 18
69: #define RCS_TOK_BRANCHES 19
70: #define RCS_TOK_DESC 20
71: #define RCS_TOK_LOG 21
72: #define RCS_TOK_TEXT 22
73: #define RCS_TOK_STRICT 23
1.1 jfb 74:
1.57 xsa 75: #define RCS_ISKEY(t) (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
1.1 jfb 76:
77:
1.57 xsa 78: #define RCS_NOSCOL 0x01 /* no terminating semi-colon */
79: #define RCS_VOPT 0x02 /* value is optional */
1.1 jfb 80:
81:
82: /* opaque parse data */
83: struct rcs_pdata {
1.57 xsa 84: u_int rp_lines;
1.1 jfb 85:
1.57 xsa 86: char *rp_buf;
87: size_t rp_blen;
88: char *rp_bufend;
89: size_t rp_tlen;
1.1 jfb 90:
91: /* pushback token buffer */
1.57 xsa 92: char rp_ptok[128];
93: int rp_pttype; /* token type, RCS_TOK_ERR if no token */
1.1 jfb 94:
1.57 xsa 95: FILE *rp_file;
1.1 jfb 96: };
97:
98:
99: struct rcs_line {
1.57 xsa 100: char *rl_line;
101: int rl_lineno;
102: TAILQ_ENTRY(rcs_line) rl_list;
1.1 jfb 103: };
1.5 vincent 104: TAILQ_HEAD(rcs_tqh, rcs_line);
1.1 jfb 105:
106: struct rcs_foo {
1.57 xsa 107: int rl_nblines;
108: char *rl_data;
109: struct rcs_tqh rl_lines;
1.1 jfb 110: };
111:
1.57 xsa 112: #define RCS_TOKSTR(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
113: #define RCS_TOKLEN(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_tlen
1.1 jfb 114:
115:
1.47 jfb 116:
117: /* invalid characters in RCS symbol names */
1.49 jfb 118: static const char rcs_sym_invch[] = RCS_SYM_INVALCHAR;
1.47 jfb 119:
1.51 jfb 120:
121: /* comment leaders, depending on the file's suffix */
122: static const struct rcs_comment {
1.57 xsa 123: const char *rc_suffix;
124: const char *rc_cstr;
1.51 jfb 125: } rcs_comments[] = {
126: { "1", ".\\\" " },
127: { "2", ".\\\" " },
128: { "3", ".\\\" " },
129: { "4", ".\\\" " },
130: { "5", ".\\\" " },
131: { "6", ".\\\" " },
132: { "7", ".\\\" " },
133: { "8", ".\\\" " },
134: { "9", ".\\\" " },
135: { "a", "-- " }, /* Ada */
136: { "ada", "-- " },
137: { "adb", "-- " },
138: { "asm", ";; " }, /* assembler (MS-DOS) */
139: { "ads", "-- " }, /* Ada */
140: { "bat", ":: " }, /* batch (MS-DOS) */
141: { "body", "-- " }, /* Ada */
142: { "c", " * " }, /* C */
143: { "c++", "// " }, /* C++ */
144: { "cc", "// " },
145: { "cpp", "// " },
146: { "cxx", "// " },
147: { "m", "// " }, /* Objective-C */
148: { "cl", ";;; " }, /* Common Lisp */
149: { "cmd", ":: " }, /* command (OS/2) */
150: { "cmf", "c " }, /* CM Fortran */
151: { "csh", "# " }, /* shell */
152: { "e", "# " }, /* efl */
153: { "epsf", "% " }, /* encapsulated postscript */
154: { "epsi", "% " }, /* encapsulated postscript */
155: { "el", "; " }, /* Emacs Lisp */
156: { "f", "c " }, /* Fortran */
157: { "for", "c " },
158: { "h", " * " }, /* C-header */
159: { "hh", "// " }, /* C++ header */
160: { "hpp", "// " },
161: { "hxx", "// " },
162: { "in", "# " }, /* for Makefile.in */
163: { "l", " * " }, /* lex */
164: { "mac", ";; " }, /* macro (DEC-10, MS-DOS, PDP-11, VMS, etc) */
165: { "mak", "# " }, /* makefile, e.g. Visual C++ */
166: { "me", ".\\\" " }, /* me-macros t/nroff */
167: { "ml", "; " }, /* mocklisp */
168: { "mm", ".\\\" " }, /* mm-macros t/nroff */
169: { "ms", ".\\\" " }, /* ms-macros t/nroff */
170: { "man", ".\\\" " }, /* man-macros t/nroff */
171: { "p", " * " }, /* pascal */
172: { "pas", " * " },
1.52 jfb 173: { "pl", "# " }, /* Perl (conflict with Prolog) */
174: { "pm", "# " }, /* Perl module */
1.51 jfb 175: { "ps", "% " }, /* postscript */
176: { "psw", "% " }, /* postscript wrap */
177: { "pswm", "% " }, /* postscript wrap */
178: { "r", "# " }, /* ratfor */
179: { "rc", " * " }, /* Microsoft Windows resource file */
180: { "red", "% " }, /* psl/rlisp */
181: { "sh", "# " }, /* shell */
182: { "sl", "% " }, /* psl */
183: { "spec", "-- " }, /* Ada */
184: { "tex", "% " }, /* tex */
185: { "y", " * " }, /* yacc */
186: { "ye", " * " }, /* yacc-efl */
187: { "yr", " * " }, /* yacc-ratfor */
188: };
189:
1.57 xsa 190: #define NB_COMTYPES (sizeof(rcs_comments)/sizeof(rcs_comments[0]))
1.51 jfb 191:
1.33 jfb 192: #ifdef notyet
1.20 jfb 193: static struct rcs_kfl {
1.57 xsa 194: char rk_char;
195: int rk_val;
1.20 jfb 196: } rcs_kflags[] = {
197: { 'k', RCS_KWEXP_NAME },
198: { 'v', RCS_KWEXP_VAL },
199: { 'l', RCS_KWEXP_LKR },
200: { 'o', RCS_KWEXP_OLD },
201: { 'b', RCS_KWEXP_NONE },
202: };
1.33 jfb 203: #endif
1.20 jfb 204:
1.1 jfb 205: static struct rcs_key {
1.57 xsa 206: char rk_str[16];
207: int rk_id;
208: int rk_val;
209: int rk_flags;
1.1 jfb 210: } rcs_keys[] = {
211: { "access", RCS_TOK_ACCESS, RCS_TOK_ID, RCS_VOPT },
1.41 jfb 212: { "author", RCS_TOK_AUTHOR, RCS_TOK_ID, 0 },
1.1 jfb 213: { "branch", RCS_TOK_BRANCH, RCS_TOK_NUM, RCS_VOPT },
214: { "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM, RCS_VOPT },
215: { "comment", RCS_TOK_COMMENT, RCS_TOK_STRING, RCS_VOPT },
216: { "date", RCS_TOK_DATE, RCS_TOK_NUM, 0 },
217: { "desc", RCS_TOK_DESC, RCS_TOK_STRING, RCS_NOSCOL },
218: { "expand", RCS_TOK_EXPAND, RCS_TOK_STRING, RCS_VOPT },
219: { "head", RCS_TOK_HEAD, RCS_TOK_NUM, RCS_VOPT },
220: { "locks", RCS_TOK_LOCKS, RCS_TOK_ID, 0 },
221: { "log", RCS_TOK_LOG, RCS_TOK_STRING, RCS_NOSCOL },
222: { "next", RCS_TOK_NEXT, RCS_TOK_NUM, RCS_VOPT },
1.41 jfb 223: { "state", RCS_TOK_STATE, RCS_TOK_ID, RCS_VOPT },
1.1 jfb 224: { "strict", RCS_TOK_STRICT, 0, 0, },
225: { "symbols", RCS_TOK_SYMBOLS, 0, 0 },
226: { "text", RCS_TOK_TEXT, RCS_TOK_STRING, RCS_NOSCOL },
227: };
228:
1.57 xsa 229: #define RCS_NKEYS (sizeof(rcs_keys)/sizeof(rcs_keys[0]))
1.1 jfb 230:
1.33 jfb 231: /*
232: * Keyword expansion table
233: */
1.63 joris 234: #define RCS_KW_AUTHOR 0x1000
235: #define RCS_KW_DATE 0x2000
236: #define RCS_KW_LOG 0x4000
237: #define RCS_KW_NAME 0x8000
238: #define RCS_KW_RCSFILE 0x0100
239: #define RCS_KW_REVISION 0x0200
240: #define RCS_KW_SOURCE 0x0400
241: #define RCS_KW_STATE 0x0800
242: #define RCS_KW_FULLPATH 0x0010
243:
244: #define RCS_KW_ID \
245: (RCS_KW_RCSFILE | RCS_KW_REVISION | RCS_KW_DATE \
246: | RCS_KW_AUTHOR | RCS_KW_STATE)
247:
248: #define RCS_KW_HEADER (RCS_KW_ID | RCS_KW_FULLPATH)
249:
1.33 jfb 250: static struct rcs_kw {
1.57 xsa 251: char kw_str[16];
1.63 joris 252: int kw_type;
1.33 jfb 253: } rcs_expkw[] = {
1.63 joris 254: { "Author", RCS_KW_AUTHOR },
255: { "Date", RCS_KW_DATE },
256: { "Header", RCS_KW_HEADER },
257: { "Id", RCS_KW_ID },
258: { "Log", RCS_KW_LOG },
259: { "Name", RCS_KW_NAME },
260: { "RCSfile", RCS_KW_RCSFILE },
261: { "Revision", RCS_KW_REVISION },
262: { "Source", RCS_KW_SOURCE },
263: { "State", RCS_KW_STATE },
1.33 jfb 264: };
1.63 joris 265:
266: #define RCS_NKWORDS (sizeof(rcs_expkw)/sizeof(rcs_expkw[0]))
1.33 jfb 267:
1.32 jfb 268: static const char *rcs_errstrs[] = {
269: "No error",
270: "No such entry",
271: "Duplicate entry found",
272: "Bad RCS number",
1.48 jfb 273: "Invalid RCS symbol",
274: "Parse error",
1.32 jfb 275: };
276:
277: #define RCS_NERR (sizeof(rcs_errstrs)/sizeof(rcs_errstrs[0]))
278:
279:
280: int rcs_errno = RCS_ERR_NOERR;
281:
1.1 jfb 282:
1.57 xsa 283: static int rcs_write(RCSFILE *);
284: static int rcs_parse(RCSFILE *);
285: static int rcs_parse_admin(RCSFILE *);
286: static int rcs_parse_delta(RCSFILE *);
287: static int rcs_parse_deltatext(RCSFILE *);
288:
289: static int rcs_parse_access(RCSFILE *);
290: static int rcs_parse_symbols(RCSFILE *);
291: static int rcs_parse_locks(RCSFILE *);
292: static int rcs_parse_branches(RCSFILE *, struct rcs_delta *);
293: static void rcs_freedelta(struct rcs_delta *);
294: static void rcs_freepdata(struct rcs_pdata *);
295: static int rcs_gettok(RCSFILE *);
296: static int rcs_pushtok(RCSFILE *, const char *, int);
297: static int rcs_growbuf(RCSFILE *);
298: static int rcs_patch_lines(struct rcs_foo *, struct rcs_foo *);
299: static int rcs_strprint(const u_char *, size_t, FILE *);
300:
1.63 joris 301: static int rcs_expand_keywords(char *, struct rcs_delta *, char *, char *,
302: size_t, int);
1.57 xsa 303: static struct rcs_delta *rcs_findrev(RCSFILE *, const RCSNUM *);
304: static struct rcs_foo *rcs_splitlines(const char *);
305: static void rcs_freefoo(struct rcs_foo *);
1.26 jfb 306:
1.1 jfb 307: /*
308: * rcs_open()
309: *
310: * Open a file containing RCS-formatted information. The file's path is
1.26 jfb 311: * given in <path>, and the opening flags are given in <flags>, which is either
312: * RCS_READ, RCS_WRITE, or RCS_RDWR. If the open requests write access and
313: * the file does not exist, the RCS_CREATE flag must also be given, in which
314: * case it will be created with the mode specified in a third argument of
315: * type mode_t. If the file exists and RCS_CREATE is passed, the open will
316: * fail.
1.1 jfb 317: * Returns a handle to the opened file on success, or NULL on failure.
318: */
1.60 xsa 319: RCSFILE *
1.26 jfb 320: rcs_open(const char *path, int flags, ...)
1.1 jfb 321: {
1.26 jfb 322: int ret;
323: mode_t fmode;
1.1 jfb 324: RCSFILE *rfp;
325: struct stat st;
1.26 jfb 326: va_list vap;
327:
328: fmode = 0;
329: flags &= 0xffff; /* ditch any internal flags */
1.1 jfb 330:
1.26 jfb 331: if (((ret = stat(path, &st)) == -1) && (errno == ENOENT)) {
332: if (flags & RCS_CREATE) {
333: va_start(vap, flags);
334: fmode = va_arg(vap, mode_t);
335: va_end(vap);
336: } else {
1.50 jfb 337: rcs_errno = RCS_ERR_ERRNO;
1.26 jfb 338: cvs_log(LP_ERR, "RCS file `%s' does not exist", path);
339: return (NULL);
340: }
341: } else if ((ret == 0) && (flags & RCS_CREATE)) {
342: cvs_log(LP_ERR, "RCS file `%s' exists", path);
1.1 jfb 343: return (NULL);
344: }
345:
1.26 jfb 346: if ((rfp = (RCSFILE *)malloc(sizeof(*rfp))) == NULL) {
1.1 jfb 347: cvs_log(LP_ERRNO, "failed to allocate RCS file structure");
1.50 jfb 348: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 349: return (NULL);
350: }
351: memset(rfp, 0, sizeof(*rfp));
352:
1.26 jfb 353: if ((rfp->rf_path = strdup(path)) == NULL) {
1.50 jfb 354: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 355: cvs_log(LP_ERRNO, "failed to duplicate RCS file path");
1.26 jfb 356: free(rfp);
1.1 jfb 357: return (NULL);
358: }
359:
360: rfp->rf_ref = 1;
1.26 jfb 361: rfp->rf_flags = flags | RCS_SLOCK;
362: rfp->rf_mode = fmode;
1.1 jfb 363:
364: TAILQ_INIT(&(rfp->rf_delta));
1.29 jfb 365: TAILQ_INIT(&(rfp->rf_access));
1.1 jfb 366: TAILQ_INIT(&(rfp->rf_symbols));
367: TAILQ_INIT(&(rfp->rf_locks));
368:
1.26 jfb 369: if (rfp->rf_flags & RCS_CREATE) {
370: } else if (rcs_parse(rfp) < 0) {
1.1 jfb 371: rcs_close(rfp);
372: return (NULL);
373: }
374:
375: return (rfp);
376: }
377:
378: /*
379: * rcs_close()
380: *
381: * Close an RCS file handle.
382: */
383: void
384: rcs_close(RCSFILE *rfp)
385: {
386: struct rcs_delta *rdp;
1.13 jfb 387: struct rcs_lock *rlp;
388: struct rcs_sym *rsp;
1.1 jfb 389:
390: if (rfp->rf_ref > 1) {
391: rfp->rf_ref--;
392: return;
393: }
394:
1.26 jfb 395: if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
396: rcs_write(rfp);
397:
1.1 jfb 398: while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
399: rdp = TAILQ_FIRST(&(rfp->rf_delta));
400: TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
401: rcs_freedelta(rdp);
402: }
403:
1.13 jfb 404: while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
405: rsp = TAILQ_FIRST(&(rfp->rf_symbols));
406: TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
407: rcsnum_free(rsp->rs_num);
1.39 joris 408: cvs_strfree(rsp->rs_name);
1.13 jfb 409: free(rsp);
410: }
411:
412: while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
413: rlp = TAILQ_FIRST(&(rfp->rf_locks));
414: TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
415: rcsnum_free(rlp->rl_num);
416: free(rlp);
417: }
418:
1.1 jfb 419: if (rfp->rf_head != NULL)
420: rcsnum_free(rfp->rf_head);
1.11 joris 421: if (rfp->rf_branch != NULL)
422: rcsnum_free(rfp->rf_branch);
1.1 jfb 423:
424: if (rfp->rf_path != NULL)
425: free(rfp->rf_path);
426: if (rfp->rf_comment != NULL)
1.39 joris 427: cvs_strfree(rfp->rf_comment);
1.1 jfb 428: if (rfp->rf_expand != NULL)
1.39 joris 429: cvs_strfree(rfp->rf_expand);
1.1 jfb 430: if (rfp->rf_desc != NULL)
1.39 joris 431: cvs_strfree(rfp->rf_desc);
1.1 jfb 432: free(rfp);
433: }
434:
435: /*
436: * rcs_write()
437: *
438: * Write the contents of the RCS file handle <rfp> to disk in the file whose
439: * path is in <rf_path>.
440: * Returns 0 on success, or -1 on failure.
441: */
1.27 jfb 442: static int
1.1 jfb 443: rcs_write(RCSFILE *rfp)
444: {
445: FILE *fp;
1.42 jfb 446: char buf[1024], numbuf[64];
1.29 jfb 447: struct rcs_access *ap;
1.1 jfb 448: struct rcs_sym *symp;
1.46 jfb 449: struct rcs_branch *brp;
1.1 jfb 450: struct rcs_delta *rdp;
451:
1.28 jfb 452: if (rfp->rf_flags & RCS_SYNCED)
1.1 jfb 453: return (0);
454:
1.29 jfb 455: if ((fp = fopen(rfp->rf_path, "w")) == NULL) {
1.50 jfb 456: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 457: cvs_log(LP_ERRNO, "failed to open RCS output file `%s'",
458: rfp->rf_path);
459: return (-1);
460: }
461:
1.28 jfb 462: if (rfp->rf_head != NULL)
463: rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
464: else
465: numbuf[0] = '\0';
466:
1.1 jfb 467: fprintf(fp, "head\t%s;\n", numbuf);
1.35 jfb 468:
469: if (rfp->rf_branch != NULL) {
470: rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
471: fprintf(fp, "branch\t%s;\n", numbuf);
472: }
473:
1.29 jfb 474: fputs("access", fp);
475: TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
476: fprintf(fp, "\n\t%s", ap->ra_name);
477: }
478: fputs(";\n", fp);
1.1 jfb 479:
480: fprintf(fp, "symbols\n");
481: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
482: rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
483: snprintf(buf, sizeof(buf), "%s:%s", symp->rs_name, numbuf);
484: fprintf(fp, "\t%s", buf);
485: if (symp != TAILQ_LAST(&(rfp->rf_symbols), rcs_slist))
486: fputc('\n', fp);
487: }
488: fprintf(fp, ";\n");
489:
490: fprintf(fp, "locks;");
491:
1.26 jfb 492: if (rfp->rf_flags & RCS_SLOCK)
1.1 jfb 493: fprintf(fp, " strict;");
494: fputc('\n', fp);
495:
1.42 jfb 496: if (rfp->rf_comment != NULL) {
497: fputs("comment\t@", fp);
1.58 xsa 498: rcs_strprint((const u_char *)rfp->rf_comment,
499: strlen(rfp->rf_comment), fp);
1.42 jfb 500: fputs("@;\n", fp);
501: }
1.1 jfb 502:
1.42 jfb 503: if (rfp->rf_expand != NULL) {
504: fputs("expand @", fp);
1.58 xsa 505: rcs_strprint((const u_char *)rfp->rf_expand,
506: strlen(rfp->rf_expand), fp);
1.42 jfb 507: fputs("@;\n", fp);
508: }
1.1 jfb 509:
1.46 jfb 510: fputs("\n\n", fp);
1.1 jfb 511:
512: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
513: fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
514: sizeof(numbuf)));
515: fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
1.44 jfb 516: rdp->rd_date.tm_year + 1900, rdp->rd_date.tm_mon + 1,
1.1 jfb 517: rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
518: rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
519: fprintf(fp, "\tauthor %s;\tstate %s;\n",
520: rdp->rd_author, rdp->rd_state);
1.46 jfb 521: fputs("branches", fp);
522: TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
523: fprintf(fp, " %s", rcsnum_tostr(brp->rb_num, numbuf,
524: sizeof(numbuf)));
525: }
526: fputs(";\n", fp);
1.1 jfb 527: fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
528: numbuf, sizeof(numbuf)));
529: }
530:
1.42 jfb 531: fputs("\ndesc\n@", fp);
532: if (rfp->rf_desc != NULL)
1.58 xsa 533: rcs_strprint((const u_char *)rfp->rf_desc,
534: strlen(rfp->rf_desc), fp);
1.42 jfb 535: fputs("@\n\n", fp);
1.1 jfb 536:
537: /* deltatexts */
538: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
539: fprintf(fp, "\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
540: sizeof(numbuf)));
1.42 jfb 541: fputs("log\n@", fp);
1.58 xsa 542: rcs_strprint((const u_char *)rdp->rd_log,
543: strlen(rdp->rd_log), fp);
1.42 jfb 544: fputs("@\ntext\n@", fp);
545: rcs_strprint(rdp->rd_text, rdp->rd_tlen, fp);
546: fputs("@\n\n", fp);
1.1 jfb 547: }
548: fclose(fp);
549:
1.26 jfb 550: rfp->rf_flags |= RCS_SYNCED;
1.1 jfb 551:
552: return (0);
553: }
554:
555: /*
1.43 jfb 556: * rcs_head_get()
557: *
558: * Retrieve the revision number of the head revision for the RCS file <file>.
559: */
1.60 xsa 560: const RCSNUM *
1.43 jfb 561: rcs_head_get(RCSFILE *file)
562: {
563: return (file->rf_head);
564: }
565:
566: /*
567: * rcs_head_set()
568: *
569: * Set the revision number of the head revision for the RCS file <file> to
570: * <rev>, which must reference a valid revision within the file.
571: */
572: int
573: rcs_head_set(RCSFILE *file, const RCSNUM *rev)
574: {
575: struct rcs_delta *rd;
576:
577: if ((rd = rcs_findrev(file, rev)) == NULL)
578: return (-1);
579:
1.52 jfb 580: if ((file->rf_head == NULL) &&
581: ((file->rf_head = rcsnum_alloc()) == NULL))
582: return (-1);
583:
1.43 jfb 584: if (rcsnum_cpy(rev, file->rf_head, 0) < 0)
585: return (-1);
586:
587: return (0);
588: }
589:
590:
591: /*
1.35 jfb 592: * rcs_branch_get()
593: *
594: * Retrieve the default branch number for the RCS file <file>.
595: * Returns the number on success. If NULL is returned, then there is no
596: * default branch for this file.
597: */
1.60 xsa 598: const RCSNUM *
1.35 jfb 599: rcs_branch_get(RCSFILE *file)
600: {
601: return (file->rf_branch);
602: }
603:
604: /*
605: * rcs_branch_set()
606: *
607: * Set the default branch for the RCS file <file> to <bnum>.
608: * Returns 0 on success, -1 on failure.
609: */
610: int
611: rcs_branch_set(RCSFILE *file, const RCSNUM *bnum)
612: {
613: if ((file->rf_branch == NULL) &&
614: ((file->rf_branch = rcsnum_alloc()) == NULL))
615: return (-1);
616:
617: if (rcsnum_cpy(bnum, file->rf_branch, 0) < 0) {
618: rcsnum_free(file->rf_branch);
619: file->rf_branch = NULL;
620: return (-1);
621: }
622:
623: return (0);
624: }
625:
626: /*
1.29 jfb 627: * rcs_access_add()
628: *
629: * Add the login name <login> to the access list for the RCS file <file>.
630: * Returns 0 on success, or -1 on failure.
631: */
632: int
633: rcs_access_add(RCSFILE *file, const char *login)
634: {
635: struct rcs_access *ap;
636:
637: /* first look for duplication */
638: TAILQ_FOREACH(ap, &(file->rf_access), ra_list) {
639: if (strcmp(ap->ra_name, login) == 0) {
1.32 jfb 640: rcs_errno = RCS_ERR_DUPENT;
1.29 jfb 641: return (-1);
642: }
643: }
644:
645: ap = (struct rcs_access *)malloc(sizeof(*ap));
646: if (ap == NULL) {
1.50 jfb 647: rcs_errno = RCS_ERR_ERRNO;
1.29 jfb 648: cvs_log(LP_ERRNO, "failed to allocate RCS access entry");
649: return (-1);
650: }
651:
1.39 joris 652: ap->ra_name = cvs_strdup(login);
1.29 jfb 653: if (ap->ra_name == NULL) {
654: cvs_log(LP_ERRNO, "failed to duplicate user name");
655: free(ap);
656: return (-1);
657: }
658:
659: TAILQ_INSERT_TAIL(&(file->rf_access), ap, ra_list);
660:
661: /* not synced anymore */
662: file->rf_flags &= ~RCS_SYNCED;
663: return (0);
664: }
665:
666: /*
667: * rcs_access_remove()
668: *
669: * Remove an entry with login name <login> from the access list of the RCS
670: * file <file>.
671: * Returns 0 on success, or -1 on failure.
672: */
673: int
674: rcs_access_remove(RCSFILE *file, const char *login)
675: {
676: struct rcs_access *ap;
677:
678: TAILQ_FOREACH(ap, &(file->rf_access), ra_list)
679: if (strcmp(ap->ra_name, login) == 0)
680: break;
681:
682: if (ap == NULL) {
1.32 jfb 683: rcs_errno = RCS_ERR_NOENT;
1.29 jfb 684: return (-1);
685: }
686:
687: TAILQ_REMOVE(&(file->rf_access), ap, ra_list);
1.39 joris 688: cvs_strfree(ap->ra_name);
1.29 jfb 689: free(ap);
690:
691: /* not synced anymore */
692: file->rf_flags &= ~RCS_SYNCED;
693: return (0);
694: }
695:
696: /*
1.26 jfb 697: * rcs_sym_add()
1.1 jfb 698: *
699: * Add a symbol to the list of symbols for the RCS file <rfp>. The new symbol
700: * is named <sym> and is bound to the RCS revision <snum>.
701: * Returns 0 on success, or -1 on failure.
702: */
703: int
1.26 jfb 704: rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
1.1 jfb 705: {
706: struct rcs_sym *symp;
707:
1.47 jfb 708: if (!rcs_sym_check(sym)) {
709: rcs_errno = RCS_ERR_BADSYM;
710: return (NULL);
711: }
712:
1.1 jfb 713: /* first look for duplication */
714: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
715: if (strcmp(symp->rs_name, sym) == 0) {
1.32 jfb 716: rcs_errno = RCS_ERR_DUPENT;
1.1 jfb 717: return (-1);
718: }
719: }
720:
1.50 jfb 721: if ((symp = (struct rcs_sym *)malloc(sizeof(*symp))) == NULL) {
722: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 723: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
724: return (-1);
725: }
726:
1.50 jfb 727: if ((symp->rs_name = cvs_strdup(sym)) == NULL) {
728: rcs_errno = RCS_ERR_ERRNO;
1.10 joris 729: cvs_log(LP_ERRNO, "failed to duplicate symbol");
730: free(symp);
731: return (-1);
732: }
733:
1.50 jfb 734: if ((symp->rs_num = rcsnum_alloc()) == NULL) {
1.39 joris 735: cvs_strfree(symp->rs_name);
1.11 joris 736: free(symp);
737: return (-1);
738: }
1.1 jfb 739: rcsnum_cpy(snum, symp->rs_num, 0);
740:
741: TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
742:
743: /* not synced anymore */
1.26 jfb 744: rfp->rf_flags &= ~RCS_SYNCED;
1.1 jfb 745: return (0);
746: }
747:
748: /*
1.27 jfb 749: * rcs_sym_remove()
750: *
751: * Remove the symbol with name <sym> from the symbol list for the RCS file
752: * <file>. If no such symbol is found, the call fails and returns with an
753: * error.
754: * Returns 0 on success, or -1 on failure.
755: */
756: int
757: rcs_sym_remove(RCSFILE *file, const char *sym)
758: {
759: struct rcs_sym *symp;
760:
1.47 jfb 761: if (!rcs_sym_check(sym)) {
762: rcs_errno = RCS_ERR_BADSYM;
763: return (NULL);
764: }
765:
1.27 jfb 766: TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
767: if (strcmp(symp->rs_name, sym) == 0)
768: break;
769:
770: if (symp == NULL) {
1.32 jfb 771: rcs_errno = RCS_ERR_NOENT;
1.27 jfb 772: return (-1);
773: }
774:
775: TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
1.39 joris 776: cvs_strfree(symp->rs_name);
1.27 jfb 777: rcsnum_free(symp->rs_num);
778: free(symp);
779:
780: /* not synced anymore */
781: file->rf_flags &= ~RCS_SYNCED;
782: return (0);
783: }
784:
785: /*
786: * rcs_sym_getrev()
787: *
788: * Retrieve the RCS revision number associated with the symbol <sym> for the
789: * RCS file <file>. The returned value is a dynamically-allocated copy and
790: * should be freed by the caller once they are done with it.
791: * Returns the RCSNUM on success, or NULL on failure.
792: */
1.60 xsa 793: RCSNUM *
1.27 jfb 794: rcs_sym_getrev(RCSFILE *file, const char *sym)
795: {
796: RCSNUM *num;
797: struct rcs_sym *symp;
798:
1.47 jfb 799: if (!rcs_sym_check(sym)) {
800: rcs_errno = RCS_ERR_BADSYM;
801: return (NULL);
802: }
803:
1.27 jfb 804: num = NULL;
805: TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
806: if (strcmp(symp->rs_name, sym) == 0)
807: break;
808:
1.36 jfb 809: if (symp == NULL)
810: rcs_errno = RCS_ERR_NOENT;
811: else if (((num = rcsnum_alloc()) != NULL) &&
1.27 jfb 812: (rcsnum_cpy(symp->rs_num, num, 0) < 0)) {
813: rcsnum_free(num);
814: num = NULL;
815: }
816:
817: return (num);
1.47 jfb 818: }
819:
820: /*
821: * rcs_sym_check()
822: *
823: * Check the RCS symbol name <sym> for any unsupported characters.
824: * Returns 1 if the tag is correct, 0 if it isn't valid.
825: */
826: int
827: rcs_sym_check(const char *sym)
828: {
829: int ret;
830: const char *cp;
831:
832: ret = 1;
833: cp = sym;
834: if (!isalpha(*cp++))
835: return (0);
836:
837: for (; *cp != '\0'; cp++)
838: if (!isgraph(*cp) || (strchr(rcs_sym_invch, *cp) != NULL)) {
839: ret = 0;
840: break;
841: }
842:
843: return (ret);
1.30 jfb 844: }
845:
846: /*
847: * rcs_lock_getmode()
848: *
849: * Retrieve the locking mode of the RCS file <file>.
850: */
851: int
852: rcs_lock_getmode(RCSFILE *file)
853: {
854: return (file->rf_flags & RCS_SLOCK) ? RCS_LOCK_STRICT : RCS_LOCK_LOOSE;
855: }
856:
857: /*
858: * rcs_lock_setmode()
859: *
860: * Set the locking mode of the RCS file <file> to <mode>, which must either
861: * be RCS_LOCK_LOOSE or RCS_LOCK_STRICT.
862: * Returns the previous mode on success, or -1 on failure.
863: */
864: int
865: rcs_lock_setmode(RCSFILE *file, int mode)
866: {
867: int pmode;
868: pmode = rcs_lock_getmode(file);
869:
870: if (mode == RCS_LOCK_STRICT)
871: file->rf_flags |= RCS_SLOCK;
872: else if (mode == RCS_LOCK_LOOSE)
873: file->rf_flags &= ~RCS_SLOCK;
874: else {
875: cvs_log(LP_ERRNO, "invalid lock mode %d", mode);
876: return (-1);
877: }
878:
879: return (pmode);
1.27 jfb 880: }
881:
882: /*
1.40 jfb 883: * rcs_lock_add()
884: *
885: * Add an RCS lock for the user <user> on revision <rev>.
886: * Returns 0 on success, or -1 on failure.
887: */
888: int
889: rcs_lock_add(RCSFILE *file, const char *user, RCSNUM *rev)
890: {
891: struct rcs_lock *lkp;
892:
893: /* first look for duplication */
894: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
895: if (strcmp(lkp->rl_name, user) == 0) {
896: rcs_errno = RCS_ERR_DUPENT;
897: return (-1);
898: }
899: }
900:
1.50 jfb 901: if ((lkp = (struct rcs_lock *)malloc(sizeof(*lkp))) == NULL) {
902: rcs_errno = RCS_ERR_ERRNO;
1.40 jfb 903: cvs_log(LP_ERRNO, "failed to allocate RCS lock");
904: return (-1);
905: }
906:
907: lkp->rl_name = cvs_strdup(user);
908: if (lkp->rl_name == NULL) {
909: cvs_log(LP_ERRNO, "failed to duplicate user name");
910: free(lkp);
911: return (-1);
912: }
1.68 ! moritz 913:
! 914: if ((lkp->rl_num = rcsnum_alloc()) == NULL) {
! 915: cvs_strfree(lkp->rl_name);
! 916: free(lkp);
! 917: return (-1);
! 918: }
! 919: rcsnum_cpy(rev, lkp->rl_num, 0);
1.40 jfb 920:
921: TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
922:
923: /* not synced anymore */
924: file->rf_flags &= ~RCS_SYNCED;
925: return (0);
926:
927:
928: }
929:
930:
931: /*
932: * rcs_lock_remove()
933: *
934: * Remove the RCS lock on revision <rev>.
935: * Returns 0 on success, or -1 on failure.
936: */
937: int
1.56 joris 938: rcs_lock_remove(RCSFILE *file, const RCSNUM *rev)
1.40 jfb 939: {
940: struct rcs_lock *lkp;
941:
942: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list)
943: if (rcsnum_cmp(lkp->rl_num, rev, 0) == 0)
944: break;
945:
946: if (lkp == NULL) {
947: rcs_errno = RCS_ERR_NOENT;
948: return (-1);
949: }
950:
951: TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
952: rcsnum_free(lkp->rl_num);
953: cvs_strfree(lkp->rl_name);
954: free(lkp);
955:
956: /* not synced anymore */
957: file->rf_flags &= ~RCS_SYNCED;
958: return (0);
959: }
960:
961: /*
1.27 jfb 962: * rcs_desc_get()
963: *
964: * Retrieve the description for the RCS file <file>.
965: */
1.57 xsa 966: const char *
1.27 jfb 967: rcs_desc_get(RCSFILE *file)
968: {
969: return (file->rf_desc);
970: }
971:
972: /*
973: * rcs_desc_set()
974: *
975: * Set the description for the RCS file <file>.
976: * Returns 0 on success, or -1 on failure.
977: */
978: int
979: rcs_desc_set(RCSFILE *file, const char *desc)
980: {
981: char *tmp;
982:
1.39 joris 983: if ((tmp = cvs_strdup(desc)) == NULL)
1.27 jfb 984: return (-1);
985:
986: if (file->rf_desc != NULL)
1.39 joris 987: cvs_strfree(file->rf_desc);
1.27 jfb 988: file->rf_desc = tmp;
989: file->rf_flags &= ~RCS_SYNCED;
990:
991: return (0);
1.51 jfb 992: }
993:
994: /*
995: * rcs_comment_lookup()
996: *
997: * Lookup the assumed comment leader based on a file's suffix.
998: * Returns a pointer to the string on success, or NULL on failure.
999: */
1.57 xsa 1000: const char *
1.51 jfb 1001: rcs_comment_lookup(const char *filename)
1002: {
1003: int i;
1004: const char *sp;
1005:
1006: if ((sp = strrchr(filename, '.')) == NULL) {
1007: rcs_errno = RCS_ERR_NOENT;
1008: return (NULL);
1009: }
1010: sp++;
1011:
1012: for (i = 0; i < (int)NB_COMTYPES; i++)
1013: if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
1014: return (rcs_comments[i].rc_cstr);
1015: return (NULL);
1.27 jfb 1016: }
1017:
1.33 jfb 1018: /*
1019: * rcs_comment_get()
1020: *
1021: * Retrieve the comment leader for the RCS file <file>.
1022: */
1.57 xsa 1023: const char *
1.33 jfb 1024: rcs_comment_get(RCSFILE *file)
1025: {
1026: return (file->rf_comment);
1027: }
1028:
1029: /*
1030: * rcs_comment_set()
1031: *
1032: * Set the comment leader for the RCS file <file>.
1033: * Returns 0 on success, or -1 on failure.
1034: */
1035: int
1036: rcs_comment_set(RCSFILE *file, const char *comment)
1037: {
1038: char *tmp;
1039:
1.39 joris 1040: if ((tmp = cvs_strdup(comment)) == NULL)
1.33 jfb 1041: return (-1);
1042:
1043: if (file->rf_comment != NULL)
1.39 joris 1044: cvs_strfree(file->rf_comment);
1.33 jfb 1045: file->rf_comment = tmp;
1046: file->rf_flags &= ~RCS_SYNCED;
1047:
1048: return (0);
1049: }
1.40 jfb 1050:
1051: /*
1052: * rcs_tag_resolve()
1053: *
1054: * Retrieve the revision number corresponding to the tag <tag> for the RCS
1055: * file <file>.
1056: */
1.60 xsa 1057: RCSNUM *
1.40 jfb 1058: rcs_tag_resolve(RCSFILE *file, const char *tag)
1059: {
1060: RCSNUM *num;
1061:
1062: if ((num = rcsnum_parse(tag)) == NULL) {
1063: num = rcs_sym_getrev(file, tag);
1064: }
1065:
1066: return (num);
1067: }
1068:
1.27 jfb 1069:
1070: /*
1.1 jfb 1071: * rcs_patch()
1072: *
1073: * Apply an RCS-format patch pointed to by <patch> to the file contents
1074: * found in <data>.
1075: * Returns 0 on success, or -1 on failure.
1076: */
1077: BUF*
1078: rcs_patch(const char *data, const char *patch)
1079: {
1.5 vincent 1080: struct rcs_foo *dlines, *plines;
1081: struct rcs_line *lp;
1.1 jfb 1082: size_t len;
1.5 vincent 1083: int lineno;
1.1 jfb 1084: BUF *res;
1085:
1086: len = strlen(data);
1087: res = cvs_buf_alloc(len, BUF_AUTOEXT);
1088: if (res == NULL)
1089: return (NULL);
1090:
1091: dlines = rcs_splitlines(data);
1.17 jfb 1092: if (dlines == NULL) {
1093: cvs_buf_free(res);
1.1 jfb 1094: return (NULL);
1.17 jfb 1095: }
1.5 vincent 1096:
1.1 jfb 1097: plines = rcs_splitlines(patch);
1.5 vincent 1098: if (plines == NULL) {
1.17 jfb 1099: cvs_buf_free(res);
1.5 vincent 1100: rcs_freefoo(dlines);
1.1 jfb 1101: return (NULL);
1.5 vincent 1102: }
1103:
1104: if (rcs_patch_lines(dlines, plines) < 0) {
1.17 jfb 1105: cvs_buf_free(res);
1.5 vincent 1106: rcs_freefoo(plines);
1107: rcs_freefoo(dlines);
1108: return (NULL);
1109: }
1110:
1111: lineno = 0;
1112: TAILQ_FOREACH(lp, &dlines->rl_lines, rl_list) {
1113: if (lineno != 0)
1114: cvs_buf_fappend(res, "%s\n", lp->rl_line);
1115: lineno++;
1116: }
1117:
1118: rcs_freefoo(dlines);
1119: rcs_freefoo(plines);
1120: return (res);
1121: }
1122:
1.7 jfb 1123: static int
1.5 vincent 1124: rcs_patch_lines(struct rcs_foo *dlines, struct rcs_foo *plines)
1125: {
1126: char op, *ep;
1127: struct rcs_line *lp, *dlp, *ndlp;
1128: int i, lineno, nbln;
1.1 jfb 1129:
1130: dlp = TAILQ_FIRST(&(dlines->rl_lines));
1131: lp = TAILQ_FIRST(&(plines->rl_lines));
1132:
1133: /* skip first bogus line */
1134: for (lp = TAILQ_NEXT(lp, rl_list); lp != NULL;
1135: lp = TAILQ_NEXT(lp, rl_list)) {
1136: op = *(lp->rl_line);
1137: lineno = (int)strtol((lp->rl_line + 1), &ep, 10);
1138: if ((lineno > dlines->rl_nblines) || (lineno <= 0) ||
1139: (*ep != ' ')) {
1140: cvs_log(LP_ERR,
1141: "invalid line specification in RCS patch");
1.61 joris 1142: return (-1);
1.1 jfb 1143: }
1144: ep++;
1145: nbln = (int)strtol(ep, &ep, 10);
1146: if ((nbln <= 0) || (*ep != '\0')) {
1147: cvs_log(LP_ERR,
1148: "invalid line number specification in RCS patch");
1.61 joris 1149: return (-1);
1.1 jfb 1150: }
1151:
1152: /* find the appropriate line */
1153: for (;;) {
1154: if (dlp == NULL)
1155: break;
1156: if (dlp->rl_lineno == lineno)
1157: break;
1158: if (dlp->rl_lineno > lineno) {
1159: dlp = TAILQ_PREV(dlp, rcs_tqh, rl_list);
1.14 deraadt 1160: } else if (dlp->rl_lineno < lineno) {
1.1 jfb 1161: ndlp = TAILQ_NEXT(dlp, rl_list);
1162: if (ndlp->rl_lineno > lineno)
1163: break;
1164: dlp = ndlp;
1165: }
1166: }
1167: if (dlp == NULL) {
1168: cvs_log(LP_ERR,
1169: "can't find referenced line in RCS patch");
1.61 joris 1170: return (-1);
1.1 jfb 1171: }
1172:
1173: if (op == 'd') {
1174: for (i = 0; (i < nbln) && (dlp != NULL); i++) {
1175: ndlp = TAILQ_NEXT(dlp, rl_list);
1176: TAILQ_REMOVE(&(dlines->rl_lines), dlp, rl_list);
1177: dlp = ndlp;
1178: }
1.14 deraadt 1179: } else if (op == 'a') {
1.1 jfb 1180: for (i = 0; i < nbln; i++) {
1181: ndlp = lp;
1182: lp = TAILQ_NEXT(lp, rl_list);
1183: if (lp == NULL) {
1184: cvs_log(LP_ERR, "truncated RCS patch");
1.5 vincent 1185: return (-1);
1.1 jfb 1186: }
1187: TAILQ_REMOVE(&(plines->rl_lines), lp, rl_list);
1188: TAILQ_INSERT_AFTER(&(dlines->rl_lines), dlp,
1189: lp, rl_list);
1190: dlp = lp;
1191:
1192: /* we don't want lookup to block on those */
1193: lp->rl_lineno = lineno;
1194:
1195: lp = ndlp;
1196: }
1.14 deraadt 1197: } else {
1.1 jfb 1198: cvs_log(LP_ERR, "unknown RCS patch operation `%c'", op);
1.5 vincent 1199: return (-1);
1.1 jfb 1200: }
1201:
1202: /* last line of the patch, done */
1203: if (lp->rl_lineno == plines->rl_nblines)
1204: break;
1205: }
1206:
1207: /* once we're done patching, rebuild the line numbers */
1.2 vincent 1208: lineno = 0;
1.5 vincent 1209: TAILQ_FOREACH(lp, &(dlines->rl_lines), rl_list)
1.1 jfb 1210: lp->rl_lineno = lineno++;
1211: dlines->rl_nblines = lineno - 1;
1212:
1.5 vincent 1213: return (0);
1.1 jfb 1214: }
1215:
1216: /*
1217: * rcs_getrev()
1218: *
1219: * Get the whole contents of revision <rev> from the RCSFILE <rfp>. The
1.4 vincent 1220: * returned buffer is dynamically allocated and should be released using
1221: * cvs_buf_free() once the caller is done using it.
1.1 jfb 1222: */
1223: BUF*
1.66 joris 1224: rcs_getrev(RCSFILE *rfp, RCSNUM *frev)
1.1 jfb 1225: {
1.63 joris 1226: int expmode, res;
1.1 jfb 1227: size_t len;
1228: void *bp;
1.66 joris 1229: RCSNUM *crev, *rev;
1.65 niallo 1230: BUF *rbuf, *dbuf = NULL;
1.1 jfb 1231: struct rcs_delta *rdp = NULL;
1.63 joris 1232: struct rcs_foo *lines;
1233: struct rcs_line *lp;
1234: char out[1024]; /* XXX */
1.1 jfb 1235:
1.28 jfb 1236: if (rfp->rf_head == NULL)
1237: return (NULL);
1.66 joris 1238:
1239: if (frev == RCS_HEAD_REV)
1240: rev = rfp->rf_head;
1241: else
1242: rev = frev;
1.28 jfb 1243:
1.1 jfb 1244: res = rcsnum_cmp(rfp->rf_head, rev, 0);
1245: if (res == 1) {
1.32 jfb 1246: rcs_errno = RCS_ERR_NOENT;
1.1 jfb 1247: return (NULL);
1.26 jfb 1248: }
1249:
1250: rdp = rcs_findrev(rfp, rfp->rf_head);
1251: if (rdp == NULL) {
1252: cvs_log(LP_ERR, "failed to get RCS HEAD revision");
1253: return (NULL);
1254: }
1255:
1.45 jfb 1256: len = rdp->rd_tlen;
1.26 jfb 1257: if ((rbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
1258: return (NULL);
1259:
1260: cvs_buf_append(rbuf, rdp->rd_text, len);
1261:
1262: if (res != 0) {
1263: /* Apply patches backwards to get the right version.
1264: * This will need some rework to support sub branches.
1265: */
1266: if ((crev = rcsnum_alloc()) == NULL) {
1267: cvs_buf_free(rbuf);
1.1 jfb 1268: return (NULL);
1269: }
1.26 jfb 1270: rcsnum_cpy(rfp->rf_head, crev, 0);
1271: do {
1272: crev->rn_id[crev->rn_len - 1]--;
1273: rdp = rcs_findrev(rfp, crev);
1274: if (rdp == NULL) {
1275: rcsnum_free(crev);
1276: cvs_buf_free(rbuf);
1277: return (NULL);
1278: }
1.1 jfb 1279:
1.26 jfb 1280: if (cvs_buf_putc(rbuf, '\0') < 0) {
1281: rcsnum_free(crev);
1.17 jfb 1282: cvs_buf_free(rbuf);
1.11 joris 1283: return (NULL);
1.17 jfb 1284: }
1.26 jfb 1285: bp = cvs_buf_release(rbuf);
1.45 jfb 1286: rbuf = rcs_patch((char *)bp, (char *)rdp->rd_text);
1.62 joris 1287: free(bp);
1.26 jfb 1288: if (rbuf == NULL)
1289: break;
1290: } while (rcsnum_cmp(crev, rev, 0) != 0);
1.1 jfb 1291:
1.26 jfb 1292: rcsnum_free(crev);
1.1 jfb 1293: }
1294:
1.63 joris 1295: /*
1296: * Do keyword expansion if required.
1297: */
1298: if (rfp->rf_expand != NULL)
1299: expmode = rcs_kwexp_get(rfp);
1300: else
1301: expmode = RCS_KWEXP_DEFAULT;
1302:
1303: if ((rbuf != NULL) && !(expmode & RCS_KWEXP_NONE)) {
1304: if ((dbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
1305: return (rbuf);
1306: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
1307: return (rbuf);
1308:
1309: if (cvs_buf_putc(rbuf, '\0') < 0) {
1310: cvs_buf_free(dbuf);
1311: return (rbuf);
1312: }
1313:
1314: bp = cvs_buf_release(rbuf);
1315: if ((lines = rcs_splitlines((char *)bp)) != NULL) {
1316: res = 0;
1317: TAILQ_FOREACH(lp, &lines->rl_lines, rl_list) {
1318: if (res++ == 0)
1319: continue;
1320: rcs_expand_keywords(rfp->rf_path, rdp,
1321: lp->rl_line, out, sizeof(out), expmode);
1322: cvs_buf_fappend(dbuf, "%s\n", out);
1323: }
1324: rcs_freefoo(lines);
1325: }
1326: free(bp);
1327: }
1328:
1329: return (dbuf);
1.1 jfb 1330: }
1331:
1332: /*
1.52 jfb 1333: * rcs_rev_add()
1334: *
1.53 jfb 1335: * Add a revision to the RCS file <rf>. The new revision's number can be
1336: * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
1337: * new revision will have a number equal to the previous head revision plus
1338: * one). The <msg> argument specifies the log message for that revision, and
1339: * <date> specifies the revision's date (a value of -1 is
1340: * equivalent to using the current time).
1.52 jfb 1341: * Returns 0 on success, or -1 on failure.
1342: */
1343: int
1.53 jfb 1344: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date)
1.52 jfb 1345: {
1346: time_t now;
1347: struct passwd *pw;
1348: struct rcs_delta *rdp;
1.67 niallo 1349: RCSNUM *tmprev = NULL;
1.52 jfb 1350:
1351: if (rev == RCS_HEAD_REV) {
1.67 niallo 1352: const RCSNUM *head_rev;
1353: char version_str[10];
1354:
1355: head_rev = rcs_head_get(rf);
1356: if ((tmprev = rcsnum_alloc()) == NULL) {
1357: cvs_log(LP_ERR, "could not allocate rcsnum");
1358: return (-1);
1359: }
1360: if (rcsnum_cpy(head_rev, tmprev, sizeof(version_str)) != 0) {
1361: cvs_log(LP_ERR, "could not perform rcsnum_cpy");
1362: rcsnum_free(tmprev);
1363: return (-1);
1364: }
1365: rev = rcsnum_inc(tmprev);
1.52 jfb 1366: } else if ((rdp = rcs_findrev(rf, rev)) != NULL) {
1367: rcs_errno = RCS_ERR_DUPENT;
1368: return (-1);
1369: }
1370:
1371: if ((pw = getpwuid(getuid())) == NULL) {
1372: rcs_errno = RCS_ERR_ERRNO;
1373: return (-1);
1374: }
1375:
1376: if ((rdp = (struct rcs_delta *)malloc(sizeof(*rdp))) == NULL) {
1377: rcs_errno = RCS_ERR_ERRNO;
1378: return (-1);
1379: }
1380: memset(rdp, 0, sizeof(*rdp));
1381:
1382: TAILQ_INIT(&(rdp->rd_branches));
1383: TAILQ_INIT(&(rdp->rd_snodes));
1384:
1385: if ((rdp->rd_num = rcsnum_alloc()) == NULL) {
1386: rcs_freedelta(rdp);
1387: return (-1);
1388: }
1389: rcsnum_cpy(rev, rdp->rd_num, 0);
1.67 niallo 1390: if (tmprev != NULL)
1391: rcsnum_free(tmprev);
1.52 jfb 1392:
1393: if ((rdp->rd_author = cvs_strdup(pw->pw_name)) == NULL) {
1394: rcs_freedelta(rdp);
1395: return (-1);
1396: }
1397:
1398: if ((rdp->rd_state = cvs_strdup(RCS_STATE_EXP)) == NULL) {
1399: rcs_freedelta(rdp);
1400: return (-1);
1401: }
1402:
1403: if ((rdp->rd_log = cvs_strdup(msg)) == NULL) {
1404: rcs_errno = RCS_ERR_ERRNO;
1405: rcs_freedelta(rdp);
1406: return (-1);
1407: }
1408:
1.53 jfb 1409: if (date != (time_t)(-1))
1410: now = date;
1411: else
1412: time(&now);
1.52 jfb 1413: gmtime_r(&now, &(rdp->rd_date));
1414:
1415: TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1416: rf->rf_ndelta++;
1.64 niallo 1417: /* not synced anymore */
1418: rf->rf_flags &= ~RCS_SYNCED;
1.52 jfb 1419:
1420: return (0);
1421: }
1422:
1423: /*
1424: * rcs_rev_remove()
1425: *
1426: * Remove the revision whose number is <rev> from the RCS file <rf>.
1427: */
1428: int
1429: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
1430: {
1431: int ret;
1432: struct rcs_delta *rdp;
1433:
1434: ret = 0;
1435: if (rev == RCS_HEAD_REV)
1436: rev = rf->rf_head;
1437:
1438: /* do we actually have that revision? */
1439: if ((rdp = rcs_findrev(rf, rev)) == NULL) {
1440: rcs_errno = RCS_ERR_NOENT;
1441: ret = -1;
1442: } else {
1443: /* XXX assumes it's not a sub node */
1444: TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
1445: rf->rf_ndelta--;
1446: rf->rf_flags &= ~RCS_SYNCED;
1447: }
1448:
1449: return (ret);
1450:
1451: }
1452:
1453: /*
1.1 jfb 1454: * rcs_findrev()
1455: *
1456: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1457: * The revision number is given in <rev>.
1458: * Returns a pointer to the delta on success, or NULL on failure.
1459: */
1460: static struct rcs_delta*
1.43 jfb 1461: rcs_findrev(RCSFILE *rfp, const RCSNUM *rev)
1.1 jfb 1462: {
1463: u_int cmplen;
1464: struct rcs_delta *rdp;
1465: struct rcs_dlist *hp;
1.6 vincent 1466: int found;
1.26 jfb 1467:
1.1 jfb 1468: cmplen = 2;
1469: hp = &(rfp->rf_delta);
1470:
1.6 vincent 1471: do {
1472: found = 0;
1473: TAILQ_FOREACH(rdp, hp, rd_list) {
1474: if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
1475: if (cmplen == rev->rn_len)
1476: return (rdp);
1.1 jfb 1477:
1.6 vincent 1478: hp = &(rdp->rd_snodes);
1479: cmplen += 2;
1480: found = 1;
1481: break;
1482: }
1.1 jfb 1483: }
1.6 vincent 1484: } while (found && cmplen < rev->rn_len);
1.1 jfb 1485:
1486: return (NULL);
1.20 jfb 1487: }
1488:
1489: /*
1.26 jfb 1490: * rcs_kwexp_set()
1491: *
1492: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1493: * Returns 0 on success, or -1 on failure.
1494: */
1495: int
1496: rcs_kwexp_set(RCSFILE *file, int mode)
1497: {
1498: int i;
1499: char *tmp, buf[8] = "";
1500:
1501: if (RCS_KWEXP_INVAL(mode))
1502: return (-1);
1503:
1504: i = 0;
1505: if (mode == RCS_KWEXP_NONE)
1506: buf[0] = 'b';
1507: else if (mode == RCS_KWEXP_OLD)
1508: buf[0] = 'o';
1509: else {
1510: if (mode & RCS_KWEXP_NAME)
1511: buf[i++] = 'k';
1512: if (mode & RCS_KWEXP_VAL)
1513: buf[i++] = 'v';
1514: if (mode & RCS_KWEXP_LKR)
1515: buf[i++] = 'l';
1516: }
1517:
1.39 joris 1518: if ((tmp = cvs_strdup(buf)) == NULL) {
1.26 jfb 1519: cvs_log(LP_ERRNO, "%s: failed to copy expansion mode",
1520: file->rf_path);
1521: return (-1);
1522: }
1523:
1.27 jfb 1524: if (file->rf_expand != NULL)
1.39 joris 1525: cvs_strfree(file->rf_expand);
1.26 jfb 1526: file->rf_expand = tmp;
1.64 niallo 1527: /* not synced anymore */
1528: file->rf_flags &= ~RCS_SYNCED;
1.26 jfb 1529:
1530: return (0);
1531: }
1532:
1533: /*
1534: * rcs_kwexp_get()
1535: *
1536: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1537: */
1538: int
1539: rcs_kwexp_get(RCSFILE *file)
1540: {
1541: return rcs_kflag_get(file->rf_expand);
1542: }
1543:
1544: /*
1.20 jfb 1545: * rcs_kflag_get()
1546: *
1547: * Get the keyword expansion mode from a set of character flags given in
1548: * <flags> and return the appropriate flag mask. In case of an error, the
1549: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1550: */
1551: int
1552: rcs_kflag_get(const char *flags)
1553: {
1554: int fl;
1555: size_t len;
1556: const char *fp;
1557:
1558: fl = 0;
1559: len = strlen(flags);
1560:
1561: for (fp = flags; *fp != '\0'; fp++) {
1562: if (*fp == 'k')
1563: fl |= RCS_KWEXP_NAME;
1564: else if (*fp == 'v')
1565: fl |= RCS_KWEXP_VAL;
1566: else if (*fp == 'l')
1567: fl |= RCS_KWEXP_LKR;
1568: else if (*fp == 'o') {
1569: if (len != 1)
1570: fl |= RCS_KWEXP_ERR;
1571: fl |= RCS_KWEXP_OLD;
1572: } else if (*fp == 'b') {
1573: if (len != 1)
1574: fl |= RCS_KWEXP_ERR;
1575: } else /* unknown letter */
1576: fl |= RCS_KWEXP_ERR;
1577: }
1578:
1579: return (fl);
1.32 jfb 1580: }
1581:
1582: /*
1583: * rcs_errstr()
1584: *
1585: * Get the error string matching the RCS error code <code>.
1586: */
1.57 xsa 1587: const char *
1.32 jfb 1588: rcs_errstr(int code)
1589: {
1.50 jfb 1590: const char *esp;
1591:
1592: if ((code < 0) || ((code >= (int)RCS_NERR) && (code != RCS_ERR_ERRNO)))
1593: esp = NULL;
1594: else if (code == RCS_ERR_ERRNO)
1595: esp = strerror(errno);
1596: else
1597: esp = rcs_errstrs[code];
1598: return (esp);
1.1 jfb 1599: }
1600:
1.21 jfb 1601: void
1602: rcs_kflag_usage(void)
1603: {
1604: fprintf(stderr, "Valid expansion modes include:\n"
1.22 jfb 1605: "\t-kkv\tGenerate keywords using the default form.\n"
1606: "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
1607: "\t-kk\tGenerate only keyword names in keyword strings.\n"
1608: "\t-kv\tGenerate only keyword values in keyword strings.\n"
1609: "\t-ko\tGenerate old keyword string "
1.21 jfb 1610: "(no changes from checked in file).\n"
1.22 jfb 1611: "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21 jfb 1612: }
1.1 jfb 1613:
1614: /*
1615: * rcs_parse()
1616: *
1617: * Parse the contents of file <path>, which are in the RCS format.
1618: * Returns 0 on success, or -1 on failure.
1619: */
1.26 jfb 1620: static int
1.1 jfb 1621: rcs_parse(RCSFILE *rfp)
1622: {
1623: int ret;
1624: struct rcs_pdata *pdp;
1625:
1.26 jfb 1626: if (rfp->rf_flags & RCS_PARSED)
1.1 jfb 1627: return (0);
1628:
1.26 jfb 1629: if ((pdp = (struct rcs_pdata *)malloc(sizeof(*pdp))) == NULL) {
1.50 jfb 1630: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1631: cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
1632: return (-1);
1633: }
1634: memset(pdp, 0, sizeof(*pdp));
1635:
1.18 jfb 1636: pdp->rp_lines = 0;
1.1 jfb 1637: pdp->rp_pttype = RCS_TOK_ERR;
1638:
1639: pdp->rp_file = fopen(rfp->rf_path, "r");
1640: if (pdp->rp_file == NULL) {
1.50 jfb 1641: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1642: cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
1643: rcs_freepdata(pdp);
1644: return (-1);
1645: }
1646:
1.59 xsa 1647: pdp->rp_buf = (char *)malloc((size_t)RCS_BUFSIZE);
1.1 jfb 1648: if (pdp->rp_buf == NULL) {
1.50 jfb 1649: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1650: cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
1651: rcs_freepdata(pdp);
1652: return (-1);
1653: }
1654: pdp->rp_blen = RCS_BUFSIZE;
1.18 jfb 1655: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1 jfb 1656:
1657: /* ditch the strict lock */
1.26 jfb 1658: rfp->rf_flags &= ~RCS_SLOCK;
1.1 jfb 1659: rfp->rf_pdata = pdp;
1660:
1.31 jfb 1661: if ((ret = rcs_parse_admin(rfp)) < 0) {
1.1 jfb 1662: rcs_freepdata(pdp);
1663: return (-1);
1.31 jfb 1664: } else if (ret == RCS_TOK_NUM) {
1665: for (;;) {
1666: ret = rcs_parse_delta(rfp);
1667: if (ret == 0)
1668: break;
1669: else if (ret == -1) {
1670: rcs_freepdata(pdp);
1671: return (-1);
1672: }
1.1 jfb 1673: }
1674: }
1675:
1676: ret = rcs_gettok(rfp);
1677: if (ret != RCS_TOK_DESC) {
1.50 jfb 1678: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1679: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1680: RCS_TOKSTR(rfp));
1681: rcs_freepdata(pdp);
1682: return (-1);
1683: }
1684:
1685: ret = rcs_gettok(rfp);
1686: if (ret != RCS_TOK_STRING) {
1.50 jfb 1687: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1688: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1689: RCS_TOKSTR(rfp));
1690: rcs_freepdata(pdp);
1691: return (-1);
1692: }
1693:
1.39 joris 1694: rfp->rf_desc = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1695: if (rfp->rf_desc == NULL) {
1696: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
1697: rcs_freepdata(pdp);
1698: return (-1);
1699: }
1.1 jfb 1700:
1701: for (;;) {
1702: ret = rcs_parse_deltatext(rfp);
1703: if (ret == 0)
1704: break;
1705: else if (ret == -1) {
1706: rcs_freepdata(pdp);
1707: return (-1);
1708: }
1709: }
1710:
1711: rcs_freepdata(pdp);
1712:
1713: rfp->rf_pdata = NULL;
1.26 jfb 1714: rfp->rf_flags |= RCS_PARSED | RCS_SYNCED;
1.1 jfb 1715:
1716: return (0);
1717: }
1718:
1719: /*
1720: * rcs_parse_admin()
1721: *
1722: * Parse the administrative portion of an RCS file.
1.31 jfb 1723: * Returns the type of the first token found after the admin section on
1724: * success, or -1 on failure.
1.1 jfb 1725: */
1726: static int
1727: rcs_parse_admin(RCSFILE *rfp)
1728: {
1729: u_int i;
1730: int tok, ntok, hmask;
1731: struct rcs_key *rk;
1732:
1733: /* hmask is a mask of the headers already encountered */
1734: hmask = 0;
1735: for (;;) {
1736: tok = rcs_gettok(rfp);
1737: if (tok == RCS_TOK_ERR) {
1.50 jfb 1738: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1739: cvs_log(LP_ERR, "parse error in RCS admin section");
1740: return (-1);
1.31 jfb 1741: } else if ((tok == RCS_TOK_NUM) || (tok == RCS_TOK_DESC)) {
1742: /*
1743: * Assume this is the start of the first delta or
1744: * that we are dealing with an empty RCS file and
1745: * we just found the description.
1746: */
1.1 jfb 1747: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31 jfb 1748: return (tok);
1.1 jfb 1749: }
1750:
1751: rk = NULL;
1.18 jfb 1752: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1753: if (rcs_keys[i].rk_id == tok)
1754: rk = &(rcs_keys[i]);
1755:
1756: if (hmask & (1 << tok)) {
1.50 jfb 1757: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1758: cvs_log(LP_ERR, "duplicate RCS key");
1759: return (-1);
1760: }
1761: hmask |= (1 << tok);
1762:
1763: switch (tok) {
1764: case RCS_TOK_HEAD:
1765: case RCS_TOK_BRANCH:
1766: case RCS_TOK_COMMENT:
1767: case RCS_TOK_EXPAND:
1768: ntok = rcs_gettok(rfp);
1769: if (ntok == RCS_TOK_SCOLON)
1770: break;
1771: if (ntok != rk->rk_val) {
1.50 jfb 1772: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1773: cvs_log(LP_ERR,
1774: "invalid value type for RCS key `%s'",
1775: rk->rk_str);
1776: }
1777:
1778: if (tok == RCS_TOK_HEAD) {
1.28 jfb 1779: if (rfp->rf_head == NULL) {
1780: rfp->rf_head = rcsnum_alloc();
1781: if (rfp->rf_head == NULL)
1782: return (-1);
1783: }
1.1 jfb 1784: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1785: rfp->rf_head);
1.14 deraadt 1786: } else if (tok == RCS_TOK_BRANCH) {
1.35 jfb 1787: if (rfp->rf_branch == NULL) {
1788: rfp->rf_branch = rcsnum_alloc();
1789: if (rfp->rf_branch == NULL)
1790: return (-1);
1791: }
1792: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1793: rfp->rf_branch) < 0)
1794: return (-1);
1.14 deraadt 1795: } else if (tok == RCS_TOK_COMMENT) {
1.39 joris 1796: rfp->rf_comment = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1797: if (rfp->rf_comment == NULL) {
1798: cvs_log(LP_ERRNO,
1799: "failed to duplicate rcs token");
1800: return (-1);
1801: }
1.14 deraadt 1802: } else if (tok == RCS_TOK_EXPAND) {
1.39 joris 1803: rfp->rf_expand = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1804: if (rfp->rf_expand == NULL) {
1805: cvs_log(LP_ERRNO,
1806: "failed to duplicate rcs token");
1807: return (-1);
1808: }
1.1 jfb 1809: }
1810:
1811: /* now get the expected semi-colon */
1812: ntok = rcs_gettok(rfp);
1813: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1814: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1815: cvs_log(LP_ERR,
1816: "missing semi-colon after RCS `%s' key",
1.26 jfb 1817: rk->rk_str);
1.1 jfb 1818: return (-1);
1819: }
1820: break;
1821: case RCS_TOK_ACCESS:
1.29 jfb 1822: if (rcs_parse_access(rfp) < 0)
1823: return (-1);
1.1 jfb 1824: break;
1825: case RCS_TOK_SYMBOLS:
1.29 jfb 1826: if (rcs_parse_symbols(rfp) < 0)
1827: return (-1);
1.1 jfb 1828: break;
1829: case RCS_TOK_LOCKS:
1.29 jfb 1830: if (rcs_parse_locks(rfp) < 0)
1831: return (-1);
1.1 jfb 1832: break;
1833: default:
1.50 jfb 1834: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1835: cvs_log(LP_ERR,
1836: "unexpected token `%s' in RCS admin section",
1837: RCS_TOKSTR(rfp));
1838: return (-1);
1839: }
1840: }
1841:
1842: return (0);
1843: }
1844:
1845: /*
1846: * rcs_parse_delta()
1847: *
1848: * Parse an RCS delta section and allocate the structure to store that delta's
1849: * information in the <rfp> delta list.
1850: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1851: * -1 on error.
1852: */
1853: static int
1854: rcs_parse_delta(RCSFILE *rfp)
1855: {
1856: int ret, tok, ntok, hmask;
1857: u_int i;
1858: char *tokstr;
1.3 vincent 1859: RCSNUM *datenum;
1.1 jfb 1860: struct rcs_delta *rdp;
1861: struct rcs_key *rk;
1862:
1863: rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
1864: if (rdp == NULL) {
1.50 jfb 1865: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1866: cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
1867: return (-1);
1868: }
1869: memset(rdp, 0, sizeof(*rdp));
1870:
1871: rdp->rd_num = rcsnum_alloc();
1.11 joris 1872: if (rdp->rd_num == NULL) {
1873: rcs_freedelta(rdp);
1874: return (-1);
1875: }
1.1 jfb 1876: rdp->rd_next = rcsnum_alloc();
1.11 joris 1877: if (rdp->rd_next == NULL) {
1878: rcs_freedelta(rdp);
1879: return (-1);
1880: }
1.1 jfb 1881:
1882: TAILQ_INIT(&(rdp->rd_branches));
1.52 jfb 1883: TAILQ_INIT(&(rdp->rd_snodes));
1.1 jfb 1884:
1885: tok = rcs_gettok(rfp);
1886: if (tok != RCS_TOK_NUM) {
1.52 jfb 1887: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1888: cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
1889: RCS_TOKSTR(rfp));
1890: rcs_freedelta(rdp);
1891: return (-1);
1892: }
1893: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1894:
1895: hmask = 0;
1896: ret = 0;
1897: tokstr = NULL;
1898:
1899: for (;;) {
1900: tok = rcs_gettok(rfp);
1901: if (tok == RCS_TOK_ERR) {
1.50 jfb 1902: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1903: cvs_log(LP_ERR, "parse error in RCS delta section");
1904: rcs_freedelta(rdp);
1905: return (-1);
1.14 deraadt 1906: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15 tedu 1907: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1 jfb 1908: ret = (tok == RCS_TOK_NUM ? 1 : 0);
1909: break;
1910: }
1911:
1912: rk = NULL;
1.18 jfb 1913: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1914: if (rcs_keys[i].rk_id == tok)
1915: rk = &(rcs_keys[i]);
1916:
1917: if (hmask & (1 << tok)) {
1.50 jfb 1918: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1919: cvs_log(LP_ERR, "duplicate RCS key");
1920: rcs_freedelta(rdp);
1921: return (-1);
1922: }
1923: hmask |= (1 << tok);
1924:
1925: switch (tok) {
1926: case RCS_TOK_DATE:
1927: case RCS_TOK_AUTHOR:
1928: case RCS_TOK_STATE:
1929: case RCS_TOK_NEXT:
1930: ntok = rcs_gettok(rfp);
1931: if (ntok == RCS_TOK_SCOLON) {
1932: if (rk->rk_flags & RCS_VOPT)
1933: break;
1934: else {
1.50 jfb 1935: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1936: cvs_log(LP_ERR, "missing mandatory "
1937: "value to RCS key `%s'",
1938: rk->rk_str);
1939: rcs_freedelta(rdp);
1940: return (-1);
1941: }
1942: }
1943:
1944: if (ntok != rk->rk_val) {
1.50 jfb 1945: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1946: cvs_log(LP_ERR,
1947: "invalid value type for RCS key `%s'",
1948: rk->rk_str);
1949: rcs_freedelta(rdp);
1950: return (-1);
1951: }
1952:
1953: if (tokstr != NULL)
1.41 jfb 1954: cvs_strfree(tokstr);
1.39 joris 1955: tokstr = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1956: if (tokstr == NULL) {
1.15 tedu 1957: cvs_log(LP_ERRNO,
1.10 joris 1958: "failed to duplicate rcs token");
1959: rcs_freedelta(rdp);
1960: return (-1);
1961: }
1.1 jfb 1962:
1963: /* now get the expected semi-colon */
1964: ntok = rcs_gettok(rfp);
1965: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1966: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1967: cvs_log(LP_ERR,
1968: "missing semi-colon after RCS `%s' key",
1.26 jfb 1969: rk->rk_str);
1.41 jfb 1970: cvs_strfree(tokstr);
1.1 jfb 1971: rcs_freedelta(rdp);
1972: return (-1);
1973: }
1974:
1975: if (tok == RCS_TOK_DATE) {
1.25 jfb 1976: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.41 jfb 1977: cvs_strfree(tokstr);
1.11 joris 1978: rcs_freedelta(rdp);
1979: return (-1);
1980: }
1.3 vincent 1981: if (datenum->rn_len != 6) {
1.50 jfb 1982: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1983: cvs_log(LP_ERR,
1984: "RCS date specification has %s "
1985: "fields",
1.3 vincent 1986: (datenum->rn_len > 6) ? "too many" :
1.1 jfb 1987: "missing");
1.41 jfb 1988: cvs_strfree(tokstr);
1.1 jfb 1989: rcs_freedelta(rdp);
1.37 tedu 1990: rcsnum_free(datenum);
1991: return (-1);
1.1 jfb 1992: }
1.3 vincent 1993: rdp->rd_date.tm_year = datenum->rn_id[0];
1.19 jfb 1994: if (rdp->rd_date.tm_year >= 1900)
1995: rdp->rd_date.tm_year -= 1900;
1.3 vincent 1996: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
1997: rdp->rd_date.tm_mday = datenum->rn_id[2];
1998: rdp->rd_date.tm_hour = datenum->rn_id[3];
1999: rdp->rd_date.tm_min = datenum->rn_id[4];
2000: rdp->rd_date.tm_sec = datenum->rn_id[5];
2001: rcsnum_free(datenum);
1.14 deraadt 2002: } else if (tok == RCS_TOK_AUTHOR) {
1.1 jfb 2003: rdp->rd_author = tokstr;
2004: tokstr = NULL;
1.14 deraadt 2005: } else if (tok == RCS_TOK_STATE) {
1.1 jfb 2006: rdp->rd_state = tokstr;
2007: tokstr = NULL;
1.14 deraadt 2008: } else if (tok == RCS_TOK_NEXT) {
1.1 jfb 2009: rcsnum_aton(tokstr, NULL, rdp->rd_next);
2010: }
2011: break;
2012: case RCS_TOK_BRANCHES:
1.46 jfb 2013: if (rcs_parse_branches(rfp, rdp) < 0) {
2014: rcs_freedelta(rdp);
2015: return (-1);
2016: }
1.1 jfb 2017: break;
2018: default:
1.50 jfb 2019: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2020: cvs_log(LP_ERR,
2021: "unexpected token `%s' in RCS delta",
2022: RCS_TOKSTR(rfp));
2023: rcs_freedelta(rdp);
2024: return (-1);
2025: }
2026: }
2027:
1.13 jfb 2028: if (tokstr != NULL)
1.39 joris 2029: cvs_strfree(tokstr);
1.13 jfb 2030:
1.1 jfb 2031: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26 jfb 2032: rfp->rf_ndelta++;
1.1 jfb 2033:
2034: return (ret);
2035: }
2036:
2037: /*
2038: * rcs_parse_deltatext()
2039: *
2040: * Parse an RCS delta text section and fill in the log and text field of the
2041: * appropriate delta section.
2042: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
2043: * -1 on error.
2044: */
2045: static int
2046: rcs_parse_deltatext(RCSFILE *rfp)
2047: {
2048: int tok;
2049: RCSNUM *tnum;
2050: struct rcs_delta *rdp;
2051:
2052: tok = rcs_gettok(rfp);
2053: if (tok == RCS_TOK_EOF)
2054: return (0);
2055:
2056: if (tok != RCS_TOK_NUM) {
1.50 jfb 2057: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2058: cvs_log(LP_ERR,
2059: "unexpected token `%s' at start of RCS delta text",
2060: RCS_TOKSTR(rfp));
2061: return (-1);
2062: }
1.13 jfb 2063:
2064: tnum = rcsnum_alloc();
2065: if (tnum == NULL)
2066: return (-1);
1.1 jfb 2067: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
2068:
2069: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
2070: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
2071: break;
2072: }
1.13 jfb 2073: rcsnum_free(tnum);
2074:
1.1 jfb 2075: if (rdp == NULL) {
2076: cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
2077: RCS_TOKSTR(rfp));
2078: return (-1);
2079: }
2080:
2081: tok = rcs_gettok(rfp);
2082: if (tok != RCS_TOK_LOG) {
1.50 jfb 2083: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2084: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
2085: RCS_TOKSTR(rfp));
2086: return (-1);
2087: }
2088:
2089: tok = rcs_gettok(rfp);
2090: if (tok != RCS_TOK_STRING) {
1.50 jfb 2091: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2092: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
2093: RCS_TOKSTR(rfp));
2094: return (-1);
2095: }
1.39 joris 2096: rdp->rd_log = cvs_strdup(RCS_TOKSTR(rfp));
1.1 jfb 2097: if (rdp->rd_log == NULL) {
2098: cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
2099: return (-1);
2100: }
2101:
2102: tok = rcs_gettok(rfp);
2103: if (tok != RCS_TOK_TEXT) {
1.50 jfb 2104: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2105: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2106: RCS_TOKSTR(rfp));
2107: return (-1);
2108: }
2109:
2110: tok = rcs_gettok(rfp);
2111: if (tok != RCS_TOK_STRING) {
1.50 jfb 2112: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2113: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2114: RCS_TOKSTR(rfp));
2115: return (-1);
2116: }
2117:
1.45 jfb 2118: rdp->rd_text = (u_char *)malloc(RCS_TOKLEN(rfp));
1.1 jfb 2119: if (rdp->rd_text == NULL) {
2120: cvs_log(LP_ERRNO, "failed to copy RCS delta text");
2121: return (-1);
2122: }
1.45 jfb 2123: memcpy(rdp->rd_text, RCS_TOKSTR(rfp), RCS_TOKLEN(rfp));
1.42 jfb 2124: rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1 jfb 2125:
2126: return (1);
2127: }
2128:
2129: /*
2130: * rcs_parse_access()
2131: *
2132: * Parse the access list given as value to the `access' keyword.
2133: * Returns 0 on success, or -1 on failure.
2134: */
2135: static int
2136: rcs_parse_access(RCSFILE *rfp)
2137: {
2138: int type;
2139:
2140: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
2141: if (type != RCS_TOK_ID) {
1.50 jfb 2142: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2143: cvs_log(LP_ERR, "unexpected token `%s' in access list",
2144: RCS_TOKSTR(rfp));
2145: return (-1);
2146: }
1.29 jfb 2147:
2148: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
2149: return (-1);
1.1 jfb 2150: }
2151:
2152: return (0);
2153: }
2154:
2155: /*
2156: * rcs_parse_symbols()
2157: *
2158: * Parse the symbol list given as value to the `symbols' keyword.
2159: * Returns 0 on success, or -1 on failure.
2160: */
2161: static int
2162: rcs_parse_symbols(RCSFILE *rfp)
2163: {
2164: int type;
2165: struct rcs_sym *symp;
2166:
2167: for (;;) {
2168: type = rcs_gettok(rfp);
2169: if (type == RCS_TOK_SCOLON)
2170: break;
2171:
1.41 jfb 2172: if (type != RCS_TOK_ID) {
1.50 jfb 2173: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2174: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2175: RCS_TOKSTR(rfp));
2176: return (-1);
2177: }
2178:
2179: symp = (struct rcs_sym *)malloc(sizeof(*symp));
2180: if (symp == NULL) {
1.50 jfb 2181: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 2182: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
2183: return (-1);
2184: }
1.39 joris 2185: symp->rs_name = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 2186: if (symp->rs_name == NULL) {
2187: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
2188: free(symp);
2189: return (-1);
2190: }
2191:
1.1 jfb 2192: symp->rs_num = rcsnum_alloc();
1.11 joris 2193: if (symp->rs_num == NULL) {
2194: cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
1.39 joris 2195: cvs_strfree(symp->rs_name);
1.11 joris 2196: free(symp);
2197: return (-1);
2198: }
1.1 jfb 2199:
2200: type = rcs_gettok(rfp);
2201: if (type != RCS_TOK_COLON) {
1.50 jfb 2202: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2203: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2204: RCS_TOKSTR(rfp));
1.11 joris 2205: rcsnum_free(symp->rs_num);
1.39 joris 2206: cvs_strfree(symp->rs_name);
1.1 jfb 2207: free(symp);
2208: return (-1);
2209: }
2210:
2211: type = rcs_gettok(rfp);
2212: if (type != RCS_TOK_NUM) {
1.50 jfb 2213: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2214: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2215: RCS_TOKSTR(rfp));
1.11 joris 2216: rcsnum_free(symp->rs_num);
1.39 joris 2217: cvs_strfree(symp->rs_name);
1.1 jfb 2218: free(symp);
2219: return (-1);
2220: }
2221:
2222: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
2223: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2224: RCS_TOKSTR(rfp));
1.11 joris 2225: rcsnum_free(symp->rs_num);
1.39 joris 2226: cvs_strfree(symp->rs_name);
1.1 jfb 2227: free(symp);
2228: return (-1);
2229: }
2230:
1.43 jfb 2231: TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1 jfb 2232: }
2233:
2234: return (0);
2235: }
2236:
2237: /*
2238: * rcs_parse_locks()
2239: *
2240: * Parse the lock list given as value to the `locks' keyword.
2241: * Returns 0 on success, or -1 on failure.
2242: */
2243: static int
2244: rcs_parse_locks(RCSFILE *rfp)
2245: {
2246: int type;
2247: struct rcs_lock *lkp;
2248:
2249: for (;;) {
2250: type = rcs_gettok(rfp);
2251: if (type == RCS_TOK_SCOLON)
2252: break;
2253:
2254: if (type != RCS_TOK_ID) {
1.50 jfb 2255: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2256: cvs_log(LP_ERR, "unexpected token `%s' in lock list",
2257: RCS_TOKSTR(rfp));
2258: return (-1);
2259: }
2260:
2261: lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
2262: if (lkp == NULL) {
2263: cvs_log(LP_ERRNO, "failed to allocate RCS lock");
2264: return (-1);
2265: }
2266: lkp->rl_num = rcsnum_alloc();
1.11 joris 2267: if (lkp->rl_num == NULL) {
2268: free(lkp);
2269: return (-1);
2270: }
1.1 jfb 2271:
2272: type = rcs_gettok(rfp);
2273: if (type != RCS_TOK_COLON) {
1.50 jfb 2274: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2275: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2276: RCS_TOKSTR(rfp));
1.37 tedu 2277: rcsnum_free(lkp->rl_num);
1.1 jfb 2278: free(lkp);
2279: return (-1);
2280: }
2281:
2282: type = rcs_gettok(rfp);
2283: if (type != RCS_TOK_NUM) {
1.50 jfb 2284: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2285: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2286: RCS_TOKSTR(rfp));
1.37 tedu 2287: rcsnum_free(lkp->rl_num);
1.1 jfb 2288: free(lkp);
2289: return (-1);
2290: }
2291:
2292: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
2293: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2294: RCS_TOKSTR(rfp));
1.37 tedu 2295: rcsnum_free(lkp->rl_num);
1.1 jfb 2296: free(lkp);
2297: return (-1);
2298: }
2299:
2300: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
2301: }
2302:
2303: /* check if we have a `strict' */
2304: type = rcs_gettok(rfp);
2305: if (type != RCS_TOK_STRICT) {
2306: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14 deraadt 2307: } else {
1.26 jfb 2308: rfp->rf_flags |= RCS_SLOCK;
1.1 jfb 2309:
2310: type = rcs_gettok(rfp);
2311: if (type != RCS_TOK_SCOLON) {
1.50 jfb 2312: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2313: cvs_log(LP_ERR,
2314: "missing semi-colon after `strict' keyword");
2315: return (-1);
2316: }
2317: }
2318:
2319: return (0);
2320: }
2321:
2322: /*
2323: * rcs_parse_branches()
2324: *
2325: * Parse the list of branches following a `branches' keyword in a delta.
2326: * Returns 0 on success, or -1 on failure.
2327: */
2328: static int
2329: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
2330: {
2331: int type;
2332: struct rcs_branch *brp;
2333:
2334: for (;;) {
2335: type = rcs_gettok(rfp);
2336: if (type == RCS_TOK_SCOLON)
2337: break;
2338:
2339: if (type != RCS_TOK_NUM) {
1.50 jfb 2340: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2341: cvs_log(LP_ERR,
2342: "unexpected token `%s' in list of branches",
2343: RCS_TOKSTR(rfp));
2344: return (-1);
2345: }
2346:
2347: brp = (struct rcs_branch *)malloc(sizeof(*brp));
2348: if (brp == NULL) {
1.50 jfb 2349: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 2350: cvs_log(LP_ERRNO, "failed to allocate RCS branch");
2351: return (-1);
2352: }
1.46 jfb 2353: brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11 joris 2354: if (brp->rb_num == NULL) {
2355: free(brp);
2356: return (-1);
2357: }
1.1 jfb 2358:
2359: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
2360: }
2361:
2362: return (0);
2363: }
2364:
2365: /*
2366: * rcs_freedelta()
2367: *
2368: * Free the contents of a delta structure.
2369: */
1.18 jfb 2370: static void
1.1 jfb 2371: rcs_freedelta(struct rcs_delta *rdp)
2372: {
1.12 jfb 2373: struct rcs_branch *rb;
1.1 jfb 2374: struct rcs_delta *crdp;
2375:
1.12 jfb 2376: if (rdp->rd_num != NULL)
2377: rcsnum_free(rdp->rd_num);
2378: if (rdp->rd_next != NULL)
2379: rcsnum_free(rdp->rd_next);
2380:
1.1 jfb 2381: if (rdp->rd_author != NULL)
1.39 joris 2382: cvs_strfree(rdp->rd_author);
1.1 jfb 2383: if (rdp->rd_state != NULL)
1.39 joris 2384: cvs_strfree(rdp->rd_state);
1.1 jfb 2385: if (rdp->rd_log != NULL)
1.39 joris 2386: cvs_strfree(rdp->rd_log);
1.1 jfb 2387: if (rdp->rd_text != NULL)
1.45 jfb 2388: free(rdp->rd_text);
1.12 jfb 2389:
2390: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
2391: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
2392: rcsnum_free(rb->rb_num);
2393: free(rb);
2394: }
1.1 jfb 2395:
2396: while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
2397: TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
2398: rcs_freedelta(crdp);
2399: }
2400:
2401: free(rdp);
2402: }
2403:
2404: /*
2405: * rcs_freepdata()
2406: *
2407: * Free the contents of the parser data structure.
2408: */
2409: static void
2410: rcs_freepdata(struct rcs_pdata *pd)
2411: {
2412: if (pd->rp_file != NULL)
2413: (void)fclose(pd->rp_file);
2414: if (pd->rp_buf != NULL)
2415: free(pd->rp_buf);
2416: free(pd);
2417: }
2418:
2419: /*
2420: * rcs_gettok()
2421: *
2422: * Get the next RCS token from the string <str>.
2423: */
2424: static int
2425: rcs_gettok(RCSFILE *rfp)
2426: {
2427: u_int i;
2428: int ch, last, type;
1.18 jfb 2429: size_t len;
2430: char *bp;
1.1 jfb 2431: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2432:
2433: type = RCS_TOK_ERR;
2434: bp = pdp->rp_buf;
1.42 jfb 2435: pdp->rp_tlen = 0;
1.1 jfb 2436: *bp = '\0';
2437:
2438: if (pdp->rp_pttype != RCS_TOK_ERR) {
2439: type = pdp->rp_pttype;
2440: strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
2441: pdp->rp_pttype = RCS_TOK_ERR;
2442: return (type);
2443: }
2444:
2445: /* skip leading whitespace */
2446: /* XXX we must skip backspace too for compatibility, should we? */
2447: do {
2448: ch = getc(pdp->rp_file);
2449: if (ch == '\n')
1.18 jfb 2450: pdp->rp_lines++;
1.1 jfb 2451: } while (isspace(ch));
2452:
2453: if (ch == EOF) {
2454: type = RCS_TOK_EOF;
1.14 deraadt 2455: } else if (ch == ';') {
1.1 jfb 2456: type = RCS_TOK_SCOLON;
1.14 deraadt 2457: } else if (ch == ':') {
1.1 jfb 2458: type = RCS_TOK_COLON;
1.14 deraadt 2459: } else if (isalpha(ch)) {
1.31 jfb 2460: type = RCS_TOK_ID;
1.1 jfb 2461: *(bp++) = ch;
1.18 jfb 2462: for (;;) {
1.1 jfb 2463: ch = getc(pdp->rp_file);
1.11 joris 2464: if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1 jfb 2465: ungetc(ch, pdp->rp_file);
2466: break;
2467: }
2468: *(bp++) = ch;
1.42 jfb 2469: pdp->rp_tlen++;
1.18 jfb 2470: if (bp == pdp->rp_bufend - 1) {
2471: len = bp - pdp->rp_buf;
2472: if (rcs_growbuf(rfp) < 0) {
2473: type = RCS_TOK_ERR;
2474: break;
2475: }
2476: bp = pdp->rp_buf + len;
2477: }
1.1 jfb 2478: }
2479: *bp = '\0';
2480:
1.18 jfb 2481: if (type != RCS_TOK_ERR) {
2482: for (i = 0; i < RCS_NKEYS; i++) {
2483: if (strcmp(rcs_keys[i].rk_str,
2484: pdp->rp_buf) == 0) {
2485: type = rcs_keys[i].rk_id;
2486: break;
2487: }
1.1 jfb 2488: }
2489: }
1.14 deraadt 2490: } else if (ch == '@') {
1.1 jfb 2491: /* we have a string */
1.18 jfb 2492: type = RCS_TOK_STRING;
1.1 jfb 2493: for (;;) {
2494: ch = getc(pdp->rp_file);
2495: if (ch == '@') {
2496: ch = getc(pdp->rp_file);
2497: if (ch != '@') {
2498: ungetc(ch, pdp->rp_file);
2499: break;
2500: }
1.14 deraadt 2501: } else if (ch == '\n')
1.18 jfb 2502: pdp->rp_lines++;
1.1 jfb 2503:
2504: *(bp++) = ch;
1.42 jfb 2505: pdp->rp_tlen++;
1.18 jfb 2506: if (bp == pdp->rp_bufend - 1) {
2507: len = bp - pdp->rp_buf;
2508: if (rcs_growbuf(rfp) < 0) {
2509: type = RCS_TOK_ERR;
2510: break;
2511: }
2512: bp = pdp->rp_buf + len;
2513: }
1.1 jfb 2514: }
2515:
2516: *bp = '\0';
1.14 deraadt 2517: } else if (isdigit(ch)) {
1.1 jfb 2518: *(bp++) = ch;
2519: last = ch;
2520: type = RCS_TOK_NUM;
2521:
2522: for (;;) {
2523: ch = getc(pdp->rp_file);
1.18 jfb 2524: if (bp == pdp->rp_bufend)
1.1 jfb 2525: break;
2526: if (!isdigit(ch) && ch != '.') {
2527: ungetc(ch, pdp->rp_file);
2528: break;
2529: }
2530:
2531: if (last == '.' && ch == '.') {
2532: type = RCS_TOK_ERR;
2533: break;
2534: }
2535: last = ch;
2536: *(bp++) = ch;
1.42 jfb 2537: pdp->rp_tlen++;
1.1 jfb 2538: }
1.18 jfb 2539: *bp = '\0';
1.1 jfb 2540: }
2541:
2542: return (type);
2543: }
2544:
2545: /*
2546: * rcs_pushtok()
2547: *
2548: * Push a token back in the parser's token buffer.
2549: */
2550: static int
2551: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2552: {
2553: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2554:
2555: if (pdp->rp_pttype != RCS_TOK_ERR)
2556: return (-1);
2557:
2558: pdp->rp_pttype = type;
2559: strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
2560: return (0);
2561: }
2562:
2563:
2564: /*
2565: * rcs_splitlines()
2566: *
2567: * Split the contents of a file into a list of lines.
2568: */
2569: static struct rcs_foo*
2570: rcs_splitlines(const char *fcont)
2571: {
2572: char *dcp;
2573: struct rcs_foo *foo;
2574: struct rcs_line *lp;
2575:
2576: foo = (struct rcs_foo *)malloc(sizeof(*foo));
2577: if (foo == NULL) {
2578: cvs_log(LP_ERR, "failed to allocate line structure");
2579: return (NULL);
2580: }
2581: TAILQ_INIT(&(foo->rl_lines));
2582: foo->rl_nblines = 0;
2583: foo->rl_data = strdup(fcont);
2584: if (foo->rl_data == NULL) {
2585: cvs_log(LP_ERRNO, "failed to copy file contents");
2586: free(foo);
2587: return (NULL);
2588: }
2589:
2590: /*
2591: * Add a first bogus line with line number 0. This is used so we
2592: * can position the line pointer before 1 when changing the first line
2593: * in rcs_patch().
2594: */
2595: lp = (struct rcs_line *)malloc(sizeof(*lp));
1.38 joris 2596: if (lp == NULL) {
2597: rcs_freefoo(foo);
1.1 jfb 2598: return (NULL);
1.38 joris 2599: }
1.5 vincent 2600:
1.1 jfb 2601: lp->rl_line = NULL;
2602: lp->rl_lineno = 0;
2603: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2604:
2605:
2606: for (dcp = foo->rl_data; *dcp != '\0';) {
2607: lp = (struct rcs_line *)malloc(sizeof(*lp));
2608: if (lp == NULL) {
1.38 joris 2609: rcs_freefoo(foo);
1.1 jfb 2610: cvs_log(LP_ERR, "failed to allocate line entry");
2611: return (NULL);
2612: }
2613:
2614: lp->rl_line = dcp;
2615: lp->rl_lineno = ++(foo->rl_nblines);
2616: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2617:
2618: dcp = strchr(dcp, '\n');
2619: if (dcp == NULL) {
2620: break;
2621: }
2622: *(dcp++) = '\0';
2623: }
2624:
2625: return (foo);
1.5 vincent 2626: }
2627:
2628: static void
2629: rcs_freefoo(struct rcs_foo *fp)
2630: {
2631: struct rcs_line *lp;
2632:
2633: while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
2634: TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
2635: free(lp);
2636: }
2637: free(fp->rl_data);
2638: free(fp);
1.18 jfb 2639: }
2640:
2641: /*
2642: * rcs_growbuf()
2643: *
2644: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2645: * RCS_BUFEXTSIZE.
2646: * In case of failure, the original buffer is left unmodified.
2647: * Returns 0 on success, or -1 on failure.
2648: */
2649: static int
2650: rcs_growbuf(RCSFILE *rf)
2651: {
2652: void *tmp;
2653: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2654:
2655: tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
2656: if (tmp == NULL) {
1.50 jfb 2657: rcs_errno = RCS_ERR_ERRNO;
1.18 jfb 2658: cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
2659: return (-1);
2660: }
2661:
2662: pdp->rp_buf = (char *)tmp;
2663: pdp->rp_blen += RCS_BUFEXTSIZE;
2664: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42 jfb 2665:
2666: return (0);
2667: }
2668:
2669: /*
2670: * rcs_strprint()
2671: *
2672: * Output an RCS string <str> of size <slen> to the stream <stream>. Any
2673: * '@' characters are escaped. Otherwise, the string can contain arbitrary
2674: * binary data.
2675: */
2676: static int
2677: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
2678: {
2679: const u_char *ap, *ep, *sp;
2680: size_t ret;
1.52 jfb 2681:
2682: if (slen == 0)
2683: return (0);
1.42 jfb 2684:
2685: ep = str + slen - 1;
2686:
2687: for (sp = str; sp <= ep;) {
2688: ap = memchr(sp, '@', ep - sp);
2689: if (ap == NULL)
2690: ap = ep;
2691: ret = fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
2692:
2693: if (*ap == '@')
2694: putc('@', stream);
2695: sp = ap + 1;
1.63 joris 2696: }
2697:
2698: return (0);
2699: }
2700:
2701: /*
2702: * rcs_expand_keywords()
2703: *
2704: * Expand any RCS keywords in <line> into <out>
2705: */
2706: static int
2707: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *line, char *out,
2708: size_t len, int mode)
2709: {
2710: int kwtype;
2711: u_int i, j, found;
2712: char *c, *kwstr, *start;
2713: char expbuf[128], buf[128];
2714:
1.65 niallo 2715: kwtype = 0;
2716: kwstr = NULL;
1.63 joris 2717: i = 0;
2718:
2719: /*
2720: * Keyword formats:
2721: * $Keyword$
2722: * $Keyword: value$
2723: */
2724: memset(out, '\0', len);
2725: for (c = line; *c != '\0' && i < len; *c++) {
2726: out[i++] = *c;
2727: if (*c == '$') {
2728: /* remember start of this possible keyword */
2729: start = c;
2730:
2731: /* first following character has to be alphanumeric */
2732: *c++;
2733: if (!isalpha(*c)) {
2734: c = start;
2735: continue;
2736: }
2737:
2738: /* look for any matching keywords */
2739: found = 0;
2740: for (j = 0; j < RCS_NKWORDS; j++) {
2741: if (!strncmp(c, rcs_expkw[j].kw_str,
2742: strlen(rcs_expkw[j].kw_str))) {
2743: found = 1;
2744: kwstr = rcs_expkw[j].kw_str;
2745: kwtype = rcs_expkw[j].kw_type;
2746: break;
2747: }
2748: }
2749:
2750: /* unknown keyword, continue looking */
2751: if (found == 0) {
2752: c = start;
2753: continue;
2754: }
2755:
2756: /* next character has to be ':' or '$' */
2757: c += strlen(kwstr);
2758: if (*c != ':' && *c != '$') {
2759: c = start;
2760: continue;
2761: }
2762:
2763: /*
2764: * if the next character was ':' we need to look for
2765: * an '$' before the end of the line to be sure it is
2766: * in fact a keyword.
2767: */
2768: if (*c == ':') {
2769: while (*c++) {
2770: if (*c == '$' || *c == '\n')
2771: break;
2772: }
2773:
2774: if (*c != '$') {
2775: c = start;
2776: continue;
2777: }
2778: }
2779:
2780: /* start constructing the expansion */
2781: expbuf[0] = '\0';
2782:
2783: if (mode & RCS_KWEXP_NAME) {
2784: strlcat(expbuf, "$", sizeof(expbuf));
2785: strlcat(expbuf, kwstr, sizeof(expbuf));
2786: if (mode & RCS_KWEXP_VAL)
2787: strlcat(expbuf, ": ", sizeof(expbuf));
2788: }
2789:
2790: /*
2791: * order matters because of RCS_KW_ID and RCS_KW_HEADER here
2792: */
2793: if (mode & RCS_KWEXP_VAL) {
2794: if (kwtype & RCS_KW_RCSFILE) {
2795: if (!(kwtype & RCS_KW_FULLPATH))
2796: strlcat(expbuf, basename(rcsfile),
2797: sizeof(expbuf));
2798: else
2799: strlcat(expbuf, rcsfile,
2800: sizeof(expbuf));
2801: strlcat(expbuf, " ", sizeof(expbuf));
2802: }
2803:
2804: if (kwtype & RCS_KW_REVISION) {
2805: rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
2806: strlcat(buf, " ", sizeof(buf));
2807: strlcat(expbuf, buf, sizeof(expbuf));
2808: }
2809:
2810: if (kwtype & RCS_KW_DATE) {
2811: strftime(buf, sizeof(buf),
2812: "%Y/%m/%d %H:%M:%S ", &rdp->rd_date);
2813: strlcat(expbuf, buf, sizeof(expbuf));
2814: }
2815:
2816: if (kwtype & RCS_KW_AUTHOR) {
2817: strlcat(expbuf, rdp->rd_author,
2818: sizeof(expbuf));
2819: strlcat(expbuf, " ", sizeof(expbuf));
2820: }
2821:
2822: if (kwtype & RCS_KW_STATE) {
2823: strlcat(expbuf, rdp->rd_state,
2824: sizeof(expbuf));
2825: strlcat(expbuf, " ", sizeof(expbuf));
2826: }
2827:
2828: /* order does not matter anymore below */
2829: if (kwtype & RCS_KW_LOG)
2830: strlcat(expbuf, " ", sizeof(expbuf));
2831:
2832: if (kwtype & RCS_KW_SOURCE) {
2833: strlcat(expbuf, rcsfile, sizeof(expbuf));
2834: strlcat(expbuf, " ", sizeof(expbuf));
2835: }
2836:
2837: if (kwtype & RCS_KW_NAME)
2838: strlcat(expbuf, " ", sizeof(expbuf));
2839: }
2840:
2841: /* end the expansion */
2842: if (mode & RCS_KWEXP_NAME)
2843: strlcat(expbuf, "$", sizeof(expbuf));
2844:
2845: out[--i] = '\0';
2846: strlcat(out, expbuf, len);
2847: i += strlen(expbuf);
2848: }
1.42 jfb 2849: }
1.18 jfb 2850:
2851: return (0);
1.1 jfb 2852: }