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