home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / nls / mcprtlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-23  |  14.7 KB  |  583 lines

  1.  
  2. /***********************************************************
  3. Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that Alfalfa's name not be used in
  12. advertising or publicity pertaining to distribution of the software
  13. without specific, written prior permission.
  14.  
  15. ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. If you make any modifications, bugfixes or other changes to this software
  24. we'd appreciate it if you could send a copy to us so we can keep things
  25. up-to-date.  Many thanks.
  26.                 Kee Hinckley
  27.                 Alfalfa Software, Inc.
  28.                 267 Allston St., #3
  29.                 Cambridge, MA 02139  USA
  30.                 nazgul@alfalfa.com
  31.     
  32. ******************************************************************/
  33.  
  34.  
  35. /* Edit History
  36.  
  37. 04/15/92   2 nazgul    Avoid data overruns on allocated strings.
  38. 03/25/91   4 hamilton    Handle null data in MCPrintFree
  39. 01/18/91   3 hamilton    #if not rescanned
  40. 01/12/91   2 schulert    conditionally use prototypes
  41. 01/06/91   1 schulert    check for NULL char* before calling strlen()
  42. 11/03/90   2 hamilton    Alphalpha->Alfalfa & OmegaMail->Poste
  43. 08/13/90   1 hamilton    Add <ctype.h> for sco
  44. 08/10/90   2 nazgul    Added support for %S and %M
  45. 08/10/90   1 nazgul    Supports %d and %s
  46. */
  47.  
  48. #define    CATGETS
  49.  
  50. #include <stdio.h>
  51. #include <ctype.h>
  52.  
  53. #include "mcprtlib.h"
  54. #ifdef CATGETS
  55. # include "nl_types.h"
  56.  
  57. #ifndef MCMakeId
  58. # define MCuint unsigned int
  59. # define MCushort unsigned short
  60. # define MCulong unsigned long
  61. # define MCMakeId(s,m)  (MCulong)(((MCushort)s<<(sizeof(short)*8))\
  62.                                   |(MCushort)m)
  63. # define MCSetId(id)    (MCuint) ((MCuint)id >> (MCuint)(sizeof(short) * 8))
  64. # define MCMsgId(id)    (MCuint) (((MCuint)id << (MCuint)(sizeof(short) * 8))\
  65.                                   >> (MCuint)(sizeof(short) * 8))
  66. #endif
  67.  
  68. #endif
  69.  
  70. #ifndef True
  71. # define True    ~0
  72. # define False    0
  73. #endif
  74.  
  75. #define MCShortMod    0x01
  76. #define MCLongMod    0x02
  77. #define MCLongFMod    0x04
  78.  
  79.  
  80. #define    GETNUM(x)    x = 0; \
  81.                         while (isdigit(*fptr)) x = (x * 10) + *fptr++ - '0'; \
  82.                         if (!*fptr) goto err;
  83.     
  84.  
  85. MCRockT    *MCPrintInit(
  86. #if defined(__STDC__) || defined(__cplusplus)
  87.         char *fmt)
  88. #else
  89.         fmt)
  90. char *fmt;
  91. #endif
  92. {
  93.     int        argCnt, curPos, typeCnt, done, mod, replyCnt, i, pos;
  94.     char    *fptr, *cptr;
  95.     MCArgumentT    *argList, arg;
  96.     MCRockT    *rock;
  97.     MCTypesT    *typeList;
  98.     
  99.     /* This can count too many, but that's okay */
  100.     for (argCnt = 0, fptr = fmt; *fptr; ++fptr) {
  101.     if (*fptr == '%') ++argCnt;
  102.     }
  103.     ++argCnt;    /* One for the end */
  104.     
  105.     rock = (MCRockT *) malloc(sizeof(MCRockT));
  106.     bzero(rock, sizeof(*rock));
  107.     
  108.     rock->argList = argList = (MCArgumentT *) malloc(sizeof(MCArgumentT) * argCnt);
  109.     
  110.     bzero(&arg, sizeof(arg));
  111.     arg.data = fmt;
  112.     arg.dataLen = 0;
  113.     argCnt = 0;
  114.     curPos = 1;
  115.     typeCnt = 0;
  116.     for (fptr = fmt; *fptr; ++fptr) {
  117.     if (*fptr == '%') {
  118.         arg.dataLen = fptr - arg.data;
  119.         arg.pos = 0;
  120.         ++fptr;
  121.         
  122.         /* Check for position */
  123.  
  124.         if (isdigit(*fptr)) {
  125.         for (cptr = fptr; isdigit(*cptr); ++cptr);
  126.         if (*cptr == '$') {
  127.             GETNUM(arg.pos);
  128.             ++fptr;
  129.         }
  130.         }
  131.         
  132.         /* Check for flags (%   + - # 0) */
  133.         
  134.         arg.flag = 0;
  135.         done = False;
  136.         while (!done) {
  137.         switch (*fptr) {
  138.           case ' ':
  139.             arg.flag |= MCSpaceFlag;
  140.             break;
  141.           case '+':
  142.             arg.flag |= MCPlusFlag;
  143.             break;
  144.           case '-':
  145.             arg.flag |= MCMinusFlag;
  146.             break;
  147.           case '#':
  148.             arg.flag |= MCAltFlag;
  149.             break;
  150.           case '0':
  151.             arg.flag |= MCZeroFlag;
  152.             break;
  153.           default:
  154.             done = True;
  155.             break;
  156.         }
  157.         ++fptr;
  158.         }
  159.         --fptr;
  160.         
  161.         /* Check the width argument */
  162.         
  163.         arg.hasWidth = False;
  164.         arg.widthPos = 0;
  165.         if (isdigit(*fptr)) {            /* width */
  166.         arg.hasWidth = True;
  167.         GETNUM(arg.width);
  168.         } else if (*fptr == '*') {
  169.         arg.hasWidth = True;
  170.         ++fptr;
  171.         if (isdigit(*fptr)) {        /* reference to width pos */
  172.             GETNUM(arg.widthPos);
  173.             ++fptr;
  174.         } else {
  175.             arg.widthPos = curPos++;
  176.         }
  177.         }
  178.         
  179.         /* Check for precision argument */
  180.         
  181.         arg.hasPrec = False;
  182.         arg.precPos = 0;
  183.         if (*fptr == '.') {
  184.         ++fptr;
  185.         if (isdigit(*fptr)) {        /* precision */
  186.             arg.hasPrec = True;
  187.             GETNUM(arg.prec);
  188.         } else if (*fptr == '*') {
  189.             arg.hasPrec = True;
  190.             ++fptr;
  191.             if (isdigit(*fptr)) {        /* reference to prec pos */
  192.             GETNUM(arg.precPos);
  193.             ++fptr;
  194.             } else {
  195.             arg.precPos = curPos++;
  196.             }
  197.         }
  198.         }
  199.         
  200.         /* Check for size modifier */
  201.         
  202.         for (mod = 0, done = False; !done; ++fptr) {
  203.         switch (*fptr) {
  204.           case 'h':
  205.             mod |= MCShortMod;
  206.             break;
  207.           case 'l':
  208.             mod |= MCLongMod;
  209.             break;
  210.           case 'L':
  211.             mod |= MCLongFMod;
  212.             break;
  213.           default:
  214.             done = True;
  215.             --fptr;
  216.             break;
  217.         }
  218.         }
  219.         
  220.         /* Check for the real thing */
  221.         
  222.         arg.c = *fptr;
  223.         arg.type = 0;
  224.         switch (*fptr) {
  225. #ifdef CATGETS
  226.           case 'M': case 'S':
  227. #endif
  228.           case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
  229.           case 'c':
  230.         if (mod & MCShortMod) arg.type = MCShortType;
  231.         else if (mod & MCLongMod) arg.type = MCLongType;
  232.         else arg.type = MCIntType;
  233.         break;
  234.           case 'f': case 'e': case 'E': case 'g': case 'G':
  235.         if (mod & MCLongFMod) arg.type = MCLongFloatType;
  236.         else arg.type = MCFloatType;
  237.         break;
  238.           case 's':
  239.         arg.type = MCStringType;
  240.         break;
  241. #ifdef CATGETS
  242.           case 'C':
  243. #endif        
  244.           case 'p':
  245.         arg.type = MCVoidPType;
  246.         break;
  247.           case 'n':
  248.         if (mod & MCShortMod) arg.type = MCShortPType;
  249.         else if (mod & MCLongMod) arg.type = MCLongPType;
  250.         else arg.type = MCIntPType;
  251.         break;
  252.           case '%':
  253.         ++arg.dataLen;    /* An empty arg with a data element including % */
  254.         break;
  255.           default:
  256.         goto err;
  257.         break;
  258.         }
  259.         
  260.         /* They should never mix with and without positions, but be nice */
  261.         if (!arg.pos) arg.pos = curPos++;
  262.         else curPos = arg.pos+1;
  263.         
  264.         /* Keep track of how many type elements we'll need */
  265.         if (arg.pos > typeCnt) typeCnt = arg.pos;
  266.         if (arg.precPos > typeCnt) typeCnt = arg.precPos;
  267.         if (arg.widthPos > typeCnt) typeCnt = arg.widthPos;
  268.         
  269.         argList[argCnt++] = arg;
  270.         bzero(&arg, sizeof(arg));
  271.         arg.data = fptr+1;
  272.     } else {
  273.         /* Otherwise things will just end up in arg.data */
  274.     }
  275.     }
  276.     /* Catch any trailing text */
  277.     arg.dataLen = fptr - arg.data;
  278.     argList[argCnt++] = arg;
  279.     
  280.     /* 
  281.      * We now have an array which precisely describes all of the arguments.
  282.      * Now we allocate space for each of the arguments.
  283.      * Then we loop through that array and fill it in with the correct arguments.
  284.      * Finally we loop through the arglist and print out each of the arguments.
  285.      * Simple, no?
  286.      */
  287.     
  288.     /* Allocate the type list */
  289.     
  290.     rock->typeList = typeList = (MCTypesT *) malloc(sizeof(MCTypesT) * typeCnt);
  291.     
  292.     /* Go through and set the types */
  293.     
  294.     /*
  295.      * I was going to check here and see if, when a type was referenced twice,
  296.      * they typed it differently, and return an error when they did.  But then
  297.      * it occured to me that doing that could be useful.  Consider passing a
  298.      * pointer and wanting to see it in both string and hex form.  If they think
  299.      * they know what they're doing we might as well let them do it.
  300.      */
  301.     
  302.     /*
  303.      * Set the correct types and figure out how many data segments we are going
  304.      * to have.
  305.      */
  306.     for (replyCnt = i = 0; i < argCnt; ++i) {
  307.     if (argList[i].type) {
  308.         pos = argList[i].pos-1;
  309.         typeList[pos].type = argList[i].type;
  310.         ++replyCnt;
  311.         if (argList[i].precPos) typeList[argList[i].precPos-1].type = MCIntType;
  312.         if (argList[i].widthPos) typeList[argList[i].widthPos-1].type = MCIntType;
  313.     }
  314.     if (argList[i].dataLen) ++replyCnt;
  315.     }
  316.     ++replyCnt;    /* with NULLs */
  317.  
  318.     rock->replyCnt = replyCnt;
  319.     rock->argCnt = argCnt;
  320.     rock->typeCnt = typeCnt;
  321.     rock->replyList = NULL;
  322.     return(rock);
  323. err:
  324.     MCPrintFree(rock);
  325.     return(NULL);
  326. }
  327.  
  328. int    MCPrintParse(
  329. #if defined(__STDC__) || defined(__cplusplus)
  330.         MCRockT *rock)
  331. #else
  332.         rock)
  333. MCRockT *rock;
  334. #endif
  335. {
  336.     MCArgumentT    *argList = rock->argList, arg;
  337.     int        argCnt = rock->argCnt;
  338.     MCTypesT    *typeList = rock->typeList, type;
  339.     int        typeCnt = rock->typeCnt;
  340.     MCReplyT    *replyList;
  341.     int        replyCnt = rock->replyCnt;
  342.  
  343.     int        i, pos, ri, j, base, isSigned, isNeg, len, alen, retlen = 0;
  344.     char    *index, *cptr, buf[BUFSIZ], *fptr;
  345. #ifdef CATGETS
  346.     int        setId = 0, msgId = 0;
  347.     nl_catd    catd;
  348. #endif
  349.     
  350.     
  351.     /* Allocate the reply structure */
  352.     rock->replyList = replyList = (MCReplyT *) malloc(sizeof(MCReplyT) * replyCnt);
  353.     
  354.     /* Now we do the dirty work of actually formatting the stuff */
  355.  
  356.     for (ri = i = 0; i < argCnt; ++i) {
  357.     pos = argList[i].pos-1;
  358.  
  359.     /* Handle the data from the format string */
  360.     if (argList[i].dataLen) {
  361.         replyList[ri].argType = MCFmtArg;
  362.         replyList[ri].data = argList[i].data;
  363.         retlen += replyList[ri].dataLen = argList[i].dataLen;
  364.         ++ri;
  365.     }
  366.  
  367.     /* Fill in the indirect width and precision stuff */
  368.     if (argList[i].hasPrec && argList[i].precPos) {
  369.         argList[i].prec = typeList[argList[i].precPos-1].u.intV;
  370.     }
  371.     if (argList[i].hasWidth && argList[i].widthPos) {
  372.         argList[i].width = typeList[argList[i].widthPos-1].u.intV;
  373.     }
  374.     
  375.     if (argList[i].type) {
  376.         type = typeList[pos];
  377.         arg = argList[i];
  378.         if (arg.type == MCShortType || arg.type == MCLongType ||
  379.         arg.type == MCIntType) {
  380. intType:    index = "0123456789abcdef";
  381.         switch (arg.c) {
  382.           case 'd': case 'i':
  383.             base = 10;
  384.             isSigned = True;
  385.             break;
  386.           case 'u':
  387.             base = 10;
  388.             isSigned = False;
  389.             break;
  390.           case 'o':
  391.             base = 8;
  392.             isSigned = False;
  393.             break;
  394.           case 'X':
  395.             index = "0123456789ABCDEF";
  396.           case 'x':
  397.             base = 16;
  398.             isSigned = False;
  399.             break;
  400. #ifdef CATGETS
  401.           case 'S':
  402.             setId = type.u.intV;
  403.             goto nextArg;
  404.           case 'M':
  405.             if (arg.type == MCLongType) {
  406.             setId = MCSetId(type.u.longV);
  407.             msgId = MCMsgId(type.u.longV);
  408.             } else msgId = type.u.intV;
  409.             type.u.charPV = catgets(catd, setId, msgId, "<unable to load msg>");
  410.             goto strType;
  411. #endif
  412.           default:
  413.             goto err;
  414.         }
  415.         if (base == -1) {
  416.         }
  417.         
  418.         cptr = buf;
  419.         isNeg = False;
  420.         switch (arg.type) {
  421.           case MCShortType:
  422.             if (isSigned && type.u.shortV < 0) {
  423.             isNeg = True;
  424.             type.u.shortV = -type.u.shortV;
  425.             }
  426.             if (!type.u.shortV) break;
  427.             while (type.u.shortV) {
  428.             *cptr++ = index[type.u.shortV % base];
  429.             type.u.shortV /= 10;
  430.             }
  431.             break;
  432.           case MCLongType:
  433.             if (isSigned && type.u.longV < 0) {
  434.             isNeg = True;
  435.             type.u.longV = -type.u.longV;
  436.             }
  437.             if (!type.u.longV) break;
  438.             while (type.u.longV) {
  439.             *cptr++ = index[type.u.longV % base];
  440.             type.u.longV /= 10;
  441.             }
  442.             break;
  443.           case MCIntType:
  444.             if (isSigned && type.u.intV < 0) {
  445.             isNeg = True;
  446.             type.u.intV = -type.u.intV;
  447.             }
  448.             if (!type.u.intV) break;
  449.             while (type.u.intV) {
  450.             *cptr++ = index[type.u.intV % base];
  451.             type.u.intV /= 10;
  452.             }
  453.             break;
  454.         }
  455.  
  456.         /* precision */
  457.         if (cptr == buf && !arg.hasPrec) *cptr++ = '0';
  458.         if (arg.hasPrec && cptr-buf < arg.prec) {
  459.             for (j = cptr-buf; j < arg.prec; ++j) *cptr++ = '0';
  460.         }
  461.  
  462.         /* zero width padding */
  463.         if ((arg.flag & MCZeroFlag) && arg.hasWidth && !(arg.flag & MCMinusFlag)) {
  464.             for (j = cptr-buf; j < arg.width; ++j) *cptr++ = '0';
  465.             if (isNeg || (arg.flag & MCPlusFlag) || (arg.flag & MCSpaceFlag)) --cptr;
  466.         }
  467.         
  468.         /* signs */
  469.         if (isNeg) *cptr++ = '-';
  470.         else if (arg.flag & MCPlusFlag) *cptr++ = '+';
  471.         else if (arg.flag & MCSpaceFlag) *cptr++ = ' ';
  472.  
  473.         /* alternate forms */
  474.         if (arg.flag & MCAltFlag) {
  475.             if (arg.c == 'x') *cptr++ = 'x';
  476.             else if (arg.c == 'X') *cptr++ = 'X';
  477.             else if (arg.c != 'o') *cptr++ = '#';    /* Undefined */
  478.             *cptr++ = '0';
  479.         }
  480.         
  481.         /* get the storage space */
  482.         if (arg.hasWidth && arg.width > cptr-buf) len = arg.width;
  483.         else len = cptr-buf;
  484.         
  485.         replyList[ri].argType = MCDataArg;
  486.         fptr = replyList[ri].data = (char *) malloc(len+1);
  487.         replyList[ri].dataLen = len;
  488.         fptr[len] = NULL;
  489.  
  490.         if (arg.hasWidth && arg.width > cptr-buf) {
  491.             if (arg.flag & MCMinusFlag) {
  492.             /* pad the end */
  493.             fptr += len;
  494.             for (j = cptr-buf; j < arg.width; ++j) *--fptr = ' ';
  495.             fptr = replyList[ri].data;
  496.             } else {
  497.             /* pad the beginning */
  498.             for (j = cptr-buf; j < arg.width; ++j) *fptr++ = ' ';
  499.             }
  500.         }
  501.         for (j = cptr-buf; j > 0; --j) {
  502.             *fptr++ = *--cptr;
  503.         }
  504.         ++ri;
  505.         } else if (arg.type == MCStringType) {
  506. strType:
  507.         if (arg.hasPrec) len = arg.prec;
  508.         else len = type.u.charPV?strlen(type.u.charPV):0;
  509.         if (arg.hasWidth && arg.width > len) alen = arg.width;
  510.         else alen = len;
  511.         
  512.         replyList[ri].argType = MCUserArg;
  513.         fptr = replyList[ri].data = (char *) malloc(alen+1);
  514.         replyList[ri].dataLen = alen;
  515.         fptr[alen] = NULL;
  516.         
  517.         if (len < alen) {
  518.             if (arg.flag & MCMinusFlag) {
  519.             fptr += alen;
  520.             for (j = len; j < alen; ++j) *--fptr = ' ';
  521.             fptr = replyList[ri].data;
  522.             } else {
  523.             /* pad the beginning */
  524.             for (j = len; j < alen; ++j) *fptr++ = ' ';
  525.             }
  526.         }
  527.         bcopy(type.u.charPV, fptr, len);
  528.         ++ri;
  529.         } else if (arg.type == MCVoidPType) {
  530. #ifdef CATGETS
  531.         if (arg.c == 'C') {
  532.             catd = (nl_catd) type.u.voidPV;
  533.             goto nextArg;
  534.         }
  535. #endif
  536.         arg.c = 'X';
  537.         arg.flag |= MCAltFlag;
  538.         goto intType;
  539.         } else {
  540.         /* MCLongFloatType  MCFloatType */
  541.         /* MCShortPType MCLongPType MCIntPType */
  542.         goto err;
  543.         }
  544.         retlen += replyList[ri-1].dataLen;
  545.     }
  546. nextArg:;    /* Used for arguments with no output */
  547.     }
  548.     replyList[ri].argType = 0;
  549.     replyList[ri].data = NULL;
  550.     replyList[ri].dataLen = 0;
  551.     rock->replyCnt = ri;
  552.     
  553.     return(retlen);
  554. err:
  555.     MCPrintFree(rock);
  556.     return(-1);
  557. }    
  558.         
  559. void MCPrintFree(
  560. #if defined(__STDC__) || defined(__cplusplus)
  561.         MCRockT *rock)
  562. #else
  563.         rock)
  564. MCRockT *rock;
  565. #endif
  566. {
  567.     int        i;
  568.     
  569.     if (!rock) return;
  570.     
  571.     if (rock->argList) free(rock->argList);
  572.     if (rock->typeList) free(rock->typeList);
  573.     
  574.     if (rock->replyList) {
  575.     for (i = 0; i < rock->argCnt; ++i) {
  576.         if ((rock->replyList[i].argType & MCFree) && rock->replyList[i].data)
  577.           free(rock->replyList[i].data);
  578.     }
  579.     free(rock->replyList);
  580.     }
  581.     free(rock);
  582. }
  583.