40 printf(
"Escape an input string, adopting the av_get_token() escaping logic\n");
41 printf(
"usage: ffescape [OPTIONS]\n");
44 "-e echo each input line on output\n"
45 "-h print this help\n"
46 "-i INFILE set INFILE as input file, stdin if omitted\n"
47 "-l LEVEL set the number of escaping levels, 1 if omitted\n"
48 "-m ESCAPE_MODE select escape mode between 'full', 'lazy', 'quote', default is 'lazy'\n"
49 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
50 "-p PROMPT set output prompt, is '=> ' by default\n"
51 "-s SPECIAL_CHARS set the list of special characters\n");
54 #define WHITESPACES " \n\t"
62 static int escape(
char **
dst,
const char *src,
const char *special_chars,
78 if ((special_chars && strchr(special_chars, *src)) ||
79 strchr(
"'\\", *src) ||
87 char c = dstbuf.str[dstbuf.len-1];
88 dstbuf.str[dstbuf.len-1] =
'\\';
119 int main(
int argc,
char **argv)
122 char *src_buf, *dst_buf;
123 const char *outfilename =
NULL, *infilename =
NULL;
125 const char *prompt =
"=> ";
129 char *special_chars =
NULL;
132 while ((c =
getopt(argc, argv,
"ehi:l:o:m:p:s:")) != -1) {
146 long int li = strtol(
optarg, &tail, 10);
147 if (*tail || li > INT_MAX || li < 0) {
149 "Invalid value '%s' for option -l, argument must be a non negative integer\n",
162 "Invalid value '%s' for option -m, "
163 "valid arguments are 'full', 'lazy', 'quote'\n",
optarg);
181 if (!infilename || !strcmp(infilename,
"-")) {
182 infilename =
"stdin";
185 infile = fopen(infilename,
"r");
192 if (!outfilename || !strcmp(outfilename,
"-")) {
193 outfilename =
"stdout";
196 outfile = fopen(outfilename,
"w");
205 while ((c = fgetc(infile)) != EOF)
217 fprintf(outfile,
"%s", src_buf);
222 if (
escape(&dst_buf, src_buf, special_chars, escape_mode) < 0) {
230 fprintf(outfile,
"%s%s", prompt, dst_buf);