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

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