[MPlayer-dev-eng] [PATCH] suboption escaping

Reimar Döffinger Reimar.Doeffinger at stud.uni-karlsruhe.de
Sat Aug 20 16:07:57 CEST 2005


Hi,
On Fri, Aug 19, 2005 at 10:09:21AM -0500, Joey Parrish wrote:
> I would accept "" parsing if you have a patch to offer.  All I care
> about is mencoder -info "title=Star Trek: The Next Generation".  If you
> can make this work (other than the %len% method, which is a pain for
> scripting) then I'll be happy.

My suggestion... it will bring the two suboption parsers a bit more in sync...
A few strings aren't freed in the error case, but IMHO that does not
matter (an it was like that also before).

Greetings,
Reimar Döffinger
-------------- next part --------------
Index: m_option.c
===================================================================
RCS file: /cvsroot/mplayer/main/m_option.c,v
retrieving revision 1.38
diff -u -r1.38 m_option.c
--- m_option.c	20 Jun 2005 23:07:34 -0000	1.38
+++ m_option.c	20 Aug 2005 14:02:30 -0000
@@ -872,7 +872,6 @@
   char *subopt;
   int nr = 0,i,r;
   m_option_t *subopts;
-  char *token;
   char *p;
   char** lst = NULL;
 
@@ -881,20 +880,54 @@
 
   subparam = malloc(strlen(param)+1);
   subopt = malloc(strlen(param)+1);
-  p = strdup(param); // In case that param is a static string (cf man strtok)
+  p = param;
 
   subopts = opt->p;
 
-  token = strtok(p, (char *)&(":"));
-  while(token)
+  while(p[0])
     {
-      int sscanf_ret;
+      int sscanf_ret = 1;
+      int optlen = strcspn(p, ":=");
       /* clear out */
       subopt[0] = subparam[0] = 0;
+      strlcpy(subopt, p, optlen);
+      p = &p[optlen];
+      if (p[0] == '=') {
+        sscanf_ret = 2;
+        p = &p[1];
+        if (p[0] == '"') {
+          p = &p[1];
+          optlen = strcspn(p, "\"");
+          strlcpy(subparam, p, optlen);
+          p = &p[optlen];
+          if (p[0] != '"') {
+            mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Terminating '\"' missing for '%s'\n", subopt);
+            return M_OPT_INVALID;
+          }
+          p = &p[1];
+        } else if (p[0] == '%') {
+          p = &p[1];
+          optlen = (int)strtol(p, &p, 0);
+          if (!p || p[0] != '%' || (optlen > strlen(p) - 1)) {
+            mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Invalid length %i for '%s'\n", optlen, subopt);
+            return M_OPT_INVALID;
+          }
+          p = &p[1];
+          strlcpy(subparam, p, optlen);
+          p = &p[optlen];
+        } else {
+          optlen = strcspn(p, ":");
+          strlcpy(subparam, p, optlen);
+          p = &p[optlen];
+        }
+      }
+      if (p[0] == ':')
+        p = &p[1];
+      else if (p[0]) {
+        mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Incorrect termination for '%s'\n", subopt);
+        return M_OPT_INVALID;
+      }
 			    
-      sscanf_ret = sscanf(token, "%[^=]=%[^:]", subopt, subparam);
-
-      mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "token: '%s', subopt='%s', subparam='%s' (ret: %d)\n", token, subopt, subparam, sscanf_ret);
       switch(sscanf_ret)
 	{
 	case 1:
@@ -918,16 +951,11 @@
 	    nr++;
 	  }
 	  break;
-	default:
-	  mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Invalid subconfig argument! ('%s')\n", token);
-	  return M_OPT_INVALID;
 	}
-      token = strtok(NULL, (char *)&(":"));
     }
 
   free(subparam);
   free(subopt);
-  free(p);
   if(dst)
     VAL(dst) = lst;
 
Index: subopt-helper.c
===================================================================
RCS file: /cvsroot/mplayer/main/subopt-helper.c,v
retrieving revision 1.7
diff -u -r1.7 subopt-helper.c
--- subopt-helper.c	16 Jun 2005 09:08:07 -0000	1.7
+++ subopt-helper.c	20 Aug 2005 14:02:31 -0000
@@ -262,6 +262,15 @@
     match = &str[len];
   }
   else
+  if (str[0] == '"') {
+    str = &str[1];
+    match = strchr(str, '"');
+    if (!match)
+      return NULL;
+    valp->len = match - str;
+    valp->str = str;
+    return &match[1];
+  }
   if ( !match )
     match = &str[strlen(str)];
 


More information about the MPlayer-dev-eng mailing list