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