aboutsummaryrefslogtreecommitdiff
path: root/Tc.c
blob: f54a2a23ebe31268e11a13f100d39d6b2259aa19 (plain)
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
/*
 * 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 <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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;
}