home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga GigaPD 3
/
Amiga_GigaPD_v3_3of3.iso
/
fred_fish
/
fish_601-700_lha
/
fish_624.lha
/
CSH
/
csh519s.lha
/
comm2.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-25
|
33KB
|
1,438 lines
/*
* COMM2.C
*
* (c)1986 Matthew Dillon 9 October 1986
*
* Version 2.07M by Steve Drew 10-Sep-87
* Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
* Version 5.00L by Urban Mueller 17-Feb-91
*
*/
#include "shell.h"
/* comm2.c */
static long dptrtosecs(DPTR *d);
static long timeof(char *s);
static int evalif(void);
static int clinum(char *name);
static int copydir(long srcdir, long destdir, int recur);
static int copyfile(char *srcname, long srcdir, char *destname, long destdir);
static int file_date(struct DateStamp *date, char *name);
static void changedisk(struct MsgPort *task);
static int func_array( char *fav[], int fac);
static int func_int(int i);
static int func_bool(int i);
static int func_string(char *str);
static int commas(char *av[], int ac, int n);
static int wordset(char **av, int ac, char **(*func)(char **,int,char**,int,int*,int));
static int split_arg(char **av, int ac);
static char *info_part(char **av, int ac, int n, char *buf);
/* Casting conveniences */
#define BPTR_TO_C(strtag, var) ((struct strtag *)(BADDR( (ULONG) var)))
#define PROC(task) ((struct Process *)task)
#define CLI(proc) (BPTR_TO_C(CommandLineInterface, proc->pr_CLI))
/* Externs */
extern int has_wild; /* flag set if any arg has a ? or * */
int
do_abortline( void )
{
Exec_abortline = 1;
return 0;
}
int
do_error( void )
{
return atoi(av[1]);
}
int
do_return( void )
{
int retcode=(ac<2 ? 0 : atoi(av[1]));
Exec_abortline = 1;
if (Src_stack) {
Src_abort[Src_stack-1]=1;
return retcode;
} else
main_exit(retcode);
return 0;
}
/*
* STRHEAD
*
* place a string into a variable removing everything after and including
* the 'break' character
*
* strhead varname breakchar string
*
*/
int
do_strhead( void )
{
char *s;
if (*av[2] && (s=index( av[3], *av[2])))
*s='\0';
set_var (LEVEL_SET, av[1], av[3]);
return 0;
}
int
do_strtail( void )
{
char *s;
if (*av[2] && (s=index(av[3],*av[2]))) s++; else s=av[3];
set_var (LEVEL_SET, av[1], s);
return 0;
}
static long
dptrtosecs(DPTR *d)
{
struct DateStamp *ds=(&d->fib->fib_Date);
return ds->ds_Days*86400 + ds->ds_Minute*60 + ds->ds_Tick/TICKS_PER_SECOND;
}
static long
timeof(char *s)
{
DPTR *d;
int dummy;
long n;
if ( (d=dopen(s,&dummy))==NULL ) return 0L;
n=dptrtosecs(d);
dclose(d);
return n;
}
/*
* if -f file (exists) or:
*
* if A < B <, >, =, <=, >=, <>, where A and B are either:
* nothing
* a string
* a value (begins w/ number)
*/
#define IF_NOT 128
int
do_if( char *garbage, int com )
{
int result;
switch (com) {
case 0:
if (If_stack && If_base[If_stack - 1])
If_base[If_stack++] = 1;
else {
result=evalif();
If_base[If_stack++]=(options & IF_NOT ? result : !result);
}
break;
case 1:
if (If_stack > 1 && If_base[If_stack - 2]) break;
if (If_stack) If_base[If_stack - 1] ^= 1;
break;
case 2:
if (If_stack) --If_stack;
break;
}
disable = (If_stack) ? If_base[If_stack - 1] : 0;
if (If_stack >= MAXIF) {
fprintf(stderr,"If's too deep\n");
disable = If_stack = 0;
return -1;
}
return 0;
}
static int
isint( char *s )
{
while( ISSPACE(*s) ) ++s;
return *s=='+' || *s=='-' || *s>='0' && *s<='9';
}
static int
evalif( void )
{
char c, *str, *left, *right, *cmp;
long num, t0, i=1;
int nac=ac-1, err;
if(!strcmp(av[ac-1],"then")) ac--;
switch(options & ~IF_NOT) {
case 0:
for( i=1; i<ac; i++ )
if( strlen(str=av[i])<=2 && *str && index("!<=>",*str) &&
(!str[1] || index("<=>",str[1])))
break;
if ( i==ac )
return ac>1 && *av[1] && strcmp(av[1],"0");
left =av[1];
right=av[i+1];
if( 1+1!=i ) left = compile_av(av,1,i ,0xA0, 0);
if( i+1+1!=ac ) right= compile_av(av,i+1,ac,0xA0, 0);
cmp = av[i];
num = Atol(left) - Atol(right);
if( *cmp=='!' ) cmp++, options^=IF_NOT;
if( !isint(left) || !isint(right)) num=strcmp(left,right);
if( 1+1!=i ) free(left);
if( i+1+1!=ac ) free(right);
if (num < 0) c='<';
else if (num > 0) c='>';
else c='=';
return index(cmp, c) != NULL;
case 1:
return do_rpn(NULL,i);
case 2:
case 256:
return exists(av[i]);
case 4:
t0=timeof(av[i++]);
for ( ; i<ac ; i++)
if (t0<=timeof(av[i])) return 1;
return 0;
case 8:
return AvailMem( MEMF_FAST )!=0;
case 16:
return isdir(av[i])!=0;
case 32:
return get_var(LEVEL_SET,av[i]) != 0;
case 64:
if( ac>1 )
return get_opt(av+1,&nac,-*av[1],&err);
return 0;
default:
ierror(NULL,500);
}
return 0;
}
#if 0
do_while( void )
{
char *com=av[--ac];
int ret=0;
while( evalif() && ret==0 && !CHECKBREAK() )
ret=execute( com );
return ret;
}
#endif
int
do_label( void )
{
char aseek[32], *fwd;
if (Src_stack == 0) {
ierror (NULL, 502);
return -1;
}
sprintf (aseek, "%ld %d", Src_pos[Src_stack-1], If_stack);
set_var (LEVEL_LABEL + Src_stack - 1, av[1], aseek);
fwd=get_var(LEVEL_SET,v_gotofwd);
if (fwd && !strcmp(av[1],fwd))
forward_goto = 0;
return 0;
}
int
do_goto( void )
{
int new;
long pos;
char *lab;
if (Src_stack == 0) {
ierror (NULL, 502);
} else {
lab = get_var (LEVEL_LABEL + Src_stack - 1, av[1]);
if (lab == NULL) {
forward_goto = 1;
set_var (LEVEL_SET, v_gotofwd, av[1]);
return(0);
} else {
pos = atoi(lab);
fseek (Src_base[Src_stack - 1], pos, 0);
Src_pos[Src_stack - 1] = pos;
new = atoi(next_word(lab));
for (; If_stack < new; ++If_stack)
If_base[If_stack] = 0;
If_stack = new;
}
}
Exec_abortline = 1;
return (0); /* Don't execute rest of this line */
}
int
do_inc(char *garbage, int com)
{
char *var, num[32];
if (ac>2) com *= atoi(av[2]);
if (var = get_var (LEVEL_SET, av[1])) {
sprintf (num, "%d", atoi(var)+com);
set_var (LEVEL_SET, av[1], num);
}
return 0;
}
BPTR Input(void);
BPTR Output(void);
int
do_input( void )
{
char *str, in[256], *get, *put;
int i, quote=0;
if( options&2 ) {
if( !isconsole(Input()) ) return 20;
setrawcon( -1, 0 );
in[1]=0;
for ( i=1; i<ac && !CHECKBREAK(); ++i) {
Read( Input(), in, 1 );
set_var (LEVEL_SET, av[i], in);
}
setrawcon( 0, 0 );
return 0;
}
for ( i=1; i<ac; ++i)
if (fgets(in,256,stdin)) {
str=in, put=in+strlen(in)-1;
if( *put=='\n' ) *put=0;
if( !(options&1) ) {
while( *str==' ' ) str++;
for( put=get=str; *get; ) {
if( *get=='\"' )
quote=1-quote, get++;
else if( *get==' ' && !quote ) {
while( *get==' ' ) get++;
if( *get ) *put++=0xA0;
} else
*put++=*get++;
}
*put=0;
}
set_var (LEVEL_SET, av[i], str);
}
return 0;
}
int
do_ver( void )
{
extern char shellname[];
extern char shellcompiled[];
puts(shellname);
puts("1.00 Lattice (c) 1986 Matthew Dillon\n"
"2.05 Manx(M) versions by Steve Drew\n"
"3.00 ARP (A) versions by Carlo Borreo, Cesare Dieni\n"
"4.00 ARP 1.3 versions by Carlo Borreo, Cesare Dieni\n"
"5.00 Lattice versions by U. Dominik Mueller\n");
printf( shellcompiled );
return 0;
}
static int
clinum( char *name )
{
int ncli=(long)FindCLI(0L), count;
struct Task *task;
char cmd[40+1];
if( *name>='0' && *name<='9' )
return atoi( name );
Forbid();
for (count = 1; count <= ncli ; count++)
if (task = (struct Task *)FindCLI(count)) {
if ( !PROC(task)->pr_TaskNum || PROC(task)->pr_CLI == 0) continue;
BtoCStr(cmd, CLI(PROC(task))->cli_CommandName, 40L);
if( !Strcmp( BaseName( cmd ), BaseName( name )))
goto done;
}
count=-1;
done:
Permit();
return count;
}
int
do_ps( void )
{
/* this code fragment based on ps.c command by Dewi Williams */
int count; /* loop variable */
struct Task *task; /* EXEC descriptor */
char strbuf[64+1]; /* scratch for btocstr() */
char cmd[40+1], *com; /* holds cmd name */
long ncli,mycli,cli,i;
char onoff[80];
memset( onoff, 0, 80 );
for( i=1; i<ac; i++ )
onoff[ 1+clinum( av[i] ) ]=1;
if( options&2 )
for( i=0; i<80; i++ )
onoff[i]=1-onoff[i];
printf("Proc Command Name CLI Type Pri. Address Directory\n");
Forbid();
ncli=(long)FindCLI(0L);
mycli= Myprocess->pr_TaskNum;
for (count = 1; count <= ncli ; count++) /* or just assume 20?*/
if (task = (struct Task *)FindCLI((long)count)) {/* Sanity check */
cli=PROC(task)->pr_TaskNum;
if( ac>1 && !onoff[1+cli] )
continue;
if ( cli==0 || PROC(task)->pr_CLI == 0) continue; /* or complain? */
BtoCStr(cmd, CLI(PROC(task))->cli_CommandName, 40L);
BtoCStr(strbuf,CLI(PROC(task))->cli_SetName , 64L);
com=cmd;
if( !(options&1) )
com=BaseName(com);
printf("%c%2d %-20.20s %-11.11s %3d %8lx %s\n",
cli==mycli ? '*' : ' ',
count,
com,
task->tc_Node.ln_Name,
(signed char)task->tc_Node.ln_Pri,
task,
strbuf
);
}
Permit();
return 0;
}
/*
* CP [-d] [-u] file file
* CP [-d] [-u] file file file... destdir
* CP [-r][-u][-d] dir dir dir... destdir
*/
#define CP_RECUR 0x1
#define CP_UPDATE 0x2
#define CP_NODATE 0x4
#define CP_NOFLAGS 0x8
#define CP_FRESH 0x10
#define CP_MOVE 0x20
#define CP_QUIET 0x40
#define CP_OVERWRITE 0x80
static char *errstr; /* let's be a little more informative */
static int level;
int
do_copy( void )
{
int recur, ierr;
char *destname;
char destisdir;
FIB *fib;
int i;
errstr = "";
ierr = 0;
fib = (FIB *)SAllocMem((long)sizeof(FIB), MEMF_PUBLIC);
recur = OPT( CP_RECUR );
destname = av[ac - 1];
if (ac < 3) {
ierr = 500;
goto done;
}
destisdir = isdir(destname);
if (ac > 3 && !destisdir) {
ierr = 507;
goto done;
}
/*
* copy set: reduce to:
* file to file file to file
* dir to file (NOT ALLOWED)
* file to dir dir to dir
* dir to dir dir to dir
*
*/
for ( i=1; i<ac-1 && !dobreak(); ++i) {
short srcisdir = isdir(av[i]);
if (srcisdir && has_wild && (ac >2)) /* hack to stop dir's from */
continue; /* getting copied if specified */
/* from wild expansion */
if (srcisdir) {
BPTR srcdir, destdir;
if (!destisdir) {
if (exists(destname)) {
ierr = 507; /* disallow dir to file */
goto done;
}
if (destdir = CreateDir(destname)) UnLock(destdir);
destisdir = 1;
}
if (!(destdir = Lock(destname, ACCESS_READ))) {
ierr = 205;
errstr = destname;
goto done;
}
if (!(srcdir = Lock(av[i], ACCESS_READ))) {
ierr = 205;
errstr = av[i];
UnLock(destdir);
goto done;
}
ierr = copydir(srcdir, destdir, recur);
UnLock(srcdir);
UnLock(destdir);
if (ierr) break;
} else { /* FILE to DIR, FILE to FILE */
BPTR destdir, srcdir, tmp;
char *destfilename;
srcdir = (BPTR)(Myprocess->pr_CurrentDir);
if ((tmp = Lock(av[i], ACCESS_READ)) == NULL || !Examine(tmp,fib)) {
if (tmp) UnLock(tmp);
ierr = 205;
errstr = av[i];
goto done;
}
UnLock(tmp);
if (destisdir) {
destdir = Lock(destname, ACCESS_READ);
destfilename = fib->fib_FileName;
} else {
destdir = srcdir;
destfilename = destname;
}
ierr = copyfile(av[i], srcdir, destfilename, destdir);
if (destisdir) UnLock(destdir);
if (ierr) break;
}
}
done:
FreeMem(fib, sizeof(FIB));
if (ierr) {
ierror(errstr, ierr);
return 20;
}
return 0;
}
static int
copydir(BPTR srcdir, BPTR destdir, int recur)
{
BPTR cwd;
BPTR destlock, srclock;
char *srcname;
FIB *srcfib;
int ierr=0;
srcfib = (FIB *)SAllocMem((long)sizeof(FIB), MEMF_PUBLIC);
if( !Examine(srcdir, srcfib)) {
ierr=IoErr();
goto done;
}
srcname=srcfib->fib_FileName;
while (ExNext(srcdir, srcfib)) {
if (CHECKBREAK())
break;
if (srcfib->fib_DirEntryType < 0) {
ierr = copyfile(srcname,srcdir,srcname,destdir);
if (ierr) break;
} else {
if ( srcfib->fib_DirEntryType!=ST_USERDIR ) {
printf("%*s%s (Dir)....[Skipped. Is a link]\n", level * 6,
" ",srcname);
continue;
}
if (recur) {
cwd = CurrentDir(srcdir);
if (srclock = Lock(srcname, ACCESS_READ)) {
CurrentDir(destdir);
if (!(destlock = Lock(srcname,ACCESS_READ))) {
destlock = CreateDir(srcname);
printf("%*s%s (Dir)....[Created]\n", level * 6,
" ",srcname);
/* UnLock and re Lock if newly created
* for file_date() to work properly */
if (destlock)
UnLock(destlock);
destlock = Lock(srcname, ACCESS_READ);
} else
printf("%*s%s (Dir)\n", level * 6," ",srcname);
if (destlock) {
level++;
ierr = copydir(srclock, destlock, recur);
level--;
UnLock(destlock);
} else
ierr = IoErr();
UnLock(srclock);
if (!OPT(CP_NOFLAGS)) {
SetProtection(srcname,srcfib->fib_Protection );
if( *srcfib->fib_Comment )
SetComment( srcname, srcfib->fib_Comment );
}
} else {
ierr = IoErr();
}
CurrentDir(cwd);
if (ierr)
break;
}
}
}
done:
FreeMem(srcfib, sizeof(FIB));
return ierr;
}
#define COPYBUF 32768
static int
copyfile(char *srcname, BPTR srcdir, char *destname, BPTR destdir)
{
BPTR cwd;
BPTR f_src = NULL;
BPTR f_dest = NULL;
long dest_mode;
long j;
int stat,ierr=0,reset=0;
char *buf;
DPTR *dpd = NULL;
DPTR *dps;
long src_pbits;
errstr = "";
if ( !(buf= (char *)AllocMem(COPYBUF, MEMF_PUBLIC|MEMF_CLEAR)))
return ERROR_NO_FREE_STORE;
cwd = CurrentDir(srcdir);
if ( !(dps = dopen(srcname,&stat))) {
ierr = IoErr();
goto done;
}
UnLock(dps->lock);
dps->lock = NULL;
src_pbits = dps->fib->fib_Protection ;
/*
* ensure src file can be read - if not, force it so (maybe)
*/
if ( src_pbits & FIBF_READ) { /* not readable */
if (!OPT(CP_OVERWRITE)) {
errstr = srcname;
ierr = ERROR_READ_PROTECTED ;
goto done;
}
if (!SetProtection(srcname, 0)) { /* ----rwed */
pError(srcname);
goto done;
}
reset=1;
}
if ( (f_src=Open(srcname, MODE_OLDFILE)) == NULL) {
errstr = srcname;
ierr = IoErr();
goto done;
}
CurrentDir(destdir);
/*
* if dest does not exist, open it with 'rwed' protections
* if dest does exist, get its protections.
* if -o option , set its protections to enable a write
* (and reset them to original status when copied)
* if not -o , then error
*/
dest_mode = MODE_NEWFILE;
if( (dpd=dopen(destname, &stat)) != NULL) {
UnLock(dpd->lock);
dpd->lock = NULL;
if ( OPT(CP_OVERWRITE)) {
if (!SetProtection(destname, 0)) { /* ----rwed */
pError(destname);
goto done;
}
} else if( dpd->fib->fib_Protection & FIBF_WRITE ) { /* not writeable */
errstr = destname;
ierr = ERROR_WRITE_PROTECTED;
goto done;
}
}
/*
* check if copying necessary (update, fresh)
*/
if (OPT( CP_UPDATE | CP_FRESH )) {
if (!dpd && OPT(CP_FRESH)) {
if( !OPT(CP_QUIET))
printf("%*s%s....not there\n",level*6," ",srcname);
goto done;
} else if ( dpd && dptrtosecs(dpd) >= dptrtosecs(dps) &&
!Strcmp(dps->fib->fib_FileName, dpd->fib->fib_FileName)) {
if (!OPT(CP_QUIET))
printf("%*s%s....not newer\n",level*6," ",srcname);
goto done;
}
}
/*
* now open dest file
*/
if ( !(f_dest= Open(destname, dest_mode))) {
errstr = destname;
ierr = IoErr();
goto done;
}
printf("%*s%s..",level*6," ",srcname);
fflush(stdout);
/*
* now actually copy something :-)
*/
while (j = Read(f_src, buf, COPYBUF)) {
if( dobreak() ) {
ierr = 513;
break;
}
if (Write(f_dest, buf, j) != j) {
ierr = IoErr();
break;
}
}
Close(f_src);
Close(f_dest);
f_src= f_dest= NULL;
/*
* replace original src protection bits - maybe
*/
if ( reset ) {
CurrentDir( srcdir );
if (!SetProtection(srcname, src_pbits )) {
pError(srcname);
goto done;
}
CurrentDir( destdir );
}
if (!ierr) {
SetProtection( destname, src_pbits & ~FIBF_ARCHIVE);
if (!OPT(CP_NODATE))
file_date(&dps->fib->fib_Date, destname);
if (!OPT(CP_NOFLAGS)) {
if( *dps->fib->fib_Comment )
SetComment( destname, dps->fib->fib_Comment );
}
if( OPT(CP_MOVE) ) {
CurrentDir(srcdir);
DeleteFile(srcname);
printf("..moved\n");
} else
printf("..copied\n");
} else {
DeleteFile(destname);
}
done:
if ( f_src ) Close(f_src);
if ( f_dest ) Close(f_dest);
if ( dps ) dclose(dps);
if ( dpd ) dclose(dpd);
if ( buf ) FreeMem(buf, COPYBUF);
CurrentDir(cwd);
return ierr;
}
int
do_touch( void )
{
struct DateStamp ds;
int i;
DateStamp(&ds);
for (i=1; i<ac; i++)
if (file_date(&ds, av[i]))
ierror(av[i],500);
else
clear_archive_bit( av[i] );
return 0;
}
static int
file_date( struct DateStamp *date, char *name )
{
long packargs[7];
struct MsgPort *task;
DPTR *tmp;
BPTR dirlock;
char *ptr;
int stat;
if (!(task = (struct MsgPort *)DeviceProc(name))) return(1);
if (tmp = dopen(name, &stat)) {
dirlock = ParentDir(tmp->lock);
ptr=SAllocMem(65L,MEMF_PUBLIC);
CtoBStr(tmp->fib->fib_FileName,(ULONG)ptr >> 2L,64L);
dclose(tmp);
packargs[1]=dirlock;
packargs[2]=(ULONG)ptr >> 2L;
packargs[3]=(long)date;
SendPacket(ACTION_SET_DATE,packargs,task);
UnLock(dirlock);
FreeMem(ptr,65L);
}
return 0;
}
int
do_addbuffers( void )
{
long packargs[7], i;
struct MsgPort *task;
for( i=1; i<=ac-2; i+=2 ) {
if( i==ac-1 )
{ ierror( av[i], 500 ); return 20; }
if( !(task=(struct MsgPort *)DeviceProc(av[i])))
{ ierror(av[1],510); return 20; }
packargs[0]=myatoi(av[i+1],1,32767); if (atoierr) return 20;
SendPacket(ACTION_MORE_CACHE,packargs,task);
}
return 0;
}
int
do_relabel( void )
{
long packargs[7];
struct MsgPort *task=(struct MsgPort *)DeviceProc(av[1]);
char *ptr;
if (!task) { ierror(av[1],510); return 20; }
ptr=SAllocMem(65L,MEMF_PUBLIC);
CtoBStr(av[2],(ULONG)ptr >> 2L,64L);
packargs[0]=(ULONG)ptr >> 2L;
SendPacket(ACTION_RENAME_DISK,packargs,task);
FreeMem(ptr,65L);
Delay(10);
changedisk(task);
return 0;
}
int
do_diskchange( void )
{
struct MsgPort *task=(struct MsgPort *)DeviceProc(av[1]);
if (!task) { ierror(av[1],510); return 20; }
changedisk(task);
return 0;
}
static void
changedisk(struct MsgPort *task)
{
long packargs[7];
packargs[0]= DOSTRUE;
SendPacket(ACTION_INHIBIT,packargs,task);
packargs[0]= DOSFALSE;
SendPacket(ACTION_INHIBIT,packargs,task);
}
extern int atoierr;
static int
func_array( char *fav[], int fac)
{
char *ret;
if( atoierr ) return 20;
if( fac ) {
ret=compile_av( fav, 0, fac, 0xa0, 0);
set_var( LEVEL_SET, v_value, ret );
free( ret );
} else
unset_var( LEVEL_SET, v_value );
return 0;
}
static int
func_int( int i )
{
char buf[12];
if( atoierr ) return 20;
sprintf(buf,"%d",i);
set_var( LEVEL_SET, v_value, buf );
return 0;
}
static int
func_bool( int i )
{
if( atoierr ) return 20;
set_var( LEVEL_SET, v_value, i ? "1" : "0" );
return 0;
}
static int
func_string( char *str )
{
if( atoierr ) return 20;
set_var( LEVEL_SET, v_value, str ? str : "" );
return 0;
}
static int
commas( char *av[], int ac, int n )
{
int i=0;
while( --ac>=0 )
if( !strcmp( *av++, ",") )
i++;
if( i-=n )
fprintf( stderr, "Need %d comma%s\n", n, (n==1) ? "" : "s" );
return i;
}
static int
wordset( char *av[], int ac, char **(*func)(char **,int,char**,int,int*,int) )
{
char **av1=av, **av2;
int ac1=0, ac2, ret;
if( commas( av, ac, 1 ) ) return 20;
while( strcmp( *av++, ",") ) ac1++;
av2=av, ac2=ac-ac1-1;
av=(*func)( av1, ac1, av2, ac2, &ac, 0 );
ret=func_array( av, ac );
free( av );
return ret;
}
static int
split_arg( char **av, int ac )
{
char **arr, **old, *arg;
int i, j=1, ret;
for( i=strlen(av[0])-1; i>=0; i-- )
if( av[0][i]==' ' )
av[0][i]=0, j++;
arr=old=(char **)salloc( j*sizeof( char * ) );
arg=*av;
for( ; j>0; j-- ) {
*arr++=arg;
arg+=strlen(arg)+1;
}
ret=func_array( old, arr-old );
free(old);
return ret;
}
static char *
info_part( char **av, int ac, int n, char *buf )
{
char *str;
DPTR *dp;
int len=0, i, t;
buf[0]=0;
while( --ac>=0 ) {
if( dp=dopen( *av++, &t) ) {
if( n==0 ) {
for (str=buf, i=7; i>=0; i--)
*str++= (dp->fib->fib_Protection & (1L<<i) ?
"hspa----" : "----rwed")[7-i];
*str=0;
} else
len+= dp->fib->fib_NumBlocks+1;
dclose( dp );
}
}
if( n ) sprintf(buf, "%d", len);
return buf;
}
static struct FileRequester freq;
static char *
file_request(char **av, int ac, char *path)
{
struct FileRequester fr;
char filebuf[128];
path[0]=0; filebuf[0]=0;
fr=freq; /* clear everything */
fr.fr_Hail="";
if( ac>0 ) {
fr.fr_Hail=av[0];
if( ac>1 ) {
strcpy( path,av[1]);
if( ac>2 )
strcpy(filebuf,av[2]);
}
}
fr.fr_File = filebuf;
fr.fr_Dir = path;
fr.fr_Flags2= FR2F_LongPath;
if( !FileRequest( &fr ) )
return NULL;
TackOn( path,filebuf );
return path;
}
int
confirm( char *title, char *file )
{
char buf[80];
buf[0]=0;
if( !confirmed ) {
fprintf(stderr,"%s %s%-16s%s [YES/no/all/done] ? ",
title,o_hilite,file,o_lolite);
strupr(fgets(buf,80,stdin));
if( *buf=='A' )
confirmed=1;
if( *buf=='D' || breakcheck() )
confirmed=2;
}
if( confirmed==2 )
return 0;
return confirmed || *buf != 'N';
}
enum funcid {
FN_STUB=1, FN_MEMBER, FN_DIRS, FN_NAMEEXT, FN_NAMEROOT, FN_FILES,
FN_FILELEN, FN_SORTARGS, FN_UPPER, FN_WORDS, FN_ABBREV, FN_ABS,
FN_BINTODEC, FN_CENTER, FN_COMPARE, FN_DECTOHEX, FN_DELWORD,
FN_DELWORDS, FN_EXISTS, FN_INDEX, FN_STRCMP, FN_SUBWORDS,
FN_WORD, FN_MIN, FN_MAX, FN_DRIVES, FN_WITHOUT, FN_UNION, FN_INTERSECT,
FN_AVAILMEM, FN_UNIQUE, FN_RPN, FN_CONCAT, FN_SPLIT, FN_DRIVE,
FN_FILEPROT, FN_FILEBLKS, FN_LOWER, FN_HOWMANY, FN_COMPLETE, FN_FIRST,
FN_LAST, FN_MATCH, FN_CLINUM, FN_FREEBYTES, FN_FREEBLKS, FN_INFO,
FN_MEGS, FN_FREESTORE, FN_CHECKPORT, FN_PICKOPTS, FN_PICKARGS,
FN_FILEREQ, FN_VOLUME, FN_LOOKFOR, FN_APPSUFF, FN_DIRNAME, FN_AGE,
FN_GETCLASS, FN_CONFIRM, FN_WINWIDTH, FN_WINHEIGHT, FN_WINTOP,
FN_WINLEFT, FN_CONSOLE, FN_SORTNUM, FN_IOERROR, FN_TRIM, FN_MOUNTED,
FN_RND, FN_DIRSTR, FN_MIX, FN_WINROWS, FN_WINCOLS, FN_SUBFILE,
FN_SCRHEIGHT, FN_SCRWIDTH, FN_FLINES
};
#define MAXAV 30000 /* Max. # of arguments */
struct FUNCTION {
short id, minargs, maxargs;
char *name;
} Function[]={
FN_ABBREV, 2, 3, "abbrev",
FN_ABS, 1, 1, "abs",
FN_AGE, 1, 1, "age",
FN_APPSUFF, 2, 2, "appsuff",
FN_PICKARGS, 0, MAXAV, "arg",
FN_AVAILMEM, 0, 1, "availmem",
FN_STUB, 1, 1, "basename",
FN_CENTER, 2, 2, "center",
FN_CHECKPORT,1, 1, "checkport",
FN_CLINUM, 1, 1, "clinum",
FN_COMPLETE, 1, MAXAV, "complete",
FN_CONCAT, 0, MAXAV, "concat",
FN_CONFIRM, 1, MAXAV, "confirm",
FN_CONSOLE, 1, 1, "console",
FN_DECTOHEX, 1, 1, "dectohex",
FN_DELWORD, 1, MAXAV, "delword",
FN_DELWORDS, 2, MAXAV, "delwords",
FN_DIRNAME, 1, 1, "dirname",
FN_DIRS, 0, MAXAV, "dirs",
FN_DIRSTR, 2, 2, "dirstr",
FN_DRIVE, 1, 1, "drive",
FN_DRIVES, 0, 0, "drives",
FN_EXISTS, 1, 1, "exists",
FN_FILEBLKS, 1, MAXAV, "fileblks",
FN_FILELEN, 0, MAXAV, "filelen",
FN_FILEPROT, 1, 1, "fileprot",
FN_FILEREQ, 0, 3, "filereq",
FN_FILES, 0, MAXAV, "files",
FN_FIRST, 0, MAXAV, "first",
FN_FLINES, 1, 1, "flines",
FN_FREEBLKS, 1, 1, "freeblks",
FN_FREEBYTES,1, 1, "freebytes",
FN_FREESTORE,1, 1, "freestore",
FN_STUB, 1, 1, "getenv",
FN_GETCLASS, 1, 1, "getclass",
FN_HOWMANY, 0, 0, "howmany",
FN_IOERROR, 1, 1, "ioerr",
FN_INDEX, 2, 2, "index",
FN_INFO, 1, 1, "info",
FN_INTERSECT,1, MAXAV, "intersect",
FN_LAST, 0, MAXAV, "last",
FN_LOOKFOR, 2, 2, "lookfor",
FN_LOWER, 0, MAXAV, "lower",
FN_MATCH, 1, MAXAV, "match",
FN_MAX, 1, MAXAV, "max",
FN_MEGS, 1, 1, "megs",
FN_MEMBER, 1, MAXAV, "member",
FN_MIN, 1, MAXAV, "min",
FN_MIX, 0, MAXAV, "mix",
FN_MOUNTED, 1, 1, "mounted",
FN_NAMEEXT, 1, 1, "nameext",
FN_NAMEROOT, 1, 1, "nameroot",
FN_PICKOPTS, 0, MAXAV, "opt",
FN_DIRNAME, 1, 1, "pathname",
FN_PICKARGS, 0, MAXAV, "pickargs",
FN_PICKOPTS, 0, MAXAV, "pickopts",
FN_RND, 0, 1, "rnd",
FN_RPN, 1, MAXAV, "rpn",
FN_SCRHEIGHT,0, 0, "scrheight",
FN_SCRWIDTH, 0, 0, "scrwidth",
FN_SORTARGS, 0, MAXAV, "sortargs",
FN_SORTNUM, 0, MAXAV, "sortnum",
FN_SPLIT, 0, MAXAV, "split",
FN_STRCMP, 2, 2, "strcmp",
FN_STUB, 2, 2, "strhead",
FN_STUB, 2, 2, "strleft",
FN_STUB, 2, 3, "strmid",
FN_STUB, 2, 2, "strright",
FN_STUB, 2, 2, "strtail",
FN_SUBFILE, 3, 3, "subfile",
FN_SUBWORDS, 2, MAXAV, "subwords",
FN_STUB, 2, 2, "tackon",
FN_TRIM, 0, MAXAV, "trim",
FN_UNION, 1, MAXAV, "union",
FN_UNIQUE, 0, MAXAV, "unique",
FN_UPPER, 0, MAXAV, "upper",
FN_VOLUME, 1, 1, "volume",
FN_WINCOLS, 0, 0, "wincols",
FN_WINHEIGHT,0, 0, "winheight",
FN_WINLEFT, 0, 0, "winleft",
FN_WINROWS, 0, 0, "winrows",
FN_WINTOP, 0, 0, "wintop",
FN_WINWIDTH, 0, 0, "winwidth",
FN_WITHOUT, 1, MAXAV, "without",
FN_WORD, 1, MAXAV, "word",
FN_WORDS, 0, MAXAV, "words",
0, 0, NULL
};
extern char shellctr[];
extern int w_width, w_height;
int
dofunc( int id, char **av, int ac)
{
char **oldav=av, **get=av, buf[200], *str=buf, *t;
int oldac=ac, i=0, j=0, n=0, n2=1, l;
buf[0]=0;
av[ac]=NULL;
atoierr=0;
switch( id ) {
case FN_ABBREV:
if( ac==3 ) i=posatoi(av[2] ); else i=strlen(av[0]);
return func_bool( !Strncmp( av[0], av[1], i ));
case FN_ABS:
i=unlatoi(av[0]);
return func_int( i>= 0 ? i : -i );
case FN_AGE: {
struct DateStamp ds; long time;
DateStamp( &ds ); if( ds.ds_Days==0 ) return 99999;
if( !(time=timeof(av[0]))) return 99999;
return func_int( (ds.ds_Days*86400+ds.ds_Minute*60-time)/86400 ); }
case FN_APPSUFF:
strcpy(buf,av[0]);
l=strlen(av[0])-strlen(av[1]);
if( l<0 || Strcmp(av[0]+l,av[1])) {
strcat(buf,".");
strcat(buf,av[1]);
}
return func_string( buf );
case FN_AVAILMEM:
if( ac==1 && !Strcmp(av[0],"chip")) n=MEMF_CHIP;
if( ac==1 && !Strcmp(av[0],"fast")) n=MEMF_FAST;
return func_int( AvailMem( n ));
case FN_CENTER:
if( (n=posatoi( av[1] )) > (l=strlen(av[0])) ) i=(n-l)/2, j=n-i-l;
sprintf( buf, "%*s%s%*s", i,"",av[0], j,"" );
return func_string( buf );
case FN_CHECKPORT:
return func_bool( (int)FindPort( av[0] ) );
case FN_GETCLASS:
if( str=getclass(av[0]) )
if( str=index(strncpy( buf,str,100 ),0xA0) )
*str=0;
return func_string(buf);
case FN_COMPLETE:
for( i=1, l=strlen(av[0]); i<ac; i++ )
if( !Strncmp( av[0], av[i], l ) )
{ str=av[i]; break; }
return func_string( str );
case FN_CONCAT:
return func_string( compile_av( av, 0, ac, ' ', 1));
case FN_CONFIRM:
for( i=1, get++, confirmed=0; i<ac; i++ )
if( confirm( av[0], av[i]) )
*get++=av[i];
return func_array( av+1, (get-av)-1 );
case FN_CONSOLE:
if( !strcmp(av[0],"STDIN")) i=isconsole(Input());
else if( !strcmp(av[0],"STDOUT")) i=isconsole(Output());
return func_bool(i);
case FN_DECTOHEX:
sprintf( buf, "%x", unlatoi( av[0] ));
return func_string( buf );
case FN_DELWORDS:
n2=posatoi( av[--ac] ); if( atoierr ) return 20;
case FN_DELWORD:
n=posatoi( av[--ac] )-1;
for( ; i<ac && i<n; i++ ) *av++=*get++;
for( ; i<ac && i<n+n2; i++ ) get++;
for( ; i<ac ; i++ ) *av++=*get++;
return func_array( oldav, av-oldav );
case FN_DIRNAME:
str=av[0]+strlen(av[0])-1;
while( str>av[0] && *str!=':' && *str!='/' ) str--;
if( *str==':' ) str++;
*str=0;
return func_string(av[0]);
case FN_DIRS:
for( ; --ac>=0; get++ )
if( exists( *get ) && isdir( *get ) )
*av++=*get;
return func_array( oldav, av-oldav );
case FN_DIRSTR:
if( av=expand(oldav[1],&n2)) {
lformat(oldav[0], buf, (FILEINFO *)av[0]-1);
free_expand(av);
}
if( str=index(buf,'\n' )) *str=0;
return func_string( buf );
case FN_DRIVE:
return func_string( drive_name( av[0] ) );
case FN_DRIVES:
get_drives( buf );
return func_string( buf );
case FN_EXISTS:
return func_bool( exists( av[0] ));
case FN_FILEBLKS:
return func_string( info_part( av, ac, 1, buf ) );
case FN_FILELEN:
while( --ac>=0 )
i+=filesize( *av++ );
return func_int( i );
case FN_FLINES:
if( str=get_var(LEVEL_SET, av[0]))
for( n=1; str=index(str,0xA0); str++, n++ ) ;
return func_int( n );
case FN_FILEPROT:
return func_string( info_part( av, ac, 0, buf ) );
case FN_FILEREQ:
return func_string( file_request( av, ac, buf ) );
case FN_FILES:
for( ; --ac>=0; get++ )
if( exists( *get ) && !isdir( *get ) )
*av++=*get;
return func_array( oldav, av-oldav );
case FN_FIRST:
return func_string( av[0] );
case FN_FREEBLKS:
return func_string( oneinfo( av[0], 3 ));
case FN_FREEBYTES:
return func_string( oneinfo( av[0], 2 ));
case FN_FREESTORE:
return func_string( oneinfo( av[0], 4 ));
case FN_HOWMANY:
Getenv( shellctr, buf, 10);
return func_string( buf );
case FN_IOERROR:
return func_string( ioerror( atoi( av[0] )));
case FN_INDEX:
str=strstr( av[0], av[1] );
return func_int( str ? (str-av[0])+1 : 0 );
case FN_INFO:
return func_string( oneinfo( av[0], 1 ));
case FN_INTERSECT:
return wordset( av, ac, and );
case FN_LAST:
return func_string( ac ? av[ac-1] : "" );
case FN_LOOKFOR:
return func_string( dofind( av[0], "", buf, av[1]));
case FN_LOWER:
while( --ac>=0 ) strlwr( *av++ );
return func_array( oldav, av-oldav );
case FN_MATCH:
{ PATTERN *pat=compare_preparse( av[--ac], 0 );
for( ; --ac>=0; get++ )
if( compare_ok( pat, *get ) )
*av++=*get;
compare_free(pat);
return func_array( oldav, av-oldav );
}
case FN_MAX:
for( n=MININT; i<ac; i++ )
{ if( (j=unlatoi(av[i] )) > n ) n=j; if( atoierr ) return 20; }
return func_int( n );
case FN_MEGS:
return func_string( itok( atoi( av[0] )));
case FN_MEMBER:
for( i=1; i<ac && Strcmp(av[0],av[i]) ; i++ ) ;
return func_bool( ac!=i );
case FN_MIN:
for( n=MAXINT; i<ac; i++ )
{ if( (j=unlatoi(av[i] )) < n ) n=j; if( atoierr ) return 20; }
return func_int( n );
case FN_MIX:
for( ; i<ac; i++ )
j=rand()%ac, str=av[j], av[j]=av[i], av[i]=str;
return func_array(av,ac);
case FN_MOUNTED:
return func_bool((int)mounted(av[0]));
case FN_NAMEEXT:
return func_string( rindex(av[0],'.')?rindex(av[0],'.')+1:(char *)NULL);
case FN_NAMEROOT:
if( rindex(av[0],'.') ) *rindex(av[0],'.')=0;
return func_string( av[0] );
case FN_PICKARGS:
while( *get && **get=='-' ) get++;
while( *get ) *av++=*get++ ;
return func_array( oldav, av-oldav );
case FN_PICKOPTS:
while( *get && **get=='-' ) *av++=*get++;
return func_array( oldav, av-oldav );
case FN_CLINUM:
return func_int( clinum( av[0] ) );
case FN_RND:
/* if( ac>0 ) srand(atoi(av[0])); */
return func_int( rand() );
case FN_RPN:
return func_int( eval_rpn( av, ac, 1 ));
case FN_SCRHEIGHT:
return func_int( Win ? Win->WScreen->Height: 0 );
case FN_SCRWIDTH:
return func_int( Win ? Win->WScreen->Width : 0 );
case FN_SORTARGS:
QuickSort( av, ac );
return func_array( av, ac );
case FN_SORTNUM:
DirQuickSort( av, ac, numcmp, 0, 0 );
return func_array( av, ac );
case FN_SPLIT:
return split_arg( av, ac );
case FN_STRCMP:
return func_int( strcmp( av[0], av[1] ) );
case FN_TRIM:
for( ; *av; av++ ) {
for( ; **av==' '; (*av)++ ) ;
for( str=*av+strlen(*av); str>*av && str[-1]==' '; *--str=0 ) ;
}
return func_array( oldav, av-oldav );
case FN_UNION:
return wordset( av, ac, or );
case FN_UNIQUE:
QuickSort( av, ac );
while( *get )
{ *av++=*get++; while( *get && !Strcmp(*get,*(get-1)))
get++; }
return func_array( oldav, av-oldav );
case FN_UPPER:
while( --ac>=0 ) strupr( *av++ );
return func_array( oldav, oldac );
case FN_VOLUME:
return func_string( oneinfo( av[0], 5 ));
case FN_WINCOLS:
newwidth();
return func_int(w_width);
case FN_WINTOP:
return func_int( Win ? Win->TopEdge : 0 );
case FN_WINLEFT:
return func_int( Win ? Win->LeftEdge: 0 );
case FN_WINHEIGHT:
return func_int( Win ? Win->Height : 0 );
case FN_WINROWS:
newwidth();
return func_int(w_height);
case FN_WINWIDTH:
return func_int( Win ? Win->Width : 0 );
case FN_WORD:
n2=1; goto wordf;
case FN_SUBFILE:
n2= posatoi( av[2] ); if( atoierr ) return 20;
n = posatoi( av[1] ); if( atoierr ) return 20;
if( !(str=get_var( LEVEL_SET, av[0] ))) return 20;
for( i=1; i<n; i++ )
str= (str=index(str,0xA0)) ? str+1 : "";
for( t=str, i=0; i<n2; i++ )
if(t=index(t+1,0xA0));
if( t ) *t=0;
set_var( LEVEL_SET, v_value, str );
if( t ) *t=0xA0;
return 0;
case FN_SUBWORDS:
n2=posatoi( av[--ac] ); if( atoierr ) return 20;
wordf:
n=posatoi( av[--ac] )-1; if( atoierr ) return 20;
for( i=0; i<ac && i<n; i++ ) get++;
for( ; i<ac && i<n+n2; i++ ) *av++=*get++;;
return func_array( oldav, av-oldav );
case FN_WITHOUT:
return wordset( av, ac, without );
case FN_WORDS:
return func_int( ac );
}
return func_string( "" );
}