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