/* pick -- interactively select arguments to be echoed Copyright (C) 1990-1999 Free Software Foundation, Inc. This program 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, or (at your option) any later version. This program 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 this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include "error.h" #include "long-options.h" #include "readtokens.h" #include "system.h" #define PROGRAM_NAME "pick" #define AUTHORS "Leonard Stiles" #define DELIM "\n" #define RESPFNAME "/dev/tty" /* whence to read user response */ char *program_name; void usage (int status) { if (status != 0) fprintf (stderr, _("Try `%s --help' for more information.\n"), program_name); else { printf (_("\ Usage: %s ARGUMENT...\n\ or: %s OPTION\n\ "), program_name, program_name); printf (_("\ Interactively select arguments to be echoed\n\ \n\ --help display this help and exit\n\ --version output version information and exit\n\ \n\ For each ARGUMENT, prompt whether it should be echoed to the standard\n\ output; if the only argument is `-', read from the standard input.\n\ ")); puts (_("\nReport bugs to .")); } exit (status); } void pick (FILE* fp, char *s) { int c; fprintf (stderr, "%s? ", s); while ((c = fgetc (fp)) == ' ') ; if (c == 'y') puts (s); else if (c == 'q' || c == EOF) exit (0); while (c != '\n') c = fgetc (fp); } int main (int argc, char **argv) { FILE *ttyf; program_name = *argv; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION, AUTHORS, usage); /* The above handles --help and --version. Since there is no other invocation of getopt, handle `--' here. */ if (argc > 1 && STREQ (argv[1], "--")) { --argc; ++argv; } if ((ttyf = fopen (RESPFNAME, "r")) == NULL) error (2, errno, _("error opening %s"), RESPFNAME); if (argc == 2 && STREQ (argv[1], "-")) { token_buffer tbuf; init_tokenbuffer (&tbuf); while (readtoken (stdin, DELIM, sizeof (DELIM) - 1, &tbuf) >= 0) pick (ttyf, tbuf.buffer); free (tbuf.buffer); } else while (--argc) pick (ttyf, *++argv); if (fclose (ttyf) != 0) error (2, errno, _("error closing %s"), RESPFNAME); return 0; }