about summary refs log tree commit diff stats
path: root/tag.c
blob: 2a06bc99865db3b912884aef47e73a42584a0b5c (plain) (blame)
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
/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
 * See LICENSE file for license details.
 */
#include "dwm.h"
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <X11/Xutil.h>


typedef struct {
	const char *clpattern;
	const char *tpattern;
	Bool isfloat;
} Rule;

typedef struct {
	regex_t *clregex;
	regex_t *tregex;
} RReg;

/* static */

TAGS
RULES

static RReg *rreg = NULL;
static unsigned int len = 0;

/* extern */

Client *
getnext(Client *c) {
	for(; c && !isvisible(c); c = c->next);
	return c;
}

Client *
getprev(Client *c) {
	for(; c && !isvisible(c); c = c->prev);
	return c;
}

void
initrregs(void) {
	unsigned int i;
	regex_t *reg;

	if(rreg)
		return;
	len = sizeof rule / sizeof rule[0];
	rreg = emallocz(len * sizeof(RReg));
	for(i = 0; i < len; i++) {
		if(rule[i].clpattern) {
			reg = emallocz(sizeof(regex_t));
			if(regcomp(reg, rule[i].clpattern, REG_EXTENDED))
				free(reg);
			else
				rreg[i].clregex = reg;
		}
		if(rule[i].tpattern) {
			reg = emallocz(sizeof(regex_t));
			if(regcomp(reg, rule[i].tpattern, REG_EXTENDED))
				free(reg);
			else
				rreg[i].tregex = reg;
		}
	}
}

void
settags(Client *c, Client *trans) {
	char prop[512];
	unsigned int i, j;
	regmatch_t tmp;
	Bool matched = trans != NULL;
	XClassHint ch;

	if(matched) {
		for(i = 0; i < ntags; i++)
			c->tags[i] = trans->tags[i];
	}
	else if(XGetClassHint(dpy, c->win, &ch)) {
		snprintf(prop, sizeof prop, "%s:%s:%s",
				ch.res_class ? ch.res_class : "",
				ch.res_name ? ch.res_name : "", c->name);
		for(i = 0; i < len; i++)
			if(rreg[i].clregex && !regexec(rreg[i].clregex, prop, 1, &tmp, 0)) {
				c->isfloat = rule[i].isfloat;
				for(j = 0; rreg[i].tregex && j < ntags; j++) {
					if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
						matched = True;
						c->tags[j] = True;
					}
				}
			}
		if(ch.res_class)
			XFree(ch.res_class);
		if(ch.res_name)
			XFree(ch.res_name);
	}
	if(!matched)
		for(i = 0; i < ntags; i++)
			c->tags[i] = seltag[i];
}

void
tag(Arg *arg) {
	unsigned int i;

	if(!sel)
		return;
	for(i = 0; i < ntags; i++)
		sel->tags[i] = (arg->i == -1) ? True : False;
	if(arg->i >= 0 && arg->i < ntags)
		sel->tags[arg->i] = True;
	arrange();
}

