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

1.16    ! niallo      1: /*     $OpenBSD: rcsnum.c,v 1.15 2005/08/02 11:48:56 joris 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:
                     27: #include <sys/param.h>
                     28:
                     29: #include <ctype.h>
                     30: #include <stdio.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
                     33:
1.15      joris      34: #include "cvs.h"
1.12      xsa        35: #include "log.h"
1.1       jfb        36: #include "rcs.h"
                     37:
                     38:
1.14      xsa        39: static int     rcsnum_setsize(RCSNUM *, u_int);
1.11      jfb        40:
1.1       jfb        41:
                     42: /*
                     43:  * rcsnum_alloc()
                     44:  *
1.11      jfb        45:  * Allocate an RCS number structure and return a pointer to it on success,
                     46:  * or NULL on failure.
1.1       jfb        47:  */
1.14      xsa        48: RCSNUM *
1.1       jfb        49: rcsnum_alloc(void)
                     50: {
                     51:        RCSNUM *rnp;
                     52:
                     53:        rnp = (RCSNUM *)malloc(sizeof(*rnp));
                     54:        if (rnp == NULL) {
1.11      jfb        55:                rcs_errno = RCS_ERR_ERRNO;
1.1       jfb        56:                return (NULL);
                     57:        }
                     58:        rnp->rn_len = 0;
                     59:        rnp->rn_id = NULL;
                     60:
                     61:        return (rnp);
                     62: }
                     63:
                     64: /*
1.7       jfb        65:  * rcsnum_parse()
                     66:  *
                     67:  * Parse a string specifying an RCS number and return the corresponding RCSNUM.
                     68:  */
1.14      xsa        69: RCSNUM *
1.7       jfb        70: rcsnum_parse(const char *str)
                     71: {
                     72:        char *ep;
                     73:        RCSNUM *num;
                     74:
                     75:        if ((num = rcsnum_alloc()) == NULL)
                     76:                return (NULL);
                     77:
1.8       jfb        78:        if ((rcsnum_aton(str, &ep, num) < 0) || (*ep != '\0')) {
1.7       jfb        79:                rcsnum_free(num);
1.9       jfb        80:                num = NULL;
                     81:                if (*ep != '\0')
                     82:                        rcs_errno = RCS_ERR_BADNUM;
1.7       jfb        83:        }
                     84:
                     85:        return (num);
                     86: }
                     87:
                     88: /*
1.1       jfb        89:  * rcsnum_free()
                     90:  *
                     91:  * Free an RCSNUM structure previously allocated with rcsnum_alloc().
                     92:  */
                     93: void
                     94: rcsnum_free(RCSNUM *rn)
                     95: {
                     96:        if (rn->rn_id != NULL)
                     97:                free(rn->rn_id);
                     98:        free(rn);
                     99: }
                    100:
                    101: /*
                    102:  * rcsnum_tostr()
1.10      jfb       103:  *
                    104:  * Format the RCS number <nump> into a human-readable dot-separated
                    105:  * representation and store the resulting string in <buf>, which is of size
                    106:  * <blen>.
1.1       jfb       107:  * Returns a pointer to the start of <buf> on success, or NULL on failure.
                    108:  */
