home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
util
/
csh-5.39.lha
/
Csh
/
src
/
comm3.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-09-06
|
59KB
|
2,857 lines
/*
* COMM3.C
*
* Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
* Version 5.00L by Urban Mueller 17-Feb-91
* Version 5.20L by Andreas M. Kirchwitz (Fri, 13 Mar 1992)
*
*/
#include "shell.h"
/* comm3.c */
static void doassign(char *log, char *phy);
static void assignlist(void);
static int strings_in_file(long mask, char *s, char *path);
static int htype_a_file (long mask, char *s, char *path);
static void install_menu(char **mav, int mac);
static int line_filter( char *(*line)(char *) );
/* 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))
int do_ln(void)
{
char *linkname;
long link_type;
long dest_data;
BOOL success;
if( ac!=2 && ac!=3 ) {
show_usage( NULL );
return 20;
}
if (ac==2)
linkname = FilePart(av[1]);
else
linkname = av[2];
if (options&1) {
link_type = LINK_SOFT;
dest_data = (long)av[1];
}
else {
link_type = LINK_HARD;
if (!( dest_data = (long)Lock(av[1],ACCESS_READ) )) {
pError(av[1]);
return 20;
}
}
success = MakeLink(linkname,dest_data,link_type);
if (link_type==LINK_HARD)
UnLock((BPTR)dest_data);
if (!success) {
pError(linkname);
return 20;
}
return 0;
}
do_tee( void )
{
char buf[256];
FILE *out;
prepscroll( ac==1 );
if( ac>2 ) { ierror( av[2], 500 ); return 20; }
if( ac==1 ) out=stderr;
else if( !(out=fopen( av[1], "w" ))) { pError( av[1] ); return 20; }
while (safegets(buf,stdin)) {
puts(buf);
quickscroll();
fprintf(out,"%s\n",buf);
}
if( ac!=1 ) fclose( out );
return 0;
}
do_head( char *garbage, int com )
{
FILE *f;
int i, n = 10;
char buf[256];
if (ac==1) { /* use stdin */
f = stdin;
}
else {
f=fopen(av[1],"r");
if (f==NULL) {
if (ac==2 && isnum(av[1])) { /* use stdin */
f = stdin;
n = atol(av[1]);
}
else {
pError(av[1]);
return 20;
}
}
}
if (ac>2) {
if (!isnum(av[2])) { /* AMK: IoErr() replaced by isnum() */
ierror(av[2],511);
return 20;
}
n=(int)atol(av[2]);
}
if (com) { /* tail specific part */
i=0;
while (fgets(buf, 256, f) && ! dobreak()) i++;
rewind(f);
if (n>i) n=i;
i=i-n;
while (i-- && fgets(buf, 256, f) && ! dobreak()) ;
}
for (i=1; i<=n && fgets(buf, 256, f) && ! dobreak(); i++)
printf("%s", buf);
if (f!=stdin)
fclose(f);
return 0;
}
#if 0
do_head( char *garbage, int com )
{
int i, n;
FILE *f;
char buf[256];
if (ac>2) {
if (!isnum(av[2])) { /* AMK: IoErr() replaced by isnum() */
ierror(av[2],511);
return 20;
}
n=(int)atol(av[2]);
} else n=10;
f=fopen(av[1], "r");
if (f==NULL) {
pError(av[1]);
return 20;
}
if (com) { /* tail specific part */
i=0;
while (fgets(buf, 256, f) && ! dobreak()) i++;
rewind(f);
if (n>i) n=i;
i=i-n;
while (i-- && fgets(buf, 256, f) && ! dobreak()) ;
}
for (i=1; i<=n && fgets(buf, 256, f) && ! dobreak(); i++)
printf("%s", buf);
fclose(f);
return 0;
}
#endif
static int
exword( char **src, char *buf )
{
*buf=0;
if( **src==0 ) return 0;
while( **src && **src!=0xA0 )
*buf++ = *(*src)++;
*buf=0;
if( **src ) (*src)++;
return 1;
}
static char helpfound=0;
void
man (FILE *f, char *s)
{
char buf[140], entry[100];
int len = sprintf (entry, " %s", s);
char *example = " ";
prepscroll (0);
rewind (f);
do { /* look for required argument */
if (fgets (buf, sizeof(buf), f)==NULL || dobreak())
return; /* GMD */
} while (strnicmp (entry, buf, len)) ;
helpfound = 1;
do { /* display help */
if (dobreak())
return;
quickscroll ();
printf ("%s", buf);
if (fgets (buf, sizeof(buf), f) == NULL)
return;
} while ((!isalphanum (*buf)) && strncmp (buf, example, strlen(example))) ;
}
do_man( void )
{
FILE *f;
int i;
char buf[200], name[60], *src, *var, docfound=0;
buf[0]=0;
if( var=get_var(LEVEL_SET,"_man" ) )
strcpy(buf,var);
if (ac==1) ac=2, av[1]="MAN";
for (i=1; i<ac; i++) {
src=buf, helpfound=0;
while( exword( &src, name) )
if( f=fopen(name, "r") ) {
docfound=1;
man(f, av[i]);
fclose(f);
if( helpfound )
break;
}
if( !docfound )
fprintf(stderr,"%s not found\n",buf);
else if( !helpfound )
fprintf(stderr, "Help not found for %s\n", av[i]);
}
return 0;
}
do_assign( void )
{
int i;
if ( ac==1 ) assignlist();
else if( ac==2 ) doassign(av[1], NULL);
else if( !(ac&1) ) ierror(NULL, 500);
else
for( i=1; i<ac; i+=2 )
doassign( av[i],av[i+1] );
return 0;
}
/* AMK: rewritten code, removed bug when strlen(log) was 0 */
static void
doassign(char *log, char *phy)
{
int last=strlen(log);
if (last<2 || log[last-1] != ':') fprintf(stderr, "Bad name %s\n", log);
else {
int succ=0;
log[last-1] = 0;
if (!phy)
succ=AssignLock(log,NULL);
else if (options&1) { /* add assign, CLI: assign ADD */
BPTR lock;
if( lock=Lock(phy,ACCESS_READ) )
if( !(succ=AssignAdd(log,lock)))
UnLock(lock);
}
/* late-binding assign, CLI: assign DEFER */
else if (options&2 || options&8)
succ=AssignLate(log,phy);
/* non-binding assign, CLI: assign PATH */
else if (options&4 || options&16)
succ=AssignPath(log,phy);
else if (!options) {
BPTR lock;
if( lock=Lock(phy,ACCESS_READ) )
if( !(succ=AssignLock(log,lock)))
UnLock(lock);
}
if( !succ )
pError( log );
}
}
static void
assignlist()
{
char *ptr;
struct DosList *dl;
struct AssignList *path;
ULONG flags;
char fmt[256],devname[256];
char **dev_list;
long dev_num,i,cnt;
dev_list = NULL;
dev_num = 0;
flags = LDF_VOLUMES|LDF_READ;
Myprocess->pr_WindowPtr = (APTR)(-1);
if (dl=LockDosList(flags)) {
while (dl=NextDosEntry(dl,flags)) {
BtoCStr(devname,dl->dol_Name,254L); /* 256 - '\0' + ':' */
sprintf(fmt,"%s%s",devname,
dl->dol_misc.dol_volume.dol_LockList?"":" [Mounted]");
add_array_list(&dev_list,&dev_num,fmt);
}
UnLockDosList(flags);
}
Myprocess->pr_WindowPtr = o_noreq ? (APTR) -1L : 0L/*Mywindow*/;
QuickSort(dev_list,dev_num);
printf("Volumes:\n");
for(i=0; i<dev_num; i++)
printf("%s\n",dev_list[i]);
free_array_list(dev_list,dev_num);
if (dobreak())
return;
dev_list = NULL;
dev_num = 0;
flags = LDF_ASSIGNS|LDF_READ;
Myprocess->pr_WindowPtr = (APTR)(-1);
if (dl=LockDosList(flags)) {
while (dl=NextDosEntry(dl,flags)) {
BtoCStr(devname,dl->dol_Name,254L); /* 256 - '\0' + ':' */
sprintf(fmt,"%-14s ",devname);
ptr = fmt+strlen(fmt);
switch (dl->dol_Type) {
case DLT_DIRECTORY :
if(dl->dol_Lock) {
if (!NameFromLock(dl->dol_Lock,ptr,200L)) {
struct FileLock *fl_tmp = BADDR(dl->dol_Lock);
/* get volume name of assign to an unmounted volume */
if (fl_tmp->fl_Volume) {
struct DosList *dl_tmp = BADDR(fl_tmp->fl_Volume);
if (dl_tmp->dol_Name) {
BtoCStr(devname,dl_tmp->dol_Name,254L); /* 256 - '\0' + ':' */
sprintf(ptr,"Volume: %s",devname);
}
else
strcpy(ptr,"n/a");
}
else
strcpy(ptr,"n/a");
/*fprintf(stderr,"csh.assignlist.1: NameFromLock() failed\n");*/
/*strcpy(ptr,"Lock's name too long");*/
}
}
else
strcpy(ptr,"Nonexisting lock");
/* --- */
if (path=dl->dol_misc.dol_assign.dol_List) {
char **ass_list=NULL;
long ass_num=0,str_len=0;
char *ass_str;
add_array_list(&ass_list,&ass_num,fmt);
str_len += strlen(fmt);
for (; path; path=path->al_Next) {
sprintf(fmt,"%-13s+ ","");
ptr = fmt+strlen(fmt);
if (path->al_Lock) {
if (!NameFromLock(path->al_Lock,ptr,200L)) {
struct FileLock *fl_tmp = BADDR(path->al_Lock);
/* get volume name of assign to an unmounted volume */
if (fl_tmp->fl_Volume) {
struct DosList *dl_tmp = BADDR(fl_tmp->fl_Volume);
if (dl_tmp->dol_Name) {
BtoCStr(devname,dl_tmp->dol_Name,254L); /* 256 - '\0' + ':' */
sprintf(ptr,"Volume: %s",devname);
}
else
strcpy(ptr,"n/a");
}