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