home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 1: Amiga
/
FrozenFish-Apr94.iso
/
bbs
/
alib
/
d1xx
/
d199
/
csh.lha
/
Csh
/
comm3.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-03-31
|
10KB
|
543 lines
/*
* COMM3.C
*
* Version 3.02A by Carlo Borreo & Cesare Dieni 20-Dec-88
*
*/
do_assign() {
switch(ac) {
case 1: assignlist();
break;
case 2: doassign(av[1], NULL);
break;
case 3: doassign(av[1], av[2]);
break;
default: ierror(NULL, 500);
break;
}
return 0;
}
char *assign_errors[4]={
"",
"Name %s is not valid\n",
"Weird error\n",
"Can't cancel %s\n"
};
doassign(log, phy)
char *log, *phy;
{
int last=strlen(log) - 1;
if (log[last] != ':') fprintf(stderr, "Bad name %s\n", log);
else {
log[last] = 0;
fprintf(stderr,assign_errors[Assign(log, phy)],log);
}
}
assignlist()
{
struct DirectoryEntry *de_head=NULL, *de;
char buf[256];
BPTR lock;
int ctr=0;
AddDADevs(&de_head, DLF_DEVICES | DLF_VOLUMES | DLF_DIRS);
printf("Devices:\n");
for (de=de_head; de && de->de_Type==DLX_DEVICE; de=de->de_Next) {
printf("%-8s",de->de_Name);
if (ctr++ == 5) { ctr=0; printf("\n"); }
}
printf("\n\nVolumes:\n");
for ( ;
de && (de->de_Type==DLX_VOLUME || de->de_Type==DLX_UNMOUNTED);
de=de->de_Next
)
printf( "%-16s %s\n",
de->de_Name,
de->de_Type == DLX_VOLUME ? "[Mounted]" : ""
);
printf("\nDirectories:\n");
for (; de && de->de_Type==DLX_ASSIGN; de=de->de_Next) {
if (lock=Lock(de->de_Name, ACCESS_READ)) {
PathName(lock, buf, 256L);
UnLock(lock);
}
else
strcpy(buf,"Unexisting lock");
printf("%-20s%s\n",de->de_Name,buf);
}
FreeDAList(&de_head);
}
do_join()
{
BPTR sou, dest;
char *buffer;
unsigned int i;
long n;
char *namedest=av[--ac];
get_opt("r", &i);
if (options==0 && exists(namedest)) { ierror(namedest,203); return 20; }
if ( (buffer=malloc(8192)) == NULL ) { ierror(NULL,103); return 20; }
if ( (dest=Open(namedest, MODE_NEWFILE)) == NULL )
{ pError(namedest); goto fail1; }
for (i=1; i<ac; i++) {
if ( (sou=Open(av[i], MODE_OLDFILE)) == NULL ) pError(av[i]);
else
while( (n=Read(sou, buffer, 8192L)) > 0 )
if (Write(dest, buffer, n) != n)
{ pError(namedest); Close(sou); goto fail2; }
Close(sou);
}
fail2:
Close(dest);
fail1:
free(buffer);
return 0;
}
#define BUFDIM 512L
#define MAXSTR 256
int minstr;
strings_in_file(s)
char *s;
{
char c;
char readbuf[BUFDIM+1], strbuf[MAXSTR+1];
register unsigned int i, strctr=0;
BPTR fh;
int out, n;
if ( fh=Open(s, MODE_OLDFILE) ) {
fprintf(stderr, "Strings in %s (len>=%d):\n",s,minstr);
while ( (n=(int)Read(fh, readbuf, BUFDIM)) > 0 && !CHECKBREAK() )
for (i=0; i<n; i++) {
c=readbuf[i];
if (c<0x20 || c>0x7f) {
out=(strctr>=minstr);
if (!out) strctr=0;
}
else {
strbuf[strctr++]=c;
out=(strctr>=BUFDIM);
}
if (out) {
strbuf[strctr]='\0';
puts(strbuf);
strctr=0;
}
}
Close(fh);
}
else pError(s);
}
do_strings()
{
minstr=myatoi(av[--ac],1,255);
all_args("r", strings_in_file, 0);
return 0;
}
BPTR myfile[MAXMYFILES];
do_open()
{
long mode;
unsigned int n;
switch (av[2][0]) {
case 'r': mode=MODE_OLDFILE; break;
case 'w': mode=MODE_NEWFILE; break;
default : ierror(NULL,500); return;
}
Errno=0;
n=(unsigned int)myatoi(av[3],0,MAXMYFILES); if (Errno) return 20;
myfile[n]=Open(av[1],mode);
return (myfile[n]==NULL);
}
do_close()
{
register unsigned int i;
int n;
for (i=1; i<ac; i++) {
Errno=0;
n=myatoi(av[i],0,MAXMYFILES); if (Errno) return 20;
myclose(n);
}
return 0;
}
myclose(n)
{
if (myfile[n]) { Close(myfile[n]); myfile[n]=NULL; }
}
do_fileslist()
{
register unsigned short i;
int flag=0;
printf("Open files:");
for (i=0; i<MAXMYFILES; i++)
if (myfile[i]) { printf(" %d",i); flag=1; }
if (!flag) printf(" None!");
printf("\n");
return 0;
}
BPTR extOpen(name,mode)
char *name;
long mode;
{
if (name[0]=='.') return myfile[atoi(name+1)];
return Open(name,mode);
}
extClose(fh)
BPTR fh;
{
register unsigned short i;
for (i=0; i<MAXMYFILES; i++)
if (myfile[i]==fh) return;
Close(fh);
}
do_resident(avline)
char *avline;
{
unsigned int i;
BPTR seg;
struct ResidentPrgNode *p;
char *args;
get_opt("ar", &i);
switch (options) {
case 0:
ObtainSemaphore (& (ArpBase->ResPrgProtection) );
if (p=ArpBase->ResidentPrgList) {
printf("Name Users\n");
for (; p; p=p->rpn_Next)
printf("%-16s %-3ld\n",p->rpn_Name,p->rpn_Usage);
}
else printf("No resident program(s)\n");
ReleaseSemaphore(& (ArpBase->ResPrgProtection) );
break;
case 1:
for (; i<ac; i++)
if ( (seg=(BPTR)LoadPrg(av[i])) && AddResidentPrg(seg,av[i]) )
printf("OK! %s is now resident\n", BaseName(av[i]));
else pError(av[i]);
break;
case 2:
for (; i<ac; i++)
if (RemResidentPrg(av[i])) ierror(av[i],202);
else printf("Removed %s\n",av[i]);
break;
default:
ierror(NULL,500);
break;
}
return 0;
}
struct ProcessControlBlock pcb={
4000, /* pcb_StackSize */
0, /* pcb_Pri */
};
/* remaining field are NULL */
do_truerun(avline, backflag)
char *avline;
{
char name[200];
char *FindIt();
if (backflag) {
pcb.pcb_Control=NULL;
pcb.pcb_Input=pcb.p_Output=Open("NIL:",MODE_OLDFILE);
}
else {
pcb.pcb_Control=PRB_SAVEIO;
pcb.pcb_Input=pcb.p_Output =NULL;
}
if (FindIt(av[1], "", name))
ASyncRun(name,next_word(next_word(avline)),&pcb);
else
ierror(av[1],205);
return 0;
}
int exists(name)
char *name;
{
BPTR lock;
if (lock=Lock(name,ACCESS_READ)) {
UnLock(lock);
return 1;
}
return 0;
}
do_aset()
{
Setenv(av[1],av[2]);
return 0;
}
#define HTYPELINE 16L
htype_a_file(s)
char *s;
{
BPTR fh;
long n, filesize=0;
char buf[HTYPELINE+1];
register unsigned int i;
if ( (fh=Open(s,MODE_OLDFILE))==NULL ) { pError(s); return 20; }
while ( (n=Read(fh,buf,HTYPELINE))>0 && !dobreak()) {
printf("%06lx: ",filesize);
filesize+=n;
for (i=0; i<n; i++) {
printf( (i&3) ? "%02x" : " %02x",(int)(unsigned char)buf[i]);
if (buf[i]<=0x20) buf[i]='.';
}
for ( ; i<HTYPELINE; i++) {
printf( (i&3) ? " " : " ");
buf[i]=' ';
}
buf[i]=0;
printf(" %s\n",buf);
}
Close(fh);
return 0;
}
do_htype()
{
all_args("", htype_a_file, 0);
return 0;
}
do_stack()
{
long n;
if (ac>1) {
Errno=0;
n=Atol(av[1]);
if (!Errno) Mycli->cli_DefaultStack=(long)(n >> 2L);
}
else printf("current stack size is %ld bytes\n",
(long)Mycli->cli_DefaultStack << 2L);
return 0;
}
do_fault()
{
struct PERROR *p;
register unsigned int i;
int n;
for (i=1; i<ac; i++) {
n=myatoi(av[i],0,32767);
if (!Errno) {
for (p=Perror; p->errnum && p->errnum!=n; p++);
if (p->errnum)
printf("Fault %d: %s\n",n,p->errstr);
else
printf("Fault %d not recognized\n",n);
}
}
return 0;
}
struct rpncommand {
char *str;
int parsin, parsout;
};
struct rpncommand rpn[]={
"+", 2, 1,
"-", 2, 1,
"*", 2, 1,
"/", 2, 1,
"%", 2, 1,
"&", 2, 1,
"|", 2, 1,
"~", 1, 1,
">", 2, 1,
"<", 2, 1,
"==", 2, 1,
"!", 1, 1,
"DUP", 1, 2,
"DROP", 1, 0,
"SWAP", 2, 2,
"HELP", 0, 0,
NULL, 0, 1, /* this looks for a number */
};
do_rpn(garbage,ifflag) /* ifflag!=0 if called from if */
char *garbage;
{
register long n0, n1;
long t;
unsigned int i, j;
int sp=0;
long stack[100];
struct rpncommand *temp;
i=1;
if (ifflag) get_opt("rn",&i);
for (; i<ac; i++) {
for (j=0; rpn[j].str && Strcmp(rpn[j].str,av[i]); j++) ;
n0=stack[sp-1];
n1=stack[sp-2];
sp -= (rpn[j].parsin);
if (sp<0) { fprintf(stderr, "RPN: Empty stack\n"); return 1; }
switch (j) {
case 0: n0 += n1; break;
case 1: n0 = n1-n0; break;
case 2: n0 *= n1; break;
case 3: n0 = n1/n0; break;
case 4: n0 = n1%n0; break;
case 5: n0 &= n1; break;
case 6: n0 |= n1; break;
case 7: n0 = ~n0; break;
case 8: n0 = (n1 > n0); break;
case 9: n0 = (n1 < n0); break;
case 10: n0 = (n0 == n1); break;
case 11: n0 = !n0; break;
case 12: n1=n0; break;
case 13: t=n0; n0=n1; n1=t; break;
case 14: break;
case 15: printf("In Commands Out\n");
for (temp=rpn; temp->str; temp++)
printf(" %d %-10s%d\n",
temp->parsin,temp->str,temp->parsout);
break;
default: Errno=0;
n0=Atol(av[i]);
if (Errno) {
fprintf(stderr, "Bad RPN cmd: %s\n",av[i]);
return 20;
}
break;
}
stack[sp]=n0;
stack[sp+1]=n1;
sp += rpn[j].parsout;
}
if (ifflag) return (int)(stack[sp-1]); /* called from if: return top value */
for (i=sp-1;(int)i>=0;i--) printf("%ld\n", stack[i]); /* else print stack */
return 0;
}
do_path()
{
union { long *lp; long ll; } l;
BPTR lock;
char buf[256];
puts("Current dir");
l.lp = (long *) Mycli->cli_CommandDir;
while (l.ll) {
l.ll <<= 2;
PathName(l.lp[1], buf, 256L);
puts(buf);
l.ll = *l.lp;
}
puts("C:");
return 0;
}
do_pri()
{
int t, pri;
struct Process *proc;
t=myatoi(av[1],0,20); if (Errno) return 20;
pri=myatoi(av[2],-128,127); if (Errno) return 20;
Forbid();
proc=(t==0 ? Myprocess : FindCLI((long)t));
if (proc==NULL) fprintf(stderr, "process not found\n");
else SetTaskPri(proc, (long)pri);
Permit();
return 0;
}
do_strleft()
{
char buf[256];
int n;
strcpy(buf,av[2]);
n=myatoi(av[3],1,strlen(buf)); if (Errno) return 20;
buf[n]='\0';
set_var(LEVEL_SET, av[1], buf);
return 0;
}
do_strright()
{
char buf[256];
int n;
strcpy(buf, av[2]);
n=myatoi(av[3],1,strlen(buf)); if (Errno) return 20;
set_var(LEVEL_SET, av[1], buf+strlen(buf)-n);
return 0;
}
do_strmid()
{
char buf[256];
int n1, n2;
strcpy(buf, av[2]);
n1=myatoi(av[3],1,strlen(buf))-1; if (Errno) return 20;
if (ac>4) {
n2=myatoi(av[4],1,strlen(buf)-n1);
if (Errno) return 20;
buf[n1+n2]='\0';
}
set_var(LEVEL_SET, av[1], buf+n1);
return 0;
}
do_strlen()
{
char buf[16];
sprintf(buf,"%d",strlen(av[2]));
set_var(LEVEL_SET, av[1], buf);
return 0;
}
myatoi(s,min,max)
char *s;
{
long n;
Errno=0;
n=Atol(s);
if (Errno==ERRBADINT) ierror(s,511);
else if (n<min || n>max) {
Errno=ERRBADINT;
printf("%s not in (%d,%d)\n",s,min,max);
}
return (int)n;
}