void
toggletag(Arg *arg) {
	unsigned int i;

	if(!sel)
		return;
	sel->tags[arg->i] = !sel->tags[arg->i];
	for(i = 0; i < ntags && !sel->tags[i]; i++);
	if(i == ntags)
		sel->tags[arg->i] = True;
	arrange();
}
t;fd, reinterpret_cast<sockaddr*>(&result->addr), sizeof(result->addr)) < 0) { close(result->fd); result->fd = -1; raise << "Failed to connect to " << host << ':' << port << '\n' << end(); } return result; } :(before "End Primitive Recipe Declarations") _OPEN_SERVER_SOCKET, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$open-server-socket", _OPEN_SERVER_SOCKET); :(before "End Primitive Recipe Checks") case _OPEN_SERVER_SOCKET: { if (SIZE(inst.ingredients) != 1) { raise << maybe(get(Recipe, r).name) << "'$open-server-socket' requires exactly one ingredient (the port to listen for requests on), but got '" << inst.original_string << "'\n" << end(); break; } if (!is_mu_number(inst.ingredients.at(0))) { raise << maybe(get(Recipe, r).name) << "first ingredient of '$open-server-socket' should be a number, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end(); break; } if (SIZE(inst.products) != 1) { raise << maybe(get(Recipe, r).name) << "'$open-server-socket' requires exactly one product, but got '" << inst.original_string << "'\n" << end(); break; } if (!is_mu_number(inst.products.at(0))) { raise << maybe(get(Recipe, r).name) << "first product of '$open-server-socket' should be a number (file handle), but got '" << to_string(inst.products.at(0)) << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case _OPEN_SERVER_SOCKET: { int port = ingredients.at(0).at(0); socket_t* server = server_socket(port); products.resize(1); if (server->fd < 0) { delete server; products.at(0).push_back(0); break; } long long int result = reinterpret_cast<long long int>(server); products.at(0).push_back(static_cast<double>(result)); break; } :(code) socket_t* server_socket(int port) { socket_t* result = new socket_t; result->fd = socket(AF_INET, SOCK_STREAM, 0); if (result->fd < 0) { raise << "Failed to create server socket.\n" << end(); return result; } int dummy = 0; setsockopt(result->fd, SOL_SOCKET, SO_REUSEADDR, &dummy, sizeof(dummy)); result->addr.sin_family = AF_INET; result->addr.sin_addr.s_addr = INADDR_ANY; result->addr.sin_port = htons(port); if (bind(result->fd, reinterpret_cast<sockaddr*>(&result->addr), sizeof(result->addr)) >= 0) { listen(result->fd, /*queue length*/5); } else { close(result->fd); result->fd = -1; raise << "Failed to bind result socket to port " << port << ". Something's already using that port.\n" << end(); } return result; } :(before "End Primitive Recipe Declarations") _ACCEPT, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$accept", _ACCEPT); :(before "End Primitive Recipe Checks") case _ACCEPT: { if (SIZE(inst.ingredients) != 1) { raise << maybe(get(Recipe, r).name) << "'$accept' requires exactly one ingredient, but got '" << inst.original_string << "'\n" << end(); break; } if (!is_mu_number(inst.ingredients.at(0))) { raise << maybe(get(Recipe, r).name) << "first ingredient of '$accept' should be a number, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end(); break; } if (SIZE(inst.products) != 1) { raise << maybe(get(Recipe, r).name) << "'$accept' requires exactly one product, but got '" << inst.original_string << "'\n" << end(); break; } if (!is_mu_number(inst.products.at(0))) { raise << maybe(get(Recipe, r).name) << "first product of '$accept' should be a number (file handle), but got '" << to_string(inst.products.at(0)) << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case _ACCEPT: { products.resize(2); products.at(1).push_back(ingredients.at(0).at(0)); // indicate it modifies its ingredient long long int x = static_cast<long long int>(ingredients.at(0).at(0)); socket_t* server = reinterpret_cast<socket_t*>(x); if (server) { socket_t* session = accept_session(server); long long int result = reinterpret_cast<long long int>(session); products.at(0).push_back(static_cast<double>(result)); } else { products.at(0).push_back(0); } break; } :(code) socket_t* accept_session(socket_t* server) { if (server->fd == 0) return NULL; socket_t* result = new socket_t; socklen_t dummy = sizeof(result->addr); result->fd = accept(server->fd, reinterpret_cast<sockaddr*>(&result->addr), &dummy); return result; } :(before "End Primitive Recipe Declarations") _READ_FROM_SOCKET, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$read-from-socket", _READ_FROM_SOCKET); :(before "End Primitive Recipe Checks") case _READ_FROM_SOCKET: { if (SIZE(inst.ingredients) != 2) { raise << maybe(get(Recipe, r).name) << "'$read-from-socket' requires exactly two ingredients, but got '" << inst.original_string << "'\n" << end(); break; } if (!is_mu_number(inst.ingredients.at(0))) { raise << maybe(get(Recipe, r).name) << "first ingredient of '$read-from-socket' should be a number, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end(); break; } if (!is_mu_number(inst.ingredients.at(1))) { raise << maybe(get(Recipe, r).name) << "second ingredient of '$read-from-socket' should be a number, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end(); break; } if (SIZE(inst.products) != 2) { raise << maybe(get(Recipe, r).name) << "'$read-from-socket' requires exactly two product, but got '" << inst.original_string << "'\n" << end(); break; } if (!is_mu_text(inst.products.at(0))) { raise << maybe(get(Recipe, r).name) << "first product of '$read-from-socket' should be a text (address array character), but got '" << to_string(inst.products.at(0)) << "'\n" << end(); break; } if (!is_mu_boolean(inst.products.at(1))) { raise << maybe(get(Recipe, r).name) << "second product of '$read-from-socket' should be a boolean (eof?), but got '" << to_string(inst.products.at(1)) << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case _READ_FROM_SOCKET: { long long int x = static_cast<long long int>(ingredients.at(0).at(0)); socket_t* socket = reinterpret_cast<socket_t*>(x); // 1. we'd like to simply read() from the socket // however read() on a socket never returns EOF, so we wouldn't know when to stop // 2. recv() can signal EOF, but it also signals "no data yet" in the beginning // so use poll() in the beginning to wait for data before calling recv() // 3. but poll() will block on EOF, so only use poll() on the very first // $read-from-socket on a socket if (!socket->polled) { socket->polled = true; pollfd p; bzero(&p, sizeof(p)); p.fd = socket->fd; p.events = POLLIN | POLLHUP; if (poll(&p, /*num pollfds*/1, /*no timeout*/-1) <= 0) { raise << maybe(current_recipe_name()) << "error in $read-from-socket\n" << end(); products.resize(2); products.at(0).push_back(0); products.at(1).push_back(false); break; } } int bytes = static_cast<int>(ingredients.at(1).at(0)); char* contents = new char[bytes]; bzero(contents, bytes); int bytes_read = recv(socket->fd, contents, bytes-/*terminal null*/1, MSG_DONTWAIT); products.resize(2); products.at(0).push_back(new_mu_text(contents)); products.at(1).push_back(bytes_read <= 0); delete contents; break; } :(before "End Primitive Recipe Declarations") _WRITE_TO_SOCKET, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$write-to-socket", _WRITE_TO_SOCKET); :(before "End Primitive Recipe Checks") case _WRITE_TO_SOCKET: { if (SIZE(inst.ingredients) != 2) { raise << maybe(get(Recipe, r).name) << "'$write-to-socket' requires exactly two ingredient, but got '" << inst.original_string << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case _WRITE_TO_SOCKET: { long long int x = static_cast<long long int>(ingredients.at(0).at(0)); socket_t* session = reinterpret_cast<socket_t*>(x); // write just one character at a time to the session socket long long int y = static_cast<long long int>(ingredients.at(1).at(0)); char c = static_cast<char>(y); if (write(session->fd, &c, 1) != 1) { raise << maybe(current_recipe_name()) << "failed to write to socket\n" << end(); exit(0); } long long int result = reinterpret_cast<long long int>(session); products.resize(1); products.at(0).push_back(result); break; } :(before "End Primitive Recipe Declarations") _CLOSE_SOCKET, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$close-socket", _CLOSE_SOCKET); :(before "End Primitive Recipe Checks") case _CLOSE_SOCKET: { if (SIZE(inst.ingredients) != 1) { raise << maybe(get(Recipe, r).name) << "'$close-socket' requires exactly two ingredient, but got '" << inst.original_string << "'\n" << end(); break; } if (!is_mu_number(inst.ingredients.at(0))) { raise << maybe(get(Recipe, r).name) << "first ingredient of '$close-socket' should be a character, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case _CLOSE_SOCKET: { long long int x = static_cast<long long int>(ingredients.at(0).at(0)); socket_t* socket = reinterpret_cast<socket_t*>(x); close(socket->fd); break; } :(before "End Includes") #include <netinet/in.h> #include <netdb.h> #include <poll.h> #include <sys/socket.h> #include <unistd.h>