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