home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tkisrc04.zip / tcl / os2 / tclOS2Load.c < prev    next >
C/C++ Source or Header  |  1998-08-07  |  4KB  |  156 lines

  1. /* 
  2.  * tclOS2Load.c --
  3.  *
  4.  *    This procedure provides a version of the TclLoadFile that
  5.  *    works with the OS/2 "DosLoadModule" and "DosQueryProcAddr"
  6.  *    APIs for dynamic loading.
  7.  *
  8.  * Copyright (c) 1995 Sun Microsystems, Inc.
  9.  * Copyright (c) 1996-1997 Illya Vaes
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  */
  14.  
  15. #include "tclInt.h"
  16. #include "tclPort.h"
  17.  
  18.  
  19. /*
  20.  *----------------------------------------------------------------------
  21.  *
  22.  * TclLoadFile --
  23.  *
  24.  *    Dynamically loads a binary code file into memory and returns
  25.  *    the addresses of two procedures within that file, if they
  26.  *    are defined.
  27.  *
  28.  * Results:
  29.  *    A standard Tcl completion code.  If an error occurs, an error
  30.  *    message is left in interp->result.
  31.  *
  32.  * Side effects:
  33.  *    New code suddenly appears in memory.
  34.  *
  35.  *----------------------------------------------------------------------
  36.  */
  37.  
  38. int
  39. TclLoadFile(interp, fileName, sym1, sym2, proc1Ptr, proc2Ptr)
  40.     Tcl_Interp *interp;        /* Used for error reporting. */
  41.     char *fileName;        /* Name of the file containing the desired
  42.                  * code. */
  43.     char *sym1, *sym2;        /* Names of two procedures to look up in
  44.                  * the file's symbol table. */
  45.     Tcl_PackageInitProc **proc1Ptr, **proc2Ptr;
  46.                 /* Where to return the addresses corresponding
  47.                  * to sym1 and sym2. */
  48. {
  49.     HMODULE handle;
  50.     APIRET rc;
  51.     char *buffer;
  52.  
  53. #ifdef DEBUG
  54.     printf("TclLoadFile %s %s %s\n", fileName, sym1, sym2);
  55. #endif
  56.     handle = TclOS2LoadLibrary(fileName);
  57.     if (handle == NULLHANDLE) {
  58.     Tcl_AppendResult(interp, "couldn't load file \"", fileName,
  59.         "\": ", "file not found", (char *) NULL);
  60.     return TCL_ERROR;
  61.     }
  62.  
  63.     /*
  64.      * For each symbol, check for both Symbol and _Symbol, since some
  65.      * compilers generate C symbols with a leading '_' by default.
  66.      */
  67.  
  68.     rc = DosQueryProcAddr(handle, 0L, sym1, (PFN *)proc1Ptr);
  69.     if (rc != NO_ERROR) {
  70. #ifdef DEBUG
  71.         printf("DosQueryProcAddr %s ERROR %d\n", sym1, rc);
  72. #endif
  73.     buffer = ckalloc(strlen(sym1)+2);
  74.     buffer[0] = '_';
  75.     strcpy(buffer+1, sym1);
  76.         rc = DosQueryProcAddr(handle, 0L, buffer, (PFN *)proc1Ptr);
  77.     if (rc != NO_ERROR) {
  78. #ifdef DEBUG
  79.             printf("DosQueryProcAddr %s ERROR %d\n", buffer, rc);
  80. #endif
  81.         *proc1Ptr = NULL;
  82.     }
  83. #ifdef DEBUG
  84.           else {
  85.             printf("DosQueryProcAddr %s OK\n", buffer, rc);
  86.         }
  87. #endif
  88.     ckfree(buffer);
  89.     }
  90. #ifdef DEBUG
  91.       else {
  92.         printf("DosQueryProcAddr %s OK\n", sym1, rc);
  93.     }
  94. #endif
  95.     
  96.     rc = DosQueryProcAddr(handle, 0L, sym2, (PFN *)proc2Ptr);
  97.     if (rc != NO_ERROR) {
  98. #ifdef DEBUG
  99.         printf("DosQueryProcAddr %s ERROR %d\n", sym2, rc);
  100. #endif
  101.     buffer = ckalloc(strlen(sym2)+2);
  102.     buffer[0] = '_';
  103.     strcpy(buffer+1, sym2);
  104.         rc = DosQueryProcAddr(handle, 0L, buffer, (PFN *)proc2Ptr);
  105.     if (rc != NO_ERROR) {
  106. #ifdef DEBUG
  107.             printf("DosQueryProcAddr %s ERROR %d\n", buffer, rc);
  108. #endif
  109.         *proc2Ptr = NULL;
  110.     }
  111. #ifdef DEBUG
  112.           else {
  113.             printf("DosQueryProcAddr %s OK\n", buffer, rc);
  114.         }
  115. #endif
  116.     ckfree(buffer);
  117.     }
  118. #ifdef DEBUG
  119.       else {
  120.         printf("DosQueryProcAddr %s OK\n", sym2, rc);
  121.     }
  122. #endif
  123.     
  124.     return TCL_OK;
  125. }
  126.  
  127. /*
  128.  *----------------------------------------------------------------------
  129.  *
  130.  * TclGuessPackageName --
  131.  *
  132.  *      If the "load" command is invoked without providing a package
  133.  *      name, this procedure is invoked to try to figure it out.
  134.  *
  135.  * Results:
  136.  *      Always returns 0 to indicate that we couldn't figure out a
  137.  *      package name;  generic code will then try to guess the package
  138.  *      from the file name.  A return value of 1 would have meant that
  139.  *      we figured out the package name and put it in bufPtr.
  140.  *
  141.  * Side effects:
  142.  *      None.
  143.  *
  144.  *----------------------------------------------------------------------
  145.  */
  146.  
  147. int
  148. TclGuessPackageName(fileName, bufPtr)
  149.     char *fileName;             /* Name of file containing package (already
  150.                                  * translated to local form if needed). */
  151.     Tcl_DString *bufPtr;        /* Initialized empty dstring.  Append
  152.                                  * package name to this if possible. */
  153. {
  154.     return 0;
  155. }
  156.