Annotation of src/usr.bin/cvs/rcs.c, Revision 1.63
1.63 ! joris 1: /* $OpenBSD: rcs.c,v 1.62 2005/09/18 00:33:40 joris 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: }
913:
914: TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
915:
916: /* not synced anymore */
917: file->rf_flags &= ~RCS_SYNCED;
918: return (0);
919:
920:
921: }
922:
923:
924: /*
925: * rcs_lock_remove()
926: *
927: * Remove the RCS lock on revision <rev>.
928: * Returns 0 on success, or -1 on failure.
929: */
930: int
1.56 joris 931: rcs_lock_remove(RCSFILE *file, const RCSNUM *rev)
1.40 jfb 932: {
933: struct rcs_lock *lkp;
934:
935: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list)
936: if (rcsnum_cmp(lkp->rl_num, rev, 0) == 0)
937: break;
938:
939: if (lkp == NULL) {
940: rcs_errno = RCS_ERR_NOENT;
941: return (-1);
942: }
943:
944: TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
945: rcsnum_free(lkp->rl_num);
946: cvs_strfree(lkp->rl_name);
947: free(lkp);
948:
949: /* not synced anymore */
950: file->rf_flags &= ~RCS_SYNCED;
951: return (0);
952: }
953:
954: /*
1.27 jfb 955: * rcs_desc_get()
956: *
957: * Retrieve the description for the RCS file <file>.
958: */
1.57 xsa 959: const char *
1.27 jfb 960: rcs_desc_get(RCSFILE *file)
961: {
962: return (file->rf_desc);
963: }
964:
965: /*
966: * rcs_desc_set()
967: *
968: * Set the description for the RCS file <file>.
969: * Returns 0 on success, or -1 on failure.
970: */
971: int
972: rcs_desc_set(RCSFILE *file, const char *desc)
973: {
974: char *tmp;
975:
1.39 joris 976: if ((tmp = cvs_strdup(desc)) == NULL)
1.27 jfb 977: return (-1);
978:
979: if (file->rf_desc != NULL)
1.39 joris 980: cvs_strfree(file->rf_desc);
1.27 jfb 981: file->rf_desc = tmp;
982: file->rf_flags &= ~RCS_SYNCED;
983:
984: return (0);
1.51 jfb 985: }
986:
987: /*
988: * rcs_comment_lookup()
989: *
990: * Lookup the assumed comment leader based on a file's suffix.
991: * Returns a pointer to the string on success, or NULL on failure.
992: */
1.57 xsa 993: const char *
1.51 jfb 994: rcs_comment_lookup(const char *filename)
995: {
996: int i;
997: const char *sp;
998:
999: if ((sp = strrchr(filename, '.')) == NULL) {
1000: rcs_errno = RCS_ERR_NOENT;
1001: return (NULL);
1002: }
1003: sp++;
1004:
1005: for (i = 0; i < (int)NB_COMTYPES; i++)
1006: if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
1007: return (rcs_comments[i].rc_cstr);
1008: return (NULL);
1.27 jfb 1009: }
1010:
1.33 jfb 1011: /*
1012: * rcs_comment_get()
1013: *
1014: * Retrieve the comment leader for the RCS file <file>.
1015: */
1.57 xsa 1016: const char *
1.33 jfb 1017: rcs_comment_get(RCSFILE *file)
1018: {
1019: return (file->rf_comment);
1020: }
1021:
1022: /*
1023: * rcs_comment_set()
1024: *
1025: * Set the comment leader for the RCS file <file>.
1026: * Returns 0 on success, or -1 on failure.
1027: */
1028: int
1029: rcs_comment_set(RCSFILE *file, const char *comment)
1030: {
1031: char *tmp;
1032:
1.39 joris 1033: if ((tmp = cvs_strdup(comment)) == NULL)
1.33 jfb 1034: return (-1);
1035:
1036: if (file->rf_comment != NULL)
1.39 joris 1037: cvs_strfree(file->rf_comment);
1.33 jfb 1038: file->rf_comment = tmp;
1039: file->rf_flags &= ~RCS_SYNCED;
1040:
1041: return (0);
1042: }
1.40 jfb 1043:
1044: /*
1045: * rcs_tag_resolve()
1046: *
1047: * Retrieve the revision number corresponding to the tag <tag> for the RCS
1048: * file <file>.
1049: */
1.60 xsa 1050: RCSNUM *
1.40 jfb 1051: rcs_tag_resolve(RCSFILE *file, const char *tag)
1052: {
1053: RCSNUM *num;
1054:
1055: if ((num = rcsnum_parse(tag)) == NULL) {
1056: num = rcs_sym_getrev(file, tag);
1057: }
1058:
1059: return (num);
1060: }
1061:
1.27 jfb 1062:
1063: /*
1.1 jfb 1064: * rcs_patch()
1065: *
1066: * Apply an RCS-format patch pointed to by <patch> to the file contents
1067: * found in <data>.
1068: * Returns 0 on success, or -1 on failure.
1069: */
1070: BUF*
1071: rcs_patch(const char *data, const char *patch)
1072: {
1.5 vincent 1073: struct rcs_foo *dlines, *plines;
1074: struct rcs_line *lp;
1.1 jfb 1075: size_t len;
1.5 vincent 1076: int lineno;
1.1 jfb 1077: BUF *res;
1078:
1079: len = strlen(data);
1080: res = cvs_buf_alloc(len, BUF_AUTOEXT);
1081: if (res == NULL)
1082: return (NULL);
1083:
1084: dlines = rcs_splitlines(data);
1.17 jfb 1085: if (dlines == NULL) {
1086: cvs_buf_free(res);
1.1 jfb 1087: return (NULL);
1.17 jfb 1088: }
1.5 vincent 1089:
1.1 jfb 1090: plines = rcs_splitlines(patch);
1.5 vincent 1091: if (plines == NULL) {
1.17 jfb 1092: cvs_buf_free(res);
1.5 vincent 1093: rcs_freefoo(dlines);
1.1 jfb 1094: return (NULL);
1.5 vincent 1095: }
1096:
1097: if (rcs_patch_lines(dlines, plines) < 0) {
1.17 jfb 1098: cvs_buf_free(res);
1.5 vincent 1099: rcs_freefoo(plines);
1100: rcs_freefoo(dlines);
1101: return (NULL);
1102: }
1103:
1104: lineno = 0;
1105: TAILQ_FOREACH(lp, &dlines->rl_lines, rl_list) {
1106: if (lineno != 0)
1107: cvs_buf_fappend(res, "%s\n", lp->rl_line);
1108: lineno++;
1109: }
1110:
1111: rcs_freefoo(dlines);
1112: rcs_freefoo(plines);
1113: return (res);
1114: }
1115:
1.7 jfb 1116: static int
1.5 vincent 1117: rcs_patch_lines(struct rcs_foo *dlines, struct rcs_foo *plines)
1118: {
1119: char op, *ep;
1120: struct rcs_line *lp, *dlp, *ndlp;
1121: int i, lineno, nbln;
1.1 jfb 1122:
1123: dlp = TAILQ_FIRST(&(dlines->rl_lines));
1124: lp = TAILQ_FIRST(&(plines->rl_lines));
1125:
1126: /* skip first bogus line */
1127: for (lp = TAILQ_NEXT(lp, rl_list); lp != NULL;
1128: lp = TAILQ_NEXT(lp, rl_list)) {
1129: op = *(lp->rl_line);
1130: lineno = (int)strtol((lp->rl_line + 1), &ep, 10);
1131: if ((lineno > dlines->rl_nblines) || (lineno <= 0) ||
1132: (*ep != ' ')) {
1133: cvs_log(LP_ERR,
1134: "invalid line specification in RCS patch");
1.61 joris 1135: return (-1);
1.1 jfb 1136: }
1137: ep++;
1138: nbln = (int)strtol(ep, &ep, 10);
1139: if ((nbln <= 0) || (*ep != '\0')) {
1140: cvs_log(LP_ERR,
1141: "invalid line number specification in RCS patch");
1.61 joris 1142: return (-1);
1.1 jfb 1143: }
1144:
1145: /* find the appropriate line */
1146: for (;;) {
1147: if (dlp == NULL)
1148: break;
1149: if (dlp->rl_lineno == lineno)
1150: break;
1151: if (dlp->rl_lineno > lineno) {
1152: dlp = TAILQ_PREV(dlp, rcs_tqh, rl_list);
1.14 deraadt 1153: } else if (dlp->rl_lineno < lineno) {
1.1 jfb 1154: ndlp = TAILQ_NEXT(dlp, rl_list);
1155: if (ndlp->rl_lineno > lineno)
1156: break;
1157: dlp = ndlp;
1158: }
1159: }
1160: if (dlp == NULL) {
1161: cvs_log(LP_ERR,
1162: "can't find referenced line in RCS patch");
1.61 joris 1163: return (-1);
1.1 jfb 1164: }
1165:
1166: if (op == 'd') {
1167: for (i = 0; (i < nbln) && (dlp != NULL); i++) {
1168: ndlp = TAILQ_NEXT(dlp, rl_list);
1169: TAILQ_REMOVE(&(dlines->rl_lines), dlp, rl_list);
1170: dlp = ndlp;
1171: }
1.14 deraadt 1172: } else if (op == 'a') {
1.1 jfb 1173: for (i = 0; i < nbln; i++) {
1174: ndlp = lp;
1175: lp = TAILQ_NEXT(lp, rl_list);
1176: if (lp == NULL) {
1177: cvs_log(LP_ERR, "truncated RCS patch");
1.5 vincent 1178: return (-1);
1.1 jfb 1179: }
1180: TAILQ_REMOVE(&(plines->rl_lines), lp, rl_list);
1181: TAILQ_INSERT_AFTER(&(dlines->rl_lines), dlp,
1182: lp, rl_list);
1183: dlp = lp;
1184:
1185: /* we don't want lookup to block on those */
1186: lp->rl_lineno = lineno;
1187:
1188: lp = ndlp;
1189: }
1.14 deraadt 1190: } else {
1.1 jfb 1191: cvs_log(LP_ERR, "unknown RCS patch operation `%c'", op);
1.5 vincent 1192: return (-1);
1.1 jfb 1193: }
1194:
1195: /* last line of the patch, done */
1196: if (lp->rl_lineno == plines->rl_nblines)
1197: break;
1198: }
1199:
1200: /* once we're done patching, rebuild the line numbers */
1.2 vincent 1201: lineno = 0;
1.5 vincent 1202: TAILQ_FOREACH(lp, &(dlines->rl_lines), rl_list)
1.1 jfb 1203: lp->rl_lineno = lineno++;
1204: dlines->rl_nblines = lineno - 1;
1205:
1.5 vincent 1206: return (0);
1.1 jfb 1207: }
1208:
1209: /*
1210: * rcs_getrev()
1211: *
1212: * Get the whole contents of revision <rev> from the RCSFILE <rfp>. The
1.4 vincent 1213: * returned buffer is dynamically allocated and should be released using
1214: * cvs_buf_free() once the caller is done using it.
1.1 jfb 1215: */
1216: BUF*
1217: rcs_getrev(RCSFILE *rfp, RCSNUM *rev)
1218: {
1.63 ! joris 1219: int expmode, res;
1.1 jfb 1220: size_t len;
1221: void *bp;
1222: RCSNUM *crev;
1.63 ! joris 1223: BUF *rbuf, *dbuf;
1.1 jfb 1224: struct rcs_delta *rdp = NULL;
1.63 ! joris 1225: struct rcs_foo *lines;
! 1226: struct rcs_line *lp;
! 1227: char out[1024]; /* XXX */
1.1 jfb 1228:
1.28 jfb 1229: if (rfp->rf_head == NULL)
1230: return (NULL);
1231:
1.1 jfb 1232: res = rcsnum_cmp(rfp->rf_head, rev, 0);
1233: if (res == 1) {
1.32 jfb 1234: rcs_errno = RCS_ERR_NOENT;
1.1 jfb 1235: return (NULL);
1.26 jfb 1236: }
1237:
1238: rdp = rcs_findrev(rfp, rfp->rf_head);
1239: if (rdp == NULL) {
1240: cvs_log(LP_ERR, "failed to get RCS HEAD revision");
1241: return (NULL);
1242: }
1243:
1.45 jfb 1244: len = rdp->rd_tlen;
1.26 jfb 1245: if ((rbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
1246: return (NULL);
1247:
1248: cvs_buf_append(rbuf, rdp->rd_text, len);
1249:
1250: if (res != 0) {
1251: /* Apply patches backwards to get the right version.
1252: * This will need some rework to support sub branches.
1253: */
1254: if ((crev = rcsnum_alloc()) == NULL) {
1255: cvs_buf_free(rbuf);
1.1 jfb 1256: return (NULL);
1257: }
1.26 jfb 1258: rcsnum_cpy(rfp->rf_head, crev, 0);
1259: do {
1260: crev->rn_id[crev->rn_len - 1]--;
1261: rdp = rcs_findrev(rfp, crev);
1262: if (rdp == NULL) {
1263: rcsnum_free(crev);
1264: cvs_buf_free(rbuf);
1265: return (NULL);
1266: }
1.1 jfb 1267:
1.26 jfb 1268: if (cvs_buf_putc(rbuf, '\0') < 0) {
1269: rcsnum_free(crev);
1.17 jfb 1270: cvs_buf_free(rbuf);
1.11 joris 1271: return (NULL);
1.17 jfb 1272: }
1.26 jfb 1273: bp = cvs_buf_release(rbuf);
1.45 jfb 1274: rbuf = rcs_patch((char *)bp, (char *)rdp->rd_text);
1.62 joris 1275: free(bp);
1.26 jfb 1276: if (rbuf == NULL)
1277: break;
1278: } while (rcsnum_cmp(crev, rev, 0) != 0);
1.1 jfb 1279:
1.26 jfb 1280: rcsnum_free(crev);
1.1 jfb 1281: }
1282:
1.63 ! joris 1283: /*
! 1284: * Do keyword expansion if required.
! 1285: */
! 1286: if (rfp->rf_expand != NULL)
! 1287: expmode = rcs_kwexp_get(rfp);
! 1288: else
! 1289: expmode = RCS_KWEXP_DEFAULT;
! 1290:
! 1291: if ((rbuf != NULL) && !(expmode & RCS_KWEXP_NONE)) {
! 1292: if ((dbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
! 1293: return (rbuf);
! 1294: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
! 1295: return (rbuf);
! 1296:
! 1297: if (cvs_buf_putc(rbuf, '\0') < 0) {
! 1298: cvs_buf_free(dbuf);
! 1299: return (rbuf);
! 1300: }
! 1301:
! 1302: bp = cvs_buf_release(rbuf);
! 1303: if ((lines = rcs_splitlines((char *)bp)) != NULL) {
! 1304: res = 0;
! 1305: TAILQ_FOREACH(lp, &lines->rl_lines, rl_list) {
! 1306: if (res++ == 0)
! 1307: continue;
! 1308: rcs_expand_keywords(rfp->rf_path, rdp,
! 1309: lp->rl_line, out, sizeof(out), expmode);
! 1310: cvs_buf_fappend(dbuf, "%s\n", out);
! 1311: }
! 1312: rcs_freefoo(lines);
! 1313: }
! 1314: free(bp);
! 1315: }
! 1316:
! 1317: return (dbuf);
1.1 jfb 1318: }
1319:
1320: /*
1.52 jfb 1321: * rcs_rev_add()
1322: *
1.53 jfb 1323: * Add a revision to the RCS file <rf>. The new revision's number can be
1324: * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
1325: * new revision will have a number equal to the previous head revision plus
1326: * one). The <msg> argument specifies the log message for that revision, and
1327: * <date> specifies the revision's date (a value of -1 is
1328: * equivalent to using the current time).
1.52 jfb 1329: * Returns 0 on success, or -1 on failure.
1330: */
1331: int
1.53 jfb 1332: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date)
1.52 jfb 1333: {
1334: time_t now;
1335: struct passwd *pw;
1336: struct rcs_delta *rdp;
1337:
1338: if (rev == RCS_HEAD_REV) {
1339: } else if ((rdp = rcs_findrev(rf, rev)) != NULL) {
1340: rcs_errno = RCS_ERR_DUPENT;
1341: return (-1);
1342: }
1343:
1344: if ((pw = getpwuid(getuid())) == NULL) {
1345: rcs_errno = RCS_ERR_ERRNO;
1346: return (-1);
1347: }
1348:
1349: if ((rdp = (struct rcs_delta *)malloc(sizeof(*rdp))) == NULL) {
1350: rcs_errno = RCS_ERR_ERRNO;
1351: return (-1);
1352: }
1353: memset(rdp, 0, sizeof(*rdp));
1354:
1355: TAILQ_INIT(&(rdp->rd_branches));
1356: TAILQ_INIT(&(rdp->rd_snodes));
1357:
1358: if ((rdp->rd_num = rcsnum_alloc()) == NULL) {
1359: rcs_freedelta(rdp);
1360: return (-1);
1361: }
1362: rcsnum_cpy(rev, rdp->rd_num, 0);
1363:
1364: if ((rdp->rd_author = cvs_strdup(pw->pw_name)) == NULL) {
1365: rcs_freedelta(rdp);
1366: return (-1);
1367: }
1368:
1369: if ((rdp->rd_state = cvs_strdup(RCS_STATE_EXP)) == NULL) {
1370: rcs_freedelta(rdp);
1371: return (-1);
1372: }
1373:
1374: if ((rdp->rd_log = cvs_strdup(msg)) == NULL) {
1375: rcs_errno = RCS_ERR_ERRNO;
1376: rcs_freedelta(rdp);
1377: return (-1);
1378: }
1379:
1.53 jfb 1380: if (date != (time_t)(-1))
1381: now = date;
1382: else
1383: time(&now);
1.52 jfb 1384: gmtime_r(&now, &(rdp->rd_date));
1385:
1386: TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1387: rf->rf_ndelta++;
1388:
1389: return (0);
1390: }
1391:
1392: /*
1393: * rcs_rev_remove()
1394: *
1395: * Remove the revision whose number is <rev> from the RCS file <rf>.
1396: */
1397: int
1398: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
1399: {
1400: int ret;
1401: struct rcs_delta *rdp;
1402:
1403: ret = 0;
1404: if (rev == RCS_HEAD_REV)
1405: rev = rf->rf_head;
1406:
1407: /* do we actually have that revision? */
1408: if ((rdp = rcs_findrev(rf, rev)) == NULL) {
1409: rcs_errno = RCS_ERR_NOENT;
1410: ret = -1;
1411: } else {
1412: /* XXX assumes it's not a sub node */
1413: TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
1414: rf->rf_ndelta--;
1415: rf->rf_flags &= ~RCS_SYNCED;
1416: }
1417:
1418: return (ret);
1419:
1420: }
1421:
1422: /*
1.1 jfb 1423: * rcs_findrev()
1424: *
1425: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1426: * The revision number is given in <rev>.
1427: * Returns a pointer to the delta on success, or NULL on failure.
1428: */
1429: static struct rcs_delta*
1.43 jfb 1430: rcs_findrev(RCSFILE *rfp, const RCSNUM *rev)
1.1 jfb 1431: {
1432: u_int cmplen;
1433: struct rcs_delta *rdp;
1434: struct rcs_dlist *hp;
1.6 vincent 1435: int found;
1.26 jfb 1436:
1.1 jfb 1437: cmplen = 2;
1438: hp = &(rfp->rf_delta);
1439:
1.6 vincent 1440: do {
1441: found = 0;
1442: TAILQ_FOREACH(rdp, hp, rd_list) {
1443: if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
1444: if (cmplen == rev->rn_len)
1445: return (rdp);
1.1 jfb 1446:
1.6 vincent 1447: hp = &(rdp->rd_snodes);
1448: cmplen += 2;
1449: found = 1;
1450: break;
1451: }
1.1 jfb 1452: }
1.6 vincent 1453: } while (found && cmplen < rev->rn_len);
1.1 jfb 1454:
1455: return (NULL);
1.20 jfb 1456: }
1457:
1458: /*
1.26 jfb 1459: * rcs_kwexp_set()
1460: *
1461: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1462: * Returns 0 on success, or -1 on failure.
1463: */
1464: int
1465: rcs_kwexp_set(RCSFILE *file, int mode)
1466: {
1467: int i;
1468: char *tmp, buf[8] = "";
1469:
1470: if (RCS_KWEXP_INVAL(mode))
1471: return (-1);
1472:
1473: i = 0;
1474: if (mode == RCS_KWEXP_NONE)
1475: buf[0] = 'b';
1476: else if (mode == RCS_KWEXP_OLD)
1477: buf[0] = 'o';
1478: else {
1479: if (mode & RCS_KWEXP_NAME)
1480: buf[i++] = 'k';
1481: if (mode & RCS_KWEXP_VAL)
1482: buf[i++] = 'v';
1483: if (mode & RCS_KWEXP_LKR)
1484: buf[i++] = 'l';
1485: }
1486:
1.39 joris 1487: if ((tmp = cvs_strdup(buf)) == NULL) {
1.26 jfb 1488: cvs_log(LP_ERRNO, "%s: failed to copy expansion mode",
1489: file->rf_path);
1490: return (-1);
1491: }
1492:
1.27 jfb 1493: if (file->rf_expand != NULL)
1.39 joris 1494: cvs_strfree(file->rf_expand);
1.26 jfb 1495: file->rf_expand = tmp;
1496:
1497: return (0);
1498: }
1499:
1500: /*
1501: * rcs_kwexp_get()
1502: *
1503: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1504: */
1505: int
1506: rcs_kwexp_get(RCSFILE *file)
1507: {
1508: return rcs_kflag_get(file->rf_expand);
1509: }
1510:
1511: /*
1.20 jfb 1512: * rcs_kflag_get()
1513: *
1514: * Get the keyword expansion mode from a set of character flags given in
1515: * <flags> and return the appropriate flag mask. In case of an error, the
1516: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1517: */
1518: int
1519: rcs_kflag_get(const char *flags)
1520: {
1521: int fl;
1522: size_t len;
1523: const char *fp;
1524:
1525: fl = 0;
1526: len = strlen(flags);
1527:
1528: for (fp = flags; *fp != '\0'; fp++) {
1529: if (*fp == 'k')
1530: fl |= RCS_KWEXP_NAME;
1531: else if (*fp == 'v')
1532: fl |= RCS_KWEXP_VAL;
1533: else if (*fp == 'l')
1534: fl |= RCS_KWEXP_LKR;
1535: else if (*fp == 'o') {
1536: if (len != 1)
1537: fl |= RCS_KWEXP_ERR;
1538: fl |= RCS_KWEXP_OLD;
1539: } else if (*fp == 'b') {
1540: if (len != 1)
1541: fl |= RCS_KWEXP_ERR;
1542: } else /* unknown letter */
1543: fl |= RCS_KWEXP_ERR;
1544: }
1545:
1546: return (fl);
1.32 jfb 1547: }
1548:
1549: /*
1550: * rcs_errstr()
1551: *
1552: * Get the error string matching the RCS error code <code>.
1553: */
1.57 xsa 1554: const char *
1.32 jfb 1555: rcs_errstr(int code)
1556: {
1.50 jfb 1557: const char *esp;
1558:
1559: if ((code < 0) || ((code >= (int)RCS_NERR) && (code != RCS_ERR_ERRNO)))
1560: esp = NULL;
1561: else if (code == RCS_ERR_ERRNO)
1562: esp = strerror(errno);
1563: else
1564: esp = rcs_errstrs[code];
1565: return (esp);
1.1 jfb 1566: }
1567:
1.21 jfb 1568: void
1569: rcs_kflag_usage(void)
1570: {
1571: fprintf(stderr, "Valid expansion modes include:\n"
1.22 jfb 1572: "\t-kkv\tGenerate keywords using the default form.\n"
1573: "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
1574: "\t-kk\tGenerate only keyword names in keyword strings.\n"
1575: "\t-kv\tGenerate only keyword values in keyword strings.\n"
1576: "\t-ko\tGenerate old keyword string "
1.21 jfb 1577: "(no changes from checked in file).\n"
1.22 jfb 1578: "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21 jfb 1579: }
1.1 jfb 1580:
1581: /*
1582: * rcs_parse()
1583: *
1584: * Parse the contents of file <path>, which are in the RCS format.
1585: * Returns 0 on success, or -1 on failure.
1586: */
1.26 jfb 1587: static int
1.1 jfb 1588: rcs_parse(RCSFILE *rfp)
1589: {
1590: int ret;
1591: struct rcs_pdata *pdp;
1592:
1.26 jfb 1593: if (rfp->rf_flags & RCS_PARSED)
1.1 jfb 1594: return (0);
1595:
1.26 jfb 1596: if ((pdp = (struct rcs_pdata *)malloc(sizeof(*pdp))) == NULL) {
1.50 jfb 1597: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1598: cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
1599: return (-1);
1600: }
1601: memset(pdp, 0, sizeof(*pdp));
1602:
1.18 jfb 1603: pdp->rp_lines = 0;
1.1 jfb 1604: pdp->rp_pttype = RCS_TOK_ERR;
1605:
1606: pdp->rp_file = fopen(rfp->rf_path, "r");
1607: if (pdp->rp_file == NULL) {
1.50 jfb 1608: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1609: cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
1610: rcs_freepdata(pdp);
1611: return (-1);
1612: }
1613:
1.59 xsa 1614: pdp->rp_buf = (char *)malloc((size_t)RCS_BUFSIZE);
1.1 jfb 1615: if (pdp->rp_buf == NULL) {
1.50 jfb 1616: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1617: cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
1618: rcs_freepdata(pdp);
1619: return (-1);
1620: }
1621: pdp->rp_blen = RCS_BUFSIZE;
1.18 jfb 1622: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1 jfb 1623:
1624: /* ditch the strict lock */
1.26 jfb 1625: rfp->rf_flags &= ~RCS_SLOCK;
1.1 jfb 1626: rfp->rf_pdata = pdp;
1627:
1.31 jfb 1628: if ((ret = rcs_parse_admin(rfp)) < 0) {
1.1 jfb 1629: rcs_freepdata(pdp);
1630: return (-1);
1.31 jfb 1631: } else if (ret == RCS_TOK_NUM) {
1632: for (;;) {
1633: ret = rcs_parse_delta(rfp);
1634: if (ret == 0)
1635: break;
1636: else if (ret == -1) {
1637: rcs_freepdata(pdp);
1638: return (-1);
1639: }
1.1 jfb 1640: }
1641: }
1642:
1643: ret = rcs_gettok(rfp);
1644: if (ret != RCS_TOK_DESC) {
1.50 jfb 1645: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1646: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1647: RCS_TOKSTR(rfp));
1648: rcs_freepdata(pdp);
1649: return (-1);
1650: }
1651:
1652: ret = rcs_gettok(rfp);
1653: if (ret != RCS_TOK_STRING) {
1.50 jfb 1654: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1655: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1656: RCS_TOKSTR(rfp));
1657: rcs_freepdata(pdp);
1658: return (-1);
1659: }
1660:
1.39 joris 1661: rfp->rf_desc = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1662: if (rfp->rf_desc == NULL) {
1663: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
1664: rcs_freepdata(pdp);
1665: return (-1);
1666: }
1.1 jfb 1667:
1668: for (;;) {
1669: ret = rcs_parse_deltatext(rfp);
1670: if (ret == 0)
1671: break;
1672: else if (ret == -1) {
1673: rcs_freepdata(pdp);
1674: return (-1);
1675: }
1676: }
1677:
1678: rcs_freepdata(pdp);
1679:
1680: rfp->rf_pdata = NULL;
1.26 jfb 1681: rfp->rf_flags |= RCS_PARSED | RCS_SYNCED;
1.1 jfb 1682:
1683: return (0);
1684: }
1685:
1686: /*
1687: * rcs_parse_admin()
1688: *
1689: * Parse the administrative portion of an RCS file.
1.31 jfb 1690: * Returns the type of the first token found after the admin section on
1691: * success, or -1 on failure.
1.1 jfb 1692: */
1693: static int
1694: rcs_parse_admin(RCSFILE *rfp)
1695: {
1696: u_int i;
1697: int tok, ntok, hmask;
1698: struct rcs_key *rk;
1699:
1700: /* hmask is a mask of the headers already encountered */
1701: hmask = 0;
1702: for (;;) {
1703: tok = rcs_gettok(rfp);
1704: if (tok == RCS_TOK_ERR) {
1.50 jfb 1705: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1706: cvs_log(LP_ERR, "parse error in RCS admin section");
1707: return (-1);
1.31 jfb 1708: } else if ((tok == RCS_TOK_NUM) || (tok == RCS_TOK_DESC)) {
1709: /*
1710: * Assume this is the start of the first delta or
1711: * that we are dealing with an empty RCS file and
1712: * we just found the description.
1713: */
1.1 jfb 1714: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31 jfb 1715: return (tok);
1.1 jfb 1716: }
1717:
1718: rk = NULL;
1.18 jfb 1719: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1720: if (rcs_keys[i].rk_id == tok)
1721: rk = &(rcs_keys[i]);
1722:
1723: if (hmask & (1 << tok)) {
1.50 jfb 1724: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1725: cvs_log(LP_ERR, "duplicate RCS key");
1726: return (-1);
1727: }
1728: hmask |= (1 << tok);
1729:
1730: switch (tok) {
1731: case RCS_TOK_HEAD:
1732: case RCS_TOK_BRANCH:
1733: case RCS_TOK_COMMENT:
1734: case RCS_TOK_EXPAND:
1735: ntok = rcs_gettok(rfp);
1736: if (ntok == RCS_TOK_SCOLON)
1737: break;
1738: if (ntok != rk->rk_val) {
1.50 jfb 1739: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1740: cvs_log(LP_ERR,
1741: "invalid value type for RCS key `%s'",
1742: rk->rk_str);
1743: }
1744:
1745: if (tok == RCS_TOK_HEAD) {
1.28 jfb 1746: if (rfp->rf_head == NULL) {
1747: rfp->rf_head = rcsnum_alloc();
1748: if (rfp->rf_head == NULL)
1749: return (-1);
1750: }
1.1 jfb 1751: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1752: rfp->rf_head);
1.14 deraadt 1753: } else if (tok == RCS_TOK_BRANCH) {
1.35 jfb 1754: if (rfp->rf_branch == NULL) {
1755: rfp->rf_branch = rcsnum_alloc();
1756: if (rfp->rf_branch == NULL)
1757: return (-1);
1758: }
1759: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1760: rfp->rf_branch) < 0)
1761: return (-1);
1.14 deraadt 1762: } else if (tok == RCS_TOK_COMMENT) {
1.39 joris 1763: rfp->rf_comment = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1764: if (rfp->rf_comment == NULL) {
1765: cvs_log(LP_ERRNO,
1766: "failed to duplicate rcs token");
1767: return (-1);
1768: }
1.14 deraadt 1769: } else if (tok == RCS_TOK_EXPAND) {
1.39 joris 1770: rfp->rf_expand = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1771: if (rfp->rf_expand == NULL) {
1772: cvs_log(LP_ERRNO,
1773: "failed to duplicate rcs token");
1774: return (-1);
1775: }
1.1 jfb 1776: }
1777:
1778: /* now get the expected semi-colon */
1779: ntok = rcs_gettok(rfp);
1780: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1781: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1782: cvs_log(LP_ERR,
1783: "missing semi-colon after RCS `%s' key",
1.26 jfb 1784: rk->rk_str);
1.1 jfb 1785: return (-1);
1786: }
1787: break;
1788: case RCS_TOK_ACCESS:
1.29 jfb 1789: if (rcs_parse_access(rfp) < 0)
1790: return (-1);
1.1 jfb 1791: break;
1792: case RCS_TOK_SYMBOLS:
1.29 jfb 1793: if (rcs_parse_symbols(rfp) < 0)
1794: return (-1);
1.1 jfb 1795: break;
1796: case RCS_TOK_LOCKS:
1.29 jfb 1797: if (rcs_parse_locks(rfp) < 0)
1798: return (-1);
1.1 jfb 1799: break;
1800: default:
1.50 jfb 1801: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1802: cvs_log(LP_ERR,
1803: "unexpected token `%s' in RCS admin section",
1804: RCS_TOKSTR(rfp));
1805: return (-1);
1806: }
1807: }
1808:
1809: return (0);
1810: }
1811:
1812: /*
1813: * rcs_parse_delta()
1814: *
1815: * Parse an RCS delta section and allocate the structure to store that delta's
1816: * information in the <rfp> delta list.
1817: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1818: * -1 on error.
1819: */
1820: static int
1821: rcs_parse_delta(RCSFILE *rfp)
1822: {
1823: int ret, tok, ntok, hmask;
1824: u_int i;
1825: char *tokstr;
1.3 vincent 1826: RCSNUM *datenum;
1.1 jfb 1827: struct rcs_delta *rdp;
1828: struct rcs_key *rk;
1829:
1830: rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
1831: if (rdp == NULL) {
1.50 jfb 1832: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1833: cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
1834: return (-1);
1835: }
1836: memset(rdp, 0, sizeof(*rdp));
1837:
1838: rdp->rd_num = rcsnum_alloc();
1.11 joris 1839: if (rdp->rd_num == NULL) {
1840: rcs_freedelta(rdp);
1841: return (-1);
1842: }
1.1 jfb 1843: rdp->rd_next = rcsnum_alloc();
1.11 joris 1844: if (rdp->rd_next == NULL) {
1845: rcs_freedelta(rdp);
1846: return (-1);
1847: }
1.1 jfb 1848:
1849: TAILQ_INIT(&(rdp->rd_branches));
1.52 jfb 1850: TAILQ_INIT(&(rdp->rd_snodes));
1.1 jfb 1851:
1852: tok = rcs_gettok(rfp);
1853: if (tok != RCS_TOK_NUM) {
1.52 jfb 1854: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1855: cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
1856: RCS_TOKSTR(rfp));
1857: rcs_freedelta(rdp);
1858: return (-1);
1859: }
1860: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1861:
1862: hmask = 0;
1863: ret = 0;
1864: tokstr = NULL;
1865:
1866: for (;;) {
1867: tok = rcs_gettok(rfp);
1868: if (tok == RCS_TOK_ERR) {
1.50 jfb 1869: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1870: cvs_log(LP_ERR, "parse error in RCS delta section");
1871: rcs_freedelta(rdp);
1872: return (-1);
1.14 deraadt 1873: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15 tedu 1874: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1 jfb 1875: ret = (tok == RCS_TOK_NUM ? 1 : 0);
1876: break;
1877: }
1878:
1879: rk = NULL;
1.18 jfb 1880: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1881: if (rcs_keys[i].rk_id == tok)
1882: rk = &(rcs_keys[i]);
1883:
1884: if (hmask & (1 << tok)) {
1.50 jfb 1885: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1886: cvs_log(LP_ERR, "duplicate RCS key");
1887: rcs_freedelta(rdp);
1888: return (-1);
1889: }
1890: hmask |= (1 << tok);
1891:
1892: switch (tok) {
1893: case RCS_TOK_DATE:
1894: case RCS_TOK_AUTHOR:
1895: case RCS_TOK_STATE:
1896: case RCS_TOK_NEXT:
1897: ntok = rcs_gettok(rfp);
1898: if (ntok == RCS_TOK_SCOLON) {
1899: if (rk->rk_flags & RCS_VOPT)
1900: break;
1901: else {
1.50 jfb 1902: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1903: cvs_log(LP_ERR, "missing mandatory "
1904: "value to RCS key `%s'",
1905: rk->rk_str);
1906: rcs_freedelta(rdp);
1907: return (-1);
1908: }
1909: }
1910:
1911: if (ntok != rk->rk_val) {
1.50 jfb 1912: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1913: cvs_log(LP_ERR,
1914: "invalid value type for RCS key `%s'",
1915: rk->rk_str);
1916: rcs_freedelta(rdp);
1917: return (-1);
1918: }
1919:
1920: if (tokstr != NULL)
1.41 jfb 1921: cvs_strfree(tokstr);
1.39 joris 1922: tokstr = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1923: if (tokstr == NULL) {
1.15 tedu 1924: cvs_log(LP_ERRNO,
1.10 joris 1925: "failed to duplicate rcs token");
1926: rcs_freedelta(rdp);
1927: return (-1);
1928: }
1.1 jfb 1929:
1930: /* now get the expected semi-colon */
1931: ntok = rcs_gettok(rfp);
1932: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1933: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1934: cvs_log(LP_ERR,
1935: "missing semi-colon after RCS `%s' key",
1.26 jfb 1936: rk->rk_str);
1.41 jfb 1937: cvs_strfree(tokstr);
1.1 jfb 1938: rcs_freedelta(rdp);
1939: return (-1);
1940: }
1941:
1942: if (tok == RCS_TOK_DATE) {
1.25 jfb 1943: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.41 jfb 1944: cvs_strfree(tokstr);
1.11 joris 1945: rcs_freedelta(rdp);
1946: return (-1);
1947: }
1.3 vincent 1948: if (datenum->rn_len != 6) {
1.50 jfb 1949: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1950: cvs_log(LP_ERR,
1951: "RCS date specification has %s "
1952: "fields",
1.3 vincent 1953: (datenum->rn_len > 6) ? "too many" :
1.1 jfb 1954: "missing");
1.41 jfb 1955: cvs_strfree(tokstr);
1.1 jfb 1956: rcs_freedelta(rdp);
1.37 tedu 1957: rcsnum_free(datenum);
1958: return (-1);
1.1 jfb 1959: }
1.3 vincent 1960: rdp->rd_date.tm_year = datenum->rn_id[0];
1.19 jfb 1961: if (rdp->rd_date.tm_year >= 1900)
1962: rdp->rd_date.tm_year -= 1900;
1.3 vincent 1963: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
1964: rdp->rd_date.tm_mday = datenum->rn_id[2];
1965: rdp->rd_date.tm_hour = datenum->rn_id[3];
1966: rdp->rd_date.tm_min = datenum->rn_id[4];
1967: rdp->rd_date.tm_sec = datenum->rn_id[5];
1968: rcsnum_free(datenum);
1.14 deraadt 1969: } else if (tok == RCS_TOK_AUTHOR) {
1.1 jfb 1970: rdp->rd_author = tokstr;
1971: tokstr = NULL;
1.14 deraadt 1972: } else if (tok == RCS_TOK_STATE) {
1.1 jfb 1973: rdp->rd_state = tokstr;
1974: tokstr = NULL;
1.14 deraadt 1975: } else if (tok == RCS_TOK_NEXT) {
1.1 jfb 1976: rcsnum_aton(tokstr, NULL, rdp->rd_next);
1977: }
1978: break;
1979: case RCS_TOK_BRANCHES:
1.46 jfb 1980: if (rcs_parse_branches(rfp, rdp) < 0) {
1981: rcs_freedelta(rdp);
1982: return (-1);
1983: }
1.1 jfb 1984: break;
1985: default:
1.50 jfb 1986: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1987: cvs_log(LP_ERR,
1988: "unexpected token `%s' in RCS delta",
1989: RCS_TOKSTR(rfp));
1990: rcs_freedelta(rdp);
1991: return (-1);
1992: }
1993: }
1994:
1.13 jfb 1995: if (tokstr != NULL)
1.39 joris 1996: cvs_strfree(tokstr);
1.13 jfb 1997:
1.1 jfb 1998: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26 jfb 1999: rfp->rf_ndelta++;
1.1 jfb 2000:
2001: return (ret);
2002: }
2003:
2004: /*
2005: * rcs_parse_deltatext()
2006: *
2007: * Parse an RCS delta text section and fill in the log and text field of the
2008: * appropriate delta section.
2009: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
2010: * -1 on error.
2011: */
2012: static int
2013: rcs_parse_deltatext(RCSFILE *rfp)
2014: {
2015: int tok;
2016: RCSNUM *tnum;
2017: struct rcs_delta *rdp;
2018:
2019: tok = rcs_gettok(rfp);
2020: if (tok == RCS_TOK_EOF)
2021: return (0);
2022:
2023: if (tok != RCS_TOK_NUM) {
1.50 jfb 2024: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2025: cvs_log(LP_ERR,
2026: "unexpected token `%s' at start of RCS delta text",
2027: RCS_TOKSTR(rfp));
2028: return (-1);
2029: }
1.13 jfb 2030:
2031: tnum = rcsnum_alloc();
2032: if (tnum == NULL)
2033: return (-1);
1.1 jfb 2034: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
2035:
2036: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
2037: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
2038: break;
2039: }
1.13 jfb 2040: rcsnum_free(tnum);
2041:
1.1 jfb 2042: if (rdp == NULL) {
2043: cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
2044: RCS_TOKSTR(rfp));
2045: return (-1);
2046: }
2047:
2048: tok = rcs_gettok(rfp);
2049: if (tok != RCS_TOK_LOG) {
1.50 jfb 2050: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2051: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
2052: RCS_TOKSTR(rfp));
2053: return (-1);
2054: }
2055:
2056: tok = rcs_gettok(rfp);
2057: if (tok != RCS_TOK_STRING) {
1.50 jfb 2058: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2059: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
2060: RCS_TOKSTR(rfp));
2061: return (-1);
2062: }
1.39 joris 2063: rdp->rd_log = cvs_strdup(RCS_TOKSTR(rfp));
1.1 jfb 2064: if (rdp->rd_log == NULL) {
2065: cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
2066: return (-1);
2067: }
2068:
2069: tok = rcs_gettok(rfp);
2070: if (tok != RCS_TOK_TEXT) {
1.50 jfb 2071: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2072: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2073: RCS_TOKSTR(rfp));
2074: return (-1);
2075: }
2076:
2077: tok = rcs_gettok(rfp);
2078: if (tok != RCS_TOK_STRING) {
1.50 jfb 2079: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2080: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2081: RCS_TOKSTR(rfp));
2082: return (-1);
2083: }
2084:
1.45 jfb 2085: rdp->rd_text = (u_char *)malloc(RCS_TOKLEN(rfp));
1.1 jfb 2086: if (rdp->rd_text == NULL) {
2087: cvs_log(LP_ERRNO, "failed to copy RCS delta text");
2088: return (-1);
2089: }
1.45 jfb 2090: memcpy(rdp->rd_text, RCS_TOKSTR(rfp), RCS_TOKLEN(rfp));
1.42 jfb 2091: rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1 jfb 2092:
2093: return (1);
2094: }
2095:
2096: /*
2097: * rcs_parse_access()
2098: *
2099: * Parse the access list given as value to the `access' keyword.
2100: * Returns 0 on success, or -1 on failure.
2101: */
2102: static int
2103: rcs_parse_access(RCSFILE *rfp)
2104: {
2105: int type;
2106:
2107: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
2108: if (type != RCS_TOK_ID) {
1.50 jfb 2109: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2110: cvs_log(LP_ERR, "unexpected token `%s' in access list",
2111: RCS_TOKSTR(rfp));
2112: return (-1);
2113: }
1.29 jfb 2114:
2115: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
2116: return (-1);
1.1 jfb 2117: }
2118:
2119: return (0);
2120: }
2121:
2122: /*
2123: * rcs_parse_symbols()
2124: *
2125: * Parse the symbol list given as value to the `symbols' keyword.
2126: * Returns 0 on success, or -1 on failure.
2127: */
2128: static int
2129: rcs_parse_symbols(RCSFILE *rfp)
2130: {
2131: int type;
2132: struct rcs_sym *symp;
2133:
2134: for (;;) {
2135: type = rcs_gettok(rfp);
2136: if (type == RCS_TOK_SCOLON)
2137: break;
2138:
1.41 jfb 2139: if (type != RCS_TOK_ID) {
1.50 jfb 2140: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2141: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2142: RCS_TOKSTR(rfp));
2143: return (-1);
2144: }
2145:
2146: symp = (struct rcs_sym *)malloc(sizeof(*symp));
2147: if (symp == NULL) {
1.50 jfb 2148: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 2149: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
2150: return (-1);
2151: }
1.39 joris 2152: symp->rs_name = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 2153: if (symp->rs_name == NULL) {
2154: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
2155: free(symp);
2156: return (-1);
2157: }
2158:
1.1 jfb 2159: symp->rs_num = rcsnum_alloc();
1.11 joris 2160: if (symp->rs_num == NULL) {
2161: cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
1.39 joris 2162: cvs_strfree(symp->rs_name);
1.11 joris 2163: free(symp);
2164: return (-1);
2165: }
1.1 jfb 2166:
2167: type = rcs_gettok(rfp);
2168: if (type != RCS_TOK_COLON) {
1.50 jfb 2169: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2170: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2171: RCS_TOKSTR(rfp));
1.11 joris 2172: rcsnum_free(symp->rs_num);
1.39 joris 2173: cvs_strfree(symp->rs_name);
1.1 jfb 2174: free(symp);
2175: return (-1);
2176: }
2177:
2178: type = rcs_gettok(rfp);
2179: if (type != RCS_TOK_NUM) {
1.50 jfb 2180: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2181: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2182: RCS_TOKSTR(rfp));
1.11 joris 2183: rcsnum_free(symp->rs_num);
1.39 joris 2184: cvs_strfree(symp->rs_name);
1.1 jfb 2185: free(symp);
2186: return (-1);
2187: }
2188:
2189: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
2190: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2191: RCS_TOKSTR(rfp));
1.11 joris 2192: rcsnum_free(symp->rs_num);
1.39 joris 2193: cvs_strfree(symp->rs_name);
1.1 jfb 2194: free(symp);
2195: return (-1);
2196: }
2197:
1.43 jfb 2198: TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1 jfb 2199: }
2200:
2201: return (0);
2202: }
2203:
2204: /*
2205: * rcs_parse_locks()
2206: *
2207: * Parse the lock list given as value to the `locks' keyword.
2208: * Returns 0 on success, or -1 on failure.
2209: */
2210: static int
2211: rcs_parse_locks(RCSFILE *rfp)
2212: {
2213: int type;
2214: struct rcs_lock *lkp;
2215:
2216: for (;;) {
2217: type = rcs_gettok(rfp);
2218: if (type == RCS_TOK_SCOLON)
2219: break;
2220:
2221: if (type != RCS_TOK_ID) {
1.50 jfb 2222: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2223: cvs_log(LP_ERR, "unexpected token `%s' in lock list",
2224: RCS_TOKSTR(rfp));
2225: return (-1);
2226: }
2227:
2228: lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
2229: if (lkp == NULL) {
2230: cvs_log(LP_ERRNO, "failed to allocate RCS lock");
2231: return (-1);
2232: }
2233: lkp->rl_num = rcsnum_alloc();
1.11 joris 2234: if (lkp->rl_num == NULL) {
2235: free(lkp);
2236: return (-1);
2237: }
1.1 jfb 2238:
2239: type = rcs_gettok(rfp);
2240: if (type != RCS_TOK_COLON) {
1.50 jfb 2241: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2242: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2243: RCS_TOKSTR(rfp));
1.37 tedu 2244: rcsnum_free(lkp->rl_num);
1.1 jfb 2245: free(lkp);
2246: return (-1);
2247: }
2248:
2249: type = rcs_gettok(rfp);
2250: if (type != RCS_TOK_NUM) {
1.50 jfb 2251: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2252: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2253: RCS_TOKSTR(rfp));
1.37 tedu 2254: rcsnum_free(lkp->rl_num);
1.1 jfb 2255: free(lkp);
2256: return (-1);
2257: }
2258:
2259: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
2260: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2261: RCS_TOKSTR(rfp));
1.37 tedu 2262: rcsnum_free(lkp->rl_num);
1.1 jfb 2263: free(lkp);
2264: return (-1);
2265: }
2266:
2267: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
2268: }
2269:
2270: /* check if we have a `strict' */
2271: type = rcs_gettok(rfp);
2272: if (type != RCS_TOK_STRICT) {
2273: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14 deraadt 2274: } else {
1.26 jfb 2275: rfp->rf_flags |= RCS_SLOCK;
1.1 jfb 2276:
2277: type = rcs_gettok(rfp);
2278: if (type != RCS_TOK_SCOLON) {
1.50 jfb 2279: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2280: cvs_log(LP_ERR,
2281: "missing semi-colon after `strict' keyword");
2282: return (-1);
2283: }
2284: }
2285:
2286: return (0);
2287: }
2288:
2289: /*
2290: * rcs_parse_branches()
2291: *
2292: * Parse the list of branches following a `branches' keyword in a delta.
2293: * Returns 0 on success, or -1 on failure.
2294: */
2295: static int
2296: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
2297: {
2298: int type;
2299: struct rcs_branch *brp;
2300:
2301: for (;;) {
2302: type = rcs_gettok(rfp);
2303: if (type == RCS_TOK_SCOLON)
2304: break;
2305:
2306: if (type != RCS_TOK_NUM) {
1.50 jfb 2307: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2308: cvs_log(LP_ERR,
2309: "unexpected token `%s' in list of branches",
2310: RCS_TOKSTR(rfp));
2311: return (-1);
2312: }
2313:
2314: brp = (struct rcs_branch *)malloc(sizeof(*brp));
2315: if (brp == NULL) {
1.50 jfb 2316: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 2317: cvs_log(LP_ERRNO, "failed to allocate RCS branch");
2318: return (-1);
2319: }
1.46 jfb 2320: brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11 joris 2321: if (brp->rb_num == NULL) {
2322: free(brp);
2323: return (-1);
2324: }
1.1 jfb 2325:
2326: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
2327: }
2328:
2329: return (0);
2330: }
2331:
2332: /*
2333: * rcs_freedelta()
2334: *
2335: * Free the contents of a delta structure.
2336: */
1.18 jfb 2337: static void
1.1 jfb 2338: rcs_freedelta(struct rcs_delta *rdp)
2339: {
1.12 jfb 2340: struct rcs_branch *rb;
1.1 jfb 2341: struct rcs_delta *crdp;
2342:
1.12 jfb 2343: if (rdp->rd_num != NULL)
2344: rcsnum_free(rdp->rd_num);
2345: if (rdp->rd_next != NULL)
2346: rcsnum_free(rdp->rd_next);
2347:
1.1 jfb 2348: if (rdp->rd_author != NULL)
1.39 joris 2349: cvs_strfree(rdp->rd_author);
1.1 jfb 2350: if (rdp->rd_state != NULL)
1.39 joris 2351: cvs_strfree(rdp->rd_state);
1.1 jfb 2352: if (rdp->rd_log != NULL)
1.39 joris 2353: cvs_strfree(rdp->rd_log);
1.1 jfb 2354: if (rdp->rd_text != NULL)
1.45 jfb 2355: free(rdp->rd_text);
1.12 jfb 2356:
2357: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
2358: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
2359: rcsnum_free(rb->rb_num);
2360: free(rb);
2361: }
1.1 jfb 2362:
2363: while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
2364: TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
2365: rcs_freedelta(crdp);
2366: }
2367:
2368: free(rdp);
2369: }
2370:
2371: /*
2372: * rcs_freepdata()
2373: *
2374: * Free the contents of the parser data structure.
2375: */
2376: static void
2377: rcs_freepdata(struct rcs_pdata *pd)
2378: {
2379: if (pd->rp_file != NULL)
2380: (void)fclose(pd->rp_file);
2381: if (pd->rp_buf != NULL)
2382: free(pd->rp_buf);
2383: free(pd);
2384: }
2385:
2386: /*
2387: * rcs_gettok()
2388: *
2389: * Get the next RCS token from the string <str>.
2390: */
2391: static int
2392: rcs_gettok(RCSFILE *rfp)
2393: {
2394: u_int i;
2395: int ch, last, type;
1.18 jfb 2396: size_t len;
2397: char *bp;
1.1 jfb 2398: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2399:
2400: type = RCS_TOK_ERR;
2401: bp = pdp->rp_buf;
1.42 jfb 2402: pdp->rp_tlen = 0;
1.1 jfb 2403: *bp = '\0';
2404:
2405: if (pdp->rp_pttype != RCS_TOK_ERR) {
2406: type = pdp->rp_pttype;
2407: strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
2408: pdp->rp_pttype = RCS_TOK_ERR;
2409: return (type);
2410: }
2411:
2412: /* skip leading whitespace */
2413: /* XXX we must skip backspace too for compatibility, should we? */
2414: do {
2415: ch = getc(pdp->rp_file);
2416: if (ch == '\n')
1.18 jfb 2417: pdp->rp_lines++;
1.1 jfb 2418: } while (isspace(ch));
2419:
2420: if (ch == EOF) {
2421: type = RCS_TOK_EOF;
1.14 deraadt 2422: } else if (ch == ';') {
1.1 jfb 2423: type = RCS_TOK_SCOLON;
1.14 deraadt 2424: } else if (ch == ':') {
1.1 jfb 2425: type = RCS_TOK_COLON;
1.14 deraadt 2426: } else if (isalpha(ch)) {
1.31 jfb 2427: type = RCS_TOK_ID;
1.1 jfb 2428: *(bp++) = ch;
1.18 jfb 2429: for (;;) {
1.1 jfb 2430: ch = getc(pdp->rp_file);
1.11 joris 2431: if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1 jfb 2432: ungetc(ch, pdp->rp_file);
2433: break;
2434: }
2435: *(bp++) = ch;
1.42 jfb 2436: pdp->rp_tlen++;
1.18 jfb 2437: if (bp == pdp->rp_bufend - 1) {
2438: len = bp - pdp->rp_buf;
2439: if (rcs_growbuf(rfp) < 0) {
2440: type = RCS_TOK_ERR;
2441: break;
2442: }
2443: bp = pdp->rp_buf + len;
2444: }
1.1 jfb 2445: }
2446: *bp = '\0';
2447:
1.18 jfb 2448: if (type != RCS_TOK_ERR) {
2449: for (i = 0; i < RCS_NKEYS; i++) {
2450: if (strcmp(rcs_keys[i].rk_str,
2451: pdp->rp_buf) == 0) {
2452: type = rcs_keys[i].rk_id;
2453: break;
2454: }
1.1 jfb 2455: }
2456: }
1.14 deraadt 2457: } else if (ch == '@') {
1.1 jfb 2458: /* we have a string */
1.18 jfb 2459: type = RCS_TOK_STRING;
1.1 jfb 2460: for (;;) {
2461: ch = getc(pdp->rp_file);
2462: if (ch == '@') {
2463: ch = getc(pdp->rp_file);
2464: if (ch != '@') {
2465: ungetc(ch, pdp->rp_file);
2466: break;
2467: }
1.14 deraadt 2468: } else if (ch == '\n')
1.18 jfb 2469: pdp->rp_lines++;
1.1 jfb 2470:
2471: *(bp++) = ch;
1.42 jfb 2472: pdp->rp_tlen++;
1.18 jfb 2473: if (bp == pdp->rp_bufend - 1) {
2474: len = bp - pdp->rp_buf;
2475: if (rcs_growbuf(rfp) < 0) {
2476: type = RCS_TOK_ERR;
2477: break;
2478: }
2479: bp = pdp->rp_buf + len;
2480: }
1.1 jfb 2481: }
2482:
2483: *bp = '\0';
1.14 deraadt 2484: } else if (isdigit(ch)) {
1.1 jfb 2485: *(bp++) = ch;
2486: last = ch;
2487: type = RCS_TOK_NUM;
2488:
2489: for (;;) {
2490: ch = getc(pdp->rp_file);
1.18 jfb 2491: if (bp == pdp->rp_bufend)
1.1 jfb 2492: break;
2493: if (!isdigit(ch) && ch != '.') {
2494: ungetc(ch, pdp->rp_file);
2495: break;
2496: }
2497:
2498: if (last == '.' && ch == '.') {
2499: type = RCS_TOK_ERR;
2500: break;
2501: }
2502: last = ch;
2503: *(bp++) = ch;
1.42 jfb 2504: pdp->rp_tlen++;
1.1 jfb 2505: }
1.18 jfb 2506: *bp = '\0';
1.1 jfb 2507: }
2508:
2509: return (type);
2510: }
2511:
2512: /*
2513: * rcs_pushtok()
2514: *
2515: * Push a token back in the parser's token buffer.
2516: */
2517: static int
2518: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2519: {
2520: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2521:
2522: if (pdp->rp_pttype != RCS_TOK_ERR)
2523: return (-1);
2524:
2525: pdp->rp_pttype = type;
2526: strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
2527: return (0);
2528: }
2529:
2530:
2531: /*
2532: * rcs_splitlines()
2533: *
2534: * Split the contents of a file into a list of lines.
2535: */
2536: static struct rcs_foo*
2537: rcs_splitlines(const char *fcont)
2538: {
2539: char *dcp;
2540: struct rcs_foo *foo;
2541: struct rcs_line *lp;
2542:
2543: foo = (struct rcs_foo *)malloc(sizeof(*foo));
2544: if (foo == NULL) {
2545: cvs_log(LP_ERR, "failed to allocate line structure");
2546: return (NULL);
2547: }
2548: TAILQ_INIT(&(foo->rl_lines));
2549: foo->rl_nblines = 0;
2550: foo->rl_data = strdup(fcont);
2551: if (foo->rl_data == NULL) {
2552: cvs_log(LP_ERRNO, "failed to copy file contents");
2553: free(foo);
2554: return (NULL);
2555: }
2556:
2557: /*
2558: * Add a first bogus line with line number 0. This is used so we
2559: * can position the line pointer before 1 when changing the first line
2560: * in rcs_patch().
2561: */
2562: lp = (struct rcs_line *)malloc(sizeof(*lp));
1.38 joris 2563: if (lp == NULL) {
2564: rcs_freefoo(foo);
1.1 jfb 2565: return (NULL);
1.38 joris 2566: }
1.5 vincent 2567:
1.1 jfb 2568: lp->rl_line = NULL;
2569: lp->rl_lineno = 0;
2570: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2571:
2572:
2573: for (dcp = foo->rl_data; *dcp != '\0';) {
2574: lp = (struct rcs_line *)malloc(sizeof(*lp));
2575: if (lp == NULL) {
1.38 joris 2576: rcs_freefoo(foo);
1.1 jfb 2577: cvs_log(LP_ERR, "failed to allocate line entry");
2578: return (NULL);
2579: }
2580:
2581: lp->rl_line = dcp;
2582: lp->rl_lineno = ++(foo->rl_nblines);
2583: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2584:
2585: dcp = strchr(dcp, '\n');
2586: if (dcp == NULL) {
2587: break;
2588: }
2589: *(dcp++) = '\0';
2590: }
2591:
2592: return (foo);
1.5 vincent 2593: }
2594:
2595: static void
2596: rcs_freefoo(struct rcs_foo *fp)
2597: {
2598: struct rcs_line *lp;
2599:
2600: while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
2601: TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
2602: free(lp);
2603: }
2604: free(fp->rl_data);
2605: free(fp);
1.18 jfb 2606: }
2607:
2608: /*
2609: * rcs_growbuf()
2610: *
2611: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2612: * RCS_BUFEXTSIZE.
2613: * In case of failure, the original buffer is left unmodified.
2614: * Returns 0 on success, or -1 on failure.
2615: */
2616: static int
2617: rcs_growbuf(RCSFILE *rf)
2618: {
2619: void *tmp;
2620: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2621:
2622: tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
2623: if (tmp == NULL) {
1.50 jfb 2624: rcs_errno = RCS_ERR_ERRNO;
1.18 jfb 2625: cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
2626: return (-1);
2627: }
2628:
2629: pdp->rp_buf = (char *)tmp;
2630: pdp->rp_blen += RCS_BUFEXTSIZE;
2631: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42 jfb 2632:
2633: return (0);
2634: }
2635:
2636: /*
2637: * rcs_strprint()
2638: *
2639: * Output an RCS string <str> of size <slen> to the stream <stream>. Any
2640: * '@' characters are escaped. Otherwise, the string can contain arbitrary
2641: * binary data.
2642: */
2643: static int
2644: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
2645: {
2646: const u_char *ap, *ep, *sp;
2647: size_t ret;
1.52 jfb 2648:
2649: if (slen == 0)
2650: return (0);
1.42 jfb 2651:
2652: ep = str + slen - 1;
2653:
2654: for (sp = str; sp <= ep;) {
2655: ap = memchr(sp, '@', ep - sp);
2656: if (ap == NULL)
2657: ap = ep;
2658: ret = fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
2659:
2660: if (*ap == '@')
2661: putc('@', stream);
2662: sp = ap + 1;
1.63 ! joris 2663: }
! 2664:
! 2665: return (0);
! 2666: }
! 2667:
! 2668: /*
! 2669: * rcs_expand_keywords()
! 2670: *
! 2671: * Expand any RCS keywords in <line> into <out>
! 2672: */
! 2673: static int
! 2674: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *line, char *out,
! 2675: size_t len, int mode)
! 2676: {
! 2677: int kwtype;
! 2678: u_int i, j, found;
! 2679: char *c, *kwstr, *start;
! 2680: char expbuf[128], buf[128];
! 2681:
! 2682: i = 0;
! 2683:
! 2684: /*
! 2685: * Keyword formats:
! 2686: * $Keyword$
! 2687: * $Keyword: value$
! 2688: */
! 2689: memset(out, '\0', len);
! 2690: for (c = line; *c != '\0' && i < len; *c++) {
! 2691: out[i++] = *c;
! 2692: if (*c == '$') {
! 2693: /* remember start of this possible keyword */
! 2694: start = c;
! 2695:
! 2696: /* first following character has to be alphanumeric */
! 2697: *c++;
! 2698: if (!isalpha(*c)) {
! 2699: c = start;
! 2700: continue;
! 2701: }
! 2702:
! 2703: /* look for any matching keywords */
! 2704: found = 0;
! 2705: for (j = 0; j < RCS_NKWORDS; j++) {
! 2706: if (!strncmp(c, rcs_expkw[j].kw_str,
! 2707: strlen(rcs_expkw[j].kw_str))) {
! 2708: found = 1;
! 2709: kwstr = rcs_expkw[j].kw_str;
! 2710: kwtype = rcs_expkw[j].kw_type;
! 2711: break;
! 2712: }
! 2713: }
! 2714:
! 2715: /* unknown keyword, continue looking */
! 2716: if (found == 0) {
! 2717: c = start;
! 2718: continue;
! 2719: }
! 2720:
! 2721: /* next character has to be ':' or '$' */
! 2722: c += strlen(kwstr);
! 2723: if (*c != ':' && *c != '$') {
! 2724: c = start;
! 2725: continue;
! 2726: }
! 2727:
! 2728: /*
! 2729: * if the next character was ':' we need to look for
! 2730: * an '$' before the end of the line to be sure it is
! 2731: * in fact a keyword.
! 2732: */
! 2733: if (*c == ':') {
! 2734: while (*c++) {
! 2735: if (*c == '$' || *c == '\n')
! 2736: break;
! 2737: }
! 2738:
! 2739: if (*c != '$') {
! 2740: c = start;
! 2741: continue;
! 2742: }
! 2743: }
! 2744:
! 2745: /* start constructing the expansion */
! 2746: expbuf[0] = '\0';
! 2747:
! 2748: if (mode & RCS_KWEXP_NAME) {
! 2749: strlcat(expbuf, "$", sizeof(expbuf));
! 2750: strlcat(expbuf, kwstr, sizeof(expbuf));
! 2751: if (mode & RCS_KWEXP_VAL)
! 2752: strlcat(expbuf, ": ", sizeof(expbuf));
! 2753: }
! 2754:
! 2755: /*
! 2756: * order matters because of RCS_KW_ID and RCS_KW_HEADER here
! 2757: */
! 2758: if (mode & RCS_KWEXP_VAL) {
! 2759: if (kwtype & RCS_KW_RCSFILE) {
! 2760: if (!(kwtype & RCS_KW_FULLPATH))
! 2761: strlcat(expbuf, basename(rcsfile),
! 2762: sizeof(expbuf));
! 2763: else
! 2764: strlcat(expbuf, rcsfile,
! 2765: sizeof(expbuf));
! 2766: strlcat(expbuf, " ", sizeof(expbuf));
! 2767: }
! 2768:
! 2769: if (kwtype & RCS_KW_REVISION) {
! 2770: rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
! 2771: strlcat(buf, " ", sizeof(buf));
! 2772: strlcat(expbuf, buf, sizeof(expbuf));
! 2773: }
! 2774:
! 2775: if (kwtype & RCS_KW_DATE) {
! 2776: strftime(buf, sizeof(buf),
! 2777: "%Y/%m/%d %H:%M:%S ", &rdp->rd_date);
! 2778: strlcat(expbuf, buf, sizeof(expbuf));
! 2779: }
! 2780:
! 2781: if (kwtype & RCS_KW_AUTHOR) {
! 2782: strlcat(expbuf, rdp->rd_author,
! 2783: sizeof(expbuf));
! 2784: strlcat(expbuf, " ", sizeof(expbuf));
! 2785: }
! 2786:
! 2787: if (kwtype & RCS_KW_STATE) {
! 2788: strlcat(expbuf, rdp->rd_state,
! 2789: sizeof(expbuf));
! 2790: strlcat(expbuf, " ", sizeof(expbuf));
! 2791: }
! 2792:
! 2793: /* order does not matter anymore below */
! 2794: if (kwtype & RCS_KW_LOG)
! 2795: strlcat(expbuf, " ", sizeof(expbuf));
! 2796:
! 2797: if (kwtype & RCS_KW_SOURCE) {
! 2798: strlcat(expbuf, rcsfile, sizeof(expbuf));
! 2799: strlcat(expbuf, " ", sizeof(expbuf));
! 2800: }
! 2801:
! 2802: if (kwtype & RCS_KW_NAME)
! 2803: strlcat(expbuf, " ", sizeof(expbuf));
! 2804: }
! 2805:
! 2806: /* end the expansion */
! 2807: if (mode & RCS_KWEXP_NAME)
! 2808: strlcat(expbuf, "$", sizeof(expbuf));
! 2809:
! 2810: out[--i] = '\0';
! 2811: strlcat(out, expbuf, len);
! 2812: i += strlen(expbuf);
! 2813: }
1.42 jfb 2814: }
1.18 jfb 2815:
2816: return (0);
1.1 jfb 2817: }