File: wmiir.c

package info (click to toggle)
wmii2 2.5.2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 592 kB
  • ctags: 927
  • sloc: ansic: 9,195; makefile: 228; sh: 136
file content (228 lines) | stat: -rw-r--r-- 5,222 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
 * See LICENSE file for license details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ixp.h"

static IXPClient *c;
static int      exit_code = 0;

static char    *version[] = {
	"wmiir - window manager improved remote - " VERSION "\n"
	" (C)opyright MMIV-MMV Anselm R. Garbe\n", 0
};

static void
usage()
{
	fprintf(stderr, "%s",
	 "usage: wmiir [-s <socket file>] [-v] <action> <action_arg> [...]\n"
		"      -s    socket file (default: $WMIIR_SOCKET)\n"
		"      -f    read actions from stdin\n"
		"      -v    version info\n"
		"actions:\n"
		"      create <file> [<content>] -- creates and optionally writes content to a file\n"
	     "      write  <file> <content>   -- writes content to a file\n"
		"      read   <directory/file>   -- reads file or directory contents\n"
		"      remove <directory/file>   -- removes file or directory, use with care!\n");
	exit(1);
}

static void
perform(char *action, char *file, char *content)
{
	size_t          out_len = 0;
	char            output[2050];
	int             crt, fd = -1;

	if (!action)
		return;

	crt = !strncmp(action, "create", 7);
	if (!strncmp(action, "write", 6) || crt) {
		if (!file)
			return;
		/* create file first */
		if (crt) {
			c->create(c, file);
			if (c->errstr) {
				fprintf(stderr, "wmiir: error: create %s: %s\n", file,
					c->errstr);
				exit_code = 1;
				return;
			}
		}
		if (!content)
			return;
		fd = c->open(c, file);
		if (c->errstr) {
			fprintf(stderr, "wmiir: error: open %s: %s\n", file, c->errstr);
			exit_code = 1;
			return;
		}
		c->write(c, fd, content, strlen(content));
		if (c->errstr) {
			fprintf(stderr, "wmiir: error: write %s: %s\n", file,
				c->errstr);
			exit_code = 1;
			if (!strncmp(c->errstr, DEAD_SERVER, strlen(DEAD_SERVER) + 1))
				return;
		}
	} else if (!strncmp(action, "read", 5)) {
		if (!file)
			return;
		fd = c->open(c, file);
		if (c->errstr) {
			fprintf(stderr, "wmiir: error: open %s: %s\n", file, c->errstr);
			exit_code = 1;
			return;
		}
		do {
			out_len = c->read(c, fd, output, 2048);
			if (c->errstr) {
				fprintf(stderr, "wmiir: error: read %s: %s\n", file,
					c->errstr);
				exit_code = 1;
				if (!strncmp
				    (c->errstr, DEAD_SERVER, strlen(DEAD_SERVER) + 1))
					return;
				break;
			}
			output[out_len] = '\0';
			fprintf(stdout, "%s", output);
		}
		while (out_len == 2048);
		fprintf(stdout, "%s", "\n");
	} else if (!strncmp(action, "remove", 7)) {
		if (!file)
			return;
		c->remove(c, file);
		if (c->errstr) {
			fprintf(stderr, "wmiir: error: remove %s: %s\n", file,
				c->errstr);
			exit_code = 1;
			return;
		}
	}
	if (fd != -1) {
		c->close(c, fd);
		if (c->errstr) {
			fprintf(stderr, "wmiir: error: close %s: %s\n", file,
				c->errstr);
			exit_code = 1;
			return;
		}
	}
}

int
main(int argc, char *argv[])
{
	int             i = 0, read_stdin = 0;
	char            line[4096], *p;
	char           *sockfile = getenv("WMIIR_SOCKET");

	/* command line args */
	if (argc > 1) {
		for (i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
			switch (argv[i][1]) {
			case 'v':
				fprintf(stderr, "%s", version[0]);
				exit(0);
				break;
			case 's':
				if (i + 1 < argc) {
					sockfile = argv[++i];
				} else {
					usage();
				}
				break;
			case 'f':
				read_stdin = 1;
				break;
			default:
				usage();
				break;
			}
		}
	}
	if ((argc <= 1) || (!read_stdin && (i + 1) >= argc)) {
		fprintf(stderr, "%s", "wmiir: arguments: ");
		for (i = 1; i < argc; i++)
			fprintf(stderr, "%s, ", argv[i]);
		fprintf(stderr, "%s", "\n");
		usage();
	}
	if (!sockfile) {
		fprintf(stderr, "%s",
			"wmiir: error: WMIIR_SOCKET environment not set\n");
		usage();
	}
	/* open socket */
	if (!(c = init_client(sockfile))) {
		fprintf(stderr, "wmiir: cannot connect to server '%s'\n", sockfile);
		exit(1);
	}
	if (read_stdin) {
		/* simple shell */
		char           *action, *file, *content;
		while (fgets(line, 4096, stdin)) {
			p = line;
			while (*p != '\0' && (*p == ' ' || *p == '\t'))
				p++;
			if (*p == '\0')
				continue;	/* empty line */
			if (strncmp(p, "create ", 7) &&
			    strncmp(p, "write ", 6) &&
			 strncmp(p, "read ", 5) && strncmp(p, "remove ", 7))
				continue;

			action = p;
			while (*p != '\0' && *p != ' ' && *p != '\t' && *p != '\n')
				p++;
			if (*p == '\0' || *p == '\n')
				continue;	/* ignore bogus command */
			*p = '\0';
			p++;
			while (*p != '\0' && (*p == ' ' || *p == '\t'))
				p++;
			if (*p == '\0')
				continue;	/* ignore bogus command */
			file = p;
			while (*p != '\0' && *p != ' ' && *p != '\t' && *p != '\n')
				p++;
			if (*p == '\0' || *p == '\n') {
				content = 0;
				*p = '\0';
			} else {
				*p = '\0';
				p++;
				content = p;
			}
			if (file[0] == '\0')
				continue;
			if (content) {
				static size_t   len;
				if ((len = strlen(content)))
					content[len - 1] = '\0';
			}
			perform(action, file, content);
			if (c->errstr)
				fprintf(stderr, "wmiir: error: read %s: %s\n", file,
					c->errstr);
		}
	} else {
		perform(argv[i], argv[i + 1], (i + 2) < argc ? argv[i + 2] : 0);
	}

	if (c->errstr) {
		deinit_client(c);
		exit_code = 1;
	}
	return exit_code;
}