Hi,

> On Apr 18, 2021, at 12:03 AM, Ananth Chellappa <ananth...@gmail.com> wrote:
> 
> Hello Brian and Chet,
>         Please consider merging the enhancement made by Naseeba described
> here :
> 
> https://github.com/ananthchellappa/bash-5.1/blob/main/README.md

In your future emails, you may want to consider

- writing a subject that provides a useful summary of the purpose
  or contents of the email,
- writing a body that contains your message or a useful summary of
  it, and
- not requiring readers to visit an external website to learn
  literally the first thing about what you wish to discuss.

Here's the relevant description from that README:

        This version of bash contains the below described update
        done by **Naseeba Kilayil** through Upwork :

        Currently, when you do set +o history, then commands are
        no longer logged to ~/.bash_history. The problem is that
        while you get the privacy, you also lose command recall
        with !number and arrow-keys. So we want to **enhance**
        `set +o history` to suspend logging to ~/.bash_history,
        but (while in private mode - i.e., till you do `set -o
        history`) you do get command recall with !number and
        arrow keys. These private mode commands will, of course,
        be forgotten once user exits private mode.

The history feature encompasses a lot more than just saving to the
history file.  The idea of `set +o history` leaving history expansion
and command recall enabled makes no sense to me; those *are* history
features.

Is the desired outcome just to retain all history features except
persistence to ~/.bash_history (or any other file)?  If so, this
can be accomplished by unsetting HISTFILE.

P.S. I've attached the patches from the aforelinked Git repository,
for the curious.

-- 
vq

From 4cddd70586f64530e41ccb3c4293c5e805ef840a Mon Sep 17 00:00:00 2001
From: Ananth <ana...@wix.com>
Date: Sat, 17 Apr 2021 20:42:11 -0700
Subject: [PATCH 1/8] Always keep history

---
 bashhist.c       |  2 ++
 bashhist.h       |  2 ++
 builtins/set.def | 16 ++++++++--------
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/bashhist.c b/bashhist.c
index 2a05a53..eb6471a 100644
--- a/bashhist.c
+++ b/bashhist.c
@@ -91,6 +91,8 @@ static struct ignorevar histignore =
 int remember_on_history = 0;
 int enable_history_list = -1;  /* value for `set -o history' */
 
+int private_history_mode = 0;
+
 /* The number of lines that Bash has added to this history session.  The
    difference between the number of the top element in the history list
    (offset from history_base) and the number of lines in the history file.
diff --git a/bashhist.h b/bashhist.h
index 615f5d2..8061bac 100644
--- a/bashhist.h
+++ b/bashhist.h
@@ -39,6 +39,8 @@
 #  endif /* !HISTEXPAND_DEFAULT */
 #endif
 
+extern int private_history_mode;
+
 extern int remember_on_history;
 extern int enable_history_list;                /* value for `set -o history' */
 extern int literal_history;            /* controlled by `shopt lithist' */
diff --git a/builtins/set.def b/builtins/set.def
index 8ee0165..150e153 100644
--- a/builtins/set.def
+++ b/builtins/set.def
@@ -395,7 +395,7 @@ set_ignoreeof (on_or_off, option_name)
   ignoreeof = on_or_off == FLAG_ON;
   unbind_variable_noref ("ignoreeof");
   if (ignoreeof)
-    bind_variable ("IGNOREEOF", "10", 0); 
+    bind_variable ("IGNOREEOF", "10", 0);
   else
     unbind_variable_noref ("IGNOREEOF");
   sv_ignoreeof ("IGNOREEOF");
@@ -468,16 +468,16 @@ bash_set_history (on_or_off, option_name)
 {
   if (on_or_off == FLAG_ON)
     {
-      enable_history_list = 1;
-      bash_history_enable ();
-      if (history_lines_this_session == 0)
-       load_history ();
+      private_history_mode = 0;
     }
   else
     {
-      enable_history_list = 0;
-      bash_history_disable ();
+      private_history_mode = 1;
     }
+    enable_history_list = 1;
+    bash_history_enable ();
+    if (history_lines_this_session == 0)
+      load_history ();
   return (1 - enable_history_list);
 }
 #endif
@@ -706,7 +706,7 @@ set_builtin (list)
            break;
        }
     }
-    
+
   /* Do the set command.  While the list consists of words starting with
      '-' or '+' treat them as flags, otherwise, start assigning them to
      $1 ... $n. */
-- 
2.31.1

