1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#define _LARGEFILE64_SOURCE
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <regex.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "mt.h"
#include "error.h"
#include "utils.h"
#include "mem.h"
#include "selbox.h"
#include "ui.h"
#include "help.h"
#include "globals.h"
void init_history(history_t *ph)
{
}
void load_history(history_t *ph)
{
if (ph -> history == NULL)
{
int array_in_bytes = sizeof(char *) * ph -> history_size;
ph -> history = (char **)mymalloc(array_in_bytes, __FILE__, __PRETTY_FUNCTION__, __LINE__);
memset(ph -> history, 0x00, array_in_bytes);
if (file_exist(ph -> history_file) == 0)
{
int loop;
FILE *fh = fopen(ph -> history_file, "r");
if (!fh)
error_popup("Load history", -1, "Failed to open history file %s.\n", ph -> history_file);
for(loop=0; loop<ph -> history_size; loop++)
{
char *lf;
char buffer[HISTORY_IO_BUFFER];
if (!fgets(buffer, sizeof(buffer), fh))
break;
lf = strchr(buffer, '\n');
if (lf)
*lf = 0x00;
(ph -> history)[loop] = mystrdup(buffer, __FILE__, __PRETTY_FUNCTION__, __LINE__);
}
fclose(fh);
}
}
}
int history_find_null_entry(history_t *ph)
{
int loop;
for(loop=0; loop<ph -> history_size; loop++)
{
if (!(ph -> history)[loop])
{
return loop;
}
}
return -1;
}
void save_history(history_t *ph)
{
int loop;
int last_entry = history_find_null_entry(ph);
int n_entries = last_entry == -1 ? ph -> history_size : last_entry;
FILE *fh = fopen(ph -> history_file, "w+");
if (!fh)
{
error_popup("Error saving history", -1, "Error creating/opening file %s: %s", ph -> history_file, strerror(errno));
return;
}
for(loop=0; loop<n_entries; loop++)
{
fprintf(fh, "%s\n", (ph -> history)[loop]);
}
fclose(fh);
}
void history_add(history_t *ph, char *string)
{
if (ph -> history_size > 0)
{
int loop;
int found = -1;
load_history(ph);
/* bail out if this string is already in the history */
for(loop=0; loop<ph -> history_size; loop++)
{
if ((ph -> history)[loop] != NULL && strcmp((ph -> history)[loop], string) == 0)
return ;
}
/* find free spot */
found = history_find_null_entry(ph);
/* when no free spot was found, free-up an entry */
if (found == -1)
{
myfree((ph -> history)[ph -> history_size - 1]);
memmove(&(ph -> history)[1], &(ph -> history)[0], sizeof(char *) * (ph -> history_size - 1));
found = 0;
}
(ph -> history)[found] = mystrdup(string, __FILE__, __PRETTY_FUNCTION__, __LINE__);
save_history(ph);
}
}
char * search_history(history_t *ph, char *search_string)
{
if (ph -> history_size > 0)
{
int sel_index, n_entries, free_entry;
load_history(ph);
free_entry = history_find_null_entry(ph);
n_entries = free_entry == -1 ? ph -> history_size : free_entry;
if (n_entries > 0)
{
sel_index = selection_box((void **)ph -> history, NULL, n_entries, SEL_HISTORY, HELP_HISTORY, NULL);
if (sel_index >= 0)
{
return mystrdup((ph -> history)[sel_index], __FILE__, __PRETTY_FUNCTION__, __LINE__);
}
}
else
error_popup("Search history", -1, "The history list is empty.");
}
return NULL;
}
|