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

Annotation of src/usr.bin/cvs/rcs.c, Revision 1.179

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