From aeea5a2e2d36e6e6c57063835a1fb1a17f5c60fc Mon Sep 17 00:00:00 2001
From: Ananth <ana...@wix.com>
Date: Sat, 17 Apr 2021 20:46:47 -0700
Subject: [PATCH 2/8] tag history entry as private

---
 bashhist.c                   | 15 +++++++++++----
 lib/readline/doc/hstech.texi | 15 ++++++++++++---
 lib/readline/history.c       | 28 ++++++++++++++++++++++------
 lib/readline/history.h       |  4 +++-
 4 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/bashhist.c b/bashhist.c
index eb6471a..6190018 100644
--- a/bashhist.c
+++ b/bashhist.c
@@ -599,7 +599,7 @@ pre_process_line (line, print_changes, addit)
            {
 #    if defined (READLINE)
              if (expanded == 2 && rl_dispatching == 0 && *history_value)
-#    else            
+#    else
              if (expanded == 2 && *history_value)
 #    endif /* !READLINE */
                maybe_add_history (history_value);
@@ -793,7 +793,7 @@ check_add_history (line, force)
         remove other matching lines from the history. */
       if (history_control & HC_ERASEDUPS)
        hc_erasedups (line);
-        
+
       if (force)
        {
          really_add_history (line);
@@ -859,7 +859,7 @@ bash_syslog_history (line)
     }
 }
 #endif
-       
+
 /* Add a line to the history list.
    The variable COMMAND_ORIENTED_HISTORY controls the style of history
    remembering;  when non-zero, and LINE is not the first line of a
@@ -961,7 +961,14 @@ really_add_history (line)
 {
   hist_last_line_added = 1;
   hist_last_line_pushed = 0;
-  add_history (line);
+  if(private_history_mode)
+  {
+    add_private_history(line);
+  }
+  else
+  {
+    add_history (line);
+  }
   history_lines_this_session++;
 }
 
diff --git a/lib/readline/doc/hstech.texi b/lib/readline/doc/hstech.texi
index 7ac1195..7a5b4c6 100644
--- a/lib/readline/doc/hstech.texi
+++ b/lib/readline/doc/hstech.texi
@@ -46,7 +46,7 @@ History Interactively}.
 Many programs read input from the user a line at a time.  The @sc{gnu}
 History library is able to keep track of those lines, associate arbitrary
 data with each line, and utilize information from previous lines in
-composing new ones. 
+composing new ones.
 
 A programmer using the History library has available functions
 for remembering lines on a history list, associating arbitrary data
@@ -171,6 +171,15 @@ If the maximum number of history entries has been set using
 that maximum, the oldest history entry is removed.
 @end deftypefun
 
+@deftypefun void add_private_history (const char *string)
+Place @var{string} at the end of the history list.  The associated data
+field (if any) is set to @code{NULL}.
+If the maximum number of history entries has been set using
+@code{stifle_history()}, and the new number of history entries would exceed
+that maximum, the oldest history entry is removed. History added by this is not
+written to the history file
+@end deftypefun
+
 @deftypefun void add_history_time (const char *string)
 Change the time stamp associated with the most recent history entry to
 @var{string}.
@@ -311,8 +320,8 @@ offset.  The search is anchored: matching lines must begin 
with
 @var{string}.  If @var{direction} is less than 0, then the search is
 through previous entries, otherwise through subsequent entries.
 If @var{string} is found, then the
-current history index is set to that entry, and the return value is 0. 
-Otherwise, nothing is changed, and a -1 is returned. 
+current history index is set to that entry, and the return value is 0.
+Otherwise, nothing is changed, and a -1 is returned.
 @end deftypefun
 
 @deftypefun int history_search_pos (const char *string, int direction, int pos)
diff --git a/lib/readline/history.c b/lib/readline/history.c
index 67158b1..409daac 100644
--- a/lib/readline/history.c
+++ b/lib/readline/history.c
@@ -165,7 +165,7 @@ history_set_pos (int pos)
   history_offset = pos;
   return (1);
 }
- 
+
 /* Return the current history array.  The caller has to be careful, since this
    is the actual array of data, and could be bashed or made corrupt easily.
    The array is terminated with a NULL pointer. */
@@ -226,6 +226,7 @@ alloc_history_entry (char *string, char *ts)
   temp->line = string ? savestring (string) : string;
   temp->data = (char *)NULL;
   temp->timestamp = ts;
