1*1acd27e7Smillert /* 2*1acd27e7Smillert * rl - command-line interface to read a line from the standard input 3*1acd27e7Smillert * (or another fd) using readline. 4*1acd27e7Smillert * 5*1acd27e7Smillert * usage: rl [-p prompt] [-u unit] [-d default] [-n nchars] 6*1acd27e7Smillert */ 7*1acd27e7Smillert 8*1acd27e7Smillert #if defined (HAVE_CONFIG_H) 9*1acd27e7Smillert # include <config.h> 10*1acd27e7Smillert #endif 11*1acd27e7Smillert 12*1acd27e7Smillert #include <stdio.h> 13*1acd27e7Smillert #include <sys/types.h> 14*1acd27e7Smillert #include "posixstat.h" 15*1acd27e7Smillert 16*1acd27e7Smillert #if defined (READLINE_LIBRARY) 17*1acd27e7Smillert # include "readline.h" 18*1acd27e7Smillert # include "history.h" 19*1acd27e7Smillert #else 20*1acd27e7Smillert # include <readline/readline.h> 21*1acd27e7Smillert # include <readline/history.h> 22*1acd27e7Smillert #endif 23*1acd27e7Smillert 24*1acd27e7Smillert extern int optind; 25*1acd27e7Smillert extern char *optarg; 26*1acd27e7Smillert 27*1acd27e7Smillert #if !defined (strchr) && !defined (__STDC__) 28*1acd27e7Smillert extern char *strrchr(); 29*1acd27e7Smillert #endif 30*1acd27e7Smillert 31*1acd27e7Smillert static char *progname; 32*1acd27e7Smillert static char *deftext; 33*1acd27e7Smillert 34*1acd27e7Smillert static int 35*1acd27e7Smillert set_deftext () 36*1acd27e7Smillert { 37*1acd27e7Smillert if (deftext) 38*1acd27e7Smillert { 39*1acd27e7Smillert rl_insert_text (deftext); 40*1acd27e7Smillert deftext = (char *)NULL; 41*1acd27e7Smillert rl_startup_hook = (Function *)NULL; 42*1acd27e7Smillert } 43*1acd27e7Smillert return 0; 44*1acd27e7Smillert } 45*1acd27e7Smillert 46*1acd27e7Smillert static void 47*1acd27e7Smillert usage() 48*1acd27e7Smillert { 49*1acd27e7Smillert fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n", 50*1acd27e7Smillert progname, progname); 51*1acd27e7Smillert } 52*1acd27e7Smillert 53*1acd27e7Smillert int 54*1acd27e7Smillert main (argc, argv) 55*1acd27e7Smillert int argc; 56*1acd27e7Smillert char **argv; 57*1acd27e7Smillert { 58*1acd27e7Smillert char *temp, *prompt; 59*1acd27e7Smillert struct stat sb; 60*1acd27e7Smillert int opt, fd, nch; 61*1acd27e7Smillert FILE *ifp; 62*1acd27e7Smillert 63*1acd27e7Smillert progname = strrchr(argv[0], '/'); 64*1acd27e7Smillert if (progname == 0) 65*1acd27e7Smillert progname = argv[0]; 66*1acd27e7Smillert else 67*1acd27e7Smillert progname++; 68*1acd27e7Smillert 69*1acd27e7Smillert /* defaults */ 70*1acd27e7Smillert prompt = "readline$ "; 71*1acd27e7Smillert fd = nch = 0; 72*1acd27e7Smillert deftext = (char *)0; 73*1acd27e7Smillert 74*1acd27e7Smillert while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF) 75*1acd27e7Smillert { 76*1acd27e7Smillert switch (opt) 77*1acd27e7Smillert { 78*1acd27e7Smillert case 'p': 79*1acd27e7Smillert prompt = optarg; 80*1acd27e7Smillert break; 81*1acd27e7Smillert case 'u': 82*1acd27e7Smillert fd = atoi(optarg); 83*1acd27e7Smillert if (fd < 0) 84*1acd27e7Smillert { 85*1acd27e7Smillert fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg); 86*1acd27e7Smillert exit (2); 87*1acd27e7Smillert } 88*1acd27e7Smillert break; 89*1acd27e7Smillert case 'd': 90*1acd27e7Smillert deftext = optarg; 91*1acd27e7Smillert break; 92*1acd27e7Smillert case 'n': 93*1acd27e7Smillert nch = atoi(optarg); 94*1acd27e7Smillert if (nch < 0) 95*1acd27e7Smillert { 96*1acd27e7Smillert fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg); 97*1acd27e7Smillert exit (2); 98*1acd27e7Smillert } 99*1acd27e7Smillert break; 100*1acd27e7Smillert default: 101*1acd27e7Smillert usage (); 102*1acd27e7Smillert exit (2); 103*1acd27e7Smillert } 104*1acd27e7Smillert } 105*1acd27e7Smillert 106*1acd27e7Smillert if (fd != 0) 107*1acd27e7Smillert { 108*1acd27e7Smillert if (fstat (fd, &sb) < 0) 109*1acd27e7Smillert { 110*1acd27e7Smillert fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd); 111*1acd27e7Smillert exit (1); 112*1acd27e7Smillert } 113*1acd27e7Smillert ifp = fdopen (fd, "r"); 114*1acd27e7Smillert rl_instream = ifp; 115*1acd27e7Smillert } 116*1acd27e7Smillert 117*1acd27e7Smillert if (deftext && *deftext) 118*1acd27e7Smillert rl_startup_hook = set_deftext; 119*1acd27e7Smillert 120*1acd27e7Smillert if (nch > 0) 121*1acd27e7Smillert rl_num_chars_to_read = nch; 122*1acd27e7Smillert 123*1acd27e7Smillert temp = readline (prompt); 124*1acd27e7Smillert 125*1acd27e7Smillert /* Test for EOF. */ 126*1acd27e7Smillert if (temp == 0) 127*1acd27e7Smillert exit (1); 128*1acd27e7Smillert 129*1acd27e7Smillert puts (temp); 130*1acd27e7Smillert exit (0); 131*1acd27e7Smillert } 132