home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------------------*
- *
- * wish - windowing user friendly shell
- * ------------------------------------
- *
- * Copyright (c) 1988-1993 Hellmuth Michaelis
- *
- * Eggerstedtstr. 28
- * 22765 Hamburg
- * Germany
- *
- * Tel: +49 / 40 / 384298 (private)
- * Tel: +49 / 40 / 55903-170 (at work)
- * e-mail: hm@hcshh.hcs.de
- *
- * --------oOo--------
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- *---------------------------------------------------------------------------*
- *
- * Last Edit-Date: [Mon Aug 30 19:59:18 1993]
- *
- * -hm converting to curses and multiwindows
- * -hm general cleanup ....
- * -hm adding a close-current-dir function and fkey
- * -hm adding history to command line
- * -hm default display attribs on
- * -hm changing coomandline control to emacs-commands
- * -hm vt220 support, housekeeping
- * -hm show time in header
- * -hm try to preserve current position after command
- * -hm backspace char from termio struct
- * -hm porting to 386BSD
- * -hm show links flag
- * -hm config file processing "$HOME/.wishrc"
- * -hm add / subtract option processing
- * -hm exit() -> fatal() changed
- * -hm hpterm / config menu
- * -hm fixed delete character bug
- * -hm fkey label structures
- * -hm ESC,ESC filename completition
- * -hm GNU copyleft
- *
- *----------------------------------------------------------------------------*/
-
- #define MAIN /* declare variables into this file */
-
- #include "wish.h" /* everything we want */
- #include "control.h" /* control-characters */
-
- /*---------------------------------------------------------------------------*
- * main loop
- *---------------------------------------------------------------------------*/
- int main (int argc, char *argv[])
- {
- char c; /* just for argument processing */
- int i; /* gp int */
- int kchar; /* character from keyboard */
- int optmode; /* add (1) or subtract (0) option mode */
- errorflag = 0; /* no error yet */
-
- /* first - set up defaults from environment */
-
- if((envhome = getenv("HOME")) == NULL)
- {
- fprintf(stderr, "\n Environment variable \"HOME\" not set!\n");
- exit(1);
- }
-
- if((envmore = getenv("PAGER")) == NULL)
- {
- envmore = "more"; /* default pager for file display */
- }
-
- if((envedit = getenv("EDITOR")) == NULL)
- {
- if((envedit = getenv("VISUAL")) == NULL)
- {
- envedit = "vi"; /* default editor */
- }
- }
-
- if((term_string = getenv("TERM")) == NULL)
- {
- fprintf(stderr,"\nwish: environment variable \"TERM\" undefined, cannot run!\n");
- exit(1);
- }
-
- /* init local changable string buffers & pointers */
-
- strcpy(opt_edit, envedit); /* cp from env to local buffer */
- strcpy(opt_more, envmore); /* cp from env to local buffer */
- strcpy(opt_wild, "*"); /* init wildcard buffer ... */
-
- /* second - configure ourselves from users .wishrc */
-
- readrc(); /* read $HOME/.wishrc */
-
- /* adjust for misconfigured wildcards */
-
- if(opt_wild && (strpbrk(opt_wild,WILDCHARS)==NULL))
- opt_wildon = 0;
-
- /* third - configure ourselves from command line parms */
-
- while((--argc > 0) && ( ((*++argv)[0] == '-') || ((*argv)[0] == '+')) )
- {
- if((*argv)[0] == '-')
- optmode = 0;
- else
- optmode = 1;
-
- c = *++argv[0];
-
- while(c)
- {
- switch(c)
- {
- case 'a': /* attributes line */
- if(optmode)
- opt_attrib = 1;
- else
- opt_attrib = 0;
- break;
-
- case 'd': /* initial preserve dir when cd .. */
- if(optmode)
- opt_preserve = 1;
- else
- opt_preserve = 0;
- break;
-
- case 'f': /* NO f-key labels on non-hp terminals */
- if(optmode)
- opt_labels = 1;
- else
- opt_labels = 0;
- break;
-
- case 'l': /* show links not files */
- if(optmode)
- opt_links = 1;
- else
- opt_links = 0;
- break;
-
- case 'p': /* display current dir in file window */
- if(optmode)
- opt_point = 1;
- else
- opt_point = 0;
- break;
-
- case 'r': /* press return to continue */
- if(optmode)
- opt_return = 1;
- else
- opt_return = 0;
- break;
-
- case 'w': /* enable wildcards */
- if(optmode)
- opt_wildon = 1;
- else
- opt_wildon = 0;
- break;
-
- case 'c': /* cdrom mode */
- if(optmode)
- opt_cdrom = 1;
- else
- opt_cdrom = 0;
- break;
-
- case 'C': /* cdrom progs->files */
- if(optmode)
- opt_cdnoprog = 1;
- else
- opt_cdnoprog = 0;
- break;
-
- case 'n': /* display dot-filenames */
- if(optmode)
- opt_dotnames = 1;
- else
- opt_dotnames = 0;
- break;
-
- default:
- fprintf(stderr,"\nwish: illegal option -%c",c);
-
- case '?':
- usage(); /* no return */
- }
- }
- }
-
- if(opt_attrib == 0) /* if no attrib line, */
- opt_links = 0; /* no link detection necessary */
-
- /* allocate memory for history */
-
- for(i=0; i < HISLINES; i++)
- {
- if((hislines[i] = (char *)malloc(HISLNLEN)) == NULL)
- {
- fprintf(stderr,"\nwish: history malloc failed, exit ...\n");
- exit(1);
- }
- sprintf(hislines[i],"%02d>",i); /* write prompt */
- }
-
- cur_fktab = &sys_keys; /* current fkey label table */
-
- init_header(); /* initialize header string */
- init_time(); /* display time init */
- init_screen(); /* initialize screen */
- init_flabels(); /* init fkey-labels */
- init_history(); /* init commandline history */
- init_files(PRES_NO, NULL); /* read current directory */
- attribs(1); /* display attributes */
- update_all(); /* force first screen-update */
-
- switch(termtype) /* hpux curses workaround */
- { /* for 2nd bug ..... */
- case TERM_VT1: /* force header update on */
- case TERM_VT2: /* dec terminals 2 times */
- case TERM_VT3: /* to display inverse!!!! */
- case TERM_PCVT:
- wmove(cmnd_w, C_HEAD, 0);
- waddstr(cmnd_w,headerline);
- wrefresh(cmnd_w);
- header();
- break;
- }
-
- wmove(cmnd_w, C_LINE, curcol());/* cursor to command window */
- wrefresh(cmnd_w); /* update command window */
-
- bschar = erasechar(); /* get backspace char */
-
- for(;;)
- {
- kchar = getch(); /* get char */
-
- clrerror(); /* if error line in header, clear ! */
-
- if(((kchar >= 0x20) && (kchar <= 0x7e)) ||
- ((kchar >= 0x80) && (kchar <= 0xff)))
- {
- /* printable char's to commandline */
-
- cmdline(kchar);
- wmove(cmnd_w, C_LINE, curcol());
- wrefresh(cmnd_w);
- continue;
- }
- else if(kchar == bschar) /* backspace */
- {
- handlebs();
- }
- else /* special keys = special actions */
- {
- switch(kchar)
- {
- case CR:
- handlecr();
- update_files();
- break;
-
- case KEY_DC: /* delete last char */
- del_char();
- break;
-
- case KEY_BACKSPACE:
- handlebs();
- break;
-
- case KEY_RIGHT: /* right move cursor */
- case TAB:
- move_right();
- update_files();
- break;
-
- case KEY_LEFT: /* left move cursor */
- case KEY_BTAB:
- move_left();
- update_files();
- break;
-
- case KEY_UP: /* up-move cursor */
- move_up();
- update_files();
- break;
-
- case KEY_DOWN: /* down-move cursor */
- move_down();
- update_files();
- break;
-
- case KEY_HOME: /* move cursor to first dir */
- move_home();
- update_files();
- break;
-
- case KEY_LL: /* move cursor to last file */
- move_hmdn();
- update_files();
- break;
-
- case KEY_NPAGE: /* next/previous page */
- next_page();
- update_files();
- break;
-
- case KEY_PPAGE: /* next/previous page */
- prev_page();
- update_files();
- break;
-
- case KEY_F(1): /* function key 1 */
- edit_current();
- break;
-
- case KEY_F(2): /* function key 2 */
- name_echo();
- break;
-
- case KEY_F(3): /* function key 3 */
- help();
- update_files();
- break;
-
- case KEY_F(4): /* function key 4 */
- config();
- break;
-
- case KEY_F(5): /* function key 5 */
- tag_current(cur_file);
- move_right();
- update_files();
- break;
-
- case KEY_F(6): /* function key 6 */
- untag_all();
- update_files();
- break;
-
- case KEY_F(7): /* function key 7 */
- break;
-
- case KEY_F(8): /* function key 8 */
- close_cdir();
- update_files();
- break;
-
- case CNTRL_F: /* right move cursor */
- right_line();
- break;
-
- case CNTRL_B: /* left move cursor */
- left_line();
- break;
-
- case CNTRL_P: /* up-move cursor */
- prev_line();
- break;
-
- case CNTRL_N: /* down-move cursor */
- next_line();
- break;
-
- case CNTRL_D:
- if(cr_on_files())
- {
- move(LINES,COLS-1); /* last display position */
- free_list(); /* free memory */
- fini_flabels(); /* normal fkey-labels */
- endwin(); /* normalize screen */
- putchar('\n'); /* newline */
- exit(0);
- }
- del_char();
- break;
-
- case CNTRL_K: /* clear to eol */
- clear_toeol();
- break;
-
- case CNTRL_Y: /* yank kill buffer */
- yank();
- break;
-
- case CNTRL_A: /* begin of line */
- bol_line();
- break;
-
- case CNTRL_E: /* end of line */
- eol_line();
- break;
-
- case CNTRL_L: /* refresh */
- touchwin(curscr);
- wrefresh(curscr);
- break;
-
- case ESC:
- kchar = getch(); /* get char */
- switch(kchar)
- {
- case '1': /* function key 1 */
- edit_current();
- break;
-
- case '2': /* function key 2 */
- name_echo();
- break;
-
- case '3': /* function key 3 */
- help();
- update_files();
- break;
-
- case '4': /* function key 4 */
- config();
- break;
-
- case '5': /* function key 5 */
- tag_current(cur_file);
- move_right();
- update_files();
- break;
-
- case '6': /* function key 6 */
- untag_all();
- update_files();
- break;
-
- case '7': /* function key 7 */
- break;
-
- case '8': /* function key 8 */
- close_cdir();
- update_files();
- break;
-
- case 'n':
- case 'N':
- next_page();
- update_files();
- break;
-
- case 'p':
- case 'P':
- prev_page();
- update_files();
- break;
-
- case ESC: /* filename completition */
- complete();
- break;
- }
- }
- }
- wmove(cmnd_w, C_LINE, curcol());
- wrefresh(cmnd_w);
- }
- }
-
- /*---------------------------------------------------------------------------*
- * initialize curses and window dimensions
- *---------------------------------------------------------------------------*/
- void init_screen(void) /* initialize everything */
- {
- int attrpos;
-
- initscr(); /* curses init */
- nonl(); /* optimize */
- raw(); /* raw input */
- noecho(); /* do not echo input */
- idlok(stdscr,TRUE); /* use insert/delete */
- keypad(stdscr,TRUE); /* use special keys */
- meta(stdscr,TRUE); /* use 8-bit chars */
-
- set_termtype(); /* init terminal dep. stuff */
-
- if((cmnd_w = newwin(C_HEIGHT, COLS, 0, 0)) == NULL)
- fatal("cannot create command window");
-
- fileheight = LINES-C_HEIGHT;
-
- if(opt_labels)
- {
- if((flbl_w = newwin(1, COLS, LINES-1, 0)) == NULL)
- fatal("cannot create fkey-label window");
- fileheight--;
- }
-
- if(opt_attrib)
- {
- fileheight -= 2;
- attrpos = C_HEIGHT+fileheight;
-
- if((attr_w = newwin(2, COLS, attrpos, 0)) == NULL)
- fatal("cannot create attribute window");
- }
-
- if((fst_w = newwin(2, COLS, C_HEIGHT, 0)) == NULL)
- fatal("cannot create file status window");
-
- fileheight -= 2;
-
- if((file_w = newwin(fileheight, COLS, C_HEIGHT+2, 0)) == NULL)
- fatal("cannot create file window");
-
- header(); /* print headerline */
- clearok(curscr,TRUE);
- }
-
- /*---------------------------------------------------------------------------*
- * refresh all windows
- *---------------------------------------------------------------------------*/
- void update_all(void)
- {
- wnoutrefresh(cmnd_w);
- wnoutrefresh(fst_w);
- touchwin(file_w); /* after cur_blink() */
- wnoutrefresh(file_w);
- if(opt_attrib)
- wnoutrefresh(attr_w);
- if(opt_labels)
- wnoutrefresh(flbl_w);
- doupdate();
- }
-
- /*---------------------------------------------------------------------------*
- * refresh file window and attrib window
- *---------------------------------------------------------------------------*/
- void update_files(void)
- {
- wnoutrefresh(file_w);
- if(opt_attrib)
- wnoutrefresh(attr_w);
- if(opt_labels)
- wnoutrefresh(flbl_w);
- doupdate();
- }
-
- /*---------------------------------------------------------------------------*
- * find out terminal-type and initialize terminal specific things
- *---------------------------------------------------------------------------*/
- void set_termtype(void)
- {
- static char initvt[] = { ESC, ')', '0', '\0'};
-
- if(ceol_standout_glitch) /* HP-Terminal */
- {
- opt_labels = 0; /* no virtual fkey-labels */
- if(!strcmp(term_string,"hpterm")) /* X11 hpterm */
- termtype = TERM_HPX;
- else
- termtype = TERM_HP;
- }
-
- else if(!strncmp(term_string,"vt1",3)) /* DEC VT1xx ?? */
- {
- opt_labels = 1; /* virtual fkey-labels */
- termtype = TERM_VT1;
- }
-
- else if(!strncmp(term_string,"vt220",5))/* DEC VT220 ?? */
- {
- opt_labels = 1; /* virtual fkey-labels */
- write(2, initvt, 3);
- termtype = TERM_VT2;
- }
-
- else if(!strncmp(term_string,"vt320",5))/* DEC VT320 ?? */
- {
- opt_labels = 1; /* virtual fkey-labels */
- write(2, initvt, 3);
- termtype = TERM_VT3;
- }
-
- else if(!strncmp(term_string,"pcvt",4)) /* 386BSD pcvt ?? */
- {
- opt_labels = 0; /* real fkey-labels */
- write(2, initvt, 3);
- termtype = TERM_PCVT;
- }
-
- else /* dumb thing .. */
- {
- termtype = TERM_DUMB;
- }
- }
-
- /*---------------------------------- EOF ----------------------------------*/
-