Annotation of src/usr.bin/rcs/rcsnum.c, Revision 1.8
1.8 ! tobias 1: /* $OpenBSD: rcsnum.c,v 1.7 2007/05/29 00:19:10 ray Exp $ */
1.1 joris 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
1.4 xsa 27: #include <sys/param.h>
28:
29: #include <ctype.h>
30: #include <err.h>
31: #include <string.h>
1.1 joris 32:
33: #include "rcs.h"
34: #include "xmalloc.h"
35:
36: static void rcsnum_setsize(RCSNUM *, u_int);
37: static char *rcsnum_itoa(u_int16_t, char *, size_t);
38:
39: int rcsnum_flags;
40:
41: /*
42: * rcsnum_alloc()
43: *
44: * Allocate an RCS number structure and return a pointer to it.
45: */
46: RCSNUM *
47: rcsnum_alloc(void)
48: {
49: RCSNUM *rnp;
50:
51: rnp = xmalloc(sizeof(*rnp));
52: rnp->rn_len = 0;
53: rnp->rn_id = NULL;
54:
55: return (rnp);
56: }
57:
58: /*
59: * rcsnum_parse()
60: *
61: * Parse a string specifying an RCS number and return the corresponding RCSNUM.
62: */
63: RCSNUM *
64: rcsnum_parse(const char *str)
65: {
66: char *ep;
67: RCSNUM *num;
68:
69: num = rcsnum_alloc();
70: if (rcsnum_aton(str, &ep, num) < 0 || *ep != '\0') {
71: rcsnum_free(num);
72: num = NULL;
73: if (*ep != '\0')
74: rcs_errno = RCS_ERR_BADNUM;
75: }
76:
77: return (num);
78: }
79:
80: /*
81: * rcsnum_free()
82: *
83: * Free an RCSNUM structure previously allocated with rcsnum_alloc().
84: */
85: void
86: rcsnum_free(RCSNUM *rn)
87: {
88: if (rn->rn_id != NULL)
89: xfree(rn->rn_id);
90: xfree(rn);
91: }
92:
93: /*
94: * rcsnum_tostr()
95: *
96: * Format the RCS number <nump> into a human-readable dot-separated
97: * representation and store the resulting string in <buf>, which is of size
98: * <blen>.
99: * Returns a pointer to the start of <buf>. On failure <buf> is set to
100: * an empty string.
101: */
102: char *
103: rcsnum_tostr(const RCSNUM *nump, char *buf, size_t blen)
104: {
105: u_int i;
106: char tmp[8];
107:
108: if (nump == NULL || nump->rn_len == 0) {
109: buf[0] = '\0';
110: return (buf);
111: }
112:
1.2 ray 113: if (strlcpy(buf, rcsnum_itoa(nump->rn_id[0], buf, blen), blen) >= blen)
114: errx(1, "rcsnum_tostr: string truncated");
1.1 joris 115: for (i = 1; i < nump->rn_len; i++) {
1.2 ray 116: const char *str;
117:
118: str = rcsnum_itoa(nump->rn_id[i], tmp, sizeof(tmp));
119: if (strlcat(buf, ".", blen) >= blen ||
120: strlcat(buf, str, blen) >= blen)
121: errx(1, "rcsnum_tostr: string truncated");
1.1 joris 122: }
123:
124: return (buf);
125: }
126:
127: static char *
128: rcsnum_itoa(u_int16_t num, char *buf, size_t len)
129: {
130: u_int16_t i;
131: char *p;
132:
133: if (num == 0)
134: return "0";
135:
136: p = buf + len - 1;
137: i = num;
138: bzero(buf, len);
139: while (i) {
140: *--p = '0' + (i % 10);
141: i /= 10;
142: }
143: return (p);
144: }
145:
146: /*
147: * rcsnum_cpy()
148: *
149: * Copy the number stored in <nsrc> in the destination <ndst> up to <depth>
150: * numbers deep. If <depth> is 0, there is no depth limit.
151: */
152: void
153: rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth)
154: {
155: u_int len;
156:
157: len = nsrc->rn_len;
158: if (depth != 0 && len > depth)
159: len = depth;
160:
1.5 ray 161: rcsnum_setsize(ndst, len);
162: /* Overflow checked in rcsnum_setsize(). */
1.3 ray 163: (void)memcpy(ndst->rn_id, nsrc->rn_id,
164: len * sizeof(*(nsrc->rn_id)));
1.1 joris 165: }
166:
167: /*
168: * rcsnum_cmp()
169: *
170: * Compare the two numbers <n1> and <n2>. Returns -1 if <n1> is larger than
171: * <n2>, 0 if they are both the same, and 1 if <n2> is larger than <n1>.
172: * The <depth> argument specifies how many numbers deep should be checked for
173: * the result. A value of 0 means that the depth will be the minimum of the
174: * two numbers.
175: */
176: int
177: rcsnum_cmp(const RCSNUM *n1, const RCSNUM *n2, u_int depth)
178: {
179: int res;
180: u_int i;
181: size_t slen;
182:
183: slen = MIN(n1->rn_len, n2->rn_len);
184: if (depth != 0 && slen > depth)
185: slen = depth;
186:
187: for (i = 0; i < slen; i++) {
188: res = n1->rn_id[i] - n2->rn_id[i];
189: if (res < 0)
190: return (1);
191: else if (res > 0)
192: return (-1);
193: }
194:
195: if (n1->rn_len > n2->rn_len)
196: return (-1);
197: else if (n2->rn_len > n1->rn_len)
198: return (1);
199:
200: return (0);
201: }
202:
203: /*
204: * rcsnum_aton()
205: *
206: * Translate the string <str> containing a sequence of digits and periods into
207: * its binary representation, which is stored in <nump>. The address of the
208: * first byte not part of the number is stored in <ep> on return, if it is not
209: * NULL.
210: * Returns 0 on success, or -1 on failure.
211: */
212: int
213: rcsnum_aton(const char *str, char **ep, RCSNUM *nump)
214: {
215: u_int32_t val;
216: const char *sp;
217: char *s;
218:
219: if (nump->rn_id == NULL)
220: nump->rn_id = xmalloc(sizeof(*(nump->rn_id)));
221:
222: nump->rn_len = 0;
223: nump->rn_id[0] = 0;
224:
225: for (sp = str;; sp++) {
226: if (!isdigit(*sp) && (*sp != '.'))
227: break;
228:
229: if (*sp == '.') {
230: if (nump->rn_len >= RCSNUM_MAXLEN - 1) {
231: rcs_errno = RCS_ERR_BADNUM;
232: goto rcsnum_aton_failed;
233: }
234:
235: nump->rn_len++;
1.7 ray 236: nump->rn_id = xrealloc(nump->rn_id,
1.1 joris 237: nump->rn_len + 1, sizeof(*(nump->rn_id)));
238: nump->rn_id[nump->rn_len] = 0;
239: continue;
240: }
241:
1.6 ray 242: val = (nump->rn_id[nump->rn_len] * 10) + (*sp - '0');
1.1 joris 243: if (val > RCSNUM_MAXNUM)
244: errx(1, "RCSNUM overflow!");
245:
246: nump->rn_id[nump->rn_len] = val;
247: }
248:
249: if (ep != NULL)
250: *(const char **)ep = sp;
251:
252: /*
253: * Handle "magic" RCS branch numbers.
254: *
255: * What are they?
256: *
257: * Magic branch numbers have an extra .0. at the second farmost
258: * rightside of the branch number, so instead of having an odd
259: * number of dot-separated decimals, it will have an even number.
260: *
261: * Now, according to all the documentation i've found on the net
262: * about this, cvs does this for "efficiency reasons", i'd like
263: * to hear one.
264: *
265: * We just make sure we remove the .0. from in the branch number.
266: *
267: * XXX - for compatibility reasons with GNU cvs we _need_
268: * to skip this part for the 'log' command, apparently it does
269: * show the magic branches for an unknown and probably
270: * completely insane and not understandable reason in that output.
271: *
272: */
273: if (nump->rn_len > 2 && nump->rn_id[nump->rn_len - 1] == 0
274: && !(rcsnum_flags & RCSNUM_NO_MAGIC)) {
275: /*
276: * Look for ".0.x" at the end of the branch number.
277: */
278: if ((s = strrchr(str, '.')) != NULL) {
279: s--;
280: while (*s != '.')
281: s--;
282:
283: /*
284: * If we have a "magic" branch, adjust it
285: * so the .0. is removed.
286: */
287: if (!strncmp(s, RCS_MAGIC_BRANCH,
288: strlen(RCS_MAGIC_BRANCH))) {
289: nump->rn_id[nump->rn_len - 1] =
290: nump->rn_id[nump->rn_len];
291: nump->rn_len--;
292: }
293: }
294: }
295:
296: /* We can't have a single-digit rcs number. */
297: if (nump->rn_len == 0) {
1.8 ! tobias 298: nump->rn_len++;
1.7 ray 299: nump->rn_id = xrealloc(nump->rn_id,
1.1 joris 300: nump->rn_len + 1, sizeof(*(nump->rn_id)));
1.8 ! tobias 301: nump->rn_id[nump->rn_len] = 0;
1.1 joris 302: }
303:
304: nump->rn_len++;
305: return (nump->rn_len);
306:
307: rcsnum_aton_failed:
308: nump->rn_len = 0;
309: xfree(nump->rn_id);
310: nump->rn_id = NULL;
311: return (-1);
312: }
313:
314: /*
315: * rcsnum_inc()
316: *
317: * Increment the revision number specified in <num>.
318: * Returns a pointer to the <num> on success, or NULL on failure.
319: */
320: RCSNUM *
321: rcsnum_inc(RCSNUM *num)
322: {
323: if (num->rn_id[num->rn_len - 1] == RCSNUM_MAXNUM)
324: return (NULL);
325: num->rn_id[num->rn_len - 1]++;
326: return (num);
327: }
328:
329: /*
330: * rcsnum_dec()
331: *
332: * Decreases the revision number specified in <num>, if doing so will not
333: * result in an ending value below 1. E.g. 4.2 will go to 4.1 but 4.1 will
334: * be returned as 4.1.
335: */
336: RCSNUM *
337: rcsnum_dec(RCSNUM *num)
338: {
339: /* XXX - Is it an error for the number to be 0? */
340: if (num->rn_id[num->rn_len - 1] <= 1)
341: return (num);
342: num->rn_id[num->rn_len - 1]--;
343: return (num);
344: }
345:
346: /*
347: * rcsnum_revtobr()
348: *
349: * Retrieve the branch number associated with the revision number <num>.
350: * If <num> is a branch revision, the returned value will be the same
351: * number as the argument.
352: */
353: RCSNUM *
354: rcsnum_revtobr(const RCSNUM *num)
355: {
356: RCSNUM *brnum;
357:
358: if (num->rn_len < 2)
359: return (NULL);
360:
361: brnum = rcsnum_alloc();
362: rcsnum_cpy(num, brnum, 0);
363:
364: if (!RCSNUM_ISBRANCH(brnum))
365: brnum->rn_len--;
366:
367: return (brnum);
368: }
369:
370: /*
371: * rcsnum_brtorev()
372: *
373: * Retrieve the initial revision number associated with the branch number <num>.
374: * If <num> is a revision number, an error will be returned.
375: */
376: RCSNUM *
377: rcsnum_brtorev(const RCSNUM *brnum)
378: {
379: RCSNUM *num;
380:
381: if (!RCSNUM_ISBRANCH(brnum)) {
382: return (NULL);
383: }
384:
385: num = rcsnum_alloc();
386: rcsnum_setsize(num, brnum->rn_len + 1);
387: rcsnum_cpy(brnum, num, brnum->rn_len);
388: num->rn_id[num->rn_len++] = 1;
389:
390: return (num);
391: }
392:
393: static void
394: rcsnum_setsize(RCSNUM *num, u_int len)
395: {
1.7 ray 396: num->rn_id = xrealloc(num->rn_id, len, sizeof(*(num->rn_id)));
1.1 joris 397: num->rn_len = len;
398: }