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

Annotation of src/usr.bin/rcs/rcsprog.c, Revision 1.27

1.27    ! xsa         1: /*     $OpenBSD: rcsprog.c,v 1.26 2005/10/12 17:13:30 deraadt Exp $    */
1.1       deraadt     2: /*
                      3:  * Copyright (c) 2005 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
1.6       joris      27: #include <sys/param.h>
1.1       deraadt    28: #include <sys/wait.h>
1.8       joris      29: #include <sys/stat.h>
1.1       deraadt    30:
1.3       joris      31: #include <ctype.h>
1.1       deraadt    32: #include <err.h>
1.3       joris      33: #include <errno.h>
1.1       deraadt    34: #include <pwd.h>
1.3       joris      35: #include <string.h>
1.1       deraadt    36: #include <stdio.h>
                     37: #include <stdlib.h>
                     38: #include <unistd.h>
                     39:
                     40: #include "log.h"
                     41: #include "rcs.h"
1.9       joris      42: #include "rcsprog.h"
1.1       deraadt    43: #include "strtab.h"
                     44:
                     45: const char rcs_version[] = "OpenCVS RCS version 3.6";
1.12      joris      46: int verbose = 1;
1.1       deraadt    47:
                     48: struct rcs_prog {
1.26      deraadt    49:        char    *prog_name;
                     50:        int     (*prog_hdlr)(int, char **);
                     51:        void    (*prog_usage)(void);
1.1       deraadt    52: } programs[] = {
1.2       deraadt    53:        { "rcs",        rcs_main,       rcs_usage       },
1.17      joris      54:        { "ci",         checkin_main,   checkin_usage   },
1.11      joris      55:        { "co",         checkout_main,  checkout_usage  },
1.20      joris      56:        { "rcsclean",   rcsclean_main,  rcsclean_usage  },
1.18      joris      57:        { "rcsdiff",    rcsdiff_main,   rcsdiff_usage   },
1.21      joris      58:        { "rlog",       rlog_main,      rlog_usage      },
1.22      joris      59:        { "ident",      ident_main,     ident_usage     },
1.1       deraadt    60: };
                     61:
1.9       joris      62: int
                     63: rcs_statfile(char *fname, char *out, size_t len)
                     64: {
                     65:        int l;
                     66:        char *s;
                     67:        char filev[MAXPATHLEN], fpath[MAXPATHLEN];
                     68:        struct stat st;
                     69:
                     70:        l = snprintf(filev, sizeof(filev), "%s%s", fname, RCS_FILE_EXT);
                     71:        if (l == -1 || l >= (int)sizeof(filev))
                     72:                return (-1);
                     73:
1.16      joris      74:        if ((stat(RCSDIR, &st) != -1) && (st.st_mode & S_IFDIR)) {
1.9       joris      75:                l = snprintf(fpath, sizeof(fpath), "%s/%s", RCSDIR, filev);
                     76:                if (l == -1 || l >= (int)sizeof(fpath))
                     77:                        return (-1);
                     78:        } else {
                     79:                strlcpy(fpath, filev, sizeof(fpath));
                     80:        }
                     81:
                     82:        if (stat(fpath, &st) == -1) {
1.20      joris      83:                if (strcmp(__progname, "rcsclean"))
                     84:                        cvs_log(LP_ERRNO, "%s", fpath);
1.9       joris      85:                return (-1);
                     86:        }
                     87:
                     88:        strlcpy(out, fpath, len);
1.27    ! xsa        89:        if ((verbose == 1) && (strcmp(__progname, "rcsclean"))) {
1.12      joris      90:                if (!strcmp(__progname, "co")) {
1.19      joris      91:                        printf("%s --> ", fpath);
1.12      joris      92:                        if ((s = strrchr(filev, ',')) != NULL) {
                     93:                                *s = '\0';
                     94:                                printf("%s\n", fname);
                     95:                        }
                     96:                } else {
                     97:                        printf("RCS file: %s\n", fpath);
1.9       joris      98:                }
                     99:        }
                    100:
                    101:        return (0);
                    102: }
1.1       deraadt   103:
                    104: int
                    105: main(int argc, char **argv)
                    106: {
                    107:        u_int i;
                    108:        int ret;
                    109:
                    110:        ret = -1;
                    111:        cvs_strtab_init();
1.9       joris     112:        cvs_log_init(LD_STD, 0);
1.1       deraadt   113:
                    114:        for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++)
1.2       deraadt   115:                if (strcmp(__progname, programs[i].prog_name) == 0) {
                    116:                        usage = programs[i].prog_usage;
1.1       deraadt   117:                        ret = programs[i].prog_hdlr(argc, argv);
1.2       deraadt   118:                        break;
                    119:                }
1.1       deraadt   120:
                    121:        cvs_strtab_cleanup();
                    122:
1.23      niallo    123:        exit(ret);
1.1       deraadt   124: }
                    125:
                    126:
                    127: void
                    128: rcs_usage(void)
                    129: {
                    130:        fprintf(stderr,
1.26      deraadt   131:            "usage: rcs [-hiLMUV] [-a users] [-b [rev]] [-c string] [-e users]\n"
                    132:            "           [-k opt] [-m rev:log] file ...\n");
1.1       deraadt   133: }
                    134:
                    135: /*
                    136:  * rcs_main()
                    137:  *
                    138:  * Handler for the `rcs' program.
                    139:  * Returns 0 on success, or >0 on error.
                    140:  */
                    141: int
                    142: rcs_main(int argc, char **argv)
                    143: {
                    144:        int i, ch, flags, kflag, lkmode;
1.10      joris     145:        char fpath[MAXPATHLEN];
1.24      joris     146:        char *logstr, *logmsg;
1.1       deraadt   147:        char *oldfile, *alist, *comment, *elist, *unp, *sp;
                    148:        mode_t fmode;
                    149:        RCSFILE *file;
1.24      joris     150:        RCSNUM *logrev;
1.1       deraadt   151:
                    152:        kflag = lkmode = -1;
                    153:        fmode = 0;
                    154:        flags = RCS_RDWR;
1.24      joris     155:        logstr = oldfile = alist = comment = elist = NULL;
1.1       deraadt   156:
1.24      joris     157:        while ((ch = getopt(argc, argv, "A:a:b::c:e::hik:Lm:MqUV")) != -1) {
1.1       deraadt   158:                switch (ch) {
                    159:                case 'A':
                    160:                        oldfile = optarg;
                    161:                        break;
                    162:                case 'a':
                    163:                        alist = optarg;
                    164:                        break;
                    165:                case 'c':
                    166:                        comment = optarg;
                    167:                        break;
                    168:                case 'e':
                    169:                        elist = optarg;
                    170:                        break;
                    171:                case 'h':
1.2       deraadt   172:                        (usage)();
1.1       deraadt   173:                        exit(0);
                    174:                case 'i':
                    175:                        flags |= RCS_CREATE;
                    176:                        break;
                    177:                case 'k':
                    178:                        kflag = rcs_kflag_get(optarg);
                    179:                        if (RCS_KWEXP_INVAL(kflag)) {
                    180:                                cvs_log(LP_ERR,
                    181:                                    "invalid keyword substitution mode `%s'",
                    182:                                    optarg);
                    183:                                exit(1);
                    184:                        }
                    185:                        break;
                    186:                case 'L':
                    187:                        if (lkmode == RCS_LOCK_LOOSE)
                    188:                                cvs_log(LP_WARN, "-U overriden by -L");
                    189:                        lkmode = RCS_LOCK_STRICT;
                    190:                        break;
1.24      joris     191:                case 'm':
                    192:                        if ((logstr = strdup(optarg)) == NULL) {
                    193:                                cvs_log(LP_ERRNO, "failed to copy logstring");
                    194:                                exit(1);
                    195:                        }
                    196:                        break;
1.1       deraadt   197:                case 'M':
                    198:                        /* ignore for the moment */
                    199:                        break;
1.12      joris     200:                case 'q':
                    201:                        verbose = 0;
                    202:                        break;
1.1       deraadt   203:                case 'U':
                    204:                        if (lkmode == RCS_LOCK_STRICT)
                    205:                                cvs_log(LP_WARN, "-L overriden by -U");
                    206:                        lkmode = RCS_LOCK_LOOSE;
                    207:                        break;
                    208:                case 'V':
                    209:                        printf("%s\n", rcs_version);
                    210:                        exit(0);
                    211:                default:
1.2       deraadt   212:                        (usage)();
1.1       deraadt   213:                        exit(1);
                    214:                }
                    215:        }
                    216:
                    217:        argc -= optind;
                    218:        argv += optind;
                    219:        if (argc == 0) {
                    220:                cvs_log(LP_ERR, "no input file");
1.5       joris     221:                (usage)();
1.1       deraadt   222:                exit(1);
                    223:        }
                    224:
                    225:        for (i = 0; i < argc; i++) {
1.9       joris     226:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8       joris     227:                        continue;
1.6       joris     228:
                    229:                file = rcs_open(fpath, flags, fmode);
                    230:                if (file == NULL)
                    231:                        continue;
1.1       deraadt   232:
1.24      joris     233:                if (logstr != NULL) {
                    234:                        if ((logmsg = strchr(logstr, ':')) == NULL) {
                    235:                                cvs_log(LP_ERR, "missing log message");
                    236:                                rcs_close(file);
                    237:                                continue;
                    238:                        }
                    239:
                    240:                        *logmsg++ = '\0';
                    241:                        if ((logrev = rcsnum_parse(logstr)) == NULL) {
                    242:                                cvs_log(LP_ERR, "'%s' bad revision number", logstr);
                    243:                                rcs_close(file);
                    244:                                continue;
                    245:                        }
                    246:
                    247:                        if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
                    248:                                cvs_log(LP_ERR,
                    249:                                    "failed to set logmsg for '%s' to '%s'",
                    250:                                    logstr, logmsg);
                    251:                                rcs_close(file);
1.25      joris     252:                                rcsnum_free(logrev);
1.24      joris     253:                                continue;
                    254:                        }
                    255:
                    256:                        rcsnum_free(logrev);
                    257:                }
                    258:
1.1       deraadt   259:                /* entries to add to the access list */
                    260:                if (alist != NULL) {
                    261:                        unp = alist;
                    262:                        do {
                    263:                                sp = strchr(unp, ',');
                    264:                                if (sp != NULL)
                    265:                                        *(sp++) = '\0';
                    266:
                    267:                                rcs_access_add(file, unp);
                    268:
                    269:                                unp = sp;
                    270:                        } while (sp != NULL);
                    271:                }
                    272:
                    273:                if (comment != NULL)
                    274:                        rcs_comment_set(file, comment);
                    275:
                    276:                if (kflag != -1)
                    277:                        rcs_kwexp_set(file, kflag);
                    278:
                    279:                if (lkmode != -1)
                    280:                        rcs_lock_setmode(file, lkmode);
                    281:
                    282:                rcs_close(file);
1.9       joris     283:
1.14      xsa       284:                if (verbose == 1)
1.12      joris     285:                        printf("done\n");
1.1       deraadt   286:        }
1.24      joris     287:
                    288:        if (logstr != NULL)
                    289:                free(logstr);
1.1       deraadt   290:
                    291:        return (0);
                    292: }