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