1 /* $OpenBSD: rcsmerge.c,v 1.49 2006/10/12 17:20:12 niallo Exp $ */ 2 /* 3 * Copyright (c) 2005, 2006 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 "includes.h" 28 29 #include "rcsprog.h" 30 #include "diff.h" 31 32 int 33 rcsmerge_main(int argc, char **argv) 34 { 35 int fd, ch, flags, kflag, status; 36 char fpath[MAXPATHLEN], r1[16], r2[16], *rev_str1, *rev_str2; 37 RCSFILE *file; 38 RCSNUM *rev1, *rev2; 39 BUF *bp; 40 41 flags = 0; 42 kflag = RCS_KWEXP_ERR; 43 status = D_ERROR; 44 rev1 = rev2 = NULL; 45 rev_str1 = rev_str2 = NULL; 46 47 while ((ch = rcs_getopt(argc, argv, "AEek:p::q::r::TVx::z:")) != -1) { 48 switch (ch) { 49 case 'A': 50 /* 51 * kept for compatibility 52 */ 53 break; 54 case 'E': 55 flags |= MERGE_EFLAG; 56 flags |= MERGE_OFLAG; 57 break; 58 case 'e': 59 flags |= MERGE_EFLAG; 60 break; 61 case 'k': 62 kflag = rcs_kflag_get(rcs_optarg); 63 if (RCS_KWEXP_INVAL(kflag)) { 64 warnx("invalid RCS keyword substitution mode"); 65 (usage)(); 66 exit(D_ERROR); 67 } 68 break; 69 case 'p': 70 rcs_setrevstr2(&rev_str1, &rev_str2, rcs_optarg); 71 flags |= PIPEOUT; 72 break; 73 case 'q': 74 rcs_setrevstr2(&rev_str1, &rev_str2, rcs_optarg); 75 flags |= QUIET; 76 break; 77 case 'r': 78 rcs_setrevstr2(&rev_str1, &rev_str2, 79 rcs_optarg ? rcs_optarg : ""); 80 break; 81 case 'T': 82 /* 83 * kept for compatibility 84 */ 85 break; 86 case 'V': 87 printf("%s\n", rcs_version); 88 exit(0); 89 case 'x': 90 /* Use blank extension if none given. */ 91 rcs_suffixes = rcs_optarg ? rcs_optarg : ""; 92 break; 93 case 'z': 94 timezone_flag = rcs_optarg; 95 break; 96 default: 97 (usage)(); 98 exit(D_ERROR); 99 } 100 } 101 102 argc -= rcs_optind; 103 argv += rcs_optind; 104 105 if (rev_str1 == NULL) { 106 warnx("no base revision number given"); 107 (usage)(); 108 exit(D_ERROR); 109 } 110 111 if (argc < 1) { 112 warnx("no input file"); 113 (usage)(); 114 exit(D_ERROR); 115 } 116 117 if (argc > 2 || (argc == 2 && argv[1] != NULL)) 118 warnx("warning: excess arguments ignored"); 119 120 if ((fd = rcs_choosefile(argv[0], fpath, sizeof(fpath))) < 0) 121 err(status, "%s", fpath); 122 123 if (!(flags & QUIET)) 124 (void)fprintf(stderr, "RCS file: %s\n", fpath); 125 126 if ((file = rcs_open(fpath, fd, RCS_READ)) == NULL) 127 return (status); 128 129 if (strcmp(rev_str1, "") == 0) { 130 rev1 = rcsnum_alloc(); 131 rcsnum_cpy(file->rf_head, rev1, 0); 132 } else if ((rev1 = rcs_getrevnum(rev_str1, file)) == NULL) 133 errx(D_ERROR, "invalid revision: %s", rev_str1); 134 135 if (rev_str2 != NULL && strcmp(rev_str2, "") != 0) { 136 if ((rev2 = rcs_getrevnum(rev_str2, file)) == NULL) 137 errx(D_ERROR, "invalid revision: %s", rev_str2); 138 } else { 139 rev2 = rcsnum_alloc(); 140 rcsnum_cpy(file->rf_head, rev2, 0); 141 } 142 143 if (rcsnum_cmp(rev1, rev2, 0) == 0) 144 goto out; 145 146 if ((bp = rcs_diff3(file, argv[0], rev1, rev2, flags)) == NULL) 147 errx(D_ERROR, "failed to merge"); 148 149 if (!(flags & QUIET)) { 150 (void)rcsnum_tostr(rev1, r1, sizeof(r1)); 151 (void)rcsnum_tostr(rev2, r2, sizeof(r2)); 152 153 (void)fprintf(stderr, "Merging differences between %s and " 154 "%s into %s%s\n", r1, r2, argv[0], 155 (flags & PIPEOUT) ? "; result to stdout":""); 156 } 157 158 if (diff3_conflicts != 0) 159 status = D_OVERLAPS; 160 else 161 status = 0; 162 163 if (flags & PIPEOUT) 164 rcs_buf_write_fd(bp, STDOUT_FILENO); 165 else { 166 /* XXX mode */ 167 if (rcs_buf_write(bp, argv[0], 0644) < 0) 168 warnx("rcs_buf_write failed"); 169 170 } 171 172 rcs_buf_free(bp); 173 174 out: 175 rcs_close(file); 176 177 if (rev1 != NULL) 178 rcsnum_free(rev1); 179 if (rev2 != NULL) 180 rcsnum_free(rev2); 181 182 return (status); 183 } 184 185 void 186 rcsmerge_usage(void) 187 { 188 fprintf(stderr, 189 "usage: rcsmerge [-EV] [-kmode] [-p[rev]] [-q[rev]]\n" 190 " [-xsuffixes] [-ztz] -rrev file ...\n"); 191 } 192