1.13      xsa       109: char *
1.1       jfb       110: rcsnum_tostr(const RCSNUM *nump, char *buf, size_t blen)
                    111: {
                    112:        u_int i;
                    113:        char tmp[8];
                    114:
1.10      jfb       115:        if ((nump == NULL) || (nump->rn_len == 0)) {
1.1       jfb       116:                buf[0] = '\0';
                    117:                return (buf);
                    118:        }
                    119:
1.4       tedu      120:        snprintf(buf, blen, "%u", nump->rn_id[0]);
1.1       jfb       121:        for (i = 1; i < nump->rn_len; i++) {
                    122:                snprintf(tmp, sizeof(tmp), ".%u", nump->rn_id[i]);
                    123:                strlcat(buf, tmp, blen);
                    124:        }
                    125:
                    126:        return (buf);
                    127: }
                    128:
                    129: /*
                    130:  * rcsnum_cpy()
                    131:  *
                    132:  * Copy the number stored in <nsrc> in the destination <ndst> up to <depth>
                    133:  * numbers deep.
                    134:  * Returns 0 on success, or -1 on failure.
                    135:  */
                    136: int
                    137: rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth)
                    138: {
                    139:        u_int len;
                    140:        size_t sz;
                    141:        void *tmp;
                    142:
                    143:        len = nsrc->rn_len;
                    144:        if ((depth != 0) && (len > depth))
                    145:                len = depth;
                    146:        sz = len * sizeof(u_int16_t);
                    147:
                    148:        tmp = realloc(ndst->rn_id, sz);
                    149:        if (tmp == NULL) {
1.11      jfb       150:                rcs_errno = RCS_ERR_ERRNO;
1.1       jfb       151:                return (-1);
                    152:        }
                    153:
                    154:        ndst->rn_id = (u_int16_t *)tmp;
                    155:        ndst->rn_len = len;
                    156:        memcpy(ndst->rn_id, nsrc->rn_id, sz);
                    157:        return (0);
                    158: }
                    159:
                    160: /*
                    161:  * rcsnum_cmp()
                    162:  *
                    163:  * Compare the two numbers <n1> and <n2>. Returns -1 if <n1> is larger than
                    164:  * <n2>, 0 if they are both the same, and 1 if <n2> is larger than <n1>.
                    165:  * The <depth> argument specifies how many numbers deep should be checked for
                    166:  * the result.  A value of 0 means that the depth will be the minimum of the
                    167:  * two numbers.
                    168:  */
                    169: int
                    170: rcsnum_cmp(const RCSNUM *n1, const RCSNUM *n2, u_int depth)
                    171: {
                    172:        int res;
                    173:        u_int i;
                    174:        size_t slen;
                    175:
                    176:        slen = MIN(n1->rn_len, n2->rn_len);
                    177:        if ((depth != 0) && (slen > depth))
                    178:                slen = depth;
1.4       tedu      179:
1.1       jfb       180:        for (i = 0; i < slen; i++) {
                    181:                res = n1->rn_id[i] - n2->rn_id[i];
                    182:                if (res < 0)
                    183:                        return (1);
                    184:                else if (res > 0)
                    185:                        return (-1);
                    186:        }
                    187:
                    188:        if (n1->rn_len > n2->rn_len)
                    189:                return (-1);
                    190:        else if (n2->rn_len > n1->rn_len)
                    191:                return (1);
                    192:
                    193:        return (0);
                    194: }
                    195:
                    196: /*
                    197:  * rcsnum_aton()
                    198:  *
                    199:  * Translate the string <str> containing a sequence of digits and periods into
                    200:  * its binary representation, which is stored in <nump>.  The address of the
                    201:  * first byte not part of the number is stored in <ep> on return, if it is not
                    202:  * NULL.
                    203:  * Returns 0 on success, or -1 on failure.
                    204:  */
                    205: int
                    206: rcsnum_aton(const char *str, char **ep, RCSNUM *nump)
                    207: {
1.6       jfb       208:        u_int32_t val;
1.1       jfb       209:        const char *sp;
                    210:        void *tmp;
1.15      joris     211:        char *s;
1.1       jfb       212:
1.2       vincent   213:        if (nump->rn_id == NULL) {
                    214:                nump->rn_id = (u_int16_t *)malloc(sizeof(u_int16_t));
1.6       jfb       215:                if (nump->rn_id == NULL) {
1.11      jfb       216:                        rcs_errno = RCS_ERR_ERRNO;
1.2       vincent   217:                        return (-1);
1.6       jfb       218:                }
1.2       vincent   219:        }
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++;
                    235:                        tmp = realloc(nump->rn_id,
                    236:                            (nump->rn_len + 1) * sizeof(u_int16_t));
1.9       jfb       237:                        if (tmp == NULL)
1.6       jfb       238:                                goto rcsnum_aton_failed;
1.1       jfb       239:                        nump->rn_id = (u_int16_t *)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);
                    245:                if (val > RCSNUM_MAXNUM) {
                    246:                        cvs_log(LP_ERR, "RCSNUM overflow");
                    247:                        goto rcsnum_aton_failed;
                    248:                }
                    249:
                    250:                nump->rn_id[nump->rn_len] = val;
1.1       jfb       251:        }
                    252:
                    253:        if (ep != NULL)
1.5       jfb       254:                *(const char **)ep = sp;
1.15      joris     255:
                    256:        /*
                    257:         * Handle "magic" RCS branch numbers.
                    258:         *
                    259:         * What are they?
                    260:         *
                    261:         * Magic branch numbers have an extra .0. at the second farmost
                    262:         * rightside of the branch number, so instead of having an odd
                    263:         * number of dot-separated decimals, it will have an even number.
                    264:         *
                    265:         * Now, according to all the documentation i've found on the net
                    266:         * about this, cvs does this for "efficiency reasons", i'd like
                    267:         * to hear one.
                    268:         *
                    269:         * We just make sure we remove the .0. from in the branch number.
                    270:         *
                    271:         * XXX - for compatibility reasons with GNU cvs we _need_
                    272:         * to skip this part for the 'log' command, apparently it does
                    273:         * show the magic branches for an unknown and probably
                    274:         * completely insane and not understandable reason in that output.
                    275:         *
                    276:         */