+  temp->private = 0;
 
   return temp;
 }
@@ -268,8 +269,7 @@ hist_inittime (void)
 
 /* Place STRING at the end of the history list.  The data field
    is  set to NULL. */
-void
-add_history (const char *string)
+static void common_add_history (const char *string, int private)
 {
   HIST_ENTRY *temp;
   int new_length;
@@ -320,12 +320,28 @@ add_history (const char *string)
     }
 
   temp = alloc_history_entry ((char *)string, hist_inittime ());
+  if(private)
+  {
+    temp->private = 1;
+  }
 
   the_history[new_length] = (HIST_ENTRY *)NULL;
   the_history[new_length - 1] = temp;
   history_length = new_length;
 }
 
+
+void add_history(const char *string)
+{
+  common_add_history(string, 0);
+}
+
+void add_private_history(const char *string)
+{
+  common_add_history(string, 1);
+}
+
+
 /* Change the time stamp of the most recent history entry to STRING. */
 void
 add_history_time (const char *string)
@@ -373,7 +389,7 @@ copy_history_entry (HIST_ENTRY *hist)
 
   return ret;
 }
-  
+
 /* Make the history entry at WHICH have LINE and DATA.  This returns
    the old entry so you can dispose of the data.  In the case of an
    invalid WHICH, a NULL pointer is returned. */
@@ -470,8 +486,8 @@ _hs_replace_history_data (int which, histdata_t *old, 
histdata_t *new)
       entry = the_history[last];
       entry->data = new;       /* XXX - we don't check entry->old */
     }
-}      
-  
+}
+
 /* Remove history element WHICH from the history.  The removed
    element is returned to you so you can free the line, data,
    and containing structure. */
diff --git a/lib/readline/history.h b/lib/readline/history.h
index cc3de29..4d1583e 100644
--- a/lib/readline/history.h
+++ b/lib/readline/history.h
@@ -47,6 +47,7 @@ typedef struct _hist_entry {
   char *line;
   char *timestamp;             /* char * rather than time_t for read/write */
   histdata_t data;
+  int private;
 } HIST_ENTRY;
 
 /* Size of the history-library-managed space in history entry HS. */
@@ -81,6 +82,7 @@ extern void history_set_history_state PARAMS((HISTORY_STATE 
*));
 /* Place STRING at the end of the history list.
    The associated data field (if any) is set to NULL. */
 extern void add_history PARAMS((const char *));
+extern void add_private_history PARAMS((const char *));
 
 /* Change the timestamp associated with the most recent history entry to
    STRING. */
@@ -133,7 +135,7 @@ extern HIST_ENTRY **history_list PARAMS((void));
 /* Returns the number which says what history element we are now
    looking at.  */
 extern int where_history PARAMS((void));
-  
+
 /* Return the history entry at the current position, as determined by
    history_offset.  If there is no entry there, return a NULL pointer. */
 extern HIST_ENTRY *current_history PARAMS((void));
-- 
2.31.1

From 653e2172e03590a1c7f5ef6f2dd7daf10a72ef86 Mon Sep 17 00:00:00 2001
From: Ananth <ana...@wix.com>
Date: Sat, 17 Apr 2021 20:48:22 -0700
Subject: [PATCH 3/8] scrub private history when exiting private mode

---
 bashhist.h             |  1 +
 builtins/set.def       | 11 +++++++----
 lib/readline/history.c | 28 ++++++++++++++++++++++++++++
 3 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/bashhist.h b/bashhist.h
index 8061bac..7eeaa88 100644
--- a/bashhist.h
+++ b/bashhist.h
@@ -71,6 +71,7 @@ extern void bash_history_reinit PARAMS((int));
 extern void bash_history_disable PARAMS((void));
 extern void bash_history_enable PARAMS((void));
 extern void bash_clear_history PARAMS((void));
+extern void cleanup_private_history PARAMS((void));
 extern int bash_delete_histent PARAMS((int));
 extern int bash_delete_history_range PARAMS((int, int));
 extern int bash_delete_last_history PARAMS((void));
diff --git a/builtins/set.def b/builtins/set.def
index 150e153..589b9d9 100644
--- a/builtins/set.def
+++ b/builtins/set.def
@@ -469,15 +469,18 @@ bash_set_history (on_or_off, option_name)
   if (on_or_off == FLAG_ON)
     {
       private_history_mode = 0;
+      cleanup_private_history();
     }
   else
     {
       private_history_mode = 1;
     }
