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

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