[comp.sys.amiga] Shell v2.07 Additions

adam@lamont.Columbia.edu (adam levin) (02/08/88)

Here are some modifications to Shell v2.07 which (IMHO) enhance command-line
editing and the history function.  You can include any or all of them; just
follow the directions.  The enhancements are:

      - The history stack is saved upon quitting,
          restored upon starting the shell.

      - Shift Up-Arrow jumps to the top (oldest entry)
          of the history stack.
      - Shift Down-Arrow jumps to the bottom (newest entry)
          of the history stack.
      - Shift Right-Arrow moves the cursor right a word at a time.
      - Shift Left-Arrow moves the cursor left a word at a time.

If any of this code gets included in later versions, all I ask for is credit.
-- Adam Levin

By the way, I had to change the line in "Main.c"
   extern int Enable_Abort;
to
   extern long Enable_Abort;
before Manx 3.4b would even touch it.  You may have to too.


-----------------------------------------------------------

S A V E   T H E   H I S T O R Y 

      - The history stack is saved upon quitting,
          restored upon starting the shell.

You must add the following line to "Shell.h":

#define HISTORY_FILENAME "S:.history"

Note that you can use any filename you want, but be aware that if you use
just ".history" then when you start "shell", it will look for ".history"
in your current directory.  It will also save ".history" in whichever
directory you are in when you quit.


This code saves the history stack in the file defined by HISTORY_FILENAME.
In the file Comm1.c, alter the routine "do_quit()" to be:

do_quit()
{
char history_command[256];
/*
    Save the history file by using Shell's own history command, and
    redirecting the output into the file named by HISTORY_FILENAME.
    Note that the "history >..." command will become the last entry
    in the history stack, and will be written to the file.
    This doesn't hurt anything, but you ought to know.
*/
   sprintf(history_command, "history >%s", HISTORY_FILENAME);
   exec_command(history_command);
   if (Src_stack) {
      Quit = 1;
      return(do_return());
   }
   main_exit (0);
}


The following code reads the file defined by HISTORY_FILENAME, chops off
the entry number, and then puts it onto the history stack.

In the file "Main.c", add the following two lines immediately below the
first left curly brace of the routine "init()":
 
   char line[256];
   FILE *hist;

and add this chunk immediately above the last right curly brace in "init()":

/*  Read in the saved history file (if it exists).  */
   if ((hist = fopen(HISTORY_FILENAME, "r")) != NULL) {
      while (fgets(line, 256, hist) != NULL) {
      /*  Remove the newline character.  */
         line[strlen(line)-1] = '\000';
         /*  Add it to the history stack.  Indexing into the line
         with the [4] skips over the history number.  */
         add_history(&line[4]);
      }
   fclose(hist);
   }
 

-----------------------------------------------------------

S H I F T - U P   &   S H I F T - D O W N

      - Shift Up-Arrow jumps to the top (oldest entry)
          of the history stack.
      - Shift Down-Arrow jumps to the bottom (newest entry)
          of the history stack.

In the file "RawConsole.c", in the routine "rawgets(line,prompt)",
remove these three lines:

            case 'T':           /* shift up   */
            case 'S':           /* shift down */
            break;

and replace them with:

            case 'T':           /* shift-up   */
               n = recall = H_len-1;
            case 'S':           /* shift-down */
               line[pl] = '\0';
               if (c2 == 'S') {
                  n = recall = 0;
                  if (H_head) strcpy(&line[pl], H_head->line);
               }
               else if (H_tail) strcpy(&line[pl], H_tail->line);
               printf("\015\233J%s%s",prompt,ps);
               i = max = strlen(ps) + pl;
            break;



-----------------------------------------------------------

S H I F T - R I G H T   &   S H I F T - L E F T

      - Shift Right-Arrow moves the cursor right a word at a time.
      - Shift Left-Arrow moves the cursor left a word at a time.

In the file "RawConsole.c", in the routine "rawgets(line,prompt)",
at about the 118th line you'll find:

            case ' ':           /* shift -> <-*/
            c3 = getchar();
            break;

Insert the following code between the "c3 = getchar();" and the "break;" lines.

             switch(c3) {
                case('@'):      /* shift ->   */
                   while (ps[i-strlen(prompt)] == ' ' && i<max) {
                      i++;
                      printf("\233C");
                   }
                   while (ps[i-strlen(prompt)] != ' ' && i<max) {
                      i++;
                      printf("\233C");
                   }
                break;
                case('A'):      /* shift <-   */
                   while (ps[i-strlen(prompt)-1] == ' ' && i>pl) {
                      i--;
                      printf("\233D");
                   }
                   while (ps[i-strlen(prompt)-1] != ' ' && i>pl) {
                      i--;
                      printf("\233D");
                   }
                break;
                default:
                break;
          }


And that's all there is to it!

-Adam Levin