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

1.7     ! xsa         1: /*     $OpenBSD: rcsmerge.c,v 1.6 2005/10/26 18:13:58 xsa Exp $        */
1.1       xsa         2: /*
                      3:  * Copyright (c) 2005 Xavier Santolaria <xsa@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:
                     27: #include <sys/param.h>
                     28: #include <sys/stat.h>
                     29:
                     30: #include <stdio.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
                     33: #include <unistd.h>
                     34:
                     35: #include "log.h"
                     36: #include "rcs.h"
                     37: #include "diff.h"
                     38: #include "rcsprog.h"
                     39:
                     40: static int kflag = RCS_KWEXP_ERR;
                     41:
                     42: int
                     43: rcsmerge_main(int argc, char **argv)
                     44: {
                     45:        int i, ch;
1.2       joris      46:        char *fcont, fpath[MAXPATHLEN];
1.1       xsa        47:        RCSFILE *file;
1.2       joris      48:        RCSNUM *baserev, *rev2, *frev;
                     49:        BUF *bp;
1.1       xsa        50:
1.2       joris      51:        baserev = rev2 = RCS_HEAD_REV;
1.1       xsa        52:
1.7     ! xsa        53:        while ((ch = rcs_getopt(argc, argv, "k:p::q::r:TV")) != -1) {
1.1       xsa        54:                switch (ch) {
                     55:                case 'k':
                     56:                        kflag = rcs_kflag_get(rcs_optarg);
                     57:                        if (RCS_KWEXP_INVAL(kflag)) {
                     58:                                cvs_log(LP_ERR,
                     59:                                    "invalid RCS keyword expansion mode");
                     60:                                (usage)();
                     61:                                exit(1);
                     62:                        }
                     63:                        break;
1.2       joris      64:                case 'p':
                     65:                        rcs_set_rev(rcs_optarg, &baserev);
                     66:                        pipeout = 1;
                     67:                        break;
1.1       xsa        68:                case 'q':
1.7     ! xsa        69:                        rcs_set_rev(rcs_optarg, &baserev);
1.1       xsa        70:                        verbose = 0;
                     71:                        break;
                     72:                case 'r':
1.2       joris      73:                        if (baserev == RCS_HEAD_REV)
                     74:                                rcs_set_rev(rcs_optarg, &baserev);
                     75:                        else if (rev2 == RCS_HEAD_REV)
                     76:                                rcs_set_rev(rcs_optarg, &rev2);
                     77:                        else
                     78:                                cvs_log(LP_WARN, "ignored excessive -r option");
1.1       xsa        79:                        break;
                     80:                case 'T':
                     81:                        /*
                     82:                         * kept for compatibility
                     83:                         */
                     84:                        break;
                     85:                case 'V':
                     86:                        printf("%s\n", rcs_version);
                     87:                        exit(0);
                     88:                default:
                     89:                        break;
                     90:                }
                     91:        }
                     92:
                     93:        argc -= rcs_optind;
                     94:        argv += rcs_optind;
                     95:
                     96:        if (argc < 0) {
                     97:                cvs_log(LP_ERR, "no input file");
                     98:                (usage)();
                     99:                exit(1);
                    100:        }
                    101:
1.2       joris     102:        if (baserev == RCS_HEAD_REV) {
1.4       xsa       103:                cvs_log(LP_ERR, "no base revision number given");
1.2       joris     104:                (usage)();
                    105:                exit(1);
                    106:        }
                    107:
1.1       xsa       108:        for (i = 0; i < argc; i++) {
                    109:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    110:                        continue;
                    111:
                    112:                if ((file = rcs_open(fpath, RCS_READ)) == NULL)
                    113:                        continue;
1.2       joris     114:
                    115:                if (rev2 == RCS_HEAD_REV)
                    116:                        frev = file->rf_head;
                    117:                else
                    118:                        frev = rev2;
                    119:
                    120:                if ((bp = cvs_diff3(file, argv[i], baserev, frev)) == NULL) {
                    121:                        cvs_log(LP_ERR, "failed to merge");
                    122:                        rcs_close(file);
                    123:                        continue;
                    124:                }
                    125:
                    126:                if (pipeout == 1) {
                    127:                        if (cvs_buf_putc(bp, '\0') < 0) {
                    128:                                rcs_close(file);
                    129:                                continue;
                    130:                        }
                    131:
                    132:                        fcont = cvs_buf_release(bp);
                    133:                        printf("%s", fcont);
                    134:                        free(fcont);
                    135:                } else {
                    136:                        /* XXX mode */
                    137:                        if (cvs_buf_write(bp, argv[i], 0644) < 0)
                    138:                                cvs_log(LP_ERR, "failed to write new file");
                    139:
                    140:                        cvs_buf_free(bp);
1.3       joris     141:                }
                    142:
                    143:                if (diff3_conflicts > 0) {
                    144:                        cvs_log(LP_WARN, "%d conflict%s found during merge",
                    145:                            diff3_conflicts, (diff3_conflicts > 1) ? "s": "");
1.2       joris     146:                }
1.1       xsa       147:
                    148:                rcs_close(file);
                    149:        }
                    150:
                    151:        return (0);
                    152: }
                    153:
                    154: void
                    155: rcsmerge_usage(void)
                    156: {
                    157:        fprintf(stderr,
1.7     ! xsa       158:            "usage: rcsmerge [-TV] [-kmode] [-p[rev]] [-q[rev]] "
        !           159:            "[-rrev] file ...\n");
1.1       xsa       160: }