add scratch window support
[awesome.git] / awesome-client.c
blob71cd7506e78e72f753c92bb9056dafbf7a1e5337
1 /*
2 * awesome-client.c - awesome client, communicate with socket
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2007 daniel@brinkers.de
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <errno.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
29 #include "common/awclient.h"
30 #include "common/awesome-version.h"
31 #include "common/util.h"
33 /* GNU/Hurd workaround */
34 #ifndef MSG_NOSIGNAL
35 #define MSG_NOSIGNAL 0
36 #endif
38 static int
39 send_msg(char *msg, ssize_t msg_len)
41 struct sockaddr_un *addr;
42 int csfd, ret_value = EXIT_SUCCESS;
43 csfd = get_client_socket();
44 addr = get_client_addr(getenv("DISPLAY"));
46 if(!addr || csfd < 0)
47 return EXIT_FAILURE;
49 if(sendto(csfd, msg, msg_len, MSG_NOSIGNAL,
50 (const struct sockaddr *) addr, sizeof(struct sockaddr_un)) == -1)
52 switch (errno)
54 case ENOENT:
55 warn("Can't write to %s\n", addr->sun_path);
56 break;
57 default:
58 perror("error sending datagram");
60 ret_value = errno;
63 close(csfd);
65 p_delete(&addr);
66 return ret_value;
70 /** Print help and exit(2) with given exit_code.
72 static void __attribute__ ((noreturn))
73 exit_help(int exit_code)
75 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
76 fprintf(outfile, "Usage: awesome-client [--version|--help]\n"
77 "In normal operation, give no parameters and issue commands "
78 "on standard input.\n");
79 exit(exit_code);
83 int
84 main(int argc, char *argv[])
86 char buf[1024], *msg;
87 int ret_value = EXIT_SUCCESS;
88 ssize_t len, msg_len = 1;
90 if (argc < 2)
92 /* no args to parse, nothing to do */
94 else if (argc == 2)
96 if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
97 eprint_version("awesome-client");
98 else if(!a_strcmp("-h", argv[1]) || !a_strcmp("--help", argv[1]))
99 exit_help(EXIT_SUCCESS);
101 else
103 exit_help(EXIT_SUCCESS);
106 msg = p_new(char, 1);
107 while(fgets(buf, sizeof(buf), stdin))
109 len = a_strlen(buf);
110 if (len < 2 && msg_len > 1)
112 ret_value = send_msg(msg, msg_len);
113 p_delete(&msg);
114 if (ret_value != EXIT_SUCCESS)
115 return ret_value;
116 msg = p_new(char, 1);
117 msg_len = 1;
119 else if (len > 1)
121 msg_len += len;
122 p_realloc(&msg, msg_len);
123 a_strncat(msg, msg_len, buf, len);
127 if (msg_len > 1)
128 ret_value = send_msg(msg, msg_len);
130 p_delete(&msg);
132 return ret_value;
134 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80