Browse Source

Fix escaping within alt quotes

Serj Kalichev 3 years ago
parent
commit
48b8d2e49e
4 changed files with 19 additions and 9 deletions
  1. 8 3
      lub/argv/argv.c
  2. 3 2
      lub/ini/ini.c
  3. 2 1
      lub/string.h
  4. 6 3
      lub/string/string.c

+ 8 - 3
lub/argv/argv.c

@@ -8,6 +8,7 @@
 #include <string.h>
 #include <assert.h>
 #include <ctype.h>
+#include <stdio.h>
 
 /*--------------------------------------------------------- */
 static void lub_argv_init(lub_argv_t * this, const char *line, size_t off)
@@ -16,6 +17,7 @@ static void lub_argv_init(lub_argv_t * this, const char *line, size_t off)
 	const char *word = NULL;
 	lub_arg_t *arg = NULL;
 	bool_t quoted = BOOL_FALSE;
+	bool_t alt_quoted = BOOL_FALSE;
 	const char *str = line + off; // Start on specified offset
 	const char *offset = NULL;
 
@@ -32,10 +34,13 @@ static void lub_argv_init(lub_argv_t * this, const char *line, size_t off)
 	assert(arg);
 
 	/* then fill out the array with the words */
-	for (word = lub_string_nextword(str, &len, &offset, &quoted, NULL);
+	for (word = lub_string_nextword(str, &len, &offset, &quoted, NULL, &alt_quoted);
 		word && (*word != '\0');
-		word = lub_string_nextword(str, &len, &offset, &quoted, NULL)) {
-		(*arg).arg = lub_string_ndecode(word, len);
+		word = lub_string_nextword(str, &len, &offset, &quoted, NULL, &alt_quoted)) {
+		if (alt_quoted)
+			(*arg).arg = lub_string_dupn(word, len);
+		else
+			(*arg).arg = lub_string_ndecode(word, len);
 		(*arg).offset = offset - line;
 		(*arg).quoted = quoted;
 		str = offset;

+ 3 - 2
lub/ini/ini.c

@@ -125,12 +125,13 @@ int lub_ini_parse_str(lub_ini_t *this, const char *ini)
 			continue;
 		}
 		value = strtok_r(NULL, "=", &savestr);
-		begin = lub_string_nextword(name, &len, NULL, NULL, NULL);
+		begin = lub_string_nextword(name, &len, NULL, NULL, NULL, NULL);
 		rname = lub_string_dupn(begin, len);
 		if (!value) /* Empty value */
 			rvalue = NULL;
 		else {
-			begin = lub_string_nextword(value, &len, NULL, NULL, NULL);
+			begin = lub_string_nextword(value, &len, NULL, NULL,
+				NULL, NULL);
 			rvalue = lub_string_dupn(begin, len);
 		}
 		pair = lub_pair_new(rname, rvalue);

+ 2 - 1
lub/string.h

@@ -271,7 +271,8 @@ char *lub_string_tolower(const char *str);
 unsigned int lub_string_equal_part(const char *str1, const char *str2,
 	bool_t utf8);
 const char *lub_string_nextword(const char *str,
-	size_t *len, const char **offset, bool_t *quoted, bool_t *qclosed);
+	size_t *len, const char **offset, bool_t *quoted, bool_t *qclosed,
+	bool_t *alt_quoted);
 unsigned int lub_string_wordcount(const char *line);
 
 _END_C_DECL

+ 6 - 3
lub/string/string.c

@@ -284,7 +284,8 @@ const char *lub_string_suffix(const char *string)
  * @return Pointer to found substring (without quotes).
  */
 const char *lub_string_nextword(const char *str,
-	size_t *len, const char **offset, bool_t *quoted, bool_t *qclosed)
+	size_t *len, const char **offset, bool_t *quoted, bool_t *qclosed,
+	bool_t *altq)
 {
 	const char *string = str;
 	const char *word = NULL;
@@ -379,6 +380,8 @@ const char *lub_string_nextword(const char *str,
 		*quoted = dbl_quoted || alt_quoted;
 	if (qclosed)
 		*qclosed = closed_quote;
+	if (altq)
+		*altq = alt_quoted;
 
 	return word;
 }
@@ -390,9 +393,9 @@ unsigned int lub_string_wordcount(const char *line)
 	unsigned int result = 0;
 	const char *offset = NULL;
 
-	for (word = lub_string_nextword(line, NULL, &offset, NULL, NULL);
+	for (word = lub_string_nextword(line, NULL, &offset, NULL, NULL, NULL);
 		word && (*word != '\0');
-		word = lub_string_nextword(offset, NULL, &offset, NULL, NULL)) {
+		word = lub_string_nextword(offset, NULL, &offset, NULL, NULL, NULL)) {
 		result++;
 	}