On Tuesday 16 December 2014 11:21:44 Bartosz Golaszewski wrote:
> Move read_line() to libbb in order to make it available for other applets.
> While we're at it: implement a simple function which displays a prompt message
> and waits for user confirmation.
> 
> function                                             old     new   delta
> read_int                                             449     472     +23
> read_nonempty                                         30      49     +19
> .rodata                                           152682  152693     +11
> read_line                                            101      90     -11
> ------------------------------------------------------------------------------
> (add/remove: 0/0 grow/shrink: 3/1 up/down: 53/-11)             Total: 42 bytes
> 
> Signed-off-by: Bartosz Golaszewski <[email protected]>
> ---
>  include/libbb.h    |  2 ++
>  libbb/lineedit.c   | 30 ++++++++++++++++++++++++++++++
>  util-linux/fdisk.c | 23 ++---------------------
>  3 files changed, 34 insertions(+), 21 deletions(-)
> 
> diff --git a/include/libbb.h b/include/libbb.h
> index 8e8b9ca..21018e5 100644
> --- a/include/libbb.h
> +++ b/include/libbb.h
> @@ -1522,6 +1522,8 @@ int read_line_input(const char* prompt, char* command, 
> int maxsize) FAST_FUNC;
>       read_line_input(prompt, command, maxsize)
>  #endif
>  
> +int read_line(const char *prompt, char *line_buffer, size_t buflen, char 
> **line_ptr);
> +int user_confirm(const char *prompt);
>  
>  #ifndef COMM_LEN
>  # ifdef TASK_COMM_LEN
> diff --git a/libbb/lineedit.c b/libbb/lineedit.c
> index 8564307..a27771d 100644
> --- a/libbb/lineedit.c
> +++ b/libbb/lineedit.c
> @@ -2833,6 +2833,36 @@ int FAST_FUNC read_line_input(const char* prompt, 
> char* command, int maxsize)
>  
>  #endif  /* !FEATURE_EDITING */
>  
> +/* Read line; return 0 or first printable char */
> +int read_line(const char *prompt, char *line_buffer, size_t buflen, char 
> **line_ptr)
> +{
> +     int sz;
> +
> +     sz = read_line_input(NULL, prompt, line_buffer, buflen, /*timeout*/ -1);
> +     if (sz <= 0)
> +             exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
> +
> +     if (line_buffer[sz-1] == '\n')
> +             line_buffer[--sz] = '\0';
> +
> +     *line_ptr = line_buffer;
> +     while (**line_ptr != '\0' && (unsigned char)**line_ptr <= ' ')
> +             (*line_ptr)++;
> +     return **line_ptr;
> +}
> +
> +/*
> + * Display the prompt message followed by a '[y/N]' and wait for user
> + * confirmation. Return 1 if user entered 'y' or 'Y', or 0 otherwise.
> + */
> +int user_confirm(const char *prompt)
> +{
> +     char *line_ptr, buf[3], p[64];
> +
> +     snprintf(p, sizeof(p), "%s [y/N]: ", prompt);
> +
> +     return tolower(read_line(p, buf, sizeof(buf), &line_ptr)) == 'y' ? 1 : 
> 0;
> +}
>  

Hi,
We have something similar in libb/bb_ask_comfirmation.c

int FAST_FUNC bb_ask_confirmation(void)
{
        char first = 0;
        int c;

        while (((c = getchar()) != EOF) && (c != '\n')) {
                if (first == 0 && !isblank(c)) {
                        first = c|0x20;
                }
        }

        return first == 'y';
}

Ciao,
Tito

>  /*
>   * Testing
> diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
> index 39eb27b..4ba1e90 100644
> --- a/util-linux/fdisk.c
> +++ b/util-linux/fdisk.c
> @@ -563,25 +563,6 @@ seek_sector(sector_t secno)
>  }
>  
>  #if ENABLE_FEATURE_FDISK_WRITABLE
> -/* Read line; return 0 or first printable char */
> -static int
> -read_line(const char *prompt)
> -{
> -     int sz;
> -
> -     sz = read_line_input(NULL, prompt, line_buffer, sizeof(line_buffer), 
> /*timeout*/ -1);
> -     if (sz <= 0)
> -             exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
> -
> -     if (line_buffer[sz-1] == '\n')
> -             line_buffer[--sz] = '\0';
> -
> -     line_ptr = line_buffer;
> -     while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
> -             line_ptr++;
> -     return *line_ptr;
> -}
> -
>  static void
>  set_all_unchanged(void)
>  {
> @@ -607,7 +588,7 @@ write_part_table_flag(char *b)
>  static char
>  read_nonempty(const char *mesg)
>  {
> -     while (!read_line(mesg))
> +     while (!read_line(mesg, line_buffer, sizeof(line_buffer), &line_ptr))
>               continue;
>       return *line_ptr;
>  }
> @@ -615,7 +596,7 @@ read_nonempty(const char *mesg)
>  static char
>  read_maybe_empty(const char *mesg)
>  {
> -     if (!read_line(mesg)) {
> +     if (!read_line(mesg, line_buffer, sizeof(line_buffer), &line_ptr)) {
>               line_ptr = line_buffer;
>               line_ptr[0] = '\n';
>               line_ptr[1] = '\0';
> 
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to