rigctl.c: Add options to save/read command history

As commands from a previous session may not be desired, make reading the
history file at rigctl start and writing to it at rigctl close optional
by use of the -i/--read_history or -I/--save-history options.
Compilation is conditional on having Readline and Readline History
support detected at build system configuration time.

History is stored in '$HOME/.rigctl_history' by default.  An alternate
path may be specified by setting the RIGCTL_HIST_DIR environment
variable:

$ RIGCTL_HIST_DIR=~/.rigctl rigctl -iI

will temporarily set the history directory to /home/USER/.rigctl and
create .rigctl_history if it does not exist.  If the file cannot be read
or written a warning message will be given on stderr.
Hamlib-3.0
Nate Bargmann 2013-02-20 11:48:27 -06:00
rodzic 978a269689
commit 9f342cfd23
1 zmienionych plików z 64 dodań i 5 usunięć

Wyświetl plik

@ -49,6 +49,8 @@ extern char *readline ();
#endif /* HAVE_LIBREADLINE */
#ifdef HAVE_READLINE_HISTORY
# include <sys/stat.h>
# define HST_SHRT_OPTS "iI"
# if defined(HAVE_READLINE_HISTORY_H)
# include <readline/history.h>
# elif defined(HAVE_HISTORY_H)
@ -58,7 +60,9 @@ extern void add_history ();
extern int write_history ();
extern int read_history ();
# endif /* defined(HAVE_READLINE_HISTORY_H) */
/* no history */
#else
/* no history */
#define HST_SHRT_OPTS ""
#endif /* HAVE_READLINE_HISTORY */
@ -100,6 +104,10 @@ static struct option long_options[] =
{"show-conf", 0, 0, 'L'},
{"dump-caps", 0, 0, 'u'},
{"vfo", 0, 0, 'o'},
#ifdef HAVE_READLINE_HISTORY
{"read-history", 0, 0, 'i'},
{"save-history", 0, 0, 'I'},
#endif
{"verbose", 0, 0, 'v'},
{"help", 0, 0, 'h'},
{"version", 0, 0, 'V'},
@ -133,6 +141,14 @@ int main (int argc, char *argv[])
int verbose = 0;
int show_conf = 0;
int dump_caps_opt = 0;
#ifdef HAVE_READLINE_HISTORY
int rd_hist = 0;
int sv_hist = 0;
const char *hist_dir = NULL;
const char hist_file[] = "/.rigctl_history";
char *hist_path = NULL;
struct stat hist_dir_stat;
#endif
const char *rig_file=NULL, *ptt_file=NULL, *dcd_file=NULL;
ptt_type_t ptt_type = RIG_PTT_NONE;
dcd_type_t dcd_type = RIG_DCD_NONE;
@ -144,7 +160,7 @@ int main (int argc, char *argv[])
int c;
int option_index = 0;
c = getopt_long (argc, argv, SHORT_OPTIONS,
c = getopt_long (argc, argv, SHORT_OPTIONS HST_SHRT_OPTS,
long_options, &option_index);
if (c == -1)
break;
@ -262,6 +278,14 @@ int main (int argc, char *argv[])
case 'o':
vfo_mode++;
break;
#ifdef HAVE_READLINE_HISTORY
case 'i':
rd_hist++;
break;
case 'I':
sv_hist++;
break;
#endif
case 'v':
verbose++;
break;
@ -361,14 +385,31 @@ int main (int argc, char *argv[])
exitcode = 0;
#ifdef HAVE_LIBREADLINE
if (interactive && prompt && have_rl) {
rl_readline_name = "rigctl";
#ifdef HAVE_READLINE_HISTORY
using_history();
using_history(); /* Initialize Readline History */
if (rd_hist || sv_hist) {
if (!(hist_dir = getenv("RIGCTL_HIST_DIR")))
hist_dir = getenv("HOME");
if (((stat(hist_dir, &hist_dir_stat) == -1) && (errno == ENOENT))
|| !(S_ISDIR(hist_dir_stat.st_mode))) {
fprintf(stderr, "Warning: %s is not a directory!\n", hist_dir);
}
hist_path = (char *)calloc((sizeof(char) * (strlen(hist_dir) + strlen(hist_file) + 1)), sizeof(char));
strncpy(hist_path, hist_dir, strlen(hist_dir));
strncat(hist_path, hist_file, strlen(hist_file));
}
if (rd_hist && hist_path)
if (read_history(hist_path) == ENOENT)
fprintf(stderr, "Warning: Could not read history from %s\n", hist_path);
#endif
}
#endif /* HAVE_LIBREADLINE */
do {
@ -378,6 +419,20 @@ int main (int argc, char *argv[])
}
while (retcode == 0 || retcode == 2);
#ifdef HAVE_LIBREADLINE
if (interactive && prompt && have_rl) {
#ifdef HAVE_READLINE_HISTORY
if (sv_hist && hist_path)
if (write_history(hist_path) == ENOENT)
fprintf(stderr, "\nWarning: Could not write history to %s\n", hist_path);
if ((rd_hist || sv_hist) && hist_path) {
free(hist_path);
hist_path = (char *)NULL;
}
}
#endif
#endif
rig_close(my_rig); /* close port */
rig_cleanup(my_rig); /* if you care about memory */
@ -406,6 +461,10 @@ void usage(void)
" -l, --list list all model numbers and exit\n"
" -u, --dump-caps dump capabilities and exit\n"
" -o, --vfo do not default to VFO_CURR, require extra vfo arg\n"
#ifdef HAVE_READLINE_HISTORY
" -i, --read-history read prior interactive session history\n"
" -I, --save-history save current interactive session history\n"
#endif
" -v, --verbose set verbose mode, cumulative\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n\n"