From a605c88ba3a0180d5783722c2c877f2ab8b01659 Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Wed, 1 Apr 2015 22:43:44 -0300 Subject: Rewritten as a client-server application, similar to urxvt{d,c}. --- Tc.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Tc.c (limited to 'Tc.c') diff --git a/Tc.c b/Tc.c new file mode 100644 index 0000000..f54a2a2 --- /dev/null +++ b/Tc.c @@ -0,0 +1,100 @@ +/* + * Tc.c + * T client. Merely issues commands to the T daemon. + * + * T is a lean Terminal emulator. + * + * This file is part of T. + * + * T is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * T is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * T. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "T.h" + +#define ARG_NEWWIN "-n" +#define ARG_EXIT "-x" + +static int open_conn(); +static TMessage parse_args(int argc, char *argv[]); +static void usage(); + +static int +open_conn() +{ + struct sockaddr_un sock_addr; + + int sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock_fd < 0) + err("Failed to create socket", EXIT_FAILURE); + + memset(&sock_addr, 0, sizeof(sock_addr)); + sock_addr.sun_family = AF_UNIX; + strncpy(sock_addr.sun_path, SOCKPATH, sizeof(sock_addr.sun_path) - 1); + + if (connect(sock_fd, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) < 0) + err("Failed to connect to daemon", EXIT_FAILURE); + + return sock_fd; +} + +static TMessage +parse_args(int argc, char *argv[]) +{ + if (argc == 1) /* No args means new window */ + return MSG_NEWWIN; + if (argc > 2) /* More than 1 arg means derp */ + return MSG_ERROR; + if (strcmp(argv[1], ARG_NEWWIN) == 0) + return MSG_NEWWIN; + if (strcmp(argv[1], ARG_EXIT) == 0) + return MSG_EXIT; + + return MSG_ERROR; +} + +static void +usage() +{ + puts("usage:"); + puts("\tTc [ARG]"); + puts("\nwhere ARG is one of:"); + puts("\t" ARG_NEWWIN "\tRequests a new window; default if ommited"); + puts("\t" ARG_EXIT "\tRequests daemon termination; only successful if there are no open windows"); +} + +int +main(int argc, char *argv[]) +{ + TMessage msg = parse_args(argc, argv); + if (msg == MSG_ERROR) { + usage(); + return EXIT_FAILURE; + } + + int sock_fd = open_conn(); + if (write(sock_fd, &msg, sizeof(msg)) < 0) + err("Failed to send data", EXIT_FAILURE); + if (recv(sock_fd, &msg, sizeof(msg), 0) < 0) + err("Failed to recv data", EXIT_FAILURE); + close(sock_fd); + + return msg == MSG_OK ? EXIT_SUCCESS : EXIT_FAILURE; +} -- cgit v1.2.3