[vlc-devel] [PATCH] cli: fix line trimming with socket cli

Alexandre Janniaux ajanni at videolabs.io
Thu Nov 5 12:05:23 CET 2020


I forgot to mention it's probably a regression from the commit
9e1d92b91bd146f9dbab88cc47504dd6d8970d33 which changes the way
line is initially trimmed (reading the whole line instead of
stopping at the first CR/LF character). I'll add it to the
commit message before merging.

Regards,
--
Alexandre Janniaux
Videolabs

On Thu, Nov 05, 2020 at 12:01:49PM +0100, Alexandre Janniaux wrote:
> When using cli with --rc-host and --rc-fake-tty to control the cli
> interface remotely, and using telnet to connect to it, \r\n is appended
> at the end of each command. The code was removing the \n but not the \r
> character. This refactor trimming into a function for clarity and uses
> it to clean the beginning (additional spaces) and end (additional spaces
> or line feed / carriage return) of the command.
> ---
>  modules/control/cli/cli.c | 28 +++++++++++++++++++++++-----
>  1 file changed, 23 insertions(+), 5 deletions(-)
>
> diff --git a/modules/control/cli/cli.c b/modules/control/cli/cli.c
> index c741e7fcad..2095d9b8e8 100644
> --- a/modules/control/cli/cli.c
> +++ b/modules/control/cli/cli.c
> @@ -287,11 +287,9 @@ static void UnknownCmd(intf_thread_t *intf, const char *const *args,
>      (void) count;
>  }
>
> -static void Process(intf_thread_t *intf, const char *line)
> +static void Process(intf_thread_t *intf, char *cmd)
>  {
>      intf_sys_t *sys = intf->p_sys;
> -    /* Skip heading spaces */
> -    const char *cmd = line + strspn(line, " ");
>
>      if (*cmd == '\0')
>          return; /* Ignore empty line */
> @@ -349,6 +347,27 @@ error:      wordfree(&we);
>  #endif
>  }
>
> +static char *TrimLine(char *line)
> +{
> +    /* Skip heading spaces */
> +    line = &line[strspn(line, " ")];
> +
> +    size_t length = strlen(line);
> +
> +    /* remove trailing CR/LF */
> +    while (length > 0)
> +    {
> +        char c = line[length - 1];
> +        if (c != '\r' && c != '\n' && c != ' ')
> +            return line;
> +
> +        line[length - 1] = '\0';
> +        length--;
> +    }
> +
> +    return line;
> +}
> +
>  #ifndef _WIN32
>  static void *Run(void *data)
>  {
> @@ -386,8 +405,7 @@ static void *Run(void *data)
>          if (cmd != NULL)
>          {
>              int canc = vlc_savecancel();
> -            if (cmd[0] != '\0')
> -                cmd[strlen(cmd) - 1] = '\0'; /* remove trailing LF */
> +            cmd = TrimLine(cmd);
>              Process(intf, cmd);
>              vlc_restorecancel(canc);
>          }
> --
> 2.29.2
>


More information about the vlc-devel mailing list