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