-    enable_history_list = 1;
-    bash_history_enable ();
-    if (history_lines_this_session == 0)
-      load_history ();
+
+  enable_history_list = 1;
+  bash_history_enable ();
+  if (history_lines_this_session == 0)
+    load_history ();
+
   return (1 - enable_history_list);
 }
 #endif
diff --git a/lib/readline/history.c b/lib/readline/history.c
index 409daac..04f1276 100644
--- a/lib/readline/history.c
+++ b/lib/readline/history.c
@@ -621,3 +621,31 @@ clear_history (void)
   history_offset = history_length = 0;
   history_base = 1;            /* reset history base to default */
 }
+
+extern int history_lines_this_session;
+
+void
+cleanup_private_history (void)
+{
+  register int i;
+
+  /* This loses because we cannot free the data. */
+  for (i = history_length - 1; i > 0; i--)
+  {
+    HIST_ENTRY *hist_entry = the_history[i];
+    if(hist_entry)
+    {
+      if(hist_entry->private)
+      {
+        HIST_ENTRY *discard = remove_history(i);
+        if (discard)
+        {
+          free_history_entry (discard);
+          history_lines_this_session--;
+        }
+      }
+    }
+  }
+
+  history_offset = history_length;
+}
-- 
2.31.1

From 11514e744e1a0d19c93ba29c1a6bf92b4a0e35be Mon Sep 17 00:00:00 2001
From: Ananth <ana...@wix.com>
Date: Sat, 17 Apr 2021 20:49:05 -0700
Subject: [PATCH 4/8] prevent private history from being written at exit

---
 lib/readline/histfile.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/lib/readline/histfile.c b/lib/readline/histfile.c
index f0fa5ce..f2de326 100644
--- a/lib/readline/histfile.c
+++ b/lib/readline/histfile.c
@@ -161,7 +161,7 @@ history_filename (const char *filename)
 
   if (return_val)
     return (return_val);
-  
+
   home = sh_get_env_value ("HOME");
 #if defined (_WIN32)
   if (home == 0)
@@ -194,7 +194,7 @@ history_backupfile (const char *filename)
   ssize_t n;
   struct stat fs;
 
-  fn = filename;  
+  fn = filename;
 #if defined (HAVE_READLINK)
   /* Follow symlink to avoid backing up symlink itself; call will fail if
      not a symlink */
@@ -204,7 +204,7 @@ history_backupfile (const char *filename)
       fn = linkbuf;
     }
 #endif
-      
+
   len = strlen (fn);
   ret = xmalloc (len + 2);
   strcpy (ret, fn);
@@ -212,7 +212,7 @@ history_backupfile (const char *filename)
   ret[len+1] = '\0';
   return ret;
 }
-  
+
 static char *
 history_tempfile (const char *filename)
 {
@@ -223,7 +223,7 @@ history_tempfile (const char *filename)
   struct stat fs;
   int pid;
 
-  fn = filename;  
+  fn = filename;
 #if defined (HAVE_READLINK)
   /* Follow symlink so tempfile created in the same directory as any symlinked
      history file; call will fail if not a symlink */
@@ -233,7 +233,7 @@ history_tempfile (const char *filename)
       fn = linkbuf;
     }
 #endif
-      
+
   len = strlen (fn);
   ret = xmalloc (len + 11);
   strcpy (ret, fn);
@@ -251,7 +251,7 @@ history_tempfile (const char *filename)
 
   return ret;
 }
-  
+
 /* Add the contents of FILENAME to the history list, a line at a time.
    If FILENAME is NULL, then read from ~/.history.  Returns 0 if
    successful, or errno if not. */
@@ -512,7 +512,7 @@ histfile_restore (const char *backup, const char *orig)
 
 #define SHOULD_CHOWN(finfo, nfinfo) \
   (finfo.st_uid != nfinfo.st_uid || finfo.st_gid != nfinfo.st_gid)
-  
+
 /* Truncate the history file FNAME, leaving only LINES trailing lines.
    If FNAME is NULL, then use ~/.history.  Writes a new file and renames
    it to the original name.  Returns 0 on success, errno on failure. */
