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

Annotation of src/usr.bin/rcs/rcsmerge.c, Revision 1.23

1.23    ! ray         1: /*     $OpenBSD: rcsmerge.c,v 1.22 2006/03/23 08:50:41 xsa Exp $       */
1.1       xsa         2: /*
1.14      xsa         3:  * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.1       xsa         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.12      xsa        27: #include "includes.h"
1.1       xsa        28:
1.13      xsa        29: #include "rcsprog.h"
1.1       xsa        30: #include "diff.h"
                     31:
                     32: static int kflag = RCS_KWEXP_ERR;
                     33:
                     34: int
                     35: rcsmerge_main(int argc, char **argv)
                     36: {
                     37:        int i, ch;
1.15      xsa        38:        char *fcont, fpath[MAXPATHLEN], r1[16], r2[16];
1.1       xsa        39:        RCSFILE *file;
1.2       joris      40:        RCSNUM *baserev, *rev2, *frev;
                     41:        BUF *bp;
1.1       xsa        42:
1.2       joris      43:        baserev = rev2 = RCS_HEAD_REV;
1.1       xsa        44:
1.23    ! ray        45:        while ((ch = rcs_getopt(argc, argv, "AEek:p::q::r:TVx::z:")) != -1) {
1.1       xsa        46:                switch (ch) {
1.14      xsa        47:                case 'A': case 'E': case 'e':
                     48:                        break;
1.1       xsa        49:                case 'k':
                     50:                        kflag = rcs_kflag_get(rcs_optarg);
                     51:                        if (RCS_KWEXP_INVAL(kflag)) {
                     52:                                cvs_log(LP_ERR,
                     53:                                    "invalid RCS keyword expansion mode");
                     54:                                (usage)();
                     55:                                exit(1);
                     56:                        }
                     57:                        break;
1.2       joris      58:                case 'p':
                     59:                        rcs_set_rev(rcs_optarg, &baserev);
                     60:                        pipeout = 1;
                     61:                        break;
1.1       xsa        62:                case 'q':
1.7       xsa        63:                        rcs_set_rev(rcs_optarg, &baserev);
1.1       xsa        64:                        verbose = 0;
                     65:                        break;
                     66:                case 'r':
1.2       joris      67:                        if (baserev == RCS_HEAD_REV)
                     68:                                rcs_set_rev(rcs_optarg, &baserev);
                     69:                        else if (rev2 == RCS_HEAD_REV)
                     70:                                rcs_set_rev(rcs_optarg, &rev2);
                     71:                        else
1.14      xsa        72:                                fatal("too many revision numbers");
1.1       xsa        73:                        break;
                     74:                case 'T':
                     75:                        /*
                     76:                         * kept for compatibility
                     77:                         */
                     78:                        break;
                     79:                case 'V':
                     80:                        printf("%s\n", rcs_version);
                     81:                        exit(0);
1.21      ray        82:                        /* NOTREACHED */
1.9       xsa        83:                case 'x':
1.23    ! ray        84:                        /* Use blank extension if none given. */
        !            85:                        rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.17      joris      86:                        break;
                     87:                case 'z':
                     88:                        timezone_flag = rcs_optarg;
1.9       xsa        89:                        break;
1.1       xsa        90:                default:
                     91:                        break;
                     92:                }
                     93:        }
                     94:
                     95:        argc -= rcs_optind;
                     96:        argv += rcs_optind;
                     97:
                     98:        if (argc < 0) {
                     99:                cvs_log(LP_ERR, "no input file");
                    100:                (usage)();
                    101:                exit(1);
                    102:        }
                    103:
1.2       joris     104:        if (baserev == RCS_HEAD_REV) {
1.4       xsa       105:                cvs_log(LP_ERR, "no base revision number given");
1.2       joris     106:                (usage)();
                    107:                exit(1);
                    108:        }
                    109:
1.1       xsa       110:        for (i = 0; i < argc; i++) {
                    111:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    112:                        continue;
                    113:
                    114:                if ((file = rcs_open(fpath, RCS_READ)) == NULL)
                    115:                        continue;
1.2       joris     116:
1.18      xsa       117:                if (verbose == 1)
                    118:                        fprintf(stderr, "RCS file: %s\n", fpath);
1.14      xsa       119:
1.2       joris     120:                if (rev2 == RCS_HEAD_REV)
                    121:                        frev = file->rf_head;
                    122:                else
                    123:                        frev = rev2;
1.8       xsa       124:
1.14      xsa       125:                if (rcsnum_cmp(baserev, frev, 0) == 0) {
                    126:                        rcs_close(file);
                    127:                        continue;
                    128:                }
1.15      xsa       129:
                    130:                rcsnum_tostr(baserev, r1, sizeof(r1));
                    131:                rcsnum_tostr(frev, r2, sizeof(r2));
                    132:
1.18      xsa       133:                if (verbose == 1)
                    134:                        fprintf(stderr, "Merging differences between %s and "
                    135:                            "%s into %s%s\n", r1, r2, argv[i],
                    136:                            (pipeout == 1) ? "; result to stdout":"");
1.2       joris     137:
1.20      xsa       138:                if ((bp = cvs_diff3(file, argv[i], baserev,
                    139:                    frev, verbose)) == NULL) {
1.2       joris     140:                        cvs_log(LP_ERR, "failed to merge");
                    141:                        rcs_close(file);
                    142:                        continue;
                    143:                }
                    144:
                    145:                if (pipeout == 1) {
1.22      xsa       146:                        cvs_buf_putc(bp, '\0');
1.2       joris     147:                        fcont = cvs_buf_release(bp);
                    148:                        printf("%s", fcont);
1.11      joris     149:                        xfree(fcont);
1.2       joris     150:                } else {
                    151:                        /* XXX mode */
                    152:                        if (cvs_buf_write(bp, argv[i], 0644) < 0)
                    153:                                cvs_log(LP_ERR, "failed to write new file");
                    154:
                    155:                        cvs_buf_free(bp);
1.3       joris     156:                }
1.1       xsa       157:                rcs_close(file);
                    158:        }
                    159:
                    160:        return (0);
                    161: }
                    162:
                    163: void
                    164: rcsmerge_usage(void)
                    165: {
                    166:        fprintf(stderr,
1.19      xsa       167:            "usage: rcsmerge [-AEeV] [-kmode] [-p[rev]] [-q[rev]]\n"
                    168:            "                [-rrev] [-xsuffixes] [-ztz] file ...\n");
1.1       xsa       169: }