home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / SQDEV200.ZIP / SRC / MSGAPI.C < prev    next >
C/C++ Source or Header  |  1994-05-23  |  12KB  |  517 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  Squish Developers Kit Source, Version 2.00                             *
  4.  *  Copyright 1989-1994 by SCI Communications.  All rights reserved.       *
  5.  *                                                                         *
  6.  *  USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE       *
  7.  *  SQUISH DEVELOPERS KIT LICENSING AGREEMENT IN SQDEV.PRN.  IF YOU DO NOT *
  8.  *  FIND THE TEXT OF THIS AGREEMENT IN THE AFOREMENTIONED FILE, OR IF YOU  *
  9.  *  DO NOT HAVE THIS FILE, YOU SHOULD IMMEDIATELY CONTACT THE AUTHOR AT    *
  10.  *  ONE OF THE ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO  *
  11.  *  USE THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE SQUISH          *
  12.  *  DEVELOPERS KIT LICENSING AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU ARE *
  13.  *  ABLE TO REACH WITH THE AUTHOR.                                         *
  14.  *                                                                         *
  15.  *  You can contact the author at one of the address listed below:         *
  16.  *                                                                         *
  17.  *  Scott Dudley       FidoNet     1:249/106                               *
  18.  *  777 Downing St.    Internet    sjd@f106.n249.z1.fidonet.org            *
  19.  *  Kingston, Ont.     CompuServe  >INTERNET:sjd@f106.n249.z1.fidonet.org  *
  20.  *  Canada  K7M 5N3    BBS         1-613-634-3058, V.32bis                 *
  21.  *                                                                         *
  22.  ***************************************************************************/
  23.  
  24. #pragma off(unreferenced)
  25. static char rcs_id[]="$Id: MSGAPI.C 1.7 1994/02/25 05:26:33 sjd Exp sjd $";
  26. #pragma on(unreferenced)
  27.  
  28. #define MSGAPI_INIT
  29.  
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <io.h>
  33. #include <fcntl.h>
  34. #include "alc.h"
  35. #include "prog.h"
  36. #include "max.h"
  37. #include "msgapi.h"
  38. #include "apidebug.h"
  39.  
  40.  
  41.  
  42. static byte *intl="INTL";
  43. static byte *fmpt="FMPT";
  44. static byte *topt="TOPT";
  45. static byte *area_colon="AREA:";
  46.  
  47. static char *copyright="MSGAPI - Copyright 1991 by Scott J. Dudley.  All rights reserved.";
  48.  
  49. #ifdef OS_2
  50.   word _stdc far msgapierr=0;   /* Global error value for MsgAPI routines */
  51. #else
  52.   word _stdc msgapierr=0;       /* Global error value for MsgAPI routines */
  53. #endif
  54.  
  55. struct _minf _stdc mi;
  56.  
  57. unsigned _SquishCloseOpenAreas(void);
  58.  
  59. #ifdef __WATCOMC__  /* handle DLL startup requirements for WC 8.5 */
  60.   #if defined(OS_2)
  61.  
  62.     #define INCL_SUB
  63.     #define INCL_DOS
  64.     #include "pos2.h"
  65.  
  66.     void APIENTRY __DLLend(OS2UINT usTermCode)
  67.     {
  68.       char temp[100];
  69.  
  70.       _SquishCloseOpenAreas();
  71.  
  72.       sprintf(temp, "MsgAPI 2.0 Termination, usTermCode=%d\r\n",
  73.               usTermCode);
  74.  
  75.       VioWrtTTY(temp, 38, 0);
  76.       DosExitList(EXLST_EXIT, 0);
  77.     }
  78.  
  79.     int __dll_initialize(void)
  80.     {
  81.       extern char startup_txt[];
  82.       grow_handles(40);
  83.  
  84.       VioWrtTTY(startup_txt, strlen(startup_txt), 0);
  85.       /*DosBeep(200, 20);*/
  86.  
  87.       DosExitList(EXLST_ADD, __DLLend);
  88.       return 1;
  89.     }
  90.  
  91.   #endif
  92. #endif
  93.  
  94. /* Default allocation thunks */
  95.  
  96. static void OS2FAR * MAPIENTRY _palloc(size_t size)
  97. { return ((void OS2FAR *)malloc(size)); }
  98.  
  99. static void MAPIENTRY _pfree(void OS2FAR *ptr)
  100. { free(ptr); }
  101.  
  102. static void OS2FAR * MAPIENTRY _repalloc(void OS2FAR *ptr, size_t size)
  103. { return ((void OS2FAR *)realloc(ptr, size)); }
  104.  
  105. static void far * MAPIENTRY _farpalloc(size_t size)
  106. { return ((void far *)farmalloc(size)); }
  107.  
  108. static void MAPIENTRY _farpfree(void far *ptr)
  109. { farfree(ptr); }
  110.  
  111. static void far * MAPIENTRY _farrepalloc(void far *ptr, size_t size)
  112. { return ((void far *)farrealloc(ptr, size)); }
  113.  
  114.  
  115. void OS2FAR * (MAPIENTRY *palloc)(size_t size) = _palloc;
  116. void (MAPIENTRY *pfree)(void OS2FAR *ptr) = _pfree;
  117. void OS2FAR * (MAPIENTRY *repalloc)(void OS2FAR *ptr, size_t size) = _repalloc;
  118.  
  119. void far * (MAPIENTRY *farpalloc)(size_t size) = _farpalloc;
  120. void (MAPIENTRY *farpfree)(void far *ptr) = _farpfree;
  121. void far * (MAPIENTRY *farrepalloc)(void far *ptr, size_t size) = _farrepalloc;
  122.  
  123. sword MAPIENTRY MsgOpenApi(struct _minf OS2FAR *minf)
  124. {
  125.   NW(copyright);
  126.  
  127.   (void)memset(&mi, '\0', sizeof mi);
  128.   mi=*minf;
  129.   mi.haveshare=minf->haveshare=shareloaded();
  130.  
  131.   /* If the caller wants to set the malloc/free hooks, do so here */
  132.  
  133.   if (mi.req_version >= 1)
  134.   {
  135.     if (mi.palloc)
  136.       palloc=mi.palloc;
  137.  
  138.     if (mi.pfree)
  139.       pfree=mi.pfree;
  140.  
  141.     if (mi.repalloc)
  142.       repalloc=mi.repalloc;
  143.  
  144.     if (mi.farpalloc)
  145.       farpalloc=mi.farpalloc;
  146.  
  147.     if (mi.farpfree)
  148.       farpfree=mi.farpfree;
  149.  
  150.     if (mi.farrepalloc)
  151.       farrepalloc=mi.farrepalloc;
  152.   }
  153.  
  154.   return 0;
  155. }
  156.  
  157. sword MAPIENTRY MsgCloseApi(void)
  158. {
  159.   return _SquishCloseOpenAreas() ? 0 : -1;
  160. }
  161.  
  162. HAREA MAPIENTRY MsgOpenArea(byte OS2FAR *name, word mode, word type)
  163. {
  164.   if (type & MSGTYPE_SQUISH)
  165.     return (SquishOpenArea(name,mode,type));
  166.   else return (SdmOpenArea(name,mode,type));
  167. }
  168.  
  169. sword MAPIENTRY MsgValidate(word type, byte OS2FAR *name)
  170. {
  171.   if (type & MSGTYPE_SQUISH)
  172.     return (SquishValidate(name));
  173.   else /*if (type==MSGTYPE_SDM)*/
  174.     return (SdmValidate(name));
  175. /*  else return FALSE;*/
  176. }
  177.  
  178. /* Check to see if a message handle is valid.  This function should work    *
  179.  * for ALL handlers tied into MsgAPI.  This also checks to make sure that   *
  180.  * the area which the message is from is also valid.  (ie. The message      *
  181.  * handle isn't valid, unless the area handle of that message is also       *
  182.  * valid.)                                                                  */
  183.  
  184. sword MAPIENTRY InvalidMsgh(HMSG msgh)
  185. {
  186.   if (msgh==NULL || msgh->id != MSGH_ID || MsgInvalidHarea(msgh->ha))
  187.   {
  188.     msgapierr=MERR_BADH;
  189.     return TRUE;
  190.   }
  191.  
  192.   return FALSE;
  193. }
  194.  
  195. /* Check to ensure that a message area handle is valid.                     */
  196.  
  197. sword MAPIENTRY InvalidMh(HAREA mh)
  198. {
  199.   if (mh==NULL || mh->id != MSGAPI_ID)
  200.   {
  201.     msgapierr=MERR_BADH;
  202.     return TRUE;
  203.   }
  204.  
  205.   return FALSE;
  206. }
  207.  
  208.  
  209. byte * MAPIENTRY StripNasties(byte *str)
  210. {
  211.   byte *s;
  212.  
  213.   for (s=str; *s; s++)
  214.     if (*s < ' ')
  215.       *s=' ';
  216.  
  217.   return str;
  218. }
  219.  
  220.  
  221.  
  222.  
  223. /* Copy the text itself to a buffer, or count its length if out==NULL */
  224.  
  225. static word near _CopyToBuf(byte *p, byte *out, byte **end)
  226. {
  227.   word len=0;
  228.  
  229.   if (out)
  230.     *out++='\x01';
  231.  
  232.   len++;
  233.  
  234.  
  235.   for (; *p=='\x0d' || *p=='\x0a' || *p==(byte)0x8d; p++)
  236.     ;
  237.  
  238.   while (*p=='\x01' || strncmp(p, area_colon, 5)==0)
  239.   {
  240.     /* Skip over the first ^a */
  241.  
  242.     if (*p=='\x01')
  243.       p++;
  244.  
  245.     while (*p && *p != '\x0d' && *p != '\x0a' && *p != (byte)0x8d)
  246.     {
  247.       if (out)
  248.         *out++=*p;
  249.       
  250.       p++;
  251.       len++;
  252.     }
  253.  
  254.     if (out)
  255.       *out++='\x01';
  256.  
  257.     len++;
  258.  
  259.     while (*p=='\x0d' || *p=='\x0a' || *p==(byte)0x8d)
  260.       p++;
  261.   }
  262.  
  263.   /* Cap the string */
  264.  
  265.   if (out)
  266.     *out='\0';
  267.  
  268.   len++;
  269.   
  270.  
  271.   /* Make sure to leave no trailing x01's. */
  272.  
  273.   if (out && out[-1]=='\x01')
  274.     out[-1]='\0';
  275.   
  276.  
  277.   /* Now store the new end location of the kludge lines */
  278.  
  279.   if (end)
  280.     *end=p;
  281.   
  282.   return len;
  283. }
  284.  
  285.  
  286. /* Used to free returned ptr from MsgCvtCtrlToKludge, MsgCreateCtrlBuf */
  287.  
  288. void MAPIENTRY MsgFreeCtrlBuf(char *cbuf)
  289. {
  290.   pfree(cbuf);
  291. }
  292.  
  293. /* Used to free returned ptr from MsgGetCtrlToken */
  294.  
  295. void MAPIENTRY MsgFreeCtrlToken(char *cbuf)
  296. {
  297.   pfree(cbuf);
  298. }
  299.  
  300.  
  301.  
  302. byte OS2FAR * MAPIENTRY CopyToControlBuf(byte OS2FAR *txt, byte OS2FAR * OS2FAR *newtext, unsigned OS2FAR *length)
  303. {
  304.   byte *cbuf, *end;
  305.  
  306.   word clen;
  307.  
  308.  
  309.   /* Figure out how long the control info is */
  310.  
  311.   clen=_CopyToBuf(txt, NULL, NULL);
  312.  
  313.   /* Allocate memory for it */
  314.   
  315.   #define SAFE_CLEN 20
  316.  
  317.   if ((cbuf=palloc(clen+SAFE_CLEN))==NULL)
  318.     return NULL;
  319.  
  320.   (void)memset(cbuf, '\0', clen+SAFE_CLEN);
  321.  
  322.   /* Now copy the text itself */
  323.  
  324.   clen=_CopyToBuf(txt, cbuf, &end);
  325.  
  326.   if (length)
  327.     *length -= (size_t)(end-txt);
  328.  
  329.   if (newtext)
  330.     *newtext=end;
  331.  
  332.   return cbuf;
  333. }
  334.  
  335.  
  336. byte OS2FAR * MAPIENTRY GetCtrlToken(byte OS2FAR *where, byte OS2FAR *what)
  337. {
  338.   byte *end, *found, *out;
  339.  
  340.   if (where && (found=strstr(where, what)) != NULL && found[-1]=='\x01')
  341.   {
  342.     end=strchr(found,'\x01');
  343.  
  344.     if (!end)
  345.       end=found+strlen(found);
  346.  
  347.     if ((out=palloc((size_t)(end-found)+1))==NULL)
  348.       return NULL;
  349.  
  350.     (void)memmove(out, found, (size_t)(end-found));
  351.     out[(size_t)(end-found)]='\0';
  352.     return out;
  353.   }
  354.  
  355.   return NULL;
  356. }
  357.  
  358.  
  359. void MAPIENTRY ConvertControlInfo(byte OS2FAR *ctrl, NETADDR OS2FAR *orig, NETADDR OS2FAR *dest)
  360. {
  361.   byte *p, *s;
  362.   
  363.   if ((p=s=GetCtrlToken(ctrl, intl)) != NULL)
  364.   {
  365.     NETADDR norig, ndest;
  366.  
  367.     /* Copy the defaults from the original address */
  368.  
  369.     norig=*orig;
  370.     ndest=*dest;
  371.  
  372.     /* Parse the destination part of the kludge */
  373.  
  374.     s += 5;
  375.     Parse_NetNode(s, &ndest.zone, &ndest.net, &ndest.node, &ndest.point);
  376.     
  377.     while (*s != ' ' && *s)
  378.       s++;
  379.  
  380.     if (*s)
  381.       s++;
  382.  
  383.     Parse_NetNode(s, &norig.zone, &norig.net, &norig.node, &norig.point);
  384.  
  385.     pfree(p);
  386.  
  387.     /* Only use this as the "real" zonegate address if the net/node         *
  388.      * addresses in the INTL line match those in the message                *
  389.      * body.  Otherwise, it's probably a gaterouted message!                */
  390.  
  391.     if (ndest.net==dest->net && ndest.node==dest->node &&
  392.         norig.net==orig->net && norig.node==orig->node)
  393.     {
  394.       *dest=ndest;
  395.       *orig=norig;
  396.  
  397.       /* Only remove the INTL line if it's not gaterouted, which is         *
  398.        * why we do it here.                                                 */
  399.       
  400.       RemoveFromCtrl(ctrl,intl);
  401.     }
  402.   }
  403.  
  404.  
  405.   /* Handle the FMPT kludge */
  406.   
  407.   if ((s=GetCtrlToken(ctrl,fmpt)) != NULL)
  408.   {
  409.     orig->point=(word)atoi(s+5);
  410.     pfree(s);
  411.  
  412.     RemoveFromCtrl(ctrl,fmpt);
  413.   }
  414.  
  415.   
  416.   /* Handle TOPT too */
  417.   
  418.   if ((s=GetCtrlToken(ctrl,topt)) != NULL)
  419.   {
  420.     dest->point=(word)atoi(s+5);
  421.     pfree(s);
  422.  
  423.     RemoveFromCtrl(ctrl,topt);
  424.   }
  425. }
  426.  
  427.   
  428. byte OS2FAR * MAPIENTRY CvtCtrlToKludge(byte OS2FAR *ctrl)
  429. {
  430.   byte *from;
  431.   byte *buf;
  432.   byte *to;
  433.   size_t clen;
  434.   
  435.   clen=strlen(ctrl) + NumKludges(ctrl) + 20;
  436.   
  437.   if ((buf=palloc(clen))==NULL)
  438.     return NULL;
  439.   
  440.   to=buf;
  441.  
  442.   /* Convert ^aKLUDGE^aKLUDGE... into ^aKLUDGE\r^aKLUDGE\r... */
  443.   
  444.   for (from=ctrl; *from=='\x01' && from[1];)
  445.   {
  446.     /* Only copy out the ^a if it's NOT the area: line */
  447.  
  448.     if (!eqstrn(from+1, area_colon, 5))
  449.       *to++=*from;
  450.  
  451.     from++;
  452.     
  453.     while (*from && *from != '\x01')
  454.       *to++=*from++;
  455.  
  456.     *to++='\r';
  457.   }
  458.   
  459.   *to='\0';
  460.  
  461.   return buf;
  462. }
  463.  
  464.  
  465.  
  466.     
  467. void MAPIENTRY RemoveFromCtrl(byte OS2FAR *ctrl, byte OS2FAR *what)
  468. {
  469.   byte *search;
  470.   byte *p, *s;
  471.   
  472.   if ((search=palloc(strlen(what)+2))==NULL)
  473.     return;
  474.   
  475.   (void)strcpy(search, "\x01");
  476.   (void)strcat(search, what);
  477.   
  478.   /* Now search for this token in the control buffer, and remove it. */
  479.     
  480.   while ((p=strstr(ctrl, search)) != NULL)
  481.   {
  482.     for (s=p+1; *s && *s != '\x01'; s++)
  483.       ;
  484.       
  485.     (void)strocpy(p, s);
  486.   }
  487.   
  488.   pfree(search);
  489. }
  490.  
  491.  
  492. word MAPIENTRY NumKludges(char OS2FAR *txt)
  493. {
  494.   word nk=0;
  495.   char *p;
  496.   
  497.   for (p=txt; (p=strchr(p, '\x01')) != NULL; p++)
  498.     nk++;
  499.     
  500.   return nk;
  501. }
  502.  
  503. #ifdef OS_2
  504.  
  505. int far pascal farread(int handle, byte far *buf, unsigned len)
  506. {
  507.   return read(handle, buf, len);
  508. }
  509.  
  510. int far pascal farwrite(int handle, byte far *buf, unsigned len)
  511. {
  512.   return write(handle, buf, len);
  513. }
  514.  
  515. #endif
  516.  
  517.