[BACK]Return to rcsnum.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/rcsnum.c, Revision 1.50

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