NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
history.c
Go to the documentation of this file.
1
73#include "config.h"
74#include <stdbool.h>
75#include <stdint.h>
76#include <stdio.h>
77#include <string.h>
78#include <unistd.h>
79#include "mutt/lib.h"
80#include "config/lib.h"
81#include "core/lib.h"
82#include "lib.h"
83
84#define HC_FIRST HC_EXT_COMMAND
85
91struct History
92{
93 char **hist;
94 short cur;
95 short last;
96};
97
98/* global vars used for the string-history routines */
99
101static struct History Histories[HC_MAX];
104static int OldSize = 0;
105
111static struct History *get_history(enum HistoryClass hclass)
112{
113 const short c_history = cs_subset_number(NeoMutt->sub, "history");
114 if ((hclass >= HC_MAX) || (c_history == 0))
115 return NULL;
116
117 struct History *hist = &Histories[hclass];
118 return hist->hist ? hist : NULL;
119}
120
127static void init_history(struct History *h)
128{
129 if (OldSize != 0)
130 {
131 if (h->hist)
132 {
133 for (int i = 0; i <= OldSize; i++)
134 FREE(&h->hist[i]);
135 FREE(&h->hist);
136 }
137 }
138
139 const short c_history = cs_subset_number(NeoMutt->sub, "history");
140 if (c_history != 0)
141 h->hist = mutt_mem_calloc(c_history + 1, sizeof(char *));
142
143 h->cur = 0;
144 h->last = 0;
145}
146
157static int dup_hash_dec(struct HashTable *dup_hash, char *str)
158{
159 struct HashElem *he = mutt_hash_find_elem(dup_hash, str);
160 if (!he)
161 return -1;
162
163 uintptr_t count = (uintptr_t) he->data;
164 if (count <= 1)
165 {
166 mutt_hash_delete(dup_hash, str, NULL);
167 return 0;
168 }
169
170 count--;
171 he->data = (void *) count;
172 return count;
173}
174
183static int dup_hash_inc(struct HashTable *dup_hash, char *str)
184{
185 uintptr_t count;
186
187 struct HashElem *he = mutt_hash_find_elem(dup_hash, str);
188 if (!he)
189 {
190 count = 1;
191 mutt_hash_insert(dup_hash, str, (void *) count);
192 return count;
193 }
194
195 count = (uintptr_t) he->data;
196 count++;
197 he->data = (void *) count;
198 return count;
199}
200
204static void shrink_histfile(void)
205{
206 FILE *fp_tmp = NULL;
207 int n[HC_MAX] = { 0 };
208 int line, hclass, read;
209 char *linebuf = NULL, *p = NULL;
210 size_t buflen;
211 bool regen_file = false;
212 struct HashTable *dup_hashes[HC_MAX] = { 0 };
213
214 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
215 FILE *fp = mutt_file_fopen(c_history_file, "r");
216 if (!fp)
217 return;
218
219 const bool c_history_remove_dups = cs_subset_bool(NeoMutt->sub, "history_remove_dups");
220 const short c_save_history = cs_subset_number(NeoMutt->sub, "save_history");
221 if (c_history_remove_dups)
222 {
223 for (hclass = 0; hclass < HC_MAX; hclass++)
224 dup_hashes[hclass] = mutt_hash_new(MAX(10, c_save_history * 2), MUTT_HASH_STRDUP_KEYS);
225 }
226
227 line = 0;
228 while ((linebuf = mutt_file_read_line(linebuf, &buflen, fp, &line, MUTT_RL_NO_FLAGS)))
229 {
230 if ((sscanf(linebuf, "%d:%n", &hclass, &read) < 1) || (read == 0) ||
231 (*(p = linebuf + strlen(linebuf) - 1) != '|') || (hclass < 0))
232 {
233 mutt_error(_("%s:%d: Bad history file format"), c_history_file, line);
234 regen_file = true;
235 continue;
236 }
237 /* silently ignore too high class (probably newer neomutt) */
238 if (hclass >= HC_MAX)
239 continue;
240 *p = '\0';
241 if (c_history_remove_dups && (dup_hash_inc(dup_hashes[hclass], linebuf + read) > 1))
242 {
243 regen_file = true;
244 continue;
245 }
246 n[hclass]++;
247 }
248
249 if (!regen_file)
250 {
251 for (hclass = HC_FIRST; hclass < HC_MAX; hclass++)
252 {
253 if (n[hclass] > c_save_history)
254 {
255 regen_file = true;
256 break;
257 }
258 }
259 }
260
261 if (regen_file)
262 {
263 fp_tmp = mutt_file_mkstemp();
264 if (!fp_tmp)
265 {
266 mutt_perror(_("Can't create temporary file"));
267 goto cleanup;
268 }
269 rewind(fp);
270 line = 0;
271 while ((linebuf = mutt_file_read_line(linebuf, &buflen, fp, &line, MUTT_RL_NO_FLAGS)))
272 {
273 if ((sscanf(linebuf, "%d:%n", &hclass, &read) < 1) || (read == 0) ||
274 (*(p = linebuf + strlen(linebuf) - 1) != '|') || (hclass < 0))
275 {
276 continue;
277 }
278 if (hclass >= HC_MAX)
279 continue;
280 *p = '\0';
281 if (c_history_remove_dups && (dup_hash_dec(dup_hashes[hclass], linebuf + read) > 0))
282 {
283 continue;
284 }
285 *p = '|';
286 if (n[hclass]-- <= c_save_history)
287 fprintf(fp_tmp, "%s\n", linebuf);
288 }
289 }
290
291cleanup:
292 mutt_file_fclose(&fp);
293 FREE(&linebuf);
294 if (fp_tmp)
295 {
296 if (fflush(fp_tmp) == 0)
297 {
298 truncate(c_history_file, 0);
299 fp = mutt_file_fopen(c_history_file, "w");
300 if (fp)
301 {
302 rewind(fp_tmp);
303 mutt_file_copy_stream(fp_tmp, fp);
304 mutt_file_fclose(&fp);
305 }
306 }
307 mutt_file_fclose(&fp_tmp);
308 }
309 if (c_history_remove_dups)
310 for (hclass = 0; hclass < HC_MAX; hclass++)
311 mutt_hash_free(&dup_hashes[hclass]);
312}
313
319static void save_history(enum HistoryClass hclass, const char *str)
320{
321 static int n = 0;
322
323 if (!str || (*str == '\0')) // This shouldn't happen, but it's safer
324 return;
325
326 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
327 FILE *fp = mutt_file_fopen(c_history_file, "a");
328 if (!fp)
329 return;
330
331 char *tmp = mutt_str_dup(str);
333
334 // If tmp contains '\n' terminate it there.
335 char *nl = strchr(tmp, '\n');
336 if (nl)
337 *nl = '\0';
338
339 /* Format of a history item (1 line): "<histclass>:<string>|".
340 * We add a '|' in order to avoid lines ending with '\'. */
341 fprintf(fp, "%d:%s|\n", (int) hclass, tmp);
342
343 mutt_file_fclose(&fp);
344 FREE(&tmp);
345
346 if (--n < 0)
347 {
348 const short c_save_history = cs_subset_number(NeoMutt->sub, "save_history");
349 n = c_save_history;
351 }
352}
353
364static void remove_history_dups(enum HistoryClass hclass, const char *str)
365{
366 struct History *h = get_history(hclass);
367 if (!h)
368 return; /* disabled */
369
370 /* Remove dups from 0..last-1 compacting up. */
371 int source = 0;
372 int dest = 0;
373 while (source < h->last)
374 {
375 if (mutt_str_equal(h->hist[source], str))
376 FREE(&h->hist[source++]);
377 else
378 h->hist[dest++] = h->hist[source++];
379 }
380
381 /* Move 'last' entry up. */
382 h->hist[dest] = h->hist[source];
383 int old_last = h->last;
384 h->last = dest;
385
386 /* Fill in moved entries with NULL */
387 while (source > h->last)
388 h->hist[source--] = NULL;
389
390 /* Remove dups from last+1 .. `$history` compacting down. */
391 const short c_history = cs_subset_number(NeoMutt->sub, "history");
392 source = c_history;
393 dest = c_history;
394 while (source > old_last)
395 {
396 if (mutt_str_equal(h->hist[source], str))
397 FREE(&h->hist[source--]);
398 else
399 h->hist[dest--] = h->hist[source--];
400 }
401
402 /* Fill in moved entries with NULL */
403 while (dest > old_last)
404 h->hist[dest--] = NULL;
405}
406
414int mutt_hist_search(const char *search_buf, enum HistoryClass hclass, char **matches)
415{
416 if (!search_buf || !matches)
417 return 0;
418
419 struct History *h = get_history(hclass);
420 if (!h)
421 return 0;
422
423 int match_count = 0;
424 int cur = h->last;
425 const short c_history = cs_subset_number(NeoMutt->sub, "history");
426 do
427 {
428 cur--;
429 if (cur < 0)
430 cur = c_history;
431 if (cur == h->last)
432 break;
433 if (mutt_istr_find(h->hist[cur], search_buf))
434 matches[match_count++] = h->hist[cur];
435 } while (match_count < c_history);
436
437 return match_count;
438}
439
444{
445 if (!NeoMutt)
446 return;
447
448 const short c_history = cs_subset_number(NeoMutt->sub, "history");
449 for (enum HistoryClass hclass = HC_FIRST; hclass < HC_MAX; hclass++)
450 {
451 struct History *h = &Histories[hclass];
452 if (!h->hist)
453 continue;
454
455 /* The array has (`$history`+1) elements */
456 for (int i = 0; i <= c_history; i++)
457 {
458 FREE(&h->hist[i]);
459 }
460 FREE(&h->hist);
461 }
462}
463
471{
472 const short c_history = cs_subset_number(NeoMutt->sub, "history");
473 if (c_history == OldSize)
474 return;
475
476 for (enum HistoryClass hclass = HC_FIRST; hclass < HC_MAX; hclass++)
477 init_history(&Histories[hclass]);
478
479 OldSize = c_history;
480}
481
488void mutt_hist_add(enum HistoryClass hclass, const char *str, bool save)
489{
490 struct History *h = get_history(hclass);
491 if (!h)
492 return; /* disabled */
493
494 if (*str)
495 {
496 int prev = h->last - 1;
497 const short c_history = cs_subset_number(NeoMutt->sub, "history");
498 if (prev < 0)
499 prev = c_history;
500
501 /* don't add to prompt history:
502 * - lines beginning by a space
503 * - repeated lines */
504 if ((*str != ' ') && (!h->hist[prev] || !mutt_str_equal(h->hist[prev], str)))
505 {
506 const bool c_history_remove_dups = cs_subset_bool(NeoMutt->sub, "history_remove_dups");
507 if (c_history_remove_dups)
508 remove_history_dups(hclass, str);
509 const short c_save_history = cs_subset_number(NeoMutt->sub, "save_history");
510 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
511 if (save && (c_save_history != 0) && c_history_file)
512 save_history(hclass, str);
513 mutt_str_replace(&h->hist[h->last++], str);
514 if (h->last > c_history)
515 h->last = 0;
516 }
517 }
518 h->cur = h->last; /* reset to the last entry */
519}
520
528char *mutt_hist_next(enum HistoryClass hclass)
529{
530 struct History *h = get_history(hclass);
531 if (!h)
532 return ""; /* disabled */
533
534 int next = h->cur;
535 const short c_history = cs_subset_number(NeoMutt->sub, "history");
536 do
537 {
538 next++;
539 if (next > c_history)
540 next = 0;
541 if (next == h->last)
542 break;
543 } while (!h->hist[next]);
544
545 h->cur = next;
546 return NONULL(h->hist[h->cur]);
547}
548
556char *mutt_hist_prev(enum HistoryClass hclass)
557{
558 struct History *h = get_history(hclass);
559 if (!h)
560 return ""; /* disabled */
561
562 int prev = h->cur;
563 const short c_history = cs_subset_number(NeoMutt->sub, "history");
564 do
565 {
566 prev--;
567 if (prev < 0)
568 prev = c_history;
569 if (prev == h->last)
570 break;
571 } while (!h->hist[prev]);
572
573 h->cur = prev;
574 return NONULL(h->hist[h->cur]);
575}
576
585{
586 struct History *h = get_history(hclass);
587 if (!h)
588 return; /* disabled */
589
590 h->cur = h->last;
591}
592
599{
600 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
601 if (!c_history_file)
602 return;
603
604 FILE *fp = mutt_file_fopen(c_history_file, "r");
605 if (!fp)
606 return;
607
608 int line = 0, hclass, read;
609 char *linebuf = NULL, *p = NULL;
610 size_t buflen;
611
612 const char *const c_charset = cc_charset();
613 while ((linebuf = mutt_file_read_line(linebuf, &buflen, fp, &line, MUTT_RL_NO_FLAGS)))
614 {
615 read = 0;
616 if ((sscanf(linebuf, "%d:%n", &hclass, &read) < 1) || (read == 0) ||
617 (*(p = linebuf + strlen(linebuf) - 1) != '|') || (hclass < 0))
618 {
619 mutt_error(_("%s:%d: Bad history file format"), c_history_file, line);
620 continue;
621 }
622 /* silently ignore too high class (probably newer neomutt) */
623 if (hclass >= HC_MAX)
624 continue;
625 *p = '\0';
626 p = mutt_str_dup(linebuf + read);
627 if (p)
628 {
629 mutt_ch_convert_string(&p, "utf-8", c_charset, MUTT_ICONV_NO_FLAGS);
630 mutt_hist_add(hclass, p, false);
631 FREE(&p);
632 }
633 }
634
635 mutt_file_fclose(&fp);
636 FREE(&linebuf);
637}
638
651{
652 struct History *h = get_history(hclass);
653 if (!h)
654 return false; /* disabled */
655
656 return h->cur == h->last;
657}
658
667void mutt_hist_save_scratch(enum HistoryClass hclass, const char *str)
668{
669 struct History *h = get_history(hclass);
670 if (!h)
671 return; /* disabled */
672
673 /* Don't check if str has a value because the scratch buffer may contain
674 * an old garbage value that should be overwritten */
675 mutt_str_replace(&h->hist[h->last], str);
676}
677
684void mutt_hist_complete(char *buf, size_t buflen, enum HistoryClass hclass)
685{
686 const short c_history = cs_subset_number(NeoMutt->sub, "history");
687 char **matches = mutt_mem_calloc(c_history, sizeof(char *));
688 int match_count = mutt_hist_search(buf, hclass, matches);
689 if (match_count)
690 {
691 if (match_count == 1)
692 mutt_str_copy(buf, matches[0], buflen);
693 else
694 dlg_history(buf, buflen, matches, match_count);
695 }
696 FREE(&matches);
697}
698
703{
704 if (nc->event_type != NT_CONFIG)
705 return 0;
706 if (!nc->event_data)
707 return -1;
708
709 struct EventConfig *ev_c = nc->event_data;
710
711 if (!mutt_str_equal(ev_c->name, "history"))
712 return 0;
713
715 mutt_debug(LL_DEBUG5, "history done\n");
716 return 0;
717}
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:144
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:169
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
Convenience wrapper for the config headers.
const char * cc_charset(void)
Get the cached value of $charset.
Definition: config_cache.c:116
Convenience wrapper for the core headers.
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition: file.c:286
char * mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
Read a line from a file.
Definition: file.c:805
#define mutt_file_fclose(FP)
Definition: file.h:147
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:146
#define MUTT_RL_NO_FLAGS
No flags are set.
Definition: file.h:40
void dlg_history(char *buf, size_t buflen, char **matches, int match_count)
Select an item from a history list -.
Definition: dlg_history.c:139
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
#define mutt_perror(...)
Definition: logging2.h:93
int main_hist_observer(struct NotifyCallback *nc)
Notification that a Config Variable has change - Implements observer_t -.
Definition: history.c:702
struct HashElem * mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
Add a new element to the Hash Table (with string keys)
Definition: hash.c:335
void mutt_hash_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:427
struct HashTable * mutt_hash_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with string keys)
Definition: hash.c:259
struct HashElem * mutt_hash_find_elem(const struct HashTable *table, const char *strkey)
Find the HashElem in a Hash Table element using a key.
Definition: hash.c:377
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
#define MUTT_HASH_STRDUP_KEYS
make a copy of the keys
Definition: hash.h:111
HistoryClass
Type to differentiate different histories.
Definition: lib.h:50
@ HC_MAX
Definition: lib.h:58
static void remove_history_dups(enum HistoryClass hclass, const char *str)
De-dupe the history.
Definition: history.c:364
char * mutt_hist_next(enum HistoryClass hclass)
Get the next string in a History.
Definition: history.c:528
static int OldSize
The previous number of history entries to save.
Definition: history.c:104
void mutt_hist_read_file(void)
Read the History from a file.
Definition: history.c:598
static int dup_hash_inc(struct HashTable *dup_hash, char *str)
Increase the refcount of a history string.
Definition: history.c:183
void mutt_hist_save_scratch(enum HistoryClass hclass, const char *str)
Save a temporary string to the History.
Definition: history.c:667
static struct History Histories[HC_MAX]
Command histories, one for each HistoryClass.
Definition: history.c:101
#define HC_FIRST
Definition: history.c:84
int mutt_hist_search(const char *search_buf, enum HistoryClass hclass, char **matches)
Find matches in a history list.
Definition: history.c:414
void mutt_hist_init(void)
Create a set of empty History ring buffers.
Definition: history.c:470
bool mutt_hist_at_scratch(enum HistoryClass hclass)
Is the current History position at the 'scratch' place?
Definition: history.c:650
static struct History * get_history(enum HistoryClass hclass)
Get a particular history.
Definition: history.c:111
static void save_history(enum HistoryClass hclass, const char *str)
Save one history string to a file.
Definition: history.c:319
void mutt_hist_add(enum HistoryClass hclass, const char *str, bool save)
Add a string to a history.
Definition: history.c:488
void mutt_hist_reset_state(enum HistoryClass hclass)
Move the 'current' position to the end of the History.
Definition: history.c:584
static int dup_hash_dec(struct HashTable *dup_hash, char *str)
Decrease the refcount of a history string.
Definition: history.c:157
char * mutt_hist_prev(enum HistoryClass hclass)
Get the previous string in a History.
Definition: history.c:556
void mutt_hist_complete(char *buf, size_t buflen, enum HistoryClass hclass)
Complete a string from a history list.
Definition: history.c:684
static void init_history(struct History *h)
Set up a new History ring buffer.
Definition: history.c:127
static void shrink_histfile(void)
Read, de-dupe and write the history file.
Definition: history.c:204
void mutt_hist_cleanup(void)
Free all the history lists.
Definition: history.c:443
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:45
#define MAX(a, b)
Definition: memory.h:31
int mutt_ch_convert_string(char **ps, const char *from, const char *to, uint8_t flags)
Convert a string between encodings.
Definition: charset.c:831
#define MUTT_ICONV_NO_FLAGS
No flags are set.
Definition: charset.h:72
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:654
const char * mutt_istr_find(const char *haystack, const char *needle)
Find first occurrence of string (ignoring case)
Definition: string.c:515
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:575
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:274
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:43
Key value store.
#define NONULL(x)
Definition: string2.h:37
A config-change event.
Definition: subset.h:71
const char * name
Name of config item that changed.
Definition: subset.h:73
The item stored in a Hash Table.
Definition: hash.h:43
void * data
User-supplied data.
Definition: hash.h:46
A Hash Table.
Definition: hash.h:97
Saved list of user-entered commands/searches.
Definition: history.c:92
short cur
Current history item.
Definition: history.c:94
short last
Last history item.
Definition: history.c:95
char ** hist
Array of history items.
Definition: history.c:93
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
Data passed to a notification function.
Definition: observer.h:34
void * event_data
Data from notify_send()
Definition: observer.h:38
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:36
#define mutt_file_mkstemp()
Definition: tmp.h:36