Annotation of src/usr.bin/cvs/rcs.c, Revision 1.131
1.131 ! niallo 1: /* $OpenBSD: rcs.c,v 1.130 2006/02/26 16:06:46 niallo 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;
235:
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.117 niallo 422: /* Write operations need the whole file parsed */
423: rcs_parse_deltatexts(rfp, NULL);
424:
1.28 jfb 425: if (rfp->rf_flags & RCS_SYNCED)
1.1 jfb 426: return (0);
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.1 jfb 1097: (*ep != ' ')) {
1098: cvs_log(LP_ERR,
1099: "invalid line specification in RCS patch");
1.61 joris 1100: return (-1);
1.1 jfb 1101: }
1102: ep++;
1103: nbln = (int)strtol(ep, &ep, 10);
1.82 joris 1104: if ((nbln < 0) || (*ep != '\0')) {
1.1 jfb 1105: cvs_log(LP_ERR,
1106: "invalid line number specification in RCS patch");
1.61 joris 1107: return (-1);
1.1 jfb 1108: }
1109:
1110: /* find the appropriate line */
1111: for (;;) {
1112: if (dlp == NULL)
1113: break;
1.94 joris 1114: if (dlp->l_lineno == lineno)
1.1 jfb 1115: break;
1.94 joris 1116: if (dlp->l_lineno > lineno) {
1117: dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
1118: } else if (dlp->l_lineno < lineno) {
1119: ndlp = TAILQ_NEXT(dlp, l_list);
1120: if (ndlp->l_lineno > lineno)
1.1 jfb 1121: break;
1122: dlp = ndlp;
1123: }
1124: }
1125: if (dlp == NULL) {
1126: cvs_log(LP_ERR,
1127: "can't find referenced line in RCS patch");
1.61 joris 1128: return (-1);
1.1 jfb 1129: }
1130:
1131: if (op == 'd') {
1132: for (i = 0; (i < nbln) && (dlp != NULL); i++) {
1.94 joris 1133: ndlp = TAILQ_NEXT(dlp, l_list);
1134: TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
1.1 jfb 1135: dlp = ndlp;
1136: }
1.14 deraadt 1137: } else if (op == 'a') {
1.1 jfb 1138: for (i = 0; i < nbln; i++) {
1139: ndlp = lp;
1.94 joris 1140: lp = TAILQ_NEXT(lp, l_list);
1.1 jfb 1141: if (lp == NULL) {
1142: cvs_log(LP_ERR, "truncated RCS patch");
1.5 vincent 1143: return (-1);
1.1 jfb 1144: }
1.94 joris 1145: TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
1146: TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
1147: lp, l_list);
1.1 jfb 1148: dlp = lp;
1149:
1150: /* we don't want lookup to block on those */
1.94 joris 1151: lp->l_lineno = lineno;
1.1 jfb 1152:
1153: lp = ndlp;
1154: }
1.14 deraadt 1155: } else {
1.1 jfb 1156: cvs_log(LP_ERR, "unknown RCS patch operation `%c'", op);
1.5 vincent 1157: return (-1);
1.1 jfb 1158: }
1159:
1160: /* last line of the patch, done */
1.94 joris 1161: if (lp->l_lineno == plines->l_nblines)
1.1 jfb 1162: break;
1163: }
1164:
1165: /* once we're done patching, rebuild the line numbers */
1.2 vincent 1166: lineno = 0;
1.94 joris 1167: TAILQ_FOREACH(lp, &(dlines->l_lines), l_list)
1168: lp->l_lineno = lineno++;
1169: dlines->l_nblines = lineno - 1;
1.1 jfb 1170:
1.5 vincent 1171: return (0);
1.1 jfb 1172: }
1173:
1174: /*
1175: * rcs_getrev()
1176: *
1177: * Get the whole contents of revision <rev> from the RCSFILE <rfp>. The
1.4 vincent 1178: * returned buffer is dynamically allocated and should be released using
1179: * cvs_buf_free() once the caller is done using it.
1.1 jfb 1180: */
1181: BUF*
1.66 joris 1182: rcs_getrev(RCSFILE *rfp, RCSNUM *frev)
1.1 jfb 1183: {
1.131 ! niallo 1184: int res;
1.1 jfb 1185: size_t len;
1186: void *bp;
1.66 joris 1187: RCSNUM *crev, *rev;
1.125 niallo 1188: BUF *rbuf;
1.1 jfb 1189: struct rcs_delta *rdp = NULL;
1190:
1.28 jfb 1191: if (rfp->rf_head == NULL)
1192: return (NULL);
1.66 joris 1193:
1194: if (frev == RCS_HEAD_REV)
1195: rev = rfp->rf_head;
1196: else
1197: rev = frev;
1.28 jfb 1198:
1.1 jfb 1199: res = rcsnum_cmp(rfp->rf_head, rev, 0);
1200: if (res == 1) {
1.32 jfb 1201: rcs_errno = RCS_ERR_NOENT;
1.1 jfb 1202: return (NULL);
1.26 jfb 1203: }
1204:
1.117 niallo 1205: /* No matter what, we're going to need up the the description parsed */
1206: rcs_parse_desc(rfp, NULL);
1207:
1.26 jfb 1208: rdp = rcs_findrev(rfp, rfp->rf_head);
1209: if (rdp == NULL) {
1210: cvs_log(LP_ERR, "failed to get RCS HEAD revision");
1211: return (NULL);
1212: }
1.117 niallo 1213: if (rdp->rd_tlen == 0)
1214: rcs_parse_deltatexts(rfp, rfp->rf_head);
1.45 jfb 1215: len = rdp->rd_tlen;
1.115 niallo 1216: if (len == 0) {
1217: rbuf = cvs_buf_alloc(1, 0);
1218: cvs_buf_empty(rbuf);
1219: return (rbuf);
1220: }
1.26 jfb 1221:
1.116 joris 1222: rbuf = cvs_buf_alloc(len, BUF_AUTOEXT);
1.26 jfb 1223: cvs_buf_append(rbuf, rdp->rd_text, len);
1224:
1225: if (res != 0) {
1226: /* Apply patches backwards to get the right version.
1227: * This will need some rework to support sub branches.
1228: */
1229: do {
1.87 joris 1230: crev = rdp->rd_next;
1.26 jfb 1231: rdp = rcs_findrev(rfp, crev);
1232: if (rdp == NULL) {
1233: cvs_buf_free(rbuf);
1234: return (NULL);
1235: }
1.114 xsa 1236: cvs_buf_putc(rbuf, '\0');
1.117 niallo 1237: /* check if we have parsed this rev's deltatext */
1238: if (rdp->rd_tlen == 0)
1239: rcs_parse_deltatexts(rfp, rdp->rd_num);
1.114 xsa 1240:
1.26 jfb 1241: bp = cvs_buf_release(rbuf);
1.94 joris 1242: rbuf = cvs_patchfile((char *)bp, (char *)rdp->rd_text,
1243: rcs_patch_lines);
1.110 joris 1244: xfree(bp);
1.26 jfb 1245: if (rbuf == NULL)
1246: break;
1247: } while (rcsnum_cmp(crev, rev, 0) != 0);
1.1 jfb 1248: }
1.125 niallo 1249: if (cvs_buf_getc(rbuf, cvs_buf_len(rbuf)-1) != '\n'
1250: && rbuf != NULL)
1251: cvs_buf_putc(rbuf, '\n');
1252: return (rbuf);
1.1 jfb 1253: }
1254:
1255: /*
1.52 jfb 1256: * rcs_rev_add()
1257: *
1.53 jfb 1258: * Add a revision to the RCS file <rf>. The new revision's number can be
1259: * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
1260: * new revision will have a number equal to the previous head revision plus
1261: * one). The <msg> argument specifies the log message for that revision, and
1262: * <date> specifies the revision's date (a value of -1 is
1263: * equivalent to using the current time).
1.96 xsa 1264: * If <username> is NULL, set the author for this revision to the current user.
1.90 niallo 1265: * Otherwise, set it to <username>.
1.52 jfb 1266: * Returns 0 on success, or -1 on failure.
1267: */
1268: int
1.90 niallo 1269: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
1270: const char *username)
1.52 jfb 1271: {
1272: time_t now;
1273: struct passwd *pw;
1.83 joris 1274: struct rcs_delta *ordp, *rdp;
1275: RCSNUM *old;
1276:
1.111 joris 1277: old = rcsnum_alloc();
1.52 jfb 1278: if (rev == RCS_HEAD_REV) {
1.101 niallo 1279: if (rf->rf_flags & RCS_CREATE) {
1280: if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
1281: return (-1);
1.111 joris 1282: rf->rf_head = rcsnum_alloc();
1.101 niallo 1283: rcsnum_cpy(rev, rf->rf_head, 0);
1284: } else {
1285: rcsnum_cpy(rf->rf_head, old, 0);
1286: rev = rcsnum_inc(rf->rf_head);
1287: }
1.83 joris 1288: } else {
1289: if ((rdp = rcs_findrev(rf, rev)) != NULL) {
1290: rcs_errno = RCS_ERR_DUPENT;
1.92 joris 1291: rcsnum_free(old);
1.83 joris 1292: return (-1);
1293: }
1294:
1.93 joris 1295: if (!(rf->rf_flags & RCS_CREATE)) {
1296: ordp = NULL;
1297: rcsnum_cpy(rev, old, 0);
1298: while (ordp == NULL) {
1299: old = rcsnum_dec(old);
1300: ordp = rcs_findrev(rf, old);
1301: }
1.83 joris 1302: }
1.52 jfb 1303: }
1304:
1.112 xsa 1305: if ((pw = getpwuid(getuid())) == NULL)
1306: fatal("getpwuid failed");
1.52 jfb 1307:
1.110 joris 1308: rdp = (struct rcs_delta *)xmalloc(sizeof(*rdp));
1.52 jfb 1309: memset(rdp, 0, sizeof(*rdp));
1310:
1311: TAILQ_INIT(&(rdp->rd_branches));
1312: TAILQ_INIT(&(rdp->rd_snodes));
1313:
1.111 joris 1314: rdp->rd_num = rcsnum_alloc();
1.52 jfb 1315: rcsnum_cpy(rev, rdp->rd_num, 0);
1.83 joris 1316:
1.111 joris 1317: rdp->rd_next = rcsnum_alloc();
1.92 joris 1318:
1.101 niallo 1319: if (!(rf->rf_flags & RCS_CREATE))
1320: rcsnum_cpy(old, rdp->rd_next, 0);
1321:
1.83 joris 1322: rcsnum_free(old);
1.52 jfb 1323:
1.90 niallo 1324: if (username == NULL)
1325: username = pw->pw_name;
1326:
1.110 joris 1327: rdp->rd_author = xstrdup(username);
1328: rdp->rd_state = xstrdup(RCS_STATE_EXP);
1329: rdp->rd_log = xstrdup(msg);
1.52 jfb 1330:
1.53 jfb 1331: if (date != (time_t)(-1))
1332: now = date;
1333: else
1334: time(&now);
1.52 jfb 1335: gmtime_r(&now, &(rdp->rd_date));
1336:
1337: TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1338: rf->rf_ndelta++;
1.81 niallo 1339:
1.64 niallo 1340: /* not synced anymore */
1341: rf->rf_flags &= ~RCS_SYNCED;
1.52 jfb 1342:
1343: return (0);
1344: }
1345:
1346: /*
1347: * rcs_rev_remove()
1348: *
1349: * Remove the revision whose number is <rev> from the RCS file <rf>.
1350: */
1351: int
1352: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
1353: {
1354: int ret;
1355: struct rcs_delta *rdp;
1356:
1357: ret = 0;
1358: if (rev == RCS_HEAD_REV)
1359: rev = rf->rf_head;
1360:
1361: /* do we actually have that revision? */
1362: if ((rdp = rcs_findrev(rf, rev)) == NULL) {
1363: rcs_errno = RCS_ERR_NOENT;
1364: ret = -1;
1365: } else {
1366: /* XXX assumes it's not a sub node */
1367: TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
1368: rf->rf_ndelta--;
1369: rf->rf_flags &= ~RCS_SYNCED;
1370: }
1371:
1372: return (ret);
1373:
1374: }
1375:
1376: /*
1.1 jfb 1377: * rcs_findrev()
1378: *
1379: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1380: * The revision number is given in <rev>.
1381: * Returns a pointer to the delta on success, or NULL on failure.
1382: */
1.102 xsa 1383: struct rcs_delta *
1.117 niallo 1384: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
1.1 jfb 1385: {
1386: u_int cmplen;
1.117 niallo 1387: struct rcs_delta *rdp, *enddelta;
1.1 jfb 1388: struct rcs_dlist *hp;
1.6 vincent 1389: int found;
1.26 jfb 1390:
1.117 niallo 1391: /*
1392: * We need to do more parsing if the last revision in the linked list
1393: * is greater than the requested revision.
1394: */
1395: enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1396: if ((enddelta == NULL)
1397: || (rcsnum_cmp(enddelta->rd_num, rev, -1) == -1)) {
1398: rcs_parse_deltas(rfp, rev);
1399: }
1400:
1.1 jfb 1401: cmplen = 2;
1402: hp = &(rfp->rf_delta);
1403:
1.6 vincent 1404: do {
1405: found = 0;
1406: TAILQ_FOREACH(rdp, hp, rd_list) {
1407: if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
1408: if (cmplen == rev->rn_len)
1409: return (rdp);
1.1 jfb 1410:
1.6 vincent 1411: hp = &(rdp->rd_snodes);
1412: cmplen += 2;
1413: found = 1;
1414: break;
1415: }
1.1 jfb 1416: }
1.6 vincent 1417: } while (found && cmplen < rev->rn_len);
1.1 jfb 1418:
1419: return (NULL);
1.20 jfb 1420: }
1421:
1422: /*
1.26 jfb 1423: * rcs_kwexp_set()
1424: *
1425: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1426: * Returns 0 on success, or -1 on failure.
1427: */
1428: int
1429: rcs_kwexp_set(RCSFILE *file, int mode)
1430: {
1431: int i;
1432: char *tmp, buf[8] = "";
1433:
1434: if (RCS_KWEXP_INVAL(mode))
1435: return (-1);
1436:
1437: i = 0;
1438: if (mode == RCS_KWEXP_NONE)
1439: buf[0] = 'b';
1440: else if (mode == RCS_KWEXP_OLD)
1441: buf[0] = 'o';
1442: else {
1443: if (mode & RCS_KWEXP_NAME)
1444: buf[i++] = 'k';
1445: if (mode & RCS_KWEXP_VAL)
1446: buf[i++] = 'v';
1447: if (mode & RCS_KWEXP_LKR)
1448: buf[i++] = 'l';
1449: }
1450:
1.110 joris 1451: tmp = xstrdup(buf);
1.27 jfb 1452: if (file->rf_expand != NULL)
1.99 niallo 1453: free(file->rf_expand);
1.26 jfb 1454: file->rf_expand = tmp;
1.64 niallo 1455: /* not synced anymore */
1456: file->rf_flags &= ~RCS_SYNCED;
1.26 jfb 1457:
1458: return (0);
1459: }
1460:
1461: /*
1462: * rcs_kwexp_get()
1463: *
1464: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1465: */
1466: int
1467: rcs_kwexp_get(RCSFILE *file)
1468: {
1469: return rcs_kflag_get(file->rf_expand);
1470: }
1471:
1472: /*
1.20 jfb 1473: * rcs_kflag_get()
1474: *
1475: * Get the keyword expansion mode from a set of character flags given in
1476: * <flags> and return the appropriate flag mask. In case of an error, the
1477: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1478: */
1479: int
1480: rcs_kflag_get(const char *flags)
1481: {
1482: int fl;
1483: size_t len;
1484: const char *fp;
1485:
1486: fl = 0;
1487: len = strlen(flags);
1488:
1489: for (fp = flags; *fp != '\0'; fp++) {
1490: if (*fp == 'k')
1491: fl |= RCS_KWEXP_NAME;
1492: else if (*fp == 'v')
1493: fl |= RCS_KWEXP_VAL;
1494: else if (*fp == 'l')
1495: fl |= RCS_KWEXP_LKR;
1496: else if (*fp == 'o') {
1497: if (len != 1)
1498: fl |= RCS_KWEXP_ERR;
1499: fl |= RCS_KWEXP_OLD;
1500: } else if (*fp == 'b') {
1501: if (len != 1)
1502: fl |= RCS_KWEXP_ERR;
1503: } else /* unknown letter */
1504: fl |= RCS_KWEXP_ERR;
1505: }
1506:
1507: return (fl);
1.32 jfb 1508: }
1509:
1510: /*
1511: * rcs_errstr()
1512: *
1513: * Get the error string matching the RCS error code <code>.
1514: */
1.57 xsa 1515: const char *
1.32 jfb 1516: rcs_errstr(int code)
1517: {
1.50 jfb 1518: const char *esp;
1519:
1520: if ((code < 0) || ((code >= (int)RCS_NERR) && (code != RCS_ERR_ERRNO)))
1521: esp = NULL;
1522: else if (code == RCS_ERR_ERRNO)
1523: esp = strerror(errno);
1524: else
1525: esp = rcs_errstrs[code];
1526: return (esp);
1.1 jfb 1527: }
1528:
1.21 jfb 1529: void
1530: rcs_kflag_usage(void)
1531: {
1532: fprintf(stderr, "Valid expansion modes include:\n"
1.22 jfb 1533: "\t-kkv\tGenerate keywords using the default form.\n"
1534: "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
1535: "\t-kk\tGenerate only keyword names in keyword strings.\n"
1536: "\t-kv\tGenerate only keyword values in keyword strings.\n"
1537: "\t-ko\tGenerate old keyword string "
1.21 jfb 1538: "(no changes from checked in file).\n"
1.22 jfb 1539: "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21 jfb 1540: }
1.1 jfb 1541:
1.117 niallo 1542: /* rcs_parse_deltas()
1543: *
1544: * Parse deltas. If <rev> is not NULL, parse only as far as that
1545: * revision. If <rev> is NULL, parse all deltas.
1546: *
1547: * Returns 0 on success, -1 on failure.
1548: */
1549: static int
1550: rcs_parse_deltas(RCSFILE *rfp, RCSNUM *rev)
1551: {
1552: int ret;
1553: struct rcs_delta *enddelta;
1554: if ((rfp->rf_flags & PARSED_DELTAS)
1555: || (rfp->rf_flags & RCS_CREATE))
1556: return (0);
1557: for (;;) {
1558: ret = rcs_parse_delta(rfp);
1559: if (rev != NULL) {
1560: enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1561: if (rcsnum_cmp(enddelta->rd_num, rev, -1) == 0)
1562: break;
1563: }
1564: if (ret == 0) {
1565: rfp->rf_flags |= PARSED_DELTAS;
1566: break;
1567: }
1568: else if (ret == -1)
1569: fatal("error parsing deltas");
1570: }
1571: return (0);
1572: }
1573:
1574: /* rcs_parse_deltatexts()
1575: *
1576: * Parse deltatexts. If <rev> is not NULL, parse only as far as that
1577: * revision. If <rev> is NULL, parse everything.
1578: *
1579: * Returns 0 on success, -1 on failure.
1580: */
1581: static int
1582: rcs_parse_deltatexts(RCSFILE *rfp, RCSNUM *rev)
1583: {
1584: int ret;
1585: struct rcs_delta *rdp;
1586: if ((rfp->rf_flags & PARSED_DELTATEXTS)
1587: || (rfp->rf_flags & RCS_CREATE))
1588: return (0);
1589: if (!(rfp->rf_flags & PARSED_DESC))
1590: rcs_parse_desc(rfp, rev);
1591: for (;;) {
1592: if (rev != NULL) {
1593: rdp = rcs_findrev(rfp, rev);
1594: if (rdp->rd_text != NULL)
1595: break;
1596: else
1597: ret = rcs_parse_deltatext(rfp);
1598: } else
1599: ret = rcs_parse_deltatext(rfp);
1600: if (ret == 0) {
1601: rfp->rf_flags |= PARSED_DELTATEXTS;
1602: break;
1603: }
1604: else if (ret == -1) {
1605: fatal("problem parsing deltatexts");
1606: }
1607: }
1608:
1609: return (0);
1610: }
1611:
1612: /* rcs_parse_desc()
1613: *
1614: * Parse RCS description.
1615: *
1616: * Returns 0 on success, -1 on failure.
1617: */
1618: static int
1619: rcs_parse_desc(RCSFILE *rfp, RCSNUM *rev)
1620: {
1621: int ret = 0;
1622: if ((rfp->rf_flags & PARSED_DESC)
1623: || (rfp->rf_flags & RCS_CREATE))
1624: return (0);
1.122 reyk 1625: if (!(rfp->rf_flags & PARSED_DELTAS))
1.117 niallo 1626: rcs_parse_deltas(rfp, rev);
1627: /* do parsing */
1628: ret = rcs_gettok(rfp);
1629: if (ret != RCS_TOK_DESC) {
1630: rcs_errno = RCS_ERR_PARSE;
1631: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1632: RCS_TOKSTR(rfp));
1633: fatal("problem parsing RCS desc");
1634: return (-1);
1635: }
1636:
1637: ret = rcs_gettok(rfp);
1638: if (ret != RCS_TOK_STRING) {
1639: rcs_errno = RCS_ERR_PARSE;
1640: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1641: RCS_TOKSTR(rfp));
1642: fatal("problem parsing RCS desc");
1643: }
1644:
1645: rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
1646: rfp->rf_flags |= PARSED_DESC;
1647: return (0);
1648: }
1649:
1.1 jfb 1650: /*
1.117 niallo 1651: * rcs_parse_init()
1.1 jfb 1652: *
1.117 niallo 1653: * Initial parsing of file <path>, which are in the RCS format.
1654: * Just does admin section
1.1 jfb 1655: * Returns 0 on success, or -1 on failure.
1656: */
1.26 jfb 1657: static int
1.117 niallo 1658: rcs_parse_init(RCSFILE *rfp)
1.1 jfb 1659: {
1.117 niallo 1660: int ret, count;
1.1 jfb 1661: struct rcs_pdata *pdp;
1662:
1.117 niallo 1663: count = 0;
1.26 jfb 1664: if (rfp->rf_flags & RCS_PARSED)
1.1 jfb 1665: return (0);
1666:
1.110 joris 1667: pdp = (struct rcs_pdata *)xmalloc(sizeof(*pdp));
1.1 jfb 1668: memset(pdp, 0, sizeof(*pdp));
1669:
1.18 jfb 1670: pdp->rp_lines = 0;
1.1 jfb 1671: pdp->rp_pttype = RCS_TOK_ERR;
1672:
1.113 xsa 1673: if ((pdp->rp_file = fopen(rfp->rf_path, "r")) == NULL)
1674: fatal("fopen: `%s': %s", rfp->rf_path, strerror(errno));
1.1 jfb 1675:
1.110 joris 1676: pdp->rp_buf = (char *)xmalloc((size_t)RCS_BUFSIZE);
1.1 jfb 1677: pdp->rp_blen = RCS_BUFSIZE;
1.18 jfb 1678: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1 jfb 1679:
1680: /* ditch the strict lock */
1.26 jfb 1681: rfp->rf_flags &= ~RCS_SLOCK;
1.1 jfb 1682: rfp->rf_pdata = pdp;
1683:
1.31 jfb 1684: if ((ret = rcs_parse_admin(rfp)) < 0) {
1.1 jfb 1685: rcs_freepdata(pdp);
1.117 niallo 1686: fatal("could not parse admin data");
1.1 jfb 1687: }
1688:
1.117 niallo 1689: if (rfp->rf_flags & RCS_PARSE_FULLY)
1690: rcs_parse_deltatexts(rfp, NULL);
1.1 jfb 1691:
1.117 niallo 1692: rfp->rf_flags |= RCS_SYNCED;
1.1 jfb 1693: return (0);
1694: }
1695:
1696: /*
1697: * rcs_parse_admin()
1698: *
1699: * Parse the administrative portion of an RCS file.
1.31 jfb 1700: * Returns the type of the first token found after the admin section on
1701: * success, or -1 on failure.
1.1 jfb 1702: */
1703: static int
1704: rcs_parse_admin(RCSFILE *rfp)
1705: {
1706: u_int i;
1707: int tok, ntok, hmask;
1708: struct rcs_key *rk;
1709:
1710: /* hmask is a mask of the headers already encountered */
1711: hmask = 0;
1712: for (;;) {
1713: tok = rcs_gettok(rfp);
1714: if (tok == RCS_TOK_ERR) {
1.50 jfb 1715: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1716: cvs_log(LP_ERR, "parse error in RCS admin section");
1717: return (-1);
1.31 jfb 1718: } else if ((tok == RCS_TOK_NUM) || (tok == RCS_TOK_DESC)) {
1719: /*
1720: * Assume this is the start of the first delta or
1721: * that we are dealing with an empty RCS file and
1722: * we just found the description.
1723: */
1.1 jfb 1724: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31 jfb 1725: return (tok);
1.1 jfb 1726: }
1727:
1728: rk = NULL;
1.18 jfb 1729: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1730: if (rcs_keys[i].rk_id == tok)
1731: rk = &(rcs_keys[i]);
1732:
1733: if (hmask & (1 << tok)) {
1.50 jfb 1734: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1735: cvs_log(LP_ERR, "duplicate RCS key");
1736: return (-1);
1737: }
1738: hmask |= (1 << tok);
1739:
1740: switch (tok) {
1741: case RCS_TOK_HEAD:
1742: case RCS_TOK_BRANCH:
1743: case RCS_TOK_COMMENT:
1744: case RCS_TOK_EXPAND:
1745: ntok = rcs_gettok(rfp);
1746: if (ntok == RCS_TOK_SCOLON)
1747: break;
1748: if (ntok != rk->rk_val) {
1.50 jfb 1749: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1750: cvs_log(LP_ERR,
1751: "invalid value type for RCS key `%s'",
1752: rk->rk_str);
1753: }
1754:
1755: if (tok == RCS_TOK_HEAD) {
1.111 joris 1756: if (rfp->rf_head == NULL)
1.28 jfb 1757: rfp->rf_head = rcsnum_alloc();
1.1 jfb 1758: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1759: rfp->rf_head);
1.14 deraadt 1760: } else if (tok == RCS_TOK_BRANCH) {
1.111 joris 1761: if (rfp->rf_branch == NULL)
1.35 jfb 1762: rfp->rf_branch = rcsnum_alloc();
1763: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1764: rfp->rf_branch) < 0)
1765: return (-1);
1.14 deraadt 1766: } else if (tok == RCS_TOK_COMMENT) {
1.110 joris 1767: rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
1.14 deraadt 1768: } else if (tok == RCS_TOK_EXPAND) {
1.110 joris 1769: rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 1770: }
1771:
1772: /* now get the expected semi-colon */
1773: ntok = rcs_gettok(rfp);
1774: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1775: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1776: cvs_log(LP_ERR,
1777: "missing semi-colon after RCS `%s' key",
1.26 jfb 1778: rk->rk_str);
1.1 jfb 1779: return (-1);
1780: }
1781: break;
1782: case RCS_TOK_ACCESS:
1.29 jfb 1783: if (rcs_parse_access(rfp) < 0)
1784: return (-1);
1.1 jfb 1785: break;
1786: case RCS_TOK_SYMBOLS:
1.29 jfb 1787: if (rcs_parse_symbols(rfp) < 0)
1788: return (-1);
1.1 jfb 1789: break;
1790: case RCS_TOK_LOCKS:
1.29 jfb 1791: if (rcs_parse_locks(rfp) < 0)
1792: return (-1);
1.1 jfb 1793: break;
1794: default:
1.50 jfb 1795: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1796: cvs_log(LP_ERR,
1797: "unexpected token `%s' in RCS admin section",
1798: RCS_TOKSTR(rfp));
1799: return (-1);
1800: }
1801: }
1802:
1803: return (0);
1804: }
1805:
1806: /*
1807: * rcs_parse_delta()
1808: *
1809: * Parse an RCS delta section and allocate the structure to store that delta's
1810: * information in the <rfp> delta list.
1811: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1812: * -1 on error.
1813: */
1814: static int
1815: rcs_parse_delta(RCSFILE *rfp)
1816: {
1817: int ret, tok, ntok, hmask;
1818: u_int i;
1819: char *tokstr;
1.3 vincent 1820: RCSNUM *datenum;
1.1 jfb 1821: struct rcs_delta *rdp;
1822: struct rcs_key *rk;
1823:
1.110 joris 1824: rdp = (struct rcs_delta *)xmalloc(sizeof(*rdp));
1.1 jfb 1825: memset(rdp, 0, sizeof(*rdp));
1826:
1827: rdp->rd_num = rcsnum_alloc();
1828: rdp->rd_next = rcsnum_alloc();
1829:
1830: TAILQ_INIT(&(rdp->rd_branches));
1.52 jfb 1831: TAILQ_INIT(&(rdp->rd_snodes));
1.1 jfb 1832:
1833: tok = rcs_gettok(rfp);
1834: if (tok != RCS_TOK_NUM) {
1.52 jfb 1835: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1836: cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
1837: RCS_TOKSTR(rfp));
1838: rcs_freedelta(rdp);
1839: return (-1);
1840: }
1841: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1842:
1843: hmask = 0;
1844: ret = 0;
1845: tokstr = NULL;
1846:
1847: for (;;) {
1848: tok = rcs_gettok(rfp);
1849: if (tok == RCS_TOK_ERR) {
1.50 jfb 1850: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1851: cvs_log(LP_ERR, "parse error in RCS delta section");
1852: rcs_freedelta(rdp);
1853: return (-1);
1.14 deraadt 1854: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15 tedu 1855: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1 jfb 1856: ret = (tok == RCS_TOK_NUM ? 1 : 0);
1857: break;
1858: }
1859:
1860: rk = NULL;
1.18 jfb 1861: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1862: if (rcs_keys[i].rk_id == tok)
1863: rk = &(rcs_keys[i]);
1864:
1865: if (hmask & (1 << tok)) {
1.50 jfb 1866: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1867: cvs_log(LP_ERR, "duplicate RCS key");
1868: rcs_freedelta(rdp);
1869: return (-1);
1870: }
1871: hmask |= (1 << tok);
1872:
1873: switch (tok) {
1874: case RCS_TOK_DATE:
1875: case RCS_TOK_AUTHOR:
1876: case RCS_TOK_STATE:
1877: case RCS_TOK_NEXT:
1878: ntok = rcs_gettok(rfp);
1879: if (ntok == RCS_TOK_SCOLON) {
1880: if (rk->rk_flags & RCS_VOPT)
1881: break;
1882: else {
1.50 jfb 1883: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1884: cvs_log(LP_ERR, "missing mandatory "
1885: "value to RCS key `%s'",
1886: rk->rk_str);
1887: rcs_freedelta(rdp);
1888: return (-1);
1889: }
1890: }
1891:
1892: if (ntok != rk->rk_val) {
1.50 jfb 1893: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1894: cvs_log(LP_ERR,
1895: "invalid value type for RCS key `%s'",
1896: rk->rk_str);
1897: rcs_freedelta(rdp);
1898: return (-1);
1899: }
1900:
1901: if (tokstr != NULL)
1.110 joris 1902: xfree(tokstr);
1903: tokstr = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 1904: /* now get the expected semi-colon */
1905: ntok = rcs_gettok(rfp);
1906: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1907: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1908: cvs_log(LP_ERR,
1909: "missing semi-colon after RCS `%s' key",
1.26 jfb 1910: rk->rk_str);
1.110 joris 1911: xfree(tokstr);
1.1 jfb 1912: rcs_freedelta(rdp);
1913: return (-1);
1914: }
1915:
1916: if (tok == RCS_TOK_DATE) {
1.25 jfb 1917: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.110 joris 1918: xfree(tokstr);
1.11 joris 1919: rcs_freedelta(rdp);
1920: return (-1);
1921: }
1.3 vincent 1922: if (datenum->rn_len != 6) {
1.50 jfb 1923: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1924: cvs_log(LP_ERR,
1925: "RCS date specification has %s "
1926: "fields",
1.3 vincent 1927: (datenum->rn_len > 6) ? "too many" :
1.1 jfb 1928: "missing");
1.110 joris 1929: xfree(tokstr);
1.1 jfb 1930: rcs_freedelta(rdp);
1.37 tedu 1931: rcsnum_free(datenum);
1932: return (-1);
1.1 jfb 1933: }
1.3 vincent 1934: rdp->rd_date.tm_year = datenum->rn_id[0];
1.19 jfb 1935: if (rdp->rd_date.tm_year >= 1900)
1936: rdp->rd_date.tm_year -= 1900;
1.3 vincent 1937: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
1938: rdp->rd_date.tm_mday = datenum->rn_id[2];
1939: rdp->rd_date.tm_hour = datenum->rn_id[3];
1940: rdp->rd_date.tm_min = datenum->rn_id[4];
1941: rdp->rd_date.tm_sec = datenum->rn_id[5];
1942: rcsnum_free(datenum);
1.14 deraadt 1943: } else if (tok == RCS_TOK_AUTHOR) {
1.1 jfb 1944: rdp->rd_author = tokstr;
1945: tokstr = NULL;
1.14 deraadt 1946: } else if (tok == RCS_TOK_STATE) {
1.1 jfb 1947: rdp->rd_state = tokstr;
1948: tokstr = NULL;
1.14 deraadt 1949: } else if (tok == RCS_TOK_NEXT) {
1.1 jfb 1950: rcsnum_aton(tokstr, NULL, rdp->rd_next);
1951: }
1952: break;
1953: case RCS_TOK_BRANCHES:
1.46 jfb 1954: if (rcs_parse_branches(rfp, rdp) < 0) {
1955: rcs_freedelta(rdp);
1956: return (-1);
1957: }
1.1 jfb 1958: break;
1959: default:
1.50 jfb 1960: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1961: cvs_log(LP_ERR,
1962: "unexpected token `%s' in RCS delta",
1963: RCS_TOKSTR(rfp));
1964: rcs_freedelta(rdp);
1965: return (-1);
1966: }
1967: }
1968:
1.13 jfb 1969: if (tokstr != NULL)
1.110 joris 1970: xfree(tokstr);
1.13 jfb 1971:
1.1 jfb 1972: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26 jfb 1973: rfp->rf_ndelta++;
1.1 jfb 1974:
1975: return (ret);
1976: }
1977:
1978: /*
1979: * rcs_parse_deltatext()
1980: *
1981: * Parse an RCS delta text section and fill in the log and text field of the
1982: * appropriate delta section.
1983: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1984: * -1 on error.
1985: */
1986: static int
1987: rcs_parse_deltatext(RCSFILE *rfp)
1988: {
1989: int tok;
1990: RCSNUM *tnum;
1991: struct rcs_delta *rdp;
1992:
1993: tok = rcs_gettok(rfp);
1994: if (tok == RCS_TOK_EOF)
1995: return (0);
1996:
1997: if (tok != RCS_TOK_NUM) {
1.50 jfb 1998: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1999: cvs_log(LP_ERR,
2000: "unexpected token `%s' at start of RCS delta text",
2001: RCS_TOKSTR(rfp));
2002: return (-1);
2003: }
1.13 jfb 2004:
2005: tnum = rcsnum_alloc();
1.1 jfb 2006: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
2007:
2008: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
2009: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
2010: break;
2011: }
1.13 jfb 2012: rcsnum_free(tnum);
2013:
1.1 jfb 2014: if (rdp == NULL) {
2015: cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
2016: RCS_TOKSTR(rfp));
2017: return (-1);
2018: }
2019:
2020: tok = rcs_gettok(rfp);
2021: if (tok != RCS_TOK_LOG) {
1.50 jfb 2022: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2023: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
2024: RCS_TOKSTR(rfp));
2025: return (-1);
2026: }
2027:
2028: tok = rcs_gettok(rfp);
2029: if (tok != RCS_TOK_STRING) {
1.50 jfb 2030: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2031: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
2032: RCS_TOKSTR(rfp));
2033: return (-1);
2034: }
1.110 joris 2035: rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 2036: tok = rcs_gettok(rfp);
2037: if (tok != RCS_TOK_TEXT) {
1.50 jfb 2038: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2039: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2040: RCS_TOKSTR(rfp));
2041: return (-1);
2042: }
2043:
2044: tok = rcs_gettok(rfp);
2045: if (tok != RCS_TOK_STRING) {
1.50 jfb 2046: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2047: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2048: RCS_TOKSTR(rfp));
2049: return (-1);
2050: }
2051:
1.110 joris 2052: rdp->rd_text = (u_char *)xmalloc(RCS_TOKLEN(rfp) + 1);
1.74 joris 2053: strlcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp) + 1));
1.42 jfb 2054: rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1 jfb 2055:
2056: return (1);
2057: }
2058:
2059: /*
2060: * rcs_parse_access()
2061: *
2062: * Parse the access list given as value to the `access' keyword.
2063: * Returns 0 on success, or -1 on failure.
2064: */
2065: static int
2066: rcs_parse_access(RCSFILE *rfp)
2067: {
2068: int type;
2069:
2070: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
2071: if (type != RCS_TOK_ID) {
1.50 jfb 2072: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2073: cvs_log(LP_ERR, "unexpected token `%s' in access list",
2074: RCS_TOKSTR(rfp));
2075: return (-1);
2076: }
1.29 jfb 2077:
2078: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
2079: return (-1);
1.1 jfb 2080: }
2081:
2082: return (0);
2083: }
2084:
2085: /*
2086: * rcs_parse_symbols()
2087: *
2088: * Parse the symbol list given as value to the `symbols' keyword.
2089: * Returns 0 on success, or -1 on failure.
2090: */
2091: static int
2092: rcs_parse_symbols(RCSFILE *rfp)
2093: {
2094: int type;
2095: struct rcs_sym *symp;
2096:
2097: for (;;) {
2098: type = rcs_gettok(rfp);
2099: if (type == RCS_TOK_SCOLON)
2100: break;
2101:
1.41 jfb 2102: if (type != RCS_TOK_ID) {
1.50 jfb 2103: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2104: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2105: RCS_TOKSTR(rfp));
2106: return (-1);
2107: }
2108:
1.110 joris 2109: symp = (struct rcs_sym *)xmalloc(sizeof(*symp));
2110: symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 2111: symp->rs_num = rcsnum_alloc();
2112:
2113: type = rcs_gettok(rfp);
2114: if (type != RCS_TOK_COLON) {
1.50 jfb 2115: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2116: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2117: RCS_TOKSTR(rfp));
1.11 joris 2118: rcsnum_free(symp->rs_num);
1.110 joris 2119: xfree(symp->rs_name);
2120: xfree(symp);
1.1 jfb 2121: return (-1);
2122: }
2123:
2124: type = rcs_gettok(rfp);
2125: if (type != RCS_TOK_NUM) {
1.50 jfb 2126: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2127: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2128: RCS_TOKSTR(rfp));
1.11 joris 2129: rcsnum_free(symp->rs_num);
1.110 joris 2130: xfree(symp->rs_name);
2131: xfree(symp);
1.1 jfb 2132: return (-1);
2133: }
2134:
2135: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
2136: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2137: RCS_TOKSTR(rfp));
1.11 joris 2138: rcsnum_free(symp->rs_num);
1.110 joris 2139: xfree(symp->rs_name);
2140: xfree(symp);
1.1 jfb 2141: return (-1);
2142: }
2143:
1.43 jfb 2144: TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1 jfb 2145: }
2146:
2147: return (0);
2148: }
2149:
2150: /*
2151: * rcs_parse_locks()
2152: *
2153: * Parse the lock list given as value to the `locks' keyword.
2154: * Returns 0 on success, or -1 on failure.
2155: */
2156: static int
2157: rcs_parse_locks(RCSFILE *rfp)
2158: {
2159: int type;
2160: struct rcs_lock *lkp;
2161:
2162: for (;;) {
2163: type = rcs_gettok(rfp);
2164: if (type == RCS_TOK_SCOLON)
2165: break;
2166:
2167: if (type != RCS_TOK_ID) {
1.50 jfb 2168: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2169: cvs_log(LP_ERR, "unexpected token `%s' in lock list",
2170: RCS_TOKSTR(rfp));
2171: return (-1);
2172: }
2173:
1.110 joris 2174: lkp = (struct rcs_lock *)xmalloc(sizeof(*lkp));
2175: lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
1.1 jfb 2176: lkp->rl_num = rcsnum_alloc();
2177:
2178: type = rcs_gettok(rfp);
2179: if (type != RCS_TOK_COLON) {
1.50 jfb 2180: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2181: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2182: RCS_TOKSTR(rfp));
1.37 tedu 2183: rcsnum_free(lkp->rl_num);
1.110 joris 2184: xfree(lkp->rl_name);
2185: xfree(lkp);
1.1 jfb 2186: return (-1);
2187: }
2188:
2189: type = rcs_gettok(rfp);
2190: if (type != RCS_TOK_NUM) {
1.50 jfb 2191: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2192: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2193: RCS_TOKSTR(rfp));
1.37 tedu 2194: rcsnum_free(lkp->rl_num);
1.110 joris 2195: xfree(lkp->rl_name);
2196: xfree(lkp);
1.1 jfb 2197: return (-1);
2198: }
2199:
2200: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
2201: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2202: RCS_TOKSTR(rfp));
1.37 tedu 2203: rcsnum_free(lkp->rl_num);
1.110 joris 2204: xfree(lkp->rl_name);
2205: xfree(lkp);
1.1 jfb 2206: return (-1);
2207: }
2208:
2209: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
2210: }
2211:
2212: /* check if we have a `strict' */
2213: type = rcs_gettok(rfp);
2214: if (type != RCS_TOK_STRICT) {
2215: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14 deraadt 2216: } else {
1.26 jfb 2217: rfp->rf_flags |= RCS_SLOCK;
1.1 jfb 2218:
2219: type = rcs_gettok(rfp);
2220: if (type != RCS_TOK_SCOLON) {
1.50 jfb 2221: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2222: cvs_log(LP_ERR,
2223: "missing semi-colon after `strict' keyword");
2224: return (-1);
2225: }
2226: }
2227:
2228: return (0);
2229: }
2230:
2231: /*
2232: * rcs_parse_branches()
2233: *
2234: * Parse the list of branches following a `branches' keyword in a delta.
2235: * Returns 0 on success, or -1 on failure.
2236: */
2237: static int
2238: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
2239: {
2240: int type;
2241: struct rcs_branch *brp;
2242:
2243: for (;;) {
2244: type = rcs_gettok(rfp);
2245: if (type == RCS_TOK_SCOLON)
2246: break;
2247:
2248: if (type != RCS_TOK_NUM) {
1.50 jfb 2249: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2250: cvs_log(LP_ERR,
2251: "unexpected token `%s' in list of branches",
2252: RCS_TOKSTR(rfp));
2253: return (-1);
2254: }
2255:
1.110 joris 2256: brp = (struct rcs_branch *)xmalloc(sizeof(*brp));
1.46 jfb 2257: brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11 joris 2258: if (brp->rb_num == NULL) {
1.110 joris 2259: xfree(brp);
1.11 joris 2260: return (-1);
2261: }
1.1 jfb 2262:
2263: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
2264: }
2265:
2266: return (0);
2267: }
2268:
2269: /*
2270: * rcs_freedelta()
2271: *
2272: * Free the contents of a delta structure.
2273: */
1.18 jfb 2274: static void
1.1 jfb 2275: rcs_freedelta(struct rcs_delta *rdp)
2276: {
1.12 jfb 2277: struct rcs_branch *rb;
1.1 jfb 2278: struct rcs_delta *crdp;
2279:
1.12 jfb 2280: if (rdp->rd_num != NULL)
2281: rcsnum_free(rdp->rd_num);
2282: if (rdp->rd_next != NULL)
2283: rcsnum_free(rdp->rd_next);
2284:
1.1 jfb 2285: if (rdp->rd_author != NULL)
1.110 joris 2286: xfree(rdp->rd_author);
1.109 joris 2287: if (rdp->rd_locker != NULL)
1.110 joris 2288: xfree(rdp->rd_locker);
1.1 jfb 2289: if (rdp->rd_state != NULL)
1.110 joris 2290: xfree(rdp->rd_state);
1.1 jfb 2291: if (rdp->rd_log != NULL)
1.110 joris 2292: xfree(rdp->rd_log);
1.1 jfb 2293: if (rdp->rd_text != NULL)
1.110 joris 2294: xfree(rdp->rd_text);
1.12 jfb 2295:
2296: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
2297: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
2298: rcsnum_free(rb->rb_num);
1.110 joris 2299: xfree(rb);
1.12 jfb 2300: }
1.1 jfb 2301:
2302: while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
2303: TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
2304: rcs_freedelta(crdp);
2305: }
2306:
1.110 joris 2307: xfree(rdp);
1.1 jfb 2308: }
2309:
2310: /*
2311: * rcs_freepdata()
2312: *
2313: * Free the contents of the parser data structure.
2314: */
2315: static void
2316: rcs_freepdata(struct rcs_pdata *pd)
2317: {
2318: if (pd->rp_file != NULL)
2319: (void)fclose(pd->rp_file);
2320: if (pd->rp_buf != NULL)
1.110 joris 2321: xfree(pd->rp_buf);
2322: xfree(pd);
1.1 jfb 2323: }
2324:
2325: /*
2326: * rcs_gettok()
2327: *
2328: * Get the next RCS token from the string <str>.
2329: */
2330: static int
2331: rcs_gettok(RCSFILE *rfp)
2332: {
2333: u_int i;
2334: int ch, last, type;
1.18 jfb 2335: size_t len;
2336: char *bp;
1.1 jfb 2337: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2338:
2339: type = RCS_TOK_ERR;
2340: bp = pdp->rp_buf;
1.42 jfb 2341: pdp->rp_tlen = 0;
1.1 jfb 2342: *bp = '\0';
2343:
2344: if (pdp->rp_pttype != RCS_TOK_ERR) {
2345: type = pdp->rp_pttype;
2346: strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
2347: pdp->rp_pttype = RCS_TOK_ERR;
2348: return (type);
2349: }
2350:
2351: /* skip leading whitespace */
2352: /* XXX we must skip backspace too for compatibility, should we? */
2353: do {
2354: ch = getc(pdp->rp_file);
2355: if (ch == '\n')
1.18 jfb 2356: pdp->rp_lines++;
1.1 jfb 2357: } while (isspace(ch));
2358:
2359: if (ch == EOF) {
2360: type = RCS_TOK_EOF;
1.14 deraadt 2361: } else if (ch == ';') {
1.1 jfb 2362: type = RCS_TOK_SCOLON;
1.14 deraadt 2363: } else if (ch == ':') {
1.1 jfb 2364: type = RCS_TOK_COLON;
1.14 deraadt 2365: } else if (isalpha(ch)) {
1.31 jfb 2366: type = RCS_TOK_ID;
1.1 jfb 2367: *(bp++) = ch;
1.18 jfb 2368: for (;;) {
1.1 jfb 2369: ch = getc(pdp->rp_file);
1.11 joris 2370: if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1 jfb 2371: ungetc(ch, pdp->rp_file);
2372: break;
2373: }
2374: *(bp++) = ch;
1.42 jfb 2375: pdp->rp_tlen++;
1.18 jfb 2376: if (bp == pdp->rp_bufend - 1) {
2377: len = bp - pdp->rp_buf;
2378: if (rcs_growbuf(rfp) < 0) {
2379: type = RCS_TOK_ERR;
2380: break;
2381: }
2382: bp = pdp->rp_buf + len;
2383: }
1.1 jfb 2384: }
2385: *bp = '\0';
2386:
1.18 jfb 2387: if (type != RCS_TOK_ERR) {
2388: for (i = 0; i < RCS_NKEYS; i++) {
2389: if (strcmp(rcs_keys[i].rk_str,
2390: pdp->rp_buf) == 0) {
2391: type = rcs_keys[i].rk_id;
2392: break;
2393: }
1.1 jfb 2394: }
2395: }
1.14 deraadt 2396: } else if (ch == '@') {
1.1 jfb 2397: /* we have a string */
1.18 jfb 2398: type = RCS_TOK_STRING;
1.1 jfb 2399: for (;;) {
2400: ch = getc(pdp->rp_file);
2401: if (ch == '@') {
2402: ch = getc(pdp->rp_file);
2403: if (ch != '@') {
2404: ungetc(ch, pdp->rp_file);
2405: break;
2406: }
1.14 deraadt 2407: } else if (ch == '\n')
1.18 jfb 2408: pdp->rp_lines++;
1.1 jfb 2409:
2410: *(bp++) = ch;
1.42 jfb 2411: pdp->rp_tlen++;
1.18 jfb 2412: if (bp == pdp->rp_bufend - 1) {
2413: len = bp - pdp->rp_buf;
2414: if (rcs_growbuf(rfp) < 0) {
2415: type = RCS_TOK_ERR;
2416: break;
2417: }
2418: bp = pdp->rp_buf + len;
2419: }
1.1 jfb 2420: }
2421:
2422: *bp = '\0';
1.14 deraadt 2423: } else if (isdigit(ch)) {
1.1 jfb 2424: *(bp++) = ch;
2425: last = ch;
2426: type = RCS_TOK_NUM;
2427:
2428: for (;;) {
2429: ch = getc(pdp->rp_file);
1.18 jfb 2430: if (bp == pdp->rp_bufend)
1.1 jfb 2431: break;
2432: if (!isdigit(ch) && ch != '.') {
2433: ungetc(ch, pdp->rp_file);
2434: break;
2435: }
2436:
2437: if (last == '.' && ch == '.') {
2438: type = RCS_TOK_ERR;
2439: break;
2440: }
2441: last = ch;
2442: *(bp++) = ch;
1.42 jfb 2443: pdp->rp_tlen++;
1.1 jfb 2444: }
1.18 jfb 2445: *bp = '\0';
1.1 jfb 2446: }
2447:
2448: return (type);
2449: }
2450:
2451: /*
2452: * rcs_pushtok()
2453: *
2454: * Push a token back in the parser's token buffer.
2455: */
2456: static int
2457: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2458: {
2459: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2460:
2461: if (pdp->rp_pttype != RCS_TOK_ERR)
2462: return (-1);
2463:
2464: pdp->rp_pttype = type;
2465: strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
2466: return (0);
2467: }
2468:
1.18 jfb 2469:
2470: /*
2471: * rcs_growbuf()
2472: *
2473: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2474: * RCS_BUFEXTSIZE.
2475: * In case of failure, the original buffer is left unmodified.
2476: * Returns 0 on success, or -1 on failure.
2477: */
2478: static int
2479: rcs_growbuf(RCSFILE *rf)
2480: {
2481: void *tmp;
2482: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2483:
1.119 niallo 2484: tmp = xrealloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
1.18 jfb 2485: if (tmp == NULL) {
1.50 jfb 2486: rcs_errno = RCS_ERR_ERRNO;
1.18 jfb 2487: cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
2488: return (-1);
2489: }
2490:
2491: pdp->rp_buf = (char *)tmp;
2492: pdp->rp_blen += RCS_BUFEXTSIZE;
2493: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42 jfb 2494:
2495: return (0);
2496: }
2497:
2498: /*
2499: * rcs_strprint()
2500: *
2501: * Output an RCS string <str> of size <slen> to the stream <stream>. Any
2502: * '@' characters are escaped. Otherwise, the string can contain arbitrary
2503: * binary data.
2504: */
2505: static int
2506: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
2507: {
2508: const u_char *ap, *ep, *sp;
2509: size_t ret;
1.52 jfb 2510:
2511: if (slen == 0)
2512: return (0);
1.42 jfb 2513:
2514: ep = str + slen - 1;
2515:
2516: for (sp = str; sp <= ep;) {
2517: ap = memchr(sp, '@', ep - sp);
2518: if (ap == NULL)
2519: ap = ep;
2520: ret = fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
2521:
2522: if (*ap == '@')
2523: putc('@', stream);
2524: sp = ap + 1;
1.63 joris 2525: }
2526:
2527: return (0);
2528: }
2529:
2530: /*
2531: * rcs_expand_keywords()
2532: *
1.125 niallo 2533: * Return expansion any RCS keywords in <data>
2534: *
2535: * On error, return NULL.
1.63 joris 2536: */
1.125 niallo 2537: static char *
2538: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *data,
1.63 joris 2539: size_t len, int mode)
2540: {
1.125 niallo 2541: int kwtype, sizdiff;
2542: u_int i, j, found, start_offset, c_offset;
2543: char *c, *kwstr, *start, *end, *tbuf;
2544: char expbuf[256], buf[256];
1.63 joris 2545:
1.65 niallo 2546: kwtype = 0;
2547: kwstr = NULL;
1.63 joris 2548: i = 0;
2549:
2550: /*
2551: * Keyword formats:
2552: * $Keyword$
2553: * $Keyword: value$
2554: */
1.125 niallo 2555: for (c = data; *c != '\0' && i < len; *c++) {
1.63 joris 2556: if (*c == '$') {
2557: /* remember start of this possible keyword */
2558: start = c;
1.125 niallo 2559: start_offset = start - data;
1.63 joris 2560:
2561: /* first following character has to be alphanumeric */
2562: *c++;
2563: if (!isalpha(*c)) {
2564: c = start;
2565: continue;
2566: }
2567:
2568: /* look for any matching keywords */
2569: found = 0;
2570: for (j = 0; j < RCS_NKWORDS; j++) {
2571: if (!strncmp(c, rcs_expkw[j].kw_str,
2572: strlen(rcs_expkw[j].kw_str))) {
2573: found = 1;
2574: kwstr = rcs_expkw[j].kw_str;
2575: kwtype = rcs_expkw[j].kw_type;
2576: break;
2577: }
2578: }
2579:
2580: /* unknown keyword, continue looking */
2581: if (found == 0) {
2582: c = start;
2583: continue;
2584: }
2585:
2586: /* next character has to be ':' or '$' */
2587: c += strlen(kwstr);
2588: if (*c != ':' && *c != '$') {
2589: c = start;
2590: continue;
2591: }
2592:
2593: /*
2594: * if the next character was ':' we need to look for
2595: * an '$' before the end of the line to be sure it is
2596: * in fact a keyword.
2597: */
2598: if (*c == ':') {
2599: while (*c++) {
2600: if (*c == '$' || *c == '\n')
2601: break;
2602: }
2603:
2604: if (*c != '$') {
2605: c = start;
2606: continue;
2607: }
2608: }
1.125 niallo 2609: c_offset = c - data;
2610: end = c + 1;
1.63 joris 2611:
2612: /* start constructing the expansion */
2613: expbuf[0] = '\0';
2614:
2615: if (mode & RCS_KWEXP_NAME) {
2616: strlcat(expbuf, "$", sizeof(expbuf));
2617: strlcat(expbuf, kwstr, sizeof(expbuf));
2618: if (mode & RCS_KWEXP_VAL)
2619: strlcat(expbuf, ": ", sizeof(expbuf));
2620: }
2621:
2622: /*
1.80 reyk 2623: * order matters because of RCS_KW_ID and
2624: * RCS_KW_HEADER here
1.63 joris 2625: */
2626: if (mode & RCS_KWEXP_VAL) {
2627: if (kwtype & RCS_KW_RCSFILE) {
2628: if (!(kwtype & RCS_KW_FULLPATH))
1.80 reyk 2629: strlcat(expbuf,
2630: basename(rcsfile),
1.63 joris 2631: sizeof(expbuf));
2632: else
2633: strlcat(expbuf, rcsfile,
2634: sizeof(expbuf));
2635: strlcat(expbuf, " ", sizeof(expbuf));
2636: }
2637:
2638: if (kwtype & RCS_KW_REVISION) {
1.80 reyk 2639: rcsnum_tostr(rdp->rd_num, buf,
2640: sizeof(buf));
1.63 joris 2641: strlcat(buf, " ", sizeof(buf));
2642: strlcat(expbuf, buf, sizeof(expbuf));
2643: }
2644:
2645: if (kwtype & RCS_KW_DATE) {
2646: strftime(buf, sizeof(buf),
1.80 reyk 2647: "%Y/%m/%d %H:%M:%S ",
2648: &rdp->rd_date);
1.63 joris 2649: strlcat(expbuf, buf, sizeof(expbuf));
2650: }
2651:
2652: if (kwtype & RCS_KW_AUTHOR) {
2653: strlcat(expbuf, rdp->rd_author,
2654: sizeof(expbuf));
2655: strlcat(expbuf, " ", sizeof(expbuf));
2656: }
2657:
2658: if (kwtype & RCS_KW_STATE) {
2659: strlcat(expbuf, rdp->rd_state,
2660: sizeof(expbuf));
2661: strlcat(expbuf, " ", sizeof(expbuf));
2662: }
2663:
2664: /* order does not matter anymore below */
2665: if (kwtype & RCS_KW_LOG)
2666: strlcat(expbuf, " ", sizeof(expbuf));
2667:
2668: if (kwtype & RCS_KW_SOURCE) {
1.80 reyk 2669: strlcat(expbuf, rcsfile,
2670: sizeof(expbuf));
1.63 joris 2671: strlcat(expbuf, " ", sizeof(expbuf));
2672: }
2673:
2674: if (kwtype & RCS_KW_NAME)
2675: strlcat(expbuf, " ", sizeof(expbuf));
2676: }
2677:
2678: /* end the expansion */
2679: if (mode & RCS_KWEXP_NAME)
2680: strlcat(expbuf, "$", sizeof(expbuf));
1.125 niallo 2681:
2682: sizdiff = strlen(expbuf) - (end - start);
2683: tbuf = xmalloc(strlen(end) + 1);
2684: strlcpy(tbuf, end, strlen(end) + 1);
2685: /* only realloc if we have to */
2686: if (sizdiff > 0) {
2687: len += sizdiff;
2688: data = xrealloc(data, len);
2689: /*
2690: * ensure string pointers are not invalidated
2691: * after realloc()
2692: */
2693: start = data + start_offset;
2694: c = data + c_offset;
2695: }
2696: strlcpy(start, expbuf, len);
2697: strlcat(data, tbuf, len);
2698: xfree(tbuf);
1.63 joris 2699: i += strlen(expbuf);
2700: }
1.42 jfb 2701: }
1.81 niallo 2702:
1.125 niallo 2703: return (data);
1.81 niallo 2704: }
2705:
2706: /*
2707: * rcs_deltatext_set()
2708: *
2709: * Set deltatext for <rev> in RCS file <rfp> to <dtext>
1.96 xsa 2710: * Returns -1 on error, 0 on success.
1.81 niallo 2711: */
2712: int
2713: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, const char *dtext)
2714: {
2715: size_t len;
2716: struct rcs_delta *rdp;
1.117 niallo 2717:
2718: /* Write operations require full parsing */
2719: rcs_parse_deltatexts(rfp, NULL);
1.81 niallo 2720:
2721: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2722: return (-1);
2723:
2724: if (rdp->rd_text != NULL)
1.110 joris 2725: xfree(rdp->rd_text);
1.81 niallo 2726:
2727: len = strlen(dtext);
1.103 joris 2728: if (len != 0) {
1.110 joris 2729: rdp->rd_text = (u_char *)xmalloc(len);
1.103 joris 2730: rdp->rd_tlen = len - 1;
2731: strlcpy(rdp->rd_text, dtext, len);
2732: } else {
2733: rdp->rd_text = NULL;
2734: rdp->rd_tlen = 0;
2735: }
1.18 jfb 2736:
1.86 joris 2737: return (0);
2738: }
2739:
2740: /*
2741: * rcs_rev_setlog()
2742: *
2743: * Sets the log message of revision <rev> to <logtext>
2744: */
2745: int
2746: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
2747: {
2748: struct rcs_delta *rdp;
2749: char buf[16];
2750:
2751: rcsnum_tostr(rev, buf, sizeof(buf));
2752:
2753: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2754: return (-1);
2755:
2756: if (rdp->rd_log != NULL)
1.110 joris 2757: xfree(rdp->rd_log);
1.86 joris 2758:
1.110 joris 2759: rdp->rd_log = xstrdup(logtext);
1.86 joris 2760: rfp->rf_flags &= ~RCS_SYNCED;
1.95 niallo 2761: return (0);
2762: }
1.97 niallo 2763: /*
2764: * rcs_rev_getdate()
2765: *
2766: * Get the date corresponding to a given revision.
2767: * Returns the date on success, -1 on failure.
2768: */
2769: time_t
2770: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
2771: {
2772: struct rcs_delta *rdp;
2773:
2774: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2775: return (-1);
2776:
2777: return (mktime(&rdp->rd_date));
2778: }
1.95 niallo 2779:
2780: /*
2781: * rcs_state_set()
2782: *
2783: * Sets the state of revision <rev> to <state>
2784: * NOTE: default state is 'Exp'. States may not contain spaces.
2785: *
2786: * Returns -1 on failure, 0 on success.
2787: */
2788: int
2789: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
2790: {
2791: struct rcs_delta *rdp;
2792:
2793: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2794: return (-1);
2795:
2796: if (rdp->rd_state != NULL)
1.110 joris 2797: xfree(rdp->rd_state);
1.95 niallo 2798:
1.110 joris 2799: rdp->rd_state = xstrdup(state);
1.95 niallo 2800:
2801: rfp->rf_flags &= ~RCS_SYNCED;
2802:
2803: return (0);
2804: }
2805:
2806: /*
2807: * rcs_state_check()
2808: *
2809: * Check if string <state> is valid.
2810: *
1.96 xsa 2811: * Returns 0 if the string is valid, -1 otherwise.
1.95 niallo 2812: */
2813: int
2814: rcs_state_check(const char *state)
2815: {
2816: if (strchr(state, ' ') != NULL)
2817: return (-1);
2818:
1.18 jfb 2819: return (0);
1.1 jfb 2820: }
1.97 niallo 2821:
2822: /*
2823: * rcs_state_get()
2824: *
2825: * Get the state for a given revision of a specified RCSFILE.
2826: *
2827: * Returns NULL on failure.
2828: */
2829: const char *
2830: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
2831: {
2832: struct rcs_delta *rdp;
2833:
2834: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2835: return (NULL);
2836:
2837: return (rdp->rd_state);
2838: }
2839:
1.131 ! niallo 2840: /*
! 2841: * rcs_kwexp_buf()
! 2842: *
! 2843: * Do keyword expansion on a buffer if necessary
! 2844: *
! 2845: */
! 2846: BUF *
! 2847: rcs_kwexp_buf(BUF *bp, RCSFILE *rf, RCSNUM *rev)
! 2848: {
! 2849: struct rcs_delta *rdp;
! 2850: char *expanded, *tbuf;
! 2851: int expmode;
! 2852: size_t len;
! 2853:
! 2854: /*
! 2855: * Do keyword expansion if required.
! 2856: */
! 2857: if (rf->rf_expand != NULL)
! 2858: expmode = rcs_kwexp_get(rf);
! 2859: else
! 2860: expmode = RCS_KWEXP_DEFAULT;
! 2861:
! 2862: if (!(expmode & RCS_KWEXP_NONE)) {
! 2863: if ((rdp = rcs_findrev(rf, rev)) == NULL)
! 2864: fatal("could not fetch revision");
! 2865: cvs_buf_putc(bp, '\0');
! 2866: len = cvs_buf_len(bp);
! 2867: tbuf = cvs_buf_release(bp);
! 2868: expanded = rcs_expand_keywords(rf->rf_path, rdp,
! 2869: tbuf, len, expmode);
! 2870: bp = cvs_buf_alloc(len, BUF_AUTOEXT);
! 2871: cvs_buf_set(bp, expanded, strlen(expanded), 0);
! 2872: xfree(expanded);
! 2873: }
! 2874: return (bp);
! 2875: }
! 2876:
1.105 joris 2877:
1.106 joris 2878: #if !defined(RCSPROG)
2879:
1.105 joris 2880: static char *month_tab[] = {
2881: "Jan",
2882: "Feb",
2883: "Mar",
2884: "Apr",
2885: "May",
2886: "Jun",
2887: "Jul",
2888: "Aug",
2889: "Sep",
2890: "Oct",
2891: "Nov",
2892: "Dec"
2893: };
2894:
2895: /*
2896: * Checkout a certain revision <rev> of RCS file <rf> to either standard
2897: * output when running in server mode, or to <fpath> when running in local mode.
2898: *
2899: * If type is CHECKOUT_REV_MERGED we have an extra argument, which
2900: * is the buffer containing the merged file.
2901: *
2902: * If type is CHECKOUT_REV_REMOVED, the file has been removed and we
2903: * need to do the same thing.
2904: */
2905: int
2906: cvs_checkout_rev(RCSFILE *rf, RCSNUM *rev, CVSFILE *cf, char *fpath,
2907: int local, int type, ...)
2908: {
2909: BUF *bp;
2910: int l, ret, fsize;
2911: char timebuf[32], entry[MAXPATHLEN], copyfile[MAXPATHLEN];
2912: char *content, *repo, buf[MAXPATHLEN], modestr[16];
2913: struct cvsroot *root;
2914: struct cvs_ent *ent;
2915: va_list ap;
2916: time_t rcstime;
2917: struct timeval tv[2];
2918: struct tm *tp;
2919: RCSNUM *oldrev;
2920:
2921: bp = NULL;
2922: ret = -1;
2923: content = NULL;
2924: oldrev = NULL;
2925:
2926: if ((type != CHECKOUT_REV_MERGED) && (type != CHECKOUT_REV_REMOVED)) {
2927: /* fetch the contents of the revision */
2928: if ((bp = rcs_getrev(rf, rev)) == NULL) {
2929: cvs_log(LP_ERR, "revision '%s' not found in file '%s'",
2930: rcsnum_tostr(rev, buf, sizeof(buf)), fpath);
2931: goto out;
2932: }
1.131 ! niallo 2933: bp = rcs_kwexp_buf(bp, rf, rev);
1.105 joris 2934: } else if (type != CHECKOUT_REV_REMOVED) {
2935: va_start(ap, type);
2936: bp = va_arg(ap, BUF *);
2937: va_end(ap);
2938: }
2939:
2940: if (type == CHECKOUT_REV_CREATED)
2941: rcstime = rcs_rev_getdate(rf, rev);
1.108 joris 2942: else if (type == CHECKOUT_REV_MERGED ||
2943: type == CHECKOUT_REV_UPDATED) {
1.105 joris 2944: time(&rcstime);
1.108 joris 2945: if ((rcstime = cvs_hack_time(rcstime, 1)) < 0)
2946: goto out;
2947: }
1.105 joris 2948:
1.108 joris 2949: if (type == CHECKOUT_REV_CREATED ||
1.130 niallo 2950: type == CHECKOUT_REV_MERGED ||
1.108 joris 2951: type == CHECKOUT_REV_UPDATED) {
1.105 joris 2952: ctime_r(&rcstime, timebuf);
2953: l = strlen(timebuf);
2954: if ((l > 0) && (timebuf[l - 1] == '\n'))
2955: timebuf[--l] = '\0';
2956:
2957: l = snprintf(entry, sizeof(entry), "/%s/%s/%s/%s/", cf->cf_name,
2958: rcsnum_tostr(rev, buf, sizeof(buf)),
2959: (local == 1) ? timebuf : "",
2960: (type == CHECKOUT_REV_MERGED) ? "+=" : "");
2961: if (l == -1 || l >= (int)sizeof(buf))
2962: goto out;
2963: }
2964:
2965: if (type == CHECKOUT_REV_MERGED) {
1.111 joris 2966: oldrev = rcsnum_alloc();
2967: rcsnum_cpy(rev, oldrev, 0);
1.105 joris 2968:
2969: if (rcsnum_dec(oldrev) == NULL)
2970: goto out;
2971:
2972: l = snprintf(copyfile, sizeof(copyfile), ".#%s.%s",
2973: cf->cf_name, rcsnum_tostr(oldrev, buf, sizeof(buf)));
2974: if (l == -1 || l >= (int)sizeof(copyfile))
2975: goto out;
2976: }
2977:
2978: root = CVS_DIR_ROOT(cf);
2979: repo = CVS_DIR_REPO(cf);
2980:
2981: /*
2982: * In local mode, just copy the entire contents to fpath.
2983: * In server mode, we need to send it to the client together with
2984: * some responses.
2985: */
2986: if (local) {
2987: l = 0;
2988: if (cf->cf_entry == NULL) {
2989: l = 1;
2990: cf->cf_entry = cvs_ent_open(cf->cf_dir, O_RDWR);
2991: if (cf->cf_entry == NULL) {
1.122 reyk 2992: cvs_log(LP_ERR, "failed to open Entry "
2993: "file '%s'", cf->cf_dir);
1.105 joris 2994: goto out;
2995: }
2996: }
2997:
2998: cvs_ent_remove(cf->cf_entry, cf->cf_name, 1);
2999: if (type != CHECKOUT_REV_REMOVED) {
3000: cvs_ent_addln(cf->cf_entry, entry);
3001: ent = cvs_ent_get(cf->cf_entry, cf->cf_name);
3002: ent->processed = 1;
3003: }
3004:
3005: if (l == 1)
3006: cvs_ent_close(cf->cf_entry);
3007:
3008: switch (type) {
3009: case CHECKOUT_REV_REMOVED:
3010: if (cvs_unlink(fpath) < 0)
3011: goto out;
3012: break;
3013: case CHECKOUT_REV_MERGED:
3014: /* XXX move the old file when merging */
1.108 joris 3015: case CHECKOUT_REV_UPDATED:
1.105 joris 3016: case CHECKOUT_REV_CREATED:
1.114 xsa 3017: cvs_buf_write(bp, fpath, cf->cf_mode);
1.107 joris 3018: /*
3019: * correct the time first
3020: */
3021: if ((rcstime = cvs_hack_time(rcstime, 0)) == 0)
3022: goto out;
3023:
1.105 joris 3024: tv[0].tv_sec = rcstime;
3025: tv[0].tv_usec = 0;
3026: tv[1] = tv[0];
3027: if (utimes(fpath, tv) == -1)
3028: cvs_log(LP_ERRNO, "failed to set timestamps");
3029: break;
3030: }
3031: } else {
3032: /* sanity */
3033: if (cf->cf_type != DT_REG) {
3034: cvs_log(LP_ERR, "cvs_checkout_rev: none DT_REG file");
3035: goto out;
3036: }
3037:
3038: /*
3039: * if we are removing a file, we don't need this stuff.
3040: */
3041: if (type != CHECKOUT_REV_REMOVED) {
1.107 joris 3042: if ((rcstime = cvs_hack_time(rcstime, 0)) == 0)
3043: goto out;
3044:
1.105 joris 3045: tp = gmtime(&rcstime);
3046: l = snprintf(timebuf, sizeof(timebuf),
3047: "%02d %s %d %02d:%02d:%02d -0000",
3048: tp->tm_mday, month_tab[tp->tm_mon],
3049: tp->tm_year + 1900, tp->tm_hour,
3050: tp->tm_min, tp->tm_sec);
3051: if (l == -1 || l >= (int)sizeof(timebuf))
3052: goto out;
3053:
3054: fsize = cvs_buf_len(bp);
3055: cvs_modetostr(cf->cf_mode, modestr, sizeof(modestr));
1.114 xsa 3056: cvs_buf_putc(bp, '\0');
1.105 joris 3057: content = cvs_buf_release(bp);
3058: bp = NULL;
3059: }
3060:
3061: if (type == CHECKOUT_REV_MERGED) {
3062: printf("Copy-file %s/\n", (cf->cf_dir != NULL) ?
3063: cf->cf_dir : ".");
3064: printf("%s/%s/%s\n", root->cr_dir, repo, cf->cf_name);
3065: printf("%s\n", copyfile);
3066: }
3067:
3068: switch (type) {
3069: case CHECKOUT_REV_MERGED:
3070: printf("Merged");
3071: break;
3072: case CHECKOUT_REV_REMOVED:
3073: printf("Removed");
3074: break;
3075: case CHECKOUT_REV_CREATED:
3076: printf("Mod-time %s\n", timebuf);
3077: printf("Created");
3078: break;
3079: default:
3080: cvs_log(LP_ERR, "cvs_checkout_rev: bad type %d",
3081: type);
3082: goto out;
3083: }
3084:
3085: printf(" %s/\n", (cf->cf_dir != NULL) ? cf->cf_dir : ".");
3086: printf("%s/%s\n", repo, cf->cf_name);
3087:
3088: if (type != CHECKOUT_REV_REMOVED) {
3089: printf("%s\n", entry);
3090: printf("%s\n%d\n%s", modestr, fsize, content);
3091: }
3092: }
3093:
3094: ret = 0;
3095:
3096: out:
3097: if (oldrev != NULL)
3098: rcsnum_free(oldrev);
3099: if (bp != NULL)
3100: cvs_buf_free(bp);
3101: if (content != NULL)
1.110 joris 3102: xfree(content);
1.105 joris 3103:
3104: return (ret);
3105: }
1.106 joris 3106:
3107: #endif