xembed: make xembed_info_get() asynchronous
[awesome.git] / awesome-client.c
blobee6e163bccf133b589cec49795b129643401cd3e
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 #define _GNU_SOURCE
25 #include <errno.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
31 #include <stdio.h>
32 #include <readline/readline.h>
33 #include <readline/history.h>
35 #include "common/socket.h"
36 #include "common/version.h"
37 #include "common/util.h"
39 /* GNU/Hurd workaround */
40 #ifndef MSG_NOSIGNAL
41 #define MSG_NOSIGNAL 0
42 #endif
44 /** Send a message to awesome.
45 * \param msg The message.
46 * \param msg_len The message length.
47 * \return The errno of sendto().
49 static int
50 send_msg(const char *msg, ssize_t msg_len)
52 struct sockaddr_un *addr;
53 int csfd, ret_value = EXIT_SUCCESS;
54 csfd = socket_getclient();
55 addr = socket_getaddr(getenv("DISPLAY"));
57 if(!addr || csfd < 0)
58 return EXIT_FAILURE;
60 if(sendto(csfd, msg, msg_len, MSG_NOSIGNAL,
61 (const struct sockaddr *) addr, sizeof(struct sockaddr_un)) == -1)
63 switch (errno)
65 case ENOENT:
66 warn("can't write to %s", addr->sun_path);
67 break;
68 default:
69 warn("error sending datagram: %s", strerror(errno));
71 ret_value = errno;
74 close(csfd);
76 p_delete(&addr);
77 return ret_value;
81 /** Print help and exit(2) with given exit_code.
82 * \param exit_code The exit code.
83 * \return Never return.
85 static void __attribute__ ((noreturn))
86 exit_help(int exit_code)
88 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
89 fprintf(outfile, "Usage: awesome-client [--version|--help]\n"
90 "In normal operation, give no parameters and issue commands "
91 "on standard input.\n");
92 exit(exit_code);
95 /** Main function of awesome-client.
96 * \param argc Number of args.
97 * \param argv Args array.
98 * \return Value returned by send_msg().
101 main(int argc, char **argv)
103 char buf[1024], *msg, *prompt;
104 int ret_value = EXIT_SUCCESS;
105 ssize_t len, msg_len = 1;
107 if(argc == 2)
109 if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
110 eprint_version("awesome-client");
111 else if(!a_strcmp("-h", argv[1]) || !a_strcmp("--help", argv[1]))
112 exit_help(EXIT_SUCCESS);
114 else if(argc > 2)
115 exit_help(EXIT_SUCCESS);
117 if(isatty(STDIN_FILENO))
119 asprintf(&prompt, "awesome@%s%% ", getenv("DISPLAY"));
120 while((msg = readline(prompt)))
121 if((msg_len = a_strlen(msg)))
123 add_history (msg);
124 p_realloc(&msg, msg_len + 2);
125 msg[msg_len] = '\n';
126 msg[msg_len + 1] = '\0';
127 send_msg(msg, msg_len + 2);
130 else
132 msg = p_new(char, 1);
133 while(fgets(buf, sizeof(buf), stdin))
135 len = a_strlen(buf);
136 if(len < 2 && msg_len > 1)
138 ret_value = send_msg(msg, msg_len);
139 p_delete(&msg);
140 if (ret_value != EXIT_SUCCESS)
141 return ret_value;
142 msg = p_new(char, 1);
143 msg_len = 1;
145 else if (len > 1)
147 msg_len += len;
148 p_realloc(&msg, msg_len);
149 a_strncat(msg, msg_len, buf, len);
152 if(msg_len > 1)
153 ret_value = send_msg(msg, msg_len);
154 p_delete(&msg);
157 return ret_value;
160 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80