home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume25 / ile / ile.c < prev    next >
C/C++ Source or Header  |  1991-12-12  |  60KB  |  3,136 lines

  1. /*
  2.                           COPYRIGHT 1988
  3.               Evans & Sutherland Computer Corporation
  4.                         Salt Lake City, Utah
  5.                         All Rights Reserved.
  6.  
  7.      THE INFORMATION  IN  THIS  SOFTWARE  IS  SUBJECT  TO  CHANGE
  8.      WITHOUT  NOTICE  AND SHOULD NOT BE CONSTRUED AS A COMMITMENT
  9.      BY  EVANS  &  SUTHERLAND.   EVANS  &  SUTHERLAND   MAKES  NO
  10.      REPRESENTATIONS  ABOUT  THE SUITABILITY OF THIS SOFTWARE FOR
  11.      ANY PURPOSE.  IT IS SUPPLIED  "AS  IS"  WITHOUT  EXPRESS  OR
  12.      IMPLIED WARRANTY.
  13.  
  14.      IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING  DERIVATIVE
  15.      COPYRIGHT  RIGHTS,  APPROPRIATE LEGENDS MAY BE PLACED ON THE
  16.      DERIVATIVE WORK IN ADDITION TO THAT SET FORTH ABOVE.
  17.  
  18.      Permission  to  use,  copy,  modify,  and  distribute   this
  19.      software  and  its documentation for any purpose and without
  20.      fee is hereby granted, provided  that  the  above  copyright
  21.      notice  appear  in  all  copies  and that both the copyright
  22.      notice and this permission notice appear in supporting docu-
  23.      mentation,  and  that  the name of Evans & Sutherland not be
  24.      used in advertising or publicity pertaining to  distribution
  25.      of the software without specific, written prior permission.
  26.  
  27. Written by:
  28.  
  29.                         Robert C. Pendleton <bobp@hal.com>
  30.  
  31. Grateful acknowledgement is made of code and ideas contributed by
  32.  
  33.      Ian Donaldson,
  34.      Department of Communications & Electronic Engineering,
  35.      Royal Melbourne Institute of Technology,
  36.      Melbourne, Australia.
  37.  
  38. $Header: ile.c,v 2.3 88/11/11 10:31:56 bpendlet Exp $
  39. */
  40.  
  41. /*
  42. ile is compiled using:
  43.  
  44. cc ile.c -o ile -ltermcap
  45. */
  46.  
  47. #include <stdio.h>
  48. #include <fcntl.h>
  49. #include <sgtty.h>
  50. #include <signal.h>
  51. #include <string.h>
  52. #include <strings.h>
  53. #include <pwd.h>
  54. #include <utmp.h>
  55. #include <errno.h>
  56. #include <sys/ioctl.h>
  57. #include <sys/types.h>
  58. #include <sys/dir.h>
  59. #include <sys/file.h>
  60. #include <sys/time.h>
  61.  
  62. /*------------------------------------------------------------------*/
  63. /*
  64. Definitions of system stuff.
  65. */
  66.  
  67. extern int errno;
  68.  
  69. long lseek();
  70. char *malloc();
  71. char *realloc();
  72. time_t time();
  73.  
  74. /*------------------------------------------------------------------*/
  75.  
  76. #define FALSE 0
  77. #define TRUE  1
  78.  
  79. #define READ  0
  80. #define WRITE 1
  81. #define ERROR 2
  82.  
  83. #define BUFFER_SIZE 255
  84.  
  85. #define HISTORY_SIZE 101
  86.  
  87. #define USER_NAME_SIZE 8
  88.  
  89. #define EOL (-2)
  90. /*------------------------------------------------------------------*/
  91.  
  92. /* special characters used by ile */
  93.  
  94. #define del '\177'
  95.  
  96. #define CA '\1'
  97. #define CB '\2'
  98. #define CC '\3'
  99. #define CD '\4'
  100. #define CE '\5'
  101. #define CF '\6'
  102. #define bel '\7'
  103. #define bs '\10'
  104. #define CI '\11'
  105. #define nl '\12'
  106. #define CK '\13'
  107. #define CL '\14'
  108. #define cr '\15'
  109. #define CN '\16'
  110. #define CO '\17'
  111. #define CP '\20'
  112. #define CQ '\21'
  113. #define CR '\22'
  114. #define CS '\23'
  115. #define CT '\24'
  116. #define CU '\25'
  117. #define CV '\26'
  118. #define CW '\27'
  119. #define CX '\30'
  120. #define CY '\31'
  121. #define CZ '\32'
  122.  
  123. #define esc '\33'
  124.  
  125. /*------------------------------------------------------------------*/
  126. /* areas and varaibles used to get termcap information */
  127.  
  128. char *getenv();
  129.  
  130. char *tgetnum();
  131. char *tgetflag();
  132. char *tgetstr();
  133.  
  134. char termcap_entry[1024];    /* termcap entry for the users terminal */
  135. char term_seqs[1024];        /* area to store control sequences in */
  136. char *where = term_seqs;
  137.  
  138. char *cle;            /* move cursor left one space */
  139.  
  140. char *cce;            /* clear to end of line */
  141.  
  142. char *cbl;            /* audible bell */
  143.  
  144. char *cnl;            /* new line character */
  145.  
  146. char *ccr;            /* carriage return */
  147.  
  148. /*------------------------------------------------------------------*/
  149. /*
  150. The value of HOME
  151. */
  152.  
  153. char *homedir = NULL;
  154.  
  155. /*------------------------------------------------------------------*/
  156. /*
  157. The current working directory as set by query_path.
  158. initialized to PWD
  159. */
  160.  
  161. char currentdir[MAXNAMLEN + 1] = "";
  162.  
  163. /*------------------------------------------------------------------*/
  164.  
  165. /* tty status flags */
  166. /*
  167.   The original tty status flags are stored so that they can be
  168.   restored when ile exits.
  169. */
  170.  
  171. struct sgttyb tty_sgttyb;
  172. struct tchars tty_tchars;
  173. struct ltchars tty_ltchars;
  174. struct winsize tty_winsize;
  175. int windowchanged;
  176. int tty_ldisc;
  177. int tty_mode;
  178.  
  179. /*------------------------------------------------------------------*/
  180.  
  181. /* file descriptors for tty and pty */
  182.  
  183. int master_pty;
  184. int slave_tty;
  185.  
  186. /* the names of the tty and pty opened by getpty */
  187.  
  188. char ttydev[] = "/dev/ttyxx";
  189. char ptydev[] = "/dev/ptyxx";
  190.  
  191. /* path and name of the lock file */
  192.  
  193. char lock[] = "/tmp/ile.lock";
  194.  
  195. /*------------------------------------------------------------------*/
  196. /*
  197.   getpty opens a pty, storing file descriptors in pty and tty.
  198.   It trys pairs in order until it finds a pair that is not in use.
  199. */
  200.  
  201. getpty(pty, tty)
  202.     int *pty;
  203.     int *tty;
  204.  
  205. {
  206.     int devindex;
  207.     int letter;
  208.  
  209.     static char ptychar1[] = "pqrstuvwxyz";
  210.     static char ptychar2[] = "0123456789abcdef";
  211.  
  212.     letter = 0;
  213.     while (letter < 11)
  214.     {
  215.     ttydev[strlen(ttydev) - 2] = ptychar1[letter];
  216.     ptydev[strlen(ptydev) - 2] = ptychar1[letter];
  217.     letter++;
  218.  
  219.     devindex = 0;
  220.     while (devindex < 16)
  221.     {
  222.         ttydev[strlen(ttydev) - 1] = ptychar2[devindex];
  223.         ptydev[strlen(ptydev) - 1] = ptychar2[devindex];
  224.         devindex++;
  225.  
  226.         if ((*pty = open(ptydev, O_RDWR)) >= 0)
  227.         {
  228.         if ((*tty = open(ttydev, O_RDWR)) >= 0)
  229.         {
  230.             return;
  231.         }
  232.         else
  233.         {
  234.             (void) close(*pty);
  235.         }
  236.         }
  237.     }
  238.     }
  239.  
  240.     (void) fprintf(stderr, "ile: unable to allocate pty/tty pair\n");
  241.     exit(1);
  242.     /* NOTREACHED */
  243. }
  244.  
  245. /*------------------------------------------------------------------*/
  246. /*
  247. Termcap entries may have a sequences of digits optionally followed
  248. by a '*' in front of the actual sequence. This routine increments
  249. the pointer past this information.
  250. */
  251. void
  252. strip(ptr)
  253.     char **ptr;
  254. {
  255.     while (('0' <= **ptr) && (**ptr <= '9'))
  256.     {
  257.     (*ptr)++;
  258.     }
  259.  
  260.     if (**ptr == '*')
  261.     {
  262.     (*ptr)++;
  263.     }
  264. }
  265. /*------------------------------------------------------------------*/
  266. /*
  267. Set up everything needed to use the control sequences from the
  268. termcap entry for the terminal.
  269. */
  270. void
  271. get_termcap()
  272. {
  273.     char *terminal_type;    /* type of terminal */
  274.  
  275.     /* get the terminal name */
  276.  
  277.     terminal_type = getenv("TERM");
  278.  
  279.     /* get termcap entry */
  280.  
  281.     if (tgetent(termcap_entry, terminal_type) < 1)
  282.     {
  283.     (void) fprintf(stderr, "ile: can't find %s\n", terminal_type);
  284.     exit(1);
  285.     /* NOTREACHED */
  286.     }
  287.  
  288.     /* get the control sequences ile needs */
  289.  
  290.     if ((cbl = tgetstr("bl", &where)) == NULL)
  291.     {
  292.     cbl = "\7";
  293.     }
  294.  
  295.     if ((cnl = tgetstr("nl", &where)) == NULL)
  296.     {
  297.     cnl = "\n";
  298.     }
  299.  
  300.     if ((ccr = tgetstr("cr", &where)) == NULL)
  301.     {
  302.     ccr = "\r";
  303.     }
  304.  
  305.     if ((cle = tgetstr("le", &where)) == NULL)
  306.     {
  307.     if (tgetflag("bs"))
  308.     {
  309.         cle = "\b";
  310.     }
  311.     }
  312.  
  313.     if ((cle == NULL) ||
  314.     ((cce = tgetstr("ce", &where)) == NULL))
  315.     {
  316.     (void) fprintf(stderr,
  317.         "ile: can't run on %s (need capabilities \"le\" and \"ce\")\n",
  318.         terminal_type);
  319.     exit(1);
  320.     /* NOTREACHED */
  321.     }
  322.  
  323.     /* strip timing info from strings */
  324.  
  325.     strip(&cle);
  326.     strip(&cce);
  327.     strip(&cbl);
  328.     strip(&cnl);
  329.     strip(&ccr);
  330. }
  331. /*------------------------------------------------------------------*/
  332. /*
  333. If the window changes size, tell the slave_tty about it.
  334. */
  335. void
  336. change_window()
  337. {
  338.     int pgrp;
  339.  
  340.     (void) ioctl(READ, TIOCGWINSZ, &tty_winsize);
  341.     (void) ioctl(slave_tty, TIOCSWINSZ, &tty_winsize);
  342.  
  343.     (void) ioctl(slave_tty, TIOCGPGRP, (char *) &pgrp);
  344.     (void) killpg(pgrp, SIGWINCH);
  345.  
  346.     /* note the change so that we don't die after select */
  347.  
  348.     windowchanged = TRUE;
  349. }
  350. /*------------------------------------------------------------------*/
  351. /*
  352.  * set/clear the utmp slot for the pty
  353.  */
  354. int
  355. setutmp(fd, set)
  356. {
  357.     int old0;
  358.     int old1;
  359.     int old2;
  360.     int slot;
  361.     int f;
  362.     struct utmp ut;
  363.  
  364.     /* Must make fd's 0,1,2 correspond to slave_tty for ttyslot() to
  365.      * function.  Ugh!  Why doesn't ttyslot() accept a fd argument?
  366.      * 
  367.      * save fd's */
  368.  
  369.     old0 = dup(0);
  370.     old1 = dup(1);
  371.     old2 = dup(2);
  372.  
  373.     if (old0 == -1 || old1 == -1 || old2 == -1)
  374.     {
  375.     perror("ile: dup");
  376.     return (-1);        /* file table full ? */
  377.     }
  378.  
  379.     /* set fd's 0,1,2 for ttyslot() */
  380.  
  381.     (void) dup2(fd, 0);
  382.     (void) dup2(fd, 1);
  383.     (void) dup2(fd, 2);
  384.  
  385.     slot = ttyslot();
  386.  
  387.     /* put the fd's back */
  388.  
  389.     (void) dup2(old0, 0);
  390.     (void) dup2(old1, 1);
  391.     (void) dup2(old2, 2);
  392.  
  393.     (void) close(old0);
  394.     (void) close(old1);
  395.     (void) close(old2);
  396.  
  397.     if (slot < 0)
  398.     {
  399.     (void) fprintf(stderr, "ile: don't know where you are\n");
  400.     return (-1);
  401.     }
  402.  
  403.     f = open("/etc/utmp", O_WRONLY);
  404.     if (f == -1)
  405.     {
  406.     return (-1);
  407.     }
  408.  
  409.     bzero((char *) &ut, sizeof(ut));
  410.  
  411.     if (set)
  412.     {
  413.     struct passwd *pw;
  414.     char *cp;
  415.  
  416.     pw = getpwuid(getuid());
  417.     if (pw == 0)
  418.     {
  419.         (void) fprintf(stderr, "ile: who are you?\n");
  420.         (void) close(f);
  421.         return (-1);
  422.     }
  423.  
  424.     /* skip "/dev/" */
  425.  
  426.     cp = rindex(ttydev, '/');
  427.     if (cp == 0)
  428.     {
  429.         cp = ttydev;
  430.     }
  431.     else
  432.     {
  433.         cp++;
  434.     }
  435.  
  436.     (void) strncpy(ut.ut_line, cp, sizeof(ut.ut_line));
  437.     (void) strncpy(ut.ut_name, pw->pw_name, sizeof(ut.ut_line));
  438.     (void) time(&ut.ut_time);
  439.     }
  440.     (void) lseek(f, (long) (slot * sizeof(struct utmp)), L_SET);
  441.     (void) write(f, (char *) &ut, sizeof(ut));
  442.     (void) close(f);
  443.  
  444.     return (0);
  445. }
  446. /*------------------------------------------------------------------*/
  447. /*
  448. clean up and leave.
  449.  
  450. This function is bound to the SIGCHLD signal so that when the
  451. child process exits, so does ile. It is also called when an exception
  452. is detected by select() in ile().
  453. */
  454. void
  455. clean_up()
  456. {
  457.     int pgrp;
  458.  
  459.     /* kill off the child process */
  460.  
  461.     (void) ioctl(slave_tty, TIOCGPGRP, (char *) &pgrp);
  462.     (void) killpg(pgrp, SIGTERM);
  463.  
  464.     /* restore terminal status */
  465.  
  466.     (void) ioctl(READ, TIOCSETP, &tty_sgttyb);
  467.     (void) ioctl(READ, TIOCSETC, &tty_tchars);
  468.     (void) ioctl(READ, TIOCSLTC, &tty_ltchars);
  469.     (void) ioctl(READ, TIOCLSET, &tty_mode);
  470.  
  471.     /* "logout" the user */
  472.  
  473.     (void) setutmp(slave_tty, FALSE);
  474.  
  475.     /* clean up the tty/pty pair */
  476.  
  477.     (void) close(master_pty);
  478.     (void) close(slave_tty);
  479.  
  480.     /* make things look nice */
  481.  
  482.     fputs(cnl, stdout);
  483.  
  484.     exit(0);
  485.     /* NOTREACHED */
  486. }
  487. /*------------------------------------------------------------------*/
  488. /*
  489. Write a line to the slave_tty.
  490. Get the slave_tty parameters, turn off echo, send the line to the
  491. slave_tty, restore the slave_tty paramters to the way they were
  492. before. If echo was already off, this will have no effect.
  493. */
  494. write_line(line, length)
  495.     char *line;
  496.     int length;
  497. {
  498.     struct sgttyb params;
  499.     struct sgttyb new_params;
  500.  
  501.     /* get the current parameters */
  502.  
  503.     (void) ioctl(slave_tty, TIOCGETP, ¶ms);
  504.     new_params = params;
  505.     new_params.sg_flags &= ~ECHO;
  506.  
  507.     /* turn off echo so we don't see the characters twice */
  508.  
  509.     (void) ioctl(slave_tty, TIOCSETP, &new_params);
  510.  
  511.     (void) write(master_pty, line, length);
  512.  
  513.     /* set the parameters back the way they were */
  514.  
  515.     (void) ioctl(slave_tty, TIOCSETN, ¶ms);
  516. }
  517.  
  518. /*------------------------------------------------------------------*/
  519. /*
  520. The editing routines are called through the edit variable. This allows
  521. the quote and escape commands to be implemented as a straight forward
  522. state machine instead of requiring state flags and complex switch
  523. statements.
  524. */
  525. /*------------------------------------------------------------------*/
  526.  
  527. /* line edit buffer */
  528.  
  529. static char line[BUFFER_SIZE];
  530.  
  531. static int point;        /* insertion point */
  532. static int length;        /* total chars in buffer */
  533.  
  534. /* procedure to edit next character */
  535.  
  536. void (*edit) ();
  537.  
  538. /* history buffer */
  539.  
  540. struct
  541. {
  542.     int length;
  543.     char *line;
  544. } hist[HISTORY_SIZE];
  545.  
  546. int head;            /* insertion point */
  547. int here;            /* current displayed line */
  548.  
  549. /*------------------------------------------------------------------*/
  550. /*
  551. The delimiter vector is used by the forward, backward, and delete
  552. word operations to decide that a character is a delimiter.
  553. */
  554. /*------------------------------------------------------------------*/
  555.  
  556. #define CHAR_SET_SIZE 127
  557. #define CHAR_MASK 0177
  558.  
  559. char delimit[CHAR_SET_SIZE];
  560.  
  561. /*------------------------------------------------------------------*/
  562. /*
  563. The action_table is used to bind sequences of keys to operations or strings.
  564. */
  565. /*------------------------------------------------------------------*/
  566.  
  567. typedef enum
  568. {
  569.     is_action, is_string
  570. } action_type;
  571.  
  572. struct
  573. {
  574.     action_type flag;
  575.     union
  576.     {
  577.     void (*action) ();
  578.     char *string;
  579.     } aors;
  580. } action_table[4][CHAR_SET_SIZE];
  581.  
  582. /*------------------------------------------------------------------*/
  583.  
  584. void echo();
  585. void echoline();
  586. void cleartoend();
  587. void clearline();
  588. void backspace();
  589. void quote_edit();
  590. void edit_0();
  591. void edit_1();
  592. void edit_2();
  593. void edit_3();
  594. void bell();
  595. void insert();
  596.  
  597. /*------------------------------------------------------------------*/
  598. /*
  599. The following routines are action routines that are executed by the
  600. editor to carry out commands. Each routine has a single character
  601. argument. Each routine is invoked with the character that caused it
  602. to be invoked as its argument.
  603.  
  604. The argument isn't always useful, but it is included to provide a
  605. consistent interface for the routines.
  606. */
  607. /*------------------------------------------------------------------*/
  608. /*
  609. Given a specific directory and the starting string of a file name,
  610. find the longest partial file name that starts with the substring.
  611. */
  612. void
  613. complete_file_name(dir, name)
  614.     char *dir;
  615.     char *name;
  616. {
  617.     DIR *dirp;
  618.     struct direct *dp;
  619.  
  620.     int len;
  621.     int maxlen;
  622.     int oldlen;
  623.  
  624.     char oldname[MAXNAMLEN + 1];
  625.     char newname[MAXNAMLEN + 1];
  626.  
  627.     if ((dir != NULL) &&
  628.     (name != NULL) &&
  629.     ((oldlen = strlen(name)) > 0) &&
  630.     ((dirp = opendir(dir)) != NULL))
  631.     {
  632.     maxlen = oldlen;
  633.     (void) strcpy(oldname, name);
  634.     (void) strcpy(newname, name);
  635.  
  636.     /* find the longest name starting with name */
  637.  
  638.     for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  639.     {
  640.         if (dp->d_name != NULL)
  641.         {
  642.         len = strlen(dp->d_name);
  643.         if ((maxlen < len) &&
  644.             (strncmp(oldname, dp->d_name, oldlen) == 0))
  645.         {
  646.             maxlen = len;
  647.             (void) strcpy(newname, dp->d_name);
  648.         }
  649.         }
  650.     }
  651.  
  652.     rewinddir(dirp);
  653.  
  654.     /* find the longest common sub string */
  655.  
  656.     for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  657.     {
  658.         if (dp->d_name != NULL)
  659.         {
  660.         len = strlen(dp->d_name);
  661.         if ((len <= maxlen) &&
  662.             (strncmp(oldname, dp->d_name, oldlen) == 0))
  663.         {
  664.             for (;
  665.             (oldlen < len) &&
  666.             (strncmp(newname, dp->d_name, len) != 0);
  667.             len--);
  668.  
  669.             maxlen = len;
  670.             newname[maxlen] = '\0';
  671.         }
  672.         }
  673.     }
  674.  
  675.     if (strlen(name) != strlen(newname))
  676.     {
  677.         /* return the extended name */
  678.  
  679.         (void) strcpy(name, newname);
  680.     }
  681.     else
  682.     {
  683.         /* no difference so beep */
  684.  
  685.         bell('\0');
  686.     }
  687.  
  688.     (void) closedir(dirp);
  689.     }
  690. }
  691. /*------------------------------------------------------------------*/
  692. /*
  693. Hidden parameters to dirselect. They must be hidden because dirselect
  694. is passed as an argument to scandir.
  695. */
  696.  
  697. static char *namep;
  698. static int namelen;
  699.  
  700. /*------------------------------------------------------------------*/
  701. /*
  702. Passed to scandir. It is used to decide which files to display.
  703. */
  704.  
  705. static
  706. dirselect(dp)
  707.     struct direct *dp;
  708. {
  709.     return (strncmp(dp->d_name, namep, namelen) == 0);
  710. }
  711.  
  712. /*------------------------------------------------------------------*/
  713. /*
  714. List all the files in a given directory that start with the string
  715. passed as name.
  716. */
  717. void
  718. list_file_names(dir, name)
  719.     char *dir;
  720.     char *name;
  721. {
  722.     struct direct **dlist;
  723.     int i;
  724.     int nfiles;
  725.     int colwidth;
  726.     int cols;
  727.     int ncols;
  728.     int nlines;
  729.     int alphasort();
  730.  
  731.     if (dir == NULL || name == NULL)
  732.     {
  733.     return;
  734.     }
  735.  
  736.     cols = tty_winsize.ws_col;
  737.     if (cols <= 0)
  738.     {
  739.     cols = 80;
  740.     }
  741.  
  742.     namelen = strlen(name);
  743.     namep = name;
  744.  
  745.     nfiles = scandir(dir, &dlist, dirselect, alphasort);
  746.  
  747.     /* determine the longest file name length */
  748.  
  749.     colwidth = 8;        /* minimum width */
  750.     for (i = 0; i < nfiles; i++)
  751.     {
  752.     struct direct *dp;
  753.  
  754.     dp = dlist[i];
  755.  
  756.     if (dp->d_namlen > colwidth)
  757.     {
  758.         colwidth = dp->d_namlen;
  759.     }
  760.     }
  761.  
  762.     colwidth++;            /* at least 1 space between them */
  763.  
  764.     /* print the names, sorted vertically per column */
  765.     ncols = cols / colwidth;
  766.     if (ncols == 0)
  767.     {
  768.     /* longest filename is wider than the screen */
  769.     ncols = 1;
  770.     }
  771.  
  772.     nlines = (nfiles + ncols - 1) / ncols;
  773.  
  774.     if (nfiles > 0)
  775.     {
  776.     for (i = 0; i < nlines; i++)
  777.     {
  778.         int j;
  779.         int l;
  780.  
  781.         l = 0;
  782.         for (j = 0; j < ncols; j++)
  783.         {
  784.         int m;
  785.         struct direct *dp;
  786.  
  787.         m = l + i;
  788.         if (m >= nfiles)
  789.         {
  790.             break;
  791.         }
  792.  
  793.         dp = dlist[m];
  794.         fputs(dp->d_name, stdout);
  795.  
  796.         if (j < (ncols - 1))
  797.         {
  798.             int k;
  799.  
  800.             for (k = dp->d_namlen; k < colwidth; k++)
  801.             {
  802.             (void) fputc(' ', stdout);
  803.             }
  804.             l += nlines;
  805.         }
  806.         }
  807.         fputs(ccr, stdout);
  808.         fputs(cnl, stdout);
  809.     }
  810.     }
  811.     free((char *) dlist);
  812. }
  813. /*------------------------------------------------------------------*/
  814. /*
  815. Assuming that there is a file name under the cursor, return a path
  816. and a file name. If there is no path name return "."
  817. */
  818. int
  819. get_dir_and_name(dir, name, username, userend, start, middle, tail)
  820.     char *dir;
  821.     char *name;
  822.     char *username;
  823.     int *userend;
  824.     int *start;
  825.     int *middle;
  826.     int *tail;
  827. {
  828.     int dirlen;
  829.  
  830.     int newstart;
  831.  
  832.     int punlen;
  833.     char pun[USER_NAME_SIZE + 1];
  834.     struct passwd *userpwd;
  835.  
  836.     int i;
  837.  
  838.     /* set the default path and file name */
  839.  
  840.     dir[0] = '\0';
  841.     name[0] = '\0';
  842.     username[0] = '\0';
  843.  
  844.     /* search for the start of the file name */
  845.     /* start will be left pointing to the first character of the path  */
  846.  
  847.     for ((*start) = point;
  848.     ((0 < (*start)) && (line[(*start) - 1] != ' '));
  849.     (*start)--
  850.     );
  851.  
  852.     /* search for the end of the file name */
  853.     /* tail will be left pointing at the last character of the path */
  854.  
  855.     for ((*tail) = point - 1;
  856.     (((*tail) < (length - 1)) && (line[(*tail) + 1] != ' '));
  857.     (*tail)++
  858.     );
  859.  
  860.     /* search for the middle of the file name */
  861.     /* middle will be left pointing at the first character of the last
  862.      * element of the path */
  863.  
  864.     for ((*middle) = (*tail) + 1;
  865.     ((0 < (*middle)) &&
  866.         (line[(*middle) - 1] != '/') &&
  867.         (line[(*middle) - 1] != ' '));
  868.     (*middle)--
  869.     );
  870.  
  871.     /* copy path from line to dir */
  872.  
  873.     /* what base path */
  874.  
  875.     newstart = (*start);
  876.  
  877.     if ((line[newstart] == '~') &&
  878.     ((newstart + 1) < length) &&
  879.     (line[newstart + 1] == '/'))
  880.     {
  881.     /* "~/" means use the value of HOME */
  882.  
  883.     newstart++;
  884.     (void) strcpy(dir, homedir);
  885.     }
  886.     else if (line[newstart] == '~')
  887.     {
  888.     /* "~username" means use the users login directory */
  889.  
  890.     /* search for the end of the user name */
  891.  
  892.     for ((*userend) = newstart,
  893.         punlen = 0;
  894.         (((*userend) < (length - 1)) &&
  895.         (line[(*userend) + 1] != ' ') &&
  896.         (line[(*userend) + 1] != '/'));
  897.         (*userend)++,
  898.         punlen++);
  899.  
  900.     /* make middle point to middle */
  901.  
  902.     if ((*start) == (*middle))
  903.     {
  904.         (*middle) = (*start) + punlen + 1;
  905.     }
  906.  
  907.     /* extract partial user name from line */
  908.  
  909.     (void) strncpy(pun, &line[newstart + 1], punlen);
  910.     pun[punlen] = '\0';
  911.  
  912.     /* search passwd file for partial match */
  913.  
  914.     for (userpwd = getpwent();
  915.         userpwd != NULL;
  916.         userpwd = getpwent())
  917.     {
  918.         if ((punlen <= strlen(userpwd->pw_name)) &&
  919.         (strncmp(pun, userpwd->pw_name, punlen) == 0))
  920.         {
  921.  
  922.         /* we have a partial match, record it */
  923.  
  924.         if (strlen(dir) == 0)
  925.         {
  926.             newstart = (*userend) + 1;
  927.             (void) strcpy(dir, userpwd->pw_dir);
  928.             (void) strcpy(username, userpwd->pw_name);
  929.         }
  930.         else
  931.         {
  932.             /* second partial match, forget the first one. */
  933.  
  934.             newstart = (*start);
  935.             dir[0] = '\0';
  936.             username[0] = '\0';
  937.             return (FALSE);
  938.         }
  939.  
  940.         }
  941.     }
  942.     (void) setpwent();
  943.     }
  944.     else if ((line[newstart] == '.') &&
  945.         ((newstart + 1) < length) &&
  946.     (line[newstart + 1] == '/'))
  947.     {
  948.     /* if it's "./" use current dir */
  949.  
  950.     newstart++;
  951.     (void) strcpy(dir, currentdir);
  952.     }
  953.     else if ((line[newstart] == '.') &&
  954.         ((newstart + 1) < length) &&
  955.         (line[newstart + 1] == '.') &&
  956.         ((newstart + 2) < length) &&
  957.     (line[newstart + 2] == '/'))
  958.     {
  959.     /* if it's "../" strip off one name from currentdir and use that */
  960.  
  961.     newstart += 2;
  962.     (void) strcpy(dir, currentdir);
  963.     for (i = strlen(dir); (i > 0) && (dir[i] != '/'); i--)
  964.     {
  965.         /* nothing */
  966.     }
  967.     dir[i] = '\0';
  968.     }
  969.     else if (line[newstart] != '/')
  970.     {
  971.     /* doesn't start with a "/"? use currentdir */
  972.  
  973.     (void) strcpy(dir, currentdir);
  974.     (void) strcat(dir, "/");
  975.     }
  976.  
  977.     /* add on the rest of the path */
  978.  
  979.     dirlen = strlen(dir);
  980.     for (i = 0; i < ((*middle) - newstart); i++)
  981.     {
  982.     dir[dirlen + i] = line[newstart + i];
  983.     }
  984.     dir[dirlen + i] = '\0';
  985.  
  986.     /* copy file name from line to name */
  987.  
  988.     for (i = 0; i < ((*tail) - (*middle) + 1); i++)
  989.     {
  990.     name[i] = line[(*middle) + i];
  991.     }
  992.     name[i] = '\0';
  993.  
  994.     return (TRUE);
  995. }
  996. /*------------------------------------------------------------------*/
  997. /*
  998. Perform file name completion. Put the full path and file name in the
  999. line.
  1000. */
  1001. /*ARGSUSED*/
  1002. void
  1003. complete_file_full(ch)
  1004.     char ch;
  1005. {
  1006.     char dir[10 * (MAXNAMLEN + 1)];
  1007.     char name[MAXNAMLEN + 1];
  1008.     char username[USER_NAME_SIZE + 1];
  1009.  
  1010.     char newline[BUFFER_SIZE];
  1011.     int newlength;
  1012.     int newpoint;
  1013.  
  1014.     int userend;
  1015.     int start;
  1016.     int middle;
  1017.     int tail;
  1018.  
  1019.     int i;
  1020.  
  1021.     /* get the path and file name in the line */
  1022.  
  1023.     if (get_dir_and_name(dir,
  1024.         name,
  1025.         username,
  1026.         &userend,
  1027.         &start,
  1028.         &middle,
  1029.         &tail))
  1030.     {
  1031.  
  1032.     /* complete the file name if possible */
  1033.  
  1034.     complete_file_name(dir, name);
  1035.  
  1036.     /* create a new line */
  1037.  
  1038.     /* start with the line prefix */
  1039.  
  1040.     (void) strncpy(newline, line, start);
  1041.     newline[start] = '\0';
  1042.  
  1043.     /* add in the new path */
  1044.  
  1045.     (void) strcat(newline, dir);
  1046.  
  1047.     /* stick in the new file name */
  1048.  
  1049.     (void) strcat(newline, name);
  1050.     newpoint = strlen(newline);
  1051.  
  1052.     /* finish with the line postfix */
  1053.  
  1054.     (void) strncat(newline, &line[tail + 1], (length - tail - 1));
  1055.     newlength = strlen(newline);
  1056.  
  1057.     /* display the new line */
  1058.  
  1059.     clearline('\0');
  1060.  
  1061.     point = newpoint;
  1062.     length = newlength;
  1063.     (void) strncpy(line, newline, newlength);
  1064.  
  1065.     echoline(line, length);
  1066.  
  1067.     for (i = point; i < length; i++)
  1068.     {
  1069.         backspace(line[i]);
  1070.     }
  1071.     }
  1072.     else
  1073.     {
  1074.     bell('\0');
  1075.     }
  1076. }
  1077. /*------------------------------------------------------------------*/
  1078. /*
  1079. Perform file name completion in much the same style as csh.
  1080. */
  1081. /*ARGSUSED*/
  1082. void
  1083. complete_file(ch)
  1084.     char ch;
  1085. {
  1086.     char dir[10 * (MAXNAMLEN + 1)];
  1087.     char name[MAXNAMLEN + 1];
  1088.     char username[USER_NAME_SIZE + 1];
  1089.  
  1090.     int userend;
  1091.     int start;
  1092.     int middle;
  1093.     int tail;
  1094.  
  1095.     char newline[BUFFER_SIZE];
  1096.     int newlength;
  1097.     int newpoint;
  1098.  
  1099.     int userlen;
  1100.     int len;
  1101.  
  1102.     int i;
  1103.  
  1104.     /* get the path and file name in the line */
  1105.  
  1106.     if (get_dir_and_name(dir,
  1107.         name,
  1108.         username,
  1109.         &userend,
  1110.         &start,
  1111.         &middle,
  1112.         &tail))
  1113.     {
  1114.     /* how long is the user name */
  1115.  
  1116.     userlen = strlen(username);
  1117.  
  1118.     /* complete the file name if possible */
  1119.  
  1120.     complete_file_name(dir, name);
  1121.     /* create a new line */
  1122.  
  1123.     /* start with the line prefix */
  1124.  
  1125.     (void) strncpy(newline, line, start);
  1126.     newline[start] = '\0';
  1127.  
  1128.     /* add in the new username */
  1129.  
  1130.     if (userlen != 0)
  1131.     {
  1132.         /* put in new user name */
  1133.  
  1134.         (void) strcat(newline, "~");
  1135.         (void) strcat(newline, username);
  1136.         len = strlen(newline);
  1137.  
  1138.         /* put in the existing path */
  1139.  
  1140.         (void) strncat(newline, &line[userend + 1], middle - userend - 1);
  1141.         newline[len + (middle - userend - 1)] = '\0';
  1142.     }
  1143.     else
  1144.     {
  1145.         /* put in the existing path */
  1146.  
  1147.         len = strlen(newline);
  1148.         (void) strncat(newline, &line[start], middle - start);
  1149.         newline[len + (middle - start)] = '\0';
  1150.     }
  1151.  
  1152.     /* stick in the new file name */
  1153.  
  1154.     (void) strcat(newline, name);
  1155.     newpoint = strlen(newline);
  1156.  
  1157.     /* finish with the line postfix */
  1158.  
  1159.     (void) strncat(newline, &line[tail + 1], (length - tail - 1));
  1160.     newlength = strlen(newline);
  1161.  
  1162.     /* display the new line */
  1163.  
  1164.     clearline('\0');
  1165.  
  1166.     point = newpoint;
  1167.     length = newlength;
  1168.     (void) strncpy(line, newline, newlength);
  1169.  
  1170.     echoline(line, length);
  1171.  
  1172.     for (i = point; i < length; i++)
  1173.     {
  1174.         backspace(line[i]);
  1175.     }
  1176.     }
  1177.     else
  1178.     {
  1179.     bell('\0');
  1180.     }
  1181. }
  1182. /*------------------------------------------------------------------*/
  1183. /*
  1184. List the names of files that start with the directory path and
  1185. file name under the cursor.
  1186. */
  1187. /*ARGSUSED*/
  1188. void
  1189. show_files(ch)
  1190.     char ch;
  1191. {
  1192.     static char divider[] = "----------";
  1193.  
  1194.     void retype_line();
  1195.  
  1196.     char dir[10 * (MAXNAMLEN + 1)];
  1197.     char name[MAXNAMLEN + 1];
  1198.     char username[USER_NAME_SIZE + 1];
  1199.  
  1200.     int userend;
  1201.     int start;
  1202.     int middle;
  1203.     int tail;
  1204.  
  1205.     if (get_dir_and_name(dir,
  1206.         name,
  1207.         username,
  1208.         &userend,
  1209.         &start,
  1210.         &middle,
  1211.         &tail))
  1212.     {
  1213.  
  1214.     fputs(ccr, stdout);
  1215.     fputs(cnl, stdout);
  1216.  
  1217.     fputs(divider, stdout);
  1218.  
  1219.     fputs(ccr, stdout);
  1220.     fputs(cnl, stdout);
  1221.  
  1222.     list_file_names(dir, name);
  1223.  
  1224.     fputs(divider, stdout);
  1225.  
  1226.     fputs(ccr, stdout);
  1227.     fputs(cnl, stdout);
  1228.  
  1229.     retype_line('\0');
  1230.     }
  1231.     else
  1232.     {
  1233.     bell('\0');
  1234.     }
  1235. }
  1236. /*------------------------------------------------------------------*/
  1237. /*
  1238. Make the gross assumption that the program we are talking to is
  1239. a shell and send "pwd\n" to it. Whatever comes back is saved as the
  1240. value of currentdir.
  1241. */
  1242. /*ARGSUSED*/
  1243. void
  1244. query_path(ch)
  1245.     char ch;
  1246. {
  1247.     static char command[] = "pwd\n";
  1248.     char buffer[BUFFER_SIZE];
  1249.     int readfd;
  1250.     struct timeval timeout;
  1251.     int status;
  1252.     int cc;
  1253.     int i;
  1254.  
  1255.     /* send the command to the shell, we hope. */
  1256.  
  1257.     write_line(command, strlen(command));
  1258.  
  1259.     /* read the directory path back */
  1260.  
  1261.     readfd = 1 << master_pty;
  1262.     timeout.tv_sec = 2;
  1263.     timeout.tv_usec = 0;
  1264.  
  1265.     do
  1266.     {
  1267.     status = select(32,
  1268.         (fd_set *) & readfd,
  1269.         (fd_set *) NULL,
  1270.         (fd_set *) NULL,
  1271.         &timeout);
  1272.  
  1273.     if (0 < status)
  1274.     {
  1275.         cc = read(master_pty, currentdir, sizeof(currentdir));
  1276.  
  1277.         /* strip off trailing control chars and blanks */
  1278.  
  1279.         for (i = cc - 1; (currentdir[i] <= ' ') && (i > 0); i--)
  1280.         {
  1281.         currentdir[i] = '\0';
  1282.         }
  1283.  
  1284.         /* read the prompt so it can be ignored */
  1285.  
  1286.         readfd = 1 << master_pty;
  1287.         timeout.tv_sec = 2;
  1288.         timeout.tv_usec = 0;
  1289.  
  1290.         do
  1291.         {
  1292.         status = select(32,
  1293.             (fd_set *) & readfd,
  1294.             (fd_set *) NULL,
  1295.             (fd_set *) NULL,
  1296.             &timeout);
  1297.  
  1298.         if (0 < status)
  1299.         {
  1300.             cc = read(master_pty, buffer, sizeof(buffer));
  1301.         }
  1302.         else if ((-1 == status) && windowchanged)
  1303.         {
  1304.             windowchanged = FALSE;
  1305.         }
  1306.         } while (status == -1);
  1307.     }
  1308.     else if ((-1 == status) && windowchanged)
  1309.     {
  1310.         windowchanged = FALSE;
  1311.     }
  1312.     } while (status == -1);
  1313. }
  1314. /*------------------------------------------------------------------*/
  1315. /*
  1316. Ring the bell on the terminal.
  1317. */
  1318. /*ARGSUSED*/
  1319. void
  1320. bell(ch)
  1321.     char ch;
  1322. {
  1323.     fputs(cbl, stdout);
  1324. }
  1325. /*------------------------------------------------------------------*/
  1326. /*
  1327. Pass characters to the slave. Don't mess with them at all.
  1328. */
  1329. void
  1330. pass(ch)
  1331.     char ch;
  1332. {
  1333.     (void) write(master_pty, &ch, 1);
  1334. }
  1335. /*------------------------------------------------------------------*/
  1336. /*
  1337. Insert a character at point in the line buffer. While we are at it
  1338. update the display to show the insertion.
  1339. */
  1340. void
  1341. insert(ch)
  1342.     char ch;
  1343. {
  1344.     int i;
  1345.  
  1346.     if (length < (BUFFER_SIZE - 2))
  1347.     {
  1348.  
  1349.     /* display the character */
  1350.  
  1351.     echo(ch);
  1352.  
  1353.     /* redisplay the rest of the line */
  1354.  
  1355.     echoline(&line[point], (length - point));
  1356.  
  1357.     /* move the characters in the line buffer */
  1358.     /* and put the cursor back at point */
  1359.  
  1360.     for (i = length; i > point; i--)
  1361.     {
  1362.         line[i] = line[i - 1];
  1363.         backspace(line[i]);
  1364.     }
  1365.  
  1366.     /* add the character to the line buffer */
  1367.     /* and increment point and length */
  1368.  
  1369.     line[point] = ch;
  1370.     length++;
  1371.     point++;
  1372.     }
  1373.     else
  1374.     {
  1375.     bell('\0');
  1376.     }
  1377. }
  1378. /*------------------------------------------------------------------*/
  1379. /*
  1380. Transpose the letter under the cursor and the letter immediately to
  1381. the left of the cursor.
  1382. */
  1383. /*ARGSUSED*/
  1384. void
  1385. transpose_chars(ch)
  1386.     char ch;
  1387. {
  1388.     char tch;
  1389.  
  1390.     if ((0 < point) && (point < length))
  1391.     {
  1392.     /* first, update the display */
  1393.  
  1394.     backspace(line[point]);
  1395.  
  1396.     echo(line[point]);
  1397.     echo(line[point - 1]);
  1398.  
  1399.     /* now swap the chars in the line buffer */
  1400.  
  1401.     tch = line[point];
  1402.     line[point] = line[point - 1];
  1403.     line[point - 1] = tch;
  1404.  
  1405.     /* point moved forward one char */
  1406.  
  1407.     point++;
  1408.     }
  1409. }
  1410. /*------------------------------------------------------------------*/
  1411. /*
  1412. Delete a character at point in the line buffer. While we are at it
  1413. update the display to reflect the deletion.
  1414. */
  1415. /*ARGSUSED*/
  1416. void
  1417. delete_char_under(ch)
  1418.     char ch;
  1419. {
  1420.     int i;
  1421.  
  1422.     if (point < length)
  1423.     {
  1424.  
  1425.     /* clear to the end of the line */
  1426.  
  1427.     cleartoend();
  1428.  
  1429.     /* retype the rest of the line */
  1430.  
  1431.     echoline(&line[point + 1], (length - point - 1));
  1432.  
  1433.     /* build the new line */
  1434.  
  1435.     for (i = point + 1; i < length; i++)
  1436.     {
  1437.         line[i - 1] = line[i];
  1438.         backspace(line[i]);
  1439.     }
  1440.  
  1441.     length--;
  1442.  
  1443.     if (point > length)
  1444.     {
  1445.         point = length;
  1446.     }
  1447.     }
  1448.  
  1449. }
  1450. /*------------------------------------------------------------------*/
  1451. /*
  1452. Delete the character to the left of point in the line buffer. While we
  1453. are at it update the display to reflect the deletion.
  1454. */
  1455. /*ARGSUSED*/
  1456. void
  1457. delete_char(ch)
  1458.     char ch;
  1459. {
  1460.     int i;
  1461.  
  1462.     if (point > 0)
  1463.     {
  1464.     /* move the cursor left one character */
  1465.  
  1466.     backspace(line[point - 1]);
  1467.  
  1468.     /* clear to the end of the line */
  1469.  
  1470.     cleartoend();
  1471.  
  1472.     /* retype the rest of the line */
  1473.  
  1474.     echoline(&line[point], (length - point));
  1475.  
  1476.     /* build the new line */
  1477.  
  1478.     for (i = point; i < length; i++)
  1479.     {
  1480.         line[i - 1] = line[i];
  1481.         backspace(line[i]);
  1482.     }
  1483.  
  1484.     length--;
  1485.     point--;
  1486.     }
  1487.  
  1488. }
  1489. /*------------------------------------------------------------------*/
  1490. /*
  1491. Bind the edit vector to quote_edit so that the next character
  1492. will be placed in the line buffer.
  1493. */
  1494. /*ARGSUSED*/
  1495. void
  1496. quote(ch)
  1497.     char ch;
  1498. {
  1499.     edit = quote_edit;
  1500. }
  1501. /*------------------------------------------------------------------*/
  1502. /*
  1503. The next character will select an action from action_table[1]
  1504. */
  1505. /*ARGSUSED*/
  1506. void
  1507. escape_1(ch)
  1508.     char ch;
  1509. {
  1510.     edit = edit_1;
  1511. }
  1512. /*------------------------------------------------------------------*/
  1513. /*
  1514. The next character will select an action from action_table[2]
  1515. */
  1516. /*ARGSUSED*/
  1517. void
  1518. escape_2(ch)
  1519.     char ch;
  1520. {
  1521.     edit = edit_2;
  1522. }
  1523. /*------------------------------------------------------------------*/
  1524. /*
  1525. The next character will select an action from action_table[3]
  1526. */
  1527. /*ARGSUSED*/
  1528. void
  1529. escape_3(ch)
  1530.     char ch;
  1531. {
  1532.     edit = edit_3;
  1533. }
  1534. /*------------------------------------------------------------------*/
  1535. /*
  1536. Delete the word to the left of the cursor.
  1537. */
  1538. /*ARGSUSED*/
  1539. void
  1540. delete_word(ch)
  1541.     char ch;
  1542. {
  1543.     int i;
  1544.     int old;
  1545.  
  1546.     if (length > 0)
  1547.     {
  1548.     /* find the new deletion point */
  1549.  
  1550.     old = point;
  1551.  
  1552.     /* first skip over any delimiters */
  1553.  
  1554.     for (; (point > 0) && (delimit[line[point - 1]]); point--)
  1555.     {
  1556.         backspace(line[point - 1]);
  1557.     }
  1558.  
  1559.     /* now delete until we find a delimiter */
  1560.  
  1561.     for (; (point > 0) && (!delimit[line[point - 1]]); point--)
  1562.     {
  1563.         backspace(line[point - 1]);
  1564.     }
  1565.  
  1566.     /* clear to the end of the line */
  1567.  
  1568.     cleartoend();
  1569.  
  1570.     /* retype the rest of the line */
  1571.  
  1572.     echoline(&line[old], (length - old));
  1573.  
  1574.     /* construct the new line */
  1575.  
  1576.     for (i = 0; i < (length - old); i++)
  1577.     {
  1578.         line[point + i] = line[old + i];
  1579.         backspace(line[point + i]);
  1580.     }
  1581.  
  1582.     /* update the length */
  1583.  
  1584.     length = length - (old - point);
  1585.     }
  1586. }
  1587. /*------------------------------------------------------------------*/
  1588. /*
  1589. Go forward one word.
  1590. */
  1591. /*ARGSUSED*/
  1592. void
  1593. forward_word(ch)
  1594.     char ch;
  1595. {
  1596.     if (length > 0)
  1597.     {
  1598.     /* first skip any delimiters */
  1599.  
  1600.     for (; (point < length) && (delimit[line[point]]); point++)
  1601.     {
  1602.         echo(line[point]);
  1603.     }
  1604.  
  1605.     /* now skip until we find a delimiter */
  1606.  
  1607.     for (; (point < length) && (!delimit[line[point]]); point++)
  1608.     {
  1609.         echo(line[point]);
  1610.     }
  1611.     }
  1612.  
  1613. }
  1614. /*------------------------------------------------------------------*/
  1615. /*
  1616. Lower case the word.
  1617. */
  1618. /*ARGSUSED*/
  1619. void
  1620. lower_word(ch)
  1621.     char ch;
  1622. {
  1623.     if (length > 0)
  1624.     {
  1625.     /* first skip any delimiters */
  1626.  
  1627.     for (; (point < length) && (delimit[line[point]]); point++)
  1628.     {
  1629.         echo(line[point]);
  1630.     }
  1631.  
  1632.     /* now skip until we find a delimiter */
  1633.  
  1634.     for (; (point < length) && (!delimit[line[point]]); point++)
  1635.     {
  1636.         if ((line[point] >= 'A') && (line[point] <= 'Z'))
  1637.         {
  1638.         line[point] = line[point] - 'A' + 'a';
  1639.         echo(line[point]);
  1640.         }
  1641.         else
  1642.         {
  1643.         echo(line[point]);
  1644.         }
  1645.     }
  1646.     }
  1647.  
  1648. }
  1649. /*------------------------------------------------------------------*/
  1650. /*
  1651. Upper case the word.
  1652. */
  1653. /*ARGSUSED*/
  1654. void
  1655. upper_word(ch)
  1656.     char ch;
  1657. {
  1658.     if (length > 0)
  1659.     {
  1660.     /* first skip any delimiters */
  1661.  
  1662.     for (; (point < length) && (delimit[line[point]]); point++)
  1663.     {
  1664.         echo(line[point]);
  1665.     }
  1666.  
  1667.     /* now skip until we find a delimiter */
  1668.  
  1669.     for (; (point < length) && (!delimit[line[point]]); point++)
  1670.     {
  1671.         if ((line[point] >= 'a') && (line[point] <= 'z'))
  1672.         {
  1673.         line[point] = line[point] - 'a' + 'A';
  1674.         echo(line[point]);
  1675.         }
  1676.         else
  1677.         {
  1678.         echo(line[point]);
  1679.         }
  1680.     }
  1681.     }
  1682.  
  1683. }
  1684. /*------------------------------------------------------------------*/
  1685. /*
  1686. Capitalize the word.
  1687. */
  1688. /*ARGSUSED*/
  1689. void
  1690. capitalize_word(ch)
  1691.     char ch;
  1692. {
  1693.     if (length > 0)
  1694.     {
  1695.     /* first skip any delimiters */
  1696.  
  1697.     for (; (point < length) && (delimit[line[point]]); point++)
  1698.     {
  1699.         echo(line[point]);
  1700.     }
  1701.  
  1702.     /* now skip until we find a delimiter */
  1703.  
  1704.     if ((point < length) && (!delimit[line[point]]))
  1705.     {
  1706.         if ((line[point] >= 'a') && (line[point] <= 'z'))
  1707.         {
  1708.         line[point] = line[point] - 'a' + 'A';
  1709.         echo(line[point]);
  1710.         }
  1711.         else
  1712.         {
  1713.         echo(line[point]);
  1714.         }
  1715.     }
  1716.     point++;
  1717.  
  1718.     for (; (point < length) && (!delimit[line[point]]); point++)
  1719.     {
  1720.         if ((line[point] >= 'A') && (line[point] <= 'Z'))
  1721.         {
  1722.         line[point] = line[point] - 'A' + 'a';
  1723.         echo(line[point]);
  1724.         }
  1725.         else
  1726.         {
  1727.         echo(line[point]);
  1728.         }
  1729.     }
  1730.     }
  1731.  
  1732. }
  1733. /*------------------------------------------------------------------*/
  1734. /*
  1735. Go backward one word.
  1736. */
  1737. /*ARGSUSED*/
  1738. void
  1739. backward_word(ch)
  1740.     char ch;
  1741. {
  1742.     if (length > 0)
  1743.     {
  1744.     /* first backspace over any delimiters */
  1745.  
  1746.     for (; (point > 0) && (delimit[line[point - 1]]); point--)
  1747.     {
  1748.         backspace(line[point - 1]);
  1749.     }
  1750.  
  1751.     /* now backspace until we find a delimiter */
  1752.  
  1753.     for (; (point > 0) && (!delimit[line[point - 1]]); point--)
  1754.     {
  1755.         backspace(line[point - 1]);
  1756.     }
  1757.     }
  1758.  
  1759. }
  1760. /*------------------------------------------------------------------*/
  1761. /*
  1762. Move the cursor to the start of the line.
  1763. */
  1764. /*ARGSUSED*/
  1765. void
  1766. start_of_line(ch)
  1767.     char ch;
  1768. {
  1769.     int i;
  1770.  
  1771.     if (length > 0)
  1772.     {
  1773.     for (i = 0; i < point; i++)
  1774.     {
  1775.         backspace(line[i]);
  1776.     }
  1777.     point = 0;
  1778.     }
  1779. }
  1780. /*------------------------------------------------------------------*/
  1781. /*
  1782. Move the cursor one character to the left.
  1783. */
  1784. /*ARGSUSED*/
  1785. void
  1786. backward_char(ch)
  1787.     char ch;
  1788. {
  1789.     if ((length > 0) && (point > 0))
  1790.     {
  1791.     backspace(line[point - 1]);
  1792.     point--;
  1793.     }
  1794. }
  1795. /*------------------------------------------------------------------*/
  1796. /*
  1797. Move the cursor to the right of the last character on the line.
  1798. */
  1799. /*ARGSUSED*/
  1800. void
  1801. end_of_line(ch)
  1802.     char ch;
  1803. {
  1804.     if ((length > 0) && (point < length))
  1805.     {
  1806.     echoline(&line[point], (length - point));
  1807.     point = length;
  1808.     }
  1809. }
  1810. /*------------------------------------------------------------------*/
  1811. /*
  1812. Move the cursor one character to the right.
  1813. */
  1814. /*ARGSUSED*/
  1815. void
  1816. forward_char(ch)
  1817.     char ch;
  1818. {
  1819.     if ((length > 0) && (point < length))
  1820.     {
  1821.     echo(line[point]);
  1822.     point++;
  1823.     }
  1824. }
  1825. /*------------------------------------------------------------------*/
  1826. /*
  1827. Add a line to the history buffer and pass it to the child process
  1828. as input.
  1829. */
  1830. /*ARGSUSED*/
  1831. void
  1832. add_to_history(ch)
  1833.     char ch;
  1834. {
  1835.     /* Put the line in the history buffer. Make here point to the current
  1836.      * line. And increment head to point to the next history slot. */
  1837.  
  1838.     /* If the current line is identical to the current history line, don't
  1839.      * add it. */
  1840.  
  1841.     /* don't save blank lines */
  1842.  
  1843.     int prev;
  1844.  
  1845.     if ((head - 1) < 0)
  1846.     {
  1847.     prev = HISTORY_SIZE - 1;
  1848.     }
  1849.     else
  1850.     {
  1851.     prev = head - 1;
  1852.     }
  1853.  
  1854.     if ((length != 0) &&
  1855.     ((length != hist[prev].length) ||
  1856.         (strncmp(hist[prev].line, line, length) != 0)))
  1857.     {
  1858.     /* set the length of the entry */
  1859.  
  1860.     hist[head].length = length;
  1861.  
  1862.     /* make sure there is enough storage for the new line */
  1863.  
  1864.     if (hist[head].line == NULL)
  1865.     {
  1866.         if ((hist[head].line = (char *) malloc((unsigned) length)) == NULL)
  1867.         {
  1868.         perror("ile");
  1869.         }
  1870.     }
  1871.     else
  1872.     {
  1873.         if ((hist[head].line =
  1874.             (char *) realloc(hist[head].line, (unsigned) length))
  1875.         == NULL)
  1876.         {
  1877.         perror("ile");
  1878.         }
  1879.     }
  1880.  
  1881.     (void) strncpy(hist[head].line, line, length);
  1882.  
  1883.     head = (head + 1) % HISTORY_SIZE;
  1884.  
  1885.     if (hist[head].line != NULL)
  1886.     {
  1887.         free(hist[head].line);
  1888.         hist[head].length = 0;
  1889.         hist[head].line = NULL;
  1890.     }
  1891.     }
  1892.  
  1893.     /* reset here */
  1894.  
  1895.     here = head;
  1896.  
  1897.     /* Echo a carriage return or a newline as a cr-nl sequence. Then send the
  1898.      * line to the child process. Finally, clear the buffer for reuse. */
  1899.  
  1900.     fputs(ccr, stdout);
  1901.     fputs(cnl, stdout);
  1902.  
  1903.     line[length] = nl;
  1904.     length++;
  1905.  
  1906.     write_line(line, length);
  1907.  
  1908.     point = 0;
  1909.     length = 0;
  1910.  
  1911. }
  1912. /*------------------------------------------------------------------*/
  1913. /*
  1914. Erase the entire line.
  1915. */
  1916. /*ARGSUSED*/
  1917. void
  1918. erase_line(ch)
  1919.     char ch;
  1920. {
  1921.     /* remove any text from the display */
  1922.  
  1923.     clearline(ch);
  1924.  
  1925.     /* nothing in the line buffer */
  1926.  
  1927.     point = 0;
  1928.     length = 0;
  1929.  
  1930.     /* reset here */
  1931.  
  1932.     here = head;
  1933.  
  1934. }
  1935. /*------------------------------------------------------------------*/
  1936. /*
  1937. Erase from the current cursor position to the end of the line.
  1938. */
  1939. /*ARGSUSED*/
  1940. void
  1941. erase_to_end_of_line(ch)
  1942.     char ch;
  1943. {
  1944.     if ((length > 0) && (point < length))
  1945.     {
  1946.     cleartoend();
  1947.     length = point;
  1948.     }
  1949.  
  1950. }
  1951. /*------------------------------------------------------------------*/
  1952. /*
  1953. Retype the current contents of the edit buffer.
  1954. */
  1955. /*ARGSUSED*/
  1956. void
  1957. retype_line(ch)
  1958.     char ch;
  1959. {
  1960.     int i;
  1961.  
  1962.     fputs(ccr, stdout);
  1963.     fputs(cnl, stdout);
  1964.  
  1965.     echoline(line, length);
  1966.  
  1967.     for (i = point; i < length; i++)
  1968.     {
  1969.     backspace(line[i]);
  1970.     }
  1971. }
  1972. /*------------------------------------------------------------------*/
  1973. /*
  1974. Go to the the next entry in the history buffer and display it.
  1975. If we are past the last history entry, then beep.
  1976. */
  1977. void
  1978. forward_history(ch)
  1979.     char ch;
  1980. {
  1981.     if (here != head)
  1982.     {
  1983.     clearline(ch);
  1984.  
  1985.     here = (here + 1) % HISTORY_SIZE;
  1986.     length = hist[here].length;
  1987.     point = length;
  1988.  
  1989.     (void) strncpy(line, hist[here].line, length);
  1990.     echoline(line, length);
  1991.     }
  1992.     else
  1993.     {
  1994.     bell('\0');
  1995.     }
  1996. }
  1997. /*------------------------------------------------------------------*/
  1998. /*
  1999. Search backward in the history list for a line that starts with
  2000. the characters left of the cursor. If it is found make it the
  2001. current line.
  2002. */
  2003. void
  2004. search_backward_history(ch)
  2005.     char ch;
  2006. {
  2007.     int prev;
  2008.     int i;
  2009.  
  2010.     /* search backward in the history */
  2011.  
  2012.     prev = here;
  2013.  
  2014.     do
  2015.     {
  2016.     prev--;
  2017.  
  2018.     if (prev < 0)
  2019.     {
  2020.         prev = HISTORY_SIZE - 1;
  2021.     }
  2022.     }
  2023.     while ((hist[prev].line != NULL) &&
  2024.     (strncmp(line, hist[prev].line, point) != 0));
  2025.  
  2026.     /* if something was found, make it the current line */
  2027.  
  2028.     if (hist[prev].line != NULL)
  2029.     {
  2030.     /* remember the position in the history */
  2031.  
  2032.     here = prev;
  2033.  
  2034.     /* set the length and point correctly */
  2035.  
  2036.     length = hist[here].length;
  2037.     if (point > length)
  2038.     {
  2039.         point = length;
  2040.     }
  2041.  
  2042.     /* redraw the line */
  2043.  
  2044.     clearline(ch);
  2045.     (void) strncpy(line, hist[here].line, hist[here].length);
  2046.     echoline(line, length);
  2047.  
  2048.     for (i = point; i < length; i++)
  2049.     {
  2050.         backspace(line[i]);
  2051.     }
  2052.     }
  2053.     else
  2054.     {
  2055.     bell('\0');
  2056.     }
  2057. }
  2058. /*------------------------------------------------------------------*/
  2059. /*
  2060. Go back one entry in the history buffer and display it. If we are
  2061. already at the last entry, then beep.
  2062. */
  2063. void
  2064. backward_history(ch)
  2065.     char ch;
  2066. {
  2067.     int prev;
  2068.  
  2069.     prev = here - 1;
  2070.  
  2071.     if (prev < 0)
  2072.     {
  2073.     prev = HISTORY_SIZE - 1;
  2074.     }
  2075.  
  2076.     if (hist[prev].line != NULL)
  2077.     {
  2078.     clearline(ch);
  2079.  
  2080.     here = prev;
  2081.     length = hist[here].length;
  2082.     point = length;
  2083.  
  2084.     (void) strncpy(line, hist[here].line, length);
  2085.     echoline(line, length);
  2086.     }
  2087.     else
  2088.     {
  2089.     bell('\0');
  2090.     }
  2091. }
  2092. /*------------------------------------------------------------------*/
  2093. /*
  2094. The following routines are utility routines used by the editing
  2095. routines.
  2096. */
  2097. /*------------------------------------------------------------------*/
  2098. /*
  2099. Clear to the end of the current input line.
  2100. */
  2101. void
  2102. cleartoend()
  2103. {
  2104.     /* send the clear character */
  2105.  
  2106.     fputs(cce, stdout);
  2107.  
  2108.     /* send somes nulls for padding */
  2109.  
  2110.     fputs("\0\0\0\0", stdout);
  2111. }
  2112. /*------------------------------------------------------------------*/
  2113. /*
  2114. Clear the input line. Backspace to the start of the line. Then clear
  2115. to the end of the line.
  2116. */
  2117. /*ARGSUSED*/
  2118. void
  2119. clearline(ch)
  2120.     char ch;
  2121. {
  2122.     int i;
  2123.  
  2124.     for (i = 0; i < point; i++)
  2125.     {
  2126.     backspace(line[i]);
  2127.     }
  2128.  
  2129.     cleartoend();
  2130. }
  2131. /*------------------------------------------------------------------*/
  2132. /*
  2133. Echo a character. Not all characters are created equal. Control characters
  2134. are echoed in ^X form. So they take up two character positions instead of
  2135. the normal 1 character position.
  2136. */
  2137. void
  2138. echo(ch)
  2139.     char ch;
  2140. {
  2141.     /* how should we echo the char? */
  2142.  
  2143.     if (ch < ' ')
  2144.     {
  2145.     (void) fputc('^', stdout);
  2146.     (void) fputc('@' + ch, stdout);
  2147.     }
  2148.     else
  2149.     {
  2150.     (void) fputc(ch, stdout);
  2151.     }
  2152. }
  2153. /*------------------------------------------------------------------*/
  2154. /*
  2155. Echo a line. Print a whole line with control characters printed in
  2156. ^X form.
  2157. */
  2158. void
  2159. echoline(line, length)
  2160.     char *line;
  2161.     int length;
  2162. {
  2163.     int i;
  2164.  
  2165.     for (i = 0; i < length; i++)
  2166.     {
  2167.     echo(*line++);
  2168.     }
  2169.  
  2170. }
  2171. /*------------------------------------------------------------------*/
  2172. /*
  2173. Backspace over a character. Generate enough bs characters to backspace
  2174. over any character.
  2175. */
  2176. void
  2177. backspace(ch)
  2178.     char ch;
  2179. {
  2180.     if (ch < ' ')
  2181.     {
  2182.     fputs(cle, stdout);
  2183.     fputs(cle, stdout);
  2184.     }
  2185.     else
  2186.     {
  2187.     fputs(cle, stdout);
  2188.     }
  2189. }
  2190. /*------------------------------------------------------------------*/
  2191. /*
  2192. Add any character to the line buffer.
  2193. */
  2194. void
  2195. quote_edit(ch)
  2196.     char ch;
  2197. {
  2198.     insert(ch);
  2199.  
  2200.     edit = edit_0;
  2201. }
  2202. /*------------------------------------------------------------------*/
  2203. /*
  2204. Given a character and an action table number either execute the
  2205. action or pass the string to (*edit)(ch)
  2206. */
  2207. void
  2208. dispatch(table, ch)
  2209.     int table;
  2210.     char ch;
  2211. {
  2212.     char *cptr;
  2213.  
  2214.     switch (action_table[table][ch].flag)
  2215.     {
  2216.     case is_action:
  2217.  
  2218.     (*(action_table[table][ch].aors.action)) (ch);
  2219.  
  2220.     break;
  2221.  
  2222.     case is_string:
  2223.  
  2224.     cptr = action_table[table][ch].aors.string;
  2225.     while ((*cptr) != '\0')
  2226.     {
  2227.         (*edit) (*cptr);
  2228.         cptr++;
  2229.     }
  2230.  
  2231.     break;
  2232.     }
  2233. }
  2234. /*------------------------------------------------------------------*/
  2235. /*
  2236. Select an action from action_table[3] and execute it.
  2237. */
  2238. void
  2239. edit_3(ch)
  2240.     char ch;
  2241. {
  2242.     /* reset so that next input is handled by edit_0 unless over ridden by
  2243.      * the action. */
  2244.  
  2245.     edit = edit_0;
  2246.     dispatch(3, ch);
  2247.     (void) fflush(stdout);
  2248. }
  2249. /*------------------------------------------------------------------*/
  2250. /*
  2251. Select an action from action_table[2] and execute it.
  2252. */
  2253. void
  2254. edit_2(ch)
  2255.     char ch;
  2256. {
  2257.     /* reset so that next input is handled by edit_0 unless over ridden by
  2258.      * the action. */
  2259.  
  2260.     edit = edit_0;
  2261.     dispatch(2, ch);
  2262.     (void) fflush(stdout);
  2263. }
  2264. /*------------------------------------------------------------------*/
  2265. /*
  2266. Select an action from action_table[1] and execute it.
  2267. */
  2268. void
  2269. edit_1(ch)
  2270.     char ch;
  2271. {
  2272.     /* reset so that next input is handled by edit_0 unless over ridden by
  2273.      * the action. */
  2274.  
  2275.     edit = edit_0;
  2276.     dispatch(1, ch);
  2277.     (void) fflush(stdout);
  2278. }
  2279. /*------------------------------------------------------------------*/
  2280. /*
  2281. Select an action from action_table[0] and execute it.
  2282. */
  2283. void
  2284. edit_0(ch)
  2285.     char ch;
  2286. {
  2287.     dispatch(0, ch);
  2288.     (void) fflush(stdout);
  2289. }
  2290. /*------------------------------------------------------------------*/
  2291. /*
  2292. Input line editor.
  2293.  
  2294. Initialize the world. Then loop forever using select to wait for
  2295. characters to be available from either stdin or from master_pty.
  2296. When characters are available, pass them on after doing any needed
  2297. editing.
  2298. */
  2299. void
  2300. ile()
  2301. {
  2302.     /* general purpose integer variable */
  2303.  
  2304.     int i;
  2305.  
  2306.     /* arguments for read and write calls */
  2307.  
  2308.     char buffer[BUFFER_SIZE];
  2309.     int cc;
  2310.  
  2311.     /* current slave_tty parameters */
  2312.  
  2313.     struct sgttyb slave_params;
  2314.  
  2315.     /* Arguments for select call */
  2316.  
  2317.     int nfds;
  2318.     int width;
  2319.     int readfds;
  2320.  
  2321.     /* what to do if the child or parent dies */
  2322.  
  2323.     (void) signal(SIGCHLD, clean_up);
  2324.     (void) signal(SIGSEGV, clean_up);
  2325.     (void) signal(SIGBUS, clean_up);
  2326.     (void) signal(SIGTERM, clean_up);
  2327.     (void) signal(SIGHUP, clean_up);
  2328.     (void) signal(SIGINT, clean_up);
  2329.     (void) signal(SIGQUIT, clean_up);
  2330.  
  2331.     /* what to do it the window changes size */
  2332.  
  2333.     (void) signal(SIGWINCH, change_window);
  2334.  
  2335.     /* Get all the different pieces of the current ttys' state and copy them
  2336.      * to the slave_tty. */
  2337.  
  2338.     /* tty sgttyb */
  2339.  
  2340.     (void) ioctl(READ, TIOCGETP, &tty_sgttyb);
  2341.     (void) ioctl(slave_tty, TIOCSETP, &tty_sgttyb);
  2342.  
  2343.     /* tty line discipline */
  2344.  
  2345.     (void) ioctl(READ, TIOCGETD, &tty_ldisc);
  2346.     (void) ioctl(slave_tty, TIOCSETD, &tty_ldisc);
  2347.  
  2348.     /* tty tchars */
  2349.  
  2350.     (void) ioctl(READ, TIOCGETC, &tty_tchars);
  2351.     (void) ioctl(slave_tty, TIOCSETC, &tty_tchars);
  2352.  
  2353.     /* tty mode */
  2354.  
  2355.     (void) ioctl(READ, TIOCLGET, &tty_mode);
  2356.     (void) ioctl(slave_tty, TIOCLSET, &tty_mode);
  2357.  
  2358.     /* tty ltchars */
  2359.  
  2360.     (void) ioctl(READ, TIOCGLTC, &tty_ltchars);
  2361.     (void) ioctl(slave_tty, TIOCSLTC, &tty_ltchars);
  2362.  
  2363.     /* tty windsize */
  2364.  
  2365.     (void) ioctl(READ, TIOCGWINSZ, &tty_winsize);
  2366.     (void) ioctl(slave_tty, TIOCSWINSZ, &tty_winsize);
  2367.     windowchanged = FALSE;
  2368.  
  2369.     /* "login" the user */
  2370.  
  2371.     (void) setutmp(slave_tty, TRUE);
  2372.  
  2373.     /* set raw mode on tty */
  2374.     {
  2375.     struct sgttyb params;
  2376.     struct tchars tparams;
  2377.     struct ltchars ltparams;
  2378.  
  2379.     /* Simulate RAW but allow original parity to work.  Thus we use
  2380.      * CBREAK with all the options turned off. */
  2381.  
  2382.     params = tty_sgttyb;
  2383.     params.sg_flags = CBREAK;
  2384.     (void) ioctl(READ, TIOCSETP, ¶ms);
  2385.  
  2386.     tparams = tty_tchars;
  2387.     tparams.t_intrc = -1;
  2388.     tparams.t_quitc = -1;
  2389.     tparams.t_startc = -1;
  2390.     tparams.t_stopc = -1;
  2391.     tparams.t_eofc = -1;
  2392.     tparams.t_brkc = -1;
  2393.     (void) ioctl(READ, TIOCSETC, &tparams);
  2394.  
  2395.     ltparams = tty_ltchars;
  2396.     ltparams.t_suspc = -1;
  2397.     ltparams.t_dsuspc = -1;
  2398.     ltparams.t_rprntc = -1;
  2399.     ltparams.t_flushc = -1;
  2400.     ltparams.t_lnextc = -1;
  2401.     (void) ioctl(READ, TIOCSLTC, <params);
  2402.     }
  2403.  
  2404.     /* set new mode on tty */
  2405.  
  2406.     {
  2407.     int mode;
  2408.     mode = LNOFLSH | LDECCTQ | LLITOUT;
  2409.     (void) ioctl(READ, TIOCLSET, &mode);
  2410.     }
  2411.  
  2412.     /* get descriptor table size */
  2413.  
  2414.     width = getdtablesize();
  2415.     if (width > 32)
  2416.     {
  2417.     width = 32;
  2418.     }
  2419.  
  2420.     /* set initial edit function */
  2421.  
  2422.     edit = edit_0;
  2423.  
  2424.     /* initialize line buffer */
  2425.  
  2426.     point = 0;
  2427.     length = 0;
  2428.  
  2429.     /* initialize history buffer */
  2430.  
  2431.     head = 0;
  2432.     here = 0;
  2433.  
  2434.     for (i = 0; i < HISTORY_SIZE; i++)
  2435.     {
  2436.     hist[i].length = 0;
  2437.     hist[i].line = NULL;
  2438.     }
  2439.  
  2440.     for (;;)
  2441.     {
  2442.     readfds = (1 << READ) | (1 << master_pty);
  2443.  
  2444.     /* wait for input from stdin or master_pty */
  2445.  
  2446.     nfds = select(width,
  2447.         (fd_set *) & readfds,
  2448.         (fd_set *) NULL,
  2449.         (fd_set *) NULL,
  2450.         (struct timeval *) NULL);
  2451.  
  2452.     if (nfds == -1)        /* an exception has occured */
  2453.     {
  2454.         if (windowchanged)
  2455.         {
  2456.         /* nothing serious, the window changed size */
  2457.  
  2458.         windowchanged = FALSE;
  2459.         }
  2460.         else
  2461.         {
  2462.         perror("ile");
  2463.         clean_up();
  2464.         }
  2465.     }
  2466.     else if ((nfds > 0) && (readfds != 0))    /* something to read */
  2467.     {
  2468.         if ((readfds & (1 << master_pty)) != 0)
  2469.         {
  2470.         /* read the pending characters. */
  2471.  
  2472.         cc = read(master_pty, buffer, BUFFER_SIZE);
  2473.  
  2474.         /* display the characters. */
  2475.  
  2476.         (void) write(WRITE, buffer, cc);
  2477.         }
  2478.  
  2479.         if ((readfds & (1 << READ)) != 0)
  2480.         {
  2481.         /* read the pending characters. */
  2482.  
  2483.         cc = read(READ, buffer, BUFFER_SIZE);
  2484.  
  2485.         /* if the slave is in RAW or CBREAK mode, or has turned off
  2486.          * ECHO then we should not mess with its input characters */
  2487.  
  2488.         (void) ioctl(slave_tty, TIOCGETP, &slave_params);
  2489.  
  2490.         if (((slave_params.sg_flags & (RAW | CBREAK)) != 0) ||
  2491.             (slave_params.sg_flags & ECHO) == 0)
  2492.         {
  2493.             edit = pass;
  2494.         }
  2495.         else if (edit == (void (*) ()) pass)
  2496.         {
  2497.             edit = edit_0;
  2498.         }
  2499.  
  2500.         /* decide what to do with the characters. */
  2501.  
  2502.         for (i = 0; i < cc; i++)
  2503.         {
  2504.             (*edit) (CHAR_MASK & buffer[i]);
  2505.         }
  2506.         }
  2507.  
  2508.     }
  2509.     }
  2510.  
  2511. }
  2512. /*------------------------------------------------------------------*/
  2513. /*
  2514. The child process.
  2515.  
  2516. Make the pty the processes controling terminal. Bind the pty to
  2517. stdin, stdout, and stderr. Then exec the users program.
  2518. */
  2519. void
  2520. child(argv)
  2521.     char *argv[];
  2522. {
  2523.     /* shell name pointers */
  2524.  
  2525.     char *shellname;
  2526.     char *shellpath;
  2527.     char *dashshellname;
  2528.  
  2529.     /* close all file descriptors */
  2530.  
  2531.     (void) close(READ);
  2532.     (void) close(WRITE);
  2533.     (void) close(ERROR);
  2534.     (void) close(slave_tty);
  2535.  
  2536.     /* get rid of controlling terminal */
  2537.  
  2538.     {
  2539.     int tty;
  2540.  
  2541.     if ((tty = open("/dev/tty", O_RDWR) == -1) ||
  2542.         (ioctl(0, TIOCNOTTY, 0) == -1) ||
  2543.         (close(tty) == -1))
  2544.     {
  2545.         perror("ile");
  2546.     }
  2547.     }
  2548.  
  2549.     /* open the tty again */
  2550.     /* this makes the pty the controlling terminal */
  2551.  
  2552.     if ((slave_tty = open(ttydev, O_RDWR)) == -1)
  2553.     {
  2554.     perror("ile");
  2555.     }
  2556.  
  2557.     /* slave_tty is now stdin */
  2558.  
  2559.     /* bind slave_tty to stdout */
  2560.  
  2561.     (void) dup2(slave_tty, WRITE);
  2562.  
  2563.     /* bind slave_tty to stderr */
  2564.  
  2565.     (void) dup2(slave_tty, ERROR);
  2566.  
  2567.     /* close master_pty descriptor */
  2568.  
  2569.     (void) close(master_pty);
  2570.  
  2571.     /* Fire up application program. If no program name is given then fire up
  2572.      * users favorite shell. */
  2573.  
  2574.     /* get the name of the users shell. default to /bin/csh */
  2575.  
  2576.     if ((shellpath = getenv("SHELL")) == NULL ||
  2577.     (*shellpath == '\0'))
  2578.     {
  2579.     shellpath = "/bin/csh";
  2580.     }
  2581.  
  2582.     /* get just the name */
  2583.  
  2584.     if ((shellname = strrchr(shellpath, '/')) != NULL)
  2585.     {
  2586.     shellname += sizeof(char);
  2587.     }
  2588.  
  2589.     /* if the current argv[0] starts with -, then the new argv[0] */
  2590.     /* should start with - */
  2591.  
  2592.     if (*(argv[0]) == '-')
  2593.     {
  2594.     dashshellname = (char *) malloc((unsigned) strlen(shellname) + 2);
  2595.     (void) strcpy(dashshellname, "-");
  2596.     (void) strcat(dashshellname, shellpath);
  2597.     }
  2598.     else
  2599.     {
  2600.     dashshellname = shellname;
  2601.     }
  2602.  
  2603.     /* execute the shell or the specified program */
  2604.  
  2605.     if (argv[1] == NULL)
  2606.     {
  2607.     /* execute default shell */
  2608.  
  2609.     execlp(shellpath, dashshellname, 0);
  2610.     }
  2611.     else if (*argv[1] == '-')
  2612.     {
  2613.     /* there is an initialization file */
  2614.  
  2615.     if (argv[2] == NULL)
  2616.     {
  2617.         /* execute default shell */
  2618.  
  2619.         execlp(shellpath, dashshellname, 0);
  2620.     }
  2621.     else
  2622.     {
  2623.         /* execute specified program */
  2624.  
  2625.         execvp(argv[2], &argv[2]);
  2626.     }
  2627.     }
  2628.     else
  2629.     {
  2630.     /* execute specified program */
  2631.  
  2632.     execvp(argv[1], &argv[1]);
  2633.     }
  2634.  
  2635.     /* this executes if exec fails */
  2636.  
  2637.     perror("ile");
  2638.     exit(1);
  2639.     /* NOTREACHED */
  2640. }
  2641. /*------------------------------------------------------------------*/
  2642. /*
  2643. Set up default key bindings and delimeters.
  2644. */
  2645. void
  2646. default_bindings()
  2647. {
  2648.     int i;
  2649.  
  2650.     /* clear delimiter vector and the action table */
  2651.  
  2652.     for (i = 0; i < CHAR_SET_SIZE; i++)
  2653.     {
  2654.     delimit[i] = FALSE;
  2655.  
  2656.     action_table[0][i].aors.action = insert;
  2657.     action_table[1][i].aors.action = bell;
  2658.     action_table[2][i].aors.action = bell;
  2659.     action_table[3][i].aors.action = bell;
  2660.  
  2661.     action_table[0][i].flag = is_action;
  2662.     action_table[1][i].flag = is_action;
  2663.     action_table[2][i].flag = is_action;
  2664.     action_table[3][i].flag = is_action;
  2665.     }
  2666.  
  2667.     /* default delimiters */
  2668.  
  2669.     delimit[' '] = TRUE;    /* blank */
  2670.     delimit['/'] = TRUE;    /* slash */
  2671.     delimit['.'] = TRUE;    /* dot */
  2672.     delimit['-'] = TRUE;    /* dash */
  2673.  
  2674.     /* default action_table[0] */
  2675.  
  2676.     action_table[0][CA].aors.action = start_of_line;
  2677.     action_table[0][CB].aors.action = backward_char;
  2678.     action_table[0][CE].aors.action = end_of_line;
  2679.     action_table[0][CF].aors.action = forward_char;
  2680.     action_table[0][CK].aors.action = erase_to_end_of_line;
  2681.     action_table[0][CU].aors.action = erase_line;
  2682.     action_table[0][CL].aors.action = retype_line;
  2683.     action_table[0][CN].aors.action = forward_history;
  2684.     action_table[0][CP].aors.action = backward_history;
  2685.     action_table[0][CR].aors.action = search_backward_history;
  2686.     action_table[0][CT].aors.action = transpose_chars;
  2687.     action_table[0][CV].aors.action = quote;
  2688.     action_table[0][del].aors.action = delete_char;
  2689.     action_table[0][esc].aors.action = escape_1;
  2690.     action_table[0][cr].aors.action = add_to_history;
  2691.     action_table[0][nl].aors.action = add_to_history;
  2692.     action_table[0][CX].aors.action = delete_char_under;
  2693.  
  2694.     action_table[0][CC].aors.action = pass;
  2695.     action_table[0][CD].aors.action = pass;
  2696.     action_table[0][CQ].aors.action = pass;
  2697.     action_table[0][CS].aors.action = pass;
  2698.     action_table[0][CZ].aors.action = pass;
  2699.  
  2700.     /* default action_table[1] ^[ c */
  2701.  
  2702.     action_table[1]['b'].aors.action = backward_word;
  2703.     action_table[1]['f'].aors.action = forward_word;
  2704.     action_table[1][del].aors.action = delete_word;
  2705.     action_table[1]['u'].aors.action = upper_word;
  2706.     action_table[1]['l'].aors.action = lower_word;
  2707.     action_table[1]['c'].aors.action = capitalize_word;
  2708.     action_table[1]['['].aors.action = escape_2;
  2709.     action_table[1][esc].aors.action = complete_file;
  2710.     action_table[1]['s'].aors.action = complete_file_full;
  2711.     action_table[1]['d'].aors.action = show_files;
  2712.     action_table[1]['p'].aors.action = query_path;
  2713.  
  2714.     /* default action_table[2] ^[ [ */
  2715.  
  2716.     action_table[2]['A'].aors.action = backward_history;
  2717.     action_table[2]['B'].aors.action = forward_history;
  2718.     action_table[2]['C'].aors.action = forward_char;
  2719.     action_table[2]['D'].aors.action = backward_char;
  2720.  
  2721. }
  2722. /*------------------------------------------------------------------*/
  2723. /*
  2724. Return a character or EOF. This routine reads characters from input
  2725. and converts them into a character using the following rules.
  2726.  
  2727. The character may be a single character, a control
  2728. character indicated by ^x, an octal number starting with \, or an
  2729. escaped character indictated by \x.
  2730. */
  2731. int
  2732. scan_char(input)
  2733.     FILE *input;
  2734. {
  2735.     int ch;
  2736.     int value;
  2737.  
  2738.     ch = fgetc(input);
  2739.     switch (ch)
  2740.     {
  2741.     case '^':
  2742.  
  2743.     /* it is a control character */
  2744.  
  2745.     for (ch = fgetc(input); '@' <= ch; ch = ch - '@');
  2746.  
  2747.     break;
  2748.  
  2749.     case '\\':
  2750.  
  2751.     /* octal or an escaped character? */
  2752.  
  2753.     ch = fgetc(input);
  2754.     if (('0' <= ch) && (ch <= '7'))
  2755.     {
  2756.  
  2757.         /* its an octal number */
  2758.  
  2759.         value = 0;
  2760.         while (('0' <= ch) && (ch <= '7'))
  2761.         {
  2762.         value = (value * 8) + (ch - '0');
  2763.         ch = fgetc(input);
  2764.         }
  2765.         (void) ungetc(ch, input);
  2766.  
  2767.         ch = value & 0177;    /* make sure it is in range */
  2768.     }
  2769.     else
  2770.     {
  2771.         /* its an escaped character */
  2772.  
  2773.         ch = fgetc(input);
  2774.     }
  2775.  
  2776.     break;
  2777.  
  2778.     case '\n':
  2779.  
  2780.     /* the real end of the line */
  2781.  
  2782.     ch = EOL;
  2783.  
  2784.     break;
  2785.  
  2786.     default:
  2787.  
  2788.     /* it is just itself */
  2789.  
  2790.     break;
  2791.     }
  2792.  
  2793.     return (ch);
  2794.  
  2795. }
  2796. /*------------------------------------------------------------------*/
  2797. /*
  2798. Set key bindings and delimiters from the users file.
  2799. */
  2800. void
  2801. user_bindings(file)
  2802.     FILE *file;
  2803. {
  2804.  
  2805. #define NAME_SIZE 40
  2806.  
  2807.     static struct action_name_table
  2808.     {
  2809.     char *name;
  2810.     void (*action) ();
  2811.     } action_name_table[] =
  2812.     {
  2813.     {
  2814.         "complete_file_full", complete_file_full
  2815.     },
  2816.     {
  2817.         "complete_file", complete_file
  2818.     },
  2819.     {
  2820.         "show_files", show_files
  2821.     },
  2822.     {
  2823.         "query_path", query_path
  2824.     },
  2825.     {
  2826.         "bell", bell
  2827.     },
  2828.     {
  2829.         "pass", pass
  2830.     },
  2831.     {
  2832.         "insert", insert
  2833.     },
  2834.     {
  2835.         "transpose_chars", transpose_chars
  2836.     },
  2837.     {
  2838.         "delete_char", delete_char
  2839.     },
  2840.     {
  2841.         "delete_char_under", delete_char_under
  2842.     },
  2843.     {
  2844.         "quote", quote
  2845.     },
  2846.     {
  2847.         "escape_1", escape_1
  2848.     },
  2849.     {
  2850.         "escape_2", escape_2
  2851.     },
  2852.     {
  2853.         "escape_3", escape_3
  2854.     },
  2855.     {
  2856.         "delete_word", delete_word
  2857.     },
  2858.     {
  2859.         "upper_word", upper_word
  2860.     },
  2861.     {
  2862.         "lower_word", lower_word
  2863.     },
  2864.     {
  2865.         "capitalize_word", capitalize_word
  2866.     },
  2867.     {
  2868.         "forward_word", forward_word
  2869.     },
  2870.     {
  2871.         "backward_word", backward_word
  2872.     },
  2873.     {
  2874.         "start_of_line", start_of_line
  2875.     },
  2876.     {
  2877.         "backward_char", backward_char
  2878.     },
  2879.     {
  2880.         "end_of_line", end_of_line
  2881.     },
  2882.     {
  2883.         "forward_char", forward_char
  2884.     },
  2885.     {
  2886.         "add_to_history", add_to_history
  2887.     },
  2888.     {
  2889.         "erase_line", erase_line
  2890.     },
  2891.     {
  2892.         "erase_to_end_of_line", erase_to_end_of_line
  2893.     },
  2894.     {
  2895.         "retype_line", retype_line
  2896.     },
  2897.     {
  2898.         "forward_history", forward_history
  2899.     },
  2900.     {
  2901.         "backward_history", backward_history
  2902.     },
  2903.     {
  2904.         "search_backward_history", search_backward_history
  2905.     },
  2906.     {
  2907.         "", NULL
  2908.     }
  2909.     };
  2910.  
  2911.     char name[NAME_SIZE];
  2912.  
  2913.     int ch;
  2914.     int i;
  2915.  
  2916.     int linecount;
  2917.     int table;
  2918.     int entry;
  2919.  
  2920.     /* First clear the default delimiters */
  2921.  
  2922.     for (i = 0; i < CHAR_SET_SIZE; i++)
  2923.     {
  2924.     delimit[i] = FALSE;
  2925.     }
  2926.  
  2927.     /* Now read the delimiter characters */
  2928.  
  2929.     while (((int) (ch = fgetc(file)) != EOF) && (ch != '\n'))
  2930.     {
  2931.     delimit[ch] = TRUE;
  2932.     }
  2933.  
  2934.     linecount = 2;
  2935.  
  2936.     /* Now read the character binding pairs */
  2937.  
  2938.     while ((int) (ch = fgetc(file)) != EOF)
  2939.     {
  2940.     switch (ch)
  2941.     {
  2942.     case '\n':
  2943.  
  2944.         /* skipping a blank line */
  2945.         linecount++;
  2946.  
  2947.         break;
  2948.  
  2949.     case '0':
  2950.     case '1':
  2951.     case '2':
  2952.     case '3':
  2953.  
  2954.         /* which table is this entry directed to? */
  2955.  
  2956.         table = ch - '0';
  2957.  
  2958.         /* get the character code */
  2959.  
  2960.         entry = scan_char(file);
  2961.  
  2962.         /* make sure the '=' is there */
  2963.  
  2964.         ch = fgetc(file);
  2965.         if (ch != '=')
  2966.         {
  2967.         (void) fprintf(stderr,
  2968.             "ile: '=' missing on line %d\n",
  2969.             linecount);
  2970.         exit(1);
  2971.         /* NOTREACHED */
  2972.         }
  2973.  
  2974.         /* collect the action name or string */
  2975.  
  2976.         for (ch = scan_char(file), i = 0;
  2977.         ((int) ch != EOL) && (i < (NAME_SIZE - 1));
  2978.         ch = scan_char(file), i++)
  2979.         {
  2980.         name[i] = ch;
  2981.         name[i + 1] = '\0';
  2982.         }
  2983.  
  2984.         /* look it up in the action_name_table */
  2985.  
  2986.         for (i = 0;
  2987.         (action_name_table[i].action != NULL) &&
  2988.         (strcmp(name, action_name_table[i].name) != 0);
  2989.         i++);
  2990.  
  2991.         /* if it was found, put it in the action array */
  2992.  
  2993.         if (action_name_table[i].action == NULL)
  2994.         {
  2995.         /* must be a string */
  2996.  
  2997.         action_table[table][entry].flag = is_string;
  2998.         action_table[table][entry].aors.string =
  2999.             (char *) malloc((unsigned) strlen(name) + 1);
  3000.         (void) strcpy(action_table[table][entry].aors.string, name);
  3001.         }
  3002.         else
  3003.         {
  3004.         /* its an action */
  3005.  
  3006.         action_table[table][entry].flag = is_action;
  3007.         action_table[table][entry].aors.action =
  3008.             action_name_table[i].action;
  3009.         }
  3010.  
  3011.         linecount++;    /* count the line */
  3012.  
  3013.         break;
  3014.  
  3015.     default:
  3016.         (void) fprintf(stderr,
  3017.         "\nile: error in initialization file on line %d\n",
  3018.         linecount);
  3019.         exit(1);
  3020.         /* NOTREACHED */
  3021.     }
  3022.     }
  3023.  
  3024.     (void) fclose(file);
  3025. }
  3026. /*------------------------------------------------------------------*/
  3027. /*
  3028. Initialize key bindings and delimiters.
  3029. */
  3030. void
  3031. initialize(argv)
  3032.     char *argv[];
  3033. {
  3034.     FILE *file;
  3035.     char name[BUFFER_SIZE];
  3036.     char *pwd;
  3037.  
  3038.     /* set up the default bindings */
  3039.  
  3040.     default_bindings();
  3041.  
  3042.     /* Look for an initialization file. If it's there, load it. */
  3043.  
  3044.     name[0] = '\0';
  3045.     homedir = getenv("HOME");
  3046.     if (homedir == NULL)
  3047.     {
  3048.     /* no home dir, use / instead */
  3049.     name[0] = '\0';
  3050.     }
  3051.     else
  3052.     {
  3053.     (void) strcpy(name, homedir);
  3054.     }
  3055.     (void) strcat(name, "/.ilerc");
  3056.  
  3057.     /* initialize currentdir */
  3058.  
  3059.     pwd = getenv("PWD");
  3060.     if (pwd == NULL)
  3061.     {
  3062.     /* no pwd, use homedir instead */
  3063.     (void) strcpy(currentdir, homedir);
  3064.     }
  3065.     else
  3066.     {
  3067.     (void) strcpy(currentdir, pwd);
  3068.     }
  3069.  
  3070.     if ((argv[1] != NULL) &&
  3071.     (*argv[1] == '-') &&
  3072.     ((file = fopen(argv[1] + 1, "r")) != NULL))
  3073.     {
  3074.     /* load the users bindings */
  3075.  
  3076.     user_bindings(file);
  3077.     }
  3078.     else if (((file = fopen("./.ilerc", "r")) != NULL) ||
  3079.     ((file = fopen(name, "r")) != NULL))
  3080.     {
  3081.     user_bindings(file);
  3082.     }
  3083. }
  3084. /*------------------------------------------------------------------*/
  3085. /*
  3086. */
  3087. /*ARGSUSED*/
  3088. main(argc, argv)
  3089.     int argc;
  3090.     char *argv[];
  3091. {
  3092.  
  3093.     /* Child process id */
  3094.  
  3095.     int childpid;
  3096.  
  3097.     /* identify yourself */
  3098.  
  3099.     (void) fprintf(stdout, "ile rev.2\n\r");
  3100.  
  3101.     /* create the tty/pty pair */
  3102.  
  3103.     getpty(&master_pty, &slave_tty);
  3104.  
  3105.     /* get control sequences from termcap */
  3106.  
  3107.     get_termcap();
  3108.  
  3109.     /* initialize the dispatch vectors */
  3110.  
  3111.     initialize(argv);
  3112.  
  3113.     /* create the child process */
  3114.  
  3115.     childpid = fork();
  3116.  
  3117.     switch (childpid)
  3118.     {
  3119.     case 0:            /* child process */
  3120.  
  3121.     child(argv);
  3122.     break;
  3123.  
  3124.     case -1:            /* fork failed */
  3125.  
  3126.     perror("ile");
  3127.     exit(1);
  3128.     /* NOTREACHED */
  3129.  
  3130.     default:            /* parent process */
  3131.     ile();
  3132.     break;
  3133.     }
  3134.  
  3135. }
  3136.