Annotation of src/usr.bin/cvs/rcs.c, Revision 1.86
1.86 ! joris 1: /* $OpenBSD: rcs.c,v 1.85 2005/10/10 14:42:54 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.81 niallo 43: #include "diff.h"
1.39 joris 44: #include "strtab.h"
1.1 jfb 45:
1.57 xsa 46: #define RCS_BUFSIZE 16384
47: #define RCS_BUFEXTSIZE 8192
1.1 jfb 48:
49:
50: /* RCS token types */
1.57 xsa 51: #define RCS_TOK_ERR -1
52: #define RCS_TOK_EOF 0
53: #define RCS_TOK_NUM 1
54: #define RCS_TOK_ID 2
55: #define RCS_TOK_STRING 3
56: #define RCS_TOK_SCOLON 4
57: #define RCS_TOK_COLON 5
58:
59:
60: #define RCS_TOK_HEAD 8
61: #define RCS_TOK_BRANCH 9
62: #define RCS_TOK_ACCESS 10
63: #define RCS_TOK_SYMBOLS 11
64: #define RCS_TOK_LOCKS 12
65: #define RCS_TOK_COMMENT 13
66: #define RCS_TOK_EXPAND 14
67: #define RCS_TOK_DATE 15
68: #define RCS_TOK_AUTHOR 16
69: #define RCS_TOK_STATE 17
70: #define RCS_TOK_NEXT 18
71: #define RCS_TOK_BRANCHES 19
72: #define RCS_TOK_DESC 20
73: #define RCS_TOK_LOG 21
74: #define RCS_TOK_TEXT 22
75: #define RCS_TOK_STRICT 23
1.1 jfb 76:
1.57 xsa 77: #define RCS_ISKEY(t) (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
1.1 jfb 78:
79:
1.57 xsa 80: #define RCS_NOSCOL 0x01 /* no terminating semi-colon */
81: #define RCS_VOPT 0x02 /* value is optional */
1.1 jfb 82:
83:
84: /* opaque parse data */
85: struct rcs_pdata {
1.57 xsa 86: u_int rp_lines;
1.1 jfb 87:
1.57 xsa 88: char *rp_buf;
89: size_t rp_blen;
90: char *rp_bufend;
91: size_t rp_tlen;
1.1 jfb 92:
93: /* pushback token buffer */
1.57 xsa 94: char rp_ptok[128];
95: int rp_pttype; /* token type, RCS_TOK_ERR if no token */
1.1 jfb 96:
1.57 xsa 97: FILE *rp_file;
1.1 jfb 98: };
99:
100:
101: struct rcs_line {
1.57 xsa 102: char *rl_line;
103: int rl_lineno;
104: TAILQ_ENTRY(rcs_line) rl_list;
1.1 jfb 105: };
1.5 vincent 106: TAILQ_HEAD(rcs_tqh, rcs_line);
1.1 jfb 107:
108: struct rcs_foo {
1.57 xsa 109: int rl_nblines;
110: char *rl_data;
111: struct rcs_tqh rl_lines;
1.1 jfb 112: };
113:
1.57 xsa 114: #define RCS_TOKSTR(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
115: #define RCS_TOKLEN(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_tlen
1.1 jfb 116:
117:
1.47 jfb 118:
119: /* invalid characters in RCS symbol names */
1.49 jfb 120: static const char rcs_sym_invch[] = RCS_SYM_INVALCHAR;
1.47 jfb 121:
1.51 jfb 122:
123: /* comment leaders, depending on the file's suffix */
124: static const struct rcs_comment {
1.57 xsa 125: const char *rc_suffix;
126: const char *rc_cstr;
1.51 jfb 127: } rcs_comments[] = {
128: { "1", ".\\\" " },
129: { "2", ".\\\" " },
130: { "3", ".\\\" " },
131: { "4", ".\\\" " },
132: { "5", ".\\\" " },
133: { "6", ".\\\" " },
134: { "7", ".\\\" " },
135: { "8", ".\\\" " },
136: { "9", ".\\\" " },
137: { "a", "-- " }, /* Ada */
138: { "ada", "-- " },
139: { "adb", "-- " },
140: { "asm", ";; " }, /* assembler (MS-DOS) */
141: { "ads", "-- " }, /* Ada */
142: { "bat", ":: " }, /* batch (MS-DOS) */
143: { "body", "-- " }, /* Ada */
144: { "c", " * " }, /* C */
145: { "c++", "// " }, /* C++ */
146: { "cc", "// " },
147: { "cpp", "// " },
148: { "cxx", "// " },
149: { "m", "// " }, /* Objective-C */
150: { "cl", ";;; " }, /* Common Lisp */
151: { "cmd", ":: " }, /* command (OS/2) */
152: { "cmf", "c " }, /* CM Fortran */
153: { "csh", "# " }, /* shell */
154: { "e", "# " }, /* efl */
155: { "epsf", "% " }, /* encapsulated postscript */
156: { "epsi", "% " }, /* encapsulated postscript */
157: { "el", "; " }, /* Emacs Lisp */
158: { "f", "c " }, /* Fortran */
159: { "for", "c " },
160: { "h", " * " }, /* C-header */
161: { "hh", "// " }, /* C++ header */
162: { "hpp", "// " },
163: { "hxx", "// " },
164: { "in", "# " }, /* for Makefile.in */
165: { "l", " * " }, /* lex */
166: { "mac", ";; " }, /* macro (DEC-10, MS-DOS, PDP-11, VMS, etc) */
167: { "mak", "# " }, /* makefile, e.g. Visual C++ */
168: { "me", ".\\\" " }, /* me-macros t/nroff */
169: { "ml", "; " }, /* mocklisp */
170: { "mm", ".\\\" " }, /* mm-macros t/nroff */
171: { "ms", ".\\\" " }, /* ms-macros t/nroff */
172: { "man", ".\\\" " }, /* man-macros t/nroff */
173: { "p", " * " }, /* pascal */
174: { "pas", " * " },
1.52 jfb 175: { "pl", "# " }, /* Perl (conflict with Prolog) */
176: { "pm", "# " }, /* Perl module */
1.51 jfb 177: { "ps", "% " }, /* postscript */
178: { "psw", "% " }, /* postscript wrap */
179: { "pswm", "% " }, /* postscript wrap */
180: { "r", "# " }, /* ratfor */
181: { "rc", " * " }, /* Microsoft Windows resource file */
182: { "red", "% " }, /* psl/rlisp */
183: { "sh", "# " }, /* shell */
184: { "sl", "% " }, /* psl */
185: { "spec", "-- " }, /* Ada */
186: { "tex", "% " }, /* tex */
187: { "y", " * " }, /* yacc */
188: { "ye", " * " }, /* yacc-efl */
189: { "yr", " * " }, /* yacc-ratfor */
190: };
191:
1.57 xsa 192: #define NB_COMTYPES (sizeof(rcs_comments)/sizeof(rcs_comments[0]))
1.51 jfb 193:
1.33 jfb 194: #ifdef notyet
1.20 jfb 195: static struct rcs_kfl {
1.57 xsa 196: char rk_char;
197: int rk_val;
1.20 jfb 198: } rcs_kflags[] = {
199: { 'k', RCS_KWEXP_NAME },
200: { 'v', RCS_KWEXP_VAL },
201: { 'l', RCS_KWEXP_LKR },
202: { 'o', RCS_KWEXP_OLD },
203: { 'b', RCS_KWEXP_NONE },
204: };
1.33 jfb 205: #endif
1.20 jfb 206:
1.1 jfb 207: static struct rcs_key {
1.57 xsa 208: char rk_str[16];
209: int rk_id;
210: int rk_val;
211: int rk_flags;
1.1 jfb 212: } rcs_keys[] = {
213: { "access", RCS_TOK_ACCESS, RCS_TOK_ID, RCS_VOPT },
1.41 jfb 214: { "author", RCS_TOK_AUTHOR, RCS_TOK_ID, 0 },
1.1 jfb 215: { "branch", RCS_TOK_BRANCH, RCS_TOK_NUM, RCS_VOPT },
216: { "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM, RCS_VOPT },
217: { "comment", RCS_TOK_COMMENT, RCS_TOK_STRING, RCS_VOPT },
218: { "date", RCS_TOK_DATE, RCS_TOK_NUM, 0 },
219: { "desc", RCS_TOK_DESC, RCS_TOK_STRING, RCS_NOSCOL },
220: { "expand", RCS_TOK_EXPAND, RCS_TOK_STRING, RCS_VOPT },
221: { "head", RCS_TOK_HEAD, RCS_TOK_NUM, RCS_VOPT },
222: { "locks", RCS_TOK_LOCKS, RCS_TOK_ID, 0 },
223: { "log", RCS_TOK_LOG, RCS_TOK_STRING, RCS_NOSCOL },
224: { "next", RCS_TOK_NEXT, RCS_TOK_NUM, RCS_VOPT },
1.41 jfb 225: { "state", RCS_TOK_STATE, RCS_TOK_ID, RCS_VOPT },
1.1 jfb 226: { "strict", RCS_TOK_STRICT, 0, 0, },
227: { "symbols", RCS_TOK_SYMBOLS, 0, 0 },
228: { "text", RCS_TOK_TEXT, RCS_TOK_STRING, RCS_NOSCOL },
229: };
230:
1.57 xsa 231: #define RCS_NKEYS (sizeof(rcs_keys)/sizeof(rcs_keys[0]))
1.1 jfb 232:
1.33 jfb 233: /*
234: * Keyword expansion table
235: */
1.63 joris 236: #define RCS_KW_AUTHOR 0x1000
237: #define RCS_KW_DATE 0x2000
238: #define RCS_KW_LOG 0x4000
239: #define RCS_KW_NAME 0x8000
240: #define RCS_KW_RCSFILE 0x0100
241: #define RCS_KW_REVISION 0x0200
242: #define RCS_KW_SOURCE 0x0400
243: #define RCS_KW_STATE 0x0800
244: #define RCS_KW_FULLPATH 0x0010
245:
246: #define RCS_KW_ID \
247: (RCS_KW_RCSFILE | RCS_KW_REVISION | RCS_KW_DATE \
248: | RCS_KW_AUTHOR | RCS_KW_STATE)
249:
250: #define RCS_KW_HEADER (RCS_KW_ID | RCS_KW_FULLPATH)
251:
1.33 jfb 252: static struct rcs_kw {
1.57 xsa 253: char kw_str[16];
1.63 joris 254: int kw_type;
1.33 jfb 255: } rcs_expkw[] = {
1.63 joris 256: { "Author", RCS_KW_AUTHOR },
257: { "Date", RCS_KW_DATE },
258: { "Header", RCS_KW_HEADER },
259: { "Id", RCS_KW_ID },
260: { "Log", RCS_KW_LOG },
261: { "Name", RCS_KW_NAME },
262: { "RCSfile", RCS_KW_RCSFILE },
263: { "Revision", RCS_KW_REVISION },
264: { "Source", RCS_KW_SOURCE },
265: { "State", RCS_KW_STATE },
1.33 jfb 266: };
1.63 joris 267:
268: #define RCS_NKWORDS (sizeof(rcs_expkw)/sizeof(rcs_expkw[0]))
1.33 jfb 269:
1.32 jfb 270: static const char *rcs_errstrs[] = {
271: "No error",
272: "No such entry",
273: "Duplicate entry found",
274: "Bad RCS number",
1.48 jfb 275: "Invalid RCS symbol",
276: "Parse error",
1.32 jfb 277: };
278:
279: #define RCS_NERR (sizeof(rcs_errstrs)/sizeof(rcs_errstrs[0]))
280:
281:
282: int rcs_errno = RCS_ERR_NOERR;
283:
1.1 jfb 284:
1.57 xsa 285: static int rcs_write(RCSFILE *);
286: static int rcs_parse(RCSFILE *);
287: static int rcs_parse_admin(RCSFILE *);
288: static int rcs_parse_delta(RCSFILE *);
289: static int rcs_parse_deltatext(RCSFILE *);
290:
291: static int rcs_parse_access(RCSFILE *);
292: static int rcs_parse_symbols(RCSFILE *);
293: static int rcs_parse_locks(RCSFILE *);
294: static int rcs_parse_branches(RCSFILE *, struct rcs_delta *);
295: static void rcs_freedelta(struct rcs_delta *);
296: static void rcs_freepdata(struct rcs_pdata *);
297: static int rcs_gettok(RCSFILE *);
298: static int rcs_pushtok(RCSFILE *, const char *, int);
299: static int rcs_growbuf(RCSFILE *);
300: static int rcs_patch_lines(struct rcs_foo *, struct rcs_foo *);
301: static int rcs_strprint(const u_char *, size_t, FILE *);
302:
1.63 joris 303: static int rcs_expand_keywords(char *, struct rcs_delta *, char *, char *,
304: size_t, int);
1.57 xsa 305: static struct rcs_delta *rcs_findrev(RCSFILE *, const RCSNUM *);
306: static struct rcs_foo *rcs_splitlines(const char *);
307: static void rcs_freefoo(struct rcs_foo *);
1.26 jfb 308:
1.1 jfb 309: /*
310: * rcs_open()
311: *
312: * Open a file containing RCS-formatted information. The file's path is
1.26 jfb 313: * given in <path>, and the opening flags are given in <flags>, which is either
314: * RCS_READ, RCS_WRITE, or RCS_RDWR. If the open requests write access and
315: * the file does not exist, the RCS_CREATE flag must also be given, in which
316: * case it will be created with the mode specified in a third argument of
317: * type mode_t. If the file exists and RCS_CREATE is passed, the open will
318: * fail.
1.1 jfb 319: * Returns a handle to the opened file on success, or NULL on failure.
320: */
1.60 xsa 321: RCSFILE *
1.26 jfb 322: rcs_open(const char *path, int flags, ...)
1.1 jfb 323: {
1.26 jfb 324: int ret;
325: mode_t fmode;
1.1 jfb 326: RCSFILE *rfp;
327: struct stat st;
1.26 jfb 328: va_list vap;
329:
330: fmode = 0;
331: flags &= 0xffff; /* ditch any internal flags */
1.1 jfb 332:
1.26 jfb 333: if (((ret = stat(path, &st)) == -1) && (errno == ENOENT)) {
334: if (flags & RCS_CREATE) {
335: va_start(vap, flags);
336: fmode = va_arg(vap, mode_t);
337: va_end(vap);
338: } else {
1.50 jfb 339: rcs_errno = RCS_ERR_ERRNO;
1.26 jfb 340: cvs_log(LP_ERR, "RCS file `%s' does not exist", path);
341: return (NULL);
342: }
343: } else if ((ret == 0) && (flags & RCS_CREATE)) {
344: cvs_log(LP_ERR, "RCS file `%s' exists", path);
1.1 jfb 345: return (NULL);
346: }
347:
1.26 jfb 348: if ((rfp = (RCSFILE *)malloc(sizeof(*rfp))) == NULL) {
1.1 jfb 349: cvs_log(LP_ERRNO, "failed to allocate RCS file structure");
1.50 jfb 350: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 351: return (NULL);
352: }
353: memset(rfp, 0, sizeof(*rfp));
354:
1.26 jfb 355: if ((rfp->rf_path = strdup(path)) == NULL) {
1.50 jfb 356: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 357: cvs_log(LP_ERRNO, "failed to duplicate RCS file path");
1.26 jfb 358: free(rfp);
1.1 jfb 359: return (NULL);
360: }
361:
362: rfp->rf_ref = 1;
1.26 jfb 363: rfp->rf_flags = flags | RCS_SLOCK;
364: rfp->rf_mode = fmode;
1.1 jfb 365:
366: TAILQ_INIT(&(rfp->rf_delta));
1.29 jfb 367: TAILQ_INIT(&(rfp->rf_access));
1.1 jfb 368: TAILQ_INIT(&(rfp->rf_symbols));
369: TAILQ_INIT(&(rfp->rf_locks));
370:
1.26 jfb 371: if (rfp->rf_flags & RCS_CREATE) {
372: } else if (rcs_parse(rfp) < 0) {
1.1 jfb 373: rcs_close(rfp);
374: return (NULL);
375: }
376:
377: return (rfp);
378: }
379:
380: /*
381: * rcs_close()
382: *
383: * Close an RCS file handle.
384: */
385: void
386: rcs_close(RCSFILE *rfp)
387: {
388: struct rcs_delta *rdp;
1.71 moritz 389: struct rcs_access *rap;
1.13 jfb 390: struct rcs_lock *rlp;
391: struct rcs_sym *rsp;
1.1 jfb 392:
393: if (rfp->rf_ref > 1) {
394: rfp->rf_ref--;
395: return;
396: }
397:
1.26 jfb 398: if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
399: rcs_write(rfp);
400:
1.1 jfb 401: while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
402: rdp = TAILQ_FIRST(&(rfp->rf_delta));
403: TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
404: rcs_freedelta(rdp);
1.71 moritz 405: }
406:
407: while (!TAILQ_EMPTY(&(rfp->rf_access))) {
408: rap = TAILQ_FIRST(&(rfp->rf_access));
409: TAILQ_REMOVE(&(rfp->rf_access), rap, ra_list);
410: cvs_strfree(rap->ra_name);
411: free(rap);
1.1 jfb 412: }
413:
1.13 jfb 414: while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
415: rsp = TAILQ_FIRST(&(rfp->rf_symbols));
416: TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
417: rcsnum_free(rsp->rs_num);
1.39 joris 418: cvs_strfree(rsp->rs_name);
1.13 jfb 419: free(rsp);
420: }
421:
422: while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
423: rlp = TAILQ_FIRST(&(rfp->rf_locks));
424: TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
425: rcsnum_free(rlp->rl_num);
1.77 joris 426: cvs_strfree(rlp->rl_name);
1.13 jfb 427: free(rlp);
428: }
429:
1.1 jfb 430: if (rfp->rf_head != NULL)
431: rcsnum_free(rfp->rf_head);
1.11 joris 432: if (rfp->rf_branch != NULL)
433: rcsnum_free(rfp->rf_branch);
1.1 jfb 434:
435: if (rfp->rf_path != NULL)
436: free(rfp->rf_path);
437: if (rfp->rf_comment != NULL)
1.39 joris 438: cvs_strfree(rfp->rf_comment);
1.1 jfb 439: if (rfp->rf_expand != NULL)
1.39 joris 440: cvs_strfree(rfp->rf_expand);
1.1 jfb 441: if (rfp->rf_desc != NULL)
1.39 joris 442: cvs_strfree(rfp->rf_desc);
1.1 jfb 443: free(rfp);
444: }
445:
446: /*
447: * rcs_write()
448: *
449: * Write the contents of the RCS file handle <rfp> to disk in the file whose
450: * path is in <rf_path>.
451: * Returns 0 on success, or -1 on failure.
452: */
1.27 jfb 453: static int
1.1 jfb 454: rcs_write(RCSFILE *rfp)
455: {
456: FILE *fp;
1.72 niallo 457: char buf[1024], numbuf[64], fn[19] = "";
458: void *bp;
1.29 jfb 459: struct rcs_access *ap;
1.1 jfb 460: struct rcs_sym *symp;
1.46 jfb 461: struct rcs_branch *brp;
1.1 jfb 462: struct rcs_delta *rdp;
1.75 joris 463: struct rcs_lock *lkp;
1.72 niallo 464: ssize_t nread;
465: int fd, from_fd, to_fd;
1.75 joris 466:
1.72 niallo 467: from_fd = to_fd = fd = -1;
1.1 jfb 468:
1.28 jfb 469: if (rfp->rf_flags & RCS_SYNCED)
1.1 jfb 470: return (0);
471:
1.72 niallo 472: strlcpy(fn, "/tmp/rcs.XXXXXXXXXX", sizeof(fn));
473: if ((fd = mkstemp(fn)) == -1 ||
474: (fp = fdopen(fd, "w+")) == NULL) {
475: if (fd != -1) {
476: unlink(fn);
477: close(fd);
478: }
1.50 jfb 479: rcs_errno = RCS_ERR_ERRNO;
1.72 niallo 480: cvs_log(LP_ERRNO, "failed to open temp RCS output file `%s'",
481: fn);
1.1 jfb 482: return (-1);
483: }
484:
1.28 jfb 485: if (rfp->rf_head != NULL)
486: rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
487: else
488: numbuf[0] = '\0';
489:
1.1 jfb 490: fprintf(fp, "head\t%s;\n", numbuf);
1.35 jfb 491:
492: if (rfp->rf_branch != NULL) {
493: rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
494: fprintf(fp, "branch\t%s;\n", numbuf);
495: }
496:
1.29 jfb 497: fputs("access", fp);
498: TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
499: fprintf(fp, "\n\t%s", ap->ra_name);
500: }
501: fputs(";\n", fp);
1.1 jfb 502:
1.85 joris 503: fprintf(fp, "symbols");
1.1 jfb 504: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
505: rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
506: snprintf(buf, sizeof(buf), "%s:%s", symp->rs_name, numbuf);
1.85 joris 507: fprintf(fp, "\n\t%s", buf);
1.1 jfb 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.80 reyk 599: if ((to_fd = open(rfp->rf_path,
600: O_WRONLY|O_TRUNC|O_CREAT)) == -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);
1.82 joris 1242: if ((lineno > dlines->rl_nblines) || (lineno < 0) ||
1.1 jfb 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);
1.82 joris 1250: if ((nbln < 0) || (*ep != '\0')) {
1.1 jfb 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: */
1.84 joris 1370: crev = rdp->rd_next;
1.26 jfb 1371: do {
1372: rdp = rcs_findrev(rfp, crev);
1373: if (rdp == NULL) {
1374: cvs_buf_free(rbuf);
1375: return (NULL);
1376: }
1.1 jfb 1377:
1.26 jfb 1378: if (cvs_buf_putc(rbuf, '\0') < 0) {
1.17 jfb 1379: cvs_buf_free(rbuf);
1.11 joris 1380: return (NULL);
1.17 jfb 1381: }
1.26 jfb 1382: bp = cvs_buf_release(rbuf);
1.45 jfb 1383: rbuf = rcs_patch((char *)bp, (char *)rdp->rd_text);
1.62 joris 1384: free(bp);
1.26 jfb 1385: if (rbuf == NULL)
1386: break;
1.84 joris 1387: crev = rdp->rd_next;
1.26 jfb 1388: } while (rcsnum_cmp(crev, rev, 0) != 0);
1.1 jfb 1389: }
1390:
1.63 joris 1391: /*
1392: * Do keyword expansion if required.
1393: */
1394: if (rfp->rf_expand != NULL)
1395: expmode = rcs_kwexp_get(rfp);
1396: else
1397: expmode = RCS_KWEXP_DEFAULT;
1398:
1399: if ((rbuf != NULL) && !(expmode & RCS_KWEXP_NONE)) {
1400: if ((dbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
1401: return (rbuf);
1402: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
1403: return (rbuf);
1404:
1405: if (cvs_buf_putc(rbuf, '\0') < 0) {
1406: cvs_buf_free(dbuf);
1407: return (rbuf);
1408: }
1409:
1410: bp = cvs_buf_release(rbuf);
1411: if ((lines = rcs_splitlines((char *)bp)) != NULL) {
1412: res = 0;
1413: TAILQ_FOREACH(lp, &lines->rl_lines, rl_list) {
1414: if (res++ == 0)
1415: continue;
1416: rcs_expand_keywords(rfp->rf_path, rdp,
1417: lp->rl_line, out, sizeof(out), expmode);
1418: cvs_buf_fappend(dbuf, "%s\n", out);
1419: }
1420: rcs_freefoo(lines);
1421: }
1422: free(bp);
1423: }
1424:
1425: return (dbuf);
1.1 jfb 1426: }
1427:
1428: /*
1.52 jfb 1429: * rcs_rev_add()
1430: *
1.53 jfb 1431: * Add a revision to the RCS file <rf>. The new revision's number can be
1432: * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
1433: * new revision will have a number equal to the previous head revision plus
1434: * one). The <msg> argument specifies the log message for that revision, and
1435: * <date> specifies the revision's date (a value of -1 is
1436: * equivalent to using the current time).
1.52 jfb 1437: * Returns 0 on success, or -1 on failure.
1438: */
1439: int
1.53 jfb 1440: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date)
1.52 jfb 1441: {
1442: time_t now;
1443: struct passwd *pw;
1.83 joris 1444: struct rcs_delta *ordp, *rdp;
1445: RCSNUM *old;
1446:
1447: if ((old = rcsnum_alloc()) == NULL)
1448: return (-1);
1.52 jfb 1449:
1450: if (rev == RCS_HEAD_REV) {
1.83 joris 1451: rcsnum_cpy(rf->rf_head, old, 0);
1.81 niallo 1452: rev = rcsnum_inc(rf->rf_head);
1.83 joris 1453: } else {
1454: if ((rdp = rcs_findrev(rf, rev)) != NULL) {
1455: rcs_errno = RCS_ERR_DUPENT;
1456: return (-1);
1457: }
1458:
1459: ordp = NULL;
1460: rcsnum_cpy(rev, old, 0);
1461: while (ordp == NULL) {
1462: old = rcsnum_dec(old);
1463: ordp = rcs_findrev(rf, old);
1464: }
1.52 jfb 1465: }
1466:
1467: if ((pw = getpwuid(getuid())) == NULL) {
1468: rcs_errno = RCS_ERR_ERRNO;
1469: return (-1);
1470: }
1471:
1472: if ((rdp = (struct rcs_delta *)malloc(sizeof(*rdp))) == NULL) {
1473: rcs_errno = RCS_ERR_ERRNO;
1474: return (-1);
1475: }
1476: memset(rdp, 0, sizeof(*rdp));
1477:
1478: TAILQ_INIT(&(rdp->rd_branches));
1479: TAILQ_INIT(&(rdp->rd_snodes));
1480:
1481: if ((rdp->rd_num = rcsnum_alloc()) == NULL) {
1482: rcs_freedelta(rdp);
1483: return (-1);
1484: }
1485: rcsnum_cpy(rev, rdp->rd_num, 0);
1.83 joris 1486:
1487: if ((rdp->rd_next = rcsnum_alloc()) == NULL) {
1488: rcs_freedelta(rdp);
1489: return (-1);
1490: }
1491: rcsnum_cpy(old, rdp->rd_next, 0);
1492: rcsnum_free(old);
1.52 jfb 1493:
1494: if ((rdp->rd_author = cvs_strdup(pw->pw_name)) == NULL) {
1495: rcs_freedelta(rdp);
1496: return (-1);
1497: }
1498:
1499: if ((rdp->rd_state = cvs_strdup(RCS_STATE_EXP)) == NULL) {
1500: rcs_freedelta(rdp);
1501: return (-1);
1502: }
1503:
1504: if ((rdp->rd_log = cvs_strdup(msg)) == NULL) {
1505: rcs_errno = RCS_ERR_ERRNO;
1506: rcs_freedelta(rdp);
1507: return (-1);
1508: }
1509:
1.53 jfb 1510: if (date != (time_t)(-1))
1511: now = date;
1512: else
1513: time(&now);
1.52 jfb 1514: gmtime_r(&now, &(rdp->rd_date));
1515:
1516: TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1517: rf->rf_ndelta++;
1.81 niallo 1518:
1.64 niallo 1519: /* not synced anymore */
1520: rf->rf_flags &= ~RCS_SYNCED;
1.52 jfb 1521:
1522: return (0);
1523: }
1524:
1525: /*
1526: * rcs_rev_remove()
1527: *
1528: * Remove the revision whose number is <rev> from the RCS file <rf>.
1529: */
1530: int
1531: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
1532: {
1533: int ret;
1534: struct rcs_delta *rdp;
1535:
1536: ret = 0;
1537: if (rev == RCS_HEAD_REV)
1538: rev = rf->rf_head;
1539:
1540: /* do we actually have that revision? */
1541: if ((rdp = rcs_findrev(rf, rev)) == NULL) {
1542: rcs_errno = RCS_ERR_NOENT;
1543: ret = -1;
1544: } else {
1545: /* XXX assumes it's not a sub node */
1546: TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
1547: rf->rf_ndelta--;
1548: rf->rf_flags &= ~RCS_SYNCED;
1549: }
1550:
1551: return (ret);
1552:
1553: }
1554:
1555: /*
1.1 jfb 1556: * rcs_findrev()
1557: *
1558: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1559: * The revision number is given in <rev>.
1560: * Returns a pointer to the delta on success, or NULL on failure.
1561: */
1562: static struct rcs_delta*
1.43 jfb 1563: rcs_findrev(RCSFILE *rfp, const RCSNUM *rev)
1.1 jfb 1564: {
1565: u_int cmplen;
1566: struct rcs_delta *rdp;
1567: struct rcs_dlist *hp;
1.6 vincent 1568: int found;
1.26 jfb 1569:
1.1 jfb 1570: cmplen = 2;
1571: hp = &(rfp->rf_delta);
1572:
1.6 vincent 1573: do {
1574: found = 0;
1575: TAILQ_FOREACH(rdp, hp, rd_list) {
1576: if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
1577: if (cmplen == rev->rn_len)
1578: return (rdp);
1.1 jfb 1579:
1.6 vincent 1580: hp = &(rdp->rd_snodes);
1581: cmplen += 2;
1582: found = 1;
1583: break;
1584: }
1.1 jfb 1585: }
1.6 vincent 1586: } while (found && cmplen < rev->rn_len);
1.1 jfb 1587:
1588: return (NULL);
1.20 jfb 1589: }
1590:
1591: /*
1.26 jfb 1592: * rcs_kwexp_set()
1593: *
1594: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1595: * Returns 0 on success, or -1 on failure.
1596: */
1597: int
1598: rcs_kwexp_set(RCSFILE *file, int mode)
1599: {
1600: int i;
1601: char *tmp, buf[8] = "";
1602:
1603: if (RCS_KWEXP_INVAL(mode))
1604: return (-1);
1605:
1606: i = 0;
1607: if (mode == RCS_KWEXP_NONE)
1608: buf[0] = 'b';
1609: else if (mode == RCS_KWEXP_OLD)
1610: buf[0] = 'o';
1611: else {
1612: if (mode & RCS_KWEXP_NAME)
1613: buf[i++] = 'k';
1614: if (mode & RCS_KWEXP_VAL)
1615: buf[i++] = 'v';
1616: if (mode & RCS_KWEXP_LKR)
1617: buf[i++] = 'l';
1618: }
1619:
1.39 joris 1620: if ((tmp = cvs_strdup(buf)) == NULL) {
1.26 jfb 1621: cvs_log(LP_ERRNO, "%s: failed to copy expansion mode",
1622: file->rf_path);
1623: return (-1);
1624: }
1625:
1.27 jfb 1626: if (file->rf_expand != NULL)
1.39 joris 1627: cvs_strfree(file->rf_expand);
1.26 jfb 1628: file->rf_expand = tmp;
1.64 niallo 1629: /* not synced anymore */
1630: file->rf_flags &= ~RCS_SYNCED;
1.26 jfb 1631:
1632: return (0);
1633: }
1634:
1635: /*
1636: * rcs_kwexp_get()
1637: *
1638: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1639: */
1640: int
1641: rcs_kwexp_get(RCSFILE *file)
1642: {
1643: return rcs_kflag_get(file->rf_expand);
1644: }
1645:
1646: /*
1.20 jfb 1647: * rcs_kflag_get()
1648: *
1649: * Get the keyword expansion mode from a set of character flags given in
1650: * <flags> and return the appropriate flag mask. In case of an error, the
1651: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1652: */
1653: int
1654: rcs_kflag_get(const char *flags)
1655: {
1656: int fl;
1657: size_t len;
1658: const char *fp;
1659:
1660: fl = 0;
1661: len = strlen(flags);
1662:
1663: for (fp = flags; *fp != '\0'; fp++) {
1664: if (*fp == 'k')
1665: fl |= RCS_KWEXP_NAME;
1666: else if (*fp == 'v')
1667: fl |= RCS_KWEXP_VAL;
1668: else if (*fp == 'l')
1669: fl |= RCS_KWEXP_LKR;
1670: else if (*fp == 'o') {
1671: if (len != 1)
1672: fl |= RCS_KWEXP_ERR;
1673: fl |= RCS_KWEXP_OLD;
1674: } else if (*fp == 'b') {
1675: if (len != 1)
1676: fl |= RCS_KWEXP_ERR;
1677: } else /* unknown letter */
1678: fl |= RCS_KWEXP_ERR;
1679: }
1680:
1681: return (fl);
1.32 jfb 1682: }
1683:
1684: /*
1685: * rcs_errstr()
1686: *
1687: * Get the error string matching the RCS error code <code>.
1688: */
1.57 xsa 1689: const char *
1.32 jfb 1690: rcs_errstr(int code)
1691: {
1.50 jfb 1692: const char *esp;
1693:
1694: if ((code < 0) || ((code >= (int)RCS_NERR) && (code != RCS_ERR_ERRNO)))
1695: esp = NULL;
1696: else if (code == RCS_ERR_ERRNO)
1697: esp = strerror(errno);
1698: else
1699: esp = rcs_errstrs[code];
1700: return (esp);
1.1 jfb 1701: }
1702:
1.21 jfb 1703: void
1704: rcs_kflag_usage(void)
1705: {
1706: fprintf(stderr, "Valid expansion modes include:\n"
1.22 jfb 1707: "\t-kkv\tGenerate keywords using the default form.\n"
1708: "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
1709: "\t-kk\tGenerate only keyword names in keyword strings.\n"
1710: "\t-kv\tGenerate only keyword values in keyword strings.\n"
1711: "\t-ko\tGenerate old keyword string "
1.21 jfb 1712: "(no changes from checked in file).\n"
1.22 jfb 1713: "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21 jfb 1714: }
1.1 jfb 1715:
1716: /*
1717: * rcs_parse()
1718: *
1719: * Parse the contents of file <path>, which are in the RCS format.
1720: * Returns 0 on success, or -1 on failure.
1721: */
1.26 jfb 1722: static int
1.1 jfb 1723: rcs_parse(RCSFILE *rfp)
1724: {
1725: int ret;
1726: struct rcs_pdata *pdp;
1727:
1.26 jfb 1728: if (rfp->rf_flags & RCS_PARSED)
1.1 jfb 1729: return (0);
1730:
1.26 jfb 1731: if ((pdp = (struct rcs_pdata *)malloc(sizeof(*pdp))) == NULL) {
1.50 jfb 1732: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1733: cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
1734: return (-1);
1735: }
1736: memset(pdp, 0, sizeof(*pdp));
1737:
1.18 jfb 1738: pdp->rp_lines = 0;
1.1 jfb 1739: pdp->rp_pttype = RCS_TOK_ERR;
1740:
1741: pdp->rp_file = fopen(rfp->rf_path, "r");
1742: if (pdp->rp_file == NULL) {
1.50 jfb 1743: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1744: cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
1745: rcs_freepdata(pdp);
1746: return (-1);
1747: }
1748:
1.59 xsa 1749: pdp->rp_buf = (char *)malloc((size_t)RCS_BUFSIZE);
1.1 jfb 1750: if (pdp->rp_buf == NULL) {
1.50 jfb 1751: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1752: cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
1753: rcs_freepdata(pdp);
1754: return (-1);
1755: }
1756: pdp->rp_blen = RCS_BUFSIZE;
1.18 jfb 1757: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1 jfb 1758:
1759: /* ditch the strict lock */
1.26 jfb 1760: rfp->rf_flags &= ~RCS_SLOCK;
1.1 jfb 1761: rfp->rf_pdata = pdp;
1762:
1.31 jfb 1763: if ((ret = rcs_parse_admin(rfp)) < 0) {
1.1 jfb 1764: rcs_freepdata(pdp);
1765: return (-1);
1.31 jfb 1766: } else if (ret == RCS_TOK_NUM) {
1767: for (;;) {
1768: ret = rcs_parse_delta(rfp);
1769: if (ret == 0)
1770: break;
1771: else if (ret == -1) {
1772: rcs_freepdata(pdp);
1773: return (-1);
1774: }
1.1 jfb 1775: }
1776: }
1777:
1778: ret = rcs_gettok(rfp);
1779: if (ret != RCS_TOK_DESC) {
1.50 jfb 1780: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1781: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1782: RCS_TOKSTR(rfp));
1783: rcs_freepdata(pdp);
1784: return (-1);
1785: }
1786:
1787: ret = rcs_gettok(rfp);
1788: if (ret != RCS_TOK_STRING) {
1.50 jfb 1789: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1790: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1791: RCS_TOKSTR(rfp));
1792: rcs_freepdata(pdp);
1793: return (-1);
1794: }
1795:
1.39 joris 1796: rfp->rf_desc = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1797: if (rfp->rf_desc == NULL) {
1798: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
1799: rcs_freepdata(pdp);
1800: return (-1);
1801: }
1.1 jfb 1802:
1803: for (;;) {
1804: ret = rcs_parse_deltatext(rfp);
1805: if (ret == 0)
1806: break;
1807: else if (ret == -1) {
1808: rcs_freepdata(pdp);
1809: return (-1);
1810: }
1811: }
1812:
1813: rcs_freepdata(pdp);
1814:
1815: rfp->rf_pdata = NULL;
1.26 jfb 1816: rfp->rf_flags |= RCS_PARSED | RCS_SYNCED;
1.1 jfb 1817:
1818: return (0);
1819: }
1820:
1821: /*
1822: * rcs_parse_admin()
1823: *
1824: * Parse the administrative portion of an RCS file.
1.31 jfb 1825: * Returns the type of the first token found after the admin section on
1826: * success, or -1 on failure.
1.1 jfb 1827: */
1828: static int
1829: rcs_parse_admin(RCSFILE *rfp)
1830: {
1831: u_int i;
1832: int tok, ntok, hmask;
1833: struct rcs_key *rk;
1834:
1835: /* hmask is a mask of the headers already encountered */
1836: hmask = 0;
1837: for (;;) {
1838: tok = rcs_gettok(rfp);
1839: if (tok == RCS_TOK_ERR) {
1.50 jfb 1840: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1841: cvs_log(LP_ERR, "parse error in RCS admin section");
1842: return (-1);
1.31 jfb 1843: } else if ((tok == RCS_TOK_NUM) || (tok == RCS_TOK_DESC)) {
1844: /*
1845: * Assume this is the start of the first delta or
1846: * that we are dealing with an empty RCS file and
1847: * we just found the description.
1848: */
1.1 jfb 1849: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31 jfb 1850: return (tok);
1.1 jfb 1851: }
1852:
1853: rk = NULL;
1.18 jfb 1854: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1855: if (rcs_keys[i].rk_id == tok)
1856: rk = &(rcs_keys[i]);
1857:
1858: if (hmask & (1 << tok)) {
1.50 jfb 1859: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1860: cvs_log(LP_ERR, "duplicate RCS key");
1861: return (-1);
1862: }
1863: hmask |= (1 << tok);
1864:
1865: switch (tok) {
1866: case RCS_TOK_HEAD:
1867: case RCS_TOK_BRANCH:
1868: case RCS_TOK_COMMENT:
1869: case RCS_TOK_EXPAND:
1870: ntok = rcs_gettok(rfp);
1871: if (ntok == RCS_TOK_SCOLON)
1872: break;
1873: if (ntok != rk->rk_val) {
1.50 jfb 1874: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1875: cvs_log(LP_ERR,
1876: "invalid value type for RCS key `%s'",
1877: rk->rk_str);
1878: }
1879:
1880: if (tok == RCS_TOK_HEAD) {
1.28 jfb 1881: if (rfp->rf_head == NULL) {
1882: rfp->rf_head = rcsnum_alloc();
1883: if (rfp->rf_head == NULL)
1884: return (-1);
1885: }
1.1 jfb 1886: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1887: rfp->rf_head);
1.14 deraadt 1888: } else if (tok == RCS_TOK_BRANCH) {
1.35 jfb 1889: if (rfp->rf_branch == NULL) {
1890: rfp->rf_branch = rcsnum_alloc();
1891: if (rfp->rf_branch == NULL)
1892: return (-1);
1893: }
1894: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1895: rfp->rf_branch) < 0)
1896: return (-1);
1.14 deraadt 1897: } else if (tok == RCS_TOK_COMMENT) {
1.39 joris 1898: rfp->rf_comment = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1899: if (rfp->rf_comment == NULL) {
1900: cvs_log(LP_ERRNO,
1901: "failed to duplicate rcs token");
1902: return (-1);
1903: }
1.14 deraadt 1904: } else if (tok == RCS_TOK_EXPAND) {
1.39 joris 1905: rfp->rf_expand = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1906: if (rfp->rf_expand == NULL) {
1907: cvs_log(LP_ERRNO,
1908: "failed to duplicate rcs token");
1909: return (-1);
1910: }
1.1 jfb 1911: }
1912:
1913: /* now get the expected semi-colon */
1914: ntok = rcs_gettok(rfp);
1915: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1916: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1917: cvs_log(LP_ERR,
1918: "missing semi-colon after RCS `%s' key",
1.26 jfb 1919: rk->rk_str);
1.1 jfb 1920: return (-1);
1921: }
1922: break;
1923: case RCS_TOK_ACCESS:
1.29 jfb 1924: if (rcs_parse_access(rfp) < 0)
1925: return (-1);
1.1 jfb 1926: break;
1927: case RCS_TOK_SYMBOLS:
1.29 jfb 1928: if (rcs_parse_symbols(rfp) < 0)
1929: return (-1);
1.1 jfb 1930: break;
1931: case RCS_TOK_LOCKS:
1.29 jfb 1932: if (rcs_parse_locks(rfp) < 0)
1933: return (-1);
1.1 jfb 1934: break;
1935: default:
1.50 jfb 1936: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1937: cvs_log(LP_ERR,
1938: "unexpected token `%s' in RCS admin section",
1939: RCS_TOKSTR(rfp));
1940: return (-1);
1941: }
1942: }
1943:
1944: return (0);
1945: }
1946:
1947: /*
1948: * rcs_parse_delta()
1949: *
1950: * Parse an RCS delta section and allocate the structure to store that delta's
1951: * information in the <rfp> delta list.
1952: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1953: * -1 on error.
1954: */
1955: static int
1956: rcs_parse_delta(RCSFILE *rfp)
1957: {
1958: int ret, tok, ntok, hmask;
1959: u_int i;
1960: char *tokstr;
1.3 vincent 1961: RCSNUM *datenum;
1.1 jfb 1962: struct rcs_delta *rdp;
1963: struct rcs_key *rk;
1964:
1965: rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
1966: if (rdp == NULL) {
1.50 jfb 1967: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1968: cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
1969: return (-1);
1970: }
1971: memset(rdp, 0, sizeof(*rdp));
1972:
1973: rdp->rd_num = rcsnum_alloc();
1.11 joris 1974: if (rdp->rd_num == NULL) {
1975: rcs_freedelta(rdp);
1976: return (-1);
1977: }
1.1 jfb 1978: rdp->rd_next = rcsnum_alloc();
1.11 joris 1979: if (rdp->rd_next == NULL) {
1980: rcs_freedelta(rdp);
1981: return (-1);
1982: }
1.1 jfb 1983:
1984: TAILQ_INIT(&(rdp->rd_branches));
1.52 jfb 1985: TAILQ_INIT(&(rdp->rd_snodes));
1.1 jfb 1986:
1987: tok = rcs_gettok(rfp);
1988: if (tok != RCS_TOK_NUM) {
1.52 jfb 1989: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1990: cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
1991: RCS_TOKSTR(rfp));
1992: rcs_freedelta(rdp);
1993: return (-1);
1994: }
1995: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1996:
1997: hmask = 0;
1998: ret = 0;
1999: tokstr = NULL;
2000:
2001: for (;;) {
2002: tok = rcs_gettok(rfp);
2003: if (tok == RCS_TOK_ERR) {
1.50 jfb 2004: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2005: cvs_log(LP_ERR, "parse error in RCS delta section");
2006: rcs_freedelta(rdp);
2007: return (-1);
1.14 deraadt 2008: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15 tedu 2009: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1 jfb 2010: ret = (tok == RCS_TOK_NUM ? 1 : 0);
2011: break;
2012: }
2013:
2014: rk = NULL;
1.18 jfb 2015: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 2016: if (rcs_keys[i].rk_id == tok)
2017: rk = &(rcs_keys[i]);
2018:
2019: if (hmask & (1 << tok)) {
1.50 jfb 2020: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2021: cvs_log(LP_ERR, "duplicate RCS key");
2022: rcs_freedelta(rdp);
2023: return (-1);
2024: }
2025: hmask |= (1 << tok);
2026:
2027: switch (tok) {
2028: case RCS_TOK_DATE:
2029: case RCS_TOK_AUTHOR:
2030: case RCS_TOK_STATE:
2031: case RCS_TOK_NEXT:
2032: ntok = rcs_gettok(rfp);
2033: if (ntok == RCS_TOK_SCOLON) {
2034: if (rk->rk_flags & RCS_VOPT)
2035: break;
2036: else {
1.50 jfb 2037: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2038: cvs_log(LP_ERR, "missing mandatory "
2039: "value to RCS key `%s'",
2040: rk->rk_str);
2041: rcs_freedelta(rdp);
2042: return (-1);
2043: }
2044: }
2045:
2046: if (ntok != rk->rk_val) {
1.50 jfb 2047: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2048: cvs_log(LP_ERR,
2049: "invalid value type for RCS key `%s'",
2050: rk->rk_str);
2051: rcs_freedelta(rdp);
2052: return (-1);
2053: }
2054:
2055: if (tokstr != NULL)
1.41 jfb 2056: cvs_strfree(tokstr);
1.39 joris 2057: tokstr = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 2058: if (tokstr == NULL) {
1.15 tedu 2059: cvs_log(LP_ERRNO,
1.10 joris 2060: "failed to duplicate rcs token");
2061: rcs_freedelta(rdp);
2062: return (-1);
2063: }
1.1 jfb 2064:
2065: /* now get the expected semi-colon */
2066: ntok = rcs_gettok(rfp);
2067: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 2068: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2069: cvs_log(LP_ERR,
2070: "missing semi-colon after RCS `%s' key",
1.26 jfb 2071: rk->rk_str);
1.41 jfb 2072: cvs_strfree(tokstr);
1.1 jfb 2073: rcs_freedelta(rdp);
2074: return (-1);
2075: }
2076:
2077: if (tok == RCS_TOK_DATE) {
1.25 jfb 2078: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.41 jfb 2079: cvs_strfree(tokstr);
1.11 joris 2080: rcs_freedelta(rdp);
2081: return (-1);
2082: }
1.3 vincent 2083: if (datenum->rn_len != 6) {
1.50 jfb 2084: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2085: cvs_log(LP_ERR,
2086: "RCS date specification has %s "
2087: "fields",
1.3 vincent 2088: (datenum->rn_len > 6) ? "too many" :
1.1 jfb 2089: "missing");
1.41 jfb 2090: cvs_strfree(tokstr);
1.1 jfb 2091: rcs_freedelta(rdp);
1.37 tedu 2092: rcsnum_free(datenum);
2093: return (-1);
1.1 jfb 2094: }
1.3 vincent 2095: rdp->rd_date.tm_year = datenum->rn_id[0];
1.19 jfb 2096: if (rdp->rd_date.tm_year >= 1900)
2097: rdp->rd_date.tm_year -= 1900;
1.3 vincent 2098: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
2099: rdp->rd_date.tm_mday = datenum->rn_id[2];
2100: rdp->rd_date.tm_hour = datenum->rn_id[3];
2101: rdp->rd_date.tm_min = datenum->rn_id[4];
2102: rdp->rd_date.tm_sec = datenum->rn_id[5];
2103: rcsnum_free(datenum);
1.14 deraadt 2104: } else if (tok == RCS_TOK_AUTHOR) {
1.1 jfb 2105: rdp->rd_author = tokstr;
2106: tokstr = NULL;
1.14 deraadt 2107: } else if (tok == RCS_TOK_STATE) {
1.1 jfb 2108: rdp->rd_state = tokstr;
2109: tokstr = NULL;
1.14 deraadt 2110: } else if (tok == RCS_TOK_NEXT) {
1.1 jfb 2111: rcsnum_aton(tokstr, NULL, rdp->rd_next);
2112: }
2113: break;
2114: case RCS_TOK_BRANCHES:
1.46 jfb 2115: if (rcs_parse_branches(rfp, rdp) < 0) {
2116: rcs_freedelta(rdp);
2117: return (-1);
2118: }
1.1 jfb 2119: break;
2120: default:
1.50 jfb 2121: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2122: cvs_log(LP_ERR,
2123: "unexpected token `%s' in RCS delta",
2124: RCS_TOKSTR(rfp));
2125: rcs_freedelta(rdp);
2126: return (-1);
2127: }
2128: }
2129:
1.13 jfb 2130: if (tokstr != NULL)
1.39 joris 2131: cvs_strfree(tokstr);
1.13 jfb 2132:
1.1 jfb 2133: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26 jfb 2134: rfp->rf_ndelta++;
1.1 jfb 2135:
2136: return (ret);
2137: }
2138:
2139: /*
2140: * rcs_parse_deltatext()
2141: *
2142: * Parse an RCS delta text section and fill in the log and text field of the
2143: * appropriate delta section.
2144: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
2145: * -1 on error.
2146: */
2147: static int
2148: rcs_parse_deltatext(RCSFILE *rfp)
2149: {
2150: int tok;
2151: RCSNUM *tnum;
2152: struct rcs_delta *rdp;
2153:
2154: tok = rcs_gettok(rfp);
2155: if (tok == RCS_TOK_EOF)
2156: return (0);
2157:
2158: if (tok != RCS_TOK_NUM) {
1.50 jfb 2159: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2160: cvs_log(LP_ERR,
2161: "unexpected token `%s' at start of RCS delta text",
2162: RCS_TOKSTR(rfp));
2163: return (-1);
2164: }
1.13 jfb 2165:
2166: tnum = rcsnum_alloc();
2167: if (tnum == NULL)
2168: return (-1);
1.1 jfb 2169: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
2170:
2171: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
2172: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
2173: break;
2174: }
1.13 jfb 2175: rcsnum_free(tnum);
2176:
1.1 jfb 2177: if (rdp == NULL) {
2178: cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
2179: RCS_TOKSTR(rfp));
2180: return (-1);
2181: }
2182:
2183: tok = rcs_gettok(rfp);
2184: if (tok != RCS_TOK_LOG) {
1.50 jfb 2185: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2186: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
2187: RCS_TOKSTR(rfp));
2188: return (-1);
2189: }
2190:
2191: tok = rcs_gettok(rfp);
2192: if (tok != RCS_TOK_STRING) {
1.50 jfb 2193: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2194: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
2195: RCS_TOKSTR(rfp));
2196: return (-1);
2197: }
1.39 joris 2198: rdp->rd_log = cvs_strdup(RCS_TOKSTR(rfp));
1.1 jfb 2199: if (rdp->rd_log == NULL) {
2200: cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
2201: return (-1);
2202: }
2203:
2204: tok = rcs_gettok(rfp);
2205: if (tok != RCS_TOK_TEXT) {
1.50 jfb 2206: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2207: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2208: RCS_TOKSTR(rfp));
2209: return (-1);
2210: }
2211:
2212: tok = rcs_gettok(rfp);
2213: if (tok != RCS_TOK_STRING) {
1.50 jfb 2214: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2215: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2216: RCS_TOKSTR(rfp));
2217: return (-1);
2218: }
2219:
1.74 joris 2220: rdp->rd_text = (u_char *)malloc(RCS_TOKLEN(rfp) + 1);
1.1 jfb 2221: if (rdp->rd_text == NULL) {
2222: cvs_log(LP_ERRNO, "failed to copy RCS delta text");
2223: return (-1);
2224: }
1.74 joris 2225: strlcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp) + 1));
1.42 jfb 2226: rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1 jfb 2227:
2228: return (1);
2229: }
2230:
2231: /*
2232: * rcs_parse_access()
2233: *
2234: * Parse the access list given as value to the `access' keyword.
2235: * Returns 0 on success, or -1 on failure.
2236: */
2237: static int
2238: rcs_parse_access(RCSFILE *rfp)
2239: {
2240: int type;
2241:
2242: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
2243: if (type != RCS_TOK_ID) {
1.50 jfb 2244: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2245: cvs_log(LP_ERR, "unexpected token `%s' in access list",
2246: RCS_TOKSTR(rfp));
2247: return (-1);
2248: }
1.29 jfb 2249:
2250: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
2251: return (-1);
1.1 jfb 2252: }
2253:
2254: return (0);
2255: }
2256:
2257: /*
2258: * rcs_parse_symbols()
2259: *
2260: * Parse the symbol list given as value to the `symbols' keyword.
2261: * Returns 0 on success, or -1 on failure.
2262: */
2263: static int
2264: rcs_parse_symbols(RCSFILE *rfp)
2265: {
2266: int type;
2267: struct rcs_sym *symp;
2268:
2269: for (;;) {
2270: type = rcs_gettok(rfp);
2271: if (type == RCS_TOK_SCOLON)
2272: break;
2273:
1.41 jfb 2274: if (type != RCS_TOK_ID) {
1.50 jfb 2275: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2276: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2277: RCS_TOKSTR(rfp));
2278: return (-1);
2279: }
2280:
2281: symp = (struct rcs_sym *)malloc(sizeof(*symp));
2282: if (symp == NULL) {
1.50 jfb 2283: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 2284: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
2285: return (-1);
2286: }
1.39 joris 2287: symp->rs_name = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 2288: if (symp->rs_name == NULL) {
2289: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
2290: free(symp);
2291: return (-1);
2292: }
2293:
1.1 jfb 2294: symp->rs_num = rcsnum_alloc();
1.11 joris 2295: if (symp->rs_num == NULL) {
2296: cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
1.39 joris 2297: cvs_strfree(symp->rs_name);
1.11 joris 2298: free(symp);
2299: return (-1);
2300: }
1.1 jfb 2301:
2302: type = rcs_gettok(rfp);
2303: if (type != RCS_TOK_COLON) {
1.50 jfb 2304: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2305: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2306: RCS_TOKSTR(rfp));
1.11 joris 2307: rcsnum_free(symp->rs_num);
1.39 joris 2308: cvs_strfree(symp->rs_name);
1.1 jfb 2309: free(symp);
2310: return (-1);
2311: }
2312:
2313: type = rcs_gettok(rfp);
2314: if (type != RCS_TOK_NUM) {
1.50 jfb 2315: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2316: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2317: RCS_TOKSTR(rfp));
1.11 joris 2318: rcsnum_free(symp->rs_num);
1.39 joris 2319: cvs_strfree(symp->rs_name);
1.1 jfb 2320: free(symp);
2321: return (-1);
2322: }
2323:
2324: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
2325: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2326: RCS_TOKSTR(rfp));
1.11 joris 2327: rcsnum_free(symp->rs_num);
1.39 joris 2328: cvs_strfree(symp->rs_name);
1.1 jfb 2329: free(symp);
2330: return (-1);
2331: }
2332:
1.43 jfb 2333: TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1 jfb 2334: }
2335:
2336: return (0);
2337: }
2338:
2339: /*
2340: * rcs_parse_locks()
2341: *
2342: * Parse the lock list given as value to the `locks' keyword.
2343: * Returns 0 on success, or -1 on failure.
2344: */
2345: static int
2346: rcs_parse_locks(RCSFILE *rfp)
2347: {
2348: int type;
2349: struct rcs_lock *lkp;
2350:
2351: for (;;) {
2352: type = rcs_gettok(rfp);
2353: if (type == RCS_TOK_SCOLON)
2354: break;
2355:
2356: if (type != RCS_TOK_ID) {
1.50 jfb 2357: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2358: cvs_log(LP_ERR, "unexpected token `%s' in lock list",
2359: RCS_TOKSTR(rfp));
2360: return (-1);
2361: }
2362:
2363: lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
2364: if (lkp == NULL) {
2365: cvs_log(LP_ERRNO, "failed to allocate RCS lock");
2366: return (-1);
2367: }
1.76 joris 2368:
2369: if ((lkp->rl_name = cvs_strdup(RCS_TOKSTR(rfp))) == NULL) {
2370: cvs_log(LP_ERR, "failed to save locking user");
2371: free(lkp);
2372: return (-1);
2373: }
2374:
1.1 jfb 2375: lkp->rl_num = rcsnum_alloc();
1.11 joris 2376: if (lkp->rl_num == NULL) {
1.76 joris 2377: cvs_strfree(lkp->rl_name);
1.11 joris 2378: free(lkp);
2379: return (-1);
2380: }
1.1 jfb 2381:
2382: type = rcs_gettok(rfp);
2383: if (type != RCS_TOK_COLON) {
1.50 jfb 2384: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2385: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2386: RCS_TOKSTR(rfp));
1.37 tedu 2387: rcsnum_free(lkp->rl_num);
1.76 joris 2388: cvs_strfree(lkp->rl_name);
1.1 jfb 2389: free(lkp);
2390: return (-1);
2391: }
2392:
2393: type = rcs_gettok(rfp);
2394: if (type != RCS_TOK_NUM) {
1.50 jfb 2395: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2396: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2397: RCS_TOKSTR(rfp));
1.37 tedu 2398: rcsnum_free(lkp->rl_num);
1.76 joris 2399: cvs_strfree(lkp->rl_name);
1.1 jfb 2400: free(lkp);
2401: return (-1);
2402: }
2403:
2404: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
2405: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2406: RCS_TOKSTR(rfp));
1.37 tedu 2407: rcsnum_free(lkp->rl_num);
1.76 joris 2408: cvs_strfree(lkp->rl_name);
1.1 jfb 2409: free(lkp);
2410: return (-1);
2411: }
2412:
2413: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
2414: }
2415:
2416: /* check if we have a `strict' */
2417: type = rcs_gettok(rfp);
2418: if (type != RCS_TOK_STRICT) {
2419: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14 deraadt 2420: } else {
1.26 jfb 2421: rfp->rf_flags |= RCS_SLOCK;
1.1 jfb 2422:
2423: type = rcs_gettok(rfp);
2424: if (type != RCS_TOK_SCOLON) {
1.50 jfb 2425: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2426: cvs_log(LP_ERR,
2427: "missing semi-colon after `strict' keyword");
2428: return (-1);
2429: }
2430: }
2431:
2432: return (0);
2433: }
2434:
2435: /*
2436: * rcs_parse_branches()
2437: *
2438: * Parse the list of branches following a `branches' keyword in a delta.
2439: * Returns 0 on success, or -1 on failure.
2440: */
2441: static int
2442: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
2443: {
2444: int type;
2445: struct rcs_branch *brp;
2446:
2447: for (;;) {
2448: type = rcs_gettok(rfp);
2449: if (type == RCS_TOK_SCOLON)
2450: break;
2451:
2452: if (type != RCS_TOK_NUM) {
1.50 jfb 2453: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2454: cvs_log(LP_ERR,
2455: "unexpected token `%s' in list of branches",
2456: RCS_TOKSTR(rfp));
2457: return (-1);
2458: }
2459:
2460: brp = (struct rcs_branch *)malloc(sizeof(*brp));
2461: if (brp == NULL) {
1.50 jfb 2462: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 2463: cvs_log(LP_ERRNO, "failed to allocate RCS branch");
2464: return (-1);
2465: }
1.46 jfb 2466: brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11 joris 2467: if (brp->rb_num == NULL) {
2468: free(brp);
2469: return (-1);
2470: }
1.1 jfb 2471:
2472: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
2473: }
2474:
2475: return (0);
2476: }
2477:
2478: /*
2479: * rcs_freedelta()
2480: *
2481: * Free the contents of a delta structure.
2482: */
1.18 jfb 2483: static void
1.1 jfb 2484: rcs_freedelta(struct rcs_delta *rdp)
2485: {
1.12 jfb 2486: struct rcs_branch *rb;
1.1 jfb 2487: struct rcs_delta *crdp;
2488:
1.12 jfb 2489: if (rdp->rd_num != NULL)
2490: rcsnum_free(rdp->rd_num);
2491: if (rdp->rd_next != NULL)
2492: rcsnum_free(rdp->rd_next);
2493:
1.1 jfb 2494: if (rdp->rd_author != NULL)
1.39 joris 2495: cvs_strfree(rdp->rd_author);
1.1 jfb 2496: if (rdp->rd_state != NULL)
1.39 joris 2497: cvs_strfree(rdp->rd_state);
1.1 jfb 2498: if (rdp->rd_log != NULL)
1.39 joris 2499: cvs_strfree(rdp->rd_log);
1.1 jfb 2500: if (rdp->rd_text != NULL)
1.45 jfb 2501: free(rdp->rd_text);
1.12 jfb 2502:
2503: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
2504: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
2505: rcsnum_free(rb->rb_num);
2506: free(rb);
2507: }
1.1 jfb 2508:
2509: while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
2510: TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
2511: rcs_freedelta(crdp);
2512: }
2513:
2514: free(rdp);
2515: }
2516:
2517: /*
2518: * rcs_freepdata()
2519: *
2520: * Free the contents of the parser data structure.
2521: */
2522: static void
2523: rcs_freepdata(struct rcs_pdata *pd)
2524: {
2525: if (pd->rp_file != NULL)
2526: (void)fclose(pd->rp_file);
2527: if (pd->rp_buf != NULL)
2528: free(pd->rp_buf);
2529: free(pd);
2530: }
2531:
2532: /*
2533: * rcs_gettok()
2534: *
2535: * Get the next RCS token from the string <str>.
2536: */
2537: static int
2538: rcs_gettok(RCSFILE *rfp)
2539: {
2540: u_int i;
2541: int ch, last, type;
1.18 jfb 2542: size_t len;
2543: char *bp;
1.1 jfb 2544: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2545:
2546: type = RCS_TOK_ERR;
2547: bp = pdp->rp_buf;
1.42 jfb 2548: pdp->rp_tlen = 0;
1.1 jfb 2549: *bp = '\0';
2550:
2551: if (pdp->rp_pttype != RCS_TOK_ERR) {
2552: type = pdp->rp_pttype;
2553: strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
2554: pdp->rp_pttype = RCS_TOK_ERR;
2555: return (type);
2556: }
2557:
2558: /* skip leading whitespace */
2559: /* XXX we must skip backspace too for compatibility, should we? */
2560: do {
2561: ch = getc(pdp->rp_file);
2562: if (ch == '\n')
1.18 jfb 2563: pdp->rp_lines++;
1.1 jfb 2564: } while (isspace(ch));
2565:
2566: if (ch == EOF) {
2567: type = RCS_TOK_EOF;
1.14 deraadt 2568: } else if (ch == ';') {
1.1 jfb 2569: type = RCS_TOK_SCOLON;
1.14 deraadt 2570: } else if (ch == ':') {
1.1 jfb 2571: type = RCS_TOK_COLON;
1.14 deraadt 2572: } else if (isalpha(ch)) {
1.31 jfb 2573: type = RCS_TOK_ID;
1.1 jfb 2574: *(bp++) = ch;
1.18 jfb 2575: for (;;) {
1.1 jfb 2576: ch = getc(pdp->rp_file);
1.11 joris 2577: if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1 jfb 2578: ungetc(ch, pdp->rp_file);
2579: break;
2580: }
2581: *(bp++) = ch;
1.42 jfb 2582: pdp->rp_tlen++;
1.18 jfb 2583: if (bp == pdp->rp_bufend - 1) {
2584: len = bp - pdp->rp_buf;
2585: if (rcs_growbuf(rfp) < 0) {
2586: type = RCS_TOK_ERR;
2587: break;
2588: }
2589: bp = pdp->rp_buf + len;
2590: }
1.1 jfb 2591: }
2592: *bp = '\0';
2593:
1.18 jfb 2594: if (type != RCS_TOK_ERR) {
2595: for (i = 0; i < RCS_NKEYS; i++) {
2596: if (strcmp(rcs_keys[i].rk_str,
2597: pdp->rp_buf) == 0) {
2598: type = rcs_keys[i].rk_id;
2599: break;
2600: }
1.1 jfb 2601: }
2602: }
1.14 deraadt 2603: } else if (ch == '@') {
1.1 jfb 2604: /* we have a string */
1.18 jfb 2605: type = RCS_TOK_STRING;
1.1 jfb 2606: for (;;) {
2607: ch = getc(pdp->rp_file);
2608: if (ch == '@') {
2609: ch = getc(pdp->rp_file);
2610: if (ch != '@') {
2611: ungetc(ch, pdp->rp_file);
2612: break;
2613: }
1.14 deraadt 2614: } else if (ch == '\n')
1.18 jfb 2615: pdp->rp_lines++;
1.1 jfb 2616:
2617: *(bp++) = ch;
1.42 jfb 2618: pdp->rp_tlen++;
1.18 jfb 2619: if (bp == pdp->rp_bufend - 1) {
2620: len = bp - pdp->rp_buf;
2621: if (rcs_growbuf(rfp) < 0) {
2622: type = RCS_TOK_ERR;
2623: break;
2624: }
2625: bp = pdp->rp_buf + len;
2626: }
1.1 jfb 2627: }
2628:
2629: *bp = '\0';
1.14 deraadt 2630: } else if (isdigit(ch)) {
1.1 jfb 2631: *(bp++) = ch;
2632: last = ch;
2633: type = RCS_TOK_NUM;
2634:
2635: for (;;) {
2636: ch = getc(pdp->rp_file);
1.18 jfb 2637: if (bp == pdp->rp_bufend)
1.1 jfb 2638: break;
2639: if (!isdigit(ch) && ch != '.') {
2640: ungetc(ch, pdp->rp_file);
2641: break;
2642: }
2643:
2644: if (last == '.' && ch == '.') {
2645: type = RCS_TOK_ERR;
2646: break;
2647: }
2648: last = ch;
2649: *(bp++) = ch;
1.42 jfb 2650: pdp->rp_tlen++;
1.1 jfb 2651: }
1.18 jfb 2652: *bp = '\0';
1.1 jfb 2653: }
2654:
2655: return (type);
2656: }
2657:
2658: /*
2659: * rcs_pushtok()
2660: *
2661: * Push a token back in the parser's token buffer.
2662: */
2663: static int
2664: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2665: {
2666: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2667:
2668: if (pdp->rp_pttype != RCS_TOK_ERR)
2669: return (-1);
2670:
2671: pdp->rp_pttype = type;
2672: strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
2673: return (0);
2674: }
2675:
2676:
2677: /*
2678: * rcs_splitlines()
2679: *
2680: * Split the contents of a file into a list of lines.
2681: */
2682: static struct rcs_foo*
2683: rcs_splitlines(const char *fcont)
2684: {
2685: char *dcp;
2686: struct rcs_foo *foo;
2687: struct rcs_line *lp;
2688:
2689: foo = (struct rcs_foo *)malloc(sizeof(*foo));
2690: if (foo == NULL) {
2691: cvs_log(LP_ERR, "failed to allocate line structure");
2692: return (NULL);
2693: }
2694: TAILQ_INIT(&(foo->rl_lines));
2695: foo->rl_nblines = 0;
2696: foo->rl_data = strdup(fcont);
2697: if (foo->rl_data == NULL) {
2698: cvs_log(LP_ERRNO, "failed to copy file contents");
2699: free(foo);
2700: return (NULL);
2701: }
2702:
2703: /*
2704: * Add a first bogus line with line number 0. This is used so we
2705: * can position the line pointer before 1 when changing the first line
2706: * in rcs_patch().
2707: */
2708: lp = (struct rcs_line *)malloc(sizeof(*lp));
1.38 joris 2709: if (lp == NULL) {
2710: rcs_freefoo(foo);
1.1 jfb 2711: return (NULL);
1.38 joris 2712: }
1.5 vincent 2713:
1.1 jfb 2714: lp->rl_line = NULL;
2715: lp->rl_lineno = 0;
2716: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2717:
2718:
2719: for (dcp = foo->rl_data; *dcp != '\0';) {
2720: lp = (struct rcs_line *)malloc(sizeof(*lp));
2721: if (lp == NULL) {
1.38 joris 2722: rcs_freefoo(foo);
1.1 jfb 2723: cvs_log(LP_ERR, "failed to allocate line entry");
2724: return (NULL);
2725: }
2726:
2727: lp->rl_line = dcp;
2728: lp->rl_lineno = ++(foo->rl_nblines);
2729: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2730:
2731: dcp = strchr(dcp, '\n');
2732: if (dcp == NULL) {
2733: break;
2734: }
2735: *(dcp++) = '\0';
2736: }
2737:
2738: return (foo);
1.5 vincent 2739: }
2740:
2741: static void
2742: rcs_freefoo(struct rcs_foo *fp)
2743: {
2744: struct rcs_line *lp;
2745:
2746: while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
2747: TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
2748: free(lp);
2749: }
2750: free(fp->rl_data);
2751: free(fp);
1.18 jfb 2752: }
2753:
2754: /*
2755: * rcs_growbuf()
2756: *
2757: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2758: * RCS_BUFEXTSIZE.
2759: * In case of failure, the original buffer is left unmodified.
2760: * Returns 0 on success, or -1 on failure.
2761: */
2762: static int
2763: rcs_growbuf(RCSFILE *rf)
2764: {
2765: void *tmp;
2766: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2767:
2768: tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
2769: if (tmp == NULL) {
1.50 jfb 2770: rcs_errno = RCS_ERR_ERRNO;
1.18 jfb 2771: cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
2772: return (-1);
2773: }
2774:
2775: pdp->rp_buf = (char *)tmp;
2776: pdp->rp_blen += RCS_BUFEXTSIZE;
2777: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42 jfb 2778:
2779: return (0);
2780: }
2781:
2782: /*
2783: * rcs_strprint()
2784: *
2785: * Output an RCS string <str> of size <slen> to the stream <stream>. Any
2786: * '@' characters are escaped. Otherwise, the string can contain arbitrary
2787: * binary data.
2788: */
2789: static int
2790: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
2791: {
2792: const u_char *ap, *ep, *sp;
2793: size_t ret;
1.52 jfb 2794:
2795: if (slen == 0)
2796: return (0);
1.42 jfb 2797:
2798: ep = str + slen - 1;
2799:
2800: for (sp = str; sp <= ep;) {
2801: ap = memchr(sp, '@', ep - sp);
2802: if (ap == NULL)
2803: ap = ep;
2804: ret = fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
2805:
2806: if (*ap == '@')
2807: putc('@', stream);
2808: sp = ap + 1;
1.63 joris 2809: }
2810:
2811: return (0);
2812: }
2813:
2814: /*
2815: * rcs_expand_keywords()
2816: *
2817: * Expand any RCS keywords in <line> into <out>
2818: */
2819: static int
2820: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *line, char *out,
2821: size_t len, int mode)
2822: {
2823: int kwtype;
2824: u_int i, j, found;
2825: char *c, *kwstr, *start;
2826: char expbuf[128], buf[128];
2827:
1.65 niallo 2828: kwtype = 0;
2829: kwstr = NULL;
1.63 joris 2830: i = 0;
2831:
2832: /*
2833: * Keyword formats:
2834: * $Keyword$
2835: * $Keyword: value$
2836: */
2837: memset(out, '\0', len);
2838: for (c = line; *c != '\0' && i < len; *c++) {
2839: out[i++] = *c;
2840: if (*c == '$') {
2841: /* remember start of this possible keyword */
2842: start = c;
2843:
2844: /* first following character has to be alphanumeric */
2845: *c++;
2846: if (!isalpha(*c)) {
2847: c = start;
2848: continue;
2849: }
2850:
2851: /* look for any matching keywords */
2852: found = 0;
2853: for (j = 0; j < RCS_NKWORDS; j++) {
2854: if (!strncmp(c, rcs_expkw[j].kw_str,
2855: strlen(rcs_expkw[j].kw_str))) {
2856: found = 1;
2857: kwstr = rcs_expkw[j].kw_str;
2858: kwtype = rcs_expkw[j].kw_type;
2859: break;
2860: }
2861: }
2862:
2863: /* unknown keyword, continue looking */
2864: if (found == 0) {
2865: c = start;
2866: continue;
2867: }
2868:
2869: /* next character has to be ':' or '$' */
2870: c += strlen(kwstr);
2871: if (*c != ':' && *c != '$') {
2872: c = start;
2873: continue;
2874: }
2875:
2876: /*
2877: * if the next character was ':' we need to look for
2878: * an '$' before the end of the line to be sure it is
2879: * in fact a keyword.
2880: */
2881: if (*c == ':') {
2882: while (*c++) {
2883: if (*c == '$' || *c == '\n')
2884: break;
2885: }
2886:
2887: if (*c != '$') {
2888: c = start;
2889: continue;
2890: }
2891: }
2892:
2893: /* start constructing the expansion */
2894: expbuf[0] = '\0';
2895:
2896: if (mode & RCS_KWEXP_NAME) {
2897: strlcat(expbuf, "$", sizeof(expbuf));
2898: strlcat(expbuf, kwstr, sizeof(expbuf));
2899: if (mode & RCS_KWEXP_VAL)
2900: strlcat(expbuf, ": ", sizeof(expbuf));
2901: }
2902:
2903: /*
1.80 reyk 2904: * order matters because of RCS_KW_ID and
2905: * RCS_KW_HEADER here
1.63 joris 2906: */
2907: if (mode & RCS_KWEXP_VAL) {
2908: if (kwtype & RCS_KW_RCSFILE) {
2909: if (!(kwtype & RCS_KW_FULLPATH))
1.80 reyk 2910: strlcat(expbuf,
2911: basename(rcsfile),
1.63 joris 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) {
1.80 reyk 2920: rcsnum_tostr(rdp->rd_num, buf,
2921: sizeof(buf));
1.63 joris 2922: strlcat(buf, " ", sizeof(buf));
2923: strlcat(expbuf, buf, sizeof(expbuf));
2924: }
2925:
2926: if (kwtype & RCS_KW_DATE) {
2927: strftime(buf, sizeof(buf),
1.80 reyk 2928: "%Y/%m/%d %H:%M:%S ",
2929: &rdp->rd_date);
1.63 joris 2930: strlcat(expbuf, buf, sizeof(expbuf));
2931: }
2932:
2933: if (kwtype & RCS_KW_AUTHOR) {
2934: strlcat(expbuf, rdp->rd_author,
2935: sizeof(expbuf));
2936: strlcat(expbuf, " ", sizeof(expbuf));
2937: }
2938:
2939: if (kwtype & RCS_KW_STATE) {
2940: strlcat(expbuf, rdp->rd_state,
2941: sizeof(expbuf));
2942: strlcat(expbuf, " ", sizeof(expbuf));
2943: }
2944:
2945: /* order does not matter anymore below */
2946: if (kwtype & RCS_KW_LOG)
2947: strlcat(expbuf, " ", sizeof(expbuf));
2948:
2949: if (kwtype & RCS_KW_SOURCE) {
1.80 reyk 2950: strlcat(expbuf, rcsfile,
2951: sizeof(expbuf));
1.63 joris 2952: strlcat(expbuf, " ", sizeof(expbuf));
2953: }
2954:
2955: if (kwtype & RCS_KW_NAME)
2956: strlcat(expbuf, " ", sizeof(expbuf));
2957: }
2958:
2959: /* end the expansion */
2960: if (mode & RCS_KWEXP_NAME)
2961: strlcat(expbuf, "$", sizeof(expbuf));
2962:
2963: out[--i] = '\0';
2964: strlcat(out, expbuf, len);
2965: i += strlen(expbuf);
2966: }
1.42 jfb 2967: }
1.81 niallo 2968:
2969: return (0);
2970: }
2971:
2972: /*
2973: * rcs_deltatext_set()
2974: *
2975: * Set deltatext for <rev> in RCS file <rfp> to <dtext>
2976: * Returns -1 on error, 0 on success.
2977: */
2978: int
2979: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, const char *dtext)
2980: {
2981: size_t len;
2982: struct rcs_delta *rdp;
2983:
2984: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2985: return (-1);
2986:
2987: if (rdp->rd_text != NULL)
2988: free(rdp->rd_text);
2989:
2990: len = strlen(dtext);
2991: if ((rdp->rd_text = (u_char *)malloc(len)) == NULL)
2992: return (-1);
2993:
2994: rdp->rd_tlen = len - 1;
2995: strlcpy(rdp->rd_text, dtext, len);
1.18 jfb 2996:
1.86 ! joris 2997: return (0);
! 2998: }
! 2999:
! 3000: /*
! 3001: * rcs_rev_setlog()
! 3002: *
! 3003: * Sets the log message of revision <rev> to <logtext>
! 3004: */
! 3005: int
! 3006: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
! 3007: {
! 3008: struct rcs_delta *rdp;
! 3009: char buf[16];
! 3010:
! 3011: rcsnum_tostr(rev, buf, sizeof(buf));
! 3012: printf("setting log for %s to '%s'\n", buf, logtext);
! 3013:
! 3014: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
! 3015: return (-1);
! 3016:
! 3017: if (rdp->rd_log != NULL)
! 3018: cvs_strfree(rdp->rd_log);
! 3019:
! 3020: if ((rdp->rd_log = cvs_strdup(logtext)) == NULL)
! 3021: return (-1);
! 3022:
! 3023: rfp->rf_flags &= ~RCS_SYNCED;
1.18 jfb 3024: return (0);
1.1 jfb 3025: }