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

1.22    ! xsa         1: /*     $OpenBSD: rcsmerge.c,v 1.21 2006/03/16 04:04:57 ray 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.17      joris      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':
                     84:                        rcs_suffixes = rcs_optarg;
1.17      joris      85:                        break;
                     86:                case 'z':
                     87:                        timezone_flag = rcs_optarg;
1.9       xsa        88:                        break;
1.1       xsa        89:                default:
                     90:                        break;
                     91:                }
                     92:        }
                     93:
                     94:        argc -= rcs_optind;
                     95:        argv += rcs_optind;
                     96:
                     97:        if (argc < 0) {
                     98:                cvs_log(LP_ERR, "no input file");
                     99:                (usage)();
                    100:                exit(1);
                    101:        }
                    102:
1.2       joris     103:        if (baserev == RCS_HEAD_REV) {
1.4       xsa       104:                cvs_log(LP_ERR, "no base revision number given");
1.2       joris     105:                (usage)();
                    106:                exit(1);
                    107:        }
                    108:
1.1       xsa       109:        for (i = 0; i < argc; i++) {
                    110:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    111:                        continue;
                    112:
                    113:                if ((file = rcs_open(fpath, RCS_READ)) == NULL)
                    114:                        continue;
1.2       joris     115:
1.18      xsa       116:                if (verbose == 1)
                    117:                        fprintf(stderr, "RCS file: %s\n", fpath);
1.14      xsa       118:
1.2       joris     119:                if (rev2 == RCS_HEAD_REV)
                    120:                        frev = file->rf_head;
                    121:                else
                    122:                        frev = rev2;
1.8       xsa       123:
1.14      xsa       124:                if (rcsnum_cmp(baserev, frev, 0) == 0) {
                    125:                        rcs_close(file);
                    126:                        continue;
                    127:                }
1.15      xsa       128:
                    129:                rcsnum_tostr(baserev, r1, sizeof(r1));
                    130:                rcsnum_tostr(frev, r2, sizeof(r2));
                    131:
1.18      xsa       132:                if (verbose == 1)
                    133:                        fprintf(stderr, "Merging differences between %s and "
                    134:                            "%s into %s%s\n", r1, r2, argv[i],
                    135:                            (pipeout == 1) ? "; result to stdout":"");
1.2       joris     136:
1.20      xsa       137:                if ((bp = cvs_diff3(file, argv[i], baserev,
                    138:                    frev, verbose)) == NULL) {
1.2       joris     139:                        cvs_log(LP_ERR, "failed to merge");
                    140:                        rcs_close(file);
                    141:                        continue;
                    142:                }
                    143:
                    144:                if (pipeout == 1) {
1.22    ! xsa       145:                        cvs_buf_putc(bp, '\0');
1.2       joris     146:                        fcont = cvs_buf_release(bp);
                    147:                        printf("%s", fcont);
1.11      joris     148:                        xfree(fcont);
1.2       joris     149:                } else {
                    150:                        /* XXX mode */
                    151:                        if (cvs_buf_write(bp, argv[i], 0644) < 0)
                    152:                                cvs_log(LP_ERR, "failed to write new file");
                    153:
                    154:                        cvs_buf_free(bp);
1.3       joris     155:                }
1.1       xsa       156:                rcs_close(file);
                    157:        }
                    158:
                    159:        return (0);
                    160: }
                    161:
                    162: void
                    163: rcsmerge_usage(void)
                    164: {
                    165:        fprintf(stderr,
1.19      xsa       166:            "usage: rcsmerge [-AEeV] [-kmode] [-p[rev]] [-q[rev]]\n"
                    167:            "                [-rrev] [-xsuffixes] [-ztz] file ...\n");
1.1       xsa       168: }