[pulseaudio-commits] r1456 - in /trunk/src/pulsecore: cli-command.c cli-command.h pdispatch.c

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Sat May 26 16:39:34 PDT 2007


Author: lennart
Date: Sun May 27 01:39:33 2007
New Revision: 1456

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=3D1456&root=3Dpulseaudio&vi=
ew=3Drev
Log:
Add a new meta command ".ifexists" to the CLI language, to execute commands=
 only if a specified file exists. Original patch from cjvdb. Closes #36

Modified:
    trunk/src/pulsecore/cli-command.c
    trunk/src/pulsecore/cli-command.h
    trunk/src/pulsecore/pdispatch.c

Modified: trunk/src/pulsecore/cli-command.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/cli-command=
.c?rev=3D1456&root=3Dpulseaudio&r1=3D1455&r2=3D1456&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/src/pulsecore/cli-command.c (original)
+++ trunk/src/pulsecore/cli-command.c Sun May 27 01:39:33 2007
@@ -31,6 +31,7 @@
 #include <assert.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <unistd.h>
 =

 #include <pulse/xmalloc.h>
 =

@@ -63,9 +64,18 @@
     unsigned args;
 };
 =

-#define INCLUDE_META ".include"
-#define FAIL_META ".fail"
-#define NOFAIL_META ".nofail"
+#define META_INCLUDE ".include"
+#define META_FAIL ".fail"
+#define META_NOFAIL ".nofail"
+#define META_IFEXISTS ".ifexists"
+#define META_ELSE ".else"
+#define META_ENDIF ".endif"
+
+enum {
+    IFSTATE_NONE =3D -1,
+    IFSTATE_FALSE =3D 0,
+    IFSTATE_TRUE =3D 1,
+};
 =

 /* Prototypes for all available commands */
 static int pa_cli_command_exit(pa_core *c, pa_tokenizer *t, pa_strbuf *buf=
, int *fail);
@@ -959,7 +969,7 @@
     return 0;
 }
 =

