home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / autoload.cmm < prev    next >
Encoding:
Text File  |  1995-10-11  |  13.9 KB  |  486 lines

  1. /*
  2.  * Autoload.cmm
  3.  *
  4.  * This file handles most of the shell interface of CEnvi. The rest of the
  5.  * interface is included as the file "ShellChr.cmm". This version will run
  6.  * on all CEnvi systems known as of the revision date.
  7.  *
  8.  */
  9.  
  10.  
  11.  
  12. if( !defined(_NWNLM_) && !defined(_DOS_) && !defined(_DOS32_) && !defined(_OS2_) &&
  13.     !defined(_WINDOWS_) && !defined(_NTCON_) && !defined(_NTWIN_) )
  14. {
  15.   printf("\nThe system you are running CEnvi on is not supported by this\n"
  16.          "version of AUTOLOAD.CMM. A newer version should have been included\n"
  17.          "in your distribution. You should use that instead.\n");
  18.   exit(1);
  19. }
  20.  
  21. if( !defined(_SHELL_) )
  22. {
  23.   printf("Autoload.cmm is an internal shell implementation file and cannot be\n");
  24.   printf("run as a program.\n");
  25.   exit(EXIT_FAILURE);
  26. }
  27.  
  28. /* ---------------------------------------------------------------------- */
  29.  
  30. // First we have a list of commands that are to be passed on directly to
  31. // the shell. Windows and Netware have no such list of commands.
  32. if( defined(_DOS_) || defined(_DOS32_) )
  33. {
  34.   SystemCommands =
  35.     {
  36.       "DIR",      "COPY",     "MKDIR",    "MD",       "RMDIR",
  37.       "RD",       "ERASE",    "DEL",      "TYPE",     "ECHO"
  38.     };
  39. }
  40. else if( defined(_OS2_) )
  41. {
  42.   SystemCommands =
  43.     {
  44.       "DIR",      "MKDIR",    "MD",       "RMDIR",    "RD",
  45.       "ERASE",    "DEL",      "TYPE",     "START",    "DETACH",
  46.       "COPY",     "ECHO"
  47.     };
  48. }
  49. else if( defined(_NTCON_) )
  50. {
  51.   SystemCommands =
  52.     {
  53.       "DIR",      "COPY",     "MKDIR",    "MD",       "RMDIR",
  54.       "RD",       "ERASE",    "DEL",      "TYPE",     "ECHO",
  55.       "START"
  56.     };
  57. }
  58. if( defined(SystemCommands) ) qsort(SystemCommands,"stricmp");
  59.  
  60.  
  61. // Some Basic Aliases
  62. alias_list = {
  63.   { "md",      "makedir" },
  64.   { "mkdir",   "makedir" },
  65.   { "clear",   "cls" },
  66.   { "erase",   "del" },
  67.   { "rm",      "del" },
  68.   { "cp",      "copy" },
  69.   { "mv",      "move" },
  70.   { "ren",     "move" },
  71.   { "rename",  "move" },
  72.   { "chmod",   "attrib" },
  73.   { "rd",      "rmdir" },
  74.   { "load",    "start" }
  75. };
  76.  
  77. if ( defined(_DOS_) || defined(_DOS32_) ) {
  78.    // spawning can leave too-little memory, so increase COMSPEC size
  79.    if ( !defined(COMSPEC_ENV_SIZE) )
  80.       COMSPEC_ENV_SIZE = 2000;
  81. }
  82.  
  83. ///////////////////////////////////////////////////////////////////////
  84.  
  85.  
  86. #include "CmdLine.lib"
  87.  
  88. // The other systems all have this defined. Netware has no Environment variables
  89. // so we define it here.
  90.  
  91. if( defined(_NWNLM_) ) PROMPT = NULL;
  92.  
  93. if ( !defined(_WINDOWS_) && !defined(_NTWIN_)
  94.   && defined(PROMPT) && strnicmp("[CEnvi]",PROMPT,7) )
  95.   {
  96.     strcpy(oldprompt,PROMPT);
  97.     sprintf(PROMPT,"[CEnvi] %s",PROMPT);
  98.     getenv();
  99.   }
  100.  
  101. /*
  102.  * Change the current directory to the given one. Return 0 if successful.
  103.  */
  104. my_chdir(newdir)
  105. {
  106.   if( defined(_NWNLM_) ) return chdir(newdir);
  107.   if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
  108.     {
  109.       lReg.ah = 0x3B;
  110.       if ( !defined(_DOS32_) )
  111.          lReg.ds = segment(newdir), lReg.dx = offset(newdir);
  112.       else
  113.          lReg.dx = pointer(newdir);
  114.       interrupt(0x21,lReg,out);
  115.       return out.ax==3;
  116.     }
  117.   if( defined(_NTCON_) || defined(_NTWIN_) )
  118.     {
  119.       return !DynamicLink("KERNEL32","SetCurrentDirectoryA",STDCALL,newdir);
  120.     }
  121.   if( defined(_OS2_) )
  122.     {
  123. #define ORD_DOS32SETCURRENTDIR   255
  124.       return DynamicLink("doscalls",ORD_DOS32SETCURRENTDIR,BIT32,CDECL,newdir);
  125.     }
  126. }
  127.  
  128. //**************************************************************************
  129. //****** BEGIN SECTION TO HANDLE INTERNAL COMMANDS FOR THE CLI SHELL *******
  130. //**************************************************************************
  131.  
  132.  
  133. // Next is a list of all commands handled internally to the shell. This list
  134. // must remain sorted. Also, the InternalHelp array must match. It is fortunate
  135. // that currently all the 'extra' commands only defined on certain systems
  136. // fall later than any other command in the alphabet.
  137.  
  138. InternalCommands = {
  139.   "ALIAS", "CD", "CHDIR", "CLS", "ECHO", "HELP", "PROMPT", "PWD"
  140. };
  141. InternalHelp = {
  142.   "The ALIAS command lets you view or define aliases. ALIAS by itself will list\n"
  143.   "all aliases currently defined. Otherwise define an alias by using the format:\n"
  144.   "\n  ALIAS name=text string",
  145.   "CD used by itself will print the current directory. If you give it a\ndirectory name, it will change the current directory to it.",
  146.   "CHDIR changes the current directory to the given directory.",
  147.   "CLS clears the screen.", 
  148.   "ECHO prints some text to the screen.",
  149.   "HELP by itself prints all known commands, otherwise it prints help on a command.",
  150.   "PROMPT will change the prompt. By itself, it resets the prompt to the default.",
  151.   "PWD will print the current working directory.",
  152. };
  153.  
  154. if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) || defined(_NTCON_) ||
  155.     defined(_NTWIN_) || defined(_OS2_) )
  156. {
  157.   InternalCommands[GetArraySpan(InternalCommands)+1] = "SET";
  158.   InternalHelp[GetArraySpan(InternalHelp)+1] = 
  159.     "SET allows you to view or change environment variables. SET by itself will\n"
  160.     "list all such variables currently defined. Otherwise define one by using the\n"
  161.     "format:\n\n"
  162.     "  SET variable=value";
  163. }
  164.  
  165. if( defined(_WINDOWS_) || defined(_NTWIN_) || defined(_NWNLM_) )
  166. {
  167.   InternalCommands[GetArraySpan(InternalCommands)+1] = "START";
  168.   InternalCommands[GetArraySpan(InternalCommands)+1] = "TYPE";
  169.   InternalHelp[GetArraySpan(InternalHelp)+1] = 
  170.     "START starts an Executable or NLM running asynchronously.";
  171.   InternalHelp[GetArraySpan(InternalHelp)+1] = 
  172.     "TYPE will echo the contents of a file to the screen.";
  173. }
  174.  
  175. // set up filter to handle each command input
  176. ShellFilterCommand("AutoloadFilterCommand");
  177.  
  178. AutoloadFilterCommand(pCommand)
  179. {
  180.    if ( !stricmp(pCommand,"EXIT") )
  181.      {
  182.        if( defined(oldprompt) ) PROMPT = oldprompt;
  183.      } else {
  184.        strcpy(tmp,pCommand);
  185.        s = strchr(tmp,' ');
  186.        if( s ) s[0] = '\0';
  187.  
  188.        if ( !stricmp(SplitFileName(tmp).name,"autoload") )
  189.          {
  190.            printf("Autoload.cmm is an internal shell implementation file and cannot be\n");
  191.            printf("run as a program.\n");
  192.            pCommand[0] = '\0';
  193.          } else {
  194.            strcpy(command,pCommand); pCommand[0] = '\0';
  195.            ProcessCommandLine(command,"AutoloadFilterExecutor");
  196.          }
  197.      }
  198. }
  199.  
  200. AutoloadFilterExecutor(pCommand)
  201. {
  202.   lResult = EXIT_SUCCESS;
  203.   undefine(alias);
  204.   alias = "";
  205.   for( i=0;pCommand[i];i++ )
  206.     {
  207.       if( isspace(pCommand[i]) ) break;
  208.       alias[i] = pCommand[i];
  209.     }
  210.   the_rest = "";
  211.   strcpy(the_rest,pCommand+i);
  212.   alias[i] = 0;
  213.   for( j=0;j<=GetArraySpan(alias_list);j++ )
  214.     {
  215.       if( !stricmp(alias,alias_list[j][0]) )
  216.         {
  217.           pCommand = "";
  218.           strcpy(pCommand,alias_list[j][1]);
  219.           strcat(pCommand,the_rest);
  220.           break;
  221.         }
  222.     }
  223.  
  224.    // if line starts with a quote, it is cmm code to execute immediately.
  225.    if ( strchr("\"\'`",pCommand[0]) ) {
  226.       if( pCommand[strlen(pCommand)-1]==pCommand[0] )
  227.         pCommand[strlen(pCommand)-1] = 0;
  228.       lResult = interpret(pCommand+1,INTERP_TEXT|INTERP_NOINHERIT_GLOBAL|
  229.                                      INTERP_NOINHERIT_LOCAL);
  230.       pCommand[0] = 0;
  231.    }
  232.    // if command found in SystemCommands list then execute it immediately
  233.    // with CMD.EXE then, else check internal cmds and some others
  234.    else if ( 1 == sscanf(pCommand,"%s",lCommand) ) {
  235.       if( defined(SystemCommands) && bsearch(lCommand,SystemCommands,"stricmp") )
  236.         {
  237.           strcpy(command,pCommand); pCommand[0] = '\0'
  238.           if( defined(_OS2_) || defined(_NTCON_) || defined(_NTWIN_) )
  239.             {
  240.               lResult = spawn(P_WAIT,"CMD.EXE","/C",command);
  241.             }
  242.           else if( defined(_DOS_) || defined(_DOS32_) )
  243.             {
  244.               lResult = system(P_SWAP,command);
  245.             }
  246.         } else if ( bsearch(lCommand,InternalCommands,"stricmp") ) {
  247.           sprintf(lInternalFunction,"InternalCommand_%s",strupr(lCommand));
  248.           lParm = pCommand + strlen(lCommand);
  249.           while( isspace(lParm[0]) ) lParm++;
  250.           lResult = function(lInternalFunction,lParm);
  251.           pCommand[0] = '\0';
  252.         } else if ( !strcmp(lCommand+1,":") ) {
  253.           // change drive
  254.           if( defined(_OS2_) )
  255.             {
  256.               #define ORD_DOS32SETDEFAULTDISK  220
  257.               DynamicLink("doscalls",ORD_DOS32SETDEFAULTDISK,BIT32,CDECL,
  258.                           toupper(lCommand[0])-'A'+1);
  259.               lResult = EXIT_SUCCESS;
  260.             }
  261.           if( defined(_NTWIN_) || defined(_NTCON_) )
  262.             {
  263.               lResult = InternalCommand_CD(lCommand);
  264.             }
  265.           if( defined(_WINDOWS_) || defined(_DOS_) || defined(_DOS32_) )
  266.             {
  267.               lReg.ah = 0x0E;
  268.               lReg.dl = toupper(lCommand[0]) - 'A';
  269.               interrupt(0x21,lReg);
  270.               lResult = EXIT_SUCCESS;
  271.             }
  272.           pCommand[0] = '\0';
  273.         }
  274.    }
  275.    if ( pCommand[0] )
  276.      {
  277.        // no one else handled this command, so pass on to SPAWN
  278.        if( defined(_DOS_) )
  279.          {
  280.            lResult = spawn(P_SWAP,pCommand);
  281.          }
  282.        else
  283.          {
  284.            lResult = spawn(P_WAIT,pCommand);
  285.          }
  286.      }
  287.        if( lResult==-1 && pCommand[0] )
  288.          {
  289.            if( s = strchr(pCommand,' ') ) s[0] = '\0';
  290.            filestruct = SplitFileName(pCommand);
  291.            sprintf(badcommand,"%s%s",filestruct.name,filestruct.ext);
  292.            printf("Unable to find program \"%s\" as an executable, batch, or cmm program\n"
  293.                   "anywhere in your path.\n",strupr(badcommand));
  294.          }
  295.  
  296.    return lResult;
  297. }
  298.  
  299. /* ---------------------------------------------------------------------- */
  300.  
  301. // We have a number of internal commands.
  302.  
  303. InternalCommand_ALIAS(param)
  304. {
  305.   j = GetArraySpan(alias_list);
  306.  
  307.   if( param[0]=='\0' )
  308.     {
  309.       for( i=0;i<=j;i++ )
  310.         printf("alias %s=%s\n",alias_list[i][0],alias_list[i][1]);
  311.       return EXIT_SUCCESS;
  312.     }
  313.   for( i=0;param[i]!='\0';i++ )
  314.     {
  315.       if( param[i]=='=' )
  316.         {
  317.            param[i] = '\0';
  318.            for( k=0;k<=j;k++ )
  319.            if( !stricmp(param,alias_list[k][0]) )
  320.              {
  321.                strcpy(alias_list[k][1],param+i+1);
  322.                return;
  323.              }
  324.            strcpy(alias_list[j+1][0],param);
  325.            strcpy(alias_list[j+1][1],param+i+1);
  326.            return EXIT_SUCCESS;
  327.         }
  328.     }
  329.   for( i=0;i<=j;i++ )
  330.     if( !stricmp(param,alias_list[i][0]) )
  331.       {
  332.         printf("alias %s=%s\n",alias_list[i][0],alias_list[i][1]);
  333.         return EXIT_SUCCESS;
  334.       }
  335.   printf("alias %s is not defined.\n",param);
  336.   return EXIT_FAILURE;
  337. }
  338.  
  339.  
  340. InternalCommand_CD(pParm)
  341. {
  342. // change directory
  343.   if( !pParm[0] )
  344.     {
  345. // no other argument, so just display current dir
  346.       printf("%s\n",FullPath("."));
  347.    } else {
  348.       if( my_chdir(pParm) )
  349.         printf("No such directory \"%s\".\n",pParm);
  350.    }
  351.    return EXIT_SUCCESS;
  352. }
  353.  
  354.  
  355. InternalCommand_CHDIR(param)
  356. {
  357.   return InternalCommand_CD(param)
  358. }
  359.  
  360.  
  361. InternalCommand_CLS(pCommand)
  362. {
  363.    ScreenClear();
  364.    return EXIT_SUCCESS;
  365. }
  366.  
  367.  
  368. InternalCommand_ECHO(param)
  369. {
  370.   printf("%s\n",param);
  371.   return EXIT_SUCCESS;
  372. }
  373.  
  374.  
  375. InternalCommand_HELP(param)
  376. {
  377.   if( param[0]!='\0' )
  378.     {
  379.       for( i=0;i<=GetArraySpan(InternalCommands);i++ )
  380.         {
  381.           if( !stricmp(param,InternalCommands[i]) )
  382.             {
  383.               printf("\n%s\n",InternalHelp[i]);
  384.               return EXIT_SUCCESS;
  385.             }
  386.         }
  387.     }
  388.   printf("The CEnvi shell recognizes the following internal commands:\n\n");
  389.   for( i=0;i<=GetArraySpan(InternalCommands);i++ )
  390.     printf("%s\n",InternalCommands[i]);
  391.   return EXIT_SUCCESS;
  392. }
  393.  
  394.  
  395. InternalCommand_PROMPT(param)
  396. {
  397.   if( param[0]=='\0' )
  398.     {
  399.       PROMPT = "";
  400.     } else {
  401.       if( param[0]=='"' )
  402.         {
  403.           param++;
  404.           s = strlen(param);
  405.           if( param[s-1]=='"' ) param[s-1] = '\0'
  406.         }
  407.  
  408.       strcpy(PROMPT,param);
  409.     }
  410.   getenv();
  411.   return EXIT_SUCCESS;
  412. }
  413.  
  414.  
  415. InternalCommand_PWD(param)
  416. {
  417.   printf("Current working directory is %s\n",FullPath("."));
  418.   return EXIT_SUCCESS;
  419. }
  420.  
  421.  
  422. InternalCommand_SET(pParm)
  423. {
  424.    if ( !pParm[0] ) {
  425.       // no parameters so show entire SET
  426.       EnvList = getenv();
  427.       // sort alphabetically, just because we can
  428.       qsort(EnvList,GetArraySpan(EnvList),"stricmp");
  429.       for ( lIdx = 0; EnvList[lIdx]; lIdx++ )
  430.          printf("%s=%s\n",EnvList[lIdx],getenv(EnvList[lIdx]));
  431.    } else if ( !(lEqual = strchr(pParm,'=')) ) {
  432.       // show single value if there is one
  433.       if ( getenv(pParm) )
  434.          printf("%s=%s\n",strupr(pParm),getenv(pParm));
  435.    } else {
  436.       // set to new value
  437.       lEqual[0] = '\0';
  438.       putenv(pParm,lEqual+1);
  439.    }
  440.    return EXIT_SUCCESS;
  441. }
  442.  
  443.  
  444. InternalCommand_START(pCommand)
  445. {
  446.   lRet = spawn(P_NOWAIT,pCommand);
  447.   if( -1 == lRet ) printf("Error in starting %s\n",pCommand);
  448.   return lRet;
  449. }
  450.  
  451.  
  452. InternalCommand_TYPE(pCommand)
  453. {
  454.   if( lFp = fopen(pCommand,"rt") )
  455.     {
  456.       while( lLine = fgets(lFp) )
  457.         fputs(lLine,stdout);
  458.       fclose(lFp);
  459.     } else {
  460.       printf("Unable to open file \"%s\"...\n",pCommand);
  461.     }
  462.   return EXIT_SUCCESS;
  463. }
  464.  
  465. //************************************************************************
  466. //****** END SECTION TO HANDLE INTERNAL COMMANDS FOR THE CLI SHELL *******
  467. //************************************************************************
  468.  
  469. if( defined(_NTWIN_) || defined(_WINDOWS_) )
  470. {
  471.    ext_key_home =   0x24;
  472.    ext_key_end  =   0x23;
  473.    ext_key_up   =   0x26;
  474.    ext_key_down =   0x28;
  475. }
  476. if( defined(_NTCON_) || defined(_DOS_) || defined(_DOS32_) || defined(_NWNLM_) ||
  477.     defined(_OS2_) )
  478. {
  479.    ext_key_home =   0x47;
  480.    ext_key_end  =   0x4F;
  481.    ext_key_up   =   0x48;
  482.    ext_key_down =   0x50;
  483. }
  484. shellchr_alright = TRUE;
  485. #include <shellchr.cmm>
  486.