]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/xten/xten.c
This commit was generated by cvs2svn to compensate for changes in r98679,
[FreeBSD/FreeBSD.git] / usr.sbin / xten / xten.c
1 /*-
2  * Copyright (c) 1992, 1993 Eugene W. Stark
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31
32 #ifndef lint
33 static const char rcsid[] =
34   "$FreeBSD$";
35 #endif /* not lint */
36
37 /*
38  * Xten - user command interface to X-10 daemon
39  */
40
41 #include <err.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <sys/un.h>
49 #include "xtend.h"
50 #include "xten.h"
51 #include "paths.h"
52
53 #define RETRIES 10
54 #define CMDLEN 512
55
56 char *X10housenames[] = {
57   "A", "B", "C", "D", "E", "F", "G", "H",
58   "I", "J", "K", "L", "M", "N", "O", "P",
59   NULL
60 };
61
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",
68   NULL
69 };
70
71 int find(char *, char *[]);
72 static void usage(void) __dead2;
73
74 int
75 main(int argc, char *argv[])
76 {
77         int c, tmp, h, k, sock, error;
78         FILE *daemon;
79         struct sockaddr_un sa;
80         char *sockpath = SOCKPATH;
81         char reply[CMDLEN], cmd[CMDLEN], *cp;
82         int interactive = 0;
83
84         if (argc == 2 && !strcmp(argv[1], "-"))
85                 interactive++;
86         else if(argc < 3)
87                 usage();
88         if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
89                 errx(1, "can't create socket");
90         strcpy(sa.sun_path, sockpath);
91         sa.sun_family = AF_UNIX;
92         if (connect(sock, (struct sockaddr *)&sa, strlen(sa.sun_path) + 2) < 0)
93                 errx(1, "can't connect to X-10 daemon");
94         if ((daemon = fdopen(sock, "w+")) == NULL)
95                 errx(1, "can't attach stream to socket");
96         /*
97          * If interactive, copy standard input to daemon and report results
98          * on standard output.
99          */
100         if (interactive) {
101                 while (!feof(stdin)) {
102                         if (fgets(cmd, CMDLEN, stdin) != NULL) {
103                                 fprintf(daemon, "%s", cmd);
104                                 fflush(daemon);
105                                 if (fgets(reply, CMDLEN, daemon) != NULL) {
106                                         fprintf(stdout, "%s", reply);
107                                         fflush(stdout);
108                                 }
109                         }
110                 }
111                 exit(0);
112         }
113         /*
114          * Otherwise, interpret arguments and issue commands to daemon,
115          * handling retries in case of errors.
116          */
117         if ((h = find(argv[1], X10housenames)) < 0)
118                 errx(1, "invalid house code: %s", argv[1]);
119         argv++;
120         argv++;
121         while (argc >= 3) {
122                 cp = argv[0];
123                 if((tmp = find(cp, X10housenames)) >= 0) {
124                         h = tmp;
125                         argv++;
126                         argc--;
127                         continue;
128                 }
129                 while (*cp != '\0' && *cp != ':')
130                         cp++;
131                 if (*cp == ':')
132                         c = atoi(cp+1);
133                 else
134                         c = 2;
135                 *cp = '\0';
136                 if ((k = find(argv[0], X10cmdnames)) < 0) {
137                         warnx("invalid key/unit code: %s", argv[0]);
138                         error++;
139                 }
140                 error = 0;
141                 while (error < RETRIES) {
142                         fprintf(daemon, "send %s %s %d\n", X10housenames[h],
143                             X10cmdnames[k], c);
144                         fflush(daemon);
145                         fgets(reply, CMDLEN, daemon);
146                         if(strncmp(reply, "ERROR", 5)) break;
147                         error++;
148                         usleep(200000);
149                 }
150                 if (error == RETRIES) {
151                         warnx("command failed: send %s %s %d",
152                             X10housenames[h], X10cmdnames[k], c);
153                 }
154                 argc--;
155                 argv++;
156         }
157         fprintf(daemon, "done\n");
158         fgets(reply, CMDLEN, daemon);
159         exit(0);
160 }
161
162 static void
163 usage(void)
164 {
165         fprintf(stderr,
166                 "usage: xten house key[:cnt] [[house] key[:cnt] ...]\n");
167         exit(1);
168 }
169
170 int
171 find(char *s, char *tab[])
172 {
173         int i;
174
175         for (i = 0; tab[i] != NULL; i++) {
176                 if (strcasecmp(s, tab[i]) == 0) 
177                         return (i);
178         }
179         return (-1);
180 }