Annotation of src/usr.bin/cvs/rcs.c, Revision 1.90
1.90 ! niallo 1: /* $OpenBSD: rcs.c,v 1.89 2005/10/11 00:10:16 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: */
1370: do {
1.87 joris 1371: crev = rdp->rd_next;
1.26 jfb 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;
1387: } while (rcsnum_cmp(crev, rev, 0) != 0);
1.1 jfb 1388: }
1389:
1.63 joris 1390: /*
1391: * Do keyword expansion if required.
1392: */
1393: if (rfp->rf_expand != NULL)
1394: expmode = rcs_kwexp_get(rfp);
1395: else
1396: expmode = RCS_KWEXP_DEFAULT;
1397:
1398: if ((rbuf != NULL) && !(expmode & RCS_KWEXP_NONE)) {
1399: if ((dbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
1400: return (rbuf);
1401: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
1402: return (rbuf);
1403:
1404: if (cvs_buf_putc(rbuf, '\0') < 0) {
1405: cvs_buf_free(dbuf);
1406: return (rbuf);
1407: }
1408:
1409: bp = cvs_buf_release(rbuf);
1410: if ((lines = rcs_splitlines((char *)bp)) != NULL) {
1411: res = 0;
1412: TAILQ_FOREACH(lp, &lines->rl_lines, rl_list) {
1413: if (res++ == 0)
1414: continue;
1415: rcs_expand_keywords(rfp->rf_path, rdp,
1416: lp->rl_line, out, sizeof(out), expmode);
1417: cvs_buf_fappend(dbuf, "%s\n", out);
1418: }
1419: rcs_freefoo(lines);
1420: }
1421: free(bp);
1422: }
1423:
1424: return (dbuf);
1.1 jfb 1425: }
1426:
1427: /*
1.52 jfb 1428: * rcs_rev_add()
1429: *
1.53 jfb 1430: * Add a revision to the RCS file <rf>. The new revision's number can be
1431: * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
1432: * new revision will have a number equal to the previous head revision plus
1433: * one). The <msg> argument specifies the log message for that revision, and
1434: * <date> specifies the revision's date (a value of -1 is
1435: * equivalent to using the current time).
1.90 ! niallo 1436: * If <username> is NULL, set the author for this revision to the current user.
! 1437: * Otherwise, set it to <username>.
1.52 jfb 1438: * Returns 0 on success, or -1 on failure.
1439: */
1440: int
1.90 ! niallo 1441: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
! 1442: const char *username)
1.52 jfb 1443: {
1444: time_t now;
1445: struct passwd *pw;
1.83 joris 1446: struct rcs_delta *ordp, *rdp;
1447: RCSNUM *old;
1448:
1449: if ((old = rcsnum_alloc()) == NULL)
1450: return (-1);
1.52 jfb 1451:
1452: if (rev == RCS_HEAD_REV) {
1.83 joris 1453: rcsnum_cpy(rf->rf_head, old, 0);
1.81 niallo 1454: rev = rcsnum_inc(rf->rf_head);
1.83 joris 1455: } else {
1456: if ((rdp = rcs_findrev(rf, rev)) != NULL) {
1457: rcs_errno = RCS_ERR_DUPENT;
1458: return (-1);
1459: }
1460:
1461: ordp = NULL;
1462: rcsnum_cpy(rev, old, 0);
1463: while (ordp == NULL) {
1464: old = rcsnum_dec(old);
1465: ordp = rcs_findrev(rf, old);
1466: }
1.52 jfb 1467: }
1468:
1469: if ((pw = getpwuid(getuid())) == NULL) {
1470: rcs_errno = RCS_ERR_ERRNO;
1471: return (-1);
1472: }
1473:
1474: if ((rdp = (struct rcs_delta *)malloc(sizeof(*rdp))) == NULL) {
1475: rcs_errno = RCS_ERR_ERRNO;
1476: return (-1);
1477: }
1478: memset(rdp, 0, sizeof(*rdp));
1479:
1480: TAILQ_INIT(&(rdp->rd_branches));
1481: TAILQ_INIT(&(rdp->rd_snodes));
1482:
1483: if ((rdp->rd_num = rcsnum_alloc()) == NULL) {
1484: rcs_freedelta(rdp);
1485: return (-1);
1486: }
1487: rcsnum_cpy(rev, rdp->rd_num, 0);
1.83 joris 1488:
1489: if ((rdp->rd_next = rcsnum_alloc()) == NULL) {
1490: rcs_freedelta(rdp);
1491: return (-1);
1492: }
1493: rcsnum_cpy(old, rdp->rd_next, 0);
1494: rcsnum_free(old);
1.52 jfb 1495:
1.90 ! niallo 1496: if (username == NULL)
! 1497: username = pw->pw_name;
! 1498:
! 1499: if ((rdp->rd_author = cvs_strdup(username)) == NULL) {
1.52 jfb 1500: rcs_freedelta(rdp);
1501: return (-1);
1502: }
1503:
1504: if ((rdp->rd_state = cvs_strdup(RCS_STATE_EXP)) == NULL) {
1505: rcs_freedelta(rdp);
1506: return (-1);
1507: }
1508:
1509: if ((rdp->rd_log = cvs_strdup(msg)) == NULL) {
1510: rcs_errno = RCS_ERR_ERRNO;
1511: rcs_freedelta(rdp);
1512: return (-1);
1513: }
1514:
1.53 jfb 1515: if (date != (time_t)(-1))
1516: now = date;
1517: else
1518: time(&now);
1.52 jfb 1519: gmtime_r(&now, &(rdp->rd_date));
1520:
1521: TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1522: rf->rf_ndelta++;
1.81 niallo 1523:
1.64 niallo 1524: /* not synced anymore */
1525: rf->rf_flags &= ~RCS_SYNCED;
1.52 jfb 1526:
1527: return (0);
1528: }
1529:
1530: /*
1531: * rcs_rev_remove()
1532: *
1533: * Remove the revision whose number is <rev> from the RCS file <rf>.
1534: */
1535: int
1536: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
1537: {
1538: int ret;
1539: struct rcs_delta *rdp;
1540:
1541: ret = 0;
1542: if (rev == RCS_HEAD_REV)
1543: rev = rf->rf_head;
1544:
1545: /* do we actually have that revision? */
1546: if ((rdp = rcs_findrev(rf, rev)) == NULL) {
1547: rcs_errno = RCS_ERR_NOENT;
1548: ret = -1;
1549: } else {
1550: /* XXX assumes it's not a sub node */
1551: TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
1552: rf->rf_ndelta--;
1553: rf->rf_flags &= ~RCS_SYNCED;
1554: }
1555:
1556: return (ret);
1557:
1558: }
1559:
1560: /*
1.1 jfb 1561: * rcs_findrev()
1562: *
1563: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1564: * The revision number is given in <rev>.
1565: * Returns a pointer to the delta on success, or NULL on failure.
1566: */
1567: static struct rcs_delta*
1.43 jfb 1568: rcs_findrev(RCSFILE *rfp, const RCSNUM *rev)
1.1 jfb 1569: {
1570: u_int cmplen;
1571: struct rcs_delta *rdp;
1572: struct rcs_dlist *hp;
1.6 vincent 1573: int found;
1.26 jfb 1574:
1.1 jfb 1575: cmplen = 2;
1576: hp = &(rfp->rf_delta);
1577:
1.6 vincent 1578: do {
1579: found = 0;
1580: TAILQ_FOREACH(rdp, hp, rd_list) {
1581: if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
1582: if (cmplen == rev->rn_len)
1583: return (rdp);
1.1 jfb 1584:
1.6 vincent 1585: hp = &(rdp->rd_snodes);
1586: cmplen += 2;
1587: found = 1;
1588: break;
1589: }
1.1 jfb 1590: }
1.6 vincent 1591: } while (found && cmplen < rev->rn_len);
1.1 jfb 1592:
1593: return (NULL);
1.20 jfb 1594: }
1595:
1596: /*
1.26 jfb 1597: * rcs_kwexp_set()
1598: *
1599: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1600: * Returns 0 on success, or -1 on failure.
1601: */
1602: int
1603: rcs_kwexp_set(RCSFILE *file, int mode)
1604: {
1605: int i;
1606: char *tmp, buf[8] = "";
1607:
1608: if (RCS_KWEXP_INVAL(mode))
1609: return (-1);
1610:
1611: i = 0;
1612: if (mode == RCS_KWEXP_NONE)
1613: buf[0] = 'b';
1614: else if (mode == RCS_KWEXP_OLD)
1615: buf[0] = 'o';
1616: else {
1617: if (mode & RCS_KWEXP_NAME)
1618: buf[i++] = 'k';
1619: if (mode & RCS_KWEXP_VAL)
1620: buf[i++] = 'v';
1621: if (mode & RCS_KWEXP_LKR)
1622: buf[i++] = 'l';
1623: }
1624:
1.39 joris 1625: if ((tmp = cvs_strdup(buf)) == NULL) {
1.26 jfb 1626: cvs_log(LP_ERRNO, "%s: failed to copy expansion mode",
1627: file->rf_path);
1628: return (-1);
1629: }
1630:
1.27 jfb 1631: if (file->rf_expand != NULL)
1.39 joris 1632: cvs_strfree(file->rf_expand);
1.26 jfb 1633: file->rf_expand = tmp;
1.64 niallo 1634: /* not synced anymore */
1635: file->rf_flags &= ~RCS_SYNCED;
1.26 jfb 1636:
1637: return (0);
1638: }
1639:
1640: /*
1641: * rcs_kwexp_get()
1642: *
1643: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1644: */
1645: int
1646: rcs_kwexp_get(RCSFILE *file)
1647: {
1648: return rcs_kflag_get(file->rf_expand);
1649: }
1650:
1651: /*
1.20 jfb 1652: * rcs_kflag_get()
1653: *
1654: * Get the keyword expansion mode from a set of character flags given in
1655: * <flags> and return the appropriate flag mask. In case of an error, the
1656: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1657: */
1658: int
1659: rcs_kflag_get(const char *flags)
1660: {
1661: int fl;
1662: size_t len;
1663: const char *fp;
1664:
1665: fl = 0;
1666: len = strlen(flags);
1667:
1668: for (fp = flags; *fp != '\0'; fp++) {
1669: if (*fp == 'k')
1670: fl |= RCS_KWEXP_NAME;
1671: else if (*fp == 'v')
1672: fl |= RCS_KWEXP_VAL;
1673: else if (*fp == 'l')
1674: fl |= RCS_KWEXP_LKR;
1675: else if (*fp == 'o') {
1676: if (len != 1)
1677: fl |= RCS_KWEXP_ERR;
1678: fl |= RCS_KWEXP_OLD;
1679: } else if (*fp == 'b') {
1680: if (len != 1)
1681: fl |= RCS_KWEXP_ERR;
1682: } else /* unknown letter */
1683: fl |= RCS_KWEXP_ERR;
1684: }
1685:
1686: return (fl);
1.32 jfb 1687: }
1688:
1689: /*
1690: * rcs_errstr()
1691: *
1692: * Get the error string matching the RCS error code <code>.
1693: */
1.57 xsa 1694: const char *
1.32 jfb 1695: rcs_errstr(int code)
1696: {
1.50 jfb 1697: const char *esp;
1698:
1699: if ((code < 0) || ((code >= (int)RCS_NERR) && (code != RCS_ERR_ERRNO)))
1700: esp = NULL;
1701: else if (code == RCS_ERR_ERRNO)
1702: esp = strerror(errno);
1703: else
1704: esp = rcs_errstrs[code];
1705: return (esp);
1.1 jfb 1706: }
1707:
1.21 jfb 1708: void
1709: rcs_kflag_usage(void)
1710: {
1711: fprintf(stderr, "Valid expansion modes include:\n"
1.22 jfb 1712: "\t-kkv\tGenerate keywords using the default form.\n"
1713: "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
1714: "\t-kk\tGenerate only keyword names in keyword strings.\n"
1715: "\t-kv\tGenerate only keyword values in keyword strings.\n"
1716: "\t-ko\tGenerate old keyword string "
1.21 jfb 1717: "(no changes from checked in file).\n"
1.22 jfb 1718: "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21 jfb 1719: }
1.1 jfb 1720:
1721: /*
1722: * rcs_parse()
1723: *
1724: * Parse the contents of file <path>, which are in the RCS format.
1725: * Returns 0 on success, or -1 on failure.
1726: */
1.26 jfb 1727: static int
1.1 jfb 1728: rcs_parse(RCSFILE *rfp)
1729: {
1730: int ret;
1731: struct rcs_pdata *pdp;
1732:
1.26 jfb 1733: if (rfp->rf_flags & RCS_PARSED)
1.1 jfb 1734: return (0);
1735:
1.26 jfb 1736: if ((pdp = (struct rcs_pdata *)malloc(sizeof(*pdp))) == NULL) {
1.50 jfb 1737: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1738: cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
1739: return (-1);
1740: }
1741: memset(pdp, 0, sizeof(*pdp));
1742:
1.18 jfb 1743: pdp->rp_lines = 0;
1.1 jfb 1744: pdp->rp_pttype = RCS_TOK_ERR;
1745:
1746: pdp->rp_file = fopen(rfp->rf_path, "r");
1747: if (pdp->rp_file == NULL) {
1.50 jfb 1748: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1749: cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
1750: rcs_freepdata(pdp);
1751: return (-1);
1752: }
1753:
1.59 xsa 1754: pdp->rp_buf = (char *)malloc((size_t)RCS_BUFSIZE);
1.1 jfb 1755: if (pdp->rp_buf == NULL) {
1.50 jfb 1756: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1757: cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
1758: rcs_freepdata(pdp);
1759: return (-1);
1760: }
1761: pdp->rp_blen = RCS_BUFSIZE;
1.18 jfb 1762: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1 jfb 1763:
1764: /* ditch the strict lock */
1.26 jfb 1765: rfp->rf_flags &= ~RCS_SLOCK;
1.1 jfb 1766: rfp->rf_pdata = pdp;
1767:
1.31 jfb 1768: if ((ret = rcs_parse_admin(rfp)) < 0) {
1.1 jfb 1769: rcs_freepdata(pdp);
1770: return (-1);
1.31 jfb 1771: } else if (ret == RCS_TOK_NUM) {
1772: for (;;) {
1773: ret = rcs_parse_delta(rfp);
1774: if (ret == 0)
1775: break;
1776: else if (ret == -1) {
1777: rcs_freepdata(pdp);
1778: return (-1);
1779: }
1.1 jfb 1780: }
1781: }
1782:
1783: ret = rcs_gettok(rfp);
1784: if (ret != RCS_TOK_DESC) {
1.50 jfb 1785: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1786: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1787: RCS_TOKSTR(rfp));
1788: rcs_freepdata(pdp);
1789: return (-1);
1790: }
1791:
1792: ret = rcs_gettok(rfp);
1793: if (ret != RCS_TOK_STRING) {
1.50 jfb 1794: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1795: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1796: RCS_TOKSTR(rfp));
1797: rcs_freepdata(pdp);
1798: return (-1);
1799: }
1800:
1.39 joris 1801: rfp->rf_desc = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1802: if (rfp->rf_desc == NULL) {
1803: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
1804: rcs_freepdata(pdp);
1805: return (-1);
1806: }
1.1 jfb 1807:
1808: for (;;) {
1809: ret = rcs_parse_deltatext(rfp);
1810: if (ret == 0)
1811: break;
1812: else if (ret == -1) {
1813: rcs_freepdata(pdp);
1814: return (-1);
1815: }
1816: }
1817:
1818: rcs_freepdata(pdp);
1819:
1820: rfp->rf_pdata = NULL;
1.26 jfb 1821: rfp->rf_flags |= RCS_PARSED | RCS_SYNCED;
1.1 jfb 1822:
1823: return (0);
1824: }
1825:
1826: /*
1827: * rcs_parse_admin()
1828: *
1829: * Parse the administrative portion of an RCS file.
1.31 jfb 1830: * Returns the type of the first token found after the admin section on
1831: * success, or -1 on failure.
1.1 jfb 1832: */
1833: static int
1834: rcs_parse_admin(RCSFILE *rfp)
1835: {
1836: u_int i;
1837: int tok, ntok, hmask;
1838: struct rcs_key *rk;
1839:
1840: /* hmask is a mask of the headers already encountered */
1841: hmask = 0;
1842: for (;;) {
1843: tok = rcs_gettok(rfp);
1844: if (tok == RCS_TOK_ERR) {
1.50 jfb 1845: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1846: cvs_log(LP_ERR, "parse error in RCS admin section");
1847: return (-1);
1.31 jfb 1848: } else if ((tok == RCS_TOK_NUM) || (tok == RCS_TOK_DESC)) {
1849: /*
1850: * Assume this is the start of the first delta or
1851: * that we are dealing with an empty RCS file and
1852: * we just found the description.
1853: */
1.1 jfb 1854: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31 jfb 1855: return (tok);
1.1 jfb 1856: }
1857:
1858: rk = NULL;
1.18 jfb 1859: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1860: if (rcs_keys[i].rk_id == tok)
1861: rk = &(rcs_keys[i]);
1862:
1863: if (hmask & (1 << tok)) {
1.50 jfb 1864: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1865: cvs_log(LP_ERR, "duplicate RCS key");
1866: return (-1);
1867: }
1868: hmask |= (1 << tok);
1869:
1870: switch (tok) {
1871: case RCS_TOK_HEAD:
1872: case RCS_TOK_BRANCH:
1873: case RCS_TOK_COMMENT:
1874: case RCS_TOK_EXPAND:
1875: ntok = rcs_gettok(rfp);
1876: if (ntok == RCS_TOK_SCOLON)
1877: break;
1878: if (ntok != rk->rk_val) {
1.50 jfb 1879: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1880: cvs_log(LP_ERR,
1881: "invalid value type for RCS key `%s'",
1882: rk->rk_str);
1883: }
1884:
1885: if (tok == RCS_TOK_HEAD) {
1.28 jfb 1886: if (rfp->rf_head == NULL) {
1887: rfp->rf_head = rcsnum_alloc();
1888: if (rfp->rf_head == NULL)
1889: return (-1);
1890: }
1.1 jfb 1891: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1892: rfp->rf_head);
1.14 deraadt 1893: } else if (tok == RCS_TOK_BRANCH) {
1.35 jfb 1894: if (rfp->rf_branch == NULL) {
1895: rfp->rf_branch = rcsnum_alloc();
1896: if (rfp->rf_branch == NULL)
1897: return (-1);
1898: }
1899: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1900: rfp->rf_branch) < 0)
1901: return (-1);
1.14 deraadt 1902: } else if (tok == RCS_TOK_COMMENT) {
1.39 joris 1903: rfp->rf_comment = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1904: if (rfp->rf_comment == NULL) {
1905: cvs_log(LP_ERRNO,
1906: "failed to duplicate rcs token");
1907: return (-1);
1908: }
1.14 deraadt 1909: } else if (tok == RCS_TOK_EXPAND) {
1.39 joris 1910: rfp->rf_expand = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1911: if (rfp->rf_expand == NULL) {
1912: cvs_log(LP_ERRNO,
1913: "failed to duplicate rcs token");
1914: return (-1);
1915: }
1.1 jfb 1916: }
1917:
1918: /* now get the expected semi-colon */
1919: ntok = rcs_gettok(rfp);
1920: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1921: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1922: cvs_log(LP_ERR,
1923: "missing semi-colon after RCS `%s' key",
1.26 jfb 1924: rk->rk_str);
1.1 jfb 1925: return (-1);
1926: }
1927: break;
1928: case RCS_TOK_ACCESS:
1.29 jfb 1929: if (rcs_parse_access(rfp) < 0)
1930: return (-1);
1.1 jfb 1931: break;
1932: case RCS_TOK_SYMBOLS:
1.29 jfb 1933: if (rcs_parse_symbols(rfp) < 0)
1934: return (-1);
1.1 jfb 1935: break;
1936: case RCS_TOK_LOCKS:
1.29 jfb 1937: if (rcs_parse_locks(rfp) < 0)
1938: return (-1);
1.1 jfb 1939: break;
1940: default:
1.50 jfb 1941: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1942: cvs_log(LP_ERR,
1943: "unexpected token `%s' in RCS admin section",
1944: RCS_TOKSTR(rfp));
1945: return (-1);
1946: }
1947: }
1948:
1949: return (0);
1950: }
1951:
1952: /*
1953: * rcs_parse_delta()
1954: *
1955: * Parse an RCS delta section and allocate the structure to store that delta's
1956: * information in the <rfp> delta list.
1957: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1958: * -1 on error.
1959: */
1960: static int
1961: rcs_parse_delta(RCSFILE *rfp)
1962: {
1963: int ret, tok, ntok, hmask;
1964: u_int i;
1965: char *tokstr;
1.3 vincent 1966: RCSNUM *datenum;
1.1 jfb 1967: struct rcs_delta *rdp;
1968: struct rcs_key *rk;
1969:
1970: rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
1971: if (rdp == NULL) {
1.50 jfb 1972: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1973: cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
1974: return (-1);
1975: }
1976: memset(rdp, 0, sizeof(*rdp));
1977:
1978: rdp->rd_num = rcsnum_alloc();
1.11 joris 1979: if (rdp->rd_num == NULL) {
1980: rcs_freedelta(rdp);
1981: return (-1);
1982: }
1.1 jfb 1983: rdp->rd_next = rcsnum_alloc();
1.11 joris 1984: if (rdp->rd_next == NULL) {
1985: rcs_freedelta(rdp);
1986: return (-1);
1987: }
1.1 jfb 1988:
1989: TAILQ_INIT(&(rdp->rd_branches));
1.52 jfb 1990: TAILQ_INIT(&(rdp->rd_snodes));
1.1 jfb 1991:
1992: tok = rcs_gettok(rfp);
1993: if (tok != RCS_TOK_NUM) {
1.52 jfb 1994: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1995: cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
1996: RCS_TOKSTR(rfp));
1997: rcs_freedelta(rdp);
1998: return (-1);
1999: }
2000: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
2001:
2002: hmask = 0;
2003: ret = 0;
2004: tokstr = NULL;
2005:
2006: for (;;) {
2007: tok = rcs_gettok(rfp);
2008: if (tok == RCS_TOK_ERR) {
1.50 jfb 2009: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2010: cvs_log(LP_ERR, "parse error in RCS delta section");
2011: rcs_freedelta(rdp);
2012: return (-1);
1.14 deraadt 2013: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15 tedu 2014: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1 jfb 2015: ret = (tok == RCS_TOK_NUM ? 1 : 0);
2016: break;
2017: }
2018:
2019: rk = NULL;
1.18 jfb 2020: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 2021: if (rcs_keys[i].rk_id == tok)
2022: rk = &(rcs_keys[i]);
2023:
2024: if (hmask & (1 << tok)) {
1.50 jfb 2025: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2026: cvs_log(LP_ERR, "duplicate RCS key");
2027: rcs_freedelta(rdp);
2028: return (-1);
2029: }
2030: hmask |= (1 << tok);
2031:
2032: switch (tok) {
2033: case RCS_TOK_DATE:
2034: case RCS_TOK_AUTHOR:
2035: case RCS_TOK_STATE:
2036: case RCS_TOK_NEXT:
2037: ntok = rcs_gettok(rfp);
2038: if (ntok == RCS_TOK_SCOLON) {
2039: if (rk->rk_flags & RCS_VOPT)
2040: break;
2041: else {
1.50 jfb 2042: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2043: cvs_log(LP_ERR, "missing mandatory "
2044: "value to RCS key `%s'",
2045: rk->rk_str);
2046: rcs_freedelta(rdp);
2047: return (-1);
2048: }
2049: }
2050:
2051: if (ntok != rk->rk_val) {
1.50 jfb 2052: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2053: cvs_log(LP_ERR,
2054: "invalid value type for RCS key `%s'",
2055: rk->rk_str);
2056: rcs_freedelta(rdp);
2057: return (-1);
2058: }
2059:
2060: if (tokstr != NULL)
1.41 jfb 2061: cvs_strfree(tokstr);
1.39 joris 2062: tokstr = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 2063: if (tokstr == NULL) {
1.15 tedu 2064: cvs_log(LP_ERRNO,
1.10 joris 2065: "failed to duplicate rcs token");
2066: rcs_freedelta(rdp);
2067: return (-1);
2068: }
1.1 jfb 2069:
2070: /* now get the expected semi-colon */
2071: ntok = rcs_gettok(rfp);
2072: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 2073: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2074: cvs_log(LP_ERR,
2075: "missing semi-colon after RCS `%s' key",
1.26 jfb 2076: rk->rk_str);
1.41 jfb 2077: cvs_strfree(tokstr);
1.1 jfb 2078: rcs_freedelta(rdp);
2079: return (-1);
2080: }
2081:
2082: if (tok == RCS_TOK_DATE) {
1.25 jfb 2083: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.41 jfb 2084: cvs_strfree(tokstr);
1.11 joris 2085: rcs_freedelta(rdp);
2086: return (-1);
2087: }
1.3 vincent 2088: if (datenum->rn_len != 6) {
1.50 jfb 2089: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2090: cvs_log(LP_ERR,
2091: "RCS date specification has %s "
2092: "fields",
1.3 vincent 2093: (datenum->rn_len > 6) ? "too many" :
1.1 jfb 2094: "missing");
1.41 jfb 2095: cvs_strfree(tokstr);
1.1 jfb 2096: rcs_freedelta(rdp);
1.37 tedu 2097: rcsnum_free(datenum);
2098: return (-1);
1.1 jfb 2099: }
1.3 vincent 2100: rdp->rd_date.tm_year = datenum->rn_id[0];
1.19 jfb 2101: if (rdp->rd_date.tm_year >= 1900)
2102: rdp->rd_date.tm_year -= 1900;
1.3 vincent 2103: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
2104: rdp->rd_date.tm_mday = datenum->rn_id[2];
2105: rdp->rd_date.tm_hour = datenum->rn_id[3];
2106: rdp->rd_date.tm_min = datenum->rn_id[4];
2107: rdp->rd_date.tm_sec = datenum->rn_id[5];
2108: rcsnum_free(datenum);
1.14 deraadt 2109: } else if (tok == RCS_TOK_AUTHOR) {
1.1 jfb 2110: rdp->rd_author = tokstr;
2111: tokstr = NULL;
1.14 deraadt 2112: } else if (tok == RCS_TOK_STATE) {
1.1 jfb 2113: rdp->rd_state = tokstr;
2114: tokstr = NULL;
1.14 deraadt 2115: } else if (tok == RCS_TOK_NEXT) {
1.1 jfb 2116: rcsnum_aton(tokstr, NULL, rdp->rd_next);
2117: }
2118: break;
2119: case RCS_TOK_BRANCHES:
1.46 jfb 2120: if (rcs_parse_branches(rfp, rdp) < 0) {
2121: rcs_freedelta(rdp);
2122: return (-1);
2123: }
1.1 jfb 2124: break;
2125: default:
1.50 jfb 2126: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2127: cvs_log(LP_ERR,
2128: "unexpected token `%s' in RCS delta",
2129: RCS_TOKSTR(rfp));
2130: rcs_freedelta(rdp);
2131: return (-1);
2132: }
2133: }
2134:
1.13 jfb 2135: if (tokstr != NULL)
1.39 joris 2136: cvs_strfree(tokstr);
1.13 jfb 2137:
1.1 jfb 2138: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26 jfb 2139: rfp->rf_ndelta++;
1.1 jfb 2140:
2141: return (ret);
2142: }
2143:
2144: /*
2145: * rcs_parse_deltatext()
2146: *
2147: * Parse an RCS delta text section and fill in the log and text field of the
2148: * appropriate delta section.
2149: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
2150: * -1 on error.
2151: */
2152: static int
2153: rcs_parse_deltatext(RCSFILE *rfp)
2154: {
2155: int tok;
2156: RCSNUM *tnum;
2157: struct rcs_delta *rdp;
2158:
2159: tok = rcs_gettok(rfp);
2160: if (tok == RCS_TOK_EOF)
2161: return (0);
2162:
2163: if (tok != RCS_TOK_NUM) {
1.50 jfb 2164: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2165: cvs_log(LP_ERR,
2166: "unexpected token `%s' at start of RCS delta text",
2167: RCS_TOKSTR(rfp));
2168: return (-1);
2169: }
1.13 jfb 2170:
2171: tnum = rcsnum_alloc();
2172: if (tnum == NULL)
2173: return (-1);
1.1 jfb 2174: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
2175:
2176: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
2177: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
2178: break;
2179: }
1.13 jfb 2180: rcsnum_free(tnum);
2181:
1.1 jfb 2182: if (rdp == NULL) {
2183: cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
2184: RCS_TOKSTR(rfp));
2185: return (-1);
2186: }
2187:
2188: tok = rcs_gettok(rfp);
2189: if (tok != RCS_TOK_LOG) {
1.50 jfb 2190: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2191: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
2192: RCS_TOKSTR(rfp));
2193: return (-1);
2194: }
2195:
2196: tok = rcs_gettok(rfp);
2197: if (tok != RCS_TOK_STRING) {
1.50 jfb 2198: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2199: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
2200: RCS_TOKSTR(rfp));
2201: return (-1);
2202: }
1.39 joris 2203: rdp->rd_log = cvs_strdup(RCS_TOKSTR(rfp));
1.1 jfb 2204: if (rdp->rd_log == NULL) {
2205: cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
2206: return (-1);
2207: }
2208:
2209: tok = rcs_gettok(rfp);
2210: if (tok != RCS_TOK_TEXT) {
1.50 jfb 2211: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2212: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2213: RCS_TOKSTR(rfp));
2214: return (-1);
2215: }
2216:
2217: tok = rcs_gettok(rfp);
2218: if (tok != RCS_TOK_STRING) {
1.50 jfb 2219: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2220: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2221: RCS_TOKSTR(rfp));
2222: return (-1);
2223: }
2224:
1.74 joris 2225: rdp->rd_text = (u_char *)malloc(RCS_TOKLEN(rfp) + 1);
1.1 jfb 2226: if (rdp->rd_text == NULL) {
2227: cvs_log(LP_ERRNO, "failed to copy RCS delta text");
2228: return (-1);
2229: }
1.74 joris 2230: strlcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp) + 1));
1.42 jfb 2231: rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1 jfb 2232:
2233: return (1);
2234: }
2235:
2236: /*
2237: * rcs_parse_access()
2238: *
2239: * Parse the access list given as value to the `access' keyword.
2240: * Returns 0 on success, or -1 on failure.
2241: */
2242: static int
2243: rcs_parse_access(RCSFILE *rfp)
2244: {
2245: int type;
2246:
2247: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
2248: if (type != RCS_TOK_ID) {
1.50 jfb 2249: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2250: cvs_log(LP_ERR, "unexpected token `%s' in access list",
2251: RCS_TOKSTR(rfp));
2252: return (-1);
2253: }
1.29 jfb 2254:
2255: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
2256: return (-1);
1.1 jfb 2257: }
2258:
2259: return (0);
2260: }
2261:
2262: /*
2263: * rcs_parse_symbols()
2264: *
2265: * Parse the symbol list given as value to the `symbols' keyword.
2266: * Returns 0 on success, or -1 on failure.
2267: */
2268: static int
2269: rcs_parse_symbols(RCSFILE *rfp)
2270: {
2271: int type;
2272: struct rcs_sym *symp;
2273:
2274: for (;;) {
2275: type = rcs_gettok(rfp);
2276: if (type == RCS_TOK_SCOLON)
2277: break;
2278:
1.41 jfb 2279: if (type != RCS_TOK_ID) {
1.50 jfb 2280: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2281: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2282: RCS_TOKSTR(rfp));
2283: return (-1);
2284: }
2285:
2286: symp = (struct rcs_sym *)malloc(sizeof(*symp));
2287: if (symp == NULL) {
1.50 jfb 2288: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 2289: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
2290: return (-1);
2291: }
1.39 joris 2292: symp->rs_name = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 2293: if (symp->rs_name == NULL) {
2294: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
2295: free(symp);
2296: return (-1);
2297: }
2298:
1.1 jfb 2299: symp->rs_num = rcsnum_alloc();
1.11 joris 2300: if (symp->rs_num == NULL) {
2301: cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
1.39 joris 2302: cvs_strfree(symp->rs_name);
1.11 joris 2303: free(symp);
2304: return (-1);
2305: }
1.1 jfb 2306:
2307: type = rcs_gettok(rfp);
2308: if (type != RCS_TOK_COLON) {
1.50 jfb 2309: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2310: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2311: RCS_TOKSTR(rfp));
1.11 joris 2312: rcsnum_free(symp->rs_num);
1.39 joris 2313: cvs_strfree(symp->rs_name);
1.1 jfb 2314: free(symp);
2315: return (-1);
2316: }
2317:
2318: type = rcs_gettok(rfp);
2319: if (type != RCS_TOK_NUM) {
1.50 jfb 2320: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2321: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2322: RCS_TOKSTR(rfp));
1.11 joris 2323: rcsnum_free(symp->rs_num);
1.39 joris 2324: cvs_strfree(symp->rs_name);
1.1 jfb 2325: free(symp);
2326: return (-1);
2327: }
2328:
2329: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
2330: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2331: RCS_TOKSTR(rfp));
1.11 joris 2332: rcsnum_free(symp->rs_num);
1.39 joris 2333: cvs_strfree(symp->rs_name);
1.1 jfb 2334: free(symp);
2335: return (-1);
2336: }
2337:
1.43 jfb 2338: TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1 jfb 2339: }
2340:
2341: return (0);
2342: }
2343:
2344: /*
2345: * rcs_parse_locks()
2346: *
2347: * Parse the lock list given as value to the `locks' keyword.
2348: * Returns 0 on success, or -1 on failure.
2349: */
2350: static int
2351: rcs_parse_locks(RCSFILE *rfp)
2352: {
2353: int type;
2354: struct rcs_lock *lkp;
2355:
2356: for (;;) {
2357: type = rcs_gettok(rfp);
2358: if (type == RCS_TOK_SCOLON)
2359: break;
2360:
2361: if (type != RCS_TOK_ID) {
1.50 jfb 2362: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2363: cvs_log(LP_ERR, "unexpected token `%s' in lock list",
2364: RCS_TOKSTR(rfp));
2365: return (-1);
2366: }
2367:
2368: lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
2369: if (lkp == NULL) {
2370: cvs_log(LP_ERRNO, "failed to allocate RCS lock");
2371: return (-1);
2372: }
1.76 joris 2373:
2374: if ((lkp->rl_name = cvs_strdup(RCS_TOKSTR(rfp))) == NULL) {
2375: cvs_log(LP_ERR, "failed to save locking user");
2376: free(lkp);
2377: return (-1);
2378: }
2379:
1.1 jfb 2380: lkp->rl_num = rcsnum_alloc();
1.11 joris 2381: if (lkp->rl_num == NULL) {
1.76 joris 2382: cvs_strfree(lkp->rl_name);
1.11 joris 2383: free(lkp);
2384: return (-1);
2385: }
1.1 jfb 2386:
2387: type = rcs_gettok(rfp);
2388: if (type != RCS_TOK_COLON) {
1.50 jfb 2389: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2390: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2391: RCS_TOKSTR(rfp));
1.37 tedu 2392: rcsnum_free(lkp->rl_num);
1.76 joris 2393: cvs_strfree(lkp->rl_name);
1.1 jfb 2394: free(lkp);
2395: return (-1);
2396: }
2397:
2398: type = rcs_gettok(rfp);
2399: if (type != RCS_TOK_NUM) {
1.50 jfb 2400: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2401: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2402: RCS_TOKSTR(rfp));
1.37 tedu 2403: rcsnum_free(lkp->rl_num);
1.76 joris 2404: cvs_strfree(lkp->rl_name);
1.1 jfb 2405: free(lkp);
2406: return (-1);
2407: }
2408:
2409: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
2410: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2411: RCS_TOKSTR(rfp));
1.37 tedu 2412: rcsnum_free(lkp->rl_num);
1.76 joris 2413: cvs_strfree(lkp->rl_name);
1.1 jfb 2414: free(lkp);
2415: return (-1);
2416: }
2417:
2418: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
2419: }
2420:
2421: /* check if we have a `strict' */
2422: type = rcs_gettok(rfp);
2423: if (type != RCS_TOK_STRICT) {
2424: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14 deraadt 2425: } else {
1.26 jfb 2426: rfp->rf_flags |= RCS_SLOCK;
1.1 jfb 2427:
2428: type = rcs_gettok(rfp);
2429: if (type != RCS_TOK_SCOLON) {
1.50 jfb 2430: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2431: cvs_log(LP_ERR,
2432: "missing semi-colon after `strict' keyword");
2433: return (-1);
2434: }
2435: }
2436:
2437: return (0);
2438: }
2439:
2440: /*
2441: * rcs_parse_branches()
2442: *
2443: * Parse the list of branches following a `branches' keyword in a delta.
2444: * Returns 0 on success, or -1 on failure.
2445: */
2446: static int
2447: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
2448: {
2449: int type;
2450: struct rcs_branch *brp;
2451:
2452: for (;;) {
2453: type = rcs_gettok(rfp);
2454: if (type == RCS_TOK_SCOLON)
2455: break;
2456:
2457: if (type != RCS_TOK_NUM) {
1.50 jfb 2458: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2459: cvs_log(LP_ERR,
2460: "unexpected token `%s' in list of branches",
2461: RCS_TOKSTR(rfp));
2462: return (-1);
2463: }
2464:
2465: brp = (struct rcs_branch *)malloc(sizeof(*brp));
2466: if (brp == NULL) {
1.50 jfb 2467: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 2468: cvs_log(LP_ERRNO, "failed to allocate RCS branch");
2469: return (-1);
2470: }
1.46 jfb 2471: brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11 joris 2472: if (brp->rb_num == NULL) {
2473: free(brp);
2474: return (-1);
2475: }
1.1 jfb 2476:
2477: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
2478: }
2479:
2480: return (0);
2481: }
2482:
2483: /*
2484: * rcs_freedelta()
2485: *
2486: * Free the contents of a delta structure.
2487: */
1.18 jfb 2488: static void
1.1 jfb 2489: rcs_freedelta(struct rcs_delta *rdp)
2490: {
1.12 jfb 2491: struct rcs_branch *rb;
1.1 jfb 2492: struct rcs_delta *crdp;
2493:
1.12 jfb 2494: if (rdp->rd_num != NULL)
2495: rcsnum_free(rdp->rd_num);
2496: if (rdp->rd_next != NULL)
2497: rcsnum_free(rdp->rd_next);
2498:
1.1 jfb 2499: if (rdp->rd_author != NULL)
1.39 joris 2500: cvs_strfree(rdp->rd_author);
1.1 jfb 2501: if (rdp->rd_state != NULL)
1.39 joris 2502: cvs_strfree(rdp->rd_state);
1.1 jfb 2503: if (rdp->rd_log != NULL)
1.39 joris 2504: cvs_strfree(rdp->rd_log);
1.1 jfb 2505: if (rdp->rd_text != NULL)
1.45 jfb 2506: free(rdp->rd_text);
1.12 jfb 2507:
2508: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
2509: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
2510: rcsnum_free(rb->rb_num);
2511: free(rb);
2512: }
1.1 jfb 2513:
2514: while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
2515: TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
2516: rcs_freedelta(crdp);
2517: }
2518:
2519: free(rdp);
2520: }
2521:
2522: /*
2523: * rcs_freepdata()
2524: *
2525: * Free the contents of the parser data structure.
2526: */
2527: static void
2528: rcs_freepdata(struct rcs_pdata *pd)
2529: {
2530: if (pd->rp_file != NULL)
2531: (void)fclose(pd->rp_file);
2532: if (pd->rp_buf != NULL)
2533: free(pd->rp_buf);
2534: free(pd);
2535: }
2536:
2537: /*
2538: * rcs_gettok()
2539: *
2540: * Get the next RCS token from the string <str>.
2541: */
2542: static int
2543: rcs_gettok(RCSFILE *rfp)
2544: {
2545: u_int i;
2546: int ch, last, type;
1.18 jfb 2547: size_t len;
2548: char *bp;
1.1 jfb 2549: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2550:
2551: type = RCS_TOK_ERR;
2552: bp = pdp->rp_buf;
1.42 jfb 2553: pdp->rp_tlen = 0;
1.1 jfb 2554: *bp = '\0';
2555:
2556: if (pdp->rp_pttype != RCS_TOK_ERR) {
2557: type = pdp->rp_pttype;
2558: strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
2559: pdp->rp_pttype = RCS_TOK_ERR;
2560: return (type);
2561: }
2562:
2563: /* skip leading whitespace */
2564: /* XXX we must skip backspace too for compatibility, should we? */
2565: do {
2566: ch = getc(pdp->rp_file);
2567: if (ch == '\n')
1.18 jfb 2568: pdp->rp_lines++;
1.1 jfb 2569: } while (isspace(ch));
2570:
2571: if (ch == EOF) {
2572: type = RCS_TOK_EOF;
1.14 deraadt 2573: } else if (ch == ';') {
1.1 jfb 2574: type = RCS_TOK_SCOLON;
1.14 deraadt 2575: } else if (ch == ':') {
1.1 jfb 2576: type = RCS_TOK_COLON;
1.14 deraadt 2577: } else if (isalpha(ch)) {
1.31 jfb 2578: type = RCS_TOK_ID;
1.1 jfb 2579: *(bp++) = ch;
1.18 jfb 2580: for (;;) {
1.1 jfb 2581: ch = getc(pdp->rp_file);
1.11 joris 2582: if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1 jfb 2583: ungetc(ch, pdp->rp_file);
2584: break;
2585: }
2586: *(bp++) = ch;
1.42 jfb 2587: pdp->rp_tlen++;
1.18 jfb 2588: if (bp == pdp->rp_bufend - 1) {
2589: len = bp - pdp->rp_buf;
2590: if (rcs_growbuf(rfp) < 0) {
2591: type = RCS_TOK_ERR;
2592: break;
2593: }
2594: bp = pdp->rp_buf + len;
2595: }
1.1 jfb 2596: }
2597: *bp = '\0';
2598:
1.18 jfb 2599: if (type != RCS_TOK_ERR) {
2600: for (i = 0; i < RCS_NKEYS; i++) {
2601: if (strcmp(rcs_keys[i].rk_str,
2602: pdp->rp_buf) == 0) {
2603: type = rcs_keys[i].rk_id;
2604: break;
2605: }
1.1 jfb 2606: }
2607: }
1.14 deraadt 2608: } else if (ch == '@') {
1.1 jfb 2609: /* we have a string */
1.18 jfb 2610: type = RCS_TOK_STRING;
1.1 jfb 2611: for (;;) {
2612: ch = getc(pdp->rp_file);
2613: if (ch == '@') {
2614: ch = getc(pdp->rp_file);
2615: if (ch != '@') {
2616: ungetc(ch, pdp->rp_file);
2617: break;
2618: }
1.14 deraadt 2619: } else if (ch == '\n')
1.18 jfb 2620: pdp->rp_lines++;
1.1 jfb 2621:
2622: *(bp++) = ch;
1.42 jfb 2623: pdp->rp_tlen++;
1.18 jfb 2624: if (bp == pdp->rp_bufend - 1) {
2625: len = bp - pdp->rp_buf;
2626: if (rcs_growbuf(rfp) < 0) {
2627: type = RCS_TOK_ERR;
2628: break;
2629: }
2630: bp = pdp->rp_buf + len;
2631: }
1.1 jfb 2632: }
2633:
2634: *bp = '\0';
1.14 deraadt 2635: } else if (isdigit(ch)) {
1.1 jfb 2636: *(bp++) = ch;
2637: last = ch;
2638: type = RCS_TOK_NUM;
2639:
2640: for (;;) {
2641: ch = getc(pdp->rp_file);
1.18 jfb 2642: if (bp == pdp->rp_bufend)
1.1 jfb 2643: break;
2644: if (!isdigit(ch) && ch != '.') {
2645: ungetc(ch, pdp->rp_file);
2646: break;
2647: }
2648:
2649: if (last == '.' && ch == '.') {
2650: type = RCS_TOK_ERR;
2651: break;
2652: }
2653: last = ch;
2654: *(bp++) = ch;
1.42 jfb 2655: pdp->rp_tlen++;
1.1 jfb 2656: }
1.18 jfb 2657: *bp = '\0';
1.1 jfb 2658: }
2659:
2660: return (type);
2661: }
2662:
2663: /*
2664: * rcs_pushtok()
2665: *
2666: * Push a token back in the parser's token buffer.
2667: */
2668: static int
2669: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2670: {
2671: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2672:
2673: if (pdp->rp_pttype != RCS_TOK_ERR)
2674: return (-1);
2675:
2676: pdp->rp_pttype = type;
2677: strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
2678: return (0);
2679: }
2680:
2681:
2682: /*
2683: * rcs_splitlines()
2684: *
2685: * Split the contents of a file into a list of lines.
2686: */
2687: static struct rcs_foo*
2688: rcs_splitlines(const char *fcont)
2689: {
2690: char *dcp;
2691: struct rcs_foo *foo;
2692: struct rcs_line *lp;
2693:
2694: foo = (struct rcs_foo *)malloc(sizeof(*foo));
2695: if (foo == NULL) {
2696: cvs_log(LP_ERR, "failed to allocate line structure");
2697: return (NULL);
2698: }
2699: TAILQ_INIT(&(foo->rl_lines));
2700: foo->rl_nblines = 0;
2701: foo->rl_data = strdup(fcont);
2702: if (foo->rl_data == NULL) {
2703: cvs_log(LP_ERRNO, "failed to copy file contents");
2704: free(foo);
2705: return (NULL);
2706: }
2707:
2708: /*
2709: * Add a first bogus line with line number 0. This is used so we
2710: * can position the line pointer before 1 when changing the first line
2711: * in rcs_patch().
2712: */
2713: lp = (struct rcs_line *)malloc(sizeof(*lp));
1.38 joris 2714: if (lp == NULL) {
2715: rcs_freefoo(foo);
1.1 jfb 2716: return (NULL);
1.38 joris 2717: }
1.5 vincent 2718:
1.1 jfb 2719: lp->rl_line = NULL;
2720: lp->rl_lineno = 0;
2721: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2722:
2723:
2724: for (dcp = foo->rl_data; *dcp != '\0';) {
2725: lp = (struct rcs_line *)malloc(sizeof(*lp));
2726: if (lp == NULL) {
1.38 joris 2727: rcs_freefoo(foo);
1.1 jfb 2728: cvs_log(LP_ERR, "failed to allocate line entry");
2729: return (NULL);
2730: }
2731:
2732: lp->rl_line = dcp;
2733: lp->rl_lineno = ++(foo->rl_nblines);
2734: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2735:
2736: dcp = strchr(dcp, '\n');
2737: if (dcp == NULL) {
2738: break;
2739: }
2740: *(dcp++) = '\0';
2741: }
2742:
2743: return (foo);
1.5 vincent 2744: }
2745:
2746: static void
2747: rcs_freefoo(struct rcs_foo *fp)
2748: {
2749: struct rcs_line *lp;
2750:
2751: while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
2752: TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
2753: free(lp);
2754: }
2755: free(fp->rl_data);
2756: free(fp);
1.18 jfb 2757: }
2758:
2759: /*
2760: * rcs_growbuf()
2761: *
2762: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2763: * RCS_BUFEXTSIZE.
2764: * In case of failure, the original buffer is left unmodified.
2765: * Returns 0 on success, or -1 on failure.
2766: */
2767: static int
2768: rcs_growbuf(RCSFILE *rf)
2769: {
2770: void *tmp;
2771: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2772:
2773: tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
2774: if (tmp == NULL) {
1.50 jfb 2775: rcs_errno = RCS_ERR_ERRNO;
1.18 jfb 2776: cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
2777: return (-1);
2778: }
2779:
2780: pdp->rp_buf = (char *)tmp;
2781: pdp->rp_blen += RCS_BUFEXTSIZE;
2782: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42 jfb 2783:
2784: return (0);
2785: }
2786:
2787: /*
2788: * rcs_strprint()
2789: *
2790: * Output an RCS string <str> of size <slen> to the stream <stream>. Any
2791: * '@' characters are escaped. Otherwise, the string can contain arbitrary
2792: * binary data.
2793: */
2794: static int
2795: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
2796: {
2797: const u_char *ap, *ep, *sp;
2798: size_t ret;
1.52 jfb 2799:
2800: if (slen == 0)
2801: return (0);
1.42 jfb 2802:
2803: ep = str + slen - 1;
2804:
2805: for (sp = str; sp <= ep;) {
2806: ap = memchr(sp, '@', ep - sp);
2807: if (ap == NULL)
2808: ap = ep;
2809: ret = fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
2810:
2811: if (*ap == '@')
2812: putc('@', stream);
2813: sp = ap + 1;
1.63 joris 2814: }
2815:
2816: return (0);
2817: }
2818:
2819: /*
2820: * rcs_expand_keywords()
2821: *
2822: * Expand any RCS keywords in <line> into <out>
2823: */
2824: static int
2825: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *line, char *out,
2826: size_t len, int mode)
2827: {
2828: int kwtype;
2829: u_int i, j, found;
2830: char *c, *kwstr, *start;
2831: char expbuf[128], buf[128];
2832:
1.65 niallo 2833: kwtype = 0;
2834: kwstr = NULL;
1.63 joris 2835: i = 0;
2836:
2837: /*
2838: * Keyword formats:
2839: * $Keyword$
2840: * $Keyword: value$
2841: */
2842: memset(out, '\0', len);
2843: for (c = line; *c != '\0' && i < len; *c++) {
2844: out[i++] = *c;
2845: if (*c == '$') {
2846: /* remember start of this possible keyword */
2847: start = c;
2848:
2849: /* first following character has to be alphanumeric */
2850: *c++;
2851: if (!isalpha(*c)) {
2852: c = start;
2853: continue;
2854: }
2855:
2856: /* look for any matching keywords */
2857: found = 0;
2858: for (j = 0; j < RCS_NKWORDS; j++) {
2859: if (!strncmp(c, rcs_expkw[j].kw_str,
2860: strlen(rcs_expkw[j].kw_str))) {
2861: found = 1;
2862: kwstr = rcs_expkw[j].kw_str;
2863: kwtype = rcs_expkw[j].kw_type;
2864: break;
2865: }
2866: }
2867:
2868: /* unknown keyword, continue looking */
2869: if (found == 0) {
2870: c = start;
2871: continue;
2872: }
2873:
2874: /* next character has to be ':' or '$' */
2875: c += strlen(kwstr);
2876: if (*c != ':' && *c != '$') {
2877: c = start;
2878: continue;
2879: }
2880:
2881: /*
2882: * if the next character was ':' we need to look for
2883: * an '$' before the end of the line to be sure it is
2884: * in fact a keyword.
2885: */
2886: if (*c == ':') {
2887: while (*c++) {
2888: if (*c == '$' || *c == '\n')
2889: break;
2890: }
2891:
2892: if (*c != '$') {
2893: c = start;
2894: continue;
2895: }
2896: }
2897:
2898: /* start constructing the expansion */
2899: expbuf[0] = '\0';
2900:
2901: if (mode & RCS_KWEXP_NAME) {
2902: strlcat(expbuf, "$", sizeof(expbuf));
2903: strlcat(expbuf, kwstr, sizeof(expbuf));
2904: if (mode & RCS_KWEXP_VAL)
2905: strlcat(expbuf, ": ", sizeof(expbuf));
2906: }
2907:
2908: /*
1.80 reyk 2909: * order matters because of RCS_KW_ID and
2910: * RCS_KW_HEADER here
1.63 joris 2911: */
2912: if (mode & RCS_KWEXP_VAL) {
2913: if (kwtype & RCS_KW_RCSFILE) {
2914: if (!(kwtype & RCS_KW_FULLPATH))
1.80 reyk 2915: strlcat(expbuf,
2916: basename(rcsfile),
1.63 joris 2917: sizeof(expbuf));
2918: else
2919: strlcat(expbuf, rcsfile,
2920: sizeof(expbuf));
2921: strlcat(expbuf, " ", sizeof(expbuf));
2922: }
2923:
2924: if (kwtype & RCS_KW_REVISION) {
1.80 reyk 2925: rcsnum_tostr(rdp->rd_num, buf,
2926: sizeof(buf));
1.63 joris 2927: strlcat(buf, " ", sizeof(buf));
2928: strlcat(expbuf, buf, sizeof(expbuf));
2929: }
2930:
2931: if (kwtype & RCS_KW_DATE) {
2932: strftime(buf, sizeof(buf),
1.80 reyk 2933: "%Y/%m/%d %H:%M:%S ",
2934: &rdp->rd_date);
1.63 joris 2935: strlcat(expbuf, buf, sizeof(expbuf));
2936: }
2937:
2938: if (kwtype & RCS_KW_AUTHOR) {
2939: strlcat(expbuf, rdp->rd_author,
2940: sizeof(expbuf));
2941: strlcat(expbuf, " ", sizeof(expbuf));
2942: }
2943:
2944: if (kwtype & RCS_KW_STATE) {
2945: strlcat(expbuf, rdp->rd_state,
2946: sizeof(expbuf));
2947: strlcat(expbuf, " ", sizeof(expbuf));
2948: }
2949:
2950: /* order does not matter anymore below */
2951: if (kwtype & RCS_KW_LOG)
2952: strlcat(expbuf, " ", sizeof(expbuf));
2953:
2954: if (kwtype & RCS_KW_SOURCE) {
1.80 reyk 2955: strlcat(expbuf, rcsfile,
2956: sizeof(expbuf));
1.63 joris 2957: strlcat(expbuf, " ", sizeof(expbuf));
2958: }
2959:
2960: if (kwtype & RCS_KW_NAME)
2961: strlcat(expbuf, " ", sizeof(expbuf));
2962: }
2963:
2964: /* end the expansion */
2965: if (mode & RCS_KWEXP_NAME)
2966: strlcat(expbuf, "$", sizeof(expbuf));
2967:
2968: out[--i] = '\0';
2969: strlcat(out, expbuf, len);
2970: i += strlen(expbuf);
2971: }
1.42 jfb 2972: }
1.81 niallo 2973:
2974: return (0);
2975: }
2976:
2977: /*
2978: * rcs_deltatext_set()
2979: *
2980: * Set deltatext for <rev> in RCS file <rfp> to <dtext>
2981: * Returns -1 on error, 0 on success.
2982: */
2983: int
2984: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, const char *dtext)
2985: {
2986: size_t len;
2987: struct rcs_delta *rdp;
2988:
2989: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2990: return (-1);
2991:
2992: if (rdp->rd_text != NULL)
2993: free(rdp->rd_text);
2994:
2995: len = strlen(dtext);
2996: if ((rdp->rd_text = (u_char *)malloc(len)) == NULL)
2997: return (-1);
2998:
2999: rdp->rd_tlen = len - 1;
3000: strlcpy(rdp->rd_text, dtext, len);
1.18 jfb 3001:
1.86 joris 3002: return (0);
3003: }
3004:
3005: /*
3006: * rcs_rev_setlog()
3007: *
3008: * Sets the log message of revision <rev> to <logtext>
3009: */
3010: int
3011: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
3012: {
3013: struct rcs_delta *rdp;
3014: char buf[16];
3015:
3016: rcsnum_tostr(rev, buf, sizeof(buf));
3017: printf("setting log for %s to '%s'\n", buf, logtext);
3018:
3019: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
3020: return (-1);
3021:
3022: if (rdp->rd_log != NULL)
3023: cvs_strfree(rdp->rd_log);
3024:
3025: if ((rdp->rd_log = cvs_strdup(logtext)) == NULL)
3026: return (-1);
3027:
3028: rfp->rf_flags &= ~RCS_SYNCED;
1.18 jfb 3029: return (0);
1.1 jfb 3030: }