1.16    ! niallo    277: #if !defined(RCSPROG)
1.15      joris     278:        if ((nump->rn_len > 2) && (nump->rn_id[nump->rn_len - 1] == 0)
                    279:            && (cvs_cmdop != CVS_OP_LOG)) {
1.16    ! niallo    280: #else
        !           281:        if ((nump->rn_len > 2) && (nump->rn_id[nump->rn_len - 1] == 0)) {
        !           282: #endif
1.15      joris     283:                /*
                    284:                 * Look for ".0.x" at the end of the branch number.
                    285:                 */
                    286:                if ((s = strrchr(str, '.')) != NULL) {
                    287:                        *s--;
                    288:                        while (*s != '.')
                    289:                                *s--;
                    290:
                    291:                        /*
                    292:                         * If we have a "magic" branch, adjust it
                    293:                         * so the .0. is removed.
                    294:                         */
                    295:                        if (!strncmp(s, RCS_MAGIC_BRANCH,
                    296:                            strlen(RCS_MAGIC_BRANCH))) {
                    297:                                nump->rn_id[nump->rn_len - 1] =
                    298:                                    nump->rn_id[nump->rn_len];
                    299:                                nump->rn_len--;
                    300:                        }
                    301:                }
                    302:        }
1.1       jfb       303:
                    304:        nump->rn_len++;
                    305:        return (nump->rn_len);
1.6       jfb       306:
                    307: rcsnum_aton_failed:
                    308:        nump->rn_len = 0;
                    309:        free(nump->rn_id);
                    310:        nump->rn_id = NULL;
                    311:        return (-1);
1.11      jfb       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:  */
1.14      xsa       320: RCSNUM *
1.11      jfb       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_revtobr()
                    331:  *
                    332:  * Retrieve the branch number associated with the revision number <num>.
                    333:  * If <num> is a branch revision, the returned value will be the same
                    334:  * number as the argument.
                    335:  */
1.14      xsa       336: RCSNUM *
1.11      jfb       337: rcsnum_revtobr(const RCSNUM *num)
                    338: {
                    339:        RCSNUM *brnum;
                    340:
                    341:        if (num->rn_len < 2)
                    342:                return (NULL);
                    343:
                    344:        if ((brnum = rcsnum_alloc()) == NULL)
                    345:                return (NULL);
                    346:
                    347:        rcsnum_cpy(num, brnum, 0);
                    348:
                    349:        if (!RCSNUM_ISBRANCH(brnum))
                    350:                brnum->rn_len--;
                    351:
                    352:        return (brnum);
                    353: }
                    354:
                    355: /*
                    356:  * rcsnum_brtorev()
                    357:  *
                    358:  * Retrieve the initial revision number associated with the branch number <num>.
                    359:  * If <num> is a revision number, an error will be returned.
                    360:  */
1.14      xsa       361: RCSNUM *
1.11      jfb       362: rcsnum_brtorev(const RCSNUM *brnum)
                    363: {
                    364:        RCSNUM *num;
                    365:
                    366:        if (!RCSNUM_ISBRANCH(brnum)) {
                    367:                return (NULL);
                    368:        }
                    369:
                    370:        if ((num = rcsnum_alloc()) == NULL)
                    371:                return (NULL);
                    372:
                    373:        if (rcsnum_setsize(num, brnum->rn_len + 1) < 0) {
                    374:                rcsnum_free(num);
                    375:                return (NULL);
                    376:        }
                    377:
                    378:        rcsnum_cpy(brnum, num, brnum->rn_len);
                    379:        num->rn_id[num->rn_len++] = 1;
                    380:
                    381:        return (num);
                    382: }
                    383:
                    384: static int
                    385: rcsnum_setsize(RCSNUM *num, u_int len)
                    386: {
                    387:        void *tmp;
                    388:
                    389:        tmp = realloc(num->rn_id, len * sizeof(u_int16_t));
                    390:        if (tmp == NULL) {
                    391:                rcs_errno = RCS_ERR_ERRNO;
                    392:                return (-1);
                    393:        }
                    394:
                    395:        num->rn_id = (u_int16_t *)tmp;
                    396:        num->rn_len = len;
                    397:        return (0);
1.1       jfb       398: }