Annotation of src/usr.bin/cvs/rcs.c, Revision 1.239
1.239 ! tobias 1: /* $OpenBSD: rcs.c,v 1.238 2008/01/13 11:11:08 tobias 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:
1.210 otto 27: #include <sys/stat.h>
28:
29: #include <ctype.h>
30: #include <errno.h>
31: #include <libgen.h>
32: #include <pwd.h>
33: #include <stdlib.h>
34: #include <string.h>
35: #include <unistd.h>
1.1 jfb 36:
1.219 tobias 37: #include "atomicio.h"
1.94 joris 38: #include "cvs.h"
1.172 joris 39: #include "diff.h"
1.1 jfb 40: #include "rcs.h"
41:
1.57 xsa 42: #define RCS_BUFSIZE 16384
43: #define RCS_BUFEXTSIZE 8192
1.117 niallo 44: #define RCS_KWEXP_SIZE 1024
1.1 jfb 45:
46: /* RCS token types */
1.57 xsa 47: #define RCS_TOK_ERR -1
48: #define RCS_TOK_EOF 0
49: #define RCS_TOK_NUM 1
50: #define RCS_TOK_ID 2
51: #define RCS_TOK_STRING 3
52: #define RCS_TOK_SCOLON 4
53: #define RCS_TOK_COLON 5
54:
55: #define RCS_TOK_HEAD 8
56: #define RCS_TOK_BRANCH 9
57: #define RCS_TOK_ACCESS 10
58: #define RCS_TOK_SYMBOLS 11
59: #define RCS_TOK_LOCKS 12
60: #define RCS_TOK_COMMENT 13
61: #define RCS_TOK_EXPAND 14
62: #define RCS_TOK_DATE 15
63: #define RCS_TOK_AUTHOR 16
64: #define RCS_TOK_STATE 17
65: #define RCS_TOK_NEXT 18
66: #define RCS_TOK_BRANCHES 19
67: #define RCS_TOK_DESC 20
68: #define RCS_TOK_LOG 21
69: #define RCS_TOK_TEXT 22
70: #define RCS_TOK_STRICT 23
1.1 jfb 71:
1.57 xsa 72: #define RCS_ISKEY(t) (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
1.1 jfb 73:
1.57 xsa 74: #define RCS_NOSCOL 0x01 /* no terminating semi-colon */
75: #define RCS_VOPT 0x02 /* value is optional */
1.1 jfb 76:
1.218 tobias 77: #define ANNOTATE_NEVER 0
78: #define ANNOTATE_NOW 1
79: #define ANNOTATE_LATER 2
80:
1.1 jfb 81: /* opaque parse data */
82: struct rcs_pdata {
1.57 xsa 83: u_int rp_lines;
1.1 jfb 84:
1.57 xsa 85: char *rp_buf;
86: size_t rp_blen;
87: char *rp_bufend;
88: size_t rp_tlen;
1.1 jfb 89:
90: /* pushback token buffer */
1.57 xsa 91: char rp_ptok[128];
92: int rp_pttype; /* token type, RCS_TOK_ERR if no token */
1.1 jfb 93:
1.57 xsa 94: FILE *rp_file;
1.1 jfb 95: };
96:
1.57 xsa 97: #define RCS_TOKSTR(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
98: #define RCS_TOKLEN(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_tlen
1.1 jfb 99:
1.47 jfb 100: /* invalid characters in RCS symbol names */
1.49 jfb 101: static const char rcs_sym_invch[] = RCS_SYM_INVALCHAR;
1.47 jfb 102:
1.51 jfb 103: /* comment leaders, depending on the file's suffix */
104: static const struct rcs_comment {
1.57 xsa 105: const char *rc_suffix;
106: const char *rc_cstr;
1.51 jfb 107: } rcs_comments[] = {
108: { "1", ".\\\" " },
109: { "2", ".\\\" " },
110: { "3", ".\\\" " },
111: { "4", ".\\\" " },
112: { "5", ".\\\" " },
113: { "6", ".\\\" " },
114: { "7", ".\\\" " },
115: { "8", ".\\\" " },
116: { "9", ".\\\" " },
117: { "a", "-- " }, /* Ada */
118: { "ada", "-- " },
119: { "adb", "-- " },
120: { "asm", ";; " }, /* assembler (MS-DOS) */
121: { "ads", "-- " }, /* Ada */
122: { "bat", ":: " }, /* batch (MS-DOS) */
123: { "body", "-- " }, /* Ada */
124: { "c", " * " }, /* C */
125: { "c++", "// " }, /* C++ */
126: { "cc", "// " },
127: { "cpp", "// " },
128: { "cxx", "// " },
129: { "m", "// " }, /* Objective-C */
130: { "cl", ";;; " }, /* Common Lisp */
131: { "cmd", ":: " }, /* command (OS/2) */
132: { "cmf", "c " }, /* CM Fortran */
133: { "csh", "# " }, /* shell */
134: { "e", "# " }, /* efl */
135: { "epsf", "% " }, /* encapsulated postscript */
136: { "epsi", "% " }, /* encapsulated postscript */
137: { "el", "; " }, /* Emacs Lisp */
138: { "f", "c " }, /* Fortran */
139: { "for", "c " },
140: { "h", " * " }, /* C-header */
141: { "hh", "// " }, /* C++ header */
142: { "hpp", "// " },
143: { "hxx", "// " },
144: { "in", "# " }, /* for Makefile.in */
145: { "l", " * " }, /* lex */
146: { "mac", ";; " }, /* macro (DEC-10, MS-DOS, PDP-11, VMS, etc) */
147: { "mak", "# " }, /* makefile, e.g. Visual C++ */
148: { "me", ".\\\" " }, /* me-macros t/nroff */
149: { "ml", "; " }, /* mocklisp */
150: { "mm", ".\\\" " }, /* mm-macros t/nroff */
151: { "ms", ".\\\" " }, /* ms-macros t/nroff */
152: { "man", ".\\\" " }, /* man-macros t/nroff */
153: { "p", " * " }, /* pascal */
154: { "pas", " * " },
1.52 jfb 155: { "pl", "# " }, /* Perl (conflict with Prolog) */
156: { "pm", "# " }, /* Perl module */
1.51 jfb 157: { "ps", "% " }, /* postscript */
158: { "psw", "% " }, /* postscript wrap */
159: { "pswm", "% " }, /* postscript wrap */
160: { "r", "# " }, /* ratfor */
161: { "rc", " * " }, /* Microsoft Windows resource file */
162: { "red", "% " }, /* psl/rlisp */
163: { "sh", "# " }, /* shell */
164: { "sl", "% " }, /* psl */
165: { "spec", "-- " }, /* Ada */
166: { "tex", "% " }, /* tex */
167: { "y", " * " }, /* yacc */
168: { "ye", " * " }, /* yacc-efl */
169: { "yr", " * " }, /* yacc-ratfor */
170: };
171:
1.128 niallo 172: struct rcs_kw rcs_expkw[] = {
173: { "Author", RCS_KW_AUTHOR },
174: { "Date", RCS_KW_DATE },
175: { "Header", RCS_KW_HEADER },
176: { "Id", RCS_KW_ID },
1.229 tobias 177: { "Locker", RCS_KW_LOCKER },
1.128 niallo 178: { "Log", RCS_KW_LOG },
179: { "Name", RCS_KW_NAME },
180: { "RCSfile", RCS_KW_RCSFILE },
181: { "Revision", RCS_KW_REVISION },
182: { "Source", RCS_KW_SOURCE },
183: { "State", RCS_KW_STATE },
1.211 niallo 184: { "Mdocdate", RCS_KW_MDOCDATE },
1.128 niallo 185: };
186:
1.57 xsa 187: #define NB_COMTYPES (sizeof(rcs_comments)/sizeof(rcs_comments[0]))
1.51 jfb 188:
1.1 jfb 189: static struct rcs_key {
1.57 xsa 190: char rk_str[16];
191: int rk_id;
192: int rk_val;
193: int rk_flags;
1.1 jfb 194: } rcs_keys[] = {
195: { "access", RCS_TOK_ACCESS, RCS_TOK_ID, RCS_VOPT },
1.41 jfb 196: { "author", RCS_TOK_AUTHOR, RCS_TOK_ID, 0 },
1.1 jfb 197: { "branch", RCS_TOK_BRANCH, RCS_TOK_NUM, RCS_VOPT },
198: { "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM, RCS_VOPT },
199: { "comment", RCS_TOK_COMMENT, RCS_TOK_STRING, RCS_VOPT },
200: { "date", RCS_TOK_DATE, RCS_TOK_NUM, 0 },
201: { "desc", RCS_TOK_DESC, RCS_TOK_STRING, RCS_NOSCOL },
202: { "expand", RCS_TOK_EXPAND, RCS_TOK_STRING, RCS_VOPT },
203: { "head", RCS_TOK_HEAD, RCS_TOK_NUM, RCS_VOPT },
204: { "locks", RCS_TOK_LOCKS, RCS_TOK_ID, 0 },
205: { "log", RCS_TOK_LOG, RCS_TOK_STRING, RCS_NOSCOL },
206: { "next", RCS_TOK_NEXT, RCS_TOK_NUM, RCS_VOPT },
1.41 jfb 207: { "state", RCS_TOK_STATE, RCS_TOK_ID, RCS_VOPT },
1.1 jfb 208: { "strict", RCS_TOK_STRICT, 0, 0, },
209: { "symbols", RCS_TOK_SYMBOLS, 0, 0 },
210: { "text", RCS_TOK_TEXT, RCS_TOK_STRING, RCS_NOSCOL },
211: };
212:
1.57 xsa 213: #define RCS_NKEYS (sizeof(rcs_keys)/sizeof(rcs_keys[0]))
1.1 jfb 214:
1.32 jfb 215: static const char *rcs_errstrs[] = {
216: "No error",
217: "No such entry",
218: "Duplicate entry found",
219: "Bad RCS number",
1.48 jfb 220: "Invalid RCS symbol",
221: "Parse error",
1.32 jfb 222: };
223:
224: #define RCS_NERR (sizeof(rcs_errstrs)/sizeof(rcs_errstrs[0]))
225:
226: int rcs_errno = RCS_ERR_NOERR;
1.1 jfb 227:
1.218 tobias 228: int rcs_patch_lines(struct cvs_lines *, struct cvs_lines *,
229: struct cvs_line **, struct rcs_delta *);
1.167 ray 230: static void rcs_parse_init(RCSFILE *);
1.57 xsa 231: static int rcs_parse_admin(RCSFILE *);
232: static int rcs_parse_delta(RCSFILE *);
1.146 xsa 233: static void rcs_parse_deltas(RCSFILE *, RCSNUM *);
1.57 xsa 234: static int rcs_parse_deltatext(RCSFILE *);
1.146 xsa 235: static void rcs_parse_deltatexts(RCSFILE *, RCSNUM *);
1.148 xsa 236: static void rcs_parse_desc(RCSFILE *, RCSNUM *);
1.57 xsa 237:
238: static int rcs_parse_access(RCSFILE *);
239: static int rcs_parse_symbols(RCSFILE *);
240: static int rcs_parse_locks(RCSFILE *);
241: static int rcs_parse_branches(RCSFILE *, struct rcs_delta *);
242: static void rcs_freedelta(struct rcs_delta *);
243: static void rcs_freepdata(struct rcs_pdata *);
244: static int rcs_gettok(RCSFILE *);
245: static int rcs_pushtok(RCSFILE *, const char *, int);
1.151 xsa 246: static void rcs_growbuf(RCSFILE *);
1.155 ray 247: static void rcs_strprint(const u_char *, size_t, FILE *);
1.57 xsa 248:
1.233 tobias 249: static void rcs_kwexp_line(char *, struct rcs_delta *, struct cvs_lines *,
250: struct cvs_line *, int mode);
1.26 jfb 251:
1.60 xsa 252: RCSFILE *
1.172 joris 253: rcs_open(const char *path, int fd, int flags, ...)
1.1 jfb 254: {
1.172 joris 255: int mode;
1.228 tobias 256: mode_t fmode, mask;
1.1 jfb 257: RCSFILE *rfp;
1.26 jfb 258: va_list vap;
1.109 joris 259: struct rcs_delta *rdp;
260: struct rcs_lock *lkr;
1.26 jfb 261:
1.152 niallo 262: fmode = S_IRUSR|S_IRGRP|S_IROTH;
1.26 jfb 263: flags &= 0xffff; /* ditch any internal flags */
1.1 jfb 264:
1.172 joris 265: if (flags & RCS_CREATE) {
266: va_start(vap, flags);
267: mode = va_arg(vap, int);
268: va_end(vap);
269: fmode = (mode_t)mode;
1.1 jfb 270: }
1.228 tobias 271:
272: mask = umask(0);
273: umask(mask);
274: fmode &= ~mask;
1.1 jfb 275:
1.161 ray 276: rfp = xcalloc(1, sizeof(*rfp));
1.1 jfb 277:
1.110 joris 278: rfp->rf_path = xstrdup(path);
1.142 niallo 279: rfp->rf_flags = flags | RCS_SLOCK | RCS_SYNCED;
1.26 jfb 280: rfp->rf_mode = fmode;
1.172 joris 281: rfp->fd = fd;
1.175 joris 282: rfp->rf_dead = 0;
1.1 jfb 283:
284: TAILQ_INIT(&(rfp->rf_delta));
1.29 jfb 285: TAILQ_INIT(&(rfp->rf_access));
1.1 jfb 286: TAILQ_INIT(&(rfp->rf_symbols));
287: TAILQ_INIT(&(rfp->rf_locks));
288:
1.167 ray 289: if (!(rfp->rf_flags & RCS_CREATE))
290: rcs_parse_init(rfp);
1.1 jfb 291:
1.109 joris 292: /* fill in rd_locker */
293: TAILQ_FOREACH(lkr, &(rfp->rf_locks), rl_list) {
294: if ((rdp = rcs_findrev(rfp, lkr->rl_num)) == NULL) {
295: rcs_close(rfp);
296: return (NULL);
297: }
298:
1.110 joris 299: rdp->rd_locker = xstrdup(lkr->rl_name);
1.109 joris 300: }
301:
1.1 jfb 302: return (rfp);
303: }
304:
305: /*
306: * rcs_close()
307: *
308: * Close an RCS file handle.
309: */
310: void
311: rcs_close(RCSFILE *rfp)
312: {
313: struct rcs_delta *rdp;
1.71 moritz 314: struct rcs_access *rap;
1.13 jfb 315: struct rcs_lock *rlp;
316: struct rcs_sym *rsp;
1.1 jfb 317:
1.26 jfb 318: if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
319: rcs_write(rfp);
320:
1.1 jfb 321: while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
322: rdp = TAILQ_FIRST(&(rfp->rf_delta));
323: TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
324: rcs_freedelta(rdp);
1.71 moritz 325: }
326:
327: while (!TAILQ_EMPTY(&(rfp->rf_access))) {
328: rap = TAILQ_FIRST(&(rfp->rf_access));
329: TAILQ_REMOVE(&(rfp->rf_access), rap, ra_list);
1.110 joris 330: xfree(rap->ra_name);
331: xfree(rap);
1.1 jfb 332: }
333:
1.13 jfb 334: while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
335: rsp = TAILQ_FIRST(&(rfp->rf_symbols));
336: TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
337: rcsnum_free(rsp->rs_num);
1.110 joris 338: xfree(rsp->rs_name);
339: xfree(rsp);
1.13 jfb 340: }
341:
342: while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
343: rlp = TAILQ_FIRST(&(rfp->rf_locks));
344: TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
345: rcsnum_free(rlp->rl_num);
1.110 joris 346: xfree(rlp->rl_name);
347: xfree(rlp);
1.13 jfb 348: }
349:
1.1 jfb 350: if (rfp->rf_head != NULL)
351: rcsnum_free(rfp->rf_head);
1.11 joris 352: if (rfp->rf_branch != NULL)
353: rcsnum_free(rfp->rf_branch);
1.1 jfb 354:
355: if (rfp->rf_path != NULL)
1.110 joris 356: xfree(rfp->rf_path);
1.1 jfb 357: if (rfp->rf_comment != NULL)
1.110 joris 358: xfree(rfp->rf_comment);
1.1 jfb 359: if (rfp->rf_expand != NULL)
1.110 joris 360: xfree(rfp->rf_expand);
1.1 jfb 361: if (rfp->rf_desc != NULL)
1.110 joris 362: xfree(rfp->rf_desc);
1.118 joris 363: if (rfp->rf_pdata != NULL)
364: rcs_freepdata(rfp->rf_pdata);
1.110 joris 365: xfree(rfp);
1.1 jfb 366: }
367:
368: /*
369: * rcs_write()
370: *
371: * Write the contents of the RCS file handle <rfp> to disk in the file whose
372: * path is in <rf_path>.
373: */
1.172 joris 374: void
1.1 jfb 375: rcs_write(RCSFILE *rfp)
376: {
377: FILE *fp;
1.214 xsa 378: char buf[1024], numbuf[CVS_REV_BUFSZ], *fn, tmpdir[MAXPATHLEN];
1.29 jfb 379: struct rcs_access *ap;
1.1 jfb 380: struct rcs_sym *symp;
1.46 jfb 381: struct rcs_branch *brp;
1.1 jfb 382: struct rcs_delta *rdp;
1.75 joris 383: struct rcs_lock *lkp;
1.100 xsa 384: size_t len;
1.207 otto 385: int fd, saved_errno;
1.75 joris 386:
1.207 otto 387: fd = -1;
1.1 jfb 388:
1.137 joris 389: if (rfp->rf_flags & RCS_SYNCED)
1.181 joris 390: return;
391:
392: if (cvs_noexec == 1)
1.172 joris 393: return;
1.137 joris 394:
1.117 niallo 395: /* Write operations need the whole file parsed */
396: rcs_parse_deltatexts(rfp, NULL);
1.1 jfb 397:
1.207 otto 398: if (strlcpy(tmpdir, rfp->rf_path, sizeof(tmpdir)) >= sizeof(tmpdir))
399: fatal("rcs_write: truncation");
400: (void)xasprintf(&fn, "%s/rcs.XXXXXXXXXX", dirname(tmpdir));
1.172 joris 401:
1.113 xsa 402: if ((fd = mkstemp(fn)) == -1)
1.172 joris 403: fatal("%s", fn);
1.113 xsa 404:
1.207 otto 405: if ((fp = fdopen(fd, "w")) == NULL) {
1.172 joris 406: saved_errno = errno;
407: (void)unlink(fn);
1.207 otto 408: fatal("fdopen %s: %s", fn, strerror(saved_errno));
1.1 jfb 409: }
410:
1.28 jfb 411: if (rfp->rf_head != NULL)
412: rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
413: else
414: numbuf[0] = '\0';
415:
1.1 jfb 416: fprintf(fp, "head\t%s;\n", numbuf);
1.35 jfb 417:
418: if (rfp->rf_branch != NULL) {
419: rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
420: fprintf(fp, "branch\t%s;\n", numbuf);
421: }
422:
1.29 jfb 423: fputs("access", fp);
424: TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
425: fprintf(fp, "\n\t%s", ap->ra_name);
426: }
427: fputs(";\n", fp);
1.1 jfb 428:
1.85 joris 429: fprintf(fp, "symbols");
1.1 jfb 430: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
431: rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
1.172 joris 432: if (strlcpy(buf, symp->rs_name, sizeof(buf)) >= sizeof(buf) ||
433: strlcat(buf, ":", sizeof(buf)) >= sizeof(buf) ||
434: strlcat(buf, numbuf, sizeof(buf)) >= sizeof(buf))
435: fatal("rcs_write: string overflow");
1.85 joris 436: fprintf(fp, "\n\t%s", buf);
1.1 jfb 437: }
438: fprintf(fp, ";\n");
439:
1.75 joris 440: fprintf(fp, "locks");
441: TAILQ_FOREACH(lkp, &(rfp->rf_locks), rl_list) {
442: rcsnum_tostr(lkp->rl_num, numbuf, sizeof(numbuf));
443: fprintf(fp, "\n\t%s:%s", lkp->rl_name, numbuf);
444: }
445:
446: fprintf(fp, ";");
1.1 jfb 447:
1.26 jfb 448: if (rfp->rf_flags & RCS_SLOCK)
1.1 jfb 449: fprintf(fp, " strict;");
450: fputc('\n', fp);
451:
1.129 xsa 452: fputs("comment\t@", fp);
1.42 jfb 453: if (rfp->rf_comment != NULL) {
1.58 xsa 454: rcs_strprint((const u_char *)rfp->rf_comment,
455: strlen(rfp->rf_comment), fp);
1.42 jfb 456: fputs("@;\n", fp);
1.129 xsa 457: } else
458: fputs("# @;\n", fp);
1.1 jfb 459:
1.42 jfb 460: if (rfp->rf_expand != NULL) {
461: fputs("expand @", fp);
1.58 xsa 462: rcs_strprint((const u_char *)rfp->rf_expand,
463: strlen(rfp->rf_expand), fp);
1.42 jfb 464: fputs("@;\n", fp);
465: }
1.1 jfb 466:
1.46 jfb 467: fputs("\n\n", fp);
1.1 jfb 468:
469: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
470: fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
471: sizeof(numbuf)));
472: fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
1.44 jfb 473: rdp->rd_date.tm_year + 1900, rdp->rd_date.tm_mon + 1,
1.1 jfb 474: rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
475: rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
476: fprintf(fp, "\tauthor %s;\tstate %s;\n",
477: rdp->rd_author, rdp->rd_state);
1.46 jfb 478: fputs("branches", fp);
479: TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
1.232 tobias 480: fprintf(fp, "\n\t%s", rcsnum_tostr(brp->rb_num, numbuf,
1.46 jfb 481: sizeof(numbuf)));
482: }
483: fputs(";\n", fp);
1.1 jfb 484: fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
485: numbuf, sizeof(numbuf)));
486: }
487:
1.42 jfb 488: fputs("\ndesc\n@", fp);
1.141 ray 489: if (rfp->rf_desc != NULL && (len = strlen(rfp->rf_desc)) > 0) {
1.100 xsa 490: rcs_strprint((const u_char *)rfp->rf_desc, len, fp);
491: if (rfp->rf_desc[len-1] != '\n')
492: fputc('\n', fp);
493: }
494: fputs("@\n", fp);
1.1 jfb 495:
496: /* deltatexts */
497: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1.100 xsa 498: fprintf(fp, "\n\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
1.1 jfb 499: sizeof(numbuf)));
1.42 jfb 500: fputs("log\n@", fp);
1.100 xsa 501: if (rdp->rd_log != NULL) {
502: len = strlen(rdp->rd_log);
503: rcs_strprint((const u_char *)rdp->rd_log, len, fp);
504: if (rdp->rd_log[len-1] != '\n')
505: fputc('\n', fp);
506: }
1.42 jfb 507: fputs("@\ntext\n@", fp);
1.100 xsa 508: if (rdp->rd_text != NULL) {
509: rcs_strprint(rdp->rd_text, rdp->rd_tlen, fp);
510: }
511: fputs("@\n", fp);
1.1 jfb 512: }
1.207 otto 513: if (fchmod(fd, rfp->rf_mode) == -1) {
514: saved_errno = errno;
515: (void)unlink(fn);
516: fatal("fchmod %s: %s", fn, strerror(saved_errno));
517: }
518:
1.172 joris 519: (void)fclose(fp);
1.73 joris 520:
1.207 otto 521: if (rename(fn, rfp->rf_path) == -1) {
522: saved_errno = errno;
1.172 joris 523: (void)unlink(fn);
1.207 otto 524: fatal("rename(%s, %s): %s", fn, rfp->rf_path,
525: strerror(saved_errno));
1.172 joris 526: }
1.73 joris 527:
1.172 joris 528: rfp->rf_flags |= RCS_SYNCED;
1.73 joris 529:
1.172 joris 530: if (fn != NULL)
531: xfree(fn);
1.1 jfb 532: }
533:
534: /*
1.43 jfb 535: * rcs_head_get()
536: *
537: * Retrieve the revision number of the head revision for the RCS file <file>.
538: */
1.180 joris 539: RCSNUM *
1.43 jfb 540: rcs_head_get(RCSFILE *file)
541: {
1.213 niallo 542: struct rcs_branch *brp;
543: struct rcs_delta *rdp;
544: RCSNUM *rev, *rootrev;
1.180 joris 545:
1.234 tobias 546: if (file->rf_head == NULL)
547: return NULL;
548:
1.213 niallo 549: rev = rcsnum_alloc();
1.180 joris 550: if (file->rf_branch != NULL) {
1.213 niallo 551: /* we have a default branch, use that to calculate the
552: * real HEAD*/
553: rootrev = rcsnum_alloc();
554: rcsnum_cpy(file->rf_branch, rootrev, 2);
555: if ((rdp = rcs_findrev(file, rootrev)) == NULL)
556: fatal("rcs_head_get: could not find root revision");
557:
558: /* HEAD should be the last revision on the default branch */
559: TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
560: if (TAILQ_NEXT(brp, rb_list) == NULL)
561: break;
562: }
563: rcsnum_free(rootrev);
1.226 tobias 564:
565: if ((rdp = rcs_findrev(file, brp->rb_num)) == NULL)
566: fatal("rcs_head_get: could not find branch revision");
567: while (rdp->rd_next->rn_len != 0)
568: if ((rdp = rcs_findrev(file, rdp->rd_next)) == NULL)
569: fatal("rcs_head_get: could not find "
570: "next branch revision");
571:
572: rcsnum_cpy(rdp->rd_num, rev, 0);
1.195 joris 573: } else {
574: rcsnum_cpy(file->rf_head, rev, 0);
1.180 joris 575: }
576:
1.195 joris 577: return (rev);
1.43 jfb 578: }
579:
580: /*
581: * rcs_head_set()
582: *
583: * Set the revision number of the head revision for the RCS file <file> to
584: * <rev>, which must reference a valid revision within the file.
585: */
586: int
1.117 niallo 587: rcs_head_set(RCSFILE *file, RCSNUM *rev)
1.43 jfb 588: {
1.167 ray 589: if (rcs_findrev(file, rev) == NULL)
1.43 jfb 590: return (-1);
591:
1.111 joris 592: if (file->rf_head == NULL)
593: file->rf_head = rcsnum_alloc();
1.43 jfb 594:
1.111 joris 595: rcsnum_cpy(rev, file->rf_head, 0);
1.70 moritz 596: file->rf_flags &= ~RCS_SYNCED;
1.43 jfb 597: return (0);
598: }
599:
600:
601: /*
1.35 jfb 602: * rcs_branch_get()
603: *
604: * Retrieve the default branch number for the RCS file <file>.
605: * Returns the number on success. If NULL is returned, then there is no
606: * default branch for this file.
607: */
1.60 xsa 608: const RCSNUM *
1.35 jfb 609: rcs_branch_get(RCSFILE *file)
610: {
611: return (file->rf_branch);
612: }
613:
614: /*
615: * rcs_branch_set()
616: *
617: * Set the default branch for the RCS file <file> to <bnum>.
618: * Returns 0 on success, -1 on failure.
619: */
620: int
621: rcs_branch_set(RCSFILE *file, const RCSNUM *bnum)
622: {
1.111 joris 623: if (file->rf_branch == NULL)
624: file->rf_branch = rcsnum_alloc();
1.35 jfb 625:
1.111 joris 626: rcsnum_cpy(bnum, file->rf_branch, 0);
1.70 moritz 627: file->rf_flags &= ~RCS_SYNCED;
1.35 jfb 628: return (0);
629: }
630:
631: /*
1.29 jfb 632: * rcs_access_add()
633: *
634: * Add the login name <login> to the access list for the RCS file <file>.
635: * Returns 0 on success, or -1 on failure.
636: */
637: int
638: rcs_access_add(RCSFILE *file, const char *login)
639: {
640: struct rcs_access *ap;
641:
642: /* first look for duplication */
643: TAILQ_FOREACH(ap, &(file->rf_access), ra_list) {
644: if (strcmp(ap->ra_name, login) == 0) {
1.32 jfb 645: rcs_errno = RCS_ERR_DUPENT;
1.29 jfb 646: return (-1);
647: }
648: }
649:
1.161 ray 650: ap = xmalloc(sizeof(*ap));
1.110 joris 651: ap->ra_name = xstrdup(login);
1.29 jfb 652: TAILQ_INSERT_TAIL(&(file->rf_access), ap, ra_list);
653:
654: /* not synced anymore */
655: file->rf_flags &= ~RCS_SYNCED;
656: return (0);
657: }
658:
659: /*
660: * rcs_access_remove()
661: *
662: * Remove an entry with login name <login> from the access list of the RCS
663: * file <file>.
664: * Returns 0 on success, or -1 on failure.
665: */
666: int
667: rcs_access_remove(RCSFILE *file, const char *login)
668: {
669: struct rcs_access *ap;
670:
671: TAILQ_FOREACH(ap, &(file->rf_access), ra_list)
672: if (strcmp(ap->ra_name, login) == 0)
673: break;
674:
675: if (ap == NULL) {
1.32 jfb 676: rcs_errno = RCS_ERR_NOENT;
1.29 jfb 677: return (-1);
678: }
679:
680: TAILQ_REMOVE(&(file->rf_access), ap, ra_list);
1.110 joris 681: xfree(ap->ra_name);
682: xfree(ap);
1.29 jfb 683:
684: /* not synced anymore */
685: file->rf_flags &= ~RCS_SYNCED;
686: return (0);
687: }
688:
689: /*
1.26 jfb 690: * rcs_sym_add()
1.1 jfb 691: *
692: * Add a symbol to the list of symbols for the RCS file <rfp>. The new symbol
693: * is named <sym> and is bound to the RCS revision <snum>.
694: * Returns 0 on success, or -1 on failure.
695: */
696: int
1.26 jfb 697: rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
1.1 jfb 698: {
699: struct rcs_sym *symp;
700:
1.47 jfb 701: if (!rcs_sym_check(sym)) {
702: rcs_errno = RCS_ERR_BADSYM;
1.69 moritz 703: return (-1);
1.47 jfb 704: }
705:
1.1 jfb 706: /* first look for duplication */
707: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
708: if (strcmp(symp->rs_name, sym) == 0) {
1.32 jfb 709: rcs_errno = RCS_ERR_DUPENT;
1.1 jfb 710: return (-1);
711: }
712: }
713:
1.161 ray 714: symp = xmalloc(sizeof(*symp));
1.110 joris 715: symp->rs_name = xstrdup(sym);
1.111 joris 716: symp->rs_num = rcsnum_alloc();
1.1 jfb 717: rcsnum_cpy(snum, symp->rs_num, 0);
718:
719: TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
720:
721: /* not synced anymore */
1.26 jfb 722: rfp->rf_flags &= ~RCS_SYNCED;
1.1 jfb 723: return (0);
724: }
725:
726: /*
1.27 jfb 727: * rcs_sym_remove()
728: *
729: * Remove the symbol with name <sym> from the symbol list for the RCS file
730: * <file>. If no such symbol is found, the call fails and returns with an
731: * error.
732: * Returns 0 on success, or -1 on failure.
733: */
734: int
735: rcs_sym_remove(RCSFILE *file, const char *sym)
736: {
737: struct rcs_sym *symp;
738:
1.47 jfb 739: if (!rcs_sym_check(sym)) {
740: rcs_errno = RCS_ERR_BADSYM;
1.69 moritz 741: return (-1);
1.47 jfb 742: }
743:
1.27 jfb 744: TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
745: if (strcmp(symp->rs_name, sym) == 0)
746: break;
747:
748: if (symp == NULL) {
1.32 jfb 749: rcs_errno = RCS_ERR_NOENT;
1.27 jfb 750: return (-1);
751: }
752:
753: TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
1.110 joris 754: xfree(symp->rs_name);
1.27 jfb 755: rcsnum_free(symp->rs_num);
1.110 joris 756: xfree(symp);
1.27 jfb 757:
758: /* not synced anymore */
759: file->rf_flags &= ~RCS_SYNCED;
760: return (0);
1.184 xsa 761: }
762:
763: /*
764: * rcs_sym_get()
765: *
766: * Find a specific symbol <sym> entry in the tree of the RCS file <file>.
767: *
768: * Returns a pointer to the symbol on success, or NULL on failure.
769: */
770: struct rcs_sym *
771: rcs_sym_get(RCSFILE *file, const char *sym)
772: {
773: struct rcs_sym *symp;
774:
775: TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
776: if (strcmp(symp->rs_name, sym) == 0)
777: return (symp);
778:
779: return (NULL);
1.27 jfb 780: }
781:
782: /*
783: * rcs_sym_getrev()
784: *
785: * Retrieve the RCS revision number associated with the symbol <sym> for the
786: * RCS file <file>. The returned value is a dynamically-allocated copy and
787: * should be freed by the caller once they are done with it.
788: * Returns the RCSNUM on success, or NULL on failure.
789: */
1.60 xsa 790: RCSNUM *
1.27 jfb 791: rcs_sym_getrev(RCSFILE *file, const char *sym)
792: {
793: RCSNUM *num;
794: struct rcs_sym *symp;
795:
1.47 jfb 796: if (!rcs_sym_check(sym)) {
797: rcs_errno = RCS_ERR_BADSYM;
798: return (NULL);
799: }
800:
1.176 joris 801: if (!strcmp(sym, RCS_HEAD_BRANCH)) {
802: num = rcsnum_alloc();
803: rcsnum_cpy(file->rf_head, num, 0);
804: return (num);
805: }
806:
1.27 jfb 807: num = NULL;
808: TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
809: if (strcmp(symp->rs_name, sym) == 0)
810: break;
811:
1.111 joris 812: if (symp == NULL) {
1.36 jfb 813: rcs_errno = RCS_ERR_NOENT;
1.111 joris 814: } else {
815: num = rcsnum_alloc();
816: rcsnum_cpy(symp->rs_num, num, 0);
1.27 jfb 817: }
818:
819: return (num);
1.47 jfb 820: }
821:
822: /*
823: * rcs_sym_check()
824: *
825: * Check the RCS symbol name <sym> for any unsupported characters.
826: * Returns 1 if the tag is correct, 0 if it isn't valid.
827: */
828: int
829: rcs_sym_check(const char *sym)
830: {
831: int ret;
832: const char *cp;
833:
834: ret = 1;
835: cp = sym;
836: if (!isalpha(*cp++))
837: return (0);
838:
839: for (; *cp != '\0'; cp++)
840: if (!isgraph(*cp) || (strchr(rcs_sym_invch, *cp) != NULL)) {
841: ret = 0;
842: break;
843: }
844:
845: return (ret);
1.30 jfb 846: }
847:
848: /*
849: * rcs_lock_getmode()
850: *
851: * Retrieve the locking mode of the RCS file <file>.
852: */
853: int
854: rcs_lock_getmode(RCSFILE *file)
855: {
856: return (file->rf_flags & RCS_SLOCK) ? RCS_LOCK_STRICT : RCS_LOCK_LOOSE;
857: }
858:
859: /*
860: * rcs_lock_setmode()
861: *
862: * Set the locking mode of the RCS file <file> to <mode>, which must either
863: * be RCS_LOCK_LOOSE or RCS_LOCK_STRICT.
864: * Returns the previous mode on success, or -1 on failure.
865: */
866: int
867: rcs_lock_setmode(RCSFILE *file, int mode)
868: {
869: int pmode;
870: pmode = rcs_lock_getmode(file);
871:
872: if (mode == RCS_LOCK_STRICT)
873: file->rf_flags |= RCS_SLOCK;
874: else if (mode == RCS_LOCK_LOOSE)
875: file->rf_flags &= ~RCS_SLOCK;
1.145 xsa 876: else
877: fatal("rcs_lock_setmode: invalid mode `%d'", mode);
1.30 jfb 878:
1.70 moritz 879: file->rf_flags &= ~RCS_SYNCED;
1.30 jfb 880: return (pmode);
1.27 jfb 881: }
882:
883: /*
1.40 jfb 884: * rcs_lock_add()
885: *
886: * Add an RCS lock for the user <user> on revision <rev>.
887: * Returns 0 on success, or -1 on failure.
888: */
889: int
890: rcs_lock_add(RCSFILE *file, const char *user, RCSNUM *rev)
891: {
892: struct rcs_lock *lkp;
893:
894: /* first look for duplication */
895: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
1.168 deraadt 896: if (strcmp(lkp->rl_name, user) == 0 &&
897: rcsnum_cmp(rev, lkp->rl_num, 0) == 0) {
1.40 jfb 898: rcs_errno = RCS_ERR_DUPENT;
899: return (-1);
900: }
901: }
902:
1.161 ray 903: lkp = xmalloc(sizeof(*lkp));
1.110 joris 904: lkp->rl_name = xstrdup(user);
1.111 joris 905: lkp->rl_num = rcsnum_alloc();
1.68 moritz 906: rcsnum_cpy(rev, lkp->rl_num, 0);
1.40 jfb 907:
908: TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
909:
910: /* not synced anymore */
911: file->rf_flags &= ~RCS_SYNCED;
912: return (0);
913: }
914:
915:
916: /*
917: * rcs_lock_remove()
918: *
919: * Remove the RCS lock on revision <rev>.
920: * Returns 0 on success, or -1 on failure.
921: */
922: int
1.109 joris 923: rcs_lock_remove(RCSFILE *file, const char *user, RCSNUM *rev)
1.40 jfb 924: {
925: struct rcs_lock *lkp;
926:
1.109 joris 927: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
1.168 deraadt 928: if (strcmp(lkp->rl_name, user) == 0 &&
929: rcsnum_cmp(lkp->rl_num, rev, 0) == 0)
1.40 jfb 930: break;
1.109 joris 931: }
1.40 jfb 932:
933: if (lkp == NULL) {
934: rcs_errno = RCS_ERR_NOENT;
935: return (-1);
936: }
937:
938: TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
939: rcsnum_free(lkp->rl_num);
1.110 joris 940: xfree(lkp->rl_name);
941: xfree(lkp);
1.40 jfb 942:
943: /* not synced anymore */
944: file->rf_flags &= ~RCS_SYNCED;
945: return (0);
946: }
947:
948: /*
1.27 jfb 949: * rcs_desc_get()
950: *
951: * Retrieve the description for the RCS file <file>.
952: */
1.57 xsa 953: const char *
1.27 jfb 954: rcs_desc_get(RCSFILE *file)
955: {
956: return (file->rf_desc);
957: }
958:
959: /*
960: * rcs_desc_set()
961: *
962: * Set the description for the RCS file <file>.
963: */
1.149 xsa 964: void
1.27 jfb 965: rcs_desc_set(RCSFILE *file, const char *desc)
966: {
967: char *tmp;
968:
1.110 joris 969: tmp = xstrdup(desc);
1.27 jfb 970: if (file->rf_desc != NULL)
1.110 joris 971: xfree(file->rf_desc);
1.27 jfb 972: file->rf_desc = tmp;
973: file->rf_flags &= ~RCS_SYNCED;
1.51 jfb 974: }
975:
976: /*
977: * rcs_comment_lookup()
978: *
979: * Lookup the assumed comment leader based on a file's suffix.
980: * Returns a pointer to the string on success, or NULL on failure.
981: */
1.57 xsa 982: const char *
1.51 jfb 983: rcs_comment_lookup(const char *filename)
984: {
985: int i;
986: const char *sp;
987:
988: if ((sp = strrchr(filename, '.')) == NULL) {
989: rcs_errno = RCS_ERR_NOENT;
990: return (NULL);
991: }
992: sp++;
993:
994: for (i = 0; i < (int)NB_COMTYPES; i++)
995: if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
996: return (rcs_comments[i].rc_cstr);
997: return (NULL);
1.27 jfb 998: }
999:
1.33 jfb 1000: /*
1001: * rcs_comment_get()
1002: *
1003: * Retrieve the comment leader for the RCS file <file>.
1004: */
1.57 xsa 1005: const char *
1.33 jfb 1006: rcs_comment_get(RCSFILE *file)
1007: {
1008: return (file->rf_comment);
1009: }
1010:
1011: /*
1012: * rcs_comment_set()
1013: *
1014: * Set the comment leader for the RCS file <file>.
1015: */
1.150 xsa 1016: void
1.33 jfb 1017: rcs_comment_set(RCSFILE *file, const char *comment)
1018: {
1019: char *tmp;
1020:
1.110 joris 1021: tmp = xstrdup(comment);
1.33 jfb 1022: if (file->rf_comment != NULL)
1.110 joris 1023: xfree(file->rf_comment);
1.33 jfb 1024: file->rf_comment = tmp;
1025: file->rf_flags &= ~RCS_SYNCED;
1.40 jfb 1026: }
1027:
1.94 joris 1028: int
1.218 tobias 1029: rcs_patch_lines(struct cvs_lines *dlines, struct cvs_lines *plines,
1030: struct cvs_line **alines, struct rcs_delta *rdp)
1.5 vincent 1031: {
1.209 otto 1032: u_char op;
1033: char *ep;
1.94 joris 1034: struct cvs_line *lp, *dlp, *ndlp;
1.5 vincent 1035: int i, lineno, nbln;
1.193 niallo 1036: u_char tmp;
1.1 jfb 1037:
1.94 joris 1038: dlp = TAILQ_FIRST(&(dlines->l_lines));
1039: lp = TAILQ_FIRST(&(plines->l_lines));
1.1 jfb 1040:
1041: /* skip first bogus line */
1.94 joris 1042: for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
1043: lp = TAILQ_NEXT(lp, l_list)) {
1.193 niallo 1044: if (lp->l_len < 2)
1045: fatal("line too short, RCS patch seems broken");
1.94 joris 1046: op = *(lp->l_line);
1.193 niallo 1047: /* NUL-terminate line buffer for strtol() safety. */
1048: tmp = lp->l_line[lp->l_len - 1];
1049: lp->l_line[lp->l_len - 1] = '\0';
1.209 otto 1050: lineno = (int)strtol((char*)(lp->l_line + 1), &ep, 10);
1.193 niallo 1051: if (lineno - 1 > dlines->l_nblines || lineno < 0) {
1052: fatal("invalid line specification in RCS patch");
1053: }
1.1 jfb 1054: ep++;
1055: nbln = (int)strtol(ep, &ep, 10);
1.193 niallo 1056: /* Restore the last byte of the buffer */
1057: lp->l_line[lp->l_len - 1] = tmp;
1058: if (nbln < 0)
1.132 niallo 1059: fatal("invalid line number specification in RCS patch");
1.1 jfb 1060:
1061: /* find the appropriate line */
1062: for (;;) {
1063: if (dlp == NULL)
1064: break;
1.94 joris 1065: if (dlp->l_lineno == lineno)
1.1 jfb 1066: break;
1.94 joris 1067: if (dlp->l_lineno > lineno) {
1068: dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
1069: } else if (dlp->l_lineno < lineno) {
1.133 niallo 1070: if (((ndlp = TAILQ_NEXT(dlp, l_list)) == NULL) ||
1.168 deraadt 1071: ndlp->l_lineno > lineno)
1.1 jfb 1072: break;
1073: dlp = ndlp;
1074: }
1075: }
1.132 niallo 1076: if (dlp == NULL)
1077: fatal("can't find referenced line in RCS patch");
1.1 jfb 1078:
1079: if (op == 'd') {
1080: for (i = 0; (i < nbln) && (dlp != NULL); i++) {
1.94 joris 1081: ndlp = TAILQ_NEXT(dlp, l_list);
1082: TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
1.218 tobias 1083: if (alines != NULL && dlp->l_line != NULL) {
1084: dlp->l_delta = rdp;
1085: alines[dlp->l_lineno_orig - 1] =
1086: dlp;
1087: } else
1088: xfree(dlp);
1.1 jfb 1089: dlp = ndlp;
1.133 niallo 1090: /* last line is gone - reset dlp */
1091: if (dlp == NULL) {
1092: ndlp = TAILQ_LAST(&(dlines->l_lines),
1093: cvs_tqh);
1094: dlp = ndlp;
1095: }
1.1 jfb 1096: }
1.14 deraadt 1097: } else if (op == 'a') {
1.1 jfb 1098: for (i = 0; i < nbln; i++) {
1099: ndlp = lp;
1.94 joris 1100: lp = TAILQ_NEXT(lp, l_list);
1.132 niallo 1101: if (lp == NULL)
1102: fatal("truncated RCS patch");
1.94 joris 1103: TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
1.218 tobias 1104: if (alines != NULL) {
1105: if (lp->l_needsfree == 1)
1106: xfree(lp->l_line);
1107: lp->l_line = NULL;
1108: lp->l_needsfree = 0;
1109: }
1.225 tobias 1110: lp->l_delta = rdp;
1.94 joris 1111: TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
1112: lp, l_list);
1.1 jfb 1113: dlp = lp;
1114:
1115: /* we don't want lookup to block on those */
1.94 joris 1116: lp->l_lineno = lineno;
1.1 jfb 1117:
1118: lp = ndlp;
1119: }
1.132 niallo 1120: } else
1121: fatal("unknown RCS patch operation `%c'", op);
1.1 jfb 1122:
1123: /* last line of the patch, done */
1.94 joris 1124: if (lp->l_lineno == plines->l_nblines)
1.1 jfb 1125: break;
1126: }
1127:
1128: /* once we're done patching, rebuild the line numbers */
1.2 vincent 1129: lineno = 0;
1.94 joris 1130: TAILQ_FOREACH(lp, &(dlines->l_lines), l_list)
1131: lp->l_lineno = lineno++;
1132: dlines->l_nblines = lineno - 1;
1.1 jfb 1133:
1.5 vincent 1134: return (0);
1.1 jfb 1135: }
1136:
1137: /*
1.52 jfb 1138: * rcs_rev_add()
1139: *
1.53 jfb 1140: * Add a revision to the RCS file <rf>. The new revision's number can be
1141: * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
1142: * new revision will have a number equal to the previous head revision plus
1143: * one). The <msg> argument specifies the log message for that revision, and
1144: * <date> specifies the revision's date (a value of -1 is
1145: * equivalent to using the current time).
1.96 xsa 1146: * If <username> is NULL, set the author for this revision to the current user.
1.90 niallo 1147: * Otherwise, set it to <username>.
1.52 jfb 1148: * Returns 0 on success, or -1 on failure.
1149: */
1150: int
1.90 niallo 1151: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
1152: const char *username)
1.52 jfb 1153: {
1154: time_t now;
1.222 joris 1155: RCSNUM *root;
1.52 jfb 1156: struct passwd *pw;
1.180 joris 1157: struct rcs_branch *brp;
1.83 joris 1158: struct rcs_delta *ordp, *rdp;
1159:
1.52 jfb 1160: if (rev == RCS_HEAD_REV) {
1.101 niallo 1161: if (rf->rf_flags & RCS_CREATE) {
1162: if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
1163: return (-1);
1.111 joris 1164: rf->rf_head = rcsnum_alloc();
1.101 niallo 1165: rcsnum_cpy(rev, rf->rf_head, 0);
1166: } else {
1167: rev = rcsnum_inc(rf->rf_head);
1168: }
1.83 joris 1169: } else {
1170: if ((rdp = rcs_findrev(rf, rev)) != NULL) {
1171: rcs_errno = RCS_ERR_DUPENT;
1172: return (-1);
1173: }
1.52 jfb 1174: }
1175:
1.112 xsa 1176: if ((pw = getpwuid(getuid())) == NULL)
1177: fatal("getpwuid failed");
1.52 jfb 1178:
1.161 ray 1179: rdp = xcalloc(1, sizeof(*rdp));
1.52 jfb 1180:
1181: TAILQ_INIT(&(rdp->rd_branches));
1182:
1.111 joris 1183: rdp->rd_num = rcsnum_alloc();
1.52 jfb 1184: rcsnum_cpy(rev, rdp->rd_num, 0);
1.83 joris 1185:
1.111 joris 1186: rdp->rd_next = rcsnum_alloc();
1.92 joris 1187:
1.90 niallo 1188: if (username == NULL)
1189: username = pw->pw_name;
1190:
1.110 joris 1191: rdp->rd_author = xstrdup(username);
1192: rdp->rd_state = xstrdup(RCS_STATE_EXP);
1193: rdp->rd_log = xstrdup(msg);
1.52 jfb 1194:
1.53 jfb 1195: if (date != (time_t)(-1))
1196: now = date;
1197: else
1198: time(&now);
1.52 jfb 1199: gmtime_r(&now, &(rdp->rd_date));
1200:
1.180 joris 1201: if (RCSNUM_ISBRANCHREV(rev))
1202: TAILQ_INSERT_TAIL(&(rf->rf_delta), rdp, rd_list);
1203: else
1204: TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1.52 jfb 1205: rf->rf_ndelta++;
1.81 niallo 1206:
1.180 joris 1207: if (!(rf->rf_flags & RCS_CREATE)) {
1208: if (RCSNUM_ISBRANCHREV(rev)) {
1.222 joris 1209: root = rcsnum_branch_root(rev);
1.180 joris 1210: brp = xmalloc(sizeof(*brp));
1211: brp->rb_num = rcsnum_alloc();
1212: rcsnum_cpy(rdp->rd_num, brp->rb_num, 0);
1.222 joris 1213: if ((ordp = rcs_findrev(rf, root)) == NULL)
1214: fatal("root node not found");
1215: if (TAILQ_EMPTY(&(ordp->rd_branches))) {
1216: TAILQ_INSERT_TAIL(&(ordp->rd_branches),
1217: brp, rb_list);
1218: }
1219:
1.208 otto 1220: ordp = TAILQ_PREV(rdp, rcs_dlist, rd_list);
1.222 joris 1221: if (RCSNUM_ISBRANCHREV(ordp->rd_num))
1222: rcsnum_cpy(rdp->rd_num, ordp->rd_next, 0);
1.180 joris 1223: } else {
1224: ordp = TAILQ_NEXT(rdp, rd_list);
1225: rcsnum_cpy(ordp->rd_num, rdp->rd_next, 0);
1226: }
1227: }
1228:
1.64 niallo 1229: /* not synced anymore */
1230: rf->rf_flags &= ~RCS_SYNCED;
1.52 jfb 1231:
1232: return (0);
1233: }
1234:
1235: /*
1236: * rcs_rev_remove()
1237: *
1238: * Remove the revision whose number is <rev> from the RCS file <rf>.
1239: */
1240: int
1241: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
1242: {
1.194 joris 1243: char *path_tmp1, *path_tmp2;
1.166 joris 1244: struct rcs_delta *rdp, *prevrdp, *nextrdp;
1.209 otto 1245: BUF *prevbuf, *newdiff, *newdeltatext;
1.166 joris 1246:
1.52 jfb 1247: if (rev == RCS_HEAD_REV)
1248: rev = rf->rf_head;
1249:
1250: /* do we actually have that revision? */
1251: if ((rdp = rcs_findrev(rf, rev)) == NULL) {
1252: rcs_errno = RCS_ERR_NOENT;
1.166 joris 1253: return (-1);
1254: }
1255:
1256: /*
1257: * This is confusing, the previous delta is next in the TAILQ list.
1258: * the next delta is the previous one in the TAILQ list.
1259: *
1260: * When the HEAD revision got specified, nextrdp will be NULL.
1261: * When the first revision got specified, prevrdp will be NULL.
1262: */
1263: prevrdp = (struct rcs_delta *)TAILQ_NEXT(rdp, rd_list);
1264: nextrdp = (struct rcs_delta *)TAILQ_PREV(rdp, cvs_tqh, rd_list);
1265:
1266: newdeltatext = NULL;
1.209 otto 1267: prevbuf = NULL;
1.166 joris 1268:
1.168 deraadt 1269: if (prevrdp != NULL && nextrdp != NULL) {
1.166 joris 1270: newdiff = cvs_buf_alloc(64, BUF_AUTOEXT);
1271:
1272: /* calculate new diff */
1.172 joris 1273: (void)xasprintf(&path_tmp1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
1.196 niallo 1274: rcs_rev_write_stmp(rf, nextrdp->rd_num, path_tmp1, 0);
1.166 joris 1275:
1.172 joris 1276: (void)xasprintf(&path_tmp2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
1.196 niallo 1277: rcs_rev_write_stmp(rf, prevrdp->rd_num, path_tmp2, 0);
1.166 joris 1278:
1279: diff_format = D_RCSDIFF;
1.172 joris 1280: if (cvs_diffreg(path_tmp1, path_tmp2, newdiff) == D_ERROR)
1281: fatal("rcs_diffreg failed");
1.166 joris 1282:
1.194 joris 1283: newdeltatext = newdiff;
1.168 deraadt 1284: } else if (nextrdp == NULL && prevrdp != NULL) {
1.194 joris 1285: newdeltatext = prevbuf;
1.166 joris 1286: }
1287:
1288: if (newdeltatext != NULL) {
1289: if (rcs_deltatext_set(rf, prevrdp->rd_num, newdeltatext) < 0)
1290: fatal("error setting new deltatext");
1291: }
1292:
1293: TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
1294:
1295: /* update pointers */
1.168 deraadt 1296: if (prevrdp != NULL && nextrdp != NULL) {
1.166 joris 1297: rcsnum_cpy(prevrdp->rd_num, nextrdp->rd_next, 0);
1298: } else if (prevrdp != NULL) {
1.170 xsa 1299: if (rcs_head_set(rf, prevrdp->rd_num) < 0)
1300: fatal("rcs_head_set failed");
1.166 joris 1301: } else if (nextrdp != NULL) {
1302: rcsnum_free(nextrdp->rd_next);
1303: nextrdp->rd_next = rcsnum_alloc();
1.52 jfb 1304: } else {
1.166 joris 1305: rcsnum_free(rf->rf_head);
1306: rf->rf_head = NULL;
1.52 jfb 1307: }
1308:
1.166 joris 1309: rf->rf_ndelta--;
1310: rf->rf_flags &= ~RCS_SYNCED;
1311:
1312: rcs_freedelta(rdp);
1313:
1314: if (newdeltatext != NULL)
1315: xfree(newdeltatext);
1.52 jfb 1316:
1.172 joris 1317: if (path_tmp1 != NULL)
1318: xfree(path_tmp1);
1319: if (path_tmp2 != NULL)
1320: xfree(path_tmp2);
1321:
1.166 joris 1322: return (0);
1.52 jfb 1323: }
1324:
1325: /*
1.1 jfb 1326: * rcs_findrev()
1327: *
1328: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1329: * The revision number is given in <rev>.
1.156 joris 1330: *
1.1 jfb 1331: * Returns a pointer to the delta on success, or NULL on failure.
1332: */
1.102 xsa 1333: struct rcs_delta *
1.117 niallo 1334: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
1.1 jfb 1335: {
1.178 joris 1336: int isbrev;
1.156 joris 1337: struct rcs_delta *rdp;
1.178 joris 1338:
1339: isbrev = RCSNUM_ISBRANCHREV(rev);
1.26 jfb 1340:
1.117 niallo 1341: /*
1342: * We need to do more parsing if the last revision in the linked list
1343: * is greater than the requested revision.
1344: */
1.156 joris 1345: rdp = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1.168 deraadt 1346: if (rdp == NULL ||
1.178 joris 1347: (!isbrev && rcsnum_cmp(rdp->rd_num, rev, 0) == -1) ||
1348: ((isbrev && rdp->rd_num->rn_len < 4) ||
1349: (isbrev && rcsnum_differ(rev, rdp->rd_num)))) {
1.117 niallo 1350: rcs_parse_deltas(rfp, rev);
1351: }
1352:
1.156 joris 1353: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1.178 joris 1354: if (rcsnum_differ(rdp->rd_num, rev))
1355: continue;
1356: else
1.156 joris 1357: return (rdp);
1358: }
1.1 jfb 1359:
1360: return (NULL);
1.20 jfb 1361: }
1362:
1363: /*
1.26 jfb 1364: * rcs_kwexp_set()
1365: *
1366: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1367: */
1.162 xsa 1368: void
1.26 jfb 1369: rcs_kwexp_set(RCSFILE *file, int mode)
1370: {
1371: int i;
1372: char *tmp, buf[8] = "";
1373:
1374: if (RCS_KWEXP_INVAL(mode))
1.162 xsa 1375: return;
1.26 jfb 1376:
1377: i = 0;
1378: if (mode == RCS_KWEXP_NONE)
1379: buf[0] = 'b';
1380: else if (mode == RCS_KWEXP_OLD)
1381: buf[0] = 'o';
1382: else {
1383: if (mode & RCS_KWEXP_NAME)
1384: buf[i++] = 'k';
1385: if (mode & RCS_KWEXP_VAL)
1386: buf[i++] = 'v';
1387: if (mode & RCS_KWEXP_LKR)
1388: buf[i++] = 'l';
1389: }
1390:
1.110 joris 1391: tmp = xstrdup(buf);
1.27 jfb 1392: if (file->rf_expand != NULL)
1.147 ray 1393: xfree(file->rf_expand);
1.26 jfb 1394: file->rf_expand = tmp;
1.64 niallo 1395: /* not synced anymore */
1396: file->rf_flags &= ~RCS_SYNCED;
1.26 jfb 1397: }
1398:
1399: /*
1400: * rcs_kwexp_get()
1401: *
1402: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1403: */
1404: int
1405: rcs_kwexp_get(RCSFILE *file)
1406: {
1407: return rcs_kflag_get(file->rf_expand);
1408: }
1409:
1410: /*
1.20 jfb 1411: * rcs_kflag_get()
1412: *
1413: * Get the keyword expansion mode from a set of character flags given in
1414: * <flags> and return the appropriate flag mask. In case of an error, the
1415: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1416: */
1417: int
1418: rcs_kflag_get(const char *flags)
1419: {
1420: int fl;
1421: size_t len;
1422: const char *fp;
1423:
1424: fl = 0;
1.235 tobias 1425: if (!(len = strlen(flags)))
1426: return RCS_KWEXP_ERR;
1.20 jfb 1427:
1428: for (fp = flags; *fp != '\0'; fp++) {
1429: if (*fp == 'k')
1430: fl |= RCS_KWEXP_NAME;
1431: else if (*fp == 'v')
1432: fl |= RCS_KWEXP_VAL;
1433: else if (*fp == 'l')
1434: fl |= RCS_KWEXP_LKR;
1435: else if (*fp == 'o') {
1436: if (len != 1)
1437: fl |= RCS_KWEXP_ERR;
1438: fl |= RCS_KWEXP_OLD;
1439: } else if (*fp == 'b') {
1440: if (len != 1)
1441: fl |= RCS_KWEXP_ERR;
1.205 xsa 1442: fl |= RCS_KWEXP_NONE;
1.20 jfb 1443: } else /* unknown letter */
1444: fl |= RCS_KWEXP_ERR;
1445: }
1446:
1447: return (fl);
1.32 jfb 1448: }
1449:
1450: /*
1451: * rcs_errstr()
1452: *
1453: * Get the error string matching the RCS error code <code>.
1454: */
1.57 xsa 1455: const char *
1.32 jfb 1456: rcs_errstr(int code)
1457: {
1.50 jfb 1458: const char *esp;
1459:
1.168 deraadt 1460: if (code < 0 || (code >= (int)RCS_NERR && code != RCS_ERR_ERRNO))
1.50 jfb 1461: esp = NULL;
1462: else if (code == RCS_ERR_ERRNO)
1463: esp = strerror(errno);
1464: else
1465: esp = rcs_errstrs[code];
1466: return (esp);
1.1 jfb 1467: }
1468:
1.117 niallo 1469: /* rcs_parse_deltas()
1470: *
1471: * Parse deltas. If <rev> is not NULL, parse only as far as that
1472: * revision. If <rev> is NULL, parse all deltas.
1473: */
1.146 xsa 1474: static void
1.117 niallo 1475: rcs_parse_deltas(RCSFILE *rfp, RCSNUM *rev)
1476: {
1477: int ret;
1478: struct rcs_delta *enddelta;
1.146 xsa 1479:
1480: if ((rfp->rf_flags & PARSED_DELTAS) || (rfp->rf_flags & RCS_CREATE))
1481: return;
1482:
1.117 niallo 1483: for (;;) {
1484: ret = rcs_parse_delta(rfp);
1485: if (rev != NULL) {
1486: enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1.155 ray 1487: if (rcsnum_cmp(enddelta->rd_num, rev, 0) == 0)
1.117 niallo 1488: break;
1489: }
1.180 joris 1490:
1.117 niallo 1491: if (ret == 0) {
1492: rfp->rf_flags |= PARSED_DELTAS;
1493: break;
1494: }
1495: else if (ret == -1)
1496: fatal("error parsing deltas");
1497: }
1498: }
1499:
1500: /* rcs_parse_deltatexts()
1501: *
1502: * Parse deltatexts. If <rev> is not NULL, parse only as far as that
1503: * revision. If <rev> is NULL, parse everything.
1504: */
1.146 xsa 1505: static void
1.117 niallo 1506: rcs_parse_deltatexts(RCSFILE *rfp, RCSNUM *rev)
1507: {
1508: int ret;
1509: struct rcs_delta *rdp;
1.146 xsa 1510:
1.168 deraadt 1511: if ((rfp->rf_flags & PARSED_DELTATEXTS) ||
1512: (rfp->rf_flags & RCS_CREATE))
1.146 xsa 1513: return;
1514:
1.117 niallo 1515: if (!(rfp->rf_flags & PARSED_DESC))
1516: rcs_parse_desc(rfp, rev);
1517: for (;;) {
1518: if (rev != NULL) {
1519: rdp = rcs_findrev(rfp, rev);
1520: if (rdp->rd_text != NULL)
1521: break;
1522: else
1523: ret = rcs_parse_deltatext(rfp);
1524: } else
1525: ret = rcs_parse_deltatext(rfp);
1526: if (ret == 0) {
1527: rfp->rf_flags |= PARSED_DELTATEXTS;
1528: break;
1529: }
1.146 xsa 1530: else if (ret == -1)
1.117 niallo 1531: fatal("problem parsing deltatexts");
1532: }
1533: }
1534:
1535: /* rcs_parse_desc()
1536: *
1537: * Parse RCS description.
1538: */
1.148 xsa 1539: static void
1.117 niallo 1540: rcs_parse_desc(RCSFILE *rfp, RCSNUM *rev)
1541: {
1542: int ret = 0;
1.148 xsa 1543:
1544: if ((rfp->rf_flags & PARSED_DESC) || (rfp->rf_flags & RCS_CREATE))
1545: return;
1.122 reyk 1546: if (!(rfp->rf_flags & PARSED_DELTAS))
1.117 niallo 1547: rcs_parse_deltas(rfp, rev);
1548: /* do parsing */
1549: ret = rcs_gettok(rfp);
1.148 xsa 1550: if (ret != RCS_TOK_DESC)
1551: fatal("token `%s' found where RCS desc expected",
1.117 niallo 1552: RCS_TOKSTR(rfp));
1553:
1554: ret = rcs_gettok(rfp);
1.148 xsa 1555: if (ret != RCS_TOK_STRING)
1556: fatal("token `%s' found where RCS desc expected",
1.117 niallo 1557: RCS_TOKSTR(rfp));
1558:
1559: rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
1560: rfp->rf_flags |= PARSED_DESC;
1561: }
1562:
1.1 jfb 1563: /*
1.117 niallo 1564: * rcs_parse_init()
1.1 jfb 1565: *
1.117 niallo 1566: * Initial parsing of file <path>, which are in the RCS format.
1.167 ray 1567: * Just does admin section.
1.1 jfb 1568: */
1.167 ray 1569: static void
1.117 niallo 1570: rcs_parse_init(RCSFILE *rfp)
1.1 jfb 1571: {
1572: struct rcs_pdata *pdp;
1573:
1.26 jfb 1574: if (rfp->rf_flags & RCS_PARSED)
1.167 ray 1575: return;
1.1 jfb 1576:
1.161 ray 1577: pdp = xcalloc(1, sizeof(*pdp));
1.1 jfb 1578:
1.18 jfb 1579: pdp->rp_lines = 0;
1.1 jfb 1580: pdp->rp_pttype = RCS_TOK_ERR;
1581:
1.172 joris 1582: if ((pdp->rp_file = fdopen(rfp->fd, "r")) == NULL)
1.216 xsa 1583: fatal("fdopen: `%s'", rfp->rf_path);
1.1 jfb 1584:
1.161 ray 1585: pdp->rp_buf = xmalloc((size_t)RCS_BUFSIZE);
1.1 jfb 1586: pdp->rp_blen = RCS_BUFSIZE;
1.18 jfb 1587: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1 jfb 1588:
1589: /* ditch the strict lock */
1.26 jfb 1590: rfp->rf_flags &= ~RCS_SLOCK;
1.1 jfb 1591: rfp->rf_pdata = pdp;
1592:
1.167 ray 1593: if (rcs_parse_admin(rfp) < 0) {
1.1 jfb 1594: rcs_freepdata(pdp);
1.117 niallo 1595: fatal("could not parse admin data");
1.1 jfb 1596: }
1597:
1.117 niallo 1598: if (rfp->rf_flags & RCS_PARSE_FULLY)
1599: rcs_parse_deltatexts(rfp, NULL);
1.1 jfb 1600:
1.117 niallo 1601: rfp->rf_flags |= RCS_SYNCED;
1.1 jfb 1602: }
1603:
1604: /*
1605: * rcs_parse_admin()
1606: *
1607: * Parse the administrative portion of an RCS file.
1.31 jfb 1608: * Returns the type of the first token found after the admin section on
1609: * success, or -1 on failure.
1.1 jfb 1610: */
1611: static int
1612: rcs_parse_admin(RCSFILE *rfp)
1613: {
1614: u_int i;
1615: int tok, ntok, hmask;
1616: struct rcs_key *rk;
1617:
1.180 joris 1618: rfp->rf_head = NULL;
1619: rfp->rf_branch = NULL;
1620:
1.1 jfb 1621: /* hmask is a mask of the headers already encountered */
1622: hmask = 0;
1623: for (;;) {
1624: tok = rcs_gettok(rfp);
1625: if (tok == RCS_TOK_ERR) {
1.50 jfb 1626: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1627: cvs_log(LP_ERR, "parse error in RCS admin section");
1.147 ray 1628: goto fail;
1.168 deraadt 1629: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.31 jfb 1630: /*
1631: * Assume this is the start of the first delta or
1632: * that we are dealing with an empty RCS file and
1633: * we just found the description.
1634: */
1.1 jfb 1635: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31 jfb 1636: return (tok);
1.1 jfb 1637: }
1638:
1639: rk = NULL;
1.18 jfb 1640: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1641: if (rcs_keys[i].rk_id == tok)
1642: rk = &(rcs_keys[i]);
1643:
1644: if (hmask & (1 << tok)) {
1.50 jfb 1645: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1646: cvs_log(LP_ERR, "duplicate RCS key");
1.147 ray 1647: goto fail;
1.1 jfb 1648: }
1649: hmask |= (1 << tok);
1650:
1651: switch (tok) {
1652: case RCS_TOK_HEAD:
1653: case RCS_TOK_BRANCH:
1654: case RCS_TOK_COMMENT:
1655: case RCS_TOK_EXPAND:
1656: ntok = rcs_gettok(rfp);
1657: if (ntok == RCS_TOK_SCOLON)
1658: break;
1659: if (ntok != rk->rk_val) {
1.50 jfb 1660: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1661: cvs_log(LP_ERR,
1662: "invalid value type for RCS key `%s'",
1663: rk->rk_str);
1664: }
1665:
1666: if (tok == RCS_TOK_HEAD) {
1.111 joris 1667: if (rfp->rf_head == NULL)
1.28 jfb 1668: rfp->rf_head = rcsnum_alloc();
1.1 jfb 1669: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1670: rfp->rf_head);
1.14 deraadt 1671: } else if (tok == RCS_TOK_BRANCH) {
1.111 joris 1672: if (rfp->rf_branch == NULL)
1.35 jfb 1673: rfp->rf_branch = rcsnum_alloc();
1674: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1675: rfp->rf_branch) < 0)
1.147 ray 1676: goto fail;
1.14 deraadt 1677: } else if (tok == RCS_TOK_COMMENT) {
1.110 joris 1678: rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
1.14 deraadt 1679: } else if (tok == RCS_TOK_EXPAND) {
1.110 joris 1680: rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 1681: }
1682:
1683: /* now get the expected semi-colon */
1684: ntok = rcs_gettok(rfp);
1685: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1686: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1687: cvs_log(LP_ERR,
1688: "missing semi-colon after RCS `%s' key",
1.26 jfb 1689: rk->rk_str);
1.147 ray 1690: goto fail;
1.1 jfb 1691: }
1692: break;
1693: case RCS_TOK_ACCESS:
1.29 jfb 1694: if (rcs_parse_access(rfp) < 0)
1.147 ray 1695: goto fail;
1.1 jfb 1696: break;
1697: case RCS_TOK_SYMBOLS:
1.29 jfb 1698: if (rcs_parse_symbols(rfp) < 0)
1.147 ray 1699: goto fail;
1.1 jfb 1700: break;
1701: case RCS_TOK_LOCKS:
1.29 jfb 1702: if (rcs_parse_locks(rfp) < 0)
1.147 ray 1703: goto fail;
1.1 jfb 1704: break;
1705: default:
1.50 jfb 1706: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1707: cvs_log(LP_ERR,
1708: "unexpected token `%s' in RCS admin section",
1709: RCS_TOKSTR(rfp));
1.147 ray 1710: goto fail;
1.1 jfb 1711: }
1712: }
1713:
1.147 ray 1714: fail:
1715: return (-1);
1.1 jfb 1716: }
1717:
1718: /*
1719: * rcs_parse_delta()
1720: *
1721: * Parse an RCS delta section and allocate the structure to store that delta's
1722: * information in the <rfp> delta list.
1723: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1724: * -1 on error.
1725: */
1726: static int
1727: rcs_parse_delta(RCSFILE *rfp)
1728: {
1729: int ret, tok, ntok, hmask;
1730: u_int i;
1731: char *tokstr;
1.3 vincent 1732: RCSNUM *datenum;
1.1 jfb 1733: struct rcs_delta *rdp;
1734: struct rcs_key *rk;
1735:
1736: tok = rcs_gettok(rfp);
1.154 ray 1737: if (tok == RCS_TOK_DESC) {
1738: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1739: return (0);
1740: } else if (tok != RCS_TOK_NUM) {
1.52 jfb 1741: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1742: cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
1743: RCS_TOKSTR(rfp));
1744: return (-1);
1745: }
1.192 niallo 1746:
1747: rdp = xcalloc(1, sizeof(*rdp));
1748:
1749: rdp->rd_num = rcsnum_alloc();
1750: rdp->rd_next = rcsnum_alloc();
1751:
1752: TAILQ_INIT(&(rdp->rd_branches));
1753:
1.1 jfb 1754: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1755:
1756: hmask = 0;
1757: ret = 0;
1758: tokstr = NULL;
1759:
1760: for (;;) {
1761: tok = rcs_gettok(rfp);
1762: if (tok == RCS_TOK_ERR) {
1.50 jfb 1763: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1764: cvs_log(LP_ERR, "parse error in RCS delta section");
1765: rcs_freedelta(rdp);
1766: return (-1);
1.14 deraadt 1767: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15 tedu 1768: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1 jfb 1769: ret = (tok == RCS_TOK_NUM ? 1 : 0);
1770: break;
1771: }
1772:
1773: rk = NULL;
1.18 jfb 1774: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1775: if (rcs_keys[i].rk_id == tok)
1776: rk = &(rcs_keys[i]);
1777:
1778: if (hmask & (1 << tok)) {
1.50 jfb 1779: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1780: cvs_log(LP_ERR, "duplicate RCS key");
1781: rcs_freedelta(rdp);
1782: return (-1);
1783: }
1784: hmask |= (1 << tok);
1785:
1786: switch (tok) {
1787: case RCS_TOK_DATE:
1788: case RCS_TOK_AUTHOR:
1789: case RCS_TOK_STATE:
1790: case RCS_TOK_NEXT:
1791: ntok = rcs_gettok(rfp);
1792: if (ntok == RCS_TOK_SCOLON) {
1793: if (rk->rk_flags & RCS_VOPT)
1794: break;
1795: else {
1.50 jfb 1796: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1797: cvs_log(LP_ERR, "missing mandatory "
1798: "value to RCS key `%s'",
1799: rk->rk_str);
1800: rcs_freedelta(rdp);
1801: return (-1);
1802: }
1803: }
1804:
1805: if (ntok != rk->rk_val) {
1.50 jfb 1806: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1807: cvs_log(LP_ERR,
1808: "invalid value type for RCS key `%s'",
1809: rk->rk_str);
1810: rcs_freedelta(rdp);
1811: return (-1);
1812: }
1813:
1814: if (tokstr != NULL)
1.110 joris 1815: xfree(tokstr);
1816: tokstr = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 1817: /* now get the expected semi-colon */
1818: ntok = rcs_gettok(rfp);
1819: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1820: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1821: cvs_log(LP_ERR,
1822: "missing semi-colon after RCS `%s' key",
1.26 jfb 1823: rk->rk_str);
1.110 joris 1824: xfree(tokstr);
1.1 jfb 1825: rcs_freedelta(rdp);
1826: return (-1);
1827: }
1828:
1829: if (tok == RCS_TOK_DATE) {
1.25 jfb 1830: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.110 joris 1831: xfree(tokstr);
1.11 joris 1832: rcs_freedelta(rdp);
1833: return (-1);
1834: }
1.3 vincent 1835: if (datenum->rn_len != 6) {
1.50 jfb 1836: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1837: cvs_log(LP_ERR,
1838: "RCS date specification has %s "
1839: "fields",
1.3 vincent 1840: (datenum->rn_len > 6) ? "too many" :
1.1 jfb 1841: "missing");
1.110 joris 1842: xfree(tokstr);
1.1 jfb 1843: rcs_freedelta(rdp);
1.37 tedu 1844: rcsnum_free(datenum);
1845: return (-1);
1.1 jfb 1846: }
1.3 vincent 1847: rdp->rd_date.tm_year = datenum->rn_id[0];
1.19 jfb 1848: if (rdp->rd_date.tm_year >= 1900)
1849: rdp->rd_date.tm_year -= 1900;
1.3 vincent 1850: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
1851: rdp->rd_date.tm_mday = datenum->rn_id[2];
1852: rdp->rd_date.tm_hour = datenum->rn_id[3];
1853: rdp->rd_date.tm_min = datenum->rn_id[4];
1854: rdp->rd_date.tm_sec = datenum->rn_id[5];
1855: rcsnum_free(datenum);
1.14 deraadt 1856: } else if (tok == RCS_TOK_AUTHOR) {
1.1 jfb 1857: rdp->rd_author = tokstr;
1858: tokstr = NULL;
1.14 deraadt 1859: } else if (tok == RCS_TOK_STATE) {
1.1 jfb 1860: rdp->rd_state = tokstr;
1861: tokstr = NULL;
1.14 deraadt 1862: } else if (tok == RCS_TOK_NEXT) {
1.1 jfb 1863: rcsnum_aton(tokstr, NULL, rdp->rd_next);
1864: }
1865: break;
1866: case RCS_TOK_BRANCHES:
1.46 jfb 1867: if (rcs_parse_branches(rfp, rdp) < 0) {
1868: rcs_freedelta(rdp);
1869: return (-1);
1870: }
1.1 jfb 1871: break;
1872: default:
1.50 jfb 1873: rcs_errno = RCS_ERR_PARSE;
1.172 joris 1874: cvs_log(LP_ERR, "unexpected token `%s' in RCS delta",
1.1 jfb 1875: RCS_TOKSTR(rfp));
1876: rcs_freedelta(rdp);
1877: return (-1);
1878: }
1879: }
1880:
1.13 jfb 1881: if (tokstr != NULL)
1.110 joris 1882: xfree(tokstr);
1.13 jfb 1883:
1.1 jfb 1884: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26 jfb 1885: rfp->rf_ndelta++;
1.1 jfb 1886:
1887: return (ret);
1888: }
1889:
1890: /*
1891: * rcs_parse_deltatext()
1892: *
1893: * Parse an RCS delta text section and fill in the log and text field of the
1894: * appropriate delta section.
1895: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1896: * -1 on error.
1897: */
1898: static int
1899: rcs_parse_deltatext(RCSFILE *rfp)
1900: {
1901: int tok;
1902: RCSNUM *tnum;
1903: struct rcs_delta *rdp;
1904:
1905: tok = rcs_gettok(rfp);
1906: if (tok == RCS_TOK_EOF)
1907: return (0);
1908:
1909: if (tok != RCS_TOK_NUM) {
1.50 jfb 1910: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1911: cvs_log(LP_ERR,
1912: "unexpected token `%s' at start of RCS delta text",
1913: RCS_TOKSTR(rfp));
1914: return (-1);
1915: }
1.13 jfb 1916:
1917: tnum = rcsnum_alloc();
1.1 jfb 1918: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
1919:
1920: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1921: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
1922: break;
1923: }
1.13 jfb 1924: rcsnum_free(tnum);
1925:
1.1 jfb 1926: if (rdp == NULL) {
1927: cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
1928: RCS_TOKSTR(rfp));
1929: return (-1);
1930: }
1931:
1932: tok = rcs_gettok(rfp);
1933: if (tok != RCS_TOK_LOG) {
1.50 jfb 1934: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1935: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1936: RCS_TOKSTR(rfp));
1937: return (-1);
1938: }
1939:
1940: tok = rcs_gettok(rfp);
1941: if (tok != RCS_TOK_STRING) {
1.50 jfb 1942: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1943: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1944: RCS_TOKSTR(rfp));
1945: return (-1);
1946: }
1.110 joris 1947: rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 1948: tok = rcs_gettok(rfp);
1949: if (tok != RCS_TOK_TEXT) {
1.50 jfb 1950: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1951: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
1952: RCS_TOKSTR(rfp));
1953: return (-1);
1954: }
1955:
1956: tok = rcs_gettok(rfp);
1957: if (tok != RCS_TOK_STRING) {
1.50 jfb 1958: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1959: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
1960: RCS_TOKSTR(rfp));
1961: return (-1);
1962: }
1963:
1.193 niallo 1964: if (RCS_TOKLEN(rfp) == 0) {
1965: rdp->rd_text = xmalloc(1);
1966: rdp->rd_text[0] = '\0';
1967: rdp->rd_tlen = 0;
1968: } else {
1969: rdp->rd_text = xmalloc(RCS_TOKLEN(rfp));
1970: memcpy(rdp->rd_text, RCS_TOKSTR(rfp), RCS_TOKLEN(rfp));
1971: rdp->rd_tlen = RCS_TOKLEN(rfp);
1972: }
1.1 jfb 1973:
1974: return (1);
1975: }
1976:
1977: /*
1978: * rcs_parse_access()
1979: *
1980: * Parse the access list given as value to the `access' keyword.
1981: * Returns 0 on success, or -1 on failure.
1982: */
1983: static int
1984: rcs_parse_access(RCSFILE *rfp)
1985: {
1986: int type;
1987:
1988: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
1989: if (type != RCS_TOK_ID) {
1.50 jfb 1990: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1991: cvs_log(LP_ERR, "unexpected token `%s' in access list",
1992: RCS_TOKSTR(rfp));
1993: return (-1);
1994: }
1.29 jfb 1995:
1996: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
1997: return (-1);
1.1 jfb 1998: }
1999:
2000: return (0);
2001: }
2002:
2003: /*
2004: * rcs_parse_symbols()
2005: *
2006: * Parse the symbol list given as value to the `symbols' keyword.
2007: * Returns 0 on success, or -1 on failure.
2008: */
2009: static int
2010: rcs_parse_symbols(RCSFILE *rfp)
2011: {
2012: int type;
2013: struct rcs_sym *symp;
2014:
2015: for (;;) {
2016: type = rcs_gettok(rfp);
2017: if (type == RCS_TOK_SCOLON)
2018: break;
2019:
1.41 jfb 2020: if (type != RCS_TOK_ID) {
1.50 jfb 2021: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2022: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2023: RCS_TOKSTR(rfp));
2024: return (-1);
2025: }
2026:
1.161 ray 2027: symp = xmalloc(sizeof(*symp));
1.110 joris 2028: symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 2029: symp->rs_num = rcsnum_alloc();
2030:
2031: type = rcs_gettok(rfp);
2032: if (type != RCS_TOK_COLON) {
1.50 jfb 2033: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2034: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2035: RCS_TOKSTR(rfp));
1.11 joris 2036: rcsnum_free(symp->rs_num);
1.110 joris 2037: xfree(symp->rs_name);
2038: xfree(symp);
1.1 jfb 2039: return (-1);
2040: }
2041:
2042: type = rcs_gettok(rfp);
2043: if (type != RCS_TOK_NUM) {
1.50 jfb 2044: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2045: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2046: RCS_TOKSTR(rfp));
1.11 joris 2047: rcsnum_free(symp->rs_num);
1.110 joris 2048: xfree(symp->rs_name);
2049: xfree(symp);
1.1 jfb 2050: return (-1);
2051: }
2052:
2053: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
2054: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2055: RCS_TOKSTR(rfp));
1.11 joris 2056: rcsnum_free(symp->rs_num);
1.110 joris 2057: xfree(symp->rs_name);
2058: xfree(symp);
1.1 jfb 2059: return (-1);
2060: }
2061:
1.43 jfb 2062: TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1 jfb 2063: }
2064:
2065: return (0);
2066: }
2067:
2068: /*
2069: * rcs_parse_locks()
2070: *
2071: * Parse the lock list given as value to the `locks' keyword.
2072: * Returns 0 on success, or -1 on failure.
2073: */
2074: static int
2075: rcs_parse_locks(RCSFILE *rfp)
2076: {
2077: int type;
2078: struct rcs_lock *lkp;
2079:
2080: for (;;) {
2081: type = rcs_gettok(rfp);
2082: if (type == RCS_TOK_SCOLON)
2083: break;
2084:
2085: if (type != RCS_TOK_ID) {
1.50 jfb 2086: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2087: cvs_log(LP_ERR, "unexpected token `%s' in lock list",
2088: RCS_TOKSTR(rfp));
2089: return (-1);
2090: }
2091:
1.161 ray 2092: lkp = xmalloc(sizeof(*lkp));
1.110 joris 2093: lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 2094: lkp->rl_num = rcsnum_alloc();
2095:
2096: type = rcs_gettok(rfp);
2097: if (type != RCS_TOK_COLON) {
1.50 jfb 2098: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2099: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2100: RCS_TOKSTR(rfp));
1.37 tedu 2101: rcsnum_free(lkp->rl_num);
1.110 joris 2102: xfree(lkp->rl_name);
2103: xfree(lkp);
1.1 jfb 2104: return (-1);
2105: }
2106:
2107: type = rcs_gettok(rfp);
2108: if (type != RCS_TOK_NUM) {
1.50 jfb 2109: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2110: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2111: RCS_TOKSTR(rfp));
1.37 tedu 2112: rcsnum_free(lkp->rl_num);
1.110 joris 2113: xfree(lkp->rl_name);
2114: xfree(lkp);
1.1 jfb 2115: return (-1);
2116: }
2117:
2118: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
2119: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2120: RCS_TOKSTR(rfp));
1.37 tedu 2121: rcsnum_free(lkp->rl_num);
1.110 joris 2122: xfree(lkp->rl_name);
2123: xfree(lkp);
1.1 jfb 2124: return (-1);
2125: }
2126:
2127: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
2128: }
2129:
2130: /* check if we have a `strict' */
2131: type = rcs_gettok(rfp);
2132: if (type != RCS_TOK_STRICT) {
2133: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14 deraadt 2134: } else {
1.26 jfb 2135: rfp->rf_flags |= RCS_SLOCK;
1.1 jfb 2136:
2137: type = rcs_gettok(rfp);
2138: if (type != RCS_TOK_SCOLON) {
1.50 jfb 2139: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2140: cvs_log(LP_ERR,
2141: "missing semi-colon after `strict' keyword");
2142: return (-1);
2143: }
2144: }
2145:
2146: return (0);
2147: }
2148:
2149: /*
2150: * rcs_parse_branches()
2151: *
2152: * Parse the list of branches following a `branches' keyword in a delta.
2153: * Returns 0 on success, or -1 on failure.
2154: */
2155: static int
2156: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
2157: {
2158: int type;
2159: struct rcs_branch *brp;
2160:
2161: for (;;) {
2162: type = rcs_gettok(rfp);
2163: if (type == RCS_TOK_SCOLON)
2164: break;
2165:
2166: if (type != RCS_TOK_NUM) {
1.50 jfb 2167: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2168: cvs_log(LP_ERR,
2169: "unexpected token `%s' in list of branches",
2170: RCS_TOKSTR(rfp));
2171: return (-1);
2172: }
2173:
1.161 ray 2174: brp = xmalloc(sizeof(*brp));
1.46 jfb 2175: brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11 joris 2176: if (brp->rb_num == NULL) {
1.110 joris 2177: xfree(brp);
1.11 joris 2178: return (-1);
2179: }
1.1 jfb 2180:
2181: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
2182: }
2183:
2184: return (0);
2185: }
2186:
2187: /*
2188: * rcs_freedelta()
2189: *
2190: * Free the contents of a delta structure.
2191: */
1.18 jfb 2192: static void
1.1 jfb 2193: rcs_freedelta(struct rcs_delta *rdp)
2194: {
1.12 jfb 2195: struct rcs_branch *rb;
1.1 jfb 2196:
1.12 jfb 2197: if (rdp->rd_num != NULL)
2198: rcsnum_free(rdp->rd_num);
2199: if (rdp->rd_next != NULL)
2200: rcsnum_free(rdp->rd_next);
2201:
1.1 jfb 2202: if (rdp->rd_author != NULL)
1.110 joris 2203: xfree(rdp->rd_author);
1.109 joris 2204: if (rdp->rd_locker != NULL)
1.110 joris 2205: xfree(rdp->rd_locker);
1.1 jfb 2206: if (rdp->rd_state != NULL)
1.110 joris 2207: xfree(rdp->rd_state);
1.1 jfb 2208: if (rdp->rd_log != NULL)
1.110 joris 2209: xfree(rdp->rd_log);
1.1 jfb 2210: if (rdp->rd_text != NULL)
1.110 joris 2211: xfree(rdp->rd_text);
1.12 jfb 2212:
2213: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
2214: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
2215: rcsnum_free(rb->rb_num);
1.110 joris 2216: xfree(rb);
1.1 jfb 2217: }
2218:
1.110 joris 2219: xfree(rdp);
1.1 jfb 2220: }
2221:
2222: /*
2223: * rcs_freepdata()
2224: *
2225: * Free the contents of the parser data structure.
2226: */
2227: static void
2228: rcs_freepdata(struct rcs_pdata *pd)
2229: {
2230: if (pd->rp_file != NULL)
2231: (void)fclose(pd->rp_file);
2232: if (pd->rp_buf != NULL)
1.110 joris 2233: xfree(pd->rp_buf);
2234: xfree(pd);
1.1 jfb 2235: }
2236:
2237: /*
2238: * rcs_gettok()
2239: *
2240: * Get the next RCS token from the string <str>.
2241: */
2242: static int
2243: rcs_gettok(RCSFILE *rfp)
2244: {
2245: u_int i;
2246: int ch, last, type;
1.18 jfb 2247: size_t len;
2248: char *bp;
1.1 jfb 2249: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2250:
2251: type = RCS_TOK_ERR;
2252: bp = pdp->rp_buf;
1.42 jfb 2253: pdp->rp_tlen = 0;
1.1 jfb 2254: *bp = '\0';
2255:
2256: if (pdp->rp_pttype != RCS_TOK_ERR) {
2257: type = pdp->rp_pttype;
1.172 joris 2258: if (strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen) >=
2259: pdp->rp_blen)
2260: fatal("rcs_gettok: strlcpy");
1.1 jfb 2261: pdp->rp_pttype = RCS_TOK_ERR;
2262: return (type);
2263: }
2264:
2265: /* skip leading whitespace */
2266: /* XXX we must skip backspace too for compatibility, should we? */
2267: do {
2268: ch = getc(pdp->rp_file);
2269: if (ch == '\n')
1.18 jfb 2270: pdp->rp_lines++;
1.1 jfb 2271: } while (isspace(ch));
2272:
2273: if (ch == EOF) {
2274: type = RCS_TOK_EOF;
1.14 deraadt 2275: } else if (ch == ';') {
1.1 jfb 2276: type = RCS_TOK_SCOLON;
1.14 deraadt 2277: } else if (ch == ':') {
1.1 jfb 2278: type = RCS_TOK_COLON;
1.14 deraadt 2279: } else if (isalpha(ch)) {
1.31 jfb 2280: type = RCS_TOK_ID;
1.1 jfb 2281: *(bp++) = ch;
1.18 jfb 2282: for (;;) {
1.1 jfb 2283: ch = getc(pdp->rp_file);
1.188 joris 2284: if (ch == EOF) {
2285: type = RCS_TOK_EOF;
2286: break;
2287: } else if (!isalnum(ch) && ch != '_' && ch != '-' &&
1.201 joris 2288: ch != '/' && ch != '+') {
1.1 jfb 2289: ungetc(ch, pdp->rp_file);
2290: break;
2291: }
2292: *(bp++) = ch;
1.42 jfb 2293: pdp->rp_tlen++;
1.18 jfb 2294: if (bp == pdp->rp_bufend - 1) {
2295: len = bp - pdp->rp_buf;
1.151 xsa 2296: rcs_growbuf(rfp);
1.18 jfb 2297: bp = pdp->rp_buf + len;
2298: }
1.1 jfb 2299: }
2300: *bp = '\0';
2301:
1.18 jfb 2302: if (type != RCS_TOK_ERR) {
2303: for (i = 0; i < RCS_NKEYS; i++) {
2304: if (strcmp(rcs_keys[i].rk_str,
2305: pdp->rp_buf) == 0) {
2306: type = rcs_keys[i].rk_id;
2307: break;
2308: }
1.1 jfb 2309: }
2310: }
1.14 deraadt 2311: } else if (ch == '@') {
1.1 jfb 2312: /* we have a string */
1.18 jfb 2313: type = RCS_TOK_STRING;
1.1 jfb 2314: for (;;) {
2315: ch = getc(pdp->rp_file);
1.188 joris 2316: if (ch == EOF) {
2317: type = RCS_TOK_EOF;
2318: break;
2319: } else if (ch == '@') {
1.1 jfb 2320: ch = getc(pdp->rp_file);
2321: if (ch != '@') {
2322: ungetc(ch, pdp->rp_file);
2323: break;
2324: }
1.14 deraadt 2325: } else if (ch == '\n')
1.18 jfb 2326: pdp->rp_lines++;
1.1 jfb 2327:
2328: *(bp++) = ch;
1.42 jfb 2329: pdp->rp_tlen++;
1.18 jfb 2330: if (bp == pdp->rp_bufend - 1) {
2331: len = bp - pdp->rp_buf;
1.151 xsa 2332: rcs_growbuf(rfp);
1.18 jfb 2333: bp = pdp->rp_buf + len;
2334: }
1.1 jfb 2335: }
2336:
2337: *bp = '\0';
1.14 deraadt 2338: } else if (isdigit(ch)) {
1.1 jfb 2339: *(bp++) = ch;
2340: last = ch;
2341: type = RCS_TOK_NUM;
2342:
2343: for (;;) {
2344: ch = getc(pdp->rp_file);
1.188 joris 2345: if (ch == EOF) {
2346: type = RCS_TOK_EOF;
2347: break;
2348: }
1.18 jfb 2349: if (bp == pdp->rp_bufend)
1.1 jfb 2350: break;
2351: if (!isdigit(ch) && ch != '.') {
2352: ungetc(ch, pdp->rp_file);
2353: break;
2354: }
2355:
2356: if (last == '.' && ch == '.') {
2357: type = RCS_TOK_ERR;
2358: break;
2359: }
2360: last = ch;
2361: *(bp++) = ch;
1.42 jfb 2362: pdp->rp_tlen++;
1.1 jfb 2363: }
1.18 jfb 2364: *bp = '\0';
1.1 jfb 2365: }
2366:
2367: return (type);
2368: }
2369:
2370: /*
2371: * rcs_pushtok()
2372: *
2373: * Push a token back in the parser's token buffer.
2374: */
2375: static int
2376: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2377: {
2378: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2379:
2380: if (pdp->rp_pttype != RCS_TOK_ERR)
2381: return (-1);
2382:
2383: pdp->rp_pttype = type;
1.172 joris 2384: if (strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok)) >=
2385: sizeof(pdp->rp_ptok))
2386: fatal("rcs_pushtok: strlcpy");
1.1 jfb 2387: return (0);
2388: }
2389:
1.18 jfb 2390:
2391: /*
2392: * rcs_growbuf()
2393: *
2394: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2395: * RCS_BUFEXTSIZE.
2396: * In case of failure, the original buffer is left unmodified.
2397: */
1.151 xsa 2398: static void
1.18 jfb 2399: rcs_growbuf(RCSFILE *rf)
2400: {
2401: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2402:
1.212 ray 2403: pdp->rp_buf = xrealloc(pdp->rp_buf, 1,
2404: pdp->rp_blen + RCS_BUFEXTSIZE);
1.18 jfb 2405: pdp->rp_blen += RCS_BUFEXTSIZE;
2406: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42 jfb 2407: }
2408:
2409: /*
2410: * rcs_strprint()
2411: *
2412: * Output an RCS string <str> of size <slen> to the stream <stream>. Any
2413: * '@' characters are escaped. Otherwise, the string can contain arbitrary
2414: * binary data.
2415: */
1.155 ray 2416: static void
1.42 jfb 2417: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
2418: {
2419: const u_char *ap, *ep, *sp;
1.52 jfb 2420:
2421: if (slen == 0)
1.155 ray 2422: return;
1.42 jfb 2423:
2424: ep = str + slen - 1;
2425:
2426: for (sp = str; sp <= ep;) {
2427: ap = memchr(sp, '@', ep - sp);
2428: if (ap == NULL)
2429: ap = ep;
1.155 ray 2430: (void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
1.42 jfb 2431:
2432: if (*ap == '@')
2433: putc('@', stream);
2434: sp = ap + 1;
1.63 joris 2435: }
2436: }
2437:
2438: /*
1.81 niallo 2439: * rcs_deltatext_set()
2440: *
2441: * Set deltatext for <rev> in RCS file <rfp> to <dtext>
1.96 xsa 2442: * Returns -1 on error, 0 on success.
1.81 niallo 2443: */
2444: int
1.194 joris 2445: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, BUF *bp)
1.81 niallo 2446: {
2447: size_t len;
1.194 joris 2448: u_char *dtext;
1.81 niallo 2449: struct rcs_delta *rdp;
1.117 niallo 2450:
2451: /* Write operations require full parsing */
2452: rcs_parse_deltatexts(rfp, NULL);
1.81 niallo 2453:
2454: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2455: return (-1);
2456:
2457: if (rdp->rd_text != NULL)
1.110 joris 2458: xfree(rdp->rd_text);
1.81 niallo 2459:
1.194 joris 2460: len = cvs_buf_len(bp);
2461: dtext = cvs_buf_release(bp);
2462: bp = NULL;
2463:
1.103 joris 2464: if (len != 0) {
1.194 joris 2465: rdp->rd_text = xmalloc(len);
1.155 ray 2466: rdp->rd_tlen = len;
1.194 joris 2467: (void)memcpy(rdp->rd_text, dtext, len);
1.103 joris 2468: } else {
2469: rdp->rd_text = NULL;
2470: rdp->rd_tlen = 0;
2471: }
1.194 joris 2472:
2473: if (dtext != NULL)
2474: xfree(dtext);
1.18 jfb 2475:
1.86 joris 2476: return (0);
2477: }
2478:
2479: /*
2480: * rcs_rev_setlog()
2481: *
2482: * Sets the log message of revision <rev> to <logtext>
2483: */
2484: int
2485: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
2486: {
2487: struct rcs_delta *rdp;
1.214 xsa 2488: char buf[CVS_REV_BUFSZ];
1.86 joris 2489:
2490: rcsnum_tostr(rev, buf, sizeof(buf));
2491:
2492: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2493: return (-1);
2494:
2495: if (rdp->rd_log != NULL)
1.110 joris 2496: xfree(rdp->rd_log);
1.86 joris 2497:
1.110 joris 2498: rdp->rd_log = xstrdup(logtext);
1.86 joris 2499: rfp->rf_flags &= ~RCS_SYNCED;
1.95 niallo 2500: return (0);
2501: }
1.97 niallo 2502: /*
2503: * rcs_rev_getdate()
2504: *
2505: * Get the date corresponding to a given revision.
2506: * Returns the date on success, -1 on failure.
2507: */
2508: time_t
2509: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
2510: {
2511: struct rcs_delta *rdp;
2512:
2513: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2514: return (-1);
2515:
1.204 joris 2516: return (timegm(&rdp->rd_date));
1.97 niallo 2517: }
1.95 niallo 2518:
2519: /*
2520: * rcs_state_set()
2521: *
2522: * Sets the state of revision <rev> to <state>
2523: * NOTE: default state is 'Exp'. States may not contain spaces.
2524: *
2525: * Returns -1 on failure, 0 on success.
2526: */
2527: int
2528: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
2529: {
2530: struct rcs_delta *rdp;
2531:
2532: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2533: return (-1);
2534:
2535: if (rdp->rd_state != NULL)
1.110 joris 2536: xfree(rdp->rd_state);
1.95 niallo 2537:
1.110 joris 2538: rdp->rd_state = xstrdup(state);
1.95 niallo 2539:
2540: rfp->rf_flags &= ~RCS_SYNCED;
2541:
2542: return (0);
2543: }
2544:
2545: /*
2546: * rcs_state_check()
2547: *
2548: * Check if string <state> is valid.
2549: *
1.96 xsa 2550: * Returns 0 if the string is valid, -1 otherwise.
1.95 niallo 2551: */
2552: int
2553: rcs_state_check(const char *state)
2554: {
2555: if (strchr(state, ' ') != NULL)
2556: return (-1);
2557:
1.18 jfb 2558: return (0);
1.1 jfb 2559: }
1.97 niallo 2560:
2561: /*
2562: * rcs_state_get()
2563: *
2564: * Get the state for a given revision of a specified RCSFILE.
2565: *
2566: * Returns NULL on failure.
2567: */
2568: const char *
2569: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
2570: {
2571: struct rcs_delta *rdp;
2572:
2573: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2574: return (NULL);
2575:
2576: return (rdp->rd_state);
2577: }
2578:
1.213 niallo 2579: /* rcs_translate_tag() */
1.176 joris 2580: RCSNUM *
2581: rcs_translate_tag(const char *revstr, RCSFILE *rfp)
2582: {
1.213 niallo 2583: RCSNUM *rev, *brev, *frev;
1.178 joris 2584: struct rcs_branch *brp;
1.183 joris 2585: struct rcs_delta *rdp;
1.213 niallo 2586: size_t i;
1.183 joris 2587:
2588: rdp = NULL;
1.215 joris 2589:
2590: if (!strcmp(revstr, RCS_HEAD_BRANCH)) {
2591: frev = rcsnum_alloc();
2592: rcsnum_cpy(rfp->rf_head, frev, 0);
2593: return (frev);
2594: }
1.176 joris 2595:
1.213 niallo 2596: /* Possibly we could be passed a version number */
1.223 tobias 2597: if ((rev = rcsnum_parse(revstr)) != NULL) {
2598: /* Do not return if it is not in RCS file */
2599: if ((rdp = rcs_findrev(rfp, rev)) != NULL)
2600: return (rev);
2601: } else {
2602: /* More likely we will be passed a symbol */
2603: rev = rcs_sym_getrev(rfp, revstr);
2604: }
1.222 joris 2605:
1.213 niallo 2606: if (rev == NULL)
2607: return (NULL);
1.183 joris 2608:
1.220 joris 2609: /*
2610: * If it was not a branch, thats ok the symbolic
2611: * name refered to a revision, so return the resolved
1.224 tobias 2612: * revision for the given name if it is not newer than HEAD.
1.220 joris 2613: */
1.224 tobias 2614: if (!RCSNUM_ISBRANCH(rev)) {
2615: if (rcsnum_cmp(rev, rfp->rf_head, 0) < 0) {
2616: rcsnum_free(rev);
2617: return NULL;
2618: }
1.220 joris 2619: return (rev);
1.224 tobias 2620: }
1.183 joris 2621:
1.213 niallo 2622: brev = rcsnum_alloc();
2623: rcsnum_cpy(rev, brev, rev->rn_len - 1);
1.178 joris 2624:
1.180 joris 2625: if ((rdp = rcs_findrev(rfp, brev)) == NULL)
1.213 niallo 2626: fatal("rcs_translate_tag: tag `%s' does not exist", revstr);
2627: rcsnum_free(brev);
1.178 joris 2628:
1.213 niallo 2629: TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
2630: for (i = 0; i < rev->rn_len; i++)
2631: if (brp->rb_num->rn_id[i] != rev->rn_id[i])
2632: break;
2633: if (i != rev->rn_len)
2634: continue;
2635: break;
2636: }
1.178 joris 2637:
1.213 niallo 2638: frev = rcsnum_alloc();
2639: if (brp == NULL) {
2640: rcsnum_cpy(rdp->rd_num, frev, 0);
2641: return (frev);
2642: } else {
2643: /* Fetch the delta with the correct branch num */
1.180 joris 2644: if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
1.213 niallo 2645: fatal("rcs_translate_tag: could not fetch branch delta");
2646: /* Find the latest delta on that branch */
2647: for (;;) {
2648: if (rdp->rd_next->rn_len == 0)
2649: break;
1.180 joris 2650: if ((rdp = rcs_findrev(rfp, rdp->rd_next)) == NULL)
1.213 niallo 2651: fatal("rcs_translate_tag: could not fetch branch delta");
1.178 joris 2652: }
1.213 niallo 2653: rcsnum_cpy(rdp->rd_num, frev, 0);
2654: return (frev);
1.176 joris 2655: }
1.196 niallo 2656: }
2657:
2658: /*
2659: * rcs_rev_getlines()
2660: *
1.238 tobias 2661: * Get the entire contents of revision <frev> from the RCSFILE <rfp> and
1.196 niallo 2662: * return it as a pointer to a struct cvs_lines.
2663: */
2664: struct cvs_lines *
1.218 tobias 2665: rcs_rev_getlines(RCSFILE *rfp, RCSNUM *frev, struct cvs_line ***alines)
1.196 niallo 2666: {
1.209 otto 2667: size_t plen;
1.218 tobias 2668: int annotate, done, i, nextroot;
1.196 niallo 2669: RCSNUM *tnum, *bnum;
2670: struct rcs_branch *brp;
1.218 tobias 2671: struct rcs_delta *hrdp, *prdp, *rdp, *trdp;
1.196 niallo 2672: u_char *patch;
1.218 tobias 2673: struct cvs_line *line, *nline;
1.196 niallo 2674: struct cvs_lines *dlines, *plines;
2675:
2676: if ((hrdp = rcs_findrev(rfp, rfp->rf_head)) == NULL)
1.197 joris 2677: fatal("rcs_rev_getlines: no HEAD revision");
1.196 niallo 2678:
2679: tnum = frev;
2680: rcs_parse_deltatexts(rfp, hrdp->rd_num);
2681:
2682: /* revision on branch, get the branch root */
2683: nextroot = 2;
2684: if (RCSNUM_ISBRANCHREV(tnum)) {
2685: bnum = rcsnum_alloc();
2686: rcsnum_cpy(tnum, bnum, nextroot);
2687: } else {
2688: bnum = tnum;
2689: }
2690:
1.218 tobias 2691: if (alines != NULL) {
2692: /* start with annotate first at requested revision */
2693: annotate = ANNOTATE_LATER;
2694: *alines = NULL;
2695: } else
2696: annotate = ANNOTATE_NEVER;
2697:
1.196 niallo 2698: dlines = cvs_splitlines(hrdp->rd_text, hrdp->rd_tlen);
2699:
2700: done = 0;
2701:
2702: rdp = hrdp;
1.218 tobias 2703: if (!rcsnum_differ(rdp->rd_num, bnum)) {
2704: if (annotate == ANNOTATE_LATER) {
2705: /* found requested revision for annotate */
2706: i = 0;
2707: TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
2708: line->l_lineno_orig = line->l_lineno;
2709: i++;
2710: }
2711:
2712: *alines = xcalloc(i + 1, sizeof(struct cvs_line *));
2713: (*alines)[i] = NULL;
2714: annotate = ANNOTATE_NOW;
2715:
2716: /* annotate down to 1.1 from where we are */
2717: if (bnum == tnum)
2718: bnum = rcsnum_alloc();
2719: bnum = rcsnum_parse("1.1");
2720: if (!rcsnum_differ(rdp->rd_num, bnum)) {
2721: goto next;
2722: }
2723: } else
2724: goto next;
2725: }
1.196 niallo 2726:
1.218 tobias 2727: prdp = hrdp;
1.196 niallo 2728: if ((rdp = rcs_findrev(rfp, hrdp->rd_next)) == NULL)
2729: goto done;
2730:
2731: again:
2732: for (;;) {
2733: if (rdp->rd_next->rn_len != 0) {
2734: trdp = rcs_findrev(rfp, rdp->rd_next);
2735: if (trdp == NULL)
2736: fatal("failed to grab next revision");
2737: }
2738:
2739: if (rdp->rd_tlen == 0) {
2740: rcs_parse_deltatexts(rfp, rdp->rd_num);
2741: if (rdp->rd_tlen == 0) {
2742: if (!rcsnum_differ(rdp->rd_num, bnum))
2743: break;
2744: rdp = trdp;
2745: continue;
2746: }
2747: }
2748:
2749: plen = rdp->rd_tlen;
2750: patch = rdp->rd_text;
2751: plines = cvs_splitlines(patch, plen);
1.218 tobias 2752: if (annotate == ANNOTATE_NOW)
2753: rcs_patch_lines(dlines, plines, *alines, prdp);
2754: else
2755: rcs_patch_lines(dlines, plines, NULL, NULL);
1.196 niallo 2756: cvs_freelines(plines);
2757:
1.218 tobias 2758: if (!rcsnum_differ(rdp->rd_num, bnum)) {
2759: if (annotate != ANNOTATE_LATER)
2760: break;
2761:
2762: /* found requested revision for annotate */
2763: i = 0;
2764: TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
2765: line->l_lineno_orig = line->l_lineno;
2766: i++;
2767: }
2768:
2769: *alines = xcalloc(i + 1, sizeof(struct cvs_line *));
2770: (*alines)[i] = NULL;
2771: annotate = ANNOTATE_NOW;
2772:
2773: /* annotate down to 1.1 from where we are */
2774: if (bnum == tnum)
2775: bnum = rcsnum_alloc();
2776: bnum = rcsnum_parse("1.1");
2777:
2778: if (!rcsnum_differ(rdp->rd_num, bnum))
2779: break;
2780: }
1.196 niallo 2781:
1.218 tobias 2782: prdp = rdp;
1.196 niallo 2783: rdp = trdp;
2784: }
2785:
2786: next:
2787: if (!rcsnum_differ(rdp->rd_num, frev))
2788: done = 1;
2789:
2790: if (RCSNUM_ISBRANCHREV(frev) && done != 1) {
2791: nextroot += 2;
2792: rcsnum_cpy(frev, bnum, nextroot);
2793:
2794: TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
1.213 niallo 2795: for (i = 0; i < nextroot - 1; i++)
2796: if (brp->rb_num->rn_id[i] != bnum->rn_id[i])
1.196 niallo 2797: break;
1.213 niallo 2798: if (i == nextroot - 1)
2799: break;
1.196 niallo 2800: }
2801:
1.218 tobias 2802: if (brp == NULL) {
2803: if (annotate != ANNOTATE_NEVER) {
2804: if (*alines != NULL)
2805: xfree(*alines);
2806: *alines = NULL;
2807: cvs_freelines(dlines);
2808: if (bnum != tnum)
2809: rcsnum_free(bnum);
2810: return (NULL);
2811: }
1.196 niallo 2812: fatal("expected branch not found on branch list");
1.218 tobias 2813: }
1.196 niallo 2814:
2815: if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
1.197 joris 2816: fatal("rcs_rev_getlines: failed to get delta for target rev");
1.196 niallo 2817:
2818: goto again;
2819: }
2820: done:
1.225 tobias 2821: /* put remaining lines into annotate buffer */
1.218 tobias 2822: if (annotate == ANNOTATE_NOW) {
2823: for (line = TAILQ_FIRST(&(dlines->l_lines));
2824: line != NULL; line = nline) {
2825: nline = TAILQ_NEXT(line, l_list);
2826: TAILQ_REMOVE(&(dlines->l_lines), line, l_list);
2827: if (line->l_line == NULL) {
2828: xfree(line);
2829: continue;
2830: }
2831:
2832: line->l_delta = rdp;
2833: (*alines)[line->l_lineno_orig - 1] = line;
2834: }
2835:
2836: cvs_freelines(dlines);
2837: dlines = NULL;
2838: }
2839:
1.196 niallo 2840: if (bnum != tnum)
2841: rcsnum_free(bnum);
2842:
2843: return (dlines);
1.225 tobias 2844: }
2845:
2846: void
2847: rcs_annotate_getlines(RCSFILE *rfp, RCSNUM *frev, struct cvs_line ***alines)
2848: {
2849: size_t plen;
2850: int i, nextroot;
2851: RCSNUM *bnum;
2852: struct rcs_branch *brp;
2853: struct rcs_delta *rdp, *trdp;
2854: u_char *patch;
2855: struct cvs_line *line;
2856: struct cvs_lines *dlines, *plines;
2857:
2858: if (!RCSNUM_ISBRANCHREV(frev))
2859: fatal("rcs_annotate_getlines: branch revision expected");
2860:
2861: /* revision on branch, get the branch root */
2862: nextroot = 2;
2863: bnum = rcsnum_alloc();
2864: rcsnum_cpy(frev, bnum, nextroot);
2865:
2866: /*
2867: * Going from HEAD to 1.1 enables the use of an array, which is
2868: * much faster. Unfortunately this is not possible with branch
2869: * revisions, so copy over our alines (array) into dlines (tailq).
2870: */
2871: dlines = xcalloc(1, sizeof(*dlines));
2872: TAILQ_INIT(&(dlines->l_lines));
2873: line = xcalloc(1, sizeof(*line));
2874: TAILQ_INSERT_TAIL(&(dlines->l_lines), line, l_list);
2875:
2876: for (i = 0; (*alines)[i] != NULL; i++) {
2877: line = (*alines)[i];
2878: line->l_lineno = i + 1;
2879: TAILQ_INSERT_TAIL(&(dlines->l_lines), line, l_list);
2880: }
2881:
2882: rdp = rcs_findrev(rfp, bnum);
2883: if (rdp == NULL)
2884: fatal("failed to grab branch root revision");
2885:
2886: do {
2887: nextroot += 2;
2888: rcsnum_cpy(frev, bnum, nextroot);
2889:
2890: TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
2891: for (i = 0; i < nextroot - 1; i++)
2892: if (brp->rb_num->rn_id[i] != bnum->rn_id[i])
2893: break;
2894: if (i == nextroot - 1)
2895: break;
2896: }
2897:
2898: if (brp == NULL)
2899: fatal("expected branch not found on branch list");
2900:
2901: if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
2902: fatal("failed to get delta for target rev");
2903:
2904: for (;;) {
2905: if (rdp->rd_next->rn_len != 0) {
2906: trdp = rcs_findrev(rfp, rdp->rd_next);
2907: if (trdp == NULL)
2908: fatal("failed to grab next revision");
2909: }
2910:
2911: if (rdp->rd_tlen == 0) {
2912: rcs_parse_deltatexts(rfp, rdp->rd_num);
2913: if (rdp->rd_tlen == 0) {
2914: if (!rcsnum_differ(rdp->rd_num, bnum))
2915: break;
2916: rdp = trdp;
2917: continue;
2918: }
2919: }
2920:
2921: plen = rdp->rd_tlen;
2922: patch = rdp->rd_text;
2923: plines = cvs_splitlines(patch, plen);
2924: rcs_patch_lines(dlines, plines, NULL, rdp);
2925: cvs_freelines(plines);
2926:
2927: if (!rcsnum_differ(rdp->rd_num, bnum))
2928: break;
2929:
2930: rdp = trdp;
2931: }
2932: } while (rcsnum_differ(rdp->rd_num, frev));
2933:
2934: if (bnum != frev)
2935: rcsnum_free(bnum);
2936:
2937: /*
2938: * All lines have been parsed, now they must be copied over
2939: * into alines (array) again.
2940: */
2941: xfree(*alines);
2942:
2943: i = 0;
2944: TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
2945: if (line->l_line != NULL)
2946: i++;
2947: }
1.239 ! tobias 2948: *alines = xcalloc(i + 1, sizeof(struct cvs_line *));
1.225 tobias 2949: (*alines)[i] = NULL;
2950:
2951: i = 0;
2952: TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
2953: if (line->l_line != NULL)
2954: (*alines)[i++] = line;
2955: }
1.196 niallo 2956: }
2957:
2958: /*
2959: * rcs_rev_getbuf()
2960: *
2961: * XXX: This is really really slow and should be avoided if at all possible!
2962: *
2963: * Get the entire contents of revision <rev> from the RCSFILE <rfp> and
2964: * return it as a BUF pointer.
2965: */
2966: BUF *
1.200 joris 2967: rcs_rev_getbuf(RCSFILE *rfp, RCSNUM *rev, int mode)
1.196 niallo 2968: {
1.200 joris 2969: int expmode, expand;
2970: struct rcs_delta *rdp;
1.196 niallo 2971: struct cvs_lines *lines;
1.233 tobias 2972: struct cvs_line *lp, *nlp;
1.196 niallo 2973: BUF *bp;
2974:
1.200 joris 2975: expand = 0;
1.218 tobias 2976: lines = rcs_rev_getlines(rfp, rev, NULL);
1.196 niallo 2977: bp = cvs_buf_alloc(1024, BUF_AUTOEXT);
1.200 joris 2978:
2979: if (!(mode & RCS_KWEXP_NONE)) {
2980: if (rfp->rf_expand != NULL)
2981: expmode = rcs_kwexp_get(rfp);
2982: else
2983: expmode = RCS_KWEXP_DEFAULT;
2984:
2985: if (!(expmode & RCS_KWEXP_NONE)) {
2986: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2987: fatal("could not fetch revision");
2988: expand = 1;
2989: }
2990: }
2991:
1.233 tobias 2992: for(lp = TAILQ_FIRST(&lines->l_lines); lp != NULL;) {
2993: nlp = TAILQ_NEXT(lp, l_list);
2994:
2995: if (lp->l_line == NULL) {
2996: lp = nlp;
1.196 niallo 2997: continue;
1.233 tobias 2998: }
1.200 joris 2999:
3000: if (expand)
1.233 tobias 3001: rcs_kwexp_line(rfp->rf_path, rdp, lines, lp, expmode);
1.200 joris 3002:
1.233 tobias 3003: do {
3004: cvs_buf_append(bp, lp->l_line, lp->l_len);
3005: } while ((lp = TAILQ_NEXT(lp, l_list)) != nlp);
1.196 niallo 3006: }
3007:
3008: cvs_freelines(lines);
3009:
3010: return (bp);
3011: }
3012:
3013: /*
3014: * rcs_rev_write_fd()
3015: *
3016: * Write the entire contents of revision <frev> from the rcsfile <rfp> to
3017: * file descriptor <fd>.
3018: */
3019: void
3020: rcs_rev_write_fd(RCSFILE *rfp, RCSNUM *rev, int fd, int mode)
3021: {
1.200 joris 3022: int expmode, expand;
1.196 niallo 3023: struct rcs_delta *rdp;
3024: struct cvs_lines *lines;
1.233 tobias 3025: struct cvs_line *lp, *nlp;
1.217 joris 3026: extern int print_stdout;
1.196 niallo 3027:
1.200 joris 3028: expand = 0;
1.218 tobias 3029: lines = rcs_rev_getlines(rfp, rev, NULL);
1.200 joris 3030:
1.196 niallo 3031: if (!(mode & RCS_KWEXP_NONE)) {
3032: if (rfp->rf_expand != NULL)
3033: expmode = rcs_kwexp_get(rfp);
3034: else
3035: expmode = RCS_KWEXP_DEFAULT;
3036:
3037: if (!(expmode & RCS_KWEXP_NONE)) {
3038: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
3039: fatal("could not fetch revision");
1.200 joris 3040: expand = 1;
1.196 niallo 3041: }
3042: }
1.200 joris 3043:
1.233 tobias 3044: for(lp = TAILQ_FIRST(&lines->l_lines); lp != NULL;) {
3045: nlp = TAILQ_NEXT(lp, l_list);
3046:
3047: if (lp->l_line == NULL) {
3048: lp = nlp;
1.196 niallo 3049: continue;
1.233 tobias 3050: }
1.200 joris 3051:
3052: if (expand)
1.233 tobias 3053: rcs_kwexp_line(rfp->rf_path, rdp, lines, lp, expmode);
1.217 joris 3054:
1.233 tobias 3055: do {
3056: /*
3057: * Solely for the checkout and update -p options.
3058: */
3059: if (cvs_server_active == 1 &&
3060: (cvs_cmdop == CVS_OP_CHECKOUT ||
3061: cvs_cmdop == CVS_OP_UPDATE) && print_stdout == 1) {
3062: if (atomicio(vwrite, fd, "M ", 2) != 2)
3063: fatal("rcs_rev_write_fd: %s",
3064: strerror(errno));
3065: }
3066:
3067: if (atomicio(vwrite, fd, lp->l_line, lp->l_len) !=
3068: lp->l_len)
1.217 joris 3069: fatal("rcs_rev_write_fd: %s", strerror(errno));
1.233 tobias 3070: } while ((lp = TAILQ_NEXT(lp, l_list)) != nlp);
1.196 niallo 3071: }
3072:
3073: /* XXX: do we need to call futimes(2) on the output fd? */
3074:
3075: cvs_freelines(lines);
3076:
3077: }
3078:
3079: /*
3080: * rcs_rev_write_stmp()
3081: *
3082: * Write the contents of the rev <rev> to a temporary file whose path is
3083: * specified using <template> (see mkstemp(3)). NB. This function will modify
3084: * <template>, as per mkstemp.
3085: */
3086: void
3087: rcs_rev_write_stmp(RCSFILE *rfp, RCSNUM *rev, char *template, int mode)
3088: {
3089: int fd;
3090:
3091: if ((fd = mkstemp(template)) == -1)
3092: fatal("mkstemp: `%s': %s", template, strerror(errno));
3093:
3094: cvs_worklist_add(template, &temp_files);
3095: rcs_rev_write_fd(rfp, rev, fd, mode);
3096:
3097: (void)close(fd);
3098: }
3099:
3100: static void
1.233 tobias 3101: rcs_kwexp_line(char *rcsfile, struct rcs_delta *rdp, struct cvs_lines *lines,
3102: struct cvs_line *line, int mode)
1.196 niallo 3103: {
1.231 tobias 3104: BUF *tmpbuf;
1.196 niallo 3105: int kwtype;
3106: u_int j, found;
1.209 otto 3107: const u_char *c, *start, *fin, *end;
3108: char *kwstr;
1.196 niallo 3109: char expbuf[256], buf[256];
3110: char *fmt;
1.231 tobias 3111: size_t clen, kwlen, len, tlen;
1.196 niallo 3112:
3113: kwtype = 0;
3114: kwstr = NULL;
1.230 tobias 3115:
3116: if (mode & RCS_KWEXP_OLD)
3117: return;
1.196 niallo 3118:
3119: len = line->l_len;
3120: if (len == 0)
3121: return;
3122:
3123: c = line->l_line;
3124: found = 0;
3125: /* Final character in buffer. */
3126: fin = c + len - 1;
3127:
3128: /*
3129: * Keyword formats:
3130: * $Keyword$
3131: * $Keyword: value$
3132: */
3133: for (; c < fin; c++) {
1.231 tobias 3134: if (*c != '$')
3135: continue;
1.196 niallo 3136:
1.231 tobias 3137: /* remember start of this possible keyword */
3138: start = c;
1.196 niallo 3139:
1.231 tobias 3140: /* first following character has to be alphanumeric */
3141: c++;
3142: if (!isalpha(*c)) {
3143: c = start;
3144: continue;
3145: }
1.196 niallo 3146:
1.231 tobias 3147: /* Number of characters between c and fin, inclusive. */
3148: clen = fin - c + 1;
1.196 niallo 3149:
1.231 tobias 3150: /* look for any matching keywords */
3151: found = 0;
3152: for (j = 0; j < RCS_NKWORDS; j++) {
3153: kwlen = strlen(rcs_expkw[j].kw_str);
1.196 niallo 3154: /*
1.231 tobias 3155: * kwlen must be less than clen since clen
3156: * includes either a terminating `$' or a `:'.
1.196 niallo 3157: */
1.231 tobias 3158: if (kwlen < clen &&
3159: memcmp(c, rcs_expkw[j].kw_str, kwlen) == 0 &&
3160: (c[kwlen] == '$' || c[kwlen] == ':')) {
3161: found = 1;
3162: kwstr = rcs_expkw[j].kw_str;
3163: kwtype = rcs_expkw[j].kw_type;
3164: c += kwlen;
3165: break;
3166: }
3167: }
1.196 niallo 3168:
1.231 tobias 3169: if (found == 0 && cvs_tagname != NULL) {
3170: kwlen = strlen(cvs_tagname);
3171: if (kwlen < clen &&
3172: memcmp(c, cvs_tagname, kwlen) == 0 &&
3173: (c[kwlen] == '$' || c[kwlen] == ':')) {
3174: found = 1;
3175: kwstr = cvs_tagname;
3176: kwtype = RCS_KW_ID;
3177: c += kwlen;
1.196 niallo 3178: }
1.231 tobias 3179: }
1.196 niallo 3180:
1.231 tobias 3181: /* unknown keyword, continue looking */
3182: if (found == 0) {
3183: c = start;
3184: continue;
3185: }
1.196 niallo 3186:
1.231 tobias 3187: /*
3188: * if the next character was ':' we need to look for
3189: * an '$' before the end of the line to be sure it is
3190: * in fact a keyword.
3191: */
3192: if (*c == ':') {
3193: for (; c <= fin; ++c) {
3194: if (*c == '$' || *c == '\n')
3195: break;
1.196 niallo 3196: }
3197:
1.231 tobias 3198: if (*c != '$') {
3199: c = start;
3200: continue;
3201: }
3202: }
3203: end = c + 1;
1.196 niallo 3204:
1.231 tobias 3205: /* start constructing the expansion */
3206: expbuf[0] = '\0';
1.196 niallo 3207:
1.231 tobias 3208: if (mode & RCS_KWEXP_NAME) {
3209: if (strlcat(expbuf, "$", sizeof(expbuf)) >=
3210: sizeof(expbuf) || strlcat(expbuf, kwstr,
3211: sizeof(expbuf)) >= sizeof(expbuf))
3212: fatal("rcs_kwexp_line: truncated");
3213: if ((mode & RCS_KWEXP_VAL) &&
3214: strlcat(expbuf, ": ", sizeof(expbuf)) >=
3215: sizeof(expbuf))
3216: fatal("rcs_kwexp_line: truncated");
3217: }
1.196 niallo 3218:
1.231 tobias 3219: /*
3220: * order matters because of RCS_KW_ID and
3221: * RCS_KW_HEADER here
3222: */
3223: if (mode & RCS_KWEXP_VAL) {
3224: if (kwtype & RCS_KW_RCSFILE) {
3225: if (!(kwtype & RCS_KW_FULLPATH))
3226: (void)strlcat(expbuf, basename(rcsfile),
3227: sizeof(expbuf));
3228: else
3229: (void)strlcat(expbuf, rcsfile,
3230: sizeof(expbuf));
3231: if (strlcat(expbuf, " ", sizeof(expbuf)) >=
3232: sizeof(expbuf))
3233: fatal("rcs_kwexp_line: truncated");
3234: }
1.211 niallo 3235:
1.231 tobias 3236: if (kwtype & RCS_KW_REVISION) {
3237: rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
3238: if (strlcat(buf, " ", sizeof(buf)) >=
3239: sizeof(buf) || strlcat(expbuf, buf,
3240: sizeof(expbuf)) >= sizeof(buf))
3241: fatal("rcs_kwexp_line: truncated");
3242: }
1.196 niallo 3243:
1.231 tobias 3244: if (kwtype & RCS_KW_DATE) {
3245: fmt = "%Y/%m/%d %H:%M:%S ";
1.196 niallo 3246:
1.231 tobias 3247: if (strftime(buf, sizeof(buf), fmt,
3248: &rdp->rd_date) == 0)
3249: fatal("rcs_kwexp_line: strftime "
3250: "failure");
3251: if (strlcat(expbuf, buf, sizeof(expbuf)) >=
3252: sizeof(expbuf))
3253: fatal("rcs_kwexp_line: string "
3254: "truncated");
3255: }
1.196 niallo 3256:
1.231 tobias 3257: if (kwtype & RCS_KW_MDOCDATE) {
3258: /*
3259: * Do not prepend ' ' for a single
3260: * digit, %e would do so and there is
3261: * no better format for strftime().
3262: */
3263: if (rdp->rd_date.tm_mday < 10)
3264: fmt = "%B%e %Y ";
3265: else
3266: fmt = "%B %e %Y ";
3267:
3268: if (strftime(buf, sizeof(buf), fmt,
3269: &rdp->rd_date) == 0)
3270: fatal("rcs_kwexp_line: strftime "
3271: "failure");
3272: if (strlcat(expbuf, buf, sizeof(expbuf)) >=
3273: sizeof(expbuf))
3274: fatal("rcs_kwexp_line: string "
3275: "truncated");
3276: }
3277:
3278: if (kwtype & RCS_KW_AUTHOR) {
3279: if (strlcat(expbuf, rdp->rd_author,
3280: sizeof(expbuf)) >= sizeof(expbuf) ||
3281: strlcat(expbuf, " ", sizeof(expbuf)) >=
3282: sizeof(expbuf))
3283: fatal("rcs_kwexp_line: string "
3284: "truncated");
3285: }
3286:
3287: if (kwtype & RCS_KW_STATE) {
3288: if (strlcat(expbuf, rdp->rd_state,
3289: sizeof(expbuf)) >= sizeof(expbuf) ||
3290: strlcat(expbuf, " ", sizeof(expbuf)) >=
3291: sizeof(expbuf))
3292: fatal("rcs_kwexp_line: string "
3293: "truncated");
3294: }
3295:
3296: /* order does not matter anymore below */
1.233 tobias 3297: if (kwtype & RCS_KW_LOG) {
3298: char linebuf[256];
3299: struct cvs_line *cur, *lp;
3300: char *logp, *l_line, *prefix, *q, *sprefix;
3301: size_t i;
3302:
1.236 tobias 3303: /* Log line */
1.233 tobias 3304: if (!(kwtype & RCS_KW_FULLPATH))
3305: (void)strlcat(expbuf,
3306: basename(rcsfile), sizeof(expbuf));
3307: else
3308: (void)strlcat(expbuf, rcsfile,
3309: sizeof(expbuf));
3310:
1.231 tobias 3311: if (strlcat(expbuf, " ", sizeof(expbuf)) >=
3312: sizeof(expbuf))
3313: fatal("rcs_kwexp_line: string "
3314: "truncated");
1.233 tobias 3315:
3316: cur = line;
3317:
3318: /* copy rdp->rd_log for strsep */
3319: logp = xstrdup(rdp->rd_log);
3320:
3321: /* copy our prefix for later processing */
3322: prefix = xmalloc(start - line->l_line + 1);
3323: memcpy(prefix, line->l_line,
3324: start - line->l_line);
3325: prefix[start - line->l_line] = '\0';
3326:
3327: /* copy also prefix without trailing blanks. */
3328: sprefix = xstrdup(prefix);
3329: for (i = strlen(sprefix); i > 0 &&
3330: sprefix[i - 1] == ' '; i--)
3331: sprefix[i - 1] = '\0';
3332:
3333: /* new line: revision + date + author */
3334: linebuf[0] = '\0';
3335: if (strlcat(linebuf, "Revision ",
3336: sizeof(linebuf)) >= sizeof(linebuf))
3337: fatal("rcs_kwexp_line: truncated");
3338: rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
3339: if (strlcat(linebuf, buf, sizeof(linebuf))
3340: >= sizeof(buf))
3341: fatal("rcs_kwexp_line: truncated");
3342: fmt = " %Y/%m/%d %H:%M:%S ";
3343: if (strftime(buf, sizeof(buf), fmt,
3344: &rdp->rd_date) == 0)
3345: fatal("rcs_kwexp_line: strftime "
3346: "failure");
3347: if (strlcat(linebuf, buf, sizeof(linebuf))
3348: >= sizeof(linebuf))
3349: fatal("rcs_kwexp_line: string "
3350: "truncated");
3351: if (strlcat(linebuf, rdp->rd_author,
3352: sizeof(linebuf)) >= sizeof(linebuf))
3353: fatal("rcs_kwexp_line: string "
3354: "truncated");
3355:
3356: lp = xcalloc(1, sizeof(*lp));
3357: xasprintf(&(lp->l_line), "%s%s\n",
3358: prefix, linebuf);
3359: lp->l_len = strlen(lp->l_line);
3360: TAILQ_INSERT_AFTER(&(lines->l_lines), cur, lp,
3361: l_list);
3362: cur = lp;
3363:
3364: /* Log message */
3365: q = logp;
3366: while ((l_line = strsep(&q, "\n")) != NULL &&
3367: q != NULL) {
3368: lp = xcalloc(1, sizeof(*lp));
3369:
3370: if (l_line[0] == '\0') {
3371: xasprintf(&(lp->l_line), "%s\n",
3372: sprefix);
3373: } else {
3374: xasprintf(&(lp->l_line),
3375: "%s%s\n", prefix, l_line);
3376: }
3377:
3378: lp->l_len = strlen(lp->l_line);
3379: TAILQ_INSERT_AFTER(&(lines->l_lines),
3380: cur, lp, l_list);
3381: cur = lp;
3382: }
3383: xfree(logp);
3384:
3385: /*
3386: * This is just another hairy mess, but it must
1.237 tobias 3387: * be done: All characters behind Log will be
1.233 tobias 3388: * written in a new line next to log messages.
3389: * But that's not enough, we have to strip all
3390: * trailing whitespaces of our prefix.
3391: */
3392: lp = xcalloc(1, sizeof(*lp));
3393: lp->l_line = xcalloc(strlen(sprefix) +
3394: line->l_line + line->l_len - end + 1, 1);
3395: strlcpy(lp->l_line, sprefix,
3396: strlen(sprefix) + 1);
3397: memcpy(lp->l_line + strlen(sprefix),
3398: end, line->l_line + line->l_len - end);
3399: lp->l_len = strlen(lp->l_line);
3400: TAILQ_INSERT_AFTER(&(lines->l_lines), cur, lp,
3401: l_list);
3402: cur = lp;
3403:
3404: end = line->l_line + line->l_len - 1;
3405:
3406: xfree(prefix);
3407: xfree(sprefix);
3408:
3409: }
1.231 tobias 3410:
3411: if (kwtype & RCS_KW_SOURCE) {
3412: if (strlcat(expbuf, rcsfile, sizeof(expbuf)) >=
3413: sizeof(expbuf) || strlcat(expbuf, " ",
3414: sizeof(expbuf)) >= sizeof(expbuf))
3415: fatal("rcs_kwexp_line: string "
3416: "truncated");
1.196 niallo 3417: }
3418:
1.231 tobias 3419: if (kwtype & RCS_KW_NAME)
3420: if (strlcat(expbuf, " ", sizeof(expbuf)) >=
3421: sizeof(expbuf))
3422: fatal("rcs_kwexp_line: string "
3423: "truncated");
1.196 niallo 3424:
1.231 tobias 3425: if (kwtype & RCS_KW_LOCKER)
3426: if (strlcat(expbuf, " ", sizeof(expbuf)) >=
3427: sizeof(expbuf))
3428: fatal("rcs_kwexp_line: string "
3429: "truncated");
1.196 niallo 3430: }
1.231 tobias 3431:
3432: /* end the expansion */
3433: if (mode & RCS_KWEXP_NAME)
3434: if (strlcat(expbuf, "$",
3435: sizeof(expbuf)) >= sizeof(expbuf))
3436: fatal("rcs_kwexp_line: truncated");
3437:
3438: /* Concatenate everything together. */
3439: tmpbuf = cvs_buf_alloc(len + strlen(expbuf), BUF_AUTOEXT);
3440: /* Append everything before keyword. */
3441: cvs_buf_append(tmpbuf, line->l_line,
3442: start - line->l_line);
3443: /* Append keyword. */
3444: cvs_buf_append(tmpbuf, expbuf, strlen(expbuf));
3445: /* Point c to end of keyword. */
3446: tlen = cvs_buf_len(tmpbuf) - 1;
3447: /* Append everything after keyword. */
3448: cvs_buf_append(tmpbuf, end,
3449: line->l_line + line->l_len - end);
3450: c = cvs_buf_get(tmpbuf) + tlen;
3451: /* Point fin to end of data. */
3452: fin = cvs_buf_get(tmpbuf) + cvs_buf_len(tmpbuf) - 1;
3453: /* Recalculate new length. */
3454: len = cvs_buf_len(tmpbuf);
3455:
3456: /* tmpbuf is now ready, convert to string */
3457: if (line->l_needsfree)
3458: xfree(line->l_line);
3459: line->l_len = len;
3460: line->l_line = cvs_buf_release(tmpbuf);
3461: line->l_needsfree = 1;
1.196 niallo 3462: }
1.131 niallo 3463: }