[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.62

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