Annotation of src/usr.bin/rcs/rcs.c, Revision 1.8
1.8 ! xsa 1: /* $OpenBSD: rcs.c,v 1.7 2006/05/04 07:06:58 xsa 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;
1.8 ! xsa 1393: if (rcs_diffreg(path_tmp1, path_tmp2, newdiff) == D_ERROR)
! 1394: errx(1, "rcs_diffreg failed");
1.1 joris 1395:
1396: newdeltatext = rcs_buf_release(newdiff);
1397: } else if (nextrdp == NULL && prevrdp != NULL) {
1398: newdeltatext = rcs_buf_release(prevbuf);
1399: }
1400:
1401: if (newdeltatext != NULL) {
1402: if (rcs_deltatext_set(rf, prevrdp->rd_num, newdeltatext) < 0)
1403: errx(1, "error setting new deltatext");
1404: }
1405:
1406: TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
1407:
1408: /* update pointers */
1409: if (prevrdp != NULL && nextrdp != NULL) {
1410: rcsnum_cpy(prevrdp->rd_num, nextrdp->rd_next, 0);
1411: } else if (prevrdp != NULL) {
1412: if (rcs_head_set(rf, prevrdp->rd_num) < 0)
1413: errx(1, "rcs_head_set failed");
1414: } else if (nextrdp != NULL) {
1415: rcsnum_free(nextrdp->rd_next);
1416: nextrdp->rd_next = rcsnum_alloc();
1417: } else {
1418: rcsnum_free(rf->rf_head);
1419: rf->rf_head = NULL;
1420: }
1421:
1422: rf->rf_ndelta--;
1423: rf->rf_flags &= ~RCS_SYNCED;
1424:
1425: rcs_freedelta(rdp);
1426:
1427: if (newdeltatext != NULL)
1428: xfree(newdeltatext);
1.7 xsa 1429:
1430: if (path_tmp1 != NULL)
1431: xfree(path_tmp1);
1432: if (path_tmp2 != NULL)
1433: xfree(path_tmp2);
1.1 joris 1434:
1435: return (0);
1436: }
1437:
1438: /*
1439: * rcs_findrev()
1440: *
1441: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1442: * The revision number is given in <rev>.
1443: *
1444: * If the given revision is a branch number, we translate it into the latest
1445: * revision on the branch.
1446: *
1447: * Returns a pointer to the delta on success, or NULL on failure.
1448: */
1449: struct rcs_delta *
1450: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
1451: {
1452: u_int cmplen;
1453: struct rcs_delta *rdp;
1454: RCSNUM *brev, *frev;
1455:
1456: /*
1457: * We need to do more parsing if the last revision in the linked list
1458: * is greater than the requested revision.
1459: */
1460: rdp = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1461: if (rdp == NULL ||
1462: rcsnum_cmp(rdp->rd_num, rev, 0) == -1) {
1463: rcs_parse_deltas(rfp, rev);
1464: }
1465:
1466: /*
1467: * Translate a branch into the latest revision on the branch itself.
1468: */
1469: if (RCSNUM_ISBRANCH(rev)) {
1470: brev = rcsnum_brtorev(rev);
1471: frev = brev;
1472: for (;;) {
1473: rdp = rcs_findrev(rfp, frev);
1474: if (rdp == NULL)
1475: return (NULL);
1476:
1477: if (rdp->rd_next->rn_len == 0)
1478: break;
1479:
1480: frev = rdp->rd_next;
1481: }
1482:
1483: rcsnum_free(brev);
1484: return (rdp);
1485: }
1486:
1487: cmplen = rev->rn_len;
1488:
1489: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1490: if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0)
1491: return (rdp);
1492: }
1493:
1494: return (NULL);
1495: }
1496:
1497: /*
1498: * rcs_kwexp_set()
1499: *
1500: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1501: */
1502: void
1503: rcs_kwexp_set(RCSFILE *file, int mode)
1504: {
1505: int i;
1506: char *tmp, buf[8] = "";
1507:
1508: if (RCS_KWEXP_INVAL(mode))
1509: return;
1510:
1511: i = 0;
1512: if (mode == RCS_KWEXP_NONE)
1513: buf[0] = 'b';
1514: else if (mode == RCS_KWEXP_OLD)
1515: buf[0] = 'o';
1516: else {
1517: if (mode & RCS_KWEXP_NAME)
1518: buf[i++] = 'k';
1519: if (mode & RCS_KWEXP_VAL)
1520: buf[i++] = 'v';
1521: if (mode & RCS_KWEXP_LKR)
1522: buf[i++] = 'l';
1523: }
1524:
1525: tmp = xstrdup(buf);
1526: if (file->rf_expand != NULL)
1527: xfree(file->rf_expand);
1528: file->rf_expand = tmp;
1529: /* not synced anymore */
1530: file->rf_flags &= ~RCS_SYNCED;
1531: }
1532:
1533: /*
1534: * rcs_kwexp_get()
1535: *
1536: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1537: */
1538: int
1539: rcs_kwexp_get(RCSFILE *file)
1540: {
1541: return rcs_kflag_get(file->rf_expand);
1542: }
1543:
1544: /*
1545: * rcs_kflag_get()
1546: *
1547: * Get the keyword expansion mode from a set of character flags given in
1548: * <flags> and return the appropriate flag mask. In case of an error, the
1549: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1550: */
1551: int
1552: rcs_kflag_get(const char *flags)
1553: {
1554: int fl;
1555: size_t len;
1556: const char *fp;
1557:
1558: fl = 0;
1559: len = strlen(flags);
1560:
1561: for (fp = flags; *fp != '\0'; fp++) {
1562: if (*fp == 'k')
1563: fl |= RCS_KWEXP_NAME;
1564: else if (*fp == 'v')
1565: fl |= RCS_KWEXP_VAL;
1566: else if (*fp == 'l')
1567: fl |= RCS_KWEXP_LKR;
1568: else if (*fp == 'o') {
1569: if (len != 1)
1570: fl |= RCS_KWEXP_ERR;
1571: fl |= RCS_KWEXP_OLD;
1572: } else if (*fp == 'b') {
1573: if (len != 1)
1574: fl |= RCS_KWEXP_ERR;
1575: } else /* unknown letter */
1576: fl |= RCS_KWEXP_ERR;
1577: }
1578:
1579: return (fl);
1580: }
1581:
1582: /*
1583: * rcs_errstr()
1584: *
1585: * Get the error string matching the RCS error code <code>.
1586: */
1587: const char *
1588: rcs_errstr(int code)
1589: {
1590: const char *esp;
1591:
1592: if (code < 0 || (code >= (int)RCS_NERR && code != RCS_ERR_ERRNO))
1593: esp = NULL;
1594: else if (code == RCS_ERR_ERRNO)
1595: esp = strerror(errno);
1596: else
1597: esp = rcs_errstrs[code];
1598: return (esp);
1599: }
1600:
1601: /* rcs_parse_deltas()
1602: *
1603: * Parse deltas. If <rev> is not NULL, parse only as far as that
1604: * revision. If <rev> is NULL, parse all deltas.
1605: */
1606: static void
1607: rcs_parse_deltas(RCSFILE *rfp, RCSNUM *rev)
1608: {
1609: int ret;
1610: struct rcs_delta *enddelta;
1611:
1612: if ((rfp->rf_flags & PARSED_DELTAS) || (rfp->rf_flags & RCS_CREATE))
1613: return;
1614:
1615: for (;;) {
1616: ret = rcs_parse_delta(rfp);
1617: if (rev != NULL) {
1618: enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1619: if (rcsnum_cmp(enddelta->rd_num, rev, 0) == 0)
1620: break;
1621: }
1622: if (ret == 0) {
1623: rfp->rf_flags |= PARSED_DELTAS;
1624: break;
1625: }
1626: else if (ret == -1)
1627: errx(1, "error parsing deltas");
1628: }
1629: }
1630:
1631: /* rcs_parse_deltatexts()
1632: *
1633: * Parse deltatexts. If <rev> is not NULL, parse only as far as that
1634: * revision. If <rev> is NULL, parse everything.
1635: */
1636: static void
1637: rcs_parse_deltatexts(RCSFILE *rfp, RCSNUM *rev)
1638: {
1639: int ret;
1640: struct rcs_delta *rdp;
1641:
1642: if ((rfp->rf_flags & PARSED_DELTATEXTS) ||
1643: (rfp->rf_flags & RCS_CREATE))
1644: return;
1645:
1646: if (!(rfp->rf_flags & PARSED_DESC))
1647: rcs_parse_desc(rfp, rev);
1648: for (;;) {
1649: if (rev != NULL) {
1650: rdp = rcs_findrev(rfp, rev);
1651: if (rdp->rd_text != NULL)
1652: break;
1653: else
1654: ret = rcs_parse_deltatext(rfp);
1655: } else
1656: ret = rcs_parse_deltatext(rfp);
1657: if (ret == 0) {
1658: rfp->rf_flags |= PARSED_DELTATEXTS;
1659: break;
1660: }
1661: else if (ret == -1)
1662: errx(1, "problem parsing deltatexts");
1663: }
1664: }
1665:
1666: /* rcs_parse_desc()
1667: *
1668: * Parse RCS description.
1669: */
1670: static void
1671: rcs_parse_desc(RCSFILE *rfp, RCSNUM *rev)
1672: {
1673: int ret = 0;
1674:
1675: if ((rfp->rf_flags & PARSED_DESC) || (rfp->rf_flags & RCS_CREATE))
1676: return;
1677: if (!(rfp->rf_flags & PARSED_DELTAS))
1678: rcs_parse_deltas(rfp, rev);
1679: /* do parsing */
1680: ret = rcs_gettok(rfp);
1681: if (ret != RCS_TOK_DESC)
1682: errx(1, "token `%s' found where RCS desc expected",
1683: RCS_TOKSTR(rfp));
1684:
1685: ret = rcs_gettok(rfp);
1686: if (ret != RCS_TOK_STRING)
1687: errx(1, "token `%s' found where RCS desc expected",
1688: RCS_TOKSTR(rfp));
1689:
1690: rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
1691: rfp->rf_flags |= PARSED_DESC;
1692: }
1693:
1694: /*
1695: * rcs_parse_init()
1696: *
1697: * Initial parsing of file <path>, which are in the RCS format.
1698: * Just does admin section.
1699: */
1700: static void
1701: rcs_parse_init(RCSFILE *rfp)
1702: {
1703: struct rcs_pdata *pdp;
1704:
1705: if (rfp->rf_flags & RCS_PARSED)
1706: return;
1707:
1708: pdp = xcalloc(1, sizeof(*pdp));
1709:
1710: pdp->rp_lines = 0;
1711: pdp->rp_pttype = RCS_TOK_ERR;
1712:
1.3 joris 1713: if ((pdp->rp_file = fdopen(rfp->fd, "r")) == NULL)
1714: err(1, "fopen: `%s'", rfp->rf_path);
1.1 joris 1715:
1716: pdp->rp_buf = xmalloc((size_t)RCS_BUFSIZE);
1717: pdp->rp_blen = RCS_BUFSIZE;
1718: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1719:
1720: /* ditch the strict lock */
1721: rfp->rf_flags &= ~RCS_SLOCK;
1722: rfp->rf_pdata = pdp;
1723:
1724: if (rcs_parse_admin(rfp) < 0) {
1725: rcs_freepdata(pdp);
1726: errx(1, "could not parse admin data");
1727: }
1728:
1729: if (rfp->rf_flags & RCS_PARSE_FULLY)
1730: rcs_parse_deltatexts(rfp, NULL);
1731:
1732: rfp->rf_flags |= RCS_SYNCED;
1733: }
1734:
1735: /*
1736: * rcs_parse_admin()
1737: *
1738: * Parse the administrative portion of an RCS file.
1739: * Returns the type of the first token found after the admin section on
1740: * success, or -1 on failure.
1741: */
1742: static int
1743: rcs_parse_admin(RCSFILE *rfp)
1744: {
1745: u_int i;
1746: int tok, ntok, hmask;
1747: struct rcs_key *rk;
1748:
1749: /* hmask is a mask of the headers already encountered */
1750: hmask = 0;
1751: for (;;) {
1752: tok = rcs_gettok(rfp);
1753: if (tok == RCS_TOK_ERR) {
1754: rcs_errno = RCS_ERR_PARSE;
1755: warnx("parse error in RCS admin section");
1756: goto fail;
1757: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1758: /*
1759: * Assume this is the start of the first delta or
1760: * that we are dealing with an empty RCS file and
1761: * we just found the description.
1762: */
1763: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1764: return (tok);
1765: }
1766:
1767: rk = NULL;
1768: for (i = 0; i < RCS_NKEYS; i++)
1769: if (rcs_keys[i].rk_id == tok)
1770: rk = &(rcs_keys[i]);
1771:
1772: if (hmask & (1 << tok)) {
1773: rcs_errno = RCS_ERR_PARSE;
1774: warnx("duplicate RCS key");
1775: goto fail;
1776: }
1777: hmask |= (1 << tok);
1778:
1779: switch (tok) {
1780: case RCS_TOK_HEAD:
1781: case RCS_TOK_BRANCH:
1782: case RCS_TOK_COMMENT:
1783: case RCS_TOK_EXPAND:
1784: ntok = rcs_gettok(rfp);
1785: if (ntok == RCS_TOK_SCOLON)
1786: break;
1787: if (ntok != rk->rk_val) {
1788: rcs_errno = RCS_ERR_PARSE;
1789: warnx("invalid value type for RCS key `%s'",
1790: rk->rk_str);
1791: }
1792:
1793: if (tok == RCS_TOK_HEAD) {
1794: if (rfp->rf_head == NULL)
1795: rfp->rf_head = rcsnum_alloc();
1796: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1797: rfp->rf_head);
1798: } else if (tok == RCS_TOK_BRANCH) {
1799: if (rfp->rf_branch == NULL)
1800: rfp->rf_branch = rcsnum_alloc();
1801: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1802: rfp->rf_branch) < 0)
1803: goto fail;
1804: } else if (tok == RCS_TOK_COMMENT) {
1805: rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
1806: } else if (tok == RCS_TOK_EXPAND) {
1807: rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
1808: }
1809:
1810: /* now get the expected semi-colon */
1811: ntok = rcs_gettok(rfp);
1812: if (ntok != RCS_TOK_SCOLON) {
1813: rcs_errno = RCS_ERR_PARSE;
1814: warnx("missing semi-colon after RCS `%s' key",
1815: rk->rk_str);
1816: goto fail;
1817: }
1818: break;
1819: case RCS_TOK_ACCESS:
1820: if (rcs_parse_access(rfp) < 0)
1821: goto fail;
1822: break;
1823: case RCS_TOK_SYMBOLS:
1824: if (rcs_parse_symbols(rfp) < 0)
1825: goto fail;
1826: break;
1827: case RCS_TOK_LOCKS:
1828: if (rcs_parse_locks(rfp) < 0)
1829: goto fail;
1830: break;
1831: default:
1832: rcs_errno = RCS_ERR_PARSE;
1833: warnx("unexpected token `%s' in RCS admin section",
1834: RCS_TOKSTR(rfp));
1835: goto fail;
1836: }
1837: }
1838:
1839: fail:
1840: return (-1);
1841: }
1842:
1843: /*
1844: * rcs_parse_delta()
1845: *
1846: * Parse an RCS delta section and allocate the structure to store that delta's
1847: * information in the <rfp> delta list.
1848: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1849: * -1 on error.
1850: */
1851: static int
1852: rcs_parse_delta(RCSFILE *rfp)
1853: {
1854: int ret, tok, ntok, hmask;
1855: u_int i;
1856: char *tokstr;
1857: RCSNUM *datenum;
1858: struct rcs_delta *rdp;
1859: struct rcs_key *rk;
1860:
1861: rdp = xcalloc(1, sizeof(*rdp));
1862:
1863: rdp->rd_num = rcsnum_alloc();
1864: rdp->rd_next = rcsnum_alloc();
1865:
1866: TAILQ_INIT(&(rdp->rd_branches));
1867:
1868: tok = rcs_gettok(rfp);
1869: if (tok == RCS_TOK_DESC) {
1870: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1871: return (0);
1872: } else if (tok != RCS_TOK_NUM) {
1873: rcs_errno = RCS_ERR_PARSE;
1874: warnx("unexpected token `%s' at start of delta",
1875: RCS_TOKSTR(rfp));
1876: rcs_freedelta(rdp);
1877: return (-1);
1878: }
1879: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1880:
1881: hmask = 0;
1882: ret = 0;
1883: tokstr = NULL;
1884:
1885: for (;;) {
1886: tok = rcs_gettok(rfp);
1887: if (tok == RCS_TOK_ERR) {
1888: rcs_errno = RCS_ERR_PARSE;
1889: warnx("parse error in RCS delta section");
1890: rcs_freedelta(rdp);
1891: return (-1);
1892: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1893: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1894: ret = (tok == RCS_TOK_NUM ? 1 : 0);
1895: break;
1896: }
1897:
1898: rk = NULL;
1899: for (i = 0; i < RCS_NKEYS; i++)
1900: if (rcs_keys[i].rk_id == tok)
1901: rk = &(rcs_keys[i]);
1902:
1903: if (hmask & (1 << tok)) {
1904: rcs_errno = RCS_ERR_PARSE;
1905: warnx("duplicate RCS key");
1906: rcs_freedelta(rdp);
1907: return (-1);
1908: }
1909: hmask |= (1 << tok);
1910:
1911: switch (tok) {
1912: case RCS_TOK_DATE:
1913: case RCS_TOK_AUTHOR:
1914: case RCS_TOK_STATE:
1915: case RCS_TOK_NEXT:
1916: ntok = rcs_gettok(rfp);
1917: if (ntok == RCS_TOK_SCOLON) {
1918: if (rk->rk_flags & RCS_VOPT)
1919: break;
1920: else {
1921: rcs_errno = RCS_ERR_PARSE;
1922: warnx("missing mandatory "
1923: "value to RCS key `%s'",
1924: rk->rk_str);
1925: rcs_freedelta(rdp);
1926: return (-1);
1927: }
1928: }
1929:
1930: if (ntok != rk->rk_val) {
1931: rcs_errno = RCS_ERR_PARSE;
1932: warnx("invalid value type for RCS key `%s'",
1933: rk->rk_str);
1934: rcs_freedelta(rdp);
1935: return (-1);
1936: }
1937:
1938: if (tokstr != NULL)
1939: xfree(tokstr);
1940: tokstr = xstrdup(RCS_TOKSTR(rfp));
1941: /* now get the expected semi-colon */
1942: ntok = rcs_gettok(rfp);
1943: if (ntok != RCS_TOK_SCOLON) {
1944: rcs_errno = RCS_ERR_PARSE;
1945: warnx("missing semi-colon after RCS `%s' key",
1946: rk->rk_str);
1947: xfree(tokstr);
1948: rcs_freedelta(rdp);
1949: return (-1);
1950: }
1951:
1952: if (tok == RCS_TOK_DATE) {
1953: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1954: xfree(tokstr);
1955: rcs_freedelta(rdp);
1956: return (-1);
1957: }
1958: if (datenum->rn_len != 6) {
1959: rcs_errno = RCS_ERR_PARSE;
1960: warnx("RCS date specification has %s "
1961: "fields",
1962: (datenum->rn_len > 6) ? "too many" :
1963: "missing");
1964: xfree(tokstr);
1965: rcs_freedelta(rdp);
1966: rcsnum_free(datenum);
1967: return (-1);
1968: }
1969: rdp->rd_date.tm_year = datenum->rn_id[0];
1970: if (rdp->rd_date.tm_year >= 1900)
1971: rdp->rd_date.tm_year -= 1900;
1972: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
1973: rdp->rd_date.tm_mday = datenum->rn_id[2];
1974: rdp->rd_date.tm_hour = datenum->rn_id[3];
1975: rdp->rd_date.tm_min = datenum->rn_id[4];
1976: rdp->rd_date.tm_sec = datenum->rn_id[5];
1977: rcsnum_free(datenum);
1978: } else if (tok == RCS_TOK_AUTHOR) {
1979: rdp->rd_author = tokstr;
1980: tokstr = NULL;
1981: } else if (tok == RCS_TOK_STATE) {
1982: rdp->rd_state = tokstr;
1983: tokstr = NULL;
1984: } else if (tok == RCS_TOK_NEXT) {
1985: rcsnum_aton(tokstr, NULL, rdp->rd_next);
1986: }
1987: break;
1988: case RCS_TOK_BRANCHES:
1989: if (rcs_parse_branches(rfp, rdp) < 0) {
1990: rcs_freedelta(rdp);
1991: return (-1);
1992: }
1993: break;
1994: default:
1995: rcs_errno = RCS_ERR_PARSE;
1996: warnx("unexpected token `%s' in RCS delta",
1997: RCS_TOKSTR(rfp));
1998: rcs_freedelta(rdp);
1999: return (-1);
2000: }
2001: }
2002:
2003: if (tokstr != NULL)
2004: xfree(tokstr);
2005:
2006: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
2007: rfp->rf_ndelta++;
2008:
2009: return (ret);
2010: }
2011:
2012: /*
2013: * rcs_parse_deltatext()
2014: *
2015: * Parse an RCS delta text section and fill in the log and text field of the
2016: * appropriate delta section.
2017: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
2018: * -1 on error.
2019: */
2020: static int
2021: rcs_parse_deltatext(RCSFILE *rfp)
2022: {
2023: int tok;
2024: RCSNUM *tnum;
2025: struct rcs_delta *rdp;
2026:
2027: tok = rcs_gettok(rfp);
2028: if (tok == RCS_TOK_EOF)
2029: return (0);
2030:
2031: if (tok != RCS_TOK_NUM) {
2032: rcs_errno = RCS_ERR_PARSE;
2033: warnx("unexpected token `%s' at start of RCS delta text",
2034: RCS_TOKSTR(rfp));
2035: return (-1);
2036: }
2037:
2038: tnum = rcsnum_alloc();
2039: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
2040:
2041: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
2042: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
2043: break;
2044: }
2045: rcsnum_free(tnum);
2046:
2047: if (rdp == NULL) {
2048: warnx("RCS delta text `%s' has no matching delta",
2049: RCS_TOKSTR(rfp));
2050: return (-1);
2051: }
2052:
2053: tok = rcs_gettok(rfp);
2054: if (tok != RCS_TOK_LOG) {
2055: rcs_errno = RCS_ERR_PARSE;
2056: warnx("unexpected token `%s' where RCS log expected",
2057: RCS_TOKSTR(rfp));
2058: return (-1);
2059: }
2060:
2061: tok = rcs_gettok(rfp);
2062: if (tok != RCS_TOK_STRING) {
2063: rcs_errno = RCS_ERR_PARSE;
2064: warnx("unexpected token `%s' where RCS log expected",
2065: RCS_TOKSTR(rfp));
2066: return (-1);
2067: }
2068: rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
2069: tok = rcs_gettok(rfp);
2070: if (tok != RCS_TOK_TEXT) {
2071: rcs_errno = RCS_ERR_PARSE;
2072: warnx("unexpected token `%s' where RCS text expected",
2073: RCS_TOKSTR(rfp));
2074: return (-1);
2075: }
2076:
2077: tok = rcs_gettok(rfp);
2078: if (tok != RCS_TOK_STRING) {
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: rdp->rd_text = xmalloc(RCS_TOKLEN(rfp) + 1);
1.5 ray 2086: if (strlcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp) + 1)) >=
2087: RCS_TOKLEN(rfp) + 1)
2088: errx(1, "rcs_parse_deltatext: strlcpy");
1.1 joris 2089: rdp->rd_tlen = RCS_TOKLEN(rfp);
2090:
2091: return (1);
2092: }
2093:
2094: /*
2095: * rcs_parse_access()
2096: *
2097: * Parse the access list given as value to the `access' keyword.
2098: * Returns 0 on success, or -1 on failure.
2099: */
2100: static int
2101: rcs_parse_access(RCSFILE *rfp)
2102: {
2103: int type;
2104:
2105: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
2106: if (type != RCS_TOK_ID) {
2107: rcs_errno = RCS_ERR_PARSE;
2108: warnx("unexpected token `%s' in access list",
2109: RCS_TOKSTR(rfp));
2110: return (-1);
2111: }
2112:
2113: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
2114: return (-1);
2115: }
2116:
2117: return (0);
2118: }
2119:
2120: /*
2121: * rcs_parse_symbols()
2122: *
2123: * Parse the symbol list given as value to the `symbols' keyword.
2124: * Returns 0 on success, or -1 on failure.
2125: */
2126: static int
2127: rcs_parse_symbols(RCSFILE *rfp)
2128: {
2129: int type;
2130: struct rcs_sym *symp;
2131:
2132: for (;;) {
2133: type = rcs_gettok(rfp);
2134: if (type == RCS_TOK_SCOLON)
2135: break;
2136:
2137: if (type != RCS_TOK_ID) {
2138: rcs_errno = RCS_ERR_PARSE;
2139: warnx("unexpected token `%s' in symbol list",
2140: RCS_TOKSTR(rfp));
2141: return (-1);
2142: }
2143:
2144: symp = xmalloc(sizeof(*symp));
2145: symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
2146: symp->rs_num = rcsnum_alloc();
2147:
2148: type = rcs_gettok(rfp);
2149: if (type != RCS_TOK_COLON) {
2150: rcs_errno = RCS_ERR_PARSE;
2151: warnx("unexpected token `%s' in symbol list",
2152: RCS_TOKSTR(rfp));
2153: rcsnum_free(symp->rs_num);
2154: xfree(symp->rs_name);
2155: xfree(symp);
2156: return (-1);
2157: }
2158:
2159: type = rcs_gettok(rfp);
2160: if (type != RCS_TOK_NUM) {
2161: rcs_errno = RCS_ERR_PARSE;
2162: warnx("unexpected token `%s' in symbol list",
2163: RCS_TOKSTR(rfp));
2164: rcsnum_free(symp->rs_num);
2165: xfree(symp->rs_name);
2166: xfree(symp);
2167: return (-1);
2168: }
2169:
2170: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
2171: warnx("failed to parse RCS NUM `%s'",
2172: RCS_TOKSTR(rfp));
2173: rcsnum_free(symp->rs_num);
2174: xfree(symp->rs_name);
2175: xfree(symp);
2176: return (-1);
2177: }
2178:
2179: TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
2180: }
2181:
2182: return (0);
2183: }
2184:
2185: /*
2186: * rcs_parse_locks()
2187: *
2188: * Parse the lock list given as value to the `locks' keyword.
2189: * Returns 0 on success, or -1 on failure.
2190: */
2191: static int
2192: rcs_parse_locks(RCSFILE *rfp)
2193: {
2194: int type;
2195: struct rcs_lock *lkp;
2196:
2197: for (;;) {
2198: type = rcs_gettok(rfp);
2199: if (type == RCS_TOK_SCOLON)
2200: break;
2201:
2202: if (type != RCS_TOK_ID) {
2203: rcs_errno = RCS_ERR_PARSE;
2204: warnx("unexpected token `%s' in lock list",
2205: RCS_TOKSTR(rfp));
2206: return (-1);
2207: }
2208:
2209: lkp = xmalloc(sizeof(*lkp));
2210: lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
2211: lkp->rl_num = rcsnum_alloc();
2212:
2213: type = rcs_gettok(rfp);
2214: if (type != RCS_TOK_COLON) {
2215: rcs_errno = RCS_ERR_PARSE;
2216: warnx("unexpected token `%s' in symbol list",
2217: RCS_TOKSTR(rfp));
2218: rcsnum_free(lkp->rl_num);
2219: xfree(lkp->rl_name);
2220: xfree(lkp);
2221: return (-1);
2222: }
2223:
2224: type = rcs_gettok(rfp);
2225: if (type != RCS_TOK_NUM) {
2226: rcs_errno = RCS_ERR_PARSE;
2227: warnx("unexpected token `%s' in symbol list",
2228: RCS_TOKSTR(rfp));
2229: rcsnum_free(lkp->rl_num);
2230: xfree(lkp->rl_name);
2231: xfree(lkp);
2232: return (-1);
2233: }
2234:
2235: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
2236: warnx("failed to parse RCS NUM `%s'",
2237: RCS_TOKSTR(rfp));
2238: rcsnum_free(lkp->rl_num);
2239: xfree(lkp->rl_name);
2240: xfree(lkp);
2241: return (-1);
2242: }
2243:
2244: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
2245: }
2246:
2247: /* check if we have a `strict' */
2248: type = rcs_gettok(rfp);
2249: if (type != RCS_TOK_STRICT) {
2250: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
2251: } else {
2252: rfp->rf_flags |= RCS_SLOCK;
2253:
2254: type = rcs_gettok(rfp);
2255: if (type != RCS_TOK_SCOLON) {
2256: rcs_errno = RCS_ERR_PARSE;
2257: warnx("missing semi-colon after `strict' keyword");
2258: return (-1);
2259: }
2260: }
2261:
2262: return (0);
2263: }
2264:
2265: /*
2266: * rcs_parse_branches()
2267: *
2268: * Parse the list of branches following a `branches' keyword in a delta.
2269: * Returns 0 on success, or -1 on failure.
2270: */
2271: static int
2272: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
2273: {
2274: int type;
2275: struct rcs_branch *brp;
2276:
2277: for (;;) {
2278: type = rcs_gettok(rfp);
2279: if (type == RCS_TOK_SCOLON)
2280: break;
2281:
2282: if (type != RCS_TOK_NUM) {
2283: rcs_errno = RCS_ERR_PARSE;
2284: warnx("unexpected token `%s' in list of branches",
2285: RCS_TOKSTR(rfp));
2286: return (-1);
2287: }
2288:
2289: brp = xmalloc(sizeof(*brp));
2290: brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
2291: if (brp->rb_num == NULL) {
2292: xfree(brp);
2293: return (-1);
2294: }
2295:
2296: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
2297: }
2298:
2299: return (0);
2300: }
2301:
2302: /*
2303: * rcs_freedelta()
2304: *
2305: * Free the contents of a delta structure.
2306: */
2307: static void
2308: rcs_freedelta(struct rcs_delta *rdp)
2309: {
2310: struct rcs_branch *rb;
2311:
2312: if (rdp->rd_num != NULL)
2313: rcsnum_free(rdp->rd_num);
2314: if (rdp->rd_next != NULL)
2315: rcsnum_free(rdp->rd_next);
2316:
2317: if (rdp->rd_author != NULL)
2318: xfree(rdp->rd_author);
2319: if (rdp->rd_locker != NULL)
2320: xfree(rdp->rd_locker);
2321: if (rdp->rd_state != NULL)
2322: xfree(rdp->rd_state);
2323: if (rdp->rd_log != NULL)
2324: xfree(rdp->rd_log);
2325: if (rdp->rd_text != NULL)
2326: xfree(rdp->rd_text);
2327:
2328: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
2329: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
2330: rcsnum_free(rb->rb_num);
2331: xfree(rb);
2332: }
2333:
2334: xfree(rdp);
2335: }
2336:
2337: /*
2338: * rcs_freepdata()
2339: *
2340: * Free the contents of the parser data structure.
2341: */
2342: static void
2343: rcs_freepdata(struct rcs_pdata *pd)
2344: {
2345: if (pd->rp_file != NULL)
2346: (void)fclose(pd->rp_file);
2347: if (pd->rp_buf != NULL)
2348: xfree(pd->rp_buf);
2349: xfree(pd);
2350: }
2351:
2352: /*
2353: * rcs_gettok()
2354: *
2355: * Get the next RCS token from the string <str>.
2356: */
2357: static int
2358: rcs_gettok(RCSFILE *rfp)
2359: {
2360: u_int i;
2361: int ch, last, type;
2362: size_t len;
2363: char *bp;
2364: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2365:
2366: type = RCS_TOK_ERR;
2367: bp = pdp->rp_buf;
2368: pdp->rp_tlen = 0;
2369: *bp = '\0';
2370:
2371: if (pdp->rp_pttype != RCS_TOK_ERR) {
2372: type = pdp->rp_pttype;
1.5 ray 2373: if (strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen) >=
2374: pdp->rp_blen)
2375: errx(1, "rcs_gettok: strlcpy");
1.1 joris 2376: pdp->rp_pttype = RCS_TOK_ERR;
2377: return (type);
2378: }
2379:
2380: /* skip leading whitespace */
2381: /* XXX we must skip backspace too for compatibility, should we? */
2382: do {
2383: ch = getc(pdp->rp_file);
2384: if (ch == '\n')
2385: pdp->rp_lines++;
2386: } while (isspace(ch));
2387:
2388: if (ch == EOF) {
2389: type = RCS_TOK_EOF;
2390: } else if (ch == ';') {
2391: type = RCS_TOK_SCOLON;
2392: } else if (ch == ':') {
2393: type = RCS_TOK_COLON;
2394: } else if (isalpha(ch)) {
2395: type = RCS_TOK_ID;
2396: *(bp++) = ch;
2397: for (;;) {
2398: ch = getc(pdp->rp_file);
2399: if (!isalnum(ch) && ch != '_' && ch != '-' &&
2400: ch != '/') {
2401: ungetc(ch, pdp->rp_file);
2402: break;
2403: }
2404: *(bp++) = ch;
2405: pdp->rp_tlen++;
2406: if (bp == pdp->rp_bufend - 1) {
2407: len = bp - pdp->rp_buf;
2408: rcs_growbuf(rfp);
2409: bp = pdp->rp_buf + len;
2410: }
2411: }
2412: *bp = '\0';
2413:
2414: if (type != RCS_TOK_ERR) {
2415: for (i = 0; i < RCS_NKEYS; i++) {
2416: if (strcmp(rcs_keys[i].rk_str,
2417: pdp->rp_buf) == 0) {
2418: type = rcs_keys[i].rk_id;
2419: break;
2420: }
2421: }
2422: }
2423: } else if (ch == '@') {
2424: /* we have a string */
2425: type = RCS_TOK_STRING;
2426: for (;;) {
2427: ch = getc(pdp->rp_file);
2428: if (ch == '@') {
2429: ch = getc(pdp->rp_file);
2430: if (ch != '@') {
2431: ungetc(ch, pdp->rp_file);
2432: break;
2433: }
2434: } else if (ch == '\n')
2435: pdp->rp_lines++;
2436:
2437: *(bp++) = ch;
2438: pdp->rp_tlen++;
2439: if (bp == pdp->rp_bufend - 1) {
2440: len = bp - pdp->rp_buf;
2441: rcs_growbuf(rfp);
2442: bp = pdp->rp_buf + len;
2443: }
2444: }
2445:
2446: *bp = '\0';
2447: } else if (isdigit(ch)) {
2448: *(bp++) = ch;
2449: last = ch;
2450: type = RCS_TOK_NUM;
2451:
2452: for (;;) {
2453: ch = getc(pdp->rp_file);
2454: if (bp == pdp->rp_bufend)
2455: break;
2456: if (!isdigit(ch) && ch != '.') {
2457: ungetc(ch, pdp->rp_file);
2458: break;
2459: }
2460:
2461: if (last == '.' && ch == '.') {
2462: type = RCS_TOK_ERR;
2463: break;
2464: }
2465: last = ch;
2466: *(bp++) = ch;
2467: pdp->rp_tlen++;
2468: }
2469: *bp = '\0';
2470: }
2471:
2472: return (type);
2473: }
2474:
2475: /*
2476: * rcs_pushtok()
2477: *
2478: * Push a token back in the parser's token buffer.
2479: */
2480: static int
2481: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2482: {
2483: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2484:
2485: if (pdp->rp_pttype != RCS_TOK_ERR)
2486: return (-1);
2487:
2488: pdp->rp_pttype = type;
1.5 ray 2489: if (strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok)) >=
2490: sizeof(pdp->rp_ptok))
2491: errx(1, "rcs_pushtok: strlcpy");
1.1 joris 2492: return (0);
2493: }
2494:
2495:
2496: /*
2497: * rcs_growbuf()
2498: *
2499: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2500: * RCS_BUFEXTSIZE.
2501: * In case of failure, the original buffer is left unmodified.
2502: */
2503: static void
2504: rcs_growbuf(RCSFILE *rf)
2505: {
2506: void *tmp;
2507: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2508:
2509: tmp = xrealloc(pdp->rp_buf, 1, pdp->rp_blen + RCS_BUFEXTSIZE);
2510: pdp->rp_buf = tmp;
2511: pdp->rp_blen += RCS_BUFEXTSIZE;
2512: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
2513: }
2514:
2515: /*
2516: * rcs_strprint()
2517: *
2518: * Output an RCS string <str> of size <slen> to the stream <stream>. Any
2519: * '@' characters are escaped. Otherwise, the string can contain arbitrary
2520: * binary data.
2521: */
2522: static void
2523: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
2524: {
2525: const u_char *ap, *ep, *sp;
2526:
2527: if (slen == 0)
2528: return;
2529:
2530: ep = str + slen - 1;
2531:
2532: for (sp = str; sp <= ep;) {
2533: ap = memchr(sp, '@', ep - sp);
2534: if (ap == NULL)
2535: ap = ep;
2536: (void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
2537:
2538: if (*ap == '@')
2539: putc('@', stream);
2540: sp = ap + 1;
2541: }
2542: }
2543:
2544: /*
2545: * rcs_expand_keywords()
2546: *
2547: * Return expansion any RCS keywords in <data>
2548: *
2549: * On error, return NULL.
2550: */
2551: static char *
2552: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *data,
2553: size_t len, int mode)
2554: {
2555: ptrdiff_t c_offset, sizdiff, start_offset;
2556: size_t i;
2557: int kwtype;
2558: u_int j, found;
2559: char *c, *kwstr, *start, *end, *tbuf;
2560: char expbuf[256], buf[256];
2561: struct tm tb;
2562: char *fmt;
2563:
2564: kwtype = 0;
2565: kwstr = NULL;
2566: i = 0;
2567:
2568: /*
2569: * -z support for RCS
2570: */
2571: tb = rdp->rd_date;
2572: if (timezone_flag != NULL)
2573: rcs_set_tz(timezone_flag, rdp, &tb);
2574:
2575: /*
2576: * Keyword formats:
2577: * $Keyword$
2578: * $Keyword: value$
2579: */
2580: for (c = data; *c != '\0' && i < len; c++) {
2581: if (*c == '$') {
2582: /* remember start of this possible keyword */
2583: start = c;
2584: start_offset = start - data;
2585:
2586: /* first following character has to be alphanumeric */
2587: c++;
2588: if (!isalpha(*c)) {
2589: c = start;
2590: continue;
2591: }
2592:
2593: /* look for any matching keywords */
2594: found = 0;
2595: for (j = 0; j < RCS_NKWORDS; j++) {
2596: if (!strncmp(c, rcs_expkw[j].kw_str,
2597: strlen(rcs_expkw[j].kw_str))) {
2598: found = 1;
2599: kwstr = rcs_expkw[j].kw_str;
2600: kwtype = rcs_expkw[j].kw_type;
2601: break;
2602: }
2603: }
2604:
2605: /* unknown keyword, continue looking */
2606: if (found == 0) {
2607: c = start;
2608: continue;
2609: }
2610:
2611: /* next character has to be ':' or '$' */
2612: c += strlen(kwstr);
2613: if (*c != ':' && *c != '$') {
2614: c = start;
2615: continue;
2616: }
2617:
2618: /*
2619: * if the next character was ':' we need to look for
2620: * an '$' before the end of the line to be sure it is
2621: * in fact a keyword.
2622: */
2623: if (*c == ':') {
2624: while (*c++) {
2625: if (*c == '$' || *c == '\n')
2626: break;
2627: }
2628:
2629: if (*c != '$') {
2630: c = start;
2631: continue;
2632: }
2633: }
2634: c_offset = c - data;
2635: end = c + 1;
2636:
2637: /* start constructing the expansion */
2638: expbuf[0] = '\0';
2639:
2640: if (mode & RCS_KWEXP_NAME) {
1.5 ray 2641: if (strlcat(expbuf, "$", sizeof(expbuf)) >= sizeof(expbuf) ||
2642: strlcat(expbuf, kwstr, sizeof(expbuf)) >= sizeof(expbuf))
2643: errx(1, "rcs_expand_keywords: string truncated");
2644: if ((mode & RCS_KWEXP_VAL) &&
2645: strlcat(expbuf, ": ", sizeof(expbuf)) >= sizeof(expbuf))
2646: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2647: }
2648:
2649: /*
2650: * order matters because of RCS_KW_ID and
2651: * RCS_KW_HEADER here
2652: */
2653: if (mode & RCS_KWEXP_VAL) {
2654: if (kwtype & RCS_KW_RCSFILE) {
2655: if (!(kwtype & RCS_KW_FULLPATH))
1.5 ray 2656: (void)strlcat(expbuf, basename(rcsfile), sizeof(expbuf));
1.1 joris 2657: else
1.5 ray 2658: (void)strlcat(expbuf, rcsfile, sizeof(expbuf));
2659: if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2660: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2661: }
2662:
2663: if (kwtype & RCS_KW_REVISION) {
1.5 ray 2664: rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
2665: if (strlcat(buf, " ", sizeof(buf)) >= sizeof(buf) ||
2666: strlcat(expbuf, buf, sizeof(expbuf)) >= sizeof(buf))
2667: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2668: }
2669:
2670: if (kwtype & RCS_KW_DATE) {
2671: if (timezone_flag != NULL)
2672: fmt = "%Y/%m/%d %H:%M:%S%z ";
2673: else
2674: fmt = "%Y/%m/%d %H:%M:%S ";
2675:
2676: strftime(buf, sizeof(buf), fmt, &tb);
1.5 ray 2677: if (strlcat(expbuf, buf, sizeof(expbuf)) >= sizeof(expbuf))
2678: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2679: }
2680:
2681: if (kwtype & RCS_KW_AUTHOR) {
1.5 ray 2682: if (strlcat(expbuf, rdp->rd_author, sizeof(expbuf)) >= sizeof(expbuf) ||
2683: strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2684: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2685: }
2686:
2687: if (kwtype & RCS_KW_STATE) {
1.5 ray 2688: if (strlcat(expbuf, rdp->rd_state, sizeof(expbuf)) >= sizeof(expbuf) ||
2689: strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2690: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2691: }
2692:
2693: /* order does not matter anymore below */
2694: if (kwtype & RCS_KW_LOG)
1.5 ray 2695: if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2696: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2697:
2698: if (kwtype & RCS_KW_SOURCE) {
1.5 ray 2699: if (strlcat(expbuf, rcsfile, sizeof(expbuf)) >= sizeof(expbuf) ||
2700: strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2701: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2702: }
2703:
2704: if (kwtype & RCS_KW_NAME)
1.5 ray 2705: if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2706: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2707: }
2708:
2709: /* end the expansion */
2710: if (mode & RCS_KWEXP_NAME)
1.5 ray 2711: if (strlcat(expbuf, "$", sizeof(expbuf)) >= sizeof(expbuf))
2712: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2713:
2714: sizdiff = strlen(expbuf) - (end - start);
2715: tbuf = xstrdup(end);
2716: /* only realloc if we have to */
2717: if (sizdiff > 0) {
2718: char *newdata;
2719:
2720: len += sizdiff;
2721: newdata = xrealloc(data, 1, len);
2722: data = newdata;
2723: /*
2724: * ensure string pointers are not invalidated
2725: * after realloc()
2726: */
2727: start = data + start_offset;
2728: c = data + c_offset;
2729: }
1.5 ray 2730: if (strlcpy(start, expbuf, len) >= len ||
2731: strlcat(data, tbuf, len) >= len)
2732: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2733: xfree(tbuf);
2734: i += strlen(expbuf);
2735: }
2736: }
2737:
2738: return (data);
2739: }
2740:
2741: /*
2742: * rcs_deltatext_set()
2743: *
2744: * Set deltatext for <rev> in RCS file <rfp> to <dtext>
2745: * Returns -1 on error, 0 on success.
2746: */
2747: int
2748: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, const char *dtext)
2749: {
2750: size_t len;
2751: struct rcs_delta *rdp;
2752:
2753: /* Write operations require full parsing */
2754: rcs_parse_deltatexts(rfp, NULL);
2755:
2756: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2757: return (-1);
2758:
2759: if (rdp->rd_text != NULL)
2760: xfree(rdp->rd_text);
2761:
2762: len = strlen(dtext);
2763: if (len != 0) {
2764: /* XXX - use xstrdup() if rd_text changes to char *. */
2765: rdp->rd_text = xmalloc(len + 1);
2766: rdp->rd_tlen = len;
2767: (void)memcpy(rdp->rd_text, dtext, len + 1);
2768: } else {
2769: rdp->rd_text = NULL;
2770: rdp->rd_tlen = 0;
2771: }
2772:
2773: return (0);
2774: }
2775:
2776: /*
2777: * rcs_rev_setlog()
2778: *
2779: * Sets the log message of revision <rev> to <logtext>
2780: */
2781: int
2782: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
2783: {
2784: struct rcs_delta *rdp;
2785: char buf[16];
2786:
2787: rcsnum_tostr(rev, buf, sizeof(buf));
2788:
2789: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2790: return (-1);
2791:
2792: if (rdp->rd_log != NULL)
2793: xfree(rdp->rd_log);
2794:
2795: rdp->rd_log = xstrdup(logtext);
2796: rfp->rf_flags &= ~RCS_SYNCED;
2797: return (0);
2798: }
2799: /*
2800: * rcs_rev_getdate()
2801: *
2802: * Get the date corresponding to a given revision.
2803: * Returns the date on success, -1 on failure.
2804: */
2805: time_t
2806: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
2807: {
2808: struct rcs_delta *rdp;
2809:
2810: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2811: return (-1);
2812:
2813: return (mktime(&rdp->rd_date));
2814: }
2815:
2816: /*
2817: * rcs_state_set()
2818: *
2819: * Sets the state of revision <rev> to <state>
2820: * NOTE: default state is 'Exp'. States may not contain spaces.
2821: *
2822: * Returns -1 on failure, 0 on success.
2823: */
2824: int
2825: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
2826: {
2827: struct rcs_delta *rdp;
2828:
2829: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2830: return (-1);
2831:
2832: if (rdp->rd_state != NULL)
2833: xfree(rdp->rd_state);
2834:
2835: rdp->rd_state = xstrdup(state);
2836:
2837: rfp->rf_flags &= ~RCS_SYNCED;
2838:
2839: return (0);
2840: }
2841:
2842: /*
2843: * rcs_state_check()
2844: *
2845: * Check if string <state> is valid.
2846: *
2847: * Returns 0 if the string is valid, -1 otherwise.
2848: */
2849: int
2850: rcs_state_check(const char *state)
2851: {
2852: if (strchr(state, ' ') != NULL)
2853: return (-1);
2854:
2855: return (0);
2856: }
2857:
2858: /*
2859: * rcs_state_get()
2860: *
2861: * Get the state for a given revision of a specified RCSFILE.
2862: *
2863: * Returns NULL on failure.
2864: */
2865: const char *
2866: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
2867: {
2868: struct rcs_delta *rdp;
2869:
2870: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2871: return (NULL);
2872:
2873: return (rdp->rd_state);
2874: }
2875:
2876: /*
2877: * rcs_kwexp_buf()
2878: *
2879: * Do keyword expansion on a buffer if necessary
2880: *
2881: */
2882: BUF *
2883: rcs_kwexp_buf(BUF *bp, RCSFILE *rf, RCSNUM *rev)
2884: {
2885: struct rcs_delta *rdp;
2886: char *expanded, *tbuf;
2887: int expmode;
2888: size_t len;
2889:
2890: /*
2891: * Do keyword expansion if required.
2892: */
2893: if (rf->rf_expand != NULL)
2894: expmode = rcs_kwexp_get(rf);
2895: else
2896: expmode = RCS_KWEXP_DEFAULT;
2897:
2898: if (!(expmode & RCS_KWEXP_NONE)) {
2899: if ((rdp = rcs_findrev(rf, rev)) == NULL)
1.5 ray 2900: errx(1, "could not fetch revision");
1.1 joris 2901: rcs_buf_putc(bp, '\0');
2902: len = rcs_buf_len(bp);
2903: tbuf = rcs_buf_release(bp);
2904: expanded = rcs_expand_keywords(rf->rf_path, rdp,
2905: tbuf, len, expmode);
2906: bp = rcs_buf_alloc(len, BUF_AUTOEXT);
2907: rcs_buf_set(bp, expanded, strlen(expanded), 0);
2908: xfree(expanded);
2909: }
2910: return (bp);
2911: }