Annotation of src/usr.bin/cvs/rcsnum.c, Revision 1.51
1.51 ! joris 1: /* $OpenBSD: rcsnum.c,v 1.50 2008/01/31 22:19:36 tobias Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.4 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.4 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.4 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.4 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 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
1.4 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
1.42 otto 27: #include <ctype.h>
28: #include <string.h>
1.1 jfb 29:
1.15 joris 30: #include "cvs.h"
1.1 jfb 31:
1.33 xsa 32: static void rcsnum_setsize(RCSNUM *, u_int);
1.23 xsa 33: static char *rcsnum_itoa(u_int16_t, char *, size_t);
1.11 jfb 34:
1.1 jfb 35: /*
36: * rcsnum_alloc()
37: *
1.30 ray 38: * Allocate an RCS number structure and return a pointer to it.
1.1 jfb 39: */
1.14 xsa 40: RCSNUM *
1.1 jfb 41: rcsnum_alloc(void)
42: {
43: RCSNUM *rnp;
44:
1.32 ray 45: rnp = xmalloc(sizeof(*rnp));
1.1 jfb 46: rnp->rn_len = 0;
47: rnp->rn_id = NULL;
48:
49: return (rnp);
1.50 tobias 50: }
51:
52: /*
53: * rcsnum_addmagic()
54: *
55: * Adds a magic branch number to an RCS number.
56: * Returns 0 on success, or -1 on failure.
57: */
58: int
59: rcsnum_addmagic(RCSNUM *rn)
60: {
61: if (!rn->rn_len || rn->rn_len > RCSNUM_MAXLEN - 1)
62: return -1;
63: rcsnum_setsize(rn, rn->rn_len + 1);
64: rn->rn_id[rn->rn_len - 1] = rn->rn_id[rn->rn_len - 2];
65: rn->rn_id[rn->rn_len - 2] = 0;
66:
67: return 0;
1.1 jfb 68: }
69:
70: /*
1.7 jfb 71: * rcsnum_parse()
72: *
73: * Parse a string specifying an RCS number and return the corresponding RCSNUM.
74: */
1.14 xsa 75: RCSNUM *
1.7 jfb 76: rcsnum_parse(const char *str)
77: {
78: char *ep;
79: RCSNUM *num;
80:
1.19 joris 81: num = rcsnum_alloc();
1.34 deraadt 82: if (rcsnum_aton(str, &ep, num) < 0 || *ep != '\0') {
1.7 jfb 83: rcsnum_free(num);
1.9 jfb 84: num = NULL;
85: if (*ep != '\0')
86: rcs_errno = RCS_ERR_BADNUM;
1.7 jfb 87: }
88:
89: return (num);
90: }
91:
92: /*
1.1 jfb 93: * rcsnum_free()
94: *
95: * Free an RCSNUM structure previously allocated with rcsnum_alloc().
96: */
97: void
98: rcsnum_free(RCSNUM *rn)
99: {
100: if (rn->rn_id != NULL)
1.18 joris 101: xfree(rn->rn_id);
102: xfree(rn);
1.1 jfb 103: }
104:
105: /*
106: * rcsnum_tostr()
1.10 jfb 107: *
108: * Format the RCS number <nump> into a human-readable dot-separated
109: * representation and store the resulting string in <buf>, which is of size
110: * <blen>.
1.25 ray 111: * Returns a pointer to the start of <buf>. On failure <buf> is set to
112: * an empty string.
1.1 jfb 113: */
1.13 xsa 114: char *
1.1 jfb 115: rcsnum_tostr(const RCSNUM *nump, char *buf, size_t blen)
116: {
117: u_int i;
118: char tmp[8];
119:
1.34 deraadt 120: if (nump == NULL || nump->rn_len == 0) {
1.1 jfb 121: buf[0] = '\0';
122: return (buf);
123: }
124:
1.40 xsa 125: if (strlcpy(buf, rcsnum_itoa(nump->rn_id[0], buf, blen), blen) >= blen)
126: fatal("rcsnum_tostr: truncation");
1.1 jfb 127: for (i = 1; i < nump->rn_len; i++) {
1.40 xsa 128: const char *str;
129:
130: str = rcsnum_itoa(nump->rn_id[i], tmp, sizeof(tmp));
131: if (strlcat(buf, ".", blen) >= blen ||
132: strlcat(buf, str, blen) >= blen)
133: fatal("rcsnum_tostr: truncation");
1.1 jfb 134: }
135:
136: return (buf);
1.20 niallo 137: }
138:
139: static char *
140: rcsnum_itoa(u_int16_t num, char *buf, size_t len)
141: {
1.21 reyk 142: u_int16_t i;
143: char *p;
1.20 niallo 144:
1.26 niallo 145: if (num == 0)
146: return "0";
1.35 deraadt 147:
1.21 reyk 148: p = buf + len - 1;
149: i = num;
1.20 niallo 150: bzero(buf, len);
1.21 reyk 151: while (i) {
152: *--p = '0' + (i % 10);
153: i /= 10;
154: }
155: return (p);
1.1 jfb 156: }
157:
158: /*
159: * rcsnum_cpy()
160: *
161: * Copy the number stored in <nsrc> in the destination <ndst> up to <depth>
1.30 ray 162: * numbers deep. If <depth> is 0, there is no depth limit.
1.1 jfb 163: */
1.30 ray 164: void
1.1 jfb 165: rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth)
166: {
167: u_int len;
168:
169: len = nsrc->rn_len;
1.34 deraadt 170: if (depth != 0 && len > depth)
1.1 jfb 171: len = depth;
172:
1.41 ray 173: rcsnum_setsize(ndst, len);
174: /* Overflow checked in rcsnum_setsize(). */
1.39 ray 175: (void)memcpy(ndst->rn_id, nsrc->rn_id,
176: len * sizeof(*(nsrc->rn_id)));
1.1 jfb 177: }
178:
179: /*
180: * rcsnum_cmp()
181: *
182: * Compare the two numbers <n1> and <n2>. Returns -1 if <n1> is larger than
183: * <n2>, 0 if they are both the same, and 1 if <n2> is larger than <n1>.
184: * The <depth> argument specifies how many numbers deep should be checked for
185: * the result. A value of 0 means that the depth will be the minimum of the
186: * two numbers.
187: */
188: int
1.38 joris 189: rcsnum_cmp(RCSNUM *n1, RCSNUM *n2, u_int depth)
1.1 jfb 190: {
191: int res;
192: u_int i;
193: size_t slen;
194:
1.38 joris 195: if (!rcsnum_differ(n1, n2))
196: return (0);
197:
1.1 jfb 198: slen = MIN(n1->rn_len, n2->rn_len);
1.34 deraadt 199: if (depth != 0 && slen > depth)
1.1 jfb 200: slen = depth;
1.4 tedu 201:
1.1 jfb 202: for (i = 0; i < slen; i++) {
203: res = n1->rn_id[i] - n2->rn_id[i];
204: if (res < 0)
205: return (1);
206: else if (res > 0)
207: return (-1);
208: }
209:
210: if (n1->rn_len > n2->rn_len)
211: return (-1);
212: else if (n2->rn_len > n1->rn_len)
213: return (1);
214:
215: return (0);
216: }
217:
218: /*
219: * rcsnum_aton()
220: *
221: * Translate the string <str> containing a sequence of digits and periods into
222: * its binary representation, which is stored in <nump>. The address of the
223: * first byte not part of the number is stored in <ep> on return, if it is not
224: * NULL.
225: * Returns 0 on success, or -1 on failure.
226: */
227: int
228: rcsnum_aton(const char *str, char **ep, RCSNUM *nump)
229: {
1.6 jfb 230: u_int32_t val;
1.1 jfb 231: const char *sp;
1.15 joris 232: char *s;
1.1 jfb 233:
1.18 joris 234: if (nump->rn_id == NULL)
1.32 ray 235: nump->rn_id = xmalloc(sizeof(*(nump->rn_id)));
1.1 jfb 236:
237: nump->rn_len = 0;
1.6 jfb 238: nump->rn_id[0] = 0;
1.1 jfb 239:
1.3 jfb 240: for (sp = str;; sp++) {
241: if (!isdigit(*sp) && (*sp != '.'))
1.1 jfb 242: break;
243:
244: if (*sp == '.') {
1.6 jfb 245: if (nump->rn_len >= RCSNUM_MAXLEN - 1) {
1.11 jfb 246: rcs_errno = RCS_ERR_BADNUM;
1.6 jfb 247: goto rcsnum_aton_failed;
248: }
249:
1.1 jfb 250: nump->rn_len++;
1.45 ray 251: nump->rn_id = xrealloc(nump->rn_id,
1.32 ray 252: nump->rn_len + 1, sizeof(*(nump->rn_id)));
1.2 vincent 253: nump->rn_id[nump->rn_len] = 0;
1.1 jfb 254: continue;
255: }
256:
1.43 ray 257: val = (nump->rn_id[nump->rn_len] * 10) + (*sp - '0');
1.24 niallo 258: if (val > RCSNUM_MAXNUM)
259: fatal("RCSNUM overflow!");
1.6 jfb 260:
261: nump->rn_id[nump->rn_len] = val;
1.1 jfb 262: }
263:
264: if (ep != NULL)
1.5 jfb 265: *(const char **)ep = sp;
1.15 joris 266:
267: /*
268: * Handle "magic" RCS branch numbers.
269: *
270: * What are they?
271: *
272: * Magic branch numbers have an extra .0. at the second farmost
273: * rightside of the branch number, so instead of having an odd
274: * number of dot-separated decimals, it will have an even number.
275: *
1.44 krw 276: * Now, according to all the documentation I've found on the net
277: * about this, cvs does this for "efficiency reasons", I'd like
1.15 joris 278: * to hear one.
279: *
280: * We just make sure we remove the .0. from in the branch number.
281: *
282: * XXX - for compatibility reasons with GNU cvs we _need_
283: * to skip this part for the 'log' command, apparently it does
284: * show the magic branches for an unknown and probably
285: * completely insane and not understandable reason in that output.
286: *
287: */
1.51 ! joris 288: if (nump->rn_len > 2 && nump->rn_id[nump->rn_len - 1] == 0) {
1.15 joris 289: /*
290: * Look for ".0.x" at the end of the branch number.
291: */
292: if ((s = strrchr(str, '.')) != NULL) {
1.27 deraadt 293: s--;
1.15 joris 294: while (*s != '.')
1.27 deraadt 295: s--;
1.15 joris 296:
297: /*
298: * If we have a "magic" branch, adjust it
299: * so the .0. is removed.
300: */
301: if (!strncmp(s, RCS_MAGIC_BRANCH,
1.46 tobias 302: sizeof(RCS_MAGIC_BRANCH) - 1)) {
1.15 joris 303: nump->rn_id[nump->rn_len - 1] =
304: nump->rn_id[nump->rn_len];
305: nump->rn_len--;
306: }
307: }
308: }
1.1 jfb 309:
1.26 niallo 310: /* We can't have a single-digit rcs number. */
311: if (nump->rn_len == 0) {
1.48 tobias 312: nump->rn_len++;
1.45 ray 313: nump->rn_id = xrealloc(nump->rn_id,
1.32 ray 314: nump->rn_len + 1, sizeof(*(nump->rn_id)));
1.48 tobias 315: nump->rn_id[nump->rn_len] = 0;
1.26 niallo 316: }
1.31 joris 317:
1.1 jfb 318: nump->rn_len++;
319: return (nump->rn_len);
1.6 jfb 320:
321: rcsnum_aton_failed:
322: nump->rn_len = 0;
1.18 joris 323: xfree(nump->rn_id);
1.6 jfb 324: nump->rn_id = NULL;
325: return (-1);
1.11 jfb 326: }
327:
328: /*
329: * rcsnum_inc()
330: *
331: * Increment the revision number specified in <num>.
332: * Returns a pointer to the <num> on success, or NULL on failure.
333: */
1.14 xsa 334: RCSNUM *
1.11 jfb 335: rcsnum_inc(RCSNUM *num)
336: {
337: if (num->rn_id[num->rn_len - 1] == RCSNUM_MAXNUM)
338: return (NULL);
339: num->rn_id[num->rn_len - 1]++;
1.17 joris 340: return (num);
341: }
342:
343: /*
344: * rcsnum_dec()
345: *
1.26 niallo 346: * Decreases the revision number specified in <num>, if doing so will not
347: * result in an ending value below 1. E.g. 4.2 will go to 4.1 but 4.1 will
348: * be returned as 4.1.
1.17 joris 349: */
350: RCSNUM *
351: rcsnum_dec(RCSNUM *num)
352: {
1.28 ray 353: /* XXX - Is it an error for the number to be 0? */
354: if (num->rn_id[num->rn_len - 1] <= 1)
1.26 niallo 355: return (num);
1.17 joris 356: num->rn_id[num->rn_len - 1]--;
1.11 jfb 357: return (num);
358: }
359:
360: /*
361: * rcsnum_revtobr()
362: *
363: * Retrieve the branch number associated with the revision number <num>.
364: * If <num> is a branch revision, the returned value will be the same
365: * number as the argument.
366: */
1.14 xsa 367: RCSNUM *
1.11 jfb 368: rcsnum_revtobr(const RCSNUM *num)
369: {
370: RCSNUM *brnum;
371:
372: if (num->rn_len < 2)
373: return (NULL);
374:
1.19 joris 375: brnum = rcsnum_alloc();
1.11 jfb 376: rcsnum_cpy(num, brnum, 0);
377:
378: if (!RCSNUM_ISBRANCH(brnum))
379: brnum->rn_len--;
380:
381: return (brnum);
382: }
383:
384: /*
385: * rcsnum_brtorev()
386: *
387: * Retrieve the initial revision number associated with the branch number <num>.
388: * If <num> is a revision number, an error will be returned.
389: */
1.14 xsa 390: RCSNUM *
1.11 jfb 391: rcsnum_brtorev(const RCSNUM *brnum)
392: {
393: RCSNUM *num;
394:
395: if (!RCSNUM_ISBRANCH(brnum)) {
396: return (NULL);
397: }
398:
1.19 joris 399: num = rcsnum_alloc();
1.33 xsa 400: rcsnum_setsize(num, brnum->rn_len + 1);
1.11 jfb 401: rcsnum_cpy(brnum, num, brnum->rn_len);
402: num->rn_id[num->rn_len++] = 1;
403:
404: return (num);
1.49 tobias 405: }
406:
407: RCSNUM *
408: rcsnum_new_branch(RCSNUM *rev)
409: {
410: RCSNUM *branch;
411:
412: if (rev->rn_len > RCSNUM_MAXLEN - 1)
413: return NULL;
414:
415: branch = rcsnum_alloc();
416: rcsnum_cpy(rev, branch, 0);
417: rcsnum_setsize(branch, rev->rn_len + 1);
418: branch->rn_id[branch->rn_len - 1] = 2;
419:
420: return branch;
1.47 joris 421: }
422:
423: RCSNUM *
424: rcsnum_branch_root(RCSNUM *brev)
425: {
426: RCSNUM *root;
427:
428: if (!RCSNUM_ISBRANCHREV(brev))
429: fatal("rcsnum_branch_root: no revision on branch specified");
430:
431: root = rcsnum_alloc();
432: rcsnum_cpy(brev, root, 0);
433: root->rn_len -= 2;
434: return (root);
1.11 jfb 435: }
436:
1.33 xsa 437: static void
1.11 jfb 438: rcsnum_setsize(RCSNUM *num, u_int len)
439: {
1.45 ray 440: num->rn_id = xrealloc(num->rn_id, len, sizeof(*(num->rn_id)));
1.11 jfb 441: num->rn_len = len;
1.38 joris 442: }
443:
444: int
445: rcsnum_differ(RCSNUM *r1, RCSNUM *r2)
446: {
447: int i, len;
448:
449: if (r1->rn_len != r2->rn_len)
450: return (1);
451:
452: len = MIN(r1->rn_len, r2->rn_len);
453: for (i = 0; i < len; i++) {
454: if (r1->rn_id[i] != r2->rn_id[i])
455: return (1);
456: }
457:
458: return (0);
1.1 jfb 459: }