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