[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

read and readline

From: Jack Pavlovsky
Subject: read and readline
Date: Fri, 26 Sep 2003 09:58:48 +0400
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030819


I've found a lack of very important feature in bash read.
Bash's read, with "-e" option, can use readline. But it lacks
one important (and IMAO, often necessary) feature: to provide
starting value for the argument to edit. E.g., I have this code:

some_variable="long and meaningful line"
read -e -p "edit some_variable: " some_variable

I want to have some option to have this read to start editing
from the current value of some_variable, not from scratch. The
readline library itself allows such things (a very small C program
is attached). Bash's read should use readline to it full extent
(readline is written mainly for bash, isn't it?), so this feature
should be implemented. I guess it'd be best to have it as an option
to read.
#include <stdlib.h>
#include <stdio.h>
#include <readline/readline.h>

char *my_rl_default;

int set_rl_default(void) {
  return 0;

int main(int argc, char **argv)
  char *str;

  if (argc < 3) {
    printf("Usage: %s prompt default\n", argv[0]);
    printf("\nDisplay a prompt, edit a variable with the initial value 
    printf("and output the result on stderr.\n");

  /* klugey way to swap stdout and stderr places (to output all the
  ** interactive stuff to stderr) */
  FILE *temp = stdout;
  stdout = stderr;
  stderr = temp;

  rl_startup_hook = (Function*)set_rl_default;
  my_rl_default = argv[2];
  if (str = readline(argv[1])) {
    fprintf(stderr, "%s\n", str);

reply via email to

[Prev in Thread] Current Thread [Next in Thread]