home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / AUTOLOAD.CMM < prev    next >
Text File  |  1996-02-13  |  16KB  |  510 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, except the Mac which
  7.  * is in development at this time.
  8.  *
  9.  * Latest revision: 02/12/96 (Version 2.11)
  10.  */
  11.  
  12. #include "filename.lib"
  13.  
  14. if( !defined(_SHELL_) )
  15. {
  16.   printf("Autoload.cmm is an internal shell implementation file and cannot");
  17.   printf("be\nrun as a program.\n");
  18.   exit(EXIT_FAILURE);
  19. }
  20.  
  21. /* ---------------------------------------------------------------------- */
  22.  
  23. if ( defined(_DOS_) || defined(_DOS32_) ) {
  24.    // spawning can leave too-little memory, so increase COMSPEC size
  25.    if ( !defined(COMSPEC_ENV_SIZE) )
  26.       COMSPEC_ENV_SIZE = 1536;
  27. }
  28.  
  29. ///////////////////////////////////////////////////////////////////////
  30.  
  31.  
  32. #include "cmdline.lib"
  33.  
  34. // The other systems all have this defined. Netware has no Environment
  35. // variables so we define it here.
  36.  
  37. if( defined(_NWNLM_) ) PROMPT = NULL;
  38. if( defined(_UNIX_) )
  39.   if( defined(PS1) )
  40.     PROMPT = PS1;
  41.   else
  42.     PROMPT = NULL;
  43.  
  44. if ( !defined (_UNIX_) && !defined(_WINDOWS_) && !defined(_NTWIN_) &&
  45.      !defined(_95WIN_) && !defined(_NWNLM_) && strnicmp("[CEnvi]",PROMPT,7) )
  46.   {
  47.     strcpy(__Autoload_Old_Prompt,PROMPT);
  48.     sprintf(PROMPT,"[CEnvi] %s",PROMPT);
  49.     getenv();
  50.   }
  51.  
  52. /*
  53.  * Change the current directory to the given one. Return 0 if successful.
  54.  */
  55. my_chdir(newdir)
  56. {
  57.   if( defined(_UNIX_) || defined(_NWNLM_) ) return chdir(newdir);
  58.   if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
  59.     {
  60.       lReg.ah = 0x3B;
  61.          lReg.ds = segment(newdir), lReg.dx = offset(newdir);
  62.       interrupt(0x21,lReg,out);
  63.       return out.ax==3;
  64.     }
  65.   if( defined(_WIN32_) )
  66.     {
  67.       return !DynamicLink("KERNEL32","SetCurrentDirectoryA",STDCALL,newdir);
  68.     }
  69.   if( defined(_OS2_) )
  70.     {
  71.       #define ORD_DOS32SETCURRENTDIR   255
  72.       return DynamicLink("doscalls",ORD_DOS32SETCURRENTDIR,BIT32,CDECL,newdir);
  73.     }
  74. }
  75.  
  76. //**************************************************************************
  77. //****** BEGIN SECTION TO HANDLE INTERNAL COMMANDS FOR THE CLI SHELL *******
  78. //**************************************************************************
  79.  
  80. __Autoload_Alias_List; // global so AUTOEXEC.CMM may fill it in
  81.  
  82. // Next is a list of all commands handled internally to the shell. This list
  83. // must remain sorted. Also, the __Autoload_Internal_Help array must match.
  84. // It is fortunate that currently all the 'extra' commands only defined on
  85. // certain systems fall later than any other command in the alphabet.
  86.  
  87. __Autoload_Internal_Commands = {
  88.   "ALIAS", "CD", "CLS", "ECHO", "HELP", "PROMPT", "PWD", "REM"
  89. };
  90. __Autoload_Internal_Help = {
  91.   "The ALIAS command lets you view or define aliases. ALIAS by itself will list\n"
  92.   "all aliases currently defined. Otherwise define an alias by using the format:\n"
  93.   "\n  ALIAS name=text string",
  94.   "CD used by itself will print the current directory. If you give it a\ndirectory name, it will change the current directory to it.",
  95.   "CLS clears the screen.", 
  96.   "ECHO prints some text to the screen.",
  97.   "HELP by itself prints all known commands, otherwise it prints help on a command.",
  98.   "PROMPT will change the prompt. By itself, it resets the prompt to the default.",
  99.   "PWD will print the current working directory.",
  100.   "REM indicates that the following text is a remark.",
  101. };
  102.  
  103. if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) || defined(_WIN32_)
  104.  || defined(_OS2_) || defined(_UNIX_) )
  105. {
  106.   __Autoload_Internal_Commands[GetArraySpan(__Autoload_Internal_Commands)+1] = "SET";
  107.   __Autoload_Internal_Help[GetArraySpan(__Autoload_Internal_Help)+1] = 
  108.     "SET allows you to view or change environment variables. SET by itself will\n"
  109.     "list all such variables currently defined. Otherwise define one by using the\n"
  110.     "format:\n\n"
  111.     "  SET variable=value";
  112. }
  113.  
  114.  
  115. if( defined(_WINDOWS_) || defined(_NTWIN_) || defined(_95WIN_) || defined(_NWNLM_) )
  116. {
  117.   __Autoload_Internal_Commands[GetArraySpan(__Autoload_Internal_Commands)+1] = "START";
  118.   __Autoload_Internal_Help[GetArraySpan(__Autoload_Internal_Help)+1] = 
  119.     "START starts an Executable or NLM running asynchronously.";
  120. }
  121.  
  122. // Make sure all versions have access to type.
  123. __Autoload_Internal_Commands[GetArraySpan(__Autoload_Internal_Commands)+1] =
  124.   "TYPE";
  125. __Autoload_Internal_Help[GetArraySpan(__Autoload_Internal_Help)+1] = 
  126.   "TYPE will echo the contents of a file to the screen.";
  127.  
  128. // set up filter to handle each command input
  129. ShellFilterCommand("AutoloadFilterCommand");
  130.  
  131. /* ---------------------------------------------------------------------- */
  132.  
  133. AutoloadFilterCommand(pCommand)
  134. {
  135.    if ( !stricmp(pCommand,"EXIT") )
  136.      {
  137.        if( defined(__Autoload_Old_Prompt) ) PROMPT = __Autoload_Old_Prompt;
  138.      } else {
  139.        strcpy(tmp,pCommand);
  140.        s = strchr(tmp,' ');
  141.        if( s ) s[0] = '\0';
  142.  
  143.        if ( !stricmp(SplitFileName(tmp).name,"autoload") )
  144.          {
  145.            printf("Autoload.cmm is an internal shell implementation file and cannot be\n");
  146.            printf("run as a program.\n");
  147.            pCommand[0] = '\0';
  148.          } else {
  149.            strcpy(command,pCommand); pCommand[0] = '\0';
  150.            ProcessCommandLine(command,"AutoloadFilterExecutor");
  151.          }
  152.      }
  153. }
  154.  
  155. AutoloadFilterExecutor(pCommand)
  156. {
  157.   s = strlen(pCommand);
  158.   while( isspace(pCommand[s-1]) ) pCommand[--s] = '\0';
  159.   if( pCommand[0]=='\0' )
  160.     return EXIT_SUCCESS;
  161.  
  162.   lResult = EXIT_SUCCESS;
  163.  
  164. // First thing to do is check to see if this line has an alias.
  165.  
  166.   if( defined(__Autoload_Alias_List) && sscanf(pCommand,"%s",alias) )
  167.     {
  168.       strcpy(the_rest,pCommand+strlen(alias));
  169.       for( j=0;j<=GetArraySpan(__Autoload_Alias_List);j++ )
  170.         {
  171.           if( !stricmp(alias,__Autoload_Alias_List[j][0]) )
  172.             {
  173.               pCommand = "";
  174.               strcpy(pCommand,__Autoload_Alias_List[j][1]);
  175.               if( s = strstr(pCommand,"[]") )
  176.                 {
  177.                   memcpy(s+strlen(the_rest),s+2,strlen(s)-1);
  178.                   memcpy(s,the_rest,strlen(the_rest));
  179.                 } else {
  180.                   strcat(pCommand,the_rest);
  181.                 }
  182.               break;
  183.             }
  184.         }
  185.     }
  186.  
  187. // Handle Unix-like & command
  188.    background = 0;
  189.    s = strlen(pCommand)-1;
  190.    if( pCommand[s]=='&' )
  191.      {
  192.        if( pCommand[s-1]=='\\' )
  193.          {
  194.            strcpy(pCommand+s-1,"&");
  195.          } else {
  196.            pCommand[s] = '\0';
  197.            background = 1;
  198.          }
  199.      }
  200.  
  201. // if line starts with a quote, it is cmm code to execute immediately.
  202.    if( strchr("\'`",pCommand[0]) )
  203.      {
  204.        if( pCommand[strlen(pCommand)-1]==pCommand[0] )
  205.          pCommand[strlen(pCommand)-1] = 0;
  206.        lResult = interpret(pCommand+1,INTERP_TEXT|INTERP_NOINHERIT_GLOBAL|
  207.                                       INTERP_NOINHERIT_LOCAL);
  208.        return lResult;
  209.      }
  210.  
  211. // Check to see if the command is one of our internal commands.
  212.    lCommand = ""; sscanf(pCommand,"%s",lCommand);
  213.    if( bsearch(lCommand,__Autoload_Internal_Commands,"stricmp") )
  214.      {
  215.        sprintf(lInternalFunction,"__Autoload_Internal_Command_%s",strupr(lCommand));
  216.        lParm = pCommand + strlen(lCommand);
  217.        while( isspace(lParm[0]) ) lParm++;
  218.        lResult = function(lInternalFunction,lParm);
  219.        pCommand[0] = '\0';
  220.      } else if ( !strcmp(lCommand+1,":") && !defined(_UNIX_) ) {
  221. // change drive
  222.        if( defined(_OS2_) )
  223.          {
  224.            #define ORD_DOS32SETDEFAULTDISK  220
  225.            DynamicLink("doscalls",ORD_DOS32SETDEFAULTDISK,BIT32,CDECL,
  226.                        toupper(lCommand[0])-'A'+1);
  227.            lResult = EXIT_SUCCESS;
  228.          }
  229.        if( defined(_WIN32_) )
  230.          {
  231.            lResult = __Autoload_Internal_Command_CD(lCommand);
  232.          }
  233.        if( defined(_WINDOWS_) || defined(_DOS_) || defined(_DOS32_) )
  234.          {
  235.            lReg.ah = 0x0E;
  236.            lReg.dl = toupper(lCommand[0]) - 'A';
  237.            interrupt(0x21,lReg);
  238.            lResult = EXIT_SUCCESS;
  239.          }
  240.        pCommand[0] = '\0';
  241.      }
  242.  
  243. // If we get here with pCommand not blank, no one handled the command, so we
  244. // dispatch it.
  245.    if ( pCommand[0] )
  246.      {
  247. // We pass it on to the command processor if explicitly told to
  248.        if( pCommand[0]=='~' )
  249.          {
  250.            strcpy(command,pCommand+((pCommand[0]=='~')?1:0));
  251.            pCommand[0] = '\0';
  252.  
  253.            if( defined(_OS2_) || defined(_NTCON_) || defined(_NTWIN_) )
  254.              {
  255.                lResult = spawn(background?P_NOWAIT:P_WAIT,
  256.                                "CMD.EXE","/C",command);
  257.              }
  258.            else if( defined(_95CON_) || defined(_95WIN_) )
  259.              {
  260.                lResult = spawn(background?P_NOWAIT:P_WAIT,
  261.                                "command","/C",command);
  262.              }
  263.            else if( defined(_DOS_) || defined(_DOS32_) )
  264.              {
  265.                lResult = system(P_SWAP,command);
  266.              }
  267.            else
  268.              {
  269.                lResult = system(command);
  270.              }
  271.            return lResult;
  272.          }
  273.  
  274. // try to run the given program
  275.        if( defined(_DOS_) )
  276.          {
  277.            lResult = spawn(P_SWAP,pCommand);
  278.          } else if( defined(_NWNLM_) ) {
  279.            lResult = spawn(P_NOWAIT,pCommand);
  280.          } else {
  281.            lResult = spawn(background?P_NOWAIT:P_WAIT,pCommand);
  282.          }
  283.      }
  284.  
  285. // If the program could not be executed, we look to see if there is a
  286. // file by that name, and take the appropriate action.
  287.    if( lResult==-1 && pCommand[0] )
  288.      {
  289.        chartofind = ' ';
  290.        if( pCommand[0]=='"' ) { pCommand++; chartofind = '"'; }
  291.        if( s = strchr(pCommand,chartofind) ) s[0] = '\0';
  292.        if( pCommand[strlen(pCommand)-1]=='\\' && strlen(pCommand)>1 )
  293.          pCommand[strlen(pCommand)-1] = '\0';
  294.  
  295.        files = Directory(pCommand,0,0xff);
  296.        if( (files!=NULL && GetArraySpan(files)==0 &&
  297.             files[0].attrib&FATTR_SUBDIR) ||
  298.             !strcmp(pCommand,filename_separator) ||
  299.             !strcmp(pCommand,".") || !strcmp(pCommand,"..") )
  300.              {
  301.                my_chdir(pCommand);
  302.                return 0;
  303.              }
  304.        if( files!=NULL && GetArraySpan(files)==0 )
  305.          {
  306.            printf("\"%s\" is not executable.\n",pCommand);
  307.            return lresult;
  308.          }
  309.        filestruct = SplitFileName(pCommand);
  310.        sprintf(badcommand,"%s%s",filestruct.name,filestruct.ext);
  311.        printf("Unable to find program \"%s\" as an executable, batch, or cmm program\n"
  312.               "anywhere in your path.\n",strupr(badcommand));
  313.      }
  314.  
  315.    return lResult;
  316. }
  317.  
  318. /* ---------------------------------------------------------------------- */
  319.  
  320. // We have a number of internal commands.
  321.  
  322. __Autoload_Internal_Command_ALIAS(param)
  323. {
  324.   if( defined(__Autoload_Alias_List) )
  325.     j = GetArraySpan(__Autoload_Alias_List);
  326.   else
  327.     j = -1;
  328.  
  329.   if( param[0]=='\0' )
  330.     {
  331.       printf("\n");
  332.       for( i=0;i<=j;i++ )
  333.         printf("alias %s=%s\n",__Autoload_Alias_List[i][0],__Autoload_Alias_List[i][1]);
  334.       printf("\n");
  335.       return EXIT_SUCCESS;
  336.     }
  337.   for( i=0;param[i]!='\0';i++ )
  338.     {
  339.       if( param[i]=='=' )
  340.         {
  341.            param[i] = '\0';
  342.            for( k=0;k<=j;k++ )
  343.            if( !stricmp(param,__Autoload_Alias_List[k][0]) )
  344.              {
  345.                strcpy(__Autoload_Alias_List[k][1],param+i+1);
  346.                return EXIT_SUCCESS;
  347.              }
  348.            strcpy(__Autoload_Alias_List[j+1][0],param);
  349.            strcpy(__Autoload_Alias_List[j+1][1],param+i+1);
  350.            return EXIT_SUCCESS;
  351.         }
  352.     }
  353.   for( i=0;i<=j;i++ )
  354.     if( !stricmp(param,__Autoload_Alias_List[i][0]) )
  355.       {
  356.         printf("\nalias %s=%s\n\n",__Autoload_Alias_List[i][0],__Autoload_Alias_List[i][1]);
  357.         return EXIT_SUCCESS;
  358.       }
  359.   printf("alias %s is not defined.\n",param);
  360.   return EXIT_FAILURE;
  361. }
  362.  
  363.  
  364. __Autoload_Internal_Command_CD(pParm)
  365. {
  366. // change directory
  367.   if( !pParm[0] )
  368.     {
  369. // no other argument, so just display current dir
  370.       printf("%s\n",FullPath("."));
  371.    } else {
  372.       if( my_chdir(pParm) )
  373.         printf("No such directory \"%s\".\n",pParm);
  374.    }
  375.    return EXIT_SUCCESS;
  376. }
  377.  
  378.  
  379. __Autoload_Internal_Command_CLS(pCommand)
  380. {
  381.    ScreenClear();
  382.    return EXIT_SUCCESS;
  383. }
  384.  
  385.  
  386. __Autoload_Internal_Command_ECHO(param)
  387. {
  388.   printf("%s\n",param);
  389.   return EXIT_SUCCESS;
  390. }
  391.  
  392.  
  393. __Autoload_Internal_Command_HELP(param)
  394. {
  395.   if( param[0]!='\0' )
  396.     {
  397.       for( i=0;i<=GetArraySpan(__Autoload_Internal_Commands);i++ )
  398.         {
  399.           if( !stricmp(param,__Autoload_Internal_Commands[i]) )
  400.             {
  401.               printf("\n%s\n",__Autoload_Internal_Help[i]);
  402.               return EXIT_SUCCESS;
  403.             }
  404.         }
  405.     }
  406.   printf("The CEnvi shell recognizes the following internal commands:\n\n");
  407.   for( i=0;i<=GetArraySpan(__Autoload_Internal_Commands);i++ )
  408.     printf("%s\n",__Autoload_Internal_Commands[i]);
  409.   printf("\n");
  410.   return EXIT_SUCCESS;
  411. }
  412.  
  413.  
  414. __Autoload_Internal_Command_PROMPT(param)
  415. {
  416.   if( param[0]=='\0' )
  417.     {
  418.       PROMPT = "";
  419.     } else {
  420.       if( param[0]=='"' )
  421.         {
  422.           param++;
  423.           s = strlen(param);
  424.           if( param[s-1]=='"' ) param[s-1] = '\0'
  425.         }
  426.  
  427.       strcpy(PROMPT,param);
  428.     }
  429.   getenv();
  430.   return EXIT_SUCCESS;
  431. }
  432.  
  433.  
  434. __Autoload_Internal_Command_PWD(param)
  435. {
  436.   printf("Current working directory is %s\n",FullPath("."));
  437.   return EXIT_SUCCESS;
  438. }
  439.  
  440.  
  441. __Autoload_Internal_Command_REM(param)
  442. {
  443.   return EXIT_SUCCESS;
  444. }
  445.  
  446.  
  447. __Autoload_Internal_Command_SET(pParm)
  448. {
  449.    if ( !pParm[0] ) {
  450.       // no parameters so show entire SET
  451.       EnvList = getenv();
  452.       qsort(EnvList,GetArraySpan(EnvList),"stricmp");
  453.       for ( lIdx = 0; EnvList[lIdx]; lIdx++ )
  454.          printf("%s=%s\n",EnvList[lIdx],getenv(EnvList[lIdx]));
  455.    } else if ( !(lEqual = strchr(pParm,'=')) ) {
  456.       // show single value if there is one
  457.       if ( getenv(pParm) )
  458.          printf("%s=%s\n",strupr(pParm),getenv(pParm));
  459.    } else {
  460.       // set to new value
  461.       lEqual[0] = '\0';
  462.       putenv(pParm,lEqual+1);
  463.    }
  464.    return EXIT_SUCCESS;
  465. }
  466.  
  467.  
  468. __Autoload_Internal_Command_START(pCommand)
  469. {
  470.   lRet = spawn(P_NOWAIT,pCommand);
  471.   if( -1 == lRet ) printf("Error in starting %s\n",pCommand);
  472.   return lRet;
  473. }
  474.  
  475.  
  476. __Autoload_Internal_Command_TYPE(pCommand)
  477. {
  478.   if( lFp = fopen(pCommand,"rt") )
  479.     {
  480.       while( lLine = fgets(lFp) )
  481.         fputs(lLine,stdout);
  482.       fclose(lFp);
  483.     } else {
  484.       printf("Unable to open file \"%s\"...\n",pCommand);
  485.     }
  486.   return EXIT_SUCCESS;
  487. }
  488.  
  489. //************************************************************************
  490. //****** END SECTION TO HANDLE INTERNAL COMMANDS FOR THE CLI SHELL *******
  491. //************************************************************************
  492.  
  493. if( defined(_NTWIN_) || defined(_95WIN_) || defined(_WINDOWS_) )
  494. {
  495.    ext_key_home =   0x24;
  496.    ext_key_end  =   0x23;
  497.    ext_key_up   =   0x26;
  498.    ext_key_down =   0x28;
  499. }
  500. if( defined(_NTCON_) || defined(_95CON_) || defined(_DOS_) ||
  501.     defined(_DOS32_) || defined(_NWNLM_) || defined(_OS2_) )
  502. {
  503.    ext_key_home =   0x47;
  504.    ext_key_end  =   0x4F;
  505.    ext_key_up   =   0x48;
  506.    ext_key_down =   0x50;
  507. }
  508. shellchr_alright = TRUE;
  509. #include <shellchr.cmm>
  510.