home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------------------*
- *
- * wish - windowing user friendly shell
- * --------------------------------------
- *
- * (c) Copyright Hellmuth Michaelis 1989 - 1993
- *
- * Eggerstedtstr. 28
- * 22765 Hamburg
- * Germany
- *
- * Tel: +49 / 40 / 384298 (private)
- * Tel: +49 / 40 / 55903-170 (at work)
- * e-mail: hm@hcshh.hcs.de
- *
- * All rights are reserved except as explicitly granted
- * by written permission of the author.
- *
- * See the file COPYING, distributed with wish, for
- * restriction and warranty information
- *
- *---------------------------------------------------------------------------*
- *
- * last edit-date: [Mon Aug 30 12:27:37 1993]
- *
- * -hm debugging "more" output
- * -hm separate sys-call function exec_command
- * -hm time suspend/resume
- * -hm nochng flag introduced
- * -hm current dir preserve when cd ..
- * -hm compress / gzip support
- * -hm tar support
- * -hm no space on echo to first cmdline pos
- * -hm fixed no space bug ....
- * -hm curdir debugging
- * -hm housekeeping
- * -hm cd bugfix, cdmount did not do what it should
- * -hm new routine ending to support multiple extensions
- * -hm cd with wildcards
- * -hm cdrom special processing for filenames containing ';'
- *
- *----------------------------------------------------------------------------*/
-
- #include "wish.h" /* local includes */
-
- static int waitcr = 0; /* do not wait for cr */
- static int reread = 0; /* reread current dir */
- static int nochng = 0; /* nothing has changed */
- static int preserve = PRES_NO; /* preserve highlighted position */
- static char curdirb[MAXPATHLEN+1];
- static char *curdir = curdirb; /* current dir to preserve if cd .. */
-
- static char *str_nxtw(char *, int); /* returns a pointer to the next word in string */
- static char *ending(char *filename);
- static char *adjustfn(register char *name);
-
- /*---------------------------------------------------------------------------*
- * echo the current highlighted filename onto the commandline
- *---------------------------------------------------------------------------*/
- void name_echo(void)
- {
- char *ptr; /* temp ptr */
-
- ptr = &(cur_file->onam[1]); /* ptr = current filename */
- /* check for delimiter */
-
- if( (!(cr_on_files())) && (cbuff[curcol()-1] != SPACE) )
- cmdline(SPACE); /* if none, insert one */
-
- ptr = adjustfn(ptr);
-
- while((*ptr) && ((cmdline(*ptr++)) == GOOD))
- ;
- }
-
- /*---------------------------------------------------------------------------*
- * returns a pointer to the next word in string
- *---------------------------------------------------------------------------*/
- static char *str_nxtw(char *str, int frst)
- {
- char *r;
-
- if((str == NULL) || (*str == '\0'))
- return(NULL);
-
- r = str;
-
- if(frst)
- {
- while(*r == SPACE) /* while leading delimiters */
- r++;
- if(*r != '\0')
- return(r); /* first non-space char */
- else
- return(NULL);
- }
- else
- {
- while((*r != SPACE) && (*r != '\0')) /* find space */
- r++;
- if(*r == '\0') /* test for end of string */
- return(NULL);
- while(*r == SPACE) /* while there are delimiters */
- r++;
- if(*r != '\0')
- return(r); /* first non-space char */
- else
- return(NULL); /* end of string */
- }
- }
-
- /*---------------------------------------------------------------------------*
- * expand the selection character onto the commandline
- *---------------------------------------------------------------------------*/
- void expsel(char *p)
- {
- char rest[512]; /* save buffer */
- struct onefile *info; /* our file struct ptr */
- int i; /* free space counter in cbuff */
- register char *fnp; /* for filename conversion */
-
- strcpy(rest,p+1); /* save right half of cbuff after EXPSEL-char */
-
- if(*(p-1) != SPACE) /* check for delimiter present */
- {
- *p = SPACE;
- p++;
- }
-
- i = (p - cbuff) + strlen(rest); /* count of already occupied chars */
-
- info = first; /* first in list */
-
- *p = '\0'; /* terminate left half of cbuff */
-
- while(info) /* scan through dir - list */
- {
- if(info->tag) /* search for tagged files */
- {
- fnp = adjustfn(&(info->onam[1]));
-
- if(((strlen(fnp))+1) < (HISLNLEN - i))
- {
- /* we have enough space */
-
- i += (strlen(fnp)+1);
- strcat(cbuff, fnp);
- strcat(cbuff," ");
- }
- }
- info = info->next; /* next entry */
- }
- strcat(cbuff,rest); /* copy saved right half to generated part */
- }
-
- /*---------------------------------------------------------------------------*
- * user has typed a <CR>
- *---------------------------------------------------------------------------*/
- void handlecr(void)
- {
- nochng = 0; /* anything may change on screen */
- preserve = PRES_NORM; /* nothing to preserve */
-
- if(cr_on_files()) /* nothing on commandline, process high- */
- { /* lighted filename field */
- h_files(); /* cr on a filename */
- }
- else /* something in commandlinebuffer */
- {
- h_line(); /* commandline */
- }
-
- if(waitcr && opt_return) /* wait for <cr> being hit */
- {
- move(LINES-1, 0); /* last line */
- clrtoeol(); /* clear it */
- move(LINES-1, 0); /* last line */
- attrset(A_REVERSE); /* highlight on */
- addstr("Press any key to continue ...");
- attroff(A_REVERSE); /* highlight on */
- refresh();
- getch();
- move(LINES-1, 0); /* last line */
- clrtoeol(); /* clear it */
- touchwin(stdscr);
- wnoutrefresh(stdscr);
- }
- header(); /* new header */
- dis_hist(); /* display current history entry */
-
- if(reread) /* directory has to be re-read */
- {
- free_list(); /* free malloc'ed buffers */
- init_files(preserve, curdir); /* yes, read current dir */
- }
- else
- {
- if(!nochng)
- fresh_files(); /* just refresh window */
- }
- resume_time(); /* restart time display update */
- if(!nochng)
- {
- attribs(1); /* new attributes */
- init_flabels(); /* fk-labels onto screen */
- update_all(); /* update all windows */
- }
- else
- {
- wrefresh(cmnd_w);
- }
- }
-
- /*---------------------------------------------------------------------------*
- * user has typed <CR> and something is on commandline
- *---------------------------------------------------------------------------*/
- void h_line(void)
- {
- char *cptr = cbuff+3; /* ptr to start of line after prompt */
- char *cp; /* command ptr */
- char *ap; /* argument ptr */
- char *ep; /* expansion char ptr */
-
- if((cp = str_nxtw(cptr,1)) == NULL) /* just spaces on commandline ? */
- {
- nochng = 1;
- errno = 0; /* yes ... */
- error("commandline empty ... ");
- waitcr = 0;
- reread = 0;
- }
- else if( (*cp == 'c') && (*(cp+1) == 'd') &&
- ( (*(cp+2) == ' ') || (*(cp+2) == '\t') || (*(cp+2) == '\0') ) )
- {
- int ret;
- if((ap = str_nxtw(cp, 0)) == NULL)
- {
- ret = chdir(envhome); /* cd to home */
- }
- else
- {
- char *ptr;
-
- if((ptr = (char *)index(ap, '.')) != NULL)
- {
- if(!(strncmp(ptr, "..", 2)))
- {
- if(getcwd(curdir, MAXPATHLEN) == NULL)
- *curdir = '\0';
- preserve = PRES_DD;
- }
- }
- ret = cd(ap); /* cd to <dir> */
- }
-
- if(ret == -1)
- {
- nochng = 1;
- error("changing directory failed - chdir() error");
- waitcr = 0;
- reread = 0;
- }
- else
- {
- waitcr = 0;
- reread = 1;
- }
- }
- else
- {
- if((ep = strchr((cbuff+3),EXPSEL)) != NULL)
- expsel(ep); /* expand selection char */
-
- exec_command(cbuff+3);
-
- waitcr = 1; /* wait for <cr> */
- reread = 1; /* reread current dir */
- }
- save_line(); /* save commandline to history */
- }
-
- /*---------------------------------------------------------------------------*
- * user has typed <CR> and commandline is empty
- *---------------------------------------------------------------------------*/
- void h_files(void)
- {
- preserve = PRES_NORM;
-
- if(cur_file->oprm[0] == 'd') /* directories */
- {
- preserve = PRES_DD;
-
- if(getcwd(curdir, MAXPATHLEN) == NULL)
- *curdir = '\0';
-
- if((chdir(&(cur_file->onam[1]))) == -1)
- {
- nochng = 1;
- error("changing directory to current selection failed - chdir() error");
- reread = 0; /* no need to re-read */
- }
- else
- {
- reread = 1; /* (re-) read (new) dir */
- }
-
- waitcr = 0; /* don't wait for <CR> */
- }
-
- /* executables */
-
- else if((cur_file->oprm[3] == 'x') ||
- (cur_file->oprm[6] == 'x') ||
- (cur_file->oprm[9] == 'x'))
- {
- sprintf(cbuff, "%s/%s", cur_path, &(cur_file->onam[1]));
-
- exec_command(cbuff); /* shell-call */
-
- waitcr = 1;
- reread = 1;
- }
-
- /* data files */
- else if((cur_file->oprm[1] == 'r') ||
- (cur_file->oprm[4] == 'r') ||
- (cur_file->oprm[7] == 'r'))
- {
- FILE *pfp;
- char *pp;
- char *fnp;
-
- fnp = adjustfn(&(cur_file->onam[1]));
-
- sprintf(cbuff, "cat %s/%s", cur_path, fnp);
-
- if((pp = ending(fnp)) != NULL)
- strcat(cbuff, pp);
-
- strcat(cbuff,"|"); /* pipe to pager */
- strcat(cbuff, opt_more); /* pager */
-
- suspend_time(); /* stop updating time */
- fini_flabels(); /* remove user labels */
- move(0, 0); /* first line */
- touchwin(stdscr); /* force */
- clear(); /* clear it */
- refresh(); /* update screen */
- savetty(); /* save tty-modes for resetty() */
- reset_shell_mode(); /* set tty to pre-curses values */
-
- if((pfp = popen(cbuff, "w")) == NULL) /* exec */
- error("cannot open pipe to PAGER");
- else
- {
- if(pclose(pfp) == -1)
- error("PAGER pclose error");
- }
-
- resetty(); /* restore modes to curses operation */
-
- reset_prog_mode(); /* set to curses values */
-
- #ifdef CURSESBUG
- putp(keypad_xmit); /* hp curses bug .. */
- #endif
-
- touchwin(cmnd_w);
- touchwin(fst_w);
- touchwin(file_w);
- if(opt_attrib)
- touchwin(attr_w);
- if(opt_labels)
- touchwin(flbl_w);
-
- resume_time(); /* restart updating time */
- waitcr = 0; /* don't wait for <CR> */
- reread = 0; /* reread dir */
- }
-
- /* OOPS, whats this ?? */
- else
- {
- errno = 0; /* no errno display */
- error("no exec/read permission found ....");
- flash(); /* no no .. */
- waitcr = 0; /* don't wait for <CR> */
- reread = 0; /* don't reread dir !!! */
- }
- }
-
- /*---------------------------------------------------------------------------*
- * what to do with the file depending on ending
- *---------------------------------------------------------------------------*/
- static char *ending(char *filename)
- {
- int i;
- char *p;
-
- static char *etab[][2] = {
- {".tar.Z", "|compress -dfc|tar tvf -"},
- {".tar.z", "|gzip -cdf|tar tvf -"},
- {".tar.gz", "|gzip -cdf|tar tvf -"},
- {".tar", "|tar tvf -"},
- {".Z", "|compress -dfc"},
- {".z", "|gzip -cdf"},
- {".gz", "|gzip -cdf"},
- {NULL, NULL}
- };
-
- /* see if filename contains a known extension and return the */
- /* commandline string to decode that file */
-
- for(i = 0; etab[i][0] != NULL; i++)
- {
- if( ((p = strstr(filename, etab[i][0])) != NULL) &&
- (strlen(p) == strlen(etab[i][0])) )
- return(etab[i][1]);
- }
- return(NULL);
- }
-
- /*---------------------------------------------------------------------------*
- * edit the current file
- *---------------------------------------------------------------------------*/
- void edit_current(void)
- {
- char *fnp;
-
- if(cur_file->oprm[0] == 'd') /* directories */
- return;
-
- fnp = adjustfn(&(cur_file->onam[1]));
-
- sprintf(cbuff,"%s %s/%s", opt_edit, cur_path, fnp);
-
- exec_command(cbuff); /* shell-call */
-
- header(); /* new header */
- dis_hist(); /* display current history */
- fresh_files(); /* display files */
- attribs(1); /* new attributes */
- init_flabels(); /* fk-labels onto screen */
- update_all();
- resume_time(); /* restart time display update */
- }
-
- /*---------------------------------------------------------------------------*
- * close current directory
- *---------------------------------------------------------------------------*/
- void close_cdir(void)
- {
- if(getcwd(curdir, MAXPATHLEN) == NULL)
- *curdir = '\0';
-
- if((chdir("..")) == -1)
- {
- error("changing directory to '..' dir failed - chdir() error");
- return;
- }
- else
- {
- header(); /* new header */
- free_list(); /* free malloc'ed buffers */
- init_files(PRES_DD, curdir); /* yes, read current dir */
- attribs(1); /* new attributes */
- init_flabels(); /* fk-labels onto screen */
- }
- update_all();
- }
-
- /*---------------------------------------------------------------------------*
- * execute a command/program
- *---------------------------------------------------------------------------*/
- void exec_command(char *cline)
- {
- suspend_time(); /* no time display update */
- fini_flabels(); /* remove user labels */
- move(0, 0); /* last line */
- touchwin(stdscr); /* force */
- clear(); /* clear it */
- refresh();
- savetty(); /* save tty-modes for resetty() */
- reset_shell_mode(); /* set to pre-curses values */
- system(cline); /* shell-call */
- resetty(); /* restore modes to curses operation */
- reset_prog_mode(); /* set to curses values */
-
- #ifdef CURSESBUG
- putp(keypad_xmit); /* hp curses bug .. */
- #endif
-
- touchwin(cmnd_w);
- touchwin(fst_w);
- touchwin(file_w);
- if(opt_attrib)
- touchwin(attr_w);
- if(opt_labels)
- touchwin(flbl_w);
- }
-
- /*---------------------------------------------------------------------------*
- * adjust filename (cdrom, others ?)
- *---------------------------------------------------------------------------*/
- static char *adjustfn(char *name)
- {
- static char buffer[MAXPATHLEN];
- register char *bp = buffer;
- register char *sp = name;
-
- while(*sp)
- {
- if(iscdfs && (*sp == ';'))
- *bp++ = '\\';
- *bp++ = *sp++;
- }
- *bp = '\0';
- return((char *)buffer);
- }
-
- /*---------------------------------- EOF -------------------------------------*/
-