[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.41

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