+/* quote a string and escape original quotes */
+int cmdline_quote_token(char *dst, unsigned dstlen, const char *src)
+{
+ unsigned s = 0, d = 0;
+
+ /* the 2 quotes + '\0' */
+ if (dstlen < 3)
+ return -EMSGSIZE;
+
+ dst[d++] = '"';
+ while (src[s] != '\0') {
+ if (d >= (dstlen-2))
+ return -EMSGSIZE;
+
+ if (src[s] == '"')
+ dst[d++] = '\\';
+ if (src[s] == '\\' && src[s+1] == '"')
+ dst[d++] = '\\';
+
+ dst[d++] = src[s++];
+ }
+
+ if (d >= (dstlen-2))
+ return -EMSGSIZE;
+ dst[d++] = '"';
+ dst[d++] = '\0';
+ return s;
+}
+
+/* remove quote and stop when we reach the end of token */
+// XXX ret val comment
+int cmdline_unquote_token(char *dst, unsigned dstlen,
+ const char *src)
+{
+ unsigned s = 0, d = 0;
+ int quoted = 0;
+
+ while (src[s] != '\0') {
+ if (d >= dstlen)
+ return -EMSGSIZE;
+
+ if (cmdline_isendoftoken(src[s]) && quoted == 0)
+ break;
+
+ if (src[s] == '\\' && src[s+1] == '"') {
+ dst[d++] = '"';
+ s += 2;
+ continue;
+ }
+ if (src[s] == '\\' && src[s+1] == '\\') {
+ dst[d++] = '\\';
+ s += 2;
+ continue;
+ }
+ if (src[s] == '"') {
+ s++;
+ quoted = !quoted;
+ continue;
+ }
+ dst[d++] = src[s++];
+ }
+
+ if (quoted)
+ return -EINVAL;
+
+ if (d >= (dstlen-1))
+ return -EMSGSIZE;
+ dst[d++] = '\0';
+ return s;
+}
+