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