home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 January / Chip_1997-01_cd.bin / ms95 / disk21 / dir03 / f015950.re_ / f015950.re
Text File  |  1996-04-02  |  18KB  |  587 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. |   $Workfile:   mdlshare.mc  $
  18. |   $Revision:   6.6  $
  19. |       $Date:   30 Aug 1995 08:42:18  $
  20. |                                    |
  21. +----------------------------------------------------------------------*/
  22. /*----------------------------------------------------------------------+
  23. |                                    |
  24. |   Function -                                |
  25. |                                    |
  26. |       MDL Shared Library Example Application                    |
  27. |                                        |
  28. |        This MDL application also serves as an MDL shared            |
  29. |        library which has functions and variables which can            |
  30. |        be referenced by other MDL applications.                |
  31. |                                    |
  32. |       - - - - - - - - - - - - - - - - - - - - - - - - - - - - -       |
  33. |                                    |
  34. |   Public Routine Summary -                        |
  35. |                                        |
  36. |       mdlshare_passwordHook - Password text item hook function    |
  37. |       mdlshare_passwordDialog - Password dialog box command function    |
  38. |       mdlshare_setPassword - Set task password value                |
  39. |       mdlshare_setPasswordVarP - Set pointer to local task access     |
  40. |                   string for text item password    |
  41. |       initialize - Main entry point for each MDL application ref-     |
  42. |             erencing this shared library                |
  43. |       main - Main application entry point                    |
  44. |                                    |
  45. +----------------------------------------------------------------------*/
  46. /*----------------------------------------------------------------------+
  47. |                                    |
  48. |   Include Files                               |
  49. |                                    |
  50. +----------------------------------------------------------------------*/
  51. #include <mdl.h>
  52. #include <msdefs.h>
  53. #include <basedefs.h>
  54. #include <global.h>
  55. #include <dlogids.h>
  56. #include <dlogitem.h>
  57. #include <cmdlist.h>
  58. #include <keys.h>
  59. #include <cexpr.h>
  60. #include <userfnc.h>
  61. #include <stdarg.h>
  62. #include <string.h>
  63. #include <stdlib.h>
  64. #include <stdio.h>
  65.  
  66. #include "mdlshare.h"
  67. #include "mdlshcmd.h"
  68.  
  69. #include <dlogman.fdf>
  70. #include <mssystem.fdf>
  71. #include <msrsrc.fdf>
  72. #include <msparse.fdf>
  73. #include <msfile.fdf>
  74.  
  75. /*----------------------------------------------------------------------+
  76. |                                    |
  77. |   Public Variables                            |
  78. |                                    |
  79. +----------------------------------------------------------------------*/
  80. Public    int     taskCount = 0;
  81.  
  82. /*----------------------------------------------------------------------+
  83. |                                    |
  84. |   Private Global variables                        |
  85. |                                    |
  86. +----------------------------------------------------------------------*/
  87. Private int        initialized = FALSE;
  88. Private TaskInfo   *taskListP = NULL;
  89. Private int        pwIndex = 0;
  90. Private char        localPassword[26];
  91. Private char        mdlshareTaskId[32];
  92. Private char        fullPathName[MAXFILELENGTH];
  93. Private TaskInfo    libTaskInfo = {
  94.                     0L, NULL, NULL, NULL, 0, 0
  95.                   };
  96.  
  97. /*----------------------------------------------------------------------+
  98. |                                    |
  99. | name        share_printMsg                            |
  100. |                                    |
  101. | author    BSI                    05/95        |
  102. |                                    |
  103. +----------------------------------------------------------------------*/
  104. Private void    share_printMsg
  105. (
  106. int    strNum,
  107. ...
  108. )
  109.     {
  110.     char    msgBuf[128], formatStr[128];
  111.     va_list ap;
  112.  
  113.     /*    Display indicated message in messages dialog box     */
  114.     mdlResource_loadFromStringList (formatStr, libTaskInfo.rscFileH,
  115.                     MESSAGEID_Msgs, strNum);
  116.     va_start (ap, strNum);
  117.     vsprintf (msgBuf, formatStr, ap);
  118.     va_end (ap);
  119.  
  120.     mdlDialog_dmsgsPrint (msgBuf);
  121.     }
  122.  
  123. /*----------------------------------------------------------------------+
  124. |                                    |
  125. | name        share_getTaskInfoP                    |
  126. |                                    |
  127. | author    BSI                    05/95        |
  128. |                                    |
  129. +----------------------------------------------------------------------*/
  130. Private void *share_getTaskInfoP
  131. (
  132. void   *mdlDescrP
  133. )
  134.     {
  135.     TaskInfo   *taskP = taskListP;
  136.     void       *currDesc = mdlSystem_getCurrMdlDesc ();
  137.     void       *mdlDscrP = (mdlDescrP ? mdlDescrP : currDesc);
  138.  
  139.     /*    Locate the task information block for the task indicated by */
  140.     /*    the MDL Descriptor parameter.                    */
  141.     while (taskP && (taskP->mdlDescrP != mdlDscrP))
  142.         taskP = taskP->next;
  143.  
  144.     /*    If didn't find a match, see if it is our own task.        */
  145.     if (!taskP &&
  146.         ((mdlDescrP == libTaskInfo.mdlDescrP) ||
  147.      (!mdlDescrP && (libTaskInfo.mdlDescrP == currDesc))))
  148.     return &libTaskInfo;
  149.  
  150.     return (taskP);
  151.     }
  152.  
  153. /*----------------------------------------------------------------------+
  154. |                                    |
  155. | name        share_passwordHook                        |
  156. |                                    |
  157. | author    BSI                     05/95        |
  158. |                                    |
  159. +----------------------------------------------------------------------*/
  160. Public void share_passwordHook
  161. (
  162. DialogItemMessage    *dimP
  163. )
  164.     {
  165.     TaskInfo   *taskP = share_getTaskInfoP (NULL);
  166.     char       *pwdP;
  167.  
  168.     /*    Password Hook function - get proper access string from task */
  169.     /*    block.                                */
  170.     if (taskP)
  171.     pwdP = taskP->passwordP;
  172.     else
  173.     return;
  174.  
  175.     dimP->msgUnderstood = TRUE;
  176.  
  177.     switch (dimP->messageType)
  178.     {
  179.     case DITEM_MESSAGE_CREATE:
  180.         /*    Initialize our access string                */
  181.         strcpy (pwdP, "\0");
  182.         pwIndex = 0;
  183.         break;
  184.  
  185.     case DITEM_MESSAGE_FOCUSIN:
  186.         /*    Check our string position                */
  187.         if (strcmp (pwdP, "\0") == 0)
  188.         pwIndex = 0;
  189.         break;
  190.  
  191.     case DITEM_MESSAGE_KEYSTROKE:
  192.         {
  193.         /*    Handle the input keystroke for backspae processing  */
  194.         int keystroke = dimP->u.keystroke.keystroke;
  195.  
  196.         switch (keystroke)
  197.         {
  198.         case VKEY_BACKSPACE:
  199.             if (--pwIndex < 0)
  200.                 pwIndex = 0;
  201.             pwdP[pwIndex] = '\0';
  202.                 break;
  203.  
  204.          default:
  205.             if ((keystroke > 0x20) && (keystroke < 0x7f) && (pwIndex < 24))
  206.             {
  207.                     pwdP[pwIndex++] = keystroke;
  208.                        dimP->u.keystroke.keystroke = 'X';
  209.                }
  210.             break;
  211.             }
  212.         break;
  213.         }
  214.  
  215.     case DITEM_MESSAGE_FOCUSOUT:
  216.         /*    Mark end of string if leaving                */
  217.           pwdP[pwIndex] = '\0';
  218.         break;
  219.  
  220.     default:
  221.         dimP->msgUnderstood = FALSE;
  222.         break;
  223.     }
  224.     }
  225.  
  226. /*----------------------------------------------------------------------+
  227. |                                    |
  228. | name        share_openDialog                    |
  229. |                                    |
  230. | author    BSI                        06/95        |
  231. |                                    |
  232. +----------------------------------------------------------------------*/
  233. Private int share_openDialog
  234. (
  235. void
  236. )
  237.     {
  238.     long        lastAct;
  239.  
  240.     /*    Open dialog and return value                    */
  241.     mdlDialog_openModal (&lastAct, NULL, DIALOGID_Password);
  242.     return (lastAct);
  243.     }
  244.  
  245. /*----------------------------------------------------------------------+
  246. |                                    |
  247. | name        share_passwordDialog                    |
  248. |                                    |
  249. | author    BSI                     5/95        |
  250. |                                    |
  251. +----------------------------------------------------------------------*/
  252. Public int share_passwordDialog
  253. (
  254. char   *pwd        /* <=> Password to compare against/return area  */
  255. ) cmdNumber CMD_MDLSHARE_PASSWORD
  256.     {
  257.     TaskInfo   *taskP = share_getTaskInfoP (NULL);
  258.     long        lastAct;
  259.     char       *localPasswordP;
  260.  
  261.     if (!taskP)
  262.     return ERROR;
  263.  
  264.     share_printMsg (MSGID_PasswordS, taskP->taskId);
  265.  
  266.     /*    Open the dialog box in lib task space and get the password  */
  267.     /*    from the user                            */
  268.     lastAct = mdlDialog_callFunction (libTaskInfo.mdlDescrP,
  269.                       share_openDialog, NULL);
  270.  
  271.     /*    Have a password - now get the result and return that and    */
  272.     /*    the result to the user via the input pointer value        */
  273.     if (pwd)
  274.     {
  275.     /*  Caller gave us memory to hold password            */
  276.     if (*pwd)
  277.         {
  278.         /*    Have password to compare against            */
  279.         if (strcmp (pwd, localPassword) != 0)
  280.         return 1;
  281.         else
  282.         return SUCCESS;
  283.         }
  284.     else
  285.         {
  286.         /*    Have return area to place user password into        */
  287.         strcpy (pwd, localPassword);
  288.  
  289.         /*    If task block has a password compare it to the user */
  290.         /*    value, otherwise just return                */
  291.         if (*taskP->password)
  292.             if (strcmp (taskP->password, localPassword) != 0)
  293.             return 1;
  294.             else
  295.             return SUCCESS;
  296.         else
  297.             return 2;
  298.         }
  299.         }
  300.     else
  301.     /*  If task block has a password compare it to the user     */
  302.     /*  value, otherwise just return                */
  303.     if (*taskP->password)
  304.         if (strcmp (taskP->password, localPassword) != 0)
  305.             return 1;
  306.         else
  307.             return SUCCESS;
  308.     else
  309.         return 3;
  310.     }
  311.  
  312. /*----------------------------------------------------------------------+
  313. |                                    |
  314. | name        share_setPassword                    |
  315. |                                    |
  316. | author    BSI                    05/95        |
  317. |                                    |
  318. +----------------------------------------------------------------------*/
  319. Public void    share_setPassword
  320. (
  321. char   *password
  322. ) cmdNumber CMD_MDLSHARE_SET_PASSWORD
  323.     {
  324.     TaskInfo   *taskP = share_getTaskInfoP (NULL);
  325.  
  326.     /*    Set the password the user must type into the dialog box        */
  327.     if (!taskP)
  328.     return;
  329.  
  330.     strcpy (taskP->password, password);
  331.     share_printMsg (MSGID_TaskPasswordLS, taskP->taskId, password);
  332.     }
  333.  
  334. /*----------------------------------------------------------------------+
  335. |                                    |
  336. | name        share_setPasswordVarP                        |
  337. |                                    |
  338. | author    BSI                    05/95        |
  339. |                                    |
  340. +----------------------------------------------------------------------*/
  341. Public void    share_setPasswordVarP
  342. (
  343. char   *passwordP
  344. )
  345.     {
  346.     TaskInfo   *taskP = share_getTaskInfoP (NULL);
  347.  
  348.     /*    Set the password access string for the password text item   */
  349.     if (!taskP)
  350.     return;
  351.  
  352.     taskP->passwordP = passwordP;
  353.     share_printMsg (MSGID_PasswordVarSL, taskP->taskId, passwordP);
  354.     }
  355.  
  356. /*----------------------------------------------------------------------+
  357. |                                    |
  358. | name        share_allocTaskBlock                    |
  359. |                                    |
  360. | author    BSI                    05/95        |
  361. |                                    |
  362. +----------------------------------------------------------------------*/
  363. Private void   *share_allocTaskBlock
  364. (
  365. void   *parms
  366. )
  367.     {
  368.     void   *retVal;
  369.  
  370.     /*    Allocate a task information block                */
  371.     retVal = (void *)calloc (1, sizeof (TaskInfo));
  372.     share_printMsg (MSGID_AllocBlock, retVal);
  373.  
  374.     return retVal;
  375.     }
  376.  
  377. /*----------------------------------------------------------------------+
  378. |                                    |
  379. | name        share_clientTaskUnload                        |
  380. |                                    |
  381. | author    BSI                    05/95        |
  382. |                                    |
  383. +----------------------------------------------------------------------*/
  384. Private void    share_clientTaskUnload
  385. (
  386. void           *mdlDescP,
  387. long            exitReason,
  388. unsigned char  *taskIdP
  389. )
  390.     {
  391.     TaskInfo   *taskP = share_getTaskInfoP (mdlDescP);
  392.  
  393.     /*    Free task block for unloading MDL application            */
  394.     if (taskP)
  395.         {
  396.         share_printMsg (MSGID_UnloadTask, taskP->taskId, taskP);
  397.     if (taskP->prev)
  398.         taskP->prev->next = taskP->next;
  399.     if (taskP->next)
  400.         taskP->next->prev = taskP->prev;
  401.     if (taskListP == taskP)
  402.         taskListP = taskP->next;
  403.     free (taskP);
  404.     taskCount--;
  405.  
  406.     /*  If no more client tasks, unload the library            */
  407.     if (!taskCount)
  408.         {
  409.         taskListP = NULL;
  410.         }
  411.         }
  412.     }
  413.  
  414. /*----------------------------------------------------------------------+
  415. |                                    |
  416. | name        share_sharedLibraryUnload                |
  417. |                                    |
  418. | author    BSI                    05/95        |
  419. |                                    |
  420. +----------------------------------------------------------------------*/
  421. Private int    share_sharedLibraryUnload
  422. (
  423. void           *mdlDescP,
  424. long            exitReason,
  425. unsigned char  *taskIdP
  426. )
  427.     {
  428.     TaskInfo   *taskP;
  429.  
  430.     share_printMsg (MSGID_UnloadLibrary);
  431.  
  432.     /*    Free all client specific memory if still allocated        */
  433.     if (taskListP)
  434.     {
  435.     while (taskP = taskListP)
  436.         share_clientTaskUnload (taskP->mdlDescrP, 0L, taskP->taskId);
  437.     taskListP = NULL;
  438.     }
  439.     return SUCCESS;
  440.     }
  441.  
  442. /*----------------------------------------------------------------------+
  443. |                                    |
  444. | name        share_noMoreClientsHook                        |
  445. |                                    |
  446. | author    BSI                     5/95        |
  447. |                                    |
  448. +----------------------------------------------------------------------*/
  449. Private void     share_noMoreClients
  450. (
  451. void
  452. )
  453.     {
  454.     share_printMsg (MSGID_NoMoreClients);
  455.     mdlDialog_cmdNumberQueue (FALSE, CMD_MDL_SILENTUNLOAD, libTaskInfo.taskId,
  456.                   TRUE);
  457.     }
  458.  
  459. /*----------------------------------------------------------------------+
  460. |                                    |
  461. |   Hook definitions                            |
  462. |                                    |
  463. +----------------------------------------------------------------------*/
  464. DialogHookInfo    uHooks[] = { {HOOKITEMID_Password, share_passwordHook} };
  465.  
  466. /*----------------------------------------------------------------------+
  467. |                                    |
  468. | name        initialize                            |
  469. |                                    |
  470. | author    BSI                     5/95        |
  471. |                                    |
  472. +----------------------------------------------------------------------*/
  473. Public    int initialize
  474. (
  475. char           *fileNameP,      /* <= Client MDL Application            */
  476. char           *taskIdP,        /* <= Client MDL Task ID            */
  477. void           *libDescP,    /* <= Shared Library Descriptor pointer    */
  478. unsigned long    initParameter    /* <= Initialization parameters         */
  479. )
  480.     {
  481.     int        rscFileH;
  482.     TaskInfo   *taskInfoP, *taskP;
  483.  
  484.     /*    Initalize the environment for the client application:        */
  485.     /*        1)    Allocate a task block for the application in the    */
  486.     /*            shared library tasks memory space            */
  487.     /*        2)    If not initialized, set up header pointer for task  */
  488.     /*            list and set up the asynch function for handling    */
  489.     /*          all client tasks terminating, otherwise hook new    */
  490.     /*        block into list                        */
  491.     /*        3)    Initialize task specific data including access to   */
  492.     /*            shared library resources and access to the dialog   */
  493.     /*            hook functions via the local dialog box interface   */
  494.     share_printMsg (MSGID_Initialize, mdlSystem_getCurrTaskID ());
  495.  
  496.     /*    Allocate new task block in the shared library's memory        */
  497.     /*    space                                */
  498.     if (!(taskInfoP = (void *)mdlDialog_callFunction (libTaskInfo.mdlDescrP,
  499.                               share_allocTaskBlock,
  500.                               NULL)))
  501.     return -1;
  502.  
  503.     share_printMsg (MSGID_TaskBlockP, taskInfoP);
  504.  
  505.     /*    Increment client task count and connect new task block into */
  506.     /*    out task list.                            */
  507.     taskCount++;
  508.     if (!initialized)
  509.     {
  510.     share_printMsg (MSGID_InitLibrary);
  511.     taskListP = taskInfoP;
  512.     initialized++;
  513.  
  514.         /*  Set up asynch function to handle notification of all    */
  515.         /*  clients being unloaded                    */
  516.         mdlShare_setFunction (SHARED_LIB_NO_MORE_CLIENTS, libDescP,
  517.                   share_noMoreClients);
  518.     }
  519.     else
  520.         {
  521.     share_printMsg (MSGID_LinkTaskInfo);
  522.     taskP = taskListP;
  523.         while (taskP->next)
  524.         taskP = taskP->next;
  525.     taskP->next = taskInfoP;
  526.     taskInfoP->prev = taskP;
  527.         }
  528.  
  529.     /*    Initialize task specific information including access to    */
  530.     /*    the library's resources and dialog item hook function        */
  531.     strcpy (taskInfoP->taskId, taskIdP);
  532.     taskInfoP->mdlDescrP = mdlSystem_findMdlDesc (taskIdP);
  533.     taskInfoP->passwordP = &localPassword[0];
  534.     
  535.     /*
  536.     The fully qualified path name of this shared library application is
  537.     used to open the resource files which are linked into
  538.     this library.  These resources are available to any application
  539.     which uses this shared library.
  540.     */
  541.     mdlResource_openFile (&taskInfoP->rscFileH, fullPathName, RSC_READ);
  542.  
  543.     mdlDialog_hookPublish (sizeof(uHooks)/sizeof(DialogHookInfo), uHooks);
  544.  
  545.     return SUCCESS;
  546.     }
  547.  
  548. /*----------------------------------------------------------------------+
  549. |                                    |
  550. | name        main                                |
  551. |                                    |
  552. | author    BSI                     5/95        |
  553. |                                    |
  554. +----------------------------------------------------------------------*/
  555. Public void main
  556. (
  557. int   argc,        /* => number of args in next array  */
  558. char *argv[]        /* => array of cmd line arguments   */
  559. )
  560.     {
  561.     /* Save the fully qualified path name of the shared library
  562.     for use in mdlResource_openFile during initialize()
  563.     */
  564.     strcpy (fullPathName, argv[0]);
  565.  
  566.     /*    Open the resource file that we came out of            */
  567.     mdlResource_openFile (&libTaskInfo.rscFileH, NULL, RSC_READ);
  568.  
  569.     /*    Load the command table                        */
  570.     mdlParse_loadCommandTable (NULL);
  571.  
  572.     /*    Publish our hook functions                    */
  573.     mdlDialog_hookPublish (sizeof(uHooks)/sizeof(DialogHookInfo), uHooks);
  574.     strcpy (libTaskInfo.taskId, mdlSystem_getCurrTaskID ());
  575.  
  576.     libTaskInfo.mdlDescrP = mdlSystem_getCurrMdlDesc ();
  577.     strcpy (libTaskInfo.password, "mdlshare");
  578.     libTaskInfo.passwordP = &localPassword[0];
  579.  
  580.     /*    Init client application unload asynch function            */
  581.     mdlSystem_setFunction (SYSTEM_ALL_MDL_UNLOADS, share_clientTaskUnload);
  582.  
  583.     /*    Set up our own unload function                    */
  584.     mdlSystem_setFunction (SYSTEM_UNLOAD_PROGRAM,
  585.                    share_sharedLibraryUnload);
  586.     }
  587.