Annotation of src/usr.bin/rcs/ci.c, Revision 1.109
1.109 ! niallo 1: /* $OpenBSD: ci.c,v 1.108 2006/03/06 14:49:54 jmc Exp $ */
1.1 niallo 2: /*
1.98 niallo 3: * Copyright (c) 2005, 2006 Niall O'Higgins <niallo@openbsd.org>
1.1 niallo 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 cinditions
8: * are met:
9: *
10: * 1. Redistributions of source cide must retain the above cipyright
11: * notice, this list of cinditions 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.91 xsa 27: #include "includes.h"
1.1 niallo 28:
1.92 xsa 29: #include "rcsprog.h"
1.4 niallo 30: #include "diff.h"
1.9 niallo 31:
1.107 deraadt 32: #define CI_OPTSTRING "d::f::i::j::k::l::m:M::N:n:qr::s:Tt:u::Vw:x:"
33: #define DATE_NOW -1
34: #define DATE_MTIME -2
35:
36: #define KW_ID "Id"
37: #define KW_AUTHOR "Author"
38: #define KW_DATE "Date"
39: #define KW_STATE "State"
40: #define KW_REVISION "Revision"
41:
42: #define KW_TYPE_ID 1
43: #define KW_TYPE_AUTHOR 2
44: #define KW_TYPE_DATE 3
45: #define KW_TYPE_STATE 4
46: #define KW_TYPE_REVISION 5
47:
48: #define KW_NUMTOKS_ID 10
49: #define KW_NUMTOKS_AUTHOR 3
50: #define KW_NUMTOKS_DATE 4
51: #define KW_NUMTOKS_STATE 3
52: #define KW_NUMTOKS_REVISION 3
1.58 niallo 53:
1.98 niallo 54: extern struct rcs_kw rcs_expkw[];
55:
1.58 niallo 56: struct checkin_params {
57: int flags, openflags;
58: mode_t fmode;
59: time_t date;
60: RCSFILE *file;
61: RCSNUM *frev, *newrev;
62: char fpath[MAXPATHLEN], *rcs_msg, *username, *deltatext, *filename;
1.98 niallo 63: char *author, *state;
64: const char *symbol, *description;
1.58 niallo 65: };
66:
1.94 niallo 67: static int checkin_attach_symbol(struct checkin_params *);
68: static int checkin_checklock(struct checkin_params *);
1.68 xsa 69: static char *checkin_choose_rcsfile(const char *);
70: static char *checkin_diff_file(struct checkin_params *);
71: static char *checkin_getdesc(void);
72: static char *checkin_getinput(const char *);
73: static char *checkin_getlogmsg(RCSNUM *, RCSNUM *);
74: static int checkin_init(struct checkin_params *);
1.107 deraadt 75: static int checkin_keywordscan(char *, RCSNUM **, time_t *, char **,
1.98 niallo 76: char **);
1.107 deraadt 77: static int checkin_keywordtype(char *);
1.94 niallo 78: static int checkin_mtimedate(struct checkin_params *);
1.107 deraadt 79: static void checkin_parsekeyword(char *, RCSNUM **, time_t *, char **,
1.98 niallo 80: char **);
1.94 niallo 81: static int checkin_update(struct checkin_params *);
82: static void checkin_revert(struct checkin_params *);
1.4 niallo 83:
1.1 niallo 84: void
85: checkin_usage(void)
86: {
87: fprintf(stderr,
1.108 jmc 88: "usage: ci [-jMNqV] [-d[date]] [-f[rev]] [-i[rev]] [-j[rev]]\n"
1.107 deraadt 89: " [-k[rev]] [-l[rev]] [-M[rev]] [-mmsg] [-Nsymbol]\n"
90: " [-nsymbol] [-r[rev]] [-sstate] [-tfile|str] [-u[rev]]\n"
91: " [-wusername] [-xsuffixes] file ...\n");
1.58 niallo 92: }
93:
1.55 niallo 94:
1.1 niallo 95:
96: /*
97: * checkin_main()
98: *
99: * Handler for the `ci' program.
100: * Returns 0 on success, or >0 on error.
101: */
102: int
103: checkin_main(int argc, char **argv)
104: {
1.58 niallo 105: int i, ch, status;
106: struct checkin_params pb;
1.1 niallo 107:
1.58 niallo 108: pb.date = DATE_NOW;
109: pb.file = NULL;
1.98 niallo 110: pb.rcs_msg = pb.username = pb.author = pb.state = NULL;
111: pb.symbol = pb.description = NULL;
1.58 niallo 112: pb.newrev = NULL;
113: pb.fmode = pb.flags = status = 0;
1.1 niallo 114:
1.58 niallo 115: pb.flags = INTERACTIVE;
1.90 niallo 116: pb.openflags = RCS_RDWR|RCS_CREATE|RCS_PARSE_FULLY;
1.9 niallo 117:
1.58 niallo 118: while ((ch = rcs_getopt(argc, argv, CI_OPTSTRING)) != -1) {
1.1 niallo 119: switch (ch) {
1.20 niallo 120: case 'd':
1.25 niallo 121: if (rcs_optarg == NULL)
1.58 niallo 122: pb.date = DATE_MTIME;
1.87 xsa 123: else if ((pb.date = cvs_date_parse(rcs_optarg)) <= 0)
124: fatal("invalid date");
1.20 niallo 125: break;
1.29 niallo 126: case 'f':
1.58 niallo 127: rcs_set_rev(rcs_optarg, &pb.newrev);
128: pb.flags |= FORCE;
1.29 niallo 129: break;
1.1 niallo 130: case 'h':
131: (usage)();
132: exit(0);
1.58 niallo 133: case 'i':
134: rcs_set_rev(rcs_optarg, &pb.newrev);
135: pb.openflags |= RCS_CREATE;
1.66 niallo 136: pb.flags |= CI_INIT;
1.58 niallo 137: break;
138: case 'j':
139: rcs_set_rev(rcs_optarg, &pb.newrev);
140: pb.openflags &= ~RCS_CREATE;
1.66 niallo 141: pb.flags &= ~CI_INIT;
1.58 niallo 142: break;
1.98 niallo 143: case 'k':
144: rcs_set_rev(rcs_optarg, &pb.newrev);
145: pb.flags |= CI_KEYWORDSCAN;
146: break;
1.30 niallo 147: case 'l':
1.58 niallo 148: rcs_set_rev(rcs_optarg, &pb.newrev);
149: pb.flags |= CO_LOCK;
1.54 niallo 150: break;
151: case 'M':
1.58 niallo 152: rcs_set_rev(rcs_optarg, &pb.newrev);
153: pb.flags |= CO_REVDATE;
1.30 niallo 154: break;
1.1 niallo 155: case 'm':
1.58 niallo 156: pb.rcs_msg = rcs_optarg;
1.87 xsa 157: if (pb.rcs_msg == NULL)
158: fatal("missing message for -m option");
1.58 niallo 159: pb.flags &= ~INTERACTIVE;
1.3 joris 160: break;
1.42 niallo 161: case 'N':
1.84 joris 162: pb.symbol = xstrdup(rcs_optarg);
1.87 xsa 163: if (rcs_sym_check(pb.symbol) != 1)
164: fatal("invalid symbol `%s'", pb.symbol);
1.58 niallo 165: pb.flags |= CI_SYMFORCE;
1.42 niallo 166: break;
1.38 niallo 167: case 'n':
1.84 joris 168: pb.symbol = xstrdup(rcs_optarg);
1.87 xsa 169: if (rcs_sym_check(pb.symbol) != 1)
170: fatal("invalid symbol `%s'", pb.symbol);
1.38 niallo 171: break;
1.3 joris 172: case 'q':
173: verbose = 0;
1.1 niallo 174: break;
1.30 niallo 175: case 'r':
1.58 niallo 176: rcs_set_rev(rcs_optarg, &pb.newrev);
177: pb.flags |= CI_DEFAULT;
1.9 niallo 178: break;
1.51 niallo 179: case 's':
1.58 niallo 180: pb.state = rcs_optarg;
1.87 xsa 181: if (rcs_state_check(pb.state) < 0)
182: fatal("invalid state `%s'", pb.state);
1.67 xsa 183: break;
184: case 'T':
185: pb.flags |= PRESERVETIME;
1.51 niallo 186: break;
1.80 niallo 187: case 't':
1.84 joris 188: pb.description = xstrdup(rcs_optarg);
1.80 niallo 189: break;
1.9 niallo 190: case 'u':
1.58 niallo 191: rcs_set_rev(rcs_optarg, &pb.newrev);
192: pb.flags |= CO_UNLOCK;
1.9 niallo 193: break;
1.30 niallo 194: case 'V':
195: printf("%s\n", rcs_version);
196: exit(0);
1.31 niallo 197: case 'w':
1.84 joris 198: pb.author = xstrdup(rcs_optarg);
1.64 xsa 199: break;
200: case 'x':
201: rcs_suffixes = rcs_optarg;
1.31 niallo 202: break;
1.1 niallo 203: default:
204: (usage)();
205: exit(1);
206: }
207: }
208:
1.24 joris 209: argc -= rcs_optind;
210: argv += rcs_optind;
211:
1.1 niallo 212: if (argc == 0) {
213: cvs_log(LP_ERR, "no input file");
214: (usage)();
215: exit(1);
216: }
217:
1.86 xsa 218: if ((pb.username = getlogin()) == NULL)
219: fatal("getlogin failed");
1.31 niallo 220:
221:
1.1 niallo 222: for (i = 0; i < argc; i++) {
1.58 niallo 223: pb.filename = argv[i];
1.1 niallo 224:
1.58 niallo 225: /*
226: * Test for existence of ,v file. If we are expected to
227: * create one, set NEWFILE flag.
228: */
1.71 niallo 229: if (rcs_statfile(pb.filename, pb.fpath, sizeof(pb.fpath)) < 0) {
230: if (pb.openflags & RCS_CREATE)
231: pb.flags |= NEWFILE;
232: else {
233: cvs_log(LP_ERR, "No existing RCS file");
234: status = 1;
235: continue;
236: }
1.66 niallo 237: } else {
238: if (pb.flags & CI_INIT) {
239: cvs_log(LP_ERR, "%s already exists", pb.fpath);
240: status = 1;
241: continue;
242: }
1.58 niallo 243: pb.openflags &= ~RCS_CREATE;
1.66 niallo 244: }
1.63 niallo 245: /*
246: * If we are to create a new ,v file, we must decide where it
247: * should go.
248: */
249: if (pb.flags & NEWFILE) {
250: char *fpath = checkin_choose_rcsfile(pb.filename);
251: if (fpath == NULL) {
252: status = 1;
253: continue;
254: }
255: strlcpy(pb.fpath, fpath, sizeof(pb.fpath));
1.84 joris 256: xfree(fpath);
1.63 niallo 257: }
258:
1.58 niallo 259: pb.file = rcs_open(pb.fpath, pb.openflags, pb.fmode);
260:
1.87 xsa 261: if (pb.file == NULL)
262: fatal("failed to open rcsfile '%s'", pb.fpath);
1.29 niallo 263:
1.58 niallo 264: if (verbose == 1)
265: printf("%s <-- %s\n", pb.fpath, pb.filename);
1.107 deraadt 266:
1.63 niallo 267: if (pb.flags & NEWFILE)
268: status = checkin_init(&pb);
1.15 niallo 269: else
1.63 niallo 270: status = checkin_update(&pb);
1.4 niallo 271: }
272:
1.93 xsa 273: if (verbose == 1)
274: printf("done\n");
275:
1.16 niallo 276: return (status);
1.4 niallo 277: }
278:
1.59 niallo 279: /*
280: * checkin_diff_file()
281: *
1.65 xsa 282: * Generate the diff between the working file and a revision.
1.59 niallo 283: * Returns pointer to a char array on success, NULL on failure.
284: */
1.4 niallo 285: static char *
1.58 niallo 286: checkin_diff_file(struct checkin_params *pb)
1.4 niallo 287: {
288: char path1[MAXPATHLEN], path2[MAXPATHLEN];
289: BUF *b1, *b2, *b3;
290: char rbuf[64], *deltatext;
291:
1.58 niallo 292: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.4 niallo 293:
1.58 niallo 294: if ((b1 = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
295: cvs_log(LP_ERR, "failed to load file: '%s'", pb->filename);
1.4 niallo 296: return (NULL);
1.1 niallo 297: }
298:
1.58 niallo 299: if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.4 niallo 300: cvs_log(LP_ERR, "failed to load revision");
301: cvs_buf_free(b1);
302: return (NULL);
303: }
304:
1.57 xsa 305: if ((b3 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL) {
1.4 niallo 306: cvs_log(LP_ERR, "failed to allocated buffer for diff");
307: cvs_buf_free(b1);
308: cvs_buf_free(b2);
309: return (NULL);
310: }
311:
1.50 xsa 312: strlcpy(path1, rcs_tmpdir, sizeof(path1));
313: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4 niallo 314: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
315: cvs_log(LP_ERRNO, "could not write temporary file");
316: cvs_buf_free(b1);
317: cvs_buf_free(b2);
318: return (NULL);
319: }
320: cvs_buf_free(b1);
321:
1.50 xsa 322: strlcpy(path2, rcs_tmpdir, sizeof(path2));
323: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4 niallo 324: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
325: cvs_buf_free(b2);
326: (void)unlink(path1);
327: return (NULL);
328: }
329: cvs_buf_free(b2);
330:
1.5 niallo 331: diff_format = D_RCSDIFF;
1.4 niallo 332: cvs_diffreg(path1, path2, b3);
333: (void)unlink(path1);
334: (void)unlink(path2);
335:
336: cvs_buf_putc(b3, '\0');
337: deltatext = (char *)cvs_buf_release(b3);
338:
339: return (deltatext);
1.6 niallo 340: }
341:
342: /*
1.65 xsa 343: * checkin_getlogmsg()
1.59 niallo 344: *
1.6 niallo 345: * Get log message from user interactively.
1.59 niallo 346: * Returns pointer to a char array on success, NULL on failure.
1.6 niallo 347: */
348: static char *
1.34 niallo 349: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2)
1.6 niallo 350: {
1.58 niallo 351: char *rcs_msg, nrev[16], prev[16];
1.6 niallo 352: RCSNUM *tmprev;
353:
1.7 niallo 354: rcs_msg = NULL;
1.6 niallo 355: tmprev = rcsnum_alloc();
356: rcsnum_cpy(rev, tmprev, 16);
1.9 niallo 357: rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12 niallo 358: if (rev2 == NULL)
359: rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
360: else
361: rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6 niallo 362: rcsnum_free(tmprev);
363:
1.47 niallo 364: if (verbose == 1)
365: printf("new revision: %s; previous revision: %s\n", nrev,
366: prev);
1.32 joris 367:
1.107 deraadt 368: rcs_msg = checkin_getinput("enter log message, terminated with a "
369: "single '.' or end of file:\n>> ");
1.58 niallo 370: return (rcs_msg);
371: }
372:
373:
374: /*
375: * checkin_getdesc()
376: *
377: * Get file description interactively.
1.59 niallo 378: * Returns pointer to a char array on success, NULL on failure.
1.58 niallo 379: */
380: static char *
381: checkin_getdesc()
382: {
383: char *description;
384:
1.107 deraadt 385: description = checkin_getinput("enter description, terminated with "
386: "single '.' or end of file:\n"
387: "NOTE: This is NOT the log message!\n>> ");
1.58 niallo 388: return (description);
389: }
390:
391: /*
392: * checkin_getinput()
393: *
1.59 niallo 394: * Get some input from the user, in RCS style, prompting with message <prompt>.
395: * Returns pointer to a char array on success, NULL on failure.
1.58 niallo 396: */
397: static char *
398: checkin_getinput(const char *prompt)
399: {
400: BUF *inputbuf;
401: char *input, buf[128];
402:
403: if ((inputbuf = cvs_buf_alloc((size_t)64, BUF_AUTOEXT)) == NULL) {
404: cvs_log(LP_ERR, "failed to allocate input buffer");
405: return (NULL);
406: }
407:
408: printf(prompt);
1.6 niallo 409: for (;;) {
410: fgets(buf, (int)sizeof(buf), stdin);
1.9 niallo 411: if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6 niallo 412: break;
1.58 niallo 413: cvs_buf_append(inputbuf, buf, strlen(buf));
1.46 joris 414: printf(">> ");
1.6 niallo 415: }
1.32 joris 416:
1.58 niallo 417: cvs_buf_putc(inputbuf, '\0');
418: input = (char *)cvs_buf_release(inputbuf);
419:
420: return (input);
421: }
422:
423: /*
1.63 niallo 424: * checkin_update()
425: *
426: * Do a checkin to an existing RCS file.
427: *
428: * On success, return 0. On error return -1.
429: */
430: static int
431: checkin_update(struct checkin_params *pb)
432: {
1.77 xsa 433: char *filec, numb1[64], numb2[64];
1.63 niallo 434: BUF *bp;
435:
1.82 joris 436: /*
437: * XXX this is wrong, we need to get the revision the user
1.85 xsa 438: * has the lock for. So we can decide if we want to create a
1.82 joris 439: * branch or not. (if it's not current HEAD we need to branch).
440: */
1.63 niallo 441: pb->frev = pb->file->rf_head;
442:
1.82 joris 443: if (checkin_checklock(pb) < 0)
444: return (-1);
445:
446: /* If revision passed on command line is less than HEAD, bail.
447: * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
448: * there is no lock set for the user.
449: */
1.63 niallo 450: if ((pb->newrev != NULL)
451: && (rcsnum_cmp(pb->newrev, pb->frev, 0) > 0)) {
1.77 xsa 452: cvs_log(LP_ERR,
453: "%s: revision %s too low; must be higher than %s",
454: pb->file->rf_path,
455: rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
456: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.63 niallo 457: rcs_close(pb->file);
458: return (-1);
459: }
460:
1.104 niallo 461: /*
462: * Set the date of the revision to be the last modification
463: * time of the working file if -d has no argument.
464: */
465: if (pb->date == DATE_MTIME
466: && (checkin_mtimedate(pb) < 0))
467: return (-1);
468:
469: /* Date from argv/mtime must be more recent than HEAD */
470: if (pb->date != DATE_NOW) {
471: time_t head_date = rcs_rev_getdate(pb->file, pb->frev);
472: if (pb->date <= head_date) {
473: char *head_date_str, *tdate;
474: head_date_str = xstrdup(ctime(&head_date));
475: head_date_str[strlen(head_date_str) - 1] = '\0';
476: tdate = xstrdup(ctime(&pb->date));
477: tdate[strlen(tdate) - 1] = '\0';
478: cvs_log(LP_ERR,
479: "%s: Date %s preceeds %s in revision %s",
480: pb->file->rf_path, tdate, head_date_str,
481: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
482: rcs_close(pb->file);
483: xfree(head_date_str);
484: xfree(tdate);
485: return (-1);
486: }
487: }
488:
1.73 xsa 489: /* Load file contents */
1.63 niallo 490: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
1.106 niallo 491: rcs_close(pb->file);
1.63 niallo 492: return (-1);
493: }
494:
1.106 niallo 495: if (cvs_buf_putc(bp, '\0') < 0) {
496: rcs_close(pb->file);
1.63 niallo 497: return (-1);
1.106 niallo 498: }
1.63 niallo 499:
500: filec = (char *)cvs_buf_release(bp);
501:
1.73 xsa 502: /* Get RCS patch */
1.63 niallo 503: if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
504: cvs_log(LP_ERR, "failed to get diff");
1.106 niallo 505: rcs_close(pb->file);
1.63 niallo 506: return (-1);
507: }
508:
509: /*
510: * If -f is not specified and there are no differences, tell
511: * the user and revert to latest version.
512: */
513: if (!(pb->flags & FORCE) && (strlen(pb->deltatext) < 1)) {
514: checkin_revert(pb);
515: return (0);
516: }
517:
1.73 xsa 518: /* If no log message specified, get it interactively. */
1.63 niallo 519: if (pb->flags & INTERACTIVE)
520: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
521:
1.82 joris 522: if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) {
1.63 niallo 523: if (rcs_errno != RCS_ERR_NOENT)
1.82 joris 524: cvs_log(LP_WARN, "failed to remove lock");
525: else if (!(pb->flags & CO_LOCK))
526: cvs_log(LP_WARN, "previous revision was not locked; "
527: "ignoring -l option");
1.63 niallo 528: }
529:
1.73 xsa 530: /* Current head revision gets the RCS patch as rd_text */
1.87 xsa 531: if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1)
532: fatal("failed to set new rd_text for head rev");
1.63 niallo 533:
1.73 xsa 534: /* Now add our new revision */
1.63 niallo 535: if (rcs_rev_add(pb->file,
536: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82 joris 537: pb->rcs_msg, pb->date, pb->author) != 0) {
1.63 niallo 538: cvs_log(LP_ERR, "failed to add new revision");
1.106 niallo 539: rcs_close(pb->file);
1.63 niallo 540: return (-1);
541: }
542:
543: /*
544: * If we are checking in to a non-default (ie user-specified)
545: * revision, set head to this revision.
546: */
547: if (pb->newrev != NULL)
548: rcs_head_set(pb->file, pb->newrev);
549: else
550: pb->newrev = pb->file->rf_head;
551:
1.73 xsa 552: /* New head revision has to contain entire file; */
1.107 deraadt 553: if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1)
1.87 xsa 554: fatal("failed to set new head revision");
1.63 niallo 555:
1.73 xsa 556: /* Attach a symbolic name to this revision if specified. */
1.63 niallo 557: if (pb->symbol != NULL
558: && (checkin_attach_symbol(pb) < 0))
559: return (-1);
560:
1.73 xsa 561: /* Set the state of this revision if specified. */
1.63 niallo 562: if (pb->state != NULL)
563: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
564:
1.84 joris 565: xfree(pb->deltatext);
566: xfree(filec);
1.63 niallo 567: (void)unlink(pb->filename);
568:
1.73 xsa 569: /* Do checkout if -u or -l are specified. */
1.63 niallo 570: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
571: && !(pb->flags & CI_DEFAULT))
572: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 573: pb->username, pb->author, NULL, NULL);
1.63 niallo 574:
575: /* File will NOW be synced */
576: rcs_close(pb->file);
577:
578: if (pb->flags & INTERACTIVE) {
1.84 joris 579: xfree(pb->rcs_msg);
1.63 niallo 580: pb->rcs_msg = NULL;
581: }
582: return (0);
583: }
584:
585: /*
1.58 niallo 586: * checkin_init()
1.65 xsa 587: *
1.58 niallo 588: * Does an initial check in, just enough to create the new ,v file
1.63 niallo 589: * On success, return 0. On error return -1.
1.58 niallo 590: */
1.63 niallo 591: static int
1.58 niallo 592: checkin_init(struct checkin_params *pb)
593: {
1.80 niallo 594: BUF *bp, *dp;
1.93 xsa 595: char *filec, numb[64];
1.80 niallo 596: const char *rcs_desc;
1.58 niallo 597:
1.73 xsa 598: /* Load file contents */
1.58 niallo 599: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
1.106 niallo 600: rcs_close(pb->file);
1.63 niallo 601: return (-1);
1.58 niallo 602: }
603:
1.106 niallo 604: if (cvs_buf_putc(bp, '\0') < 0) {
605: rcs_close(pb->file);
1.63 niallo 606: return (-1);
1.106 niallo 607: }
1.58 niallo 608:
609: filec = (char *)cvs_buf_release(bp);
610:
1.98 niallo 611: /* Get default values from working copy if -k specified */
612: if (pb->flags & CI_KEYWORDSCAN)
613: checkin_keywordscan(filec, &pb->newrev, &pb->date, &pb->state,
614: &pb->author);
615:
1.73 xsa 616: /* Get description from user */
1.80 niallo 617: if (pb->description == NULL)
618: rcs_desc = (const char *)checkin_getdesc();
619: else {
620: if (*pb->description == '-') {
621: pb->description++;
1.81 niallo 622: rcs_desc = (const char *)pb->description;
623: } else {
1.80 niallo 624: dp = cvs_buf_load(pb->description, BUF_AUTOEXT);
625: if (dp == NULL) {
626: cvs_log(LP_ERR,
627: "failed to load description file '%s'",
628: pb->description);
1.84 joris 629: xfree(filec);
1.106 niallo 630: rcs_close(pb->file);
1.80 niallo 631: return (-1);
632: }
633: if (cvs_buf_putc(dp, '\0') < 0) {
1.84 joris 634: xfree(filec);
1.106 niallo 635: rcs_close(pb->file);
1.80 niallo 636: return (-1);
637: }
638: rcs_desc = (const char *)cvs_buf_release(dp);
639: }
640: }
1.58 niallo 641: rcs_desc_set(pb->file, rcs_desc);
642:
1.97 niallo 643: /*
644: * Set the date of the revision to be the last modification
645: * time of the working file if -d has no argument.
646: */
647: if (pb->date == DATE_MTIME
648: && (checkin_mtimedate(pb) < 0))
649: return (-1);
650:
1.73 xsa 651: /* Now add our new revision */
1.96 niallo 652: if (rcs_rev_add(pb->file,
653: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.107 deraadt 654: (pb->rcs_msg == NULL ? "Initial revision" : pb->rcs_msg),
1.103 niallo 655: pb->date, pb->author) != 0) {
1.58 niallo 656: cvs_log(LP_ERR, "failed to add new revision");
1.106 niallo 657: rcs_close(pb->file);
1.63 niallo 658: return (-1);
659: }
660: /*
661: * If we are checking in to a non-default (ie user-specified)
662: * revision, set head to this revision.
663: */
664: if (pb->newrev != NULL)
665: rcs_head_set(pb->file, pb->newrev);
666: else
667: pb->newrev = pb->file->rf_head;
668:
1.73 xsa 669: /* New head revision has to contain entire file; */
1.66 niallo 670: if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63 niallo 671: cvs_log(LP_ERR, "failed to set new head revision");
1.106 niallo 672: rcs_close(pb->file);
1.63 niallo 673: return (-1);
1.58 niallo 674: }
1.73 xsa 675: /* Attach a symbolic name to this revision if specified. */
1.66 niallo 676: if (pb->symbol != NULL
677: && (checkin_attach_symbol(pb) < 0))
678: return (-1);
679:
1.73 xsa 680: /* Set the state of this revision if specified. */
1.66 niallo 681: if (pb->state != NULL)
682: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
683:
1.84 joris 684: xfree(filec);
1.66 niallo 685: (void)unlink(pb->filename);
686:
1.73 xsa 687: /* Do checkout if -u or -l are specified. */
1.66 niallo 688: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
689: && !(pb->flags & CI_DEFAULT))
690: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 691: pb->username, pb->author, NULL, NULL);
1.63 niallo 692:
1.93 xsa 693: printf("initial revision: %s\n",
694: rcsnum_tostr(pb->newrev, numb, sizeof(numb)));
695:
1.66 niallo 696: /* File will NOW be synced */
697: rcs_close(pb->file);
1.93 xsa 698:
1.63 niallo 699: return (0);
1.58 niallo 700: }
701:
1.59 niallo 702: /*
703: * checkin_attach_symbol()
704: *
705: * Attempt to attach the specified symbol to the revision.
706: * On success, return 0. On error return -1.
707: */
1.58 niallo 708: static int
709: checkin_attach_symbol(struct checkin_params *pb)
710: {
711: char rbuf[16];
712: int ret;
713: if (verbose == 1)
714: printf("symbol: %s\n", pb->symbol);
715: if (pb->flags & CI_SYMFORCE)
716: rcs_sym_remove(pb->file, pb->symbol);
717: if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
718: && (rcs_errno == RCS_ERR_DUPENT)) {
719: rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
720: rbuf, sizeof(rbuf));
721: cvs_log(LP_ERR,
722: "symbolic name %s already bound to %s",
723: pb->symbol, rbuf);
724: rcs_close(pb->file);
725: return (-1);
726: } else if (ret == -1) {
727: cvs_log(LP_ERR, "problem adding symbol: %s",
728: pb->symbol);
729: rcs_close(pb->file);
730: return (-1);
731: }
732: return (0);
733: }
734:
1.59 niallo 735: /*
736: * checkin_revert()
737: *
738: * If there are no differences between the working file and the latest revision
739: * and the -f flag is not specified, simply revert to the latest version and
740: * warn the user.
741: *
742: */
1.58 niallo 743: static void
744: checkin_revert(struct checkin_params *pb)
745: {
746: char rbuf[16];
747:
748: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
749: cvs_log(LP_WARN,
750: "file is unchanged; reverting to previous revision %s",
751: rbuf);
752: (void)unlink(pb->filename);
753: if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
754: checkout_rev(pb->file, pb->frev, pb->filename,
1.89 joris 755: pb->flags, pb->username, pb->author, NULL, NULL);
1.82 joris 756: rcs_lock_remove(pb->file, pb->username, pb->frev);
1.58 niallo 757: rcs_close(pb->file);
758: }
759:
1.59 niallo 760: /*
761: * checkin_checklock()
762: *
763: * Check for the existence of a lock on the file. If there are no locks, or it
764: * is not locked by the correct user, return -1. Otherwise, return 0.
765: */
1.58 niallo 766: static int
767: checkin_checklock(struct checkin_params *pb)
768: {
769: struct rcs_lock *lkp;
770:
1.82 joris 771: TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
772: if ((!strcmp(lkp->rl_name, pb->username)) &&
773: (!rcsnum_cmp(lkp->rl_num, pb->frev, 0)))
774: return (0);
1.58 niallo 775: }
1.32 joris 776:
1.82 joris 777: cvs_log(LP_ERR,
778: "%s: no lock set by %s", pb->file->rf_path, pb->username);
779: rcs_close(pb->file);
780: return (-1);
1.61 niallo 781: }
782:
783: /*
1.62 niallo 784: * checkin_mtimedate()
1.61 niallo 785: *
786: * Set the date of the revision to be the last modification
1.62 niallo 787: * time of the working file.
1.61 niallo 788: *
789: * On success, return 0. On error return -1.
790: */
791: static int
1.62 niallo 792: checkin_mtimedate(struct checkin_params *pb)
1.61 niallo 793: {
794: struct stat sb;
795: if (stat(pb->filename, &sb) != 0) {
1.72 xsa 796: cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61 niallo 797: rcs_close(pb->file);
798: return (-1);
799: }
800: pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58 niallo 801: return (0);
1.63 niallo 802: }
803:
804: /*
1.75 niallo 805: * checkin_choose_rcsfile()
1.63 niallo 806: *
807: * Given a relative filename, decide where the corresponding ,v file
1.65 xsa 808: * should be.
1.63 niallo 809: *
810: * Returns pointer to a char array on success, NULL on failure.
811: */
812: static char *
813: checkin_choose_rcsfile(const char *filename)
814: {
1.76 niallo 815: char name[MAXPATHLEN], *basepath;
816: const char *ptr;
1.63 niallo 817: size_t len;
818: struct stat sb;
819:
1.84 joris 820: basepath = xmalloc(MAXPATHLEN);
1.88 alek 821: basepath[0] = '\0';
1.76 niallo 822: if (strchr(filename, '/') == NULL) {
823: strlcat(basepath, RCSDIR"/", MAXPATHLEN);
824: if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
825: /* <path>/RCS/<filename>,v */
826: strlcat(basepath, filename, MAXPATHLEN);
827: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
828: } else {
829: /* <path>/<filename>,v */
830: strlcpy(basepath, filename, MAXPATHLEN);
831: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
832: }
833: } else {
834: ptr = filename;
835: /* Walk backwards till we find the base directory */
836: len = strlen(filename);
837: ptr += len + 1;
838: while (filename[len] != '/') {
839: len--;
840: ptr--;
841: }
1.63 niallo 842: /*
843: * Need two bytes extra for trailing slash and
844: * NUL-termination.
845: */
846: len += 2;
1.76 niallo 847: if (len > MAXPATHLEN) {
1.84 joris 848: xfree(basepath);
1.63 niallo 849: return (NULL);
850: }
1.76 niallo 851: strlcpy(basepath, filename, len);
852: strlcpy(name, ptr, MAXPATHLEN);
1.63 niallo 853: strlcat(basepath, RCSDIR"/", MAXPATHLEN);
854: if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
855: /* <path>/RCS/<filename>,v */
1.76 niallo 856: strlcat(basepath, name, MAXPATHLEN);
1.63 niallo 857: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
858: } else {
859: /* <path>/<filename>,v */
1.76 niallo 860: strlcpy(basepath, filename, MAXPATHLEN);
1.63 niallo 861: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
862: }
1.76 niallo 863: }
864: return (basepath);
1.98 niallo 865: }
866:
867: /*
868: * checkin_keywordscan()
869: *
870: * Searches working file for keyword values to determine its revision
871: * number, creation date and author, and uses these values instead of
872: * calculating them locally.
873: *
874: * Params: The data buffer to scan and pointers to pointers of variables in
875: * which to store the outputs.
876: *
877: * On success, return 0. On error return -1.
878: */
879: static int
880: checkin_keywordscan(char *data, RCSNUM **rev, time_t *date, char **author,
881: char **state)
882: {
883: size_t len;
884: u_int i, j, found, end;
885: char *c, *kwstr, *start, buf[128];
886:
887: c = start = kwstr = NULL;
888:
889: i = found = 0;
890:
891: len = strlen(data);
892: for (c = data; *c != '\0' && i < len; *c++) {
893: if (*c == '$') {
894: start = c;
895: *c++;
896: if (!isalpha(*c)) {
897: c = start;
898: continue;
899: }
900: /* look for any matching keywords */
1.107 deraadt 901: found = 0;
902: for (j = 0; j < 10; j++) {
903: if (!strncmp(c, rcs_expkw[j].kw_str,
904: strlen(rcs_expkw[j].kw_str))) {
905: found = 1;
906: kwstr = rcs_expkw[j].kw_str;
907: break;
908: }
909: }
910:
911: /* unknown keyword, continue looking */
912: if (found == 0) {
913: c = start;
914: continue;
915: }
1.98 niallo 916:
917: c += strlen(kwstr);
918: if (*c != ':' && *c != '$') {
919: c = start;
920: continue;
921: }
922:
923: if (*c == ':') {
924: while (*c++) {
925: if (*c == '$') {
926: end = c - start + 2;
927: if (end >= sizeof(buf))
928: fatal("keyword buffer"
929: " too small!");
930: strlcpy(buf, start, end);
931: checkin_parsekeyword(buf, rev,
1.107 deraadt 932: date, author, state);
1.98 niallo 933: break;
934: }
935: }
936:
937: if (*c != '$') {
938: c = start;
939: continue;
940: }
941: }
942: }
943: }
944: if (found == 0)
945: return (-1);
946: else
947: return (0);
948: }
949:
950: /*
951: * checkin_keywordtype()
952: *
953: * Given an RCS keyword string, determine what type of string it is.
954: * This enables us to know what data should be in it.
955: *
956: * Returns type on success, or -1 on failure.
1.107 deraadt 957: */
1.98 niallo 958: static int
959: checkin_keywordtype(char *keystring)
960: {
961: char *p;
1.107 deraadt 962:
1.98 niallo 963: p = keystring;
964: *p++;
965: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0)
966: return (KW_TYPE_ID);
967: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
968: return (KW_TYPE_AUTHOR);
969: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
970: return (KW_TYPE_DATE);
971: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
972: return (KW_TYPE_STATE);
973: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
974: return (KW_TYPE_REVISION);
975: else
976: return (-1);
977: }
978:
979: /*
980: * checkin_parsekeyword()
981: *
982: * Do the actual parsing of an RCS keyword string, setting the values passed
983: * to the function to whatever is found.
984: *
985: */
986: static void
987: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
988: char **author, char **state)
989: {
990: char *tokens[10], *p, *datestring;
991: size_t len = 0;
1.99 niallo 992: int i = 0;
1.107 deraadt 993:
994: /* Parse data out of the expanded keyword */
1.98 niallo 995: switch (checkin_keywordtype(keystring)) {
996: case KW_TYPE_ID:
1.101 niallo 997: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 998: (p = strtok(NULL, " "))) {
1.99 niallo 999: if (i < KW_NUMTOKS_ID - 1)
1000: tokens[i++] = p;
1.107 deraadt 1001: }
1.99 niallo 1002: tokens[i] = NULL;
1.98 niallo 1003: if (*author != NULL)
1004: xfree(*author);
1005: if (*state != NULL)
1006: xfree(*state);
1007: /* only parse revision if one is not already set */
1008: if (*rev == NULL) {
1009: if ((*rev = rcsnum_parse(tokens[2])) == NULL)
1010: fatal("could not parse rcsnum");
1011: }
1012: len = strlen(tokens[5]) + 1;
1013: *author = xmalloc(len);
1014: strlcpy(*author, tokens[5], len);
1015: len = strlen(tokens[6]) + 1;
1016: *state = xmalloc(len);
1017: strlcpy(*state, tokens[6], len);
1018: len = strlen(tokens[3]) + strlen(tokens[4]) + 2;
1019: datestring = xmalloc(len);
1020: strlcpy(datestring, tokens[3], len);
1021: strlcat(datestring, " ", len);
1022: strlcat(datestring, tokens[4], len);
1023: if ((*date = cvs_date_parse(datestring)) <= 0)
1024: fatal("could not parse date\n");
1025: xfree(datestring);
1026: break;
1027: case KW_TYPE_AUTHOR:
1.101 niallo 1028: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1029: (p = strtok(NULL, " "))) {
1.99 niallo 1030: if (i < KW_NUMTOKS_AUTHOR - 1)
1031: tokens[i++] = p;
1.107 deraadt 1032: }
1.98 niallo 1033: if (*author != NULL)
1034: xfree(*author);
1035: len = strlen(tokens[1]) + 1;
1036: *author = xmalloc(len);
1037: strlcpy(*author, tokens[1], len);
1038: break;
1039: case KW_TYPE_DATE:
1.101 niallo 1040: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1041: (p = strtok(NULL, " "))) {
1.99 niallo 1042: if (i < KW_NUMTOKS_DATE - 1)
1043: tokens[i++] = p;
1.98 niallo 1044: }
1045: len = strlen(tokens[1]) + strlen(tokens[2]) + 2;
1046: datestring = xmalloc(len);
1047: strlcpy(datestring, tokens[1], len);
1048: strlcat(datestring, " ", len);
1049: strlcat(datestring, tokens[2], len);
1050: if ((*date = cvs_date_parse(datestring)) <= 0)
1051: fatal("could not parse date\n");
1052: xfree(datestring);
1053: break;
1054: case KW_TYPE_STATE:
1.101 niallo 1055: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1056: (p = strtok(NULL, " "))) {
1.99 niallo 1057: if (i < KW_NUMTOKS_STATE - 1)
1058: tokens[i++] = p;
1.107 deraadt 1059: }
1.98 niallo 1060: if (*state != NULL)
1061: xfree(*state);
1062: len = strlen(tokens[1]) + 1;
1063: *state = xmalloc(len);
1064: strlcpy(*state, tokens[1], len);
1065: break;
1066: case KW_TYPE_REVISION:
1067: /* only parse revision if one is not already set */
1068: if (*rev != NULL)
1069: break;
1.102 niallo 1070: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1071: (p = strtok(NULL, " "))) {
1.99 niallo 1072: if (i < KW_NUMTOKS_REVISION - 1)
1073: tokens[i++] = p;
1.107 deraadt 1074: }
1.98 niallo 1075: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1076: fatal("could not parse rcsnum");
1077: break;
1078: }
1.1 niallo 1079: }