2 * Copyright (c) 1992, 1993 Eugene W. Stark
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Eugene W. Stark.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY EUGENE W. STARK (THE AUTHOR) ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char rcsid[] =
38 * Xten - user command interface to X-10 daemon
46 #include <sys/types.h>
47 #include <sys/socket.h>
56 char *X10housenames[] = {
57 "A", "B", "C", "D", "E", "F", "G", "H",
58 "I", "J", "K", "L", "M", "N", "O", "P",
62 char *X10cmdnames[] = {
63 "1", "2", "3", "4", "5", "6", "7", "8",
64 "9", "10", "11", "12", "13", "14", "15", "16",
65 "AllUnitsOff", "AllLightsOn", "On", "Off", "Dim", "Bright", "AllLightsOff",
66 "ExtendedCode", "HailRequest", "HailAcknowledge", "PreSetDim0", "PreSetDim1",
67 "ExtendedData", "StatusOn", "StatusOff", "StatusRequest",
71 int find __P((char *, char *[]));
72 static void usage __P((void));
79 int c, tmp, h, k, sock, error;
81 struct sockaddr_un sa;
82 char *sockpath = SOCKPATH;
83 char reply[CMDLEN], cmd[CMDLEN], *cp;
86 if(argc == 2 && !strcmp(argv[1], "-")) interactive++;
89 if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
90 errx(1, "can't create socket");
91 strcpy(sa.sun_path, sockpath);
92 sa.sun_family = AF_UNIX;
93 if(connect(sock, (struct sockaddr *)(&sa), strlen(sa.sun_path) + 2) < 0)
94 errx(1, "can't connect to X-10 daemon");
95 if((daemon = fdopen(sock, "w+")) == NULL)
96 errx(1, "can't attach stream to socket");
98 * If interactive, copy standard input to daemon and report results
102 while(!feof(stdin)) {
103 if(fgets(cmd, CMDLEN, stdin) != NULL) {
104 fprintf(daemon, "%s", cmd);
106 if(fgets(reply, CMDLEN, daemon) != NULL) {
107 fprintf(stdout, "%s", reply);
115 * Otherwise, interpret arguments and issue commands to daemon,
116 * handling retries in case of errors.
118 if((h = find(argv[1], X10housenames)) < 0)
119 errx(1, "invalid house code: %s", argv[1]);
124 if((tmp = find(cp, X10housenames)) >= 0) {
130 while(*cp != '\0' && *cp != ':') cp++;
131 if(*cp == ':') c = atoi(cp+1);
134 if((k = find(argv[0], X10cmdnames)) < 0) {
135 warnx("invalid key/unit code: %s", argv[0]);
139 while(error < RETRIES) {
140 fprintf(daemon, "send %s %s %d\n", X10housenames[h], X10cmdnames[k], c);
142 fgets(reply, CMDLEN, daemon);
143 if(strncmp(reply, "ERROR", 5)) break;
147 if(error == RETRIES) {
148 warnx("command failed: send %s %s %d",
149 X10housenames[h], X10cmdnames[k], c);
154 fprintf(daemon, "done\n");
155 fgets(reply, CMDLEN, daemon);
163 "usage: xten house key[:cnt] [[house] key[:cnt] ...]\n");
174 for(i = 0; tab[i] != NULL; i++) {
175 if(strcasecmp(s, tab[i]) == 0) return(i);