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