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