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