home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cenvi23.zip
/
AUTOLOAD.CMM
< prev
next >
Wrap
Text File
|
1996-02-13
|
16KB
|
510 lines
/*
* Autoload.cmm
*
* This file handles most of the shell interface of CEnvi. The rest of the
* interface is included as the file "ShellChr.cmm". This version will run
* on all CEnvi systems known as of the revision date, except the Mac which
* is in development at this time.
*
* Latest revision: 02/12/96 (Version 2.11)
*/
#include "filename.lib"
if( !defined(_SHELL_) )
{
printf("Autoload.cmm is an internal shell implementation file and cannot");
printf("be\nrun as a program.\n");
exit(EXIT_FAILURE);
}
/* ---------------------------------------------------------------------- */
if ( defined(_DOS_) || defined(_DOS32_) ) {
// spawning can leave too-little memory, so increase COMSPEC size
if ( !defined(COMSPEC_ENV_SIZE) )
COMSPEC_ENV_SIZE = 1536;
}
///////////////////////////////////////////////////////////////////////
#include "cmdline.lib"
// The other systems all have this defined. Netware has no Environment
// variables so we define it here.
if( defined(_NWNLM_) ) PROMPT = NULL;
if( defined(_UNIX_) )
if( defined(PS1) )
PROMPT = PS1;
else
PROMPT = NULL;
if ( !defined (_UNIX_) && !defined(_WINDOWS_) && !defined(_NTWIN_) &&
!defined(_95WIN_) && !defined(_NWNLM_) && strnicmp("[CEnvi]",PROMPT,7) )
{
strcpy(__Autoload_Old_Prompt,PROMPT);
sprintf(PROMPT,"[CEnvi] %s",PROMPT);
getenv();
}
/*
* Change the current directory to the given one. Return 0 if successful.
*/
my_chdir(newdir)
{
if( defined(_UNIX_) || defined(_NWNLM_) ) return chdir(newdir);
if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
{
lReg.ah = 0x3B;
lReg.ds = segment(newdir), lReg.dx = offset(newdir);
interrupt(0x21,lReg,out);
return out.ax==3;
}
if( defined(_WIN32_) )
{
return !DynamicLink("KERNEL32","SetCurrentDirectoryA",STDCALL,newdir);
}
if( defined(_OS2_) )
{
#define ORD_DOS32SETCURRENTDIR 255
return DynamicLink("doscalls",ORD_DOS32SETCURRENTDIR,BIT32,CDECL,newdir);
}
}
//**************************************************************************
//****** BEGIN SECTION TO HANDLE INTERNAL COMMANDS FOR THE CLI SHELL *******
//**************************************************************************
__Autoload_Alias_List; // global so AUTOEXEC.CMM may fill it in
// Next is a list of all commands handled internally to the shell. This list
// must remain sorted. Also, the __Autoload_Internal_Help array must match.
// It is fortunate that currently all the 'extra' commands only defined on
// certain systems fall later than any other command in the alphabet.
__Autoload_Internal_Commands = {
"ALIAS", "CD", "CLS", "ECHO", "HELP", "PROMPT", "PWD", "REM"
};
__Autoload_Internal_Help = {
"The ALIAS command lets you view or define aliases. ALIAS by itself will list\n"
"all aliases currently defined. Otherwise define an alias by using the format:\n"
"\n ALIAS name=text string",
"CD used by itself will print the current directory. If you give it a\ndirectory name, it will change the current directory to it.",
"CLS clears the screen.",
"ECHO prints some text to the screen.",
"HELP by itself prints all known commands, otherwise it prints help on a command.",
"PROMPT will change the prompt. By itself, it resets the prompt to the default.",
"PWD will print the current working directory.",
"REM indicates that the following text is a remark.",
};
if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) || defined(_WIN32_)
|| defined(_OS2_) || defined(_UNIX_) )
{
__Autoload_Internal_Commands[GetArraySpan(__Autoload_Internal_Commands)+1] = "SET";
__Autoload_Internal_Help[GetArraySpan(__Autoload_Internal_Help)+1] =
"SET allows you to view or change environment variables. SET by itself will\n"
"list all such variables currently defined. Otherwise define one by using the\n"
"format:\n\n"
" SET variable=value";
}
if( defined(_WINDOWS_) || defined(_NTWIN_) || defined(_95WIN_) || defined(_NWNLM_) )
{
__Autoload_Internal_Commands[GetArraySpan(__Autoload_Internal_Commands)+1] = "START";
__Autoload_Internal_Help[GetArraySpan(__Autoload_Internal_Help)+1] =
"START starts an Executable or NLM running asynchronously.";
}
// Make sure all versions have access to type.
__Autoload_Internal_Commands[GetArraySpan(__Autoload_Internal_Commands)+1] =
"TYPE";
__Autoload_Internal_Help[GetArraySpan(__Autoload_Internal_Help)+1] =
"TYPE will echo the contents of a file to the screen.";
// set up filter to handle each command input
ShellFilterCommand("AutoloadFilterCommand");
/* ---------------------------------------------------------------------- */
AutoloadFilterCommand(pCommand)
{
if ( !stricmp(pCommand,"EXIT") )
{
if( defined(__Autoload_Old_Prompt) ) PROMPT = __Autoload_Old_Prompt;
} else {
strcpy(tmp,pCommand);
s = strchr(tmp,' ');
if( s ) s[0] = '\0';
if ( !stricmp(SplitFileName(tmp).name,"autoload") )
{
printf("Autoload.cmm is an internal shell implementation file and cannot be\n");
printf("run as a program.\n");
pCommand[0] = '\0';
} else {
strcpy(command,pCommand); pCommand[0] = '\0';
ProcessCommandLine(command,"AutoloadFilterExecutor");
}
}
}
AutoloadFilterExecutor(pCommand)
{
s = strlen(pCommand);
while( isspace(pCommand[s-1]) ) pCommand[--s] = '\0';
if( pCommand[0]=='\0' )
return EXIT_SUCCESS;
lResult = EXIT_SUCCESS;
// First thing to do is check to see if this line has an alias.
if( defined(__Autoload_Alias_List) && sscanf(pCommand,"%s",alias) )
{
strcpy(the_rest,pCommand+strlen(alias));
for( j=0;j<=GetArraySpan(__Autoload_Alias_List);j++ )
{
if( !stricmp(alias,__Autoload_Alias_List[j][0]) )
{
pCommand = "";
strcpy(pCommand,__Autoload_Alias_List[j][1]);
if( s = strstr(pCommand,"[]") )
{
memcpy(s+strlen(the_rest),s+2,strlen(s)-1);
memcpy(s,the_rest,strlen(the_rest));
} else {
strcat(pCommand,the_rest);
}
break;
}
}
}
// Handle Unix-like & command
background = 0;
s = strlen(pCommand)-1;
if( pCommand[s]=='&' )
{
if( pCommand[s-1]=='\\' )
{
strcpy(pCommand+s-1,"&");
} else {
pCommand[s] = '\0';
background = 1;
}
}
// if line starts with a quote, it is cmm code to execute immediately.
if( strchr("\'`",pCommand[0]) )
{
if( pCommand[strlen(pCommand)-1]==pCommand[0] )
pCommand[strlen(pCommand)-1] = 0;
lResult = interpret(pCommand+1,INTERP_TEXT|INTERP_NOINHERIT_GLOBAL|
INTERP_NOINHERIT_LOCAL);
return lResult;
}
// Check to see if the command is one of our internal commands.
lCommand = ""; sscanf(pCommand,"%s",lCommand);
if( bsearch(lCommand,__Autoload_Internal_Commands,"stricmp") )
{
sprintf(lInternalFunction,"__Autoload_Internal_Command_%s",strupr(lCommand));
lParm = pCommand + strlen(lCommand);
while( isspace(lParm[0]) ) lParm++;
lResult = function(lInternalFunction,lParm);
pCommand[0] = '\0';
} else if ( !strcmp(lCommand+1,":") && !defined(_UNIX_) ) {
// change drive
if( defined(_OS2_) )
{
#define ORD_DOS32SETDEFAULTDISK 220
DynamicLink("doscalls",ORD_DOS32SETDEFAULTDISK,BIT32,CDECL,
toupper(lCommand[0])-'A'+1);
lResult = EXIT_SUCCESS;
}
if( defined(_WIN32_) )
{
lResult = __Autoload_Internal_Command_CD(lCommand);
}
if( defined(_WINDOWS_) || defined(_DOS_) || defined(_DOS32_) )
{
lReg.ah = 0x0E;
lReg.dl = toupper(lCommand[0]) - 'A';
interrupt(0x21,lReg);
lResult = EXIT_SUCCESS;
}
pCommand[0] = '\0';
}
// If we get here with pCommand not blank, no one handled the command, so we
// dispatch it.
if ( pCommand[0] )
{
// We pass it on to the command processor if explicitly told to
if( pCommand[0]=='~' )
{
strcpy(command,pCommand+((pCommand[0]=='~')?1:0));
pCommand[0] = '\0';
if( defined(_OS2_) || defined(_NTCON_) || defined(_NTWIN_) )
{
lResult = spawn(background?P_NOWAIT:P_WAIT,
"CMD.EXE","/C",command);
}
else if( defined(_95CON_) || defined(_95WIN_) )
{
lResult = spawn(background?P_NOWAIT:P_WAIT,
"command","/C",command);
}
else if( defined(_DOS_) || defined(_DOS32_) )
{
lResult = system(P_SWAP,command);
}
else
{
lResult = system(command);
}
return lResult;
}
// try to run the given program
if( defined(_DOS_) )
{
lResult = spawn(P_SWAP,pCommand);
} else if( defined(_NWNLM_) ) {
lResult = spawn(P_NOWAIT,pCommand);
} else {
lResult = spawn(background?P_NOWAIT:P_WAIT,pCommand);
}
}
// If the program could not be executed, we look to see if there is a
// file by that name, and take the appropriate action.
if( lResult==-1 && pCommand[0] )
{
chartofind = ' ';
if( pCommand[0]=='"' ) { pCommand++; chartofind = '"'; }
if( s = strchr(pCommand,chartofind) ) s[0] = '\0';
if( pCommand[strlen(pCommand)-1]=='\\' && strlen(pCommand)>1 )
pCommand[strlen(pCommand)-1] = '\0';
files = Directory(pCommand,0,0xff);
if( (files!=NULL && GetArraySpan(files)==0 &&
files[0].attrib&FATTR_SUBDIR) ||
!strcmp(pCommand,filename_separator) ||
!strcmp(pCommand,".") || !strcmp(pCommand,"..") )
{
my_chdir(pCommand);
return 0;
}
if( files!=NULL && GetArraySpan(files)==0 )
{
printf("\"%s\" is not executable.\n",pCommand);
return lresult;
}
filestruct = SplitFileName(pCommand);
sprintf(badcommand,"%s%s",filestruct.name,filestruct.ext);
printf("Unable to find program \"%s\" as an executable, batch, or cmm program\n"
"anywhere in your path.\n",strupr(badcommand));
}
return lResult;
}
/* ---------------------------------------------------------------------- */
// We have a number of internal commands.
__Autoload_Internal_Command_ALIAS(param)
{
if( defined(__Autoload_Alias_List) )
j = GetArraySpan(__Autoload_Alias_List);
else
j = -1;
if( param[0]=='\0' )
{
printf("\n");
for( i=0;i<=j;i++ )
printf("alias %s=%s\n",__Autoload_Alias_List[i][0],__Autoload_Alias_List[i][1]);
printf("\n");
return EXIT_SUCCESS;
}
for( i=0;param[i]!='\0';i++ )
{
if( param[i]=='=' )
{
param[i] = '\0';
for( k=0;k<=j;k++ )
if( !stricmp(param,__Autoload_Alias_List[k][0]) )
{
strcpy(__Autoload_Alias_List[k][1],param+i+1);
return EXIT_SUCCESS;
}
strcpy(__Autoload_Alias_List[j+1][0],param);
strcpy(__Autoload_Alias_List[j+1][1],param+i+1);
return EXIT_SUCCESS;
}
}
for( i=0;i<=j;i++ )
if( !stricmp(param,__Autoload_Alias_List[i][0]) )
{
printf("\nalias %s=%s\n\n",__Autoload_Alias_List[i][0],__Autoload_Alias_List[i][1]);
return EXIT_SUCCESS;
}
printf("alias %s is not defined.\n",param);
return EXIT_FAILURE;
}
__Autoload_Internal_Command_CD(pParm)
{
// change directory
if( !pParm[0] )
{
// no other argument, so just display current dir
printf("%s\n",FullPath("."));
} else {
if( my_chdir(pParm) )
printf("No such directory \"%s\".\n",pParm);
}
return EXIT_SUCCESS;
}
__Autoload_Internal_Command_CLS(pCommand)
{
ScreenClear();
return EXIT_SUCCESS;
}
__Autoload_Internal_Command_ECHO(param)
{
printf("%s\n",param);
return EXIT_SUCCESS;
}
__Autoload_Internal_Command_HELP(param)
{
if( param[0]!='\0' )
{
for( i=0;i<=GetArraySpan(__Autoload_Internal_Commands);i++ )
{
if( !stricmp(param,__Autoload_Internal_Commands[i]) )
{
printf("\n%s\n",__Autoload_Internal_Help[i]);
return EXIT_SUCCESS;
}
}
}
printf("The CEnvi shell recognizes the following internal commands:\n\n");
for( i=0;i<=GetArraySpan(__Autoload_Internal_Commands);i++ )
printf("%s\n",__Autoload_Internal_Commands[i]);
printf("\n");
return EXIT_SUCCESS;
}
__Autoload_Internal_Command_PROMPT(param)
{
if( param[0]=='\0' )
{
PROMPT = "";
} else {
if( param[0]=='"' )
{
param++;
s = strlen(param);
if( param[s-1]=='"' ) param[s-1] = '\0'
}
strcpy(PROMPT,param);
}
getenv();
return EXIT_SUCCESS;
}
__Autoload_Internal_Command_PWD(param)
{
printf("Current working directory is %s\n",FullPath("."));
return EXIT_SUCCESS;
}
__Autoload_Internal_Command_REM(param)
{
return EXIT_SUCCESS;
}
__Autoload_Internal_Command_SET(pParm)
{
if ( !pParm[0] ) {
// no parameters so show entire SET
EnvList = getenv();
qsort(EnvList,GetArraySpan(EnvList),"stricmp");
for ( lIdx = 0; EnvList[lIdx]; lIdx++ )
printf("%s=%s\n",EnvList[lIdx],getenv(EnvList[lIdx]));
} else if ( !(lEqual = strchr(pParm,'=')) ) {
// show single value if there is one
if ( getenv(pParm) )
printf("%s=%s\n",strupr(pParm),getenv(pParm));
} else {
// set to new value
lEqual[0] = '\0';
putenv(pParm,lEqual+1);
}
return EXIT_SUCCESS;
}
__Autoload_Internal_Command_START(pCommand)
{
lRet = spawn(P_NOWAIT,pCommand);
if( -1 == lRet ) printf("Error in starting %s\n",pCommand);
return lRet;
}
__Autoload_Internal_Command_TYPE(pCommand)
{
if( lFp = fopen(pCommand,"rt") )
{
while( lLine = fgets(lFp) )
fputs(lLine,stdout);
fclose(lFp);
} else {
printf("Unable to open file \"%s\"...\n",pCommand);
}
return EXIT_SUCCESS;
}
//************************************************************************
//****** END SECTION TO HANDLE INTERNAL COMMANDS FOR THE CLI SHELL *******
//************************************************************************
if( defined(_NTWIN_) || defined(_95WIN_) || defined(_WINDOWS_) )
{
ext_key_home = 0x24;
ext_key_end = 0x23;
ext_key_up = 0x26;
ext_key_down = 0x28;
}
if( defined(_NTCON_) || defined(_95CON_) || defined(_DOS_) ||
defined(_DOS32_) || defined(_NWNLM_) || defined(_OS2_) )
{
ext_key_home = 0x47;
ext_key_end = 0x4F;
ext_key_up = 0x48;
ext_key_down = 0x50;
}
shellchr_alright = TRUE;
#include <shellchr.cmm>