[BACK]Return to rcs.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rcs

Annotation of src/usr.bin/rcs/rcs.c, Revision 1.44

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