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