-/* -*-c-*-
- *
- * $Id: xgetline.c,v 1.13 2004/04/08 01:36:29 mdw Exp $
- *
- * Fetch a line of text from the user
- *
- * (c) 1998 Straylight/Edgeware
- */
-
-/*----- Licensing notice --------------------------------------------------*
- *
- * This file is part of the Edgeware X tools collection.
- *
- * X tools is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * X tools is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with X tools; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-/*----- Header files ------------------------------------------------------*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <fcntl.h>
-#include <unistd.h>
-
-#include <gtk/gtk.h>
-#include <gdk/gdkkeysyms.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 ---------------------------------------------------------*/
-
-/* --- @quit@ --- *
- *
- * Arguments: @GtkWidget *w@ = widget raising the signal
- * @gpointer *p@ = pointer to integer result code
- *
- * Returns: ---
- *
- * Use: Sets the result code to zero (failure) and ends the loop.
- */
-
-static void quit(GtkWidget *w, gpointer *p)
-{
- int *ip = (int *)p;
- *ip = 0;
- gtk_main_quit();
-}
-
-/* --- @done@ --- *
- *
- * Arguments: @GtkWidget *w@ = widget raising the signal
- * @gpointer *p@ = pointer to integer result code
- *
- * Returns: ---
- *
- * Use: Sets the result code nonzero (success) and ends the loop.
- */
-
-static void done(GtkWidget *w, gpointer *p)
-{
- int *ip = (int *)p;
- *ip = 1;
- gtk_main_quit();
-}
-
-/* --- @version@ --- *
- *
- * Arguments: @FILE *fp@ = output stream to print the message on
- *
- * Returns: ---
- *
- * Use: Spits out a version message.
- */
-
-static void version(FILE *fp)
-{
- 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@ --- *
- *
- * Arguments: @int argc@ = number of command line arguments
- * @char *argv[]@ = addresses of arguments
- *
- * Returns: Zero if OK, and we read a string; nonzero if the user
- * cancelled.
- *
- * Use: Reads a string from the user, and returns it on standard
- * output.
- */
-
-int main(int argc, char *argv[])
-{
- /* --- Configuration variables --- */
-
- char *prompt = 0;
- char *dfl = "";
- char *title = "Input request";
- int left;
- unsigned f = 0;
- int ok = 0;
-
- const char *list = 0;
- int histmax = 20;
- GList *hist = 0;
-
-#define f_invis 1u
-#define f_duff 2u
-#define f_history 4u
-#define f_nochoice 8u
-
- /* --- User interface bits --- */
-
- GtkWidget *win;
- GtkWidget *box;
- GtkWidget *entry;
- GtkWidget *btn;
-
- /* --- Crank up the toolkit --- *
- *
- * Have to do this here: GTK snarfs some command line options which my
- * parser would barf about.
- */
-
- ego(argv[0]);
- gtk_init(&argc, &argv);
-
- /* --- Parse options from command line --- */
-
- for (;;) {
-
- /* --- Long options structure --- */
-
- static struct option opt[] = {
- { "help", 0, 0, 'h' },
- { "usage", 0, 0, 'u' },
- { "version", 0, 0, 'v' },
- { "title", OPTF_ARGREQ, 0, 't' },
- { "prompt", OPTF_ARGREQ, 0, 'p' },
- { "default", OPTF_ARGREQ, 0, 'd' },
- { "password", 0, 0, 'i' },
- { "invisible", 0, 0, 'i' },
- { "history", OPTF_ARGREQ, 0, 'H' },
- { "list", OPTF_ARGREQ, 0, 'l' },
- { "histmax", OPTF_ARGREQ, 0, 'm' },
- { "no-choice", 0, 0, 'n' },
- { 0, 0, 0, 0 }
- };
- int i;
-
- /* --- Fetch an option --- */
-
- 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 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 'p':
- prompt = optarg;
- break;
- case 'd':
- dfl = 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) {
- 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(quit), &ok);
-
- /* --- Create the box for laying out the widgets inside --- */
-
- left = (prompt ? 1 : 0);
- box = gtk_table_new(left + 2, 1, 0);
-
- /* --- Maybe create a prompt widget --- */
-
- if (prompt) {
- GtkWidget *w = gtk_label_new(prompt);
- gtk_table_attach(GTK_TABLE(box), w,
- 0, 1, 0, 1, 0, GTK_EXPAND, 4, 2);
- gtk_widget_show(w);
- }
-
- /* --- Create the entry widget --- */
-
- 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.
- */
-
- if (fp) {
- dstr d = DSTR_INIT;
-
- while (dstr_putline(&d, fp) != EOF) {
- hist = g_list_append(hist, xstrdup(d.buf));
- DRESET(&d);
- }
- 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 --- */
-
- btn = gtk_button_new_with_label("OK");
- gtk_table_attach(GTK_TABLE(box), btn,
- left + 1, left + 2, 0, 1, 0, GTK_EXPAND, 2, 2);
- GTK_WIDGET_SET_FLAGS(btn, GTK_CAN_DEFAULT);
- gtk_widget_show(btn);
-
- /* --- Add the box into the main window --- */
-
- gtk_container_add(GTK_CONTAINER(win), box);
- gtk_widget_show(box);
-
- /* --- Last minute configuration things --- */
-
- gtk_widget_grab_default(btn);
- gtk_signal_connect(GTK_OBJECT(btn), "clicked",
- GTK_SIGNAL_FUNC(done), &ok);
- gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
- GTK_SIGNAL_FUNC(gtk_widget_activate),
- GTK_OBJECT(btn));
- 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();
-
- /* --- Output the result --- */
-
- 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);
- }
-
- return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
-}
-
-/*----- That's all, folks -------------------------------------------------*/