-int pa_cli_command_execute_line(pa_core *c, const char *s, pa_strbuf *buf,=
 int *fail) {
+int pa_cli_command_execute_line_stateful(pa_core *c, const char *s, pa_str=
buf *buf, int *fail, int *ifstate) {
     const char *cs;
 =

     cs =3D s+strspn(s, whitespace);
@@ -967,19 +977,50 @@
     if (*cs =3D=3D '#' || !*cs)
         return 0;
     else if (*cs =3D=3D '.') {
-        if (!strcmp(cs, FAIL_META))
+        if (!strcmp(cs, META_ELSE)) {
+            if (!ifstate || *ifstate =3D=3D IFSTATE_NONE) {
+                pa_strbuf_printf(buf, "Meta command %s is not valid in thi=
s context\n", cs);
+                return -1;
+            } else if (*ifstate =3D=3D IFSTATE_TRUE)
+                *ifstate =3D IFSTATE_FALSE;
+            else
+                *ifstate =3D IFSTATE_TRUE;
+            return 0;
+        } else if (!strcmp(cs, META_ENDIF)) {
+            if (!ifstate || *ifstate =3D=3D IFSTATE_NONE) {
+                pa_strbuf_printf(buf, "Meta command %s is not valid in thi=
s context\n", cs);
+                return -1;
+            } else
+                *ifstate =3D IFSTATE_NONE;
+            return 0;
+        }
+        if (ifstate && *ifstate =3D=3D IFSTATE_FALSE)
+            return 0;
+        if (!strcmp(cs, META_FAIL))
             *fail =3D 1;
-        else if (!strcmp(cs, NOFAIL_META))
+        else if (!strcmp(cs, META_NOFAIL))
             *fail =3D 0;
         else {
             size_t l;
             l =3D strcspn(cs, whitespace);
 =

-            if (l =3D=3D sizeof(INCLUDE_META)-1 && !strncmp(cs, INCLUDE_ME=
TA, l)) {
+            if (l =3D=3D sizeof(META_INCLUDE)-1 && !strncmp(cs, META_INCLU=
DE, l)) {
                 const char *filename =3D cs+l+strspn(cs+l, whitespace);
 =

                 if (pa_cli_command_execute_file(c, filename, buf, fail) < =
0)
                     if (*fail) return -1;
+            } else if (l =3D=3D sizeof(META_IFEXISTS)-1 && !strncmp(cs, ME=
TA_IFEXISTS, l)) {
+                if (!ifstate) {
+                    pa_strbuf_printf(buf, "Meta command %s is not valid in=
 this context\n", cs);
+                    return -1;
+                } else if (*ifstate !=3D IFSTATE_NONE) {
+                    pa_strbuf_printf(buf, "Nested %s commands not supporte=
d\n", cs);
+                    return -1;
+                } else {
+                    const char *filename =3D cs+l+strspn(cs+l, whitespace);
+
+                    *ifstate =3D access(filename, F_OK) =3D=3D 0 ? IFSTATE=
_TRUE : IFSTATE_FALSE;
+                }
             } else {
                 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
                 if (*fail) return -1;
@@ -990,8 +1031,12 @@
         int unknown =3D 1;
         size_t l;
 =

+        if (ifstate && *ifstate =3D=3D IFSTATE_FALSE)
+             return 0;
+        =

+
         l =3D strcspn(cs, whitespace);
-
+        =

         for (command =3D commands; command->name; command++)
             if (strlen(command->name) =3D=3D l && !strncmp(cs, command->na=
me, l)) {
                 int ret;
@@ -1017,11 +1062,19 @@
     return 0;
 }
 =

+int pa_cli_command_execute_line(pa_core *c, const char *s, pa_strbuf *buf,=
 int *fail) {
+    return pa_cli_command_execute_line_stateful(c, s, buf, fail, NULL);
+}
+
 int pa_cli_command_execute_file(pa_core *c, const char *fn, pa_strbuf *buf=
, int *fail) {
     char line[256];
     FILE *f =3D NULL;
+    int ifstate =3D IFSTATE_NONE;
     int ret =3D -1;
-    assert(c && fn && buf);
+    =

+    assert(c);
+    assert(fn);
+    assert(buf);
 =

     if (!(f =3D fopen(fn, "r"))) {
         pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, pa_cstrerror(=
errno));
@@ -1034,7 +1087,7 @@
         char *e =3D line + strcspn(line, linebreak);
         *e =3D 0;
 =

-        if (pa_cli_command_execute_line(c, line, buf, fail) < 0 && *fail)
+        if (pa_cli_command_execute_line_stateful(c, line, buf, fail, &ifst=
ate) < 0 && *fail)
             goto fail;
     }
 =

@@ -1049,14 +1102,18 @@
 =

 int pa_cli_command_execute(pa_core *c, const char *s, pa_strbuf *buf, int =
*fail) {
     const char *p;
-    assert(c && s && buf && fail);
+    int ifstate =3D IFSTATE_NONE;
+    =

+    assert(c);
+    assert(s);
+    assert(buf);
 =

     p =3D s;
     while (*p) {
         size_t l =3D strcspn(p, linebreak);
         char *line =3D pa_xstrndup(p, l);
 =

-        if (pa_cli_command_execute_line(c, line, buf, fail) < 0&& *fail) {
+        if (pa_cli_command_execute_line_stateful(c, line, buf, fail, &ifst=
ate) < 0 && *fail) {
             pa_xfree(line);
             return -1;
         }

Modified: trunk/src/pulsecore/cli-command.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/cli-command=
.h?rev=3D1456&root=3Dpulseaudio&r1=3D1455&r2=3D1456&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/src/pulsecore/cli-command.h (original)
+++ trunk/src/pulsecore/cli-command.h Sun May 27 01:39:33 2007
@@ -39,4 +39,7 @@
 /* Split the specified string into lines and run pa_cli_command_execute_li=
ne() for each. */
 int pa_cli_command_execute(pa_core *c, const char *s, pa_strbuf *buf, int =
*fail);
 =

+/* Same as pa_cli_command_execute_line() but also take ifstate var. */
+int pa_cli_command_execute_line_stateful(pa_core *c, const char *s, pa_str=
buf *buf, int *fail, int *ifstate);
+
 #endif

Modified: trunk/src/pulsecore/pdispatch.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/pdispatch.c=
?rev=3D1456&root=3Dpulseaudio&r1=3D1455&r2=3D1456&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/src/pulsecore/pdispatch.c (original)
+++ trunk/src/pulsecore/pdispatch.c Sun May 27 01:39:33 2007
@@ -258,7 +258,7 @@
     struct timeval tv;
     assert(pd && pd->ref >=3D 1 && cb);
 =

-    r =3D pa_xmalloc(sizeof(struct reply_info));
+    r =3D pa_xnew(struct reply_info, 1);
     r->pdispatch =3D pd;
     r->callback =3D cb;
     r->userdata =3D userdata;




More information about the pulseaudio-commits mailing list