Re: cmdline_file_save accepts file handler instead

"Papp Gyozo (VBuster)" <[email protected]> Tue, 30 Oct 2007 18:32:29 +0100
Newsgroups gmane.comp.gnu.gengetopt.general
Message-ID <[email protected]>
Hi,

> >> that would be a very nice compromise: this way " is still use to
> >> (optionally) enclose arguments, and if " is part of the argument it
> >> should be escaped.  While ' can be used as it is (which is what you
> >> need, I seem to understand).
> 
> so are you working on this modified patch?

Yes, I think it is actually quite simple (follow where > missing from the beginning of the line):

> @@ -574,8 +572,11 @@
>              }
>          }
>        else
> -        { /* read up the remaining part up to a delimiter */
> -          next_token = strcspn (farg, " \t\r\n#\'\"");
> +        { /* read up the remaining part up to end of line or comment sign */
  +          char *ch = (str_index + (next_token = strcspn (farg, "\r\n\"#")));
> +          /* remove trailing whitespaces */
> +          for ( --ch; (*ch >= 0x09 && *ch <= 0x0D) || *ch == ' '; --ch )
> +            --next_token;
>            str_index += next_token;
>          }

Changes (against what you can see in CVS)  in brief:
- allow ' ', '\t' and '\'' in option value, that is,
- those are removed from the strcspn's character set.
- adjust str_index and next_token after removing trailing whitespaces

So if we disallow '"' then strcspn above stops on the first ", so strings like mal"formed will be malformed again. Please note this code is activated only if the value starts with neither ' nor ".