4

I am able to limit the user input to 5 characters using GNU readline:

#include <readline/readline.h>
#include <stdio.h>
#include <stdlib.h>

static int limit_rl(FILE *f)
{
    if (rl_end > 5) {
        return '\b';
    }
    return rl_getc(f);
}

int main(void)
{
    char *str;

    rl_getc_function = limit_rl;
    str = readline("> ");
    printf("%s\n", str);
    free(str);
    return 0;
}

But, how to read an input with a default value (not a prompt), e.g.:

> ummy
  ^ cursor here

if user types d and Enter return "dummy"

if user types DEL and Enter return "mmy"

David Ranieri
  • 39,972
  • 7
  • 52
  • 94

1 Answers1

2

On the homepage of readline a possible use is mentioned:

rl.c is an example program that uses Readline to read a line of input from a user and echo it to the standard output, suitable for use by shell scripts.

and since editing an existing entry could well be part of this, I decided to take a look at its source (direct download link). This indeed shows how to insert a string into the buffer used by readline before it appears on the screen, through the use of a hook function:

Variable: rl_hook_func_t * rl_startup_hook

If non-zero, this is the address of a function to call just before readline prints the first prompt.
(https://cnswww.cns.cwru.edu/php/chet/readline/readline.html#IDX223)

Inside the hook function you can directly manipulate the internal buffer, for example to insert text:

Function: int rl_insert_text (const char *text)

Insert text into the line at the current cursor position. Returns the number of characters inserted.
(https://cnswww.cns.cwru.edu/php/chet/readline/readline.html#IDX295)

The hook function only needs to do this once (it is called only once per readline call inside readline_internal_setup), but apparently rl's author went for the belt-and-suspenders approach and specifically disables it after it has been used.

Relevant snippets from rl.c, comments are mine:

/* a global char * to hold a default initial text */
static char *deftext;

/* the callback function. The argument is supposed to be 'void' per
   its declaration:
       typedef int rl_hook_func_t (void);
   so you cannot provide the default text here */
static int set_deftext ()
{
  if (deftext)
    {
      /* Apparently the "current cursor position" in which text is inserted
         is 0, when initially called */
      rl_insert_text (deftext);
      deftext = (char *)NULL;

      /* disable the global 'rl_startup_hook' function by setting it to NULL */
      rl_startup_hook = (rl_hook_func_t *)NULL;
    }
  return 0;
}

// ...
if (deftext && *deftext)
   rl_startup_hook = set_deftext;

temp = readline (prompt);
Jongware
  • 22,200
  • 8
  • 54
  • 100