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