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