@@ -728,6 +728,10 @@ history_do_write (const char *filename, int nelements, int 
overwrite)
     /* Calculate the total number of bytes to write. */
     for (buffer_size = 0, i = history_length - nelements; i < history_length; 
i++)
       {
+        if(the_history[i]->private)
+        {
+          continue;
+        }
        if (history_write_timestamps && the_history[i]->timestamp && 
the_history[i]->timestamp[0])
          buffer_size += strlen (the_history[i]->timestamp) + 1;
        buffer_size += strlen (the_history[i]->line) + 1;
@@ -749,7 +753,7 @@ mmap_error:
        FREE (tempname);
        return rv;
       }
-#else    
+#else
     buffer = (char *)malloc (buffer_size);
     if (buffer == 0)
       {
@@ -765,6 +769,10 @@ mmap_error:
 
     for (j = 0, i = history_length - nelements; i < history_length; i++)
       {
+        if(the_history[i]->private)
+        {
+          continue;
+        }
        if (history_write_timestamps && the_history[i]->timestamp && 
the_history[i]->timestamp[0])
          {
            strcpy (buffer + j, the_history[i]->timestamp);
-- 
2.31.1

From 1ec54ff849512d1b829b0f3131dae707e3dfd827 Mon Sep 17 00:00:00 2001
From: ananthchellappa <ananth.chella...@outlook.com>
Date: Sat, 17 Apr 2021 20:52:12 -0700
Subject: [PATCH 5/8] add Naseeba's name

---
 AUTHORS | 1 +
 1 file changed, 1 insertion(+)

diff --git a/AUTHORS b/AUTHORS
index 9ad0ba2..7ccad8e 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -8,6 +8,7 @@
 #
 # Filename     authors (first is original author)
 #
+support for enhanced private mode : Naseeba Kilayil (Upwork)
 README         Brian Fox, Chet Ramey
 INSTALL                Brian Fox, Chet Ramey
 COPYING                Brian Fox, Chet Ramey
-- 
2.31.1

From df01e03cdb302ae8a34a976dbc33d706cf4183a9 Mon Sep 17 00:00:00 2001
From: ananthchellappa <ananth.chella...@outlook.com>
Date: Sat, 17 Apr 2021 20:56:17 -0700
Subject: [PATCH 6/8] Credit to Naseeba

---
 README | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/README b/README
index 70b7a85..50cc6ff 100644
--- a/README
+++ b/README
@@ -1,3 +1,11 @@
+This version of bash contains the below described update done by **Naseeba 
Kilayil** through Upwork :
+
+Currently, when you do set +o history, then commands are no longer logged to 
~/.bash_history.
+The problem is that while you get the privacy, you also lose command recall 
with !number and arrow-keys.
+So we want to **enhance** `set +o history` to suspend logging to 
~/.bash_history, but (while in private mode
+- i.e., till you do `set -o history`) you do get command recall with !number 
and arrow keys. 
+These private mode commands will, of course, be forgotten once user exits 
private mode.
+
 Introduction
 ============
 
-- 
2.31.1

From c655065ea32be7802eaa600d4780966b81700146 Mon Sep 17 00:00:00 2001
From: ananthchellappa <ananth.chella...@outlook.com>
Date: Sat, 17 Apr 2021 20:57:50 -0700
Subject: [PATCH 7/8] Rename README to README.md

---
 README => README.md | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename README => README.md (100%)

diff --git a/README b/README.md
similarity index 100%
rename from README
rename to README.md
-- 
2.31.1

From 5eab2c21889b29bc9c2e48e50bdbb712510982be Mon Sep 17 00:00:00 2001
From: ananthchellappa <ananth.chella...@outlook.com>
Date: Sat, 17 Apr 2021 21:01:44 -0700
Subject: [PATCH 8/8] minor formatting issue

---
 README.md | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 50cc6ff..7baba22 100644
--- a/README.md
+++ b/README.md
@@ -2,8 +2,8 @@ This version of bash contains the below described update done 
by **Naseeba Kilay
 
 Currently, when you do set +o history, then commands are no longer logged to 
~/.bash_history.
 The problem is that while you get the privacy, you also lose command recall 
with !number and arrow-keys.
-So we want to **enhance** `set +o history` to suspend logging to 
~/.bash_history, but (while in private mode
-- i.e., till you do `set -o history`) you do get command recall with !number 
and arrow keys. 
+So we want to **enhance** `set +o history` to suspend logging to 
~/.bash_history, but (while in private mode - 
+i.e., till you do `set -o history`) you do get command recall with !number and 
arrow keys. 
 These private mode commands will, of course, be forgotten once user exits 
private mode.
 
 Introduction
-- 
2.31.1

Reply via email to