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