Annotation of src/usr.bin/cvs/rcs.c, Revision 1.263
1.263 ! tobias 1: /* $OpenBSD: rcs.c,v 1.262 2008/03/13 17:01:25 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: }
1.263 ! tobias 1146:
! 1147: cvs_freelines(plines);
1.241 joris 1148:
1149: *ladded = added;
1150: *lremoved = removed;
1.1 jfb 1151: }
1152:
1153: /*
1.52 jfb 1154: * rcs_rev_add()
1155: *
1.53 jfb 1156: * Add a revision to the RCS file <rf>. The new revision's number can be
1157: * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
1158: * new revision will have a number equal to the previous head revision plus
1159: * one). The <msg> argument specifies the log message for that revision, and
1160: * <date> specifies the revision's date (a value of -1 is
1161: * equivalent to using the current time).
1.96 xsa 1162: * If <username> is NULL, set the author for this revision to the current user.
1.90 niallo 1163: * Otherwise, set it to <username>.
1.52 jfb 1164: * Returns 0 on success, or -1 on failure.
1165: */
1166: int
1.90 niallo 1167: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
1168: const char *username)
1.52 jfb 1169: {
1170: time_t now;
1.222 joris 1171: RCSNUM *root;
1.52 jfb 1172: struct passwd *pw;
1.180 joris 1173: struct rcs_branch *brp;
1.83 joris 1174: struct rcs_delta *ordp, *rdp;
1175:
1.52 jfb 1176: if (rev == RCS_HEAD_REV) {
1.101 niallo 1177: if (rf->rf_flags & RCS_CREATE) {
1178: if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
1179: return (-1);
1.111 joris 1180: rf->rf_head = rcsnum_alloc();
1.101 niallo 1181: rcsnum_cpy(rev, rf->rf_head, 0);
1182: } else {
1183: rev = rcsnum_inc(rf->rf_head);
1184: }
1.83 joris 1185: } else {
1.254 joris 1186: if ((rdp = rcs_findrev(rf, rev)) != NULL)
1.83 joris 1187: return (-1);
1.52 jfb 1188: }
1189:
1.112 xsa 1190: if ((pw = getpwuid(getuid())) == NULL)
1191: fatal("getpwuid failed");
1.52 jfb 1192:
1.161 ray 1193: rdp = xcalloc(1, sizeof(*rdp));
1.52 jfb 1194:
1195: TAILQ_INIT(&(rdp->rd_branches));
1196:
1.111 joris 1197: rdp->rd_num = rcsnum_alloc();
1.52 jfb 1198: rcsnum_cpy(rev, rdp->rd_num, 0);
1.83 joris 1199:
1.111 joris 1200: rdp->rd_next = rcsnum_alloc();
1.92 joris 1201:
1.90 niallo 1202: if (username == NULL)
1203: username = pw->pw_name;
1204:
1.110 joris 1205: rdp->rd_author = xstrdup(username);
1206: rdp->rd_state = xstrdup(RCS_STATE_EXP);
1207: rdp->rd_log = xstrdup(msg);
1.52 jfb 1208:
1.53 jfb 1209: if (date != (time_t)(-1))
1210: now = date;
1211: else
1212: time(&now);
1.52 jfb 1213: gmtime_r(&now, &(rdp->rd_date));
1214:
1.180 joris 1215: if (RCSNUM_ISBRANCHREV(rev))
1216: TAILQ_INSERT_TAIL(&(rf->rf_delta), rdp, rd_list);
1217: else
1218: TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1.52 jfb 1219: rf->rf_ndelta++;
1.81 niallo 1220:
1.180 joris 1221: if (!(rf->rf_flags & RCS_CREATE)) {
1222: if (RCSNUM_ISBRANCHREV(rev)) {
1.222 joris 1223: root = rcsnum_branch_root(rev);
1.180 joris 1224: brp = xmalloc(sizeof(*brp));
1225: brp->rb_num = rcsnum_alloc();
1226: rcsnum_cpy(rdp->rd_num, brp->rb_num, 0);
1.222 joris 1227: if ((ordp = rcs_findrev(rf, root)) == NULL)
1228: fatal("root node not found");
1229: if (TAILQ_EMPTY(&(ordp->rd_branches))) {
1230: TAILQ_INSERT_TAIL(&(ordp->rd_branches),
1231: brp, rb_list);
1232: }
1233:
1.208 otto 1234: ordp = TAILQ_PREV(rdp, rcs_dlist, rd_list);
1.222 joris 1235: if (RCSNUM_ISBRANCHREV(ordp->rd_num))
1236: rcsnum_cpy(rdp->rd_num, ordp->rd_next, 0);
1.180 joris 1237: } else {
1238: ordp = TAILQ_NEXT(rdp, rd_list);
1239: rcsnum_cpy(ordp->rd_num, rdp->rd_next, 0);
1240: }
1241: }
1242:
1.64 niallo 1243: /* not synced anymore */
1244: rf->rf_flags &= ~RCS_SYNCED;
1.52 jfb 1245:
1246: return (0);
1247: }
1248:
1249: /*
1250: * rcs_rev_remove()
1251: *
1252: * Remove the revision whose number is <rev> from the RCS file <rf>.
1253: */
1254: int
1255: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
1256: {
1.251 joris 1257: int fd1, fd2;
1.194 joris 1258: char *path_tmp1, *path_tmp2;
1.166 joris 1259: struct rcs_delta *rdp, *prevrdp, *nextrdp;
1.209 otto 1260: BUF *prevbuf, *newdiff, *newdeltatext;
1.166 joris 1261:
1.52 jfb 1262: if (rev == RCS_HEAD_REV)
1263: rev = rf->rf_head;
1264:
1265: /* do we actually have that revision? */
1.254 joris 1266: if ((rdp = rcs_findrev(rf, rev)) == NULL)
1.166 joris 1267: return (-1);
1268:
1269: /*
1270: * This is confusing, the previous delta is next in the TAILQ list.
1271: * the next delta is the previous one in the TAILQ list.
1272: *
1273: * When the HEAD revision got specified, nextrdp will be NULL.
1274: * When the first revision got specified, prevrdp will be NULL.
1275: */
1276: prevrdp = (struct rcs_delta *)TAILQ_NEXT(rdp, rd_list);
1277: nextrdp = (struct rcs_delta *)TAILQ_PREV(rdp, cvs_tqh, rd_list);
1278:
1279: newdeltatext = NULL;
1.209 otto 1280: prevbuf = NULL;
1.262 joris 1281: path_tmp1 = path_tmp2 = NULL;
1.166 joris 1282:
1.168 deraadt 1283: if (prevrdp != NULL && nextrdp != NULL) {
1.249 tobias 1284: newdiff = cvs_buf_alloc(64);
1.166 joris 1285:
1286: /* calculate new diff */
1.172 joris 1287: (void)xasprintf(&path_tmp1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
1.251 joris 1288: fd1 = rcs_rev_write_stmp(rf, nextrdp->rd_num, path_tmp1, 0);
1.166 joris 1289:
1.172 joris 1290: (void)xasprintf(&path_tmp2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
1.251 joris 1291: fd2 = rcs_rev_write_stmp(rf, prevrdp->rd_num, path_tmp2, 0);
1.166 joris 1292:
1293: diff_format = D_RCSDIFF;
1.251 joris 1294: if (cvs_diffreg(path_tmp1, path_tmp2,
1295: fd1, fd2, newdiff) == D_ERROR)
1.172 joris 1296: fatal("rcs_diffreg failed");
1.166 joris 1297:
1.251 joris 1298: close(fd1);
1299: close(fd2);
1300:
1.194 joris 1301: newdeltatext = newdiff;
1.168 deraadt 1302: } else if (nextrdp == NULL && prevrdp != NULL) {
1.194 joris 1303: newdeltatext = prevbuf;
1.166 joris 1304: }
1305:
1306: if (newdeltatext != NULL) {
1307: if (rcs_deltatext_set(rf, prevrdp->rd_num, newdeltatext) < 0)
1308: fatal("error setting new deltatext");
1309: }
1310:
1311: TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
1312:
1313: /* update pointers */
1.168 deraadt 1314: if (prevrdp != NULL && nextrdp != NULL) {
1.166 joris 1315: rcsnum_cpy(prevrdp->rd_num, nextrdp->rd_next, 0);
1316: } else if (prevrdp != NULL) {
1.170 xsa 1317: if (rcs_head_set(rf, prevrdp->rd_num) < 0)
1318: fatal("rcs_head_set failed");
1.166 joris 1319: } else if (nextrdp != NULL) {
1320: rcsnum_free(nextrdp->rd_next);
1321: nextrdp->rd_next = rcsnum_alloc();
1.52 jfb 1322: } else {
1.166 joris 1323: rcsnum_free(rf->rf_head);
1324: rf->rf_head = NULL;
1.52 jfb 1325: }
1326:
1.166 joris 1327: rf->rf_ndelta--;
1328: rf->rf_flags &= ~RCS_SYNCED;
1329:
1330: rcs_freedelta(rdp);
1331:
1332: if (newdeltatext != NULL)
1333: xfree(newdeltatext);
1.52 jfb 1334:
1.172 joris 1335: if (path_tmp1 != NULL)
1336: xfree(path_tmp1);
1337: if (path_tmp2 != NULL)
1338: xfree(path_tmp2);
1339:
1.166 joris 1340: return (0);
1.52 jfb 1341: }
1342:
1343: /*
1.1 jfb 1344: * rcs_findrev()
1345: *
1346: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1347: * The revision number is given in <rev>.
1.156 joris 1348: *
1.1 jfb 1349: * Returns a pointer to the delta on success, or NULL on failure.
1350: */
1.102 xsa 1351: struct rcs_delta *
1.117 niallo 1352: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
1.1 jfb 1353: {
1.178 joris 1354: int isbrev;
1.156 joris 1355: struct rcs_delta *rdp;
1.178 joris 1356:
1.261 tobias 1357: if (rev == NULL)
1358: return NULL;
1359:
1.178 joris 1360: isbrev = RCSNUM_ISBRANCHREV(rev);
1.26 jfb 1361:
1.117 niallo 1362: /*
1363: * We need to do more parsing if the last revision in the linked list
1364: * is greater than the requested revision.
1365: */
1.156 joris 1366: rdp = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1.168 deraadt 1367: if (rdp == NULL ||
1.178 joris 1368: (!isbrev && rcsnum_cmp(rdp->rd_num, rev, 0) == -1) ||
1369: ((isbrev && rdp->rd_num->rn_len < 4) ||
1370: (isbrev && rcsnum_differ(rev, rdp->rd_num)))) {
1.117 niallo 1371: rcs_parse_deltas(rfp, rev);
1372: }
1373:
1.156 joris 1374: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1.178 joris 1375: if (rcsnum_differ(rdp->rd_num, rev))
1376: continue;
1377: else
1.156 joris 1378: return (rdp);
1379: }
1.1 jfb 1380:
1381: return (NULL);
1.20 jfb 1382: }
1383:
1384: /*
1.26 jfb 1385: * rcs_kwexp_set()
1386: *
1387: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1388: */
1.162 xsa 1389: void
1.26 jfb 1390: rcs_kwexp_set(RCSFILE *file, int mode)
1391: {
1392: int i;
1393: char *tmp, buf[8] = "";
1394:
1395: if (RCS_KWEXP_INVAL(mode))
1.162 xsa 1396: return;
1.26 jfb 1397:
1398: i = 0;
1399: if (mode == RCS_KWEXP_NONE)
1400: buf[0] = 'b';
1401: else if (mode == RCS_KWEXP_OLD)
1402: buf[0] = 'o';
1403: else {
1404: if (mode & RCS_KWEXP_NAME)
1405: buf[i++] = 'k';
1406: if (mode & RCS_KWEXP_VAL)
1407: buf[i++] = 'v';
1408: if (mode & RCS_KWEXP_LKR)
1409: buf[i++] = 'l';
1410: }
1411:
1.110 joris 1412: tmp = xstrdup(buf);
1.27 jfb 1413: if (file->rf_expand != NULL)
1.147 ray 1414: xfree(file->rf_expand);
1.26 jfb 1415: file->rf_expand = tmp;
1.64 niallo 1416: /* not synced anymore */
1417: file->rf_flags &= ~RCS_SYNCED;
1.26 jfb 1418: }
1419:
1420: /*
1421: * rcs_kwexp_get()
1422: *
1423: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1424: */
1425: int
1426: rcs_kwexp_get(RCSFILE *file)
1427: {
1428: return rcs_kflag_get(file->rf_expand);
1429: }
1430:
1431: /*
1.20 jfb 1432: * rcs_kflag_get()
1433: *
1434: * Get the keyword expansion mode from a set of character flags given in
1435: * <flags> and return the appropriate flag mask. In case of an error, the
1436: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1437: */
1438: int
1439: rcs_kflag_get(const char *flags)
1440: {
1441: int fl;
1442: size_t len;
1443: const char *fp;
1.253 tobias 1444:
1445: if (flags == NULL)
1446: return 0;
1.20 jfb 1447:
1448: fl = 0;
1.235 tobias 1449: if (!(len = strlen(flags)))
1450: return RCS_KWEXP_ERR;
1.20 jfb 1451:
1452: for (fp = flags; *fp != '\0'; fp++) {
1453: if (*fp == 'k')
1454: fl |= RCS_KWEXP_NAME;
1455: else if (*fp == 'v')
1456: fl |= RCS_KWEXP_VAL;
1457: else if (*fp == 'l')
1458: fl |= RCS_KWEXP_LKR;
1459: else if (*fp == 'o') {
1460: if (len != 1)
1461: fl |= RCS_KWEXP_ERR;
1462: fl |= RCS_KWEXP_OLD;
1463: } else if (*fp == 'b') {
1464: if (len != 1)
1465: fl |= RCS_KWEXP_ERR;
1.205 xsa 1466: fl |= RCS_KWEXP_NONE;
1.20 jfb 1467: } else /* unknown letter */
1468: fl |= RCS_KWEXP_ERR;
1469: }
1470:
1471: return (fl);
1.1 jfb 1472: }
1473:
1.117 niallo 1474: /* rcs_parse_deltas()
1475: *
1476: * Parse deltas. If <rev> is not NULL, parse only as far as that
1477: * revision. If <rev> is NULL, parse all deltas.
1478: */
1.146 xsa 1479: static void
1.117 niallo 1480: rcs_parse_deltas(RCSFILE *rfp, RCSNUM *rev)
1481: {
1482: int ret;
1483: struct rcs_delta *enddelta;
1.146 xsa 1484:
1485: if ((rfp->rf_flags & PARSED_DELTAS) || (rfp->rf_flags & RCS_CREATE))
1486: return;
1487:
1.117 niallo 1488: for (;;) {
1489: ret = rcs_parse_delta(rfp);
1490: if (rev != NULL) {
1491: enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1.155 ray 1492: if (rcsnum_cmp(enddelta->rd_num, rev, 0) == 0)
1.117 niallo 1493: break;
1494: }
1.180 joris 1495:
1.117 niallo 1496: if (ret == 0) {
1497: rfp->rf_flags |= PARSED_DELTAS;
1498: break;
1499: }
1500: else if (ret == -1)
1501: fatal("error parsing deltas");
1502: }
1503: }
1504:
1505: /* rcs_parse_deltatexts()
1506: *
1507: * Parse deltatexts. If <rev> is not NULL, parse only as far as that
1508: * revision. If <rev> is NULL, parse everything.
1509: */
1.146 xsa 1510: static void
1.117 niallo 1511: rcs_parse_deltatexts(RCSFILE *rfp, RCSNUM *rev)
1512: {
1513: int ret;
1514: struct rcs_delta *rdp;
1.146 xsa 1515:
1.168 deraadt 1516: if ((rfp->rf_flags & PARSED_DELTATEXTS) ||
1517: (rfp->rf_flags & RCS_CREATE))
1.146 xsa 1518: return;
1519:
1.117 niallo 1520: if (!(rfp->rf_flags & PARSED_DESC))
1521: rcs_parse_desc(rfp, rev);
1522: for (;;) {
1523: if (rev != NULL) {
1524: rdp = rcs_findrev(rfp, rev);
1525: if (rdp->rd_text != NULL)
1526: break;
1527: else
1528: ret = rcs_parse_deltatext(rfp);
1529: } else
1530: ret = rcs_parse_deltatext(rfp);
1531: if (ret == 0) {
1532: rfp->rf_flags |= PARSED_DELTATEXTS;
1533: break;
1534: }
1.146 xsa 1535: else if (ret == -1)
1.117 niallo 1536: fatal("problem parsing deltatexts");
1537: }
1538: }
1539:
1540: /* rcs_parse_desc()
1541: *
1542: * Parse RCS description.
1543: */
1.148 xsa 1544: static void
1.117 niallo 1545: rcs_parse_desc(RCSFILE *rfp, RCSNUM *rev)
1546: {
1547: int ret = 0;
1.148 xsa 1548:
1549: if ((rfp->rf_flags & PARSED_DESC) || (rfp->rf_flags & RCS_CREATE))
1550: return;
1.122 reyk 1551: if (!(rfp->rf_flags & PARSED_DELTAS))
1.117 niallo 1552: rcs_parse_deltas(rfp, rev);
1553: /* do parsing */
1554: ret = rcs_gettok(rfp);
1.148 xsa 1555: if (ret != RCS_TOK_DESC)
1556: fatal("token `%s' found where RCS desc expected",
1.117 niallo 1557: RCS_TOKSTR(rfp));
1558:
1559: ret = rcs_gettok(rfp);
1.148 xsa 1560: if (ret != RCS_TOK_STRING)
1561: fatal("token `%s' found where RCS desc expected",
1.117 niallo 1562: RCS_TOKSTR(rfp));
1563:
1564: rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
1565: rfp->rf_flags |= PARSED_DESC;
1566: }
1567:
1.1 jfb 1568: /*
1.117 niallo 1569: * rcs_parse_init()
1.1 jfb 1570: *
1.117 niallo 1571: * Initial parsing of file <path>, which are in the RCS format.
1.167 ray 1572: * Just does admin section.
1.1 jfb 1573: */
1.167 ray 1574: static void
1.117 niallo 1575: rcs_parse_init(RCSFILE *rfp)
1.1 jfb 1576: {
1577: struct rcs_pdata *pdp;
1578:
1.26 jfb 1579: if (rfp->rf_flags & RCS_PARSED)
1.167 ray 1580: return;
1.1 jfb 1581:
1.161 ray 1582: pdp = xcalloc(1, sizeof(*pdp));
1.1 jfb 1583:
1.18 jfb 1584: pdp->rp_lines = 0;
1.1 jfb 1585: pdp->rp_pttype = RCS_TOK_ERR;
1586:
1.172 joris 1587: if ((pdp->rp_file = fdopen(rfp->fd, "r")) == NULL)
1.216 xsa 1588: fatal("fdopen: `%s'", rfp->rf_path);
1.1 jfb 1589:
1.161 ray 1590: pdp->rp_buf = xmalloc((size_t)RCS_BUFSIZE);
1.1 jfb 1591: pdp->rp_blen = RCS_BUFSIZE;
1.18 jfb 1592: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1 jfb 1593:
1594: /* ditch the strict lock */
1.26 jfb 1595: rfp->rf_flags &= ~RCS_SLOCK;
1.1 jfb 1596: rfp->rf_pdata = pdp;
1597:
1.167 ray 1598: if (rcs_parse_admin(rfp) < 0) {
1.1 jfb 1599: rcs_freepdata(pdp);
1.117 niallo 1600: fatal("could not parse admin data");
1.1 jfb 1601: }
1602:
1.117 niallo 1603: if (rfp->rf_flags & RCS_PARSE_FULLY)
1604: rcs_parse_deltatexts(rfp, NULL);
1.1 jfb 1605:
1.117 niallo 1606: rfp->rf_flags |= RCS_SYNCED;
1.1 jfb 1607: }
1608:
1609: /*
1610: * rcs_parse_admin()
1611: *
1612: * Parse the administrative portion of an RCS file.
1.31 jfb 1613: * Returns the type of the first token found after the admin section on
1614: * success, or -1 on failure.
1.1 jfb 1615: */
1616: static int
1617: rcs_parse_admin(RCSFILE *rfp)
1618: {
1619: u_int i;
1620: int tok, ntok, hmask;
1621: struct rcs_key *rk;
1622:
1.180 joris 1623: rfp->rf_head = NULL;
1624: rfp->rf_branch = NULL;
1625:
1.1 jfb 1626: /* hmask is a mask of the headers already encountered */
1627: hmask = 0;
1628: for (;;) {
1629: tok = rcs_gettok(rfp);
1630: if (tok == RCS_TOK_ERR) {
1631: cvs_log(LP_ERR, "parse error in RCS admin section");
1.147 ray 1632: goto fail;
1.168 deraadt 1633: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.31 jfb 1634: /*
1635: * Assume this is the start of the first delta or
1636: * that we are dealing with an empty RCS file and
1637: * we just found the description.
1638: */
1.1 jfb 1639: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31 jfb 1640: return (tok);
1.1 jfb 1641: }
1642:
1643: rk = NULL;
1.18 jfb 1644: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1645: if (rcs_keys[i].rk_id == tok)
1646: rk = &(rcs_keys[i]);
1647:
1648: if (hmask & (1 << tok)) {
1649: cvs_log(LP_ERR, "duplicate RCS key");
1.147 ray 1650: goto fail;
1.1 jfb 1651: }
1652: hmask |= (1 << tok);
1653:
1654: switch (tok) {
1655: case RCS_TOK_HEAD:
1656: case RCS_TOK_BRANCH:
1657: case RCS_TOK_COMMENT:
1658: case RCS_TOK_EXPAND:
1659: ntok = rcs_gettok(rfp);
1660: if (ntok == RCS_TOK_SCOLON)
1661: break;
1662: if (ntok != rk->rk_val) {
1663: cvs_log(LP_ERR,
1664: "invalid value type for RCS key `%s'",
1665: rk->rk_str);
1666: }
1667:
1668: if (tok == RCS_TOK_HEAD) {
1.111 joris 1669: if (rfp->rf_head == NULL)
1.28 jfb 1670: rfp->rf_head = rcsnum_alloc();
1.1 jfb 1671: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1672: rfp->rf_head);
1.14 deraadt 1673: } else if (tok == RCS_TOK_BRANCH) {
1.111 joris 1674: if (rfp->rf_branch == NULL)
1.35 jfb 1675: rfp->rf_branch = rcsnum_alloc();
1676: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1677: rfp->rf_branch) < 0)
1.147 ray 1678: goto fail;
1.14 deraadt 1679: } else if (tok == RCS_TOK_COMMENT) {
1.110 joris 1680: rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
1.14 deraadt 1681: } else if (tok == RCS_TOK_EXPAND) {
1.110 joris 1682: rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 1683: }
1684:
1685: /* now get the expected semi-colon */
1686: ntok = rcs_gettok(rfp);
1687: if (ntok != RCS_TOK_SCOLON) {
1688: cvs_log(LP_ERR,
1689: "missing semi-colon after RCS `%s' key",
1.26 jfb 1690: rk->rk_str);
1.147 ray 1691: goto fail;
1.1 jfb 1692: }
1693: break;
1694: case RCS_TOK_ACCESS:
1.29 jfb 1695: if (rcs_parse_access(rfp) < 0)
1.147 ray 1696: goto fail;
1.1 jfb 1697: break;
1698: case RCS_TOK_SYMBOLS:
1.29 jfb 1699: if (rcs_parse_symbols(rfp) < 0)
1.147 ray 1700: goto fail;
1.1 jfb 1701: break;
1702: case RCS_TOK_LOCKS:
1.29 jfb 1703: if (rcs_parse_locks(rfp) < 0)
1.147 ray 1704: goto fail;
1.1 jfb 1705: break;
1706: default:
1707: cvs_log(LP_ERR,
1708: "unexpected token `%s' in RCS admin section",
1709: RCS_TOKSTR(rfp));
1.147 ray 1710: goto fail;
1.1 jfb 1711: }
1712: }
1713:
1.147 ray 1714: fail:
1715: return (-1);
1.1 jfb 1716: }
1717:
1718: /*
1719: * rcs_parse_delta()
1720: *
1721: * Parse an RCS delta section and allocate the structure to store that delta's
1722: * information in the <rfp> delta list.
1723: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1724: * -1 on error.
1725: */
1726: static int
1727: rcs_parse_delta(RCSFILE *rfp)
1728: {
1729: int ret, tok, ntok, hmask;
1730: u_int i;
1731: char *tokstr;
1.3 vincent 1732: RCSNUM *datenum;
1.1 jfb 1733: struct rcs_delta *rdp;
1734: struct rcs_key *rk;
1735:
1736: tok = rcs_gettok(rfp);
1.154 ray 1737: if (tok == RCS_TOK_DESC) {
1738: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1739: return (0);
1740: } else if (tok != RCS_TOK_NUM) {
1.1 jfb 1741: cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
1742: RCS_TOKSTR(rfp));
1743: return (-1);
1744: }
1.192 niallo 1745:
1746: rdp = xcalloc(1, sizeof(*rdp));
1747:
1748: rdp->rd_num = rcsnum_alloc();
1749: rdp->rd_next = rcsnum_alloc();
1750:
1751: TAILQ_INIT(&(rdp->rd_branches));
1752:
1.1 jfb 1753: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1754:
1755: hmask = 0;
1756: ret = 0;
1757: tokstr = NULL;
1758:
1759: for (;;) {
1760: tok = rcs_gettok(rfp);
1761: if (tok == RCS_TOK_ERR) {
1762: cvs_log(LP_ERR, "parse error in RCS delta section");
1763: rcs_freedelta(rdp);
1764: return (-1);
1.14 deraadt 1765: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15 tedu 1766: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1 jfb 1767: ret = (tok == RCS_TOK_NUM ? 1 : 0);
1768: break;
1769: }
1770:
1771: rk = NULL;
1.18 jfb 1772: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1773: if (rcs_keys[i].rk_id == tok)
1774: rk = &(rcs_keys[i]);
1775:
1776: if (hmask & (1 << tok)) {
1777: cvs_log(LP_ERR, "duplicate RCS key");
1778: rcs_freedelta(rdp);
1779: return (-1);
1780: }
1781: hmask |= (1 << tok);
1782:
1783: switch (tok) {
1784: case RCS_TOK_DATE:
1785: case RCS_TOK_AUTHOR:
1786: case RCS_TOK_STATE:
1787: case RCS_TOK_NEXT:
1788: ntok = rcs_gettok(rfp);
1789: if (ntok == RCS_TOK_SCOLON) {
1790: if (rk->rk_flags & RCS_VOPT)
1791: break;
1792: else {
1793: cvs_log(LP_ERR, "missing mandatory "
1794: "value to RCS key `%s'",
1795: rk->rk_str);
1796: rcs_freedelta(rdp);
1797: return (-1);
1798: }
1799: }
1800:
1801: if (ntok != rk->rk_val) {
1802: cvs_log(LP_ERR,
1803: "invalid value type for RCS key `%s'",
1804: rk->rk_str);
1805: rcs_freedelta(rdp);
1806: return (-1);
1807: }
1808:
1809: if (tokstr != NULL)
1.110 joris 1810: xfree(tokstr);
1811: tokstr = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 1812: /* now get the expected semi-colon */
1813: ntok = rcs_gettok(rfp);
1814: if (ntok != RCS_TOK_SCOLON) {
1815: cvs_log(LP_ERR,
1816: "missing semi-colon after RCS `%s' key",
1.26 jfb 1817: rk->rk_str);
1.110 joris 1818: xfree(tokstr);
1.1 jfb 1819: rcs_freedelta(rdp);
1820: return (-1);
1821: }
1822:
1823: if (tok == RCS_TOK_DATE) {
1.25 jfb 1824: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.110 joris 1825: xfree(tokstr);
1.11 joris 1826: rcs_freedelta(rdp);
1827: return (-1);
1828: }
1.3 vincent 1829: if (datenum->rn_len != 6) {
1.1 jfb 1830: cvs_log(LP_ERR,
1831: "RCS date specification has %s "
1832: "fields",
1.3 vincent 1833: (datenum->rn_len > 6) ? "too many" :
1.1 jfb 1834: "missing");
1.110 joris 1835: xfree(tokstr);
1.1 jfb 1836: rcs_freedelta(rdp);
1.37 tedu 1837: rcsnum_free(datenum);
1838: return (-1);
1.1 jfb 1839: }
1.3 vincent 1840: rdp->rd_date.tm_year = datenum->rn_id[0];
1.19 jfb 1841: if (rdp->rd_date.tm_year >= 1900)
1842: rdp->rd_date.tm_year -= 1900;
1.3 vincent 1843: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
1844: rdp->rd_date.tm_mday = datenum->rn_id[2];
1845: rdp->rd_date.tm_hour = datenum->rn_id[3];
1846: rdp->rd_date.tm_min = datenum->rn_id[4];
1847: rdp->rd_date.tm_sec = datenum->rn_id[5];
1848: rcsnum_free(datenum);
1.14 deraadt 1849: } else if (tok == RCS_TOK_AUTHOR) {
1.1 jfb 1850: rdp->rd_author = tokstr;
1851: tokstr = NULL;
1.14 deraadt 1852: } else if (tok == RCS_TOK_STATE) {
1.1 jfb 1853: rdp->rd_state = tokstr;
1854: tokstr = NULL;
1.14 deraadt 1855: } else if (tok == RCS_TOK_NEXT) {
1.1 jfb 1856: rcsnum_aton(tokstr, NULL, rdp->rd_next);
1857: }
1858: break;
1859: case RCS_TOK_BRANCHES:
1.46 jfb 1860: if (rcs_parse_branches(rfp, rdp) < 0) {
1861: rcs_freedelta(rdp);
1862: return (-1);
1863: }
1.1 jfb 1864: break;
1865: default:
1.172 joris 1866: cvs_log(LP_ERR, "unexpected token `%s' in RCS delta",
1.1 jfb 1867: RCS_TOKSTR(rfp));
1868: rcs_freedelta(rdp);
1869: return (-1);
1870: }
1871: }
1872:
1.13 jfb 1873: if (tokstr != NULL)
1.110 joris 1874: xfree(tokstr);
1.13 jfb 1875:
1.1 jfb 1876: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26 jfb 1877: rfp->rf_ndelta++;
1.1 jfb 1878:
1879: return (ret);
1880: }
1881:
1882: /*
1883: * rcs_parse_deltatext()
1884: *
1885: * Parse an RCS delta text section and fill in the log and text field of the
1886: * appropriate delta section.
1887: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1888: * -1 on error.
1889: */
1890: static int
1891: rcs_parse_deltatext(RCSFILE *rfp)
1892: {
1893: int tok;
1894: RCSNUM *tnum;
1895: struct rcs_delta *rdp;
1896:
1897: tok = rcs_gettok(rfp);
1898: if (tok == RCS_TOK_EOF)
1899: return (0);
1900:
1901: if (tok != RCS_TOK_NUM) {
1902: cvs_log(LP_ERR,
1903: "unexpected token `%s' at start of RCS delta text",
1904: RCS_TOKSTR(rfp));
1905: return (-1);
1906: }
1.13 jfb 1907:
1908: tnum = rcsnum_alloc();
1.1 jfb 1909: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
1910:
1911: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1912: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
1913: break;
1914: }
1.13 jfb 1915: rcsnum_free(tnum);
1916:
1.1 jfb 1917: if (rdp == NULL) {
1918: cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
1919: RCS_TOKSTR(rfp));
1920: return (-1);
1921: }
1922:
1923: tok = rcs_gettok(rfp);
1924: if (tok != RCS_TOK_LOG) {
1925: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1926: RCS_TOKSTR(rfp));
1927: return (-1);
1928: }
1929:
1930: tok = rcs_gettok(rfp);
1931: if (tok != RCS_TOK_STRING) {
1932: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1933: RCS_TOKSTR(rfp));
1934: return (-1);
1935: }
1.110 joris 1936: rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 1937: tok = rcs_gettok(rfp);
1938: if (tok != RCS_TOK_TEXT) {
1939: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
1940: RCS_TOKSTR(rfp));
1941: return (-1);
1942: }
1943:
1944: tok = rcs_gettok(rfp);
1945: if (tok != RCS_TOK_STRING) {
1946: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
1947: RCS_TOKSTR(rfp));
1948: return (-1);
1949: }
1950:
1.193 niallo 1951: if (RCS_TOKLEN(rfp) == 0) {
1952: rdp->rd_text = xmalloc(1);
1953: rdp->rd_text[0] = '\0';
1954: rdp->rd_tlen = 0;
1955: } else {
1956: rdp->rd_text = xmalloc(RCS_TOKLEN(rfp));
1957: memcpy(rdp->rd_text, RCS_TOKSTR(rfp), RCS_TOKLEN(rfp));
1958: rdp->rd_tlen = RCS_TOKLEN(rfp);
1959: }
1.1 jfb 1960:
1961: return (1);
1962: }
1963:
1964: /*
1965: * rcs_parse_access()
1966: *
1967: * Parse the access list given as value to the `access' keyword.
1968: * Returns 0 on success, or -1 on failure.
1969: */
1970: static int
1971: rcs_parse_access(RCSFILE *rfp)
1972: {
1973: int type;
1974:
1975: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
1976: if (type != RCS_TOK_ID) {
1977: cvs_log(LP_ERR, "unexpected token `%s' in access list",
1978: RCS_TOKSTR(rfp));
1979: return (-1);
1980: }
1.29 jfb 1981:
1982: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
1983: return (-1);
1.1 jfb 1984: }
1985:
1986: return (0);
1987: }
1988:
1989: /*
1990: * rcs_parse_symbols()
1991: *
1992: * Parse the symbol list given as value to the `symbols' keyword.
1993: * Returns 0 on success, or -1 on failure.
1994: */
1995: static int
1996: rcs_parse_symbols(RCSFILE *rfp)
1997: {
1998: int type;
1999: struct rcs_sym *symp;
2000:
2001: for (;;) {
2002: type = rcs_gettok(rfp);
2003: if (type == RCS_TOK_SCOLON)
2004: break;
2005:
1.41 jfb 2006: if (type != RCS_TOK_ID) {
1.1 jfb 2007: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2008: RCS_TOKSTR(rfp));
2009: return (-1);
2010: }
2011:
1.161 ray 2012: symp = xmalloc(sizeof(*symp));
1.110 joris 2013: symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 2014: symp->rs_num = rcsnum_alloc();
2015:
2016: type = rcs_gettok(rfp);
2017: if (type != RCS_TOK_COLON) {
2018: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2019: RCS_TOKSTR(rfp));
1.11 joris 2020: rcsnum_free(symp->rs_num);
1.110 joris 2021: xfree(symp->rs_name);
2022: xfree(symp);
1.1 jfb 2023: return (-1);
2024: }
2025:
2026: type = rcs_gettok(rfp);
2027: if (type != RCS_TOK_NUM) {
2028: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2029: RCS_TOKSTR(rfp));
1.11 joris 2030: rcsnum_free(symp->rs_num);
1.110 joris 2031: xfree(symp->rs_name);
2032: xfree(symp);
1.1 jfb 2033: return (-1);
2034: }
2035:
2036: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
2037: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2038: RCS_TOKSTR(rfp));
1.11 joris 2039: rcsnum_free(symp->rs_num);
1.110 joris 2040: xfree(symp->rs_name);
2041: xfree(symp);
1.1 jfb 2042: return (-1);
2043: }
2044:
1.43 jfb 2045: TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1 jfb 2046: }
2047:
2048: return (0);
2049: }
2050:
2051: /*
2052: * rcs_parse_locks()
2053: *
2054: * Parse the lock list given as value to the `locks' keyword.
2055: * Returns 0 on success, or -1 on failure.
2056: */
2057: static int
2058: rcs_parse_locks(RCSFILE *rfp)
2059: {
2060: int type;
2061: struct rcs_lock *lkp;
2062:
2063: for (;;) {
2064: type = rcs_gettok(rfp);
2065: if (type == RCS_TOK_SCOLON)
2066: break;
2067:
2068: if (type != RCS_TOK_ID) {
2069: cvs_log(LP_ERR, "unexpected token `%s' in lock list",
2070: RCS_TOKSTR(rfp));
2071: return (-1);
2072: }
2073:
1.161 ray 2074: lkp = xmalloc(sizeof(*lkp));
1.110 joris 2075: lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 2076: lkp->rl_num = rcsnum_alloc();
2077:
2078: type = rcs_gettok(rfp);
2079: if (type != RCS_TOK_COLON) {
2080: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2081: RCS_TOKSTR(rfp));
1.37 tedu 2082: rcsnum_free(lkp->rl_num);
1.110 joris 2083: xfree(lkp->rl_name);
2084: xfree(lkp);
1.1 jfb 2085: return (-1);
2086: }
2087:
2088: type = rcs_gettok(rfp);
2089: if (type != RCS_TOK_NUM) {
2090: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2091: RCS_TOKSTR(rfp));
1.37 tedu 2092: rcsnum_free(lkp->rl_num);
1.110 joris 2093: xfree(lkp->rl_name);
2094: xfree(lkp);
1.1 jfb 2095: return (-1);
2096: }
2097:
2098: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
2099: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2100: RCS_TOKSTR(rfp));
1.37 tedu 2101: rcsnum_free(lkp->rl_num);
1.110 joris 2102: xfree(lkp->rl_name);
2103: xfree(lkp);
1.1 jfb 2104: return (-1);
2105: }
2106:
2107: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
2108: }
2109:
2110: /* check if we have a `strict' */
2111: type = rcs_gettok(rfp);
2112: if (type != RCS_TOK_STRICT) {
2113: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14 deraadt 2114: } else {
1.26 jfb 2115: rfp->rf_flags |= RCS_SLOCK;
1.1 jfb 2116:
2117: type = rcs_gettok(rfp);
2118: if (type != RCS_TOK_SCOLON) {
2119: cvs_log(LP_ERR,
2120: "missing semi-colon after `strict' keyword");
2121: return (-1);
2122: }
2123: }
2124:
2125: return (0);
2126: }
2127:
2128: /*
2129: * rcs_parse_branches()
2130: *
2131: * Parse the list of branches following a `branches' keyword in a delta.
2132: * Returns 0 on success, or -1 on failure.
2133: */
2134: static int
2135: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
2136: {
2137: int type;
2138: struct rcs_branch *brp;
2139:
2140: for (;;) {
2141: type = rcs_gettok(rfp);
2142: if (type == RCS_TOK_SCOLON)
2143: break;
2144:
2145: if (type != RCS_TOK_NUM) {
2146: cvs_log(LP_ERR,
2147: "unexpected token `%s' in list of branches",
2148: RCS_TOKSTR(rfp));
2149: return (-1);
2150: }
2151:
1.161 ray 2152: brp = xmalloc(sizeof(*brp));
1.46 jfb 2153: brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11 joris 2154: if (brp->rb_num == NULL) {
1.110 joris 2155: xfree(brp);
1.11 joris 2156: return (-1);
2157: }
1.1 jfb 2158:
2159: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
2160: }
2161:
2162: return (0);
2163: }
2164:
2165: /*
2166: * rcs_freedelta()
2167: *
2168: * Free the contents of a delta structure.
2169: */
1.18 jfb 2170: static void
1.1 jfb 2171: rcs_freedelta(struct rcs_delta *rdp)
2172: {
1.12 jfb 2173: struct rcs_branch *rb;
1.1 jfb 2174:
1.12 jfb 2175: if (rdp->rd_num != NULL)
2176: rcsnum_free(rdp->rd_num);
2177: if (rdp->rd_next != NULL)
2178: rcsnum_free(rdp->rd_next);
2179:
1.1 jfb 2180: if (rdp->rd_author != NULL)
1.110 joris 2181: xfree(rdp->rd_author);
1.109 joris 2182: if (rdp->rd_locker != NULL)
1.110 joris 2183: xfree(rdp->rd_locker);
1.1 jfb 2184: if (rdp->rd_state != NULL)
1.110 joris 2185: xfree(rdp->rd_state);
1.1 jfb 2186: if (rdp->rd_log != NULL)
1.110 joris 2187: xfree(rdp->rd_log);
1.1 jfb 2188: if (rdp->rd_text != NULL)
1.110 joris 2189: xfree(rdp->rd_text);
1.12 jfb 2190:
2191: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
2192: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
2193: rcsnum_free(rb->rb_num);
1.110 joris 2194: xfree(rb);
1.1 jfb 2195: }
2196:
1.110 joris 2197: xfree(rdp);
1.1 jfb 2198: }
2199:
2200: /*
2201: * rcs_freepdata()
2202: *
2203: * Free the contents of the parser data structure.
2204: */
2205: static void
2206: rcs_freepdata(struct rcs_pdata *pd)
2207: {
2208: if (pd->rp_file != NULL)
2209: (void)fclose(pd->rp_file);
2210: if (pd->rp_buf != NULL)
1.110 joris 2211: xfree(pd->rp_buf);
2212: xfree(pd);
1.1 jfb 2213: }
2214:
2215: /*
2216: * rcs_gettok()
2217: *
2218: * Get the next RCS token from the string <str>.
2219: */
2220: static int
2221: rcs_gettok(RCSFILE *rfp)
2222: {
2223: u_int i;
2224: int ch, last, type;
1.18 jfb 2225: size_t len;
2226: char *bp;
1.1 jfb 2227: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2228:
2229: type = RCS_TOK_ERR;
2230: bp = pdp->rp_buf;
1.42 jfb 2231: pdp->rp_tlen = 0;
1.1 jfb 2232: *bp = '\0';
2233:
2234: if (pdp->rp_pttype != RCS_TOK_ERR) {
2235: type = pdp->rp_pttype;
1.172 joris 2236: if (strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen) >=
2237: pdp->rp_blen)
2238: fatal("rcs_gettok: strlcpy");
1.1 jfb 2239: pdp->rp_pttype = RCS_TOK_ERR;
2240: return (type);
2241: }
2242:
2243: /* skip leading whitespace */
2244: /* XXX we must skip backspace too for compatibility, should we? */
2245: do {
2246: ch = getc(pdp->rp_file);
2247: if (ch == '\n')
1.18 jfb 2248: pdp->rp_lines++;
1.1 jfb 2249: } while (isspace(ch));
2250:
2251: if (ch == EOF) {
2252: type = RCS_TOK_EOF;
1.14 deraadt 2253: } else if (ch == ';') {
1.1 jfb 2254: type = RCS_TOK_SCOLON;
1.14 deraadt 2255: } else if (ch == ':') {
1.1 jfb 2256: type = RCS_TOK_COLON;
1.14 deraadt 2257: } else if (isalpha(ch)) {
1.31 jfb 2258: type = RCS_TOK_ID;
1.1 jfb 2259: *(bp++) = ch;
1.18 jfb 2260: for (;;) {
1.1 jfb 2261: ch = getc(pdp->rp_file);
1.188 joris 2262: if (ch == EOF) {
2263: type = RCS_TOK_EOF;
2264: break;
2265: } else if (!isalnum(ch) && ch != '_' && ch != '-' &&
1.201 joris 2266: ch != '/' && ch != '+') {
1.1 jfb 2267: ungetc(ch, pdp->rp_file);
2268: break;
2269: }
2270: *(bp++) = ch;
1.42 jfb 2271: pdp->rp_tlen++;
1.18 jfb 2272: if (bp == pdp->rp_bufend - 1) {
2273: len = bp - pdp->rp_buf;
1.151 xsa 2274: rcs_growbuf(rfp);
1.18 jfb 2275: bp = pdp->rp_buf + len;
2276: }
1.1 jfb 2277: }
2278: *bp = '\0';
2279:
1.18 jfb 2280: if (type != RCS_TOK_ERR) {
2281: for (i = 0; i < RCS_NKEYS; i++) {
2282: if (strcmp(rcs_keys[i].rk_str,
2283: pdp->rp_buf) == 0) {
2284: type = rcs_keys[i].rk_id;
2285: break;
2286: }
1.1 jfb 2287: }
2288: }
1.14 deraadt 2289: } else if (ch == '@') {
1.1 jfb 2290: /* we have a string */
1.18 jfb 2291: type = RCS_TOK_STRING;
1.1 jfb 2292: for (;;) {
2293: ch = getc(pdp->rp_file);
1.188 joris 2294: if (ch == EOF) {
2295: type = RCS_TOK_EOF;
2296: break;
2297: } else if (ch == '@') {
1.1 jfb 2298: ch = getc(pdp->rp_file);
2299: if (ch != '@') {
2300: ungetc(ch, pdp->rp_file);
2301: break;
2302: }
1.14 deraadt 2303: } else if (ch == '\n')
1.18 jfb 2304: pdp->rp_lines++;
1.1 jfb 2305:
2306: *(bp++) = ch;
1.42 jfb 2307: pdp->rp_tlen++;
1.18 jfb 2308: if (bp == pdp->rp_bufend - 1) {
2309: len = bp - pdp->rp_buf;
1.151 xsa 2310: rcs_growbuf(rfp);
1.18 jfb 2311: bp = pdp->rp_buf + len;
2312: }
1.1 jfb 2313: }
2314:
2315: *bp = '\0';
1.14 deraadt 2316: } else if (isdigit(ch)) {
1.1 jfb 2317: *(bp++) = ch;
2318: last = ch;
2319: type = RCS_TOK_NUM;
2320:
2321: for (;;) {
2322: ch = getc(pdp->rp_file);
1.188 joris 2323: if (ch == EOF) {
2324: type = RCS_TOK_EOF;
2325: break;
2326: }
1.18 jfb 2327: if (bp == pdp->rp_bufend)
1.1 jfb 2328: break;
2329: if (!isdigit(ch) && ch != '.') {
2330: ungetc(ch, pdp->rp_file);
2331: break;
2332: }
2333:
2334: if (last == '.' && ch == '.') {
2335: type = RCS_TOK_ERR;
2336: break;
2337: }
2338: last = ch;
2339: *(bp++) = ch;
1.42 jfb 2340: pdp->rp_tlen++;
1.1 jfb 2341: }
1.18 jfb 2342: *bp = '\0';
1.1 jfb 2343: }
2344:
2345: return (type);
2346: }
2347:
2348: /*
2349: * rcs_pushtok()
2350: *
2351: * Push a token back in the parser's token buffer.
2352: */
2353: static int
2354: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2355: {
2356: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2357:
2358: if (pdp->rp_pttype != RCS_TOK_ERR)
2359: return (-1);
2360:
2361: pdp->rp_pttype = type;
1.172 joris 2362: if (strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok)) >=
2363: sizeof(pdp->rp_ptok))
2364: fatal("rcs_pushtok: strlcpy");
1.1 jfb 2365: return (0);
2366: }
2367:
1.18 jfb 2368:
2369: /*
2370: * rcs_growbuf()
2371: *
2372: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2373: * RCS_BUFEXTSIZE.
2374: * In case of failure, the original buffer is left unmodified.
2375: */
1.151 xsa 2376: static void
1.18 jfb 2377: rcs_growbuf(RCSFILE *rf)
2378: {
2379: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2380:
1.212 ray 2381: pdp->rp_buf = xrealloc(pdp->rp_buf, 1,
2382: pdp->rp_blen + RCS_BUFEXTSIZE);
1.18 jfb 2383: pdp->rp_blen += RCS_BUFEXTSIZE;
2384: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42 jfb 2385: }
2386:
2387: /*
2388: * rcs_strprint()
2389: *
2390: * Output an RCS string <str> of size <slen> to the stream <stream>. Any
2391: * '@' characters are escaped. Otherwise, the string can contain arbitrary
2392: * binary data.
2393: */
1.155 ray 2394: static void
1.42 jfb 2395: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
2396: {
2397: const u_char *ap, *ep, *sp;
1.52 jfb 2398:
2399: if (slen == 0)
1.155 ray 2400: return;
1.42 jfb 2401:
2402: ep = str + slen - 1;
2403:
2404: for (sp = str; sp <= ep;) {
2405: ap = memchr(sp, '@', ep - sp);
2406: if (ap == NULL)
2407: ap = ep;
1.155 ray 2408: (void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
1.42 jfb 2409:
2410: if (*ap == '@')
2411: putc('@', stream);
2412: sp = ap + 1;
1.63 joris 2413: }
2414: }
2415:
2416: /*
1.81 niallo 2417: * rcs_deltatext_set()
2418: *
2419: * Set deltatext for <rev> in RCS file <rfp> to <dtext>
1.96 xsa 2420: * Returns -1 on error, 0 on success.
1.81 niallo 2421: */
2422: int
1.194 joris 2423: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, BUF *bp)
1.81 niallo 2424: {
2425: size_t len;
1.194 joris 2426: u_char *dtext;
1.81 niallo 2427: struct rcs_delta *rdp;
1.117 niallo 2428:
2429: /* Write operations require full parsing */
2430: rcs_parse_deltatexts(rfp, NULL);
1.81 niallo 2431:
2432: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2433: return (-1);
2434:
2435: if (rdp->rd_text != NULL)
1.110 joris 2436: xfree(rdp->rd_text);
1.81 niallo 2437:
1.194 joris 2438: len = cvs_buf_len(bp);
2439: dtext = cvs_buf_release(bp);
2440: bp = NULL;
2441:
1.103 joris 2442: if (len != 0) {
1.194 joris 2443: rdp->rd_text = xmalloc(len);
1.155 ray 2444: rdp->rd_tlen = len;
1.194 joris 2445: (void)memcpy(rdp->rd_text, dtext, len);
1.103 joris 2446: } else {
2447: rdp->rd_text = NULL;
2448: rdp->rd_tlen = 0;
2449: }
1.194 joris 2450:
2451: if (dtext != NULL)
2452: xfree(dtext);
1.18 jfb 2453:
1.86 joris 2454: return (0);
2455: }
2456:
2457: /*
2458: * rcs_rev_setlog()
2459: *
2460: * Sets the log message of revision <rev> to <logtext>
2461: */
2462: int
2463: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
2464: {
2465: struct rcs_delta *rdp;
1.214 xsa 2466: char buf[CVS_REV_BUFSZ];
1.86 joris 2467:
2468: rcsnum_tostr(rev, buf, sizeof(buf));
2469:
2470: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2471: return (-1);
2472:
2473: if (rdp->rd_log != NULL)
1.110 joris 2474: xfree(rdp->rd_log);
1.86 joris 2475:
1.110 joris 2476: rdp->rd_log = xstrdup(logtext);
1.86 joris 2477: rfp->rf_flags &= ~RCS_SYNCED;
1.95 niallo 2478: return (0);
2479: }
1.97 niallo 2480: /*
2481: * rcs_rev_getdate()
2482: *
2483: * Get the date corresponding to a given revision.
2484: * Returns the date on success, -1 on failure.
2485: */
2486: time_t
2487: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
2488: {
2489: struct rcs_delta *rdp;
2490:
2491: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2492: return (-1);
2493:
1.204 joris 2494: return (timegm(&rdp->rd_date));
1.97 niallo 2495: }
1.95 niallo 2496:
2497: /*
2498: * rcs_state_set()
2499: *
2500: * Sets the state of revision <rev> to <state>
2501: * NOTE: default state is 'Exp'. States may not contain spaces.
2502: *
2503: * Returns -1 on failure, 0 on success.
2504: */
2505: int
2506: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
2507: {
2508: struct rcs_delta *rdp;
2509:
2510: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2511: return (-1);
2512:
2513: if (rdp->rd_state != NULL)
1.110 joris 2514: xfree(rdp->rd_state);
1.95 niallo 2515:
1.110 joris 2516: rdp->rd_state = xstrdup(state);
1.95 niallo 2517:
2518: rfp->rf_flags &= ~RCS_SYNCED;
2519:
2520: return (0);
2521: }
2522:
2523: /*
2524: * rcs_state_check()
2525: *
2526: * Check if string <state> is valid.
2527: *
1.96 xsa 2528: * Returns 0 if the string is valid, -1 otherwise.
1.95 niallo 2529: */
2530: int
2531: rcs_state_check(const char *state)
2532: {
2533: if (strchr(state, ' ') != NULL)
2534: return (-1);
2535:
1.18 jfb 2536: return (0);
1.1 jfb 2537: }
1.97 niallo 2538:
2539: /*
2540: * rcs_state_get()
2541: *
2542: * Get the state for a given revision of a specified RCSFILE.
2543: *
2544: * Returns NULL on failure.
2545: */
2546: const char *
2547: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
2548: {
2549: struct rcs_delta *rdp;
2550:
2551: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2552: return (NULL);
2553:
2554: return (rdp->rd_state);
2555: }
2556:
1.246 tobias 2557: /* rcs_get_revision() */
2558: static RCSNUM *
2559: rcs_get_revision(const char *revstr, RCSFILE *rfp)
1.176 joris 2560: {
1.213 niallo 2561: RCSNUM *rev, *brev, *frev;
1.178 joris 2562: struct rcs_branch *brp;
1.183 joris 2563: struct rcs_delta *rdp;
1.213 niallo 2564: size_t i;
1.183 joris 2565:
2566: rdp = NULL;
1.215 joris 2567:
2568: if (!strcmp(revstr, RCS_HEAD_BRANCH)) {
1.261 tobias 2569: if (rfp->rf_head == NULL)
2570: return NULL;
2571:
1.215 joris 2572: frev = rcsnum_alloc();
2573: rcsnum_cpy(rfp->rf_head, frev, 0);
2574: return (frev);
2575: }
1.176 joris 2576:
1.213 niallo 2577: /* Possibly we could be passed a version number */
1.223 tobias 2578: if ((rev = rcsnum_parse(revstr)) != NULL) {
2579: /* Do not return if it is not in RCS file */
1.242 joris 2580: if ((rdp = rcs_findrev(rfp, rev)) != NULL) {
2581: frev = rcsnum_alloc();
2582: rcsnum_cpy(rev, frev, 0);
2583: return (frev);
2584: }
1.223 tobias 2585: } else {
2586: /* More likely we will be passed a symbol */
2587: rev = rcs_sym_getrev(rfp, revstr);
2588: }
1.222 joris 2589:
1.213 niallo 2590: if (rev == NULL)
1.245 tobias 2591: return (NULL);
1.183 joris 2592:
1.220 joris 2593: /*
2594: * If it was not a branch, thats ok the symbolic
2595: * name refered to a revision, so return the resolved
1.224 tobias 2596: * revision for the given name if it is not newer than HEAD.
1.220 joris 2597: */
1.224 tobias 2598: if (!RCSNUM_ISBRANCH(rev)) {
2599: if (rcsnum_cmp(rev, rfp->rf_head, 0) < 0) {
2600: rcsnum_free(rev);
2601: return NULL;
2602: }
1.220 joris 2603: return (rev);
1.224 tobias 2604: }
1.183 joris 2605:
1.213 niallo 2606: brev = rcsnum_alloc();
2607: rcsnum_cpy(rev, brev, rev->rn_len - 1);
1.178 joris 2608:
1.180 joris 2609: if ((rdp = rcs_findrev(rfp, brev)) == NULL)
1.246 tobias 2610: fatal("rcs_get_revision: tag `%s' does not exist", revstr);
1.213 niallo 2611: rcsnum_free(brev);
1.178 joris 2612:
1.213 niallo 2613: TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
2614: for (i = 0; i < rev->rn_len; i++)
2615: if (brp->rb_num->rn_id[i] != rev->rn_id[i])
2616: break;
2617: if (i != rev->rn_len)
2618: continue;
2619: break;
2620: }
1.178 joris 2621:
1.240 tobias 2622: rcsnum_free(rev);
1.213 niallo 2623: frev = rcsnum_alloc();
2624: if (brp == NULL) {
2625: rcsnum_cpy(rdp->rd_num, frev, 0);
2626: return (frev);
2627: } else {
2628: /* Fetch the delta with the correct branch num */
1.180 joris 2629: if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
1.246 tobias 2630: fatal("rcs_get_revision: could not fetch branch "
2631: "delta");
1.213 niallo 2632: rcsnum_cpy(rdp->rd_num, frev, 0);
2633: return (frev);
1.176 joris 2634: }
1.196 niallo 2635: }
2636:
2637: /*
2638: * rcs_rev_getlines()
2639: *
1.238 tobias 2640: * Get the entire contents of revision <frev> from the RCSFILE <rfp> and
1.196 niallo 2641: * return it as a pointer to a struct cvs_lines.
2642: */
2643: struct cvs_lines *
1.218 tobias 2644: rcs_rev_getlines(RCSFILE *rfp, RCSNUM *frev, struct cvs_line ***alines)
1.196 niallo 2645: {
1.209 otto 2646: size_t plen;
1.218 tobias 2647: int annotate, done, i, nextroot;
1.196 niallo 2648: RCSNUM *tnum, *bnum;
2649: struct rcs_branch *brp;
1.218 tobias 2650: struct rcs_delta *hrdp, *prdp, *rdp, *trdp;
1.196 niallo 2651: u_char *patch;
1.218 tobias 2652: struct cvs_line *line, *nline;
1.196 niallo 2653: struct cvs_lines *dlines, *plines;
2654:
2655: if ((hrdp = rcs_findrev(rfp, rfp->rf_head)) == NULL)
1.197 joris 2656: fatal("rcs_rev_getlines: no HEAD revision");
1.196 niallo 2657:
2658: tnum = frev;
2659: rcs_parse_deltatexts(rfp, hrdp->rd_num);
2660:
2661: /* revision on branch, get the branch root */
2662: nextroot = 2;
2663: if (RCSNUM_ISBRANCHREV(tnum)) {
2664: bnum = rcsnum_alloc();
2665: rcsnum_cpy(tnum, bnum, nextroot);
2666: } else {
2667: bnum = tnum;
2668: }
2669:
1.218 tobias 2670: if (alines != NULL) {
2671: /* start with annotate first at requested revision */
2672: annotate = ANNOTATE_LATER;
2673: *alines = NULL;
2674: } else
2675: annotate = ANNOTATE_NEVER;
2676:
1.196 niallo 2677: dlines = cvs_splitlines(hrdp->rd_text, hrdp->rd_tlen);
2678:
2679: done = 0;
2680:
2681: rdp = hrdp;
1.218 tobias 2682: if (!rcsnum_differ(rdp->rd_num, bnum)) {
2683: if (annotate == ANNOTATE_LATER) {
2684: /* found requested revision for annotate */
2685: i = 0;
2686: TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
2687: line->l_lineno_orig = line->l_lineno;
2688: i++;
2689: }
2690:
2691: *alines = xcalloc(i + 1, sizeof(struct cvs_line *));
2692: (*alines)[i] = NULL;
2693: annotate = ANNOTATE_NOW;
2694:
2695: /* annotate down to 1.1 from where we are */
2696: if (bnum == tnum)
2697: bnum = rcsnum_alloc();
2698: bnum = rcsnum_parse("1.1");
2699: if (!rcsnum_differ(rdp->rd_num, bnum)) {
2700: goto next;
2701: }
2702: } else
2703: goto next;
2704: }
1.196 niallo 2705:
1.218 tobias 2706: prdp = hrdp;
1.196 niallo 2707: if ((rdp = rcs_findrev(rfp, hrdp->rd_next)) == NULL)
2708: goto done;
2709:
2710: again:
2711: for (;;) {
2712: if (rdp->rd_next->rn_len != 0) {
2713: trdp = rcs_findrev(rfp, rdp->rd_next);
2714: if (trdp == NULL)
2715: fatal("failed to grab next revision");
2716: }
2717:
2718: if (rdp->rd_tlen == 0) {
2719: rcs_parse_deltatexts(rfp, rdp->rd_num);
2720: if (rdp->rd_tlen == 0) {
2721: if (!rcsnum_differ(rdp->rd_num, bnum))
2722: break;
2723: rdp = trdp;
2724: continue;
2725: }
2726: }
2727:
2728: plen = rdp->rd_tlen;
2729: patch = rdp->rd_text;
2730: plines = cvs_splitlines(patch, plen);
1.218 tobias 2731: if (annotate == ANNOTATE_NOW)
2732: rcs_patch_lines(dlines, plines, *alines, prdp);
2733: else
2734: rcs_patch_lines(dlines, plines, NULL, NULL);
1.196 niallo 2735: cvs_freelines(plines);
2736:
1.218 tobias 2737: if (!rcsnum_differ(rdp->rd_num, bnum)) {
2738: if (annotate != ANNOTATE_LATER)
2739: break;
2740:
2741: /* found requested revision for annotate */
2742: i = 0;
2743: TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
2744: line->l_lineno_orig = line->l_lineno;
2745: i++;
2746: }
2747:
2748: *alines = xcalloc(i + 1, sizeof(struct cvs_line *));
2749: (*alines)[i] = NULL;
2750: annotate = ANNOTATE_NOW;
2751:
2752: /* annotate down to 1.1 from where we are */
2753: if (bnum == tnum)
2754: bnum = rcsnum_alloc();
2755: bnum = rcsnum_parse("1.1");
2756:
2757: if (!rcsnum_differ(rdp->rd_num, bnum))
2758: break;
2759: }
1.196 niallo 2760:
1.218 tobias 2761: prdp = rdp;
1.196 niallo 2762: rdp = trdp;
2763: }
2764:
2765: next:
2766: if (!rcsnum_differ(rdp->rd_num, frev))
2767: done = 1;
2768:
2769: if (RCSNUM_ISBRANCHREV(frev) && done != 1) {
2770: nextroot += 2;
2771: rcsnum_cpy(frev, bnum, nextroot);
2772:
2773: TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
1.213 niallo 2774: for (i = 0; i < nextroot - 1; i++)
2775: if (brp->rb_num->rn_id[i] != bnum->rn_id[i])
1.196 niallo 2776: break;
1.213 niallo 2777: if (i == nextroot - 1)
2778: break;
1.196 niallo 2779: }
2780:
1.218 tobias 2781: if (brp == NULL) {
2782: if (annotate != ANNOTATE_NEVER) {
2783: if (*alines != NULL)
2784: xfree(*alines);
2785: *alines = NULL;
2786: cvs_freelines(dlines);
2787: if (bnum != tnum)
2788: rcsnum_free(bnum);
2789: return (NULL);
2790: }
1.196 niallo 2791: fatal("expected branch not found on branch list");
1.218 tobias 2792: }
1.196 niallo 2793:
2794: if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
1.197 joris 2795: fatal("rcs_rev_getlines: failed to get delta for target rev");
1.196 niallo 2796:
2797: goto again;
2798: }
2799: done:
1.225 tobias 2800: /* put remaining lines into annotate buffer */
1.218 tobias 2801: if (annotate == ANNOTATE_NOW) {
2802: for (line = TAILQ_FIRST(&(dlines->l_lines));
2803: line != NULL; line = nline) {
2804: nline = TAILQ_NEXT(line, l_list);
2805: TAILQ_REMOVE(&(dlines->l_lines), line, l_list);
2806: if (line->l_line == NULL) {
2807: xfree(line);
2808: continue;
2809: }
2810:
2811: line->l_delta = rdp;
2812: (*alines)[line->l_lineno_orig - 1] = line;
2813: }
2814:
2815: cvs_freelines(dlines);
2816: dlines = NULL;
2817: }
2818:
1.196 niallo 2819: if (bnum != tnum)
2820: rcsnum_free(bnum);
2821:
2822: return (dlines);
1.225 tobias 2823: }
2824:
2825: void
2826: rcs_annotate_getlines(RCSFILE *rfp, RCSNUM *frev, struct cvs_line ***alines)
2827: {
2828: size_t plen;
2829: int i, nextroot;
2830: RCSNUM *bnum;
2831: struct rcs_branch *brp;
2832: struct rcs_delta *rdp, *trdp;
2833: u_char *patch;
2834: struct cvs_line *line;
2835: struct cvs_lines *dlines, *plines;
2836:
2837: if (!RCSNUM_ISBRANCHREV(frev))
2838: fatal("rcs_annotate_getlines: branch revision expected");
2839:
2840: /* revision on branch, get the branch root */
2841: nextroot = 2;
2842: bnum = rcsnum_alloc();
2843: rcsnum_cpy(frev, bnum, nextroot);
2844:
2845: /*
2846: * Going from HEAD to 1.1 enables the use of an array, which is
2847: * much faster. Unfortunately this is not possible with branch
2848: * revisions, so copy over our alines (array) into dlines (tailq).
2849: */
2850: dlines = xcalloc(1, sizeof(*dlines));
2851: TAILQ_INIT(&(dlines->l_lines));
2852: line = xcalloc(1, sizeof(*line));
2853: TAILQ_INSERT_TAIL(&(dlines->l_lines), line, l_list);
2854:
2855: for (i = 0; (*alines)[i] != NULL; i++) {
2856: line = (*alines)[i];
2857: line->l_lineno = i + 1;
2858: TAILQ_INSERT_TAIL(&(dlines->l_lines), line, l_list);
2859: }
2860:
2861: rdp = rcs_findrev(rfp, bnum);
2862: if (rdp == NULL)
2863: fatal("failed to grab branch root revision");
2864:
2865: do {
2866: nextroot += 2;
2867: rcsnum_cpy(frev, bnum, nextroot);
2868:
2869: TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
2870: for (i = 0; i < nextroot - 1; i++)
2871: if (brp->rb_num->rn_id[i] != bnum->rn_id[i])
2872: break;
2873: if (i == nextroot - 1)
2874: break;
2875: }
2876:
2877: if (brp == NULL)
2878: fatal("expected branch not found on branch list");
2879:
2880: if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
2881: fatal("failed to get delta for target rev");
2882:
2883: for (;;) {
2884: if (rdp->rd_next->rn_len != 0) {
2885: trdp = rcs_findrev(rfp, rdp->rd_next);
2886: if (trdp == NULL)
2887: fatal("failed to grab next revision");
2888: }
2889:
2890: if (rdp->rd_tlen == 0) {
2891: rcs_parse_deltatexts(rfp, rdp->rd_num);
2892: if (rdp->rd_tlen == 0) {
2893: if (!rcsnum_differ(rdp->rd_num, bnum))
2894: break;
2895: rdp = trdp;
2896: continue;
2897: }
2898: }
2899:
2900: plen = rdp->rd_tlen;
2901: patch = rdp->rd_text;
2902: plines = cvs_splitlines(patch, plen);
2903: rcs_patch_lines(dlines, plines, NULL, rdp);
2904: cvs_freelines(plines);
2905:
2906: if (!rcsnum_differ(rdp->rd_num, bnum))
2907: break;
2908:
2909: rdp = trdp;
2910: }
2911: } while (rcsnum_differ(rdp->rd_num, frev));
2912:
2913: if (bnum != frev)
2914: rcsnum_free(bnum);
2915:
2916: /*
2917: * All lines have been parsed, now they must be copied over
2918: * into alines (array) again.
2919: */
2920: xfree(*alines);
2921:
2922: i = 0;
2923: TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
2924: if (line->l_line != NULL)
2925: i++;
2926: }
1.239 tobias 2927: *alines = xcalloc(i + 1, sizeof(struct cvs_line *));
1.225 tobias 2928: (*alines)[i] = NULL;
2929:
2930: i = 0;
2931: TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
2932: if (line->l_line != NULL)
2933: (*alines)[i++] = line;
2934: }
1.196 niallo 2935: }
2936:
2937: /*
2938: * rcs_rev_getbuf()
2939: *
2940: * XXX: This is really really slow and should be avoided if at all possible!
2941: *
2942: * Get the entire contents of revision <rev> from the RCSFILE <rfp> and
2943: * return it as a BUF pointer.
2944: */
2945: BUF *
1.200 joris 2946: rcs_rev_getbuf(RCSFILE *rfp, RCSNUM *rev, int mode)
1.196 niallo 2947: {
1.200 joris 2948: int expmode, expand;
2949: struct rcs_delta *rdp;
1.196 niallo 2950: struct cvs_lines *lines;
1.233 tobias 2951: struct cvs_line *lp, *nlp;
1.196 niallo 2952: BUF *bp;
2953:
1.200 joris 2954: expand = 0;
1.218 tobias 2955: lines = rcs_rev_getlines(rfp, rev, NULL);
1.249 tobias 2956: bp = cvs_buf_alloc(1024);
1.200 joris 2957:
2958: if (!(mode & RCS_KWEXP_NONE)) {
2959: if (rfp->rf_expand != NULL)
2960: expmode = rcs_kwexp_get(rfp);
2961: else
2962: expmode = RCS_KWEXP_DEFAULT;
2963:
2964: if (!(expmode & RCS_KWEXP_NONE)) {
2965: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2966: fatal("could not fetch revision");
2967: expand = 1;
2968: }
2969: }
2970:
1.255 deraadt 2971: for (lp = TAILQ_FIRST(&lines->l_lines); lp != NULL;) {
1.233 tobias 2972: nlp = TAILQ_NEXT(lp, l_list);
2973:
2974: if (lp->l_line == NULL) {
2975: lp = nlp;
1.196 niallo 2976: continue;
1.233 tobias 2977: }
1.200 joris 2978:
2979: if (expand)
1.233 tobias 2980: rcs_kwexp_line(rfp->rf_path, rdp, lines, lp, expmode);
1.200 joris 2981:
1.233 tobias 2982: do {
2983: cvs_buf_append(bp, lp->l_line, lp->l_len);
2984: } while ((lp = TAILQ_NEXT(lp, l_list)) != nlp);
1.196 niallo 2985: }
2986:
2987: cvs_freelines(lines);
2988:
2989: return (bp);
2990: }
2991:
2992: /*
2993: * rcs_rev_write_fd()
2994: *
2995: * Write the entire contents of revision <frev> from the rcsfile <rfp> to
2996: * file descriptor <fd>.
2997: */
2998: void
2999: rcs_rev_write_fd(RCSFILE *rfp, RCSNUM *rev, int fd, int mode)
3000: {
1.200 joris 3001: int expmode, expand;
1.196 niallo 3002: struct rcs_delta *rdp;
3003: struct cvs_lines *lines;
1.233 tobias 3004: struct cvs_line *lp, *nlp;
1.217 joris 3005: extern int print_stdout;
1.196 niallo 3006:
1.200 joris 3007: expand = 0;
1.218 tobias 3008: lines = rcs_rev_getlines(rfp, rev, NULL);
1.200 joris 3009:
1.196 niallo 3010: if (!(mode & RCS_KWEXP_NONE)) {
3011: if (rfp->rf_expand != NULL)
3012: expmode = rcs_kwexp_get(rfp);
3013: else
3014: expmode = RCS_KWEXP_DEFAULT;
3015:
3016: if (!(expmode & RCS_KWEXP_NONE)) {
3017: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
3018: fatal("could not fetch revision");
1.200 joris 3019: expand = 1;
1.196 niallo 3020: }
3021: }
1.200 joris 3022:
1.255 deraadt 3023: for (lp = TAILQ_FIRST(&lines->l_lines); lp != NULL;) {
1.233 tobias 3024: nlp = TAILQ_NEXT(lp, l_list);
3025:
3026: if (lp->l_line == NULL) {
3027: lp = nlp;
1.196 niallo 3028: continue;
1.233 tobias 3029: }
1.200 joris 3030:
3031: if (expand)
1.233 tobias 3032: rcs_kwexp_line(rfp->rf_path, rdp, lines, lp, expmode);
1.217 joris 3033:
1.233 tobias 3034: do {
3035: /*
3036: * Solely for the checkout and update -p options.
3037: */
3038: if (cvs_server_active == 1 &&
3039: (cvs_cmdop == CVS_OP_CHECKOUT ||
3040: cvs_cmdop == CVS_OP_UPDATE) && print_stdout == 1) {
3041: if (atomicio(vwrite, fd, "M ", 2) != 2)
3042: fatal("rcs_rev_write_fd: %s",
3043: strerror(errno));
3044: }
3045:
3046: if (atomicio(vwrite, fd, lp->l_line, lp->l_len) !=
3047: lp->l_len)
1.217 joris 3048: fatal("rcs_rev_write_fd: %s", strerror(errno));
1.233 tobias 3049: } while ((lp = TAILQ_NEXT(lp, l_list)) != nlp);
1.196 niallo 3050: }
3051:
3052: cvs_freelines(lines);
3053:
3054: }
3055:
3056: /*
3057: * rcs_rev_write_stmp()
3058: *
3059: * Write the contents of the rev <rev> to a temporary file whose path is
3060: * specified using <template> (see mkstemp(3)). NB. This function will modify
3061: * <template>, as per mkstemp.
3062: */
1.251 joris 3063: int
1.196 niallo 3064: rcs_rev_write_stmp(RCSFILE *rfp, RCSNUM *rev, char *template, int mode)
3065: {
3066: int fd;
3067:
3068: if ((fd = mkstemp(template)) == -1)
3069: fatal("mkstemp: `%s': %s", template, strerror(errno));
3070:
3071: cvs_worklist_add(template, &temp_files);
3072: rcs_rev_write_fd(rfp, rev, fd, mode);
3073:
1.257 joris 3074: if (lseek(fd, 0, SEEK_SET) < 0)
1.251 joris 3075: fatal("rcs_rev_write_stmp: lseek: %s", strerror(errno));
3076:
3077: return (fd);
1.196 niallo 3078: }
3079:
3080: static void
1.233 tobias 3081: rcs_kwexp_line(char *rcsfile, struct rcs_delta *rdp, struct cvs_lines *lines,
3082: struct cvs_line *line, int mode)
1.196 niallo 3083: {
1.231 tobias 3084: BUF *tmpbuf;
1.196 niallo 3085: int kwtype;
3086: u_int j, found;
1.209 otto 3087: const u_char *c, *start, *fin, *end;
3088: char *kwstr;
1.196 niallo 3089: char expbuf[256], buf[256];
3090: char *fmt;
1.231 tobias 3091: size_t clen, kwlen, len, tlen;
1.196 niallo 3092:
3093: kwtype = 0;
3094: kwstr = NULL;
1.230 tobias 3095:
3096: if (mode & RCS_KWEXP_OLD)
3097: return;
1.196 niallo 3098:
3099: len = line->l_len;
3100: if (len == 0)
3101: return;
3102:
3103: c = line->l_line;
3104: found = 0;
3105: /* Final character in buffer. */
3106: fin = c + len - 1;
3107:
3108: /*
3109: * Keyword formats:
3110: * $Keyword$
3111: * $Keyword: value$
3112: */
3113: for (; c < fin; c++) {
1.231 tobias 3114: if (*c != '$')
3115: continue;
1.196 niallo 3116:
1.231 tobias 3117: /* remember start of this possible keyword */
3118: start = c;
1.196 niallo 3119:
1.231 tobias 3120: /* first following character has to be alphanumeric */
3121: c++;
3122: if (!isalpha(*c)) {
3123: c = start;
3124: continue;
3125: }
1.196 niallo 3126:
1.231 tobias 3127: /* Number of characters between c and fin, inclusive. */
3128: clen = fin - c + 1;
1.196 niallo 3129:
1.231 tobias 3130: /* look for any matching keywords */
3131: found = 0;
3132: for (j = 0; j < RCS_NKWORDS; j++) {
3133: kwlen = strlen(rcs_expkw[j].kw_str);
1.196 niallo 3134: /*
1.231 tobias 3135: * kwlen must be less than clen since clen
3136: * includes either a terminating `$' or a `:'.
1.196 niallo 3137: */
1.231 tobias 3138: if (kwlen < clen &&
3139: memcmp(c, rcs_expkw[j].kw_str, kwlen) == 0 &&
3140: (c[kwlen] == '$' || c[kwlen] == ':')) {
3141: found = 1;
3142: kwstr = rcs_expkw[j].kw_str;
3143: kwtype = rcs_expkw[j].kw_type;
3144: c += kwlen;
3145: break;
3146: }
3147: }
1.196 niallo 3148:
1.231 tobias 3149: if (found == 0 && cvs_tagname != NULL) {
3150: kwlen = strlen(cvs_tagname);
3151: if (kwlen < clen &&
3152: memcmp(c, cvs_tagname, kwlen) == 0 &&
3153: (c[kwlen] == '$' || c[kwlen] == ':')) {
3154: found = 1;
3155: kwstr = cvs_tagname;
3156: kwtype = RCS_KW_ID;
3157: c += kwlen;
1.196 niallo 3158: }
1.231 tobias 3159: }
1.196 niallo 3160:
1.231 tobias 3161: /* unknown keyword, continue looking */
3162: if (found == 0) {
3163: c = start;
3164: continue;
3165: }
1.196 niallo 3166:
1.231 tobias 3167: /*
3168: * if the next character was ':' we need to look for
3169: * an '$' before the end of the line to be sure it is
3170: * in fact a keyword.
3171: */
3172: if (*c == ':') {
3173: for (; c <= fin; ++c) {
3174: if (*c == '$' || *c == '\n')
3175: break;
1.196 niallo 3176: }
3177:
1.231 tobias 3178: if (*c != '$') {
3179: c = start;
3180: continue;
3181: }
3182: }
3183: end = c + 1;
1.196 niallo 3184:
1.231 tobias 3185: /* start constructing the expansion */
3186: expbuf[0] = '\0';
1.196 niallo 3187:
1.231 tobias 3188: if (mode & RCS_KWEXP_NAME) {
3189: if (strlcat(expbuf, "$", sizeof(expbuf)) >=
3190: sizeof(expbuf) || strlcat(expbuf, kwstr,
3191: sizeof(expbuf)) >= sizeof(expbuf))
3192: fatal("rcs_kwexp_line: truncated");
3193: if ((mode & RCS_KWEXP_VAL) &&
3194: strlcat(expbuf, ": ", sizeof(expbuf)) >=
3195: sizeof(expbuf))
3196: fatal("rcs_kwexp_line: truncated");
3197: }
1.196 niallo 3198:
1.231 tobias 3199: /*
3200: * order matters because of RCS_KW_ID and
3201: * RCS_KW_HEADER here
3202: */
3203: if (mode & RCS_KWEXP_VAL) {
3204: if (kwtype & RCS_KW_RCSFILE) {
3205: if (!(kwtype & RCS_KW_FULLPATH))
3206: (void)strlcat(expbuf, basename(rcsfile),
3207: sizeof(expbuf));
3208: else
3209: (void)strlcat(expbuf, rcsfile,
3210: sizeof(expbuf));
3211: if (strlcat(expbuf, " ", sizeof(expbuf)) >=
3212: sizeof(expbuf))
3213: fatal("rcs_kwexp_line: truncated");
3214: }
1.211 niallo 3215:
1.231 tobias 3216: if (kwtype & RCS_KW_REVISION) {
3217: rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
3218: if (strlcat(buf, " ", sizeof(buf)) >=
3219: sizeof(buf) || strlcat(expbuf, buf,
3220: sizeof(expbuf)) >= sizeof(buf))
3221: fatal("rcs_kwexp_line: truncated");
3222: }
1.196 niallo 3223:
1.231 tobias 3224: if (kwtype & RCS_KW_DATE) {
3225: fmt = "%Y/%m/%d %H:%M:%S ";
1.196 niallo 3226:
1.231 tobias 3227: if (strftime(buf, sizeof(buf), fmt,
3228: &rdp->rd_date) == 0)
3229: fatal("rcs_kwexp_line: strftime "
3230: "failure");
3231: if (strlcat(expbuf, buf, sizeof(expbuf)) >=
3232: sizeof(expbuf))
3233: fatal("rcs_kwexp_line: string "
3234: "truncated");
3235: }
1.196 niallo 3236:
1.231 tobias 3237: if (kwtype & RCS_KW_MDOCDATE) {
3238: /*
3239: * Do not prepend ' ' for a single
3240: * digit, %e would do so and there is
3241: * no better format for strftime().
3242: */
3243: if (rdp->rd_date.tm_mday < 10)
3244: fmt = "%B%e %Y ";
3245: else
3246: fmt = "%B %e %Y ";
3247:
3248: if (strftime(buf, sizeof(buf), fmt,
3249: &rdp->rd_date) == 0)
3250: fatal("rcs_kwexp_line: strftime "
3251: "failure");
3252: if (strlcat(expbuf, buf, sizeof(expbuf)) >=
3253: sizeof(expbuf))
3254: fatal("rcs_kwexp_line: string "
3255: "truncated");
3256: }
3257:
3258: if (kwtype & RCS_KW_AUTHOR) {
3259: if (strlcat(expbuf, rdp->rd_author,
3260: sizeof(expbuf)) >= sizeof(expbuf) ||
3261: strlcat(expbuf, " ", sizeof(expbuf)) >=
3262: sizeof(expbuf))
3263: fatal("rcs_kwexp_line: string "
3264: "truncated");
3265: }
3266:
3267: if (kwtype & RCS_KW_STATE) {
3268: if (strlcat(expbuf, rdp->rd_state,
3269: sizeof(expbuf)) >= sizeof(expbuf) ||
3270: strlcat(expbuf, " ", sizeof(expbuf)) >=
3271: sizeof(expbuf))
3272: fatal("rcs_kwexp_line: string "
3273: "truncated");
3274: }
3275:
3276: /* order does not matter anymore below */
1.233 tobias 3277: if (kwtype & RCS_KW_LOG) {
3278: char linebuf[256];
3279: struct cvs_line *cur, *lp;
3280: char *logp, *l_line, *prefix, *q, *sprefix;
3281: size_t i;
3282:
1.236 tobias 3283: /* Log line */
1.233 tobias 3284: if (!(kwtype & RCS_KW_FULLPATH))
3285: (void)strlcat(expbuf,
3286: basename(rcsfile), sizeof(expbuf));
3287: else
3288: (void)strlcat(expbuf, rcsfile,
3289: sizeof(expbuf));
3290:
1.231 tobias 3291: if (strlcat(expbuf, " ", sizeof(expbuf)) >=
3292: sizeof(expbuf))
3293: fatal("rcs_kwexp_line: string "
3294: "truncated");
1.233 tobias 3295:
3296: cur = line;
3297:
3298: /* copy rdp->rd_log for strsep */
3299: logp = xstrdup(rdp->rd_log);
3300:
3301: /* copy our prefix for later processing */
3302: prefix = xmalloc(start - line->l_line + 1);
3303: memcpy(prefix, line->l_line,
3304: start - line->l_line);
3305: prefix[start - line->l_line] = '\0';
3306:
3307: /* copy also prefix without trailing blanks. */
3308: sprefix = xstrdup(prefix);
3309: for (i = strlen(sprefix); i > 0 &&
3310: sprefix[i - 1] == ' '; i--)
3311: sprefix[i - 1] = '\0';
3312:
3313: /* new line: revision + date + author */
3314: linebuf[0] = '\0';
3315: if (strlcat(linebuf, "Revision ",
3316: sizeof(linebuf)) >= sizeof(linebuf))
3317: fatal("rcs_kwexp_line: truncated");
3318: rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
3319: if (strlcat(linebuf, buf, sizeof(linebuf))
3320: >= sizeof(buf))
3321: fatal("rcs_kwexp_line: truncated");
3322: fmt = " %Y/%m/%d %H:%M:%S ";
3323: if (strftime(buf, sizeof(buf), fmt,
3324: &rdp->rd_date) == 0)
3325: fatal("rcs_kwexp_line: strftime "
3326: "failure");
3327: if (strlcat(linebuf, buf, sizeof(linebuf))
3328: >= sizeof(linebuf))
3329: fatal("rcs_kwexp_line: string "
3330: "truncated");
3331: if (strlcat(linebuf, rdp->rd_author,
3332: sizeof(linebuf)) >= sizeof(linebuf))
3333: fatal("rcs_kwexp_line: string "
3334: "truncated");
3335:
3336: lp = xcalloc(1, sizeof(*lp));
3337: xasprintf(&(lp->l_line), "%s%s\n",
3338: prefix, linebuf);
3339: lp->l_len = strlen(lp->l_line);
3340: TAILQ_INSERT_AFTER(&(lines->l_lines), cur, lp,
3341: l_list);
3342: cur = lp;
3343:
3344: /* Log message */
3345: q = logp;
3346: while ((l_line = strsep(&q, "\n")) != NULL &&
3347: q != NULL) {
3348: lp = xcalloc(1, sizeof(*lp));
3349:
3350: if (l_line[0] == '\0') {
3351: xasprintf(&(lp->l_line), "%s\n",
3352: sprefix);
3353: } else {
3354: xasprintf(&(lp->l_line),
3355: "%s%s\n", prefix, l_line);
3356: }
3357:
3358: lp->l_len = strlen(lp->l_line);
3359: TAILQ_INSERT_AFTER(&(lines->l_lines),
3360: cur, lp, l_list);
3361: cur = lp;
3362: }
3363: xfree(logp);
3364:
3365: /*
3366: * This is just another hairy mess, but it must
1.237 tobias 3367: * be done: All characters behind Log will be
1.233 tobias 3368: * written in a new line next to log messages.
3369: * But that's not enough, we have to strip all
3370: * trailing whitespaces of our prefix.
3371: */
3372: lp = xcalloc(1, sizeof(*lp));
3373: lp->l_line = xcalloc(strlen(sprefix) +
3374: line->l_line + line->l_len - end + 1, 1);
3375: strlcpy(lp->l_line, sprefix,
3376: strlen(sprefix) + 1);
3377: memcpy(lp->l_line + strlen(sprefix),
3378: end, line->l_line + line->l_len - end);
3379: lp->l_len = strlen(lp->l_line);
3380: TAILQ_INSERT_AFTER(&(lines->l_lines), cur, lp,
3381: l_list);
3382: cur = lp;
3383:
3384: end = line->l_line + line->l_len - 1;
3385:
3386: xfree(prefix);
3387: xfree(sprefix);
3388:
3389: }
1.231 tobias 3390:
3391: if (kwtype & RCS_KW_SOURCE) {
3392: if (strlcat(expbuf, rcsfile, sizeof(expbuf)) >=
3393: sizeof(expbuf) || strlcat(expbuf, " ",
3394: sizeof(expbuf)) >= sizeof(expbuf))
3395: fatal("rcs_kwexp_line: string "
3396: "truncated");
1.196 niallo 3397: }
3398:
1.231 tobias 3399: if (kwtype & RCS_KW_NAME)
3400: if (strlcat(expbuf, " ", sizeof(expbuf)) >=
3401: sizeof(expbuf))
3402: fatal("rcs_kwexp_line: string "
3403: "truncated");
1.196 niallo 3404:
1.231 tobias 3405: if (kwtype & RCS_KW_LOCKER)
3406: if (strlcat(expbuf, " ", sizeof(expbuf)) >=
3407: sizeof(expbuf))
3408: fatal("rcs_kwexp_line: string "
3409: "truncated");
1.196 niallo 3410: }
1.231 tobias 3411:
3412: /* end the expansion */
3413: if (mode & RCS_KWEXP_NAME)
3414: if (strlcat(expbuf, "$",
3415: sizeof(expbuf)) >= sizeof(expbuf))
3416: fatal("rcs_kwexp_line: truncated");
3417:
3418: /* Concatenate everything together. */
1.249 tobias 3419: tmpbuf = cvs_buf_alloc(len + strlen(expbuf));
1.231 tobias 3420: /* Append everything before keyword. */
3421: cvs_buf_append(tmpbuf, line->l_line,
3422: start - line->l_line);
3423: /* Append keyword. */
3424: cvs_buf_append(tmpbuf, expbuf, strlen(expbuf));
3425: /* Point c to end of keyword. */
3426: tlen = cvs_buf_len(tmpbuf) - 1;
3427: /* Append everything after keyword. */
3428: cvs_buf_append(tmpbuf, end,
3429: line->l_line + line->l_len - end);
3430: c = cvs_buf_get(tmpbuf) + tlen;
3431: /* Point fin to end of data. */
3432: fin = cvs_buf_get(tmpbuf) + cvs_buf_len(tmpbuf) - 1;
3433: /* Recalculate new length. */
3434: len = cvs_buf_len(tmpbuf);
3435:
3436: /* tmpbuf is now ready, convert to string */
3437: if (line->l_needsfree)
3438: xfree(line->l_line);
3439: line->l_len = len;
3440: line->l_line = cvs_buf_release(tmpbuf);
3441: line->l_needsfree = 1;
1.196 niallo 3442: }
1.131 niallo 3443: }
1.246 tobias 3444:
3445: /* rcs_translate_tag() */
3446: RCSNUM *
3447: rcs_translate_tag(const char *revstr, RCSFILE *rfp)
3448: {
3449: int follow;
1.258 joris 3450: time_t deltatime;
1.250 joris 3451: char branch[CVS_REV_BUFSZ];
1.246 tobias 3452: RCSNUM *brev, *frev, *rev;
3453: struct rcs_delta *rdp, *trdp;
3454:
3455: brev = frev = NULL;
3456:
1.250 joris 3457: if (revstr == NULL) {
3458: if (rfp->rf_branch != NULL) {
3459: rcsnum_tostr(rfp->rf_branch, branch, sizeof(branch));
3460: revstr = branch;
3461: } else {
3462: revstr = RCS_HEAD_BRANCH;
3463: }
3464: }
1.246 tobias 3465:
3466: if ((rev = rcs_get_revision(revstr, rfp)) == NULL)
1.258 joris 3467: return (NULL);
3468:
3469: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
3470: fatal("rcs_translate_tag: cannot find revision");
3471:
3472: if (cvs_specified_date == -1) {
3473: /* XXX */
3474: if (rev->rn_len < 4) {
3475: return (rev);
3476: }
3477:
3478: /* Find the latest delta on that branch */
3479: rcsnum_free(rev);
3480: for (;;) {
3481: if (rdp->rd_next->rn_len == 0)
3482: break;
3483: if ((rdp = rcs_findrev(rfp, rdp->rd_next)) == NULL)
3484: fatal("rcs_get_revision: could not fetch "
3485: "branch delta");
3486: }
3487:
3488: rev = rcsnum_alloc();
3489: rcsnum_cpy(rdp->rd_num, rev, 0);
3490: return (rev);
3491: }
1.246 tobias 3492:
3493: /* let's see if we must follow a branch */
3494: if (!strcmp(revstr, RCS_HEAD_BRANCH))
3495: follow = 1;
3496: else {
3497: frev = rcs_sym_getrev(rfp, revstr);
3498:
3499: if (frev != NULL && RCSNUM_ISBRANCH(frev))
3500: follow = 1;
3501: else
3502: follow = 0;
3503: }
3504:
1.258 joris 3505: if (frev != NULL)
3506: rcsnum_tostr(frev, branch, sizeof(branch));
1.246 tobias 3507:
3508: if (frev != NULL) {
3509: brev = rcsnum_revtobr(frev);
1.260 joris 3510: brev->rn_len = rev->rn_len - 1;
1.246 tobias 3511: }
3512:
3513: rcsnum_free(rev);
3514:
3515: do {
1.258 joris 3516: deltatime = timelocal(&(rdp->rd_date));
3517:
3518: if (RCSNUM_ISBRANCHREV(rdp->rd_num)) {
3519: if (deltatime > cvs_specified_date) {
3520: trdp = TAILQ_PREV(rdp, rcs_dlist, rd_list);
3521: if (trdp == NULL)
3522: trdp = rdp;
1.260 joris 3523:
3524: if (trdp->rd_num->rn_len != rdp->rd_num->rn_len)
3525: return (NULL);
3526:
1.258 joris 3527: rev = rcsnum_alloc();
3528: rcsnum_cpy(trdp->rd_num, rev, 0);
1.260 joris 3529: return (rev);
3530: }
3531:
3532: if (rdp->rd_next->rn_len == 0) {
3533: rev = rcsnum_alloc();
3534: rcsnum_cpy(rdp->rd_num, rev, 0);
1.258 joris 3535: return (rev);
3536: }
3537: } else {
3538: if (deltatime < cvs_specified_date) {
3539: rev = rcsnum_alloc();
3540: rcsnum_cpy(rdp->rd_num, rev, 0);
3541: return (rev);
3542: }
1.246 tobias 3543: }
3544:
3545: if (follow && rdp->rd_next->rn_len != 0) {
3546: if (brev != NULL && !rcsnum_cmp(brev, rdp->rd_num, 0))
3547: break;
3548:
3549: trdp = rcs_findrev(rfp, rdp->rd_next);
3550: if (trdp == NULL)
3551: fatal("failed to grab next revision");
3552: rdp = trdp;
3553: } else
3554: follow = 0;
3555: } while (follow);
3556:
1.258 joris 3557: return (NULL);
1.246 tobias 3558: }
3559: