1 /*
2    Low level event script handling tests
3 
4    Copyright (C) Martin Schwenke  2018
5 
6    Based on run_event_test.c:
7 
8      Copyright (C) Amitay Isaacs  2017
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #include "replace.h"
25 
26 #include <popt.h>
27 #include <talloc.h>
28 
29 #include <assert.h>
30 
31 #include "common/event_script.c"
32 
usage(const char * prog)33 static void usage(const char *prog)
34 {
35 	fprintf(stderr,
36 		"Usage: %s list <scriptdir>\n",
37 		prog);
38 	fprintf(stderr,
39 		"       %s chmod enable <scriptdir> <scriptname>\n",
40 		prog);
41 	fprintf(stderr,
42 		"       %s chmod diable <scriptdir> <scriptname>\n",
43 		prog);
44 }
45 
do_list(TALLOC_CTX * mem_ctx,int argc,const char ** argv)46 static void do_list(TALLOC_CTX *mem_ctx, int argc, const char **argv)
47 {
48 	struct event_script_list *script_list = NULL;
49 	unsigned int i;
50 	int ret;
51 
52 	if (argc != 3) {
53 		usage(argv[0]);
54 		exit(1);
55 	}
56 
57 	ret = event_script_get_list(mem_ctx, argv[2], &script_list);
58 	if (ret != 0) {
59 		printf("Script list %s failed with result=%d\n", argv[2], ret);
60 		return;
61 	}
62 
63 	if (script_list == NULL || script_list->num_scripts == 0) {
64 		printf("No scripts found\n");
65 		return;
66 	}
67 
68 	for (i=0; i < script_list->num_scripts; i++) {
69 		struct event_script *s = script_list->script[i];
70 		printf("%s\n", s->name);
71 	}
72 }
73 
do_chmod(TALLOC_CTX * mem_ctx,int argc,const char ** argv,bool enable)74 static void do_chmod(TALLOC_CTX *mem_ctx,
75 		     int argc,
76 		     const char **argv,
77 		     bool enable)
78 {
79 	int ret;
80 
81 	if (argc != 4) {
82 		usage(argv[0]);
83 		exit(1);
84 	}
85 
86 	ret = event_script_chmod(argv[2], argv[3], enable);
87 
88 	printf("Script %s %s %s completed with result=%d\n",
89 	       argv[1], argv[2], argv[3], ret);
90 }
91 
main(int argc,const char ** argv)92 int main(int argc, const char **argv)
93 {
94 	TALLOC_CTX *mem_ctx;
95 
96 	if (argc < 3) {
97 		usage(argv[0]);
98 		exit(1);
99 	}
100 
101 	mem_ctx = talloc_new(NULL);
102 	if (mem_ctx == NULL) {
103 		fprintf(stderr, "talloc_new() failed\n");
104 		exit(1);
105 	}
106 
107 	if (strcmp(argv[1], "list") == 0) {
108 		do_list(mem_ctx, argc, argv);
109 	} else if (strcmp(argv[1], "enable") == 0) {
110 		do_chmod(mem_ctx, argc, argv, true);
111 	} else if (strcmp(argv[1], "disable") == 0) {
112 		do_chmod(mem_ctx, argc, argv, false);
113 	} else {
114 		fprintf(stderr, "Invalid command %s\n", argv[2]);
115 		usage(argv[0]);
116 	}
117 
118 	talloc_free(mem_ctx);
119 	exit(0);
120 }
121