/* -*-c-*-
*
- * $Id: xgetline.c,v 1.1 1998/11/16 23:00:49 mdw Exp $
+ * $Id: xgetline.c,v 1.10 1999/05/21 22:09:19 mdw Exp $
*
* Fetch a line of text from the user
*
/*----- Revision history --------------------------------------------------*
*
* $Log: xgetline.c,v $
+ * Revision 1.10 1999/05/21 22:09:19 mdw
+ * Take advantage of new dynamic string macros.
+ *
+ * Revision 1.9 1999/05/05 18:54:37 mdw
+ * Keep blank lines out of the history list.
+ *
+ * Revision 1.8 1998/12/16 19:58:53 mdw
+ * Stop the dropdown list from dropping down when you press enter.
+ *
+ * Revision 1.7 1998/12/11 09:53:02 mdw
+ * Updates for mLib/mgLib. Support history files for recalling past
+ * entries, using a drop-down list.
+ *
+ * Revision 1.6 1998/12/03 00:56:29 mdw
+ * Set focus on the entry field, rather than leaving things to luck.
+ *
+ * Revision 1.5 1998/12/03 00:39:44 mdw
+ * Force focus when starting up.
+ *
+ * Revision 1.4 1998/11/30 22:36:47 mdw
+ * Tidy up tabbing in help texts very slightly.
+ *
+ * Revision 1.3 1998/11/21 22:30:20 mdw
+ * Support GNU-style long options throughout, and introduce proper help
+ * text to all programs. Update manual pages to match.
+ *
+ * Revision 1.2 1998/11/18 21:25:30 mdw
+ * Remove bogus `-h' option from the options list.
+ *
* Revision 1.1 1998/11/16 23:00:49 mdw
* Initial versions.
*
#include <stdlib.h>
#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
-#include "mdwopt.h"
+#include <mLib/alloc.h>
+#include <mLib/dstr.h>
+#include <mLib/mdwopt.h>
+#include <mLib/report.h>
+#include <mLib/quis.h>
+
+#include <mgLib/cancel.h>
+#include <mgLib/mdwfocus.h>
/*----- Main code ---------------------------------------------------------*/
-/* --- @cancel@ --- *
+/* --- @quit@ --- *
*
* Arguments: @GtkWidget *w@ = widget raising the signal
* @gpointer *p@ = pointer to integer result code
* Use: Sets the result code to zero (failure) and ends the loop.
*/
-static void cancel(GtkWidget *w, gpointer *p)
+static void quit(GtkWidget *w, gpointer *p)
{
int *ip = (int *)p;
*ip = 0;
gtk_main_quit();
}
-/* --- @check_escape@ --- *
+/* --- @version@ --- *
*
- * Arguments: @GtkWidget *w@ = widget raising the signal
- * @GdkEventKey *ev@ = pointer to event data
- * @gpointer *p@ = widget to activate in response
+ * Arguments: @FILE *fp@ = output stream to print the message on
*
* Returns: ---
*
- * Use: Activates a widget when an escape keypress is detected.
+ * Use: Spits out a version message.
*/
-static gboolean check_escape(GtkWidget *w, GdkEventKey *ev, gpointer *p)
+static void version(FILE *fp)
{
- if (ev->keyval == GDK_Escape) {
- if (p)
- gtk_widget_activate(GTK_WIDGET(p));
- else
- gtk_object_destroy(GTK_OBJECT(w));
- return (1);
- }
- return (0);
+ fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
+}
+
+/* --- @usage@ --- *
+ *
+ * Arguments: @FILE *fp@ = output stream to print the message on
+ *
+ * Returns: ---
+ *
+ * Use: Spits out a usage message.
+ */
+
+static void usage(FILE *fp)
+{
+ fprintf(fp,
+ "Usage: %s [-in] [-t title] [-p prompt] [-d default]\n"
+ "\t[-l|-H file] [-m max]\n",
+ QUIS);
}
/* --- @main@ --- *
unsigned f = 0;
int ok = 0;
+ const char *list = 0;
+ int histmax = 20;
+ GList *hist;
+
enum {
f_invis = 1,
- f_duff = 2
+ f_duff = 2,
+ f_history = 4,
+ f_nochoice = 8
};
/* --- User interface bits --- */
* parser would barf about.
*/
+ ego(argv[0]);
gtk_init(&argc, &argv);
/* --- Parse options from command line --- */
static struct option opt[] = {
{ "help", 0, 0, 'h' },
+ { "usage", 0, 0, 'u' },
+ { "version", 0, 0, 'v' },
{ "title", required_argument, 0, 't' },
{ "prompt", required_argument, 0, 'p' },
{ "default", required_argument, 0, 'd' },
{ "password", 0, 0, 'i' },
{ "invisible", 0, 0, 'i' },
+ { "history", required_argument, 0, 'H' },
+ { "list", required_argument, 0, 'l' },
+ { "histmax", required_argument, 0, 'm' },
+ { "no-choice", 0, 0, 'n' },
{ 0, 0, 0, 0 }
};
int i;
/* --- Fetch an option --- */
- i = getopt_long(argc, argv, "ht:p:d:i", opt, 0);
+ i = getopt_long(argc, argv, "huv t:p:d:i H:l:m:n", opt, 0);
if (i < 0)
break;
/* --- Work out what to do with it --- */
switch (i) {
+ case 'h':
+ version(stdout);
+ fputs("\n", stdout);
+ usage(stdout);
+ fputs(
+"\n"
+"Pops up a small window requesting input from a user, and echoes the\n"
+"response to stdout, where it can be collected by a shell script.\n"
+"\n"
+"Options available are:\n"
+"\n"
+"-h, --help Display this help text\n"
+"-u, --usage Display a short usage summary\n"
+"-v, --version Display the program's version number\n"
+"\n"
+"-i, --invisible\t Don't show the user's string as it's typed\n"
+"-t, --title=TITLE Set the window's title string\n"
+"-p, --prompt=PROMPT Set the window's prompt string\n"
+"-d, --default=DEFAULT Set the default string already in the window\n"
+"\n"
+"-l, --list=FILE Read FILE into a drop-down list\n"
+"-n, --no-choice No free text input: must choose item from list\n"
+"-H, --history=FILE As for `--list', but update with new string\n"
+"-m, --histmax=MAX Maximum number of items written back to file\n",
+ stdout);
+ exit(0);
+ break;
+ case 'u':
+ usage(stdout);
+ exit(0);
+ break;
+ case 'v':
+ version(stdout);
+ exit(0);
+ break;
+
case 't':
title = optarg;
break;
case 'i':
f |= f_invis;
break;
+
+ case 'l':
+ list = optarg;
+ break;
+ case 'n':
+ f |= f_nochoice;
+ break;
+ case 'H':
+ f |= f_history;
+ list = optarg;
+ break;
+ case 'm':
+ histmax = atoi(optarg);
+ break;
+
default:
f |= f_duff;
break;
}
if (f & f_duff) {
- fprintf(stderr, "xgetline: bad arguments\n");
+ usage(stderr);
exit(EXIT_FAILURE);
}
+ if ((f & f_invis) && list) {
+ die(EXIT_FAILURE,
+ "invisible entry is dumb if you provide a list of alternatives!");
+ }
+
+ if ((f & f_nochoice) && !list)
+ die(EXIT_FAILURE, "nothing to restrict choice to!");
+
/* --- Create the main window --- */
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(win), title);
gtk_window_position(GTK_WINDOW(win), GTK_WIN_POS_MOUSE);
gtk_signal_connect(GTK_OBJECT(win), "destroy",
- GTK_SIGNAL_FUNC(cancel), &ok);
+ GTK_SIGNAL_FUNC(quit), &ok);
/* --- Create the box for laying out the widgets inside --- */
/* --- Create the entry widget --- */
- entry = gtk_entry_new();
- gtk_entry_set_text(GTK_ENTRY(entry), dfl);
- gtk_table_attach(GTK_TABLE(box), entry,
- left, left + 1, 0, 1,
- GTK_EXPAND | GTK_FILL, GTK_EXPAND, 4, 2);
- if (f & f_invis)
- gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
- gtk_widget_show(entry);
+ if (list) {
+ FILE *fp = fopen(list, "r");
+ GtkWidget *combo;
+
+ /* --- Read the items in from the file --- *
+ *
+ * Inability to open the file is not a disaster.
+ */
+
+ hist = 0;
+ if (fp) {
+ dstr d = DSTR_INIT;
+
+ while (dstr_putline(&d, fp) != EOF) {
+ hist = g_list_append(hist, xstrdup(d.buf));
+ dstr_destroy(&d);
+ }
+ fclose(fp);
+ }
+
+ /* --- Now create a combo box --- */
+
+ combo = gtk_combo_new();
+ entry = GTK_COMBO(combo)->entry;
+ if (hist)
+ gtk_combo_set_popdown_strings(GTK_COMBO(combo), hist);
+
+ /* --- Do other configuring --- */
+
+ if (f & f_nochoice) {
+ gtk_combo_set_value_in_list(GTK_COMBO(combo), 1, 0);
+ gtk_entry_set_editable(GTK_ENTRY(entry), 0);
+ }
+ gtk_combo_set_case_sensitive(GTK_COMBO(combo), 1);
+ gtk_combo_set_use_arrows_always(GTK_COMBO(combo), 1);
+ gtk_combo_disable_activate(GTK_COMBO(combo));
+ if (strcmp(dfl, "@") == 0)
+ gtk_entry_set_text(GTK_ENTRY(entry), hist ? (char *)hist->data : "");
+ else
+ gtk_entry_set_text(GTK_ENTRY(entry), dfl);
+
+ /* --- Set the widget in the right place and show it --- */
+
+ gtk_table_attach(GTK_TABLE(box), combo,
+ left, left + 1, 0, 1,
+ GTK_EXPAND | GTK_FILL, GTK_EXPAND, 4, 2);
+ gtk_widget_show(combo);
+ } else {
+ entry = gtk_entry_new();
+ gtk_entry_set_text(GTK_ENTRY(entry), dfl);
+ if (f & f_invis)
+ gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
+ gtk_table_attach(GTK_TABLE(box), entry,
+ left, left + 1, 0, 1,
+ GTK_EXPAND | GTK_FILL, GTK_EXPAND, 4, 2);
+ gtk_widget_show(entry);
+ }
/* --- Create the default action widget --- */
gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
GTK_SIGNAL_FUNC(gtk_widget_activate),
GTK_OBJECT(btn));
- gtk_signal_connect(GTK_OBJECT(win), "key_press_event",
- GTK_SIGNAL_FUNC(check_escape), 0);
+ cancel(GTK_WINDOW(win), 0);
/* --- Go go go --- */
+ gtk_widget_realize(win);
+ mdwfocus(win);
+ gtk_widget_grab_focus(entry);
gtk_widget_show(win);
gtk_main();
if (ok) {
char *p = gtk_entry_get_text(GTK_ENTRY(entry));
+
+ /* --- If history is enabled, output a new history file --- *
+ *
+ * If the first entry was accepted verbatim, or if the entry is a blank
+ * line, don't bother.
+ */
+
+ if (f & f_history && *p && !(hist && strcmp(p, hist->data) == 0)) {
+ int fd;
+ FILE *fp;
+ int i;
+ GList *g;
+
+ if ((fd = open(list, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0)
+ goto fail;
+ if ((fp = fdopen(fd, "w")) == 0) {
+ close(fd);
+ goto fail;
+ }
+
+ fputs(p, fp);
+ fputc('\n', fp);
+
+ for (i = 1, g = hist; (histmax < 1 || i < histmax) && g; g = g->next) {
+ if (*(char *)g->data && strcmp(g->data, p) != 0) {
+ fputs(g->data, fp);
+ fputc('\n', fp);
+ i++;
+ }
+ }
+ fclose(fp);
+ fail:;
+ }
+
+ /* --- Print the result and go away --- */
+
puts(p);
}