+/* Append LINE to the history line at offset WHICH, adding a newline to the
+ end of the current line first. This can be used to construct multi-line
+ history entries while reading lines from the history file. */
+void
+_hs_append_history_line (int which, const char *line)
+{
+ HIST_ENTRY *hent;
+ size_t newlen, curlen, minlen;
+ char *newline;
+
+ hent = the_history[which];
+ curlen = strlen (hent->line);
+ minlen = curlen + strlen (line) + 2; /* min space needed */
+ if (curlen > 256) /* XXX - for now */
+ {
+ newlen = 512; /* now realloc in powers of 2 */
+ /* we recalcluate every time; the operations are cheap */
+ while (newlen < minlen)
+ newlen <<= 1;
+ }
+ else
+ newlen = minlen;
+ /* Assume that realloc returns the same pointer and doesn't try a new
+ alloc/copy if the new size is the same as the one last passed. */
+ newline = realloc (hent->line, newlen);
+ if (newline)
+ {
+ hent->line = newline;
+ hent->line[curlen++] = '\n';
+ strcpy (hent->line + curlen, line);
+ }
+}
+
+/* Replace the DATA in the specified history entries, replacing OLD with
+ NEW. WHICH says which one(s) to replace: WHICH == -1 means to replace
+ all of the history entries where entry->data == OLD; WHICH == -2 means
+ to replace the `newest' history entry where entry->data == OLD; and
+ WHICH >= 0 means to replace that particular history entry's data, as
+ long as it matches OLD. */
+void
+_hs_replace_history_data (int which, histdata_t *old, histdata_t *new)
+{
+ HIST_ENTRY *entry;
+ register int i, last;
+
+ if (which < -2 || which >= history_length || history_length == 0 || the_history == 0)
+ return;
+
+ if (which >= 0)
+ {
+ entry = the_history[which];
+ if (entry && entry->data == old)
+ entry->data = new;
+ return;
+ }
+
+ last = -1;
+ for (i = 0; i < history_length; i++)
+ {
+ entry = the_history[i];
+ if (entry == 0)
+ continue;
+ if (entry->data == old)
+ {
+ last = i;
+ if (which == -1)
+ entry->data = new;
+ }
+ }
+ if (which == -2 && last >= 0)
+ {
+ entry = the_history[last];
+ entry->data = new; /* XXX - we don't check entry->old */
+ }
+}
+