jswm-con 0.2.0

Rust control interface to Jswm.
Documentation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <err.h>
#include <getopt.h>

#include <libipcon.h>

#include "libjswm.h"

#define JSWM_IPCON	"Jswm"

IPCON_HANDLER	handler = NULL ;

static int ipcon_init()
{
	int ret = -1;

	do {

		handler = ipcon_create_handler(NULL,
				LIBIPCON_FLG_USE_SND_IF | LIBIPCON_FLG_USE_RCV_IF);
		if (!handler) {
			fprintf(stderr, "Failed to create handler\n");
			break;
		}

		ret = 0;

	} while (0);

	return ret;
}

int main(int argc, char *argv[])
{
	int ret = -1;

	char *value = NULL;
	int c;
	int x = 0, y = 0, w = 0, h = 0;
	char *role = NULL;
	int index;

	opterr = 0;

	while ((c = getopt(argc, argv, "x:y:w:h:")) != -1) {
		switch(c) {
			case 'x':
				fprintf(stderr, "%s\n", optarg);
				value = optarg;
				errno = 0;
				x = strtol(value, NULL, 10);
				if (errno < 0) 
					errx(1, "Invalid x value");
				break;
			case 'y':
				fprintf(stderr, "%s\n", optarg);
				value = optarg;
				errno = 0;
				y = strtol(value, NULL, 10);
				if (errno < 0) 
					errx(1, "Invalid y value");
				break;
			case 'w':
				fprintf(stderr, "%s\n", optarg);
				value = optarg;
				errno = 0;
				w = strtol(value, NULL, 10);
				if (errno < 0 || w < 0) 
					errx(1, "Invalid w value");
				break;
			case 'h':
				fprintf(stderr, "%s\n", optarg);
				value = optarg;
				errno = 0;
				h = strtol(value, NULL, 10);
				if (errno < 0 || w < 0) 
					errx(1, "Invalid w value");
				break;
			default:
				errx(1, "Invalid option %s", optarg); 
		}
	}

	if (optind < argc) {
		role = argv[optind];
	}

	if (!role)
		errx(1, "Invalid role.");

	fprintf(stderr, "role: %s x: %d, y: %d, w: %d, h:%d\n", role, x, y, w, h);

	do {
		char *msg = NULL;

		if (ipcon_init() < 0) 
			break;

		msg = libjswm_cmdSetRolePos(role, x, y, w, h);
		if (!msg) 
			break;

		fprintf(stderr, "msg: %s\n", msg);

		ret = ipcon_send_unicast(handler,
			JSWM_IPCON,
			msg,
			strlen(msg) + 1);

		free(msg);

	} while (0);

	/* handler maybe NULL */
	ipcon_free_handler(handler);

	if (!ret)
		exit(1);

	exit(0);
}