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