home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 January / Chip_1997-01_cd.bin / ms95 / disk22 / dir03 / f014640.re_ / f014640.re
Text File  |  1996-04-02  |  9KB  |  306 lines

  1. /*----------------------------------------------------------------------+
  2. |                                    |
  3. |  Copyright (1995) Bentley Systems, Inc., All rights reserved.        |
  4. |                                    |
  5. |  "MicroStation" is a registered trademark and "MDL" and "MicroCSL"    |
  6. |  are trademarks of Bentley Systems, Inc.                    |
  7. |                                    |
  8. |  Limited permission is hereby granted to reproduce and modify this    |
  9. |  copyrighted material provided that the resulting code is used only     |
  10. |  in conjunction with Bentley Systems products under the terms of the    |
  11. |  license agreement provided therein, and that this notice is retained    |
  12. |  in its entirety in any such reproduction or modification.        |
  13. |                                    |
  14. +----------------------------------------------------------------------*/
  15. /*----------------------------------------------------------------------+
  16. |                                    |
  17. |    $Logfile:   K:/mdl/examples/externpg/externpg.c_v  $
  18. |   $Workfile:   externpg.c  $
  19. |   $Revision:   5.19  $
  20. |       $Date:   22 Nov 1995 12:40:04  $
  21. |                                    |
  22. +----------------------------------------------------------------------*/
  23. /*----------------------------------------------------------------------+
  24. |                                    |
  25. |   externpg.c -- Sample external program.  This program is started     |
  26. |          by the MDL application in externpg.mc.        |
  27. |                                    |
  28. +----------------------------------------------------------------------*/
  29. /*----------------------------------------------------------------------+
  30. |                                    |
  31. |   Include Files                               |
  32. |                                    |
  33. +----------------------------------------------------------------------*/
  34. /* basedefs.h is first to make sure the environment constants such as
  35.    "unix" are defined. */
  36. #include <basedefs.h>    
  37.  
  38. #include <mdl.h>
  39. #include <msextern.h>
  40. #include "externpg.h"
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <errno.h>
  45. #include <extprg.fdf>
  46.  
  47. /*----------------------------------------------------------------------+
  48. |                                    |
  49. |   Local defines                            |
  50. |                                    |
  51. +----------------------------------------------------------------------*/
  52. #define strcpyFromFar(dp, sp) \
  53.  { \
  54.  char            *d; \
  55.  SHMEM_SC char   *s; \
  56.  d = dp, s = sp; \
  57.  while ((*d++ = *s++) != 0); \
  58.  }
  59.  
  60. #define strcpyToFar(dp, sp) \
  61.  { \
  62.  char            *s; \
  63.  SHMEM_SC char   *d; \
  64.  d = dp, s = sp; \
  65.  while ((*d++ = *s++) != 0); \
  66.  }
  67.  
  68. /*----------------------------------------------------------------------+
  69. |                                    |
  70. |   Local type definitions                        |
  71. |                                    |
  72. +----------------------------------------------------------------------*/
  73. /*----------------------------------------------------------------------+
  74. |                                    |
  75. |   Global variables                            |
  76. |                                    |
  77. +----------------------------------------------------------------------*/
  78. SHMEM_SC char            *pSharedMem;
  79. MSMsgqDescr            *pQueueDescr;
  80.  
  81. #if defined (macintosh)
  82. FILE                *logFile;
  83. #endif
  84.  
  85. /*----------------------------------------------------------------------+
  86. |                                    |
  87. |   External variables                            |
  88. |                                    |
  89. +----------------------------------------------------------------------*/
  90. /*----------------------------------------------------------------------+
  91. |                                    |
  92. |   Function declarations                        |
  93. |                                    |
  94. +----------------------------------------------------------------------*/
  95.  
  96. /*ff Major Public Code Section */
  97. /*----------------------------------------------------------------------+
  98. |                                    |
  99. |   Major Public Code Section                        |
  100. |                                    |
  101. +----------------------------------------------------------------------*/
  102. /*----------------------------------------------------------------------+
  103. |                                    |
  104. | name        externpg_init                        |
  105. |                                    |
  106. | author    BSI                     10/92       |
  107. |                                    |
  108. +----------------------------------------------------------------------*/
  109. Private void    externpg_init
  110. (
  111. void
  112. )
  113.     {
  114. #if defined (macintosh)
  115.     /* Set the stack size */
  116.     extprg_initializePlatform (16000);
  117.  
  118.     /* Open a file for printing progress and error messages */
  119.     logFile = fopen ("externpg.log", "w");
  120. #endif
  121.     }
  122.  
  123. /*----------------------------------------------------------------------+
  124. |                                    |
  125. | name        externpg_output                        |
  126. |                                    |
  127. | author    BSI                     10/92       |
  128. |                                    |
  129. +----------------------------------------------------------------------*/
  130. Private void    externpg_output
  131. (
  132. char        *string            /* => string to output */
  133. )
  134.     {
  135. #if defined (macintosh)
  136.     if (logFile)
  137.     fprintf (logFile, "%s\n", string);
  138. #else
  139.     printf ("%s\n", string);
  140. #endif
  141.     }
  142.  
  143. /*----------------------------------------------------------------------+
  144. |                                    |
  145. | name        externpg_exit                        |
  146. |                                    |
  147. | author    BSI                     10/92       |
  148. |                                    |
  149. +----------------------------------------------------------------------*/
  150. Private void    externpg_exit
  151. (
  152. int    exitStatus
  153. )
  154.     {
  155. #if defined (macintosh)
  156.     /* Close the message file */
  157.     fclose (logFile);
  158. #endif
  159.  
  160.     extprg_shutdownPlatform ();
  161.  
  162.     exit(exitStatus);
  163.     }
  164.  
  165. #if defined (MWBUGFIXED)
  166. /*----------------------------------------------------------------------+
  167. |                                    |
  168. | name        .                            |
  169. |                                    |
  170. | author    BSI                     03/90       |
  171. |                                    |
  172. +----------------------------------------------------------------------*/
  173. Private void    strcpyFromFar
  174. (
  175. char        *destinationP,
  176. SHMEM_SC char    *sourceP
  177. )
  178.     {
  179.     /* Stop as soon as the end of the string is copied. */
  180.     while ((*destinationP++ = *sourceP++) != 0)
  181.     ;
  182.     }
  183.  
  184. /*----------------------------------------------------------------------+
  185. |                                    |
  186. | name        .                            |
  187. |                                    |
  188. | author    BSI                     03/90       |
  189. |                                    |
  190. +----------------------------------------------------------------------*/
  191. Private void    strcpyToFar
  192. (
  193. SHMEM_SC char    *destinationP,
  194. char        *sourceP
  195. )
  196.     {
  197.     /* Stop as soon as the end of the string is copied. */
  198.     while ((*destinationP++ = *sourceP++) != 0)
  199.     ;
  200.     }
  201. #endif
  202.  
  203. /*----------------------------------------------------------------------+
  204. |                                    |
  205. | name        .                            |
  206. |                                    |
  207. | author    BSI                     03/90       |
  208. |                                    |
  209. +----------------------------------------------------------------------*/
  210. Public void main
  211. (
  212. int    argc,
  213. char    *argv[]
  214. )
  215.     {
  216.     ExternalMessage message;
  217.     char    displayBuffer [200];
  218.     int        i;
  219.  
  220.     externpg_init ();
  221.  
  222.     /* Fetch command line arguments if not passed directly (Macintosh) */
  223.     extprg_getCommandLineArguments (&argc, &argv);
  224.  
  225.     for (i = 0; i < argc; i++)
  226.     externpg_output (argv[i]);
  227.  
  228. #if defined (pm386) && defined (debugMWPro)
  229.     _inline (0xcc);
  230. #endif
  231.  
  232.     /*  First initialize the message queue and shared memory. */
  233.     if (argc != 3)
  234.     externpg_exit (1);
  235.  
  236.     if ((pQueueDescr = extprg_queueAttach (*(argv+1))) == NULL)
  237.     externpg_exit (2);
  238.  
  239.     externpg_output ("Got the queue descriptor.");
  240.  
  241.     if ((pSharedMem = extprg_shmemAttach (*(argv+2))) == NULL)
  242.     {
  243.     sprintf (displayBuffer,
  244.          "Error attaching to the shared memory %s, errno = %d.",
  245.              *(argv+2), errno);
  246.     externpg_output (displayBuffer);
  247.     externpg_exit (3);
  248.     }
  249.  
  250.     externpg_output ("Got the shared memory.");
  251.  
  252.     for (;;)
  253.     {
  254.     if (extprg_messageReceive (&message, pQueueDescr, 0, 0) != 0)
  255.         {
  256.         sprintf (displayBuffer,
  257.              "messageReceive returned an error. errno = %d.",
  258.              errno);
  259.         externpg_output (displayBuffer);
  260.         externpg_exit (4);
  261.         }
  262.  
  263.     externpg_output ("Received a message.");
  264.  
  265.     switch (message.reqtype)
  266.         {
  267.         case EXTERNPG_UPDATE:
  268.         externpg_output ("received update request.");
  269.         message.reqtype = EXTERNPG_ACKNOWLEDGE;
  270.         message.msglength = 0;
  271.         /*  Do all time critical work prior to responding to
  272.             MicroStation.
  273.         */
  274.         if (extprg_messageSend (&message, pQueueDescr))
  275.             externpg_exit (5);
  276.         break;
  277.  
  278.         case EXTERNPG_DISPLAY:
  279.         /*  Display the contents of the shared memory buffer */
  280.         strcpyFromFar (displayBuffer, pSharedMem);
  281.         externpg_output (displayBuffer);
  282.  
  283.         message.reqtype = EXTERNPG_ACKNOWLEDGE;
  284.         message.msglength = 0;
  285.         strcpyToFar (pSharedMem, "Here it is");
  286.         if (extprg_messageSend (&message, pQueueDescr))
  287.             externpg_exit (5);
  288.         break;
  289.         case EXTERNPG_TERMINATE:
  290.         /*  The MDL application is being unloaded.  Perform
  291.             any clean up required, such as closing and flushing
  292.             files.  Then send an acknowledge to the
  293.             application.                */
  294.         message.reqtype = EXTERNPG_ACKNOWLEDGE;
  295.         message.msglength = 0;
  296.         if (extprg_messageSend (&message, pQueueDescr))
  297.             externpg_exit (5);
  298.         /*  Exit with no errors.  */
  299.         externpg_exit (20);
  300.         default:
  301.         externpg_output ("in default case.");
  302.         externpg_exit (6);
  303.         }
  304.     }
  305.     }
  306.