Ticket #36: 02_ifexists_else_endif.dpatch

File 02_ifexists_else_endif.dpatch, 4.3 KB (added by cjvdb, 6 years ago)
Line 
1#! /bin/sh /usr/share/dpatch/dpatch-run
2## 02_ifexists_else_endif.dpatch by CJ van den Berg <cj@vdbonline.com>
3##
4## All lines beginning with `## DP:' are a description of the patch.
5## DP: add support for .ifexists, .else and .endif meta commands
6
7@DPATCH@
8
9diff -Nru pulseaudio-0.9.5-orig/src/pulsecore/cli-command.c pulseaudio-0.9.5/src/pulsecore/cli-command.c
10--- pulseaudio-0.9.5-orig/src/pulsecore/cli-command.c   2006-08-19 18:25:38.000000000 +0200
11+++ pulseaudio-0.9.5/src/pulsecore/cli-command.c    2006-10-06 02:08:00.446876784 +0200
12@@ -28,6 +28,7 @@
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <errno.h>
16+#include <unistd.h>
17 
18 #include <pulse/xmalloc.h>
19 
20@@ -63,6 +64,13 @@
21 #define INCLUDE_META ".include"
22 #define FAIL_META ".fail"
23 #define NOFAIL_META ".nofail"
24+#define IFEXISTS_META ".ifexists"
25+#define ELSE_META ".else"
26+#define ENDIF_META ".endif"
27+
28+#define IFSTATE_NONE 0
29+#define IFSTATE_TRUE 1
30+#define IFSTATE_FALSE 2
31 
32 /* Prototypes for all available commands */
33 static int pa_cli_command_exit(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
34@@ -953,7 +961,7 @@
35     return 0;
36 }
37 
38-int pa_cli_command_execute_line(pa_core *c, const char *s, pa_strbuf *buf, int *fail) {
39+int pa_cli_command_execute_line_stateful(pa_core *c, const char *s, pa_strbuf *buf, int *fail, int *ifstate) {
40     const char *cs;
41     
42     cs = s+strspn(s, whitespace);
43@@ -961,6 +969,25 @@
44     if (*cs == '#' || !*cs)
45         return 0;
46     else if (*cs == '.') {
47+        if (!strcmp(cs, ELSE_META)) {
48+            if (!ifstate || *ifstate == IFSTATE_NONE) {
49+                pa_strbuf_printf(buf, "Meta command %s is not valid in this context\n", cs);
50+                return -1;
51+            } else if (*ifstate == IFSTATE_TRUE)
52+                *ifstate = IFSTATE_FALSE;
53+            else
54+                *ifstate = IFSTATE_TRUE;
55+            return 0;
56+        } else if (!strcmp(cs, ENDIF_META)) {
57+            if (!ifstate || *ifstate == IFSTATE_NONE) {
58+                pa_strbuf_printf(buf, "Meta command %s is not valid in this context\n", cs);
59+                return -1;
60+            } else
61+                *ifstate = IFSTATE_NONE;
62+            return 0;
63+        }
64+        if (ifstate && *ifstate == IFSTATE_FALSE)
65+            return 0;
66         if (!strcmp(cs, FAIL_META))
67             *fail = 1;
68         else if (!strcmp(cs, NOFAIL_META))
69@@ -974,6 +1001,21 @@
70 
71                 if (pa_cli_command_execute_file(c, filename, buf, fail) < 0)
72                     if (*fail) return -1;
73+            } else if (l == sizeof(IFEXISTS_META)-1 && !strncmp(cs, IFEXISTS_META, l)) {
74+                if (!ifstate) {
75+                    pa_strbuf_printf(buf, "Meta command %s is not valid in this context\n", cs);
76+                    return -1;
77+                } else if (*ifstate != IFSTATE_NONE) {
78+                    pa_strbuf_printf(buf, "Nested %s commands not supported\n", cs);
79+                    return -1;
80+                } else {
81+                    const char *filename = cs+l+strspn(cs+l, whitespace);
82+
83+                    if (access(filename, F_OK) == 0)
84+                        *ifstate = IFSTATE_TRUE;
85+                    else
86+                        *ifstate = IFSTATE_FALSE;
87+                }
88             } else {
89                 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
90                 if (*fail) return -1;
91@@ -984,6 +1026,9 @@
92         int unknown = 1;
93         size_t l;
94         
95+        if (ifstate && *ifstate == IFSTATE_FALSE)
96+            return 0;
97+
98         l = strcspn(cs, whitespace);
99 
100         for (command = commands; command->name; command++)
101@@ -1011,9 +1056,14 @@
102     return 0;
103 }
104 
105+int pa_cli_command_execute_line(pa_core *c, const char *s, pa_strbuf *buf, int *fail) {
106+    return pa_cli_command_execute_line_stateful(c, s, buf, fail, NULL);
107+}
108+
109 int pa_cli_command_execute_file(pa_core *c, const char *fn, pa_strbuf *buf, int *fail) {
110     char line[256];
111     FILE *f = NULL;
112+    int ifstate = IFSTATE_NONE;
113     int ret = -1;
114     assert(c && fn && buf);
115 
116@@ -1028,7 +1078,7 @@
117         char *e = line + strcspn(line, linebreak);
118         *e = 0;
119 
120-        if (pa_cli_command_execute_line(c, line, buf, fail) < 0 && *fail)
121+        if (pa_cli_command_execute_line_stateful(c, line, buf, fail, &ifstate) < 0 && *fail)
122             goto fail;
123     }
124