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