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

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