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