diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-11-26 14:31:29 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-11-26 14:31:29 -0500 |
commit | 9a15371ba3ab8497ac393d18baeb2f953a3b2762 (patch) | |
tree | f2f797205fd8816af277c3178204dc355cfd4a74 /swaymsg/main.c | |
parent | Add swaymsg subproject (diff) | |
download | sway-9a15371ba3ab8497ac393d18baeb2f953a3b2762.tar.gz sway-9a15371ba3ab8497ac393d18baeb2f953a3b2762.tar.zst sway-9a15371ba3ab8497ac393d18baeb2f953a3b2762.zip |
Parse command line args for swaymsg
Diffstat (limited to 'swaymsg/main.c')
-rw-r--r-- | swaymsg/main.c | 94 |
1 files changed, 92 insertions, 2 deletions
diff --git a/swaymsg/main.c b/swaymsg/main.c index 824af0e6..7c643758 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c | |||
@@ -1,11 +1,101 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include <string.h> | ||
4 | #include <getopt.h> | ||
5 | #include <stdint.h> | ||
6 | #include "stringop.h" | ||
7 | #include "ipc.h" | ||
8 | #include "readline.h" | ||
9 | #include "log.h" | ||
3 | 10 | ||
4 | void sway_terminate(void) { | 11 | void sway_terminate(void) { |
5 | exit(1); | 12 | exit(1); |
6 | } | 13 | } |
7 | 14 | ||
8 | int main(int argc, const char **argv) { | 15 | char *get_socketpath(void) { |
9 | printf("Hello world!"); | 16 | FILE *fp = popen("sway --get-socketpath", "r"); |
17 | if (!fp) { | ||
18 | return NULL; | ||
19 | } | ||
20 | char *line = read_line(fp); | ||
21 | pclose(fp); | ||
22 | return line; | ||
23 | } | ||
24 | |||
25 | int main(int argc, char **argv) { | ||
26 | static int quiet = 0; | ||
27 | char *socket_path = NULL; | ||
28 | char *cmdtype = NULL; | ||
29 | |||
30 | static struct option long_options[] = { | ||
31 | {"quiet", no_argument, &quiet, 'q'}, | ||
32 | {"version", no_argument, NULL, 'v'}, | ||
33 | {"socket", required_argument, NULL, 's'}, | ||
34 | {"type", required_argument, NULL, 't'}, | ||
35 | {0, 0, 0, 0} | ||
36 | }; | ||
37 | |||
38 | int c; | ||
39 | while (1) { | ||
40 | int option_index = 0; | ||
41 | c = getopt_long(argc, argv, "qvs:t:", long_options, &option_index); | ||
42 | if (c == -1) { | ||
43 | break; | ||
44 | } | ||
45 | switch (c) { | ||
46 | case 0: // Flag | ||
47 | break; | ||
48 | case 's': // Socket | ||
49 | socket_path = strdup(optarg); | ||
50 | break; | ||
51 | case 't': // Type | ||
52 | cmdtype = strdup(optarg); | ||
53 | break; | ||
54 | case 'v': | ||
55 | #if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE | ||
56 | fprintf(stdout, "sway version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH); | ||
57 | #else | ||
58 | fprintf(stdout, "version not detected\n"); | ||
59 | #endif | ||
60 | exit(0); | ||
61 | break; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | if (!cmdtype) { | ||
66 | cmdtype = "command"; | ||
67 | } | ||
68 | if (!socket_path) { | ||
69 | socket_path = get_socketpath(); | ||
70 | if (!socket_path) { | ||
71 | sway_abort("Unable to retrieve socket path"); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | uint32_t type; | ||
76 | |||
77 | if (strcasecmp(cmdtype, "command") == 0) { | ||
78 | type = IPC_COMMAND; | ||
79 | } else if (strcasecmp(cmdtype, "get_workspaces") == 0) { | ||
80 | type = IPC_GET_WORKSPACES; | ||
81 | } else if (strcasecmp(cmdtype, "get_outputs") == 0) { | ||
82 | type = IPC_GET_OUTPUTS; | ||
83 | } else if (strcasecmp(cmdtype, "get_tree") == 0) { | ||
84 | type = IPC_GET_TREE; | ||
85 | } else if (strcasecmp(cmdtype, "get_marks") == 0) { | ||
86 | type = IPC_GET_MARKS; | ||
87 | } else if (strcasecmp(cmdtype, "get_bar_config") == 0) { | ||
88 | type = IPC_GET_BAR_CONFIG; | ||
89 | } else if (strcasecmp(cmdtype, "get_version") == 0) { | ||
90 | type = IPC_GET_VERSION; | ||
91 | } | ||
92 | |||
93 | char *command = strdup(""); | ||
94 | if (optind < argc) { | ||
95 | command = join_args(argv + optind, argc - optind); | ||
96 | } | ||
97 | |||
98 | printf("%s", command); | ||
99 | free(socket_path); | ||
10 | return 0; | 100 | return 0; |
11 | } | 101 | } |