home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c034 / 4.ddi / SOURCE / INCLUDE.C$ / INCLUDE.bin
Encoding:
Text File  |  1990-01-02  |  4.7 KB  |  165 lines

  1. /* INCLUDE.C - A PWB extension to automatically open include files.
  2.  *
  3.  * To compile for DOS or OS/2:
  4.  *
  5.  *   cl /c /Lr /Gs /ACw include.c
  6.  *
  7.  * To link for DOS:
  8.  *
  9.  *   link /NOI exthdr include,include.mxt;
  10.  *
  11.  * To link for OS/2:
  12.  *
  13.  *   link /NOI exthdrp include,include.pxt,,,ext.def;
  14.  *
  15.  * To load, put the following command in your TOOLS.INI (or execute with
  16.  * the assign command):
  17.  *
  18.  *   load:[d:\path\]include
  19.  *
  20.  * To use:
  21.  *
  22.  *   - Place cursor on an include line and select function with
  23.  *     key (default SHIFT+CTRL+I) or item from View menu. Function will
  24.  *     recognize any valid C, Assembler, FORTRAN, BASIC, or Microsoft
  25.  *     Pascal Compiler (but not QuickPascal) include line and open the
  26.  *     specified include file.
  27.  *
  28.  *   - Place cursor on non-include line and select function. You will
  29.  *     be prompted for an include file.
  30.  *
  31.  *   - Type "ALT+A filename SHIFT+CTRL+I" to open a specific file.
  32.  *
  33.  * The function searches in the current directory and in the INCLUDE
  34.  * directory to find the file.
  35.  */
  36.  
  37. #include <string.h>
  38. #include <ext.h>
  39.  
  40. /* Function prototypes. */
  41. PWBFUNC Include( unsigned argData, ARG _far *pArg, flagType fMeta );
  42. void searchenv( char *pchName, char *pchEnviron, char *pchPathBuf );
  43. int  Exist( char _far *achName );
  44. void pascal EXPORT Dummy( char *ach );
  45.  
  46. PWBFUNC Include( unsigned argData, ARG _far *pArg, flagType fMeta )
  47. {
  48.     LINE    y;
  49.     PFILE   pFile;
  50.     static  char    achSpec[BUFLEN], achName[BUFLEN] = "";
  51.     static  char    szDelimit[] = " \t\"'<>:()";    // Any language
  52.     char    *pchToken;
  53.  
  54.     if( pArg->argType == TEXTARG )
  55.         strcpy( achName, pArg->arg.textarg.pText );
  56.     else
  57.     {
  58.         /* Get current file handle and current line. */
  59.         pFile = FileNameToHandle( "", "" );
  60.         y = pArg->arg.noarg.y;
  61.         GetLine( y, achSpec, pFile );
  62.  
  63.         /* Make uppercase copy and check to see if it is an include line. */
  64.         strupr( achSpec );
  65.         if( !(pchToken = strstr( achSpec, "INCLUDE" )) )
  66.         {
  67.             /* Not an include line, so prompt for file name. */
  68.             if( GetString( achName, "Enter include file name:", TRUE ) )
  69.                 return TRUE;
  70.         }
  71.         else
  72.         {
  73.             /* Next token after INCLUDE should be file name. */
  74.             pchToken = strtok( pchToken, szDelimit );
  75.             pchToken = strtok( NULL, szDelimit );
  76.             strcpy( achName, pchToken );
  77.         }
  78.     }
  79.     strcpy( achSpec, achName );
  80.  
  81.     /* Set file name if it exists in current or include directory. */
  82.     if( !Exist( achName ) )
  83.     {
  84.         searchenv( achName, "INCLUDE", achSpec );
  85.         if( achSpec[0] == '\0' )
  86.         {
  87.             strcat( strcpy( achName, "Can't find include file: " ) , achSpec );
  88.             DoMessage( achName );
  89.             return TRUE;
  90.         }
  91.     }
  92.     DoMessage( achName );
  93.  
  94.     /* Open file and make it the current file. */
  95.     if( (pFile = FileNameToHandle( achSpec, NULL )) == PNULL )
  96.     {
  97.         pFile = AddFile( achSpec );
  98.         FileRead( achSpec, pFile );
  99.     }
  100.     pFileToTop( pFile );
  101.     Display();
  102.     return TRUE;
  103. }
  104.  
  105. /* PWB replacement for C _searchenv */
  106. void searchenv( char *pchName, char *pchEnviron, char *pchPathBuf )
  107. {
  108.     char achT[BUFLEN], *pchToken;
  109.     int  result;
  110.  
  111.     /* Get the environment string and loop through it, checking
  112.      * each directory to see if the file exists in it.
  113.      */
  114.     strcpy( achT, mgetenv( pchEnviron ) );
  115.     pchToken = strtok( achT, ";");
  116.     do
  117.     {
  118.         strcpy( pchPathBuf, pchToken );
  119.         strcat( strcat( pchPathBuf, "\\" ), pchName );
  120.         result = Exist( pchPathBuf );
  121.         pchToken = strtok( NULL, ";" );
  122.     } while( (!result) && (pchToken != NULL) );
  123.  
  124.     if( !result )
  125.         pchPathBuf[0] = '\0';
  126. }
  127.  
  128. /* Testing for existence of a file must be done indirectly by using
  129.  * ForFile, which returns an error for a file that doesn't exist.
  130.  */
  131. int Exist( char _far *achName )
  132. {
  133.     return ForFile( achName, FORFILE_ALL, Dummy );
  134. }
  135.  
  136. /* ForFile must call something, thus this dummy function. */
  137. void pascal EXPORT Dummy( char *ach )
  138. {
  139. }
  140.  
  141. /* Initialize command key. */
  142. void EXTERNAL WhenLoaded()
  143. {
  144.     /* Add separator bar and Include item to View menu. */
  145.     AddMenuItem( MID_VIEW, "-", "", "", "" );
  146.     AddMenuItem( MID_VIEW, "Include", "Include File",
  147.                                       "", "include" );
  148.     SetKey( "include", "shift+ctrl+i" );
  149.     DoMessage( "Loaded INCLUDE" );
  150.     return;
  151. }
  152.  
  153. /* No new switches. */
  154. struct swiDesc swiTable[]=
  155. {
  156.     { NULL,     NULL,   0 }
  157. };
  158.  
  159. /* New command: include */
  160. struct cmdDesc cmdTable[] =
  161. {
  162.     { "include", Include, 0, NOARG | TEXTARG | NULLEOW | BOXSTR },
  163.     { NULL,      NULL,    0, 0 }
  164. };
  165.