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

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