home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / toolmana.lha / ToolManager / Source / library / LibInit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-26  |  8.3 KB  |  292 lines

  1. /*
  2.  * LibInit.c  V2.0
  3.  *
  4.  * shared library C stub
  5.  *
  6.  * (c) 1990-1992 Stefan Becker
  7.  */
  8.  
  9. #include "ToolManagerLib.h"
  10.  
  11. /* library name & id string */
  12. #define INTTOSTR(a) #a
  13. const char LibName[]=TMLIBNAME;
  14. const char LibId[]="$VER: " TMLIBNAME " " INTTOSTR(TMLIBVERSION) "."
  15.                    INTTOSTR(TMLIBREVISION) " (" TMLIBDATE ")\r\n";
  16.  
  17. /* prototypes for library management functions */
  18. __geta4 static struct Library *LibOpen(__A6 struct Library *, __D0 ULONG);
  19. __geta4 static BPTR            LibClose(__A6 struct Library *);
  20. __geta4 static BPTR            LibExpunge(__A6 struct Library *);
  21.         static ULONG           LibReserved(void);
  22. __geta4 static void            QuitToolManager(void);
  23. __geta4 struct TMHandle       *AllocTMHandle(void);
  24. __geta4 static void            FreeTMHandle(__A0 struct TMHandle *);
  25. __geta4 static BOOL            CreateTMObjectTagList(__A0 struct TMHandle *,
  26.                                                      __A1 char *, __D0 ULONG,
  27.                                                      __A2 struct TagItem *);
  28. __geta4 static BOOL            DeleteTMObject(__A0 struct TMHandle *,
  29.                                               __A1 char *);
  30. __geta4 static BOOL            ChangeTMObjectTagList(__A0 struct TMHandle *,
  31.                                                      __A1 char *,
  32.                                                      __A2 struct TagItem *);
  33.  
  34. /* library functions table */
  35. static const APTR LibVectors[]={
  36.                                 /* Standard functions */
  37.                                 (APTR) LibOpen,
  38.                                 (APTR) LibClose,
  39.                                 (APTR) LibExpunge,
  40.                                 (APTR) NULL,
  41.  
  42.                                 /* Library specific functions */
  43.                                 (APTR) LibReserved, /* reserved for ARexx */
  44.                                 (APTR) QuitToolManager,
  45.                                 (APTR) AllocTMHandle,
  46.                                 (APTR) FreeTMHandle,
  47.                                 (APTR) CreateTMObjectTagList,
  48.                                 (APTR) DeleteTMObject,
  49.                                 (APTR) ChangeTMObjectTagList,
  50.  
  51.                                 /* Table end */
  52.                                 (APTR) -1
  53.                                 };
  54.  
  55. /* misc. data */
  56. static BPTR LibSegment=NULL;
  57. static struct Library *PrivateDOSBase=NULL;
  58. static struct Task *HandlerTask;
  59. struct Library *SysBase=NULL;
  60. struct Library *LibBase=NULL;
  61. BOOL Closing=FALSE;
  62. const char DosName[]="dos.library";
  63.  
  64. /* library init routine */
  65. __geta4 struct Library *LibInit(__A0 BPTR LibSegList)
  66. {
  67.  struct Library *MyLib;
  68.  
  69.  /* Get ExecBase */
  70.  SysBase=*(struct Library **) 4; /* AbsExecBase */
  71.  
  72.  /* Open dos.library */
  73.  if (!(PrivateDOSBase=OpenLibrary(DosName,37))) return(0);
  74.  
  75.  LibSegment=LibSegList;
  76.  
  77.  if (!(LibBase=MyLib=MakeLibrary(LibVectors, NULL, NULL,
  78.                                  sizeof(struct Library), NULL))) {
  79.   CloseLibrary(PrivateDOSBase);
  80.   return(0);
  81.  }
  82.  
  83.  MyLib->lib_Node.ln_Type=NT_LIBRARY;
  84.  MyLib->lib_Node.ln_Name=LibName;
  85.  MyLib->lib_Flags=LIBF_CHANGED|LIBF_SUMUSED;
  86.  MyLib->lib_Version=TMLIBVERSION;
  87.  MyLib->lib_Revision=TMLIBREVISION;
  88.  MyLib->lib_IdString=(APTR) LibId;
  89.  AddLibrary(MyLib);
  90.  
  91.  DEBUG_PRINTF("Init Lib: %08lx ",MyLib);
  92.  DEBUG_PRINTF("Seg: %08lx\n",LibSegment);
  93.  
  94.  return(MyLib);
  95. }
  96.  
  97. /* shared library open function */
  98. __geta4 static struct Library *LibOpen(__A6 struct Library *lib,
  99.                                        __D0 ULONG version)
  100. {
  101.  struct Task *StartHandler(__A6 struct Library *dosbase);
  102.  
  103.  /* Handle special case: OpenCnt=0 & Handler is just closing down */
  104.  if ((lib->lib_OpenCnt == 0) && Closing) return(NULL);
  105.  
  106.  /* Handler active? Try to start it... */
  107.  if (!LibraryPort && !(HandlerTask=StartHandler(PrivateDOSBase)))
  108.   return(NULL);
  109.  
  110.  /* Oh another user :-) */
  111.  lib->lib_OpenCnt++;
  112.  
  113.  /* Reset delayed expunge flag */
  114.  lib->lib_Flags&=~LIBF_DELEXP;
  115.  
  116.  /* Return library pointer */
  117.  DEBUG_PRINTF("Open Lib: %ld\n",lib->lib_OpenCnt);
  118.  return(lib);
  119. }
  120.  
  121. /* shared library close function */
  122. __geta4 static BPTR LibClose(__A6 struct Library *lib)
  123. {
  124.  /* Open count already zero or more than one user? */
  125.  if ((lib->lib_OpenCnt == 0) || (--lib->lib_OpenCnt > 0)) return(NULL);
  126.  
  127.  /* Is handler active? Yes, send him a signal if he should shut down */
  128.  if (LibraryPort && Closing) Signal(HandlerTask,SIGBREAKF_CTRL_F);
  129.  
  130.  /* Is the delayed expunge bit set?  Yes, try to remove the library */
  131.  if (lib->lib_Flags & LIBF_DELEXP) return(LibExpunge(lib));
  132.  
  133.  /* No. Don't remove library now */
  134.  return(NULL);
  135. }
  136.  
  137. /* shared library expunge function */
  138. __geta4 static BPTR LibExpunge(__A6 struct Library *lib)
  139. {
  140.  DEBUG_PRINTF("Expunge Lib: %08lx ",lib);
  141.  DEBUG_PRINTF("Seg: %08lx\n",LibSegment);
  142.  
  143.  /* Does no-one use library now or is handler active/closing down?? */
  144.  if ((lib->lib_OpenCnt > 0) || LibraryPort || Closing) {
  145.   /* No, library still in use -> set delayed expunge flag */
  146.   lib->lib_Flags|=LIBF_DELEXP;
  147.   return(NULL);
  148.  }
  149.  
  150.  /* Yes, remove library and free resources */
  151.  Remove(&lib->lib_Node);
  152.  FreeMem((void *) ((ULONG) lib-lib->lib_NegSize),
  153.          lib->lib_NegSize+lib->lib_PosSize);
  154.  if (PrivateDOSBase) {
  155.   CloseLibrary(PrivateDOSBase);
  156.   PrivateDOSBase=NULL;
  157.  }
  158.  
  159.  /* return BPTR to our seglist */
  160.  DEBUG_PUTSTR("Removing library...\n");
  161.  return(LibSegment);
  162. }
  163.  
  164. /* Reserved function, returns NULL */
  165. static ULONG LibReserved(void)
  166. {
  167.  return(NULL);
  168. }
  169.  
  170. /* Set quit flag for handler process */
  171. __geta4 static void QuitToolManager(void)
  172. {
  173.  /* Set flag */
  174.  if (LibraryPort && !Closing) Closing=TRUE;
  175. }
  176.  
  177. /* Send IPC message */
  178. static BOOL SendIPC(struct TMHandle *handle)
  179. {
  180.  /* Handler ready? */
  181.  if (LibraryPort) {
  182.   /* Yep, send message */
  183.   PutMsg(LibraryPort,(struct Message *) handle);
  184.  
  185.   /* Wait on reply */
  186.   WaitPort(handle->tmh_Msg.mn_ReplyPort);
  187.  
  188.   /* Get reply */
  189.   GetMsg(handle->tmh_Msg.mn_ReplyPort);
  190.  
  191.   /* get return code */
  192.   return(handle->tmh_Command);
  193.  }
  194.  
  195.  /* Oops nobody listening :-( */
  196.  return(FALSE);
  197. }
  198.  
  199. /* Allocate a TMHandle */
  200. __geta4 void *AllocTMHandle(void)
  201. {
  202.  struct TMHandle *handle;
  203.  
  204.  DEBUG_PRINTF("AllocTMHandle() called.\n");
  205.  
  206.  /* Allocate memory for handle structure */
  207.  if (handle=AllocMem(sizeof(struct TMHandle),MEMF_PUBLIC)) {
  208.   struct MsgPort *rp;
  209.  
  210.   /* Create IPC Port */
  211.   if (rp=CreateMsgPort()) {
  212.    /* Init message */
  213.    handle->tmh_Msg.mn_ReplyPort=rp;
  214.    handle->tmh_Msg.mn_Length=sizeof(struct TMHandle);
  215.  
  216.    /* Send command to handler */
  217.    handle->tmh_Command=TMIPC_AllocTMHandle;
  218.    if (SendIPC(handle)) return(handle); /* All OK. */
  219.  
  220.    /* Something went wrong */
  221.    DeleteMsgPort(handle->tmh_Msg.mn_ReplyPort);
  222.   }
  223.   FreeMem(handle,sizeof(struct TMHandle));
  224.  }
  225.  
  226.  /* call failed */
  227.  DEBUG_PRINTF("AllocTMHandle() failed.\n");
  228.  return(NULL);
  229. }
  230.  
  231. __geta4 static void FreeTMHandle(__A0 struct TMHandle *handle)
  232. {
  233.  /* Send command to handler */
  234.  handle->tmh_Command=TMIPC_FreeTMHandle;
  235.  SendIPC(handle);
  236.  
  237.  /* Free handle */
  238.  DeleteMsgPort(handle->tmh_Msg.mn_ReplyPort);
  239.  FreeMem(handle,sizeof(struct TMHandle));
  240. }
  241.  
  242. __geta4 BOOL CreateTMObjectTagList(__A0 struct TMHandle *handle,
  243.                                    __A1 char *object,
  244.                                    __D0 ULONG type,
  245.                                    __A2 struct TagItem *tags)
  246. {
  247.  /* Sanity checks */
  248.  if ((handle==NULL) || (object==NULL) || (type>=TMOBJTYPES))
  249.   return(FALSE); /* Bad arguments! */
  250.  
  251.  /* Build IPC command */
  252.  handle->tmh_Command=TMIPC_CreateTMObject;
  253.  handle->tmh_Type=type;
  254.  handle->tmh_Object=object;
  255.  handle->tmh_Tags=tags;
  256.  
  257.  /* Send command to handler */
  258.  return(SendIPC(handle));
  259. }
  260.  
  261. /* Delete a TMObject (shared library version) */
  262. __geta4 BOOL DeleteTMObject(__A0 struct TMHandle *handle, __A1 char *object)
  263. {
  264.  /* Sanity checks */
  265.  if ((handle==NULL) || (object==NULL)) return(FALSE); /* Bad arguments! */
  266.  
  267.  /* Build IPC command */
  268.  handle->tmh_Command=TMIPC_DeleteTMObject;
  269.  handle->tmh_Object=object;
  270.  
  271.  /* Send command to handler */
  272.  return(SendIPC(handle));
  273. }
  274.  
  275. /* Change a TMObject (shared library version) */
  276. __geta4 BOOL ChangeTMObjectTagList(__A0 struct TMHandle *handle,
  277.                                    __A1 char *object,
  278.                                    __A2 struct TagItem *tags)
  279. {
  280.  /* Sanity checks */
  281.  if ((handle==NULL) || (object==NULL)) return(FALSE); /* Bad arguments! */
  282.  
  283.  /* Build IPC command */
  284.  handle->tmh_Command=TMIPC_ChangeTMObject;
  285.  handle->tmh_Object=object;
  286.  handle->tmh_Tags=tags;
  287.  
  288.  /* Send command to handler */
  289.  return(SendIPC(handle));
  290. }
  291.  
  292.