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