gold: Fix hardcoded library search path
[dragonfly.git] / usr.sbin / xten / xten.c
CommitLineData
984263bc
MD
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.
1de703da
MD
30 *
31 * $FreeBSD: src/usr.sbin/xten/xten.c,v 1.4 1999/08/28 01:21:02 peter Exp $
44d05a04 32 * $DragonFly: src/usr.sbin/xten/xten.c,v 1.4 2005/12/05 01:23:23 swildner Exp $
984263bc
MD
33 */
34
984263bc
MD
35/*
36 * Xten - user command interface to X-10 daemon
37 */
38
39#include <err.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44#include <sys/types.h>
45#include <sys/socket.h>
46#include <sys/un.h>
47#include "xtend.h"
48#include "xten.h"
49#include "paths.h"
50
51#define RETRIES 10
52#define CMDLEN 512
53
54char *X10housenames[] = {
55 "A", "B", "C", "D", "E", "F", "G", "H",
56 "I", "J", "K", "L", "M", "N", "O", "P",
57 NULL
58};
59
60char *X10cmdnames[] = {
61 "1", "2", "3", "4", "5", "6", "7", "8",
62 "9", "10", "11", "12", "13", "14", "15", "16",
63 "AllUnitsOff", "AllLightsOn", "On", "Off", "Dim", "Bright", "AllLightsOff",
64 "ExtendedCode", "HailRequest", "HailAcknowledge", "PreSetDim0", "PreSetDim1",
65 "ExtendedData", "StatusOn", "StatusOff", "StatusRequest",
66 NULL
67};
68
2d8a3be7
EN
69int find(char *, char *[]);
70static void usage(void);
984263bc
MD
71
72int
44d05a04 73main(int argc, char *argv[])
984263bc
MD
74{
75 int c, tmp, h, k, sock, error;
76 FILE *daemon;
77 struct sockaddr_un sa;
78 char *sockpath = SOCKPATH;
79 char reply[CMDLEN], cmd[CMDLEN], *cp;
80 int interactive = 0;
81
82 if(argc == 2 && !strcmp(argv[1], "-")) interactive++;
83 else if(argc < 3)
84 usage();
85 if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
86 errx(1, "can't create socket");
87 strcpy(sa.sun_path, sockpath);
88 sa.sun_family = AF_UNIX;
89 if(connect(sock, (struct sockaddr *)(&sa), strlen(sa.sun_path) + 2) < 0)
90 errx(1, "can't connect to X-10 daemon");
91 if((daemon = fdopen(sock, "w+")) == NULL)
92 errx(1, "can't attach stream to socket");
93 /*
94 * If interactive, copy standard input to daemon and report results
95 * on standard output.
96 */
97 if(interactive) {
98 while(!feof(stdin)) {
99 if(fgets(cmd, CMDLEN, stdin) != NULL) {
100 fprintf(daemon, "%s", cmd);
101 fflush(daemon);
102 if(fgets(reply, CMDLEN, daemon) != NULL) {
103 fprintf(stdout, "%s", reply);
104 fflush(stdout);
105 }
106 }
107 }
108 exit(0);
109 }
110 /*
111 * Otherwise, interpret arguments and issue commands to daemon,
112 * handling retries in case of errors.
113 */
114 if((h = find(argv[1], X10housenames)) < 0)
115 errx(1, "invalid house code: %s", argv[1]);
116 argv++;
117 argv++;
118 while(argc >= 3) {
119 cp = argv[0];
120 if((tmp = find(cp, X10housenames)) >= 0) {
121 h = tmp;
122 argv++;
123 argc--;
124 continue;
125 }
126 while(*cp != '\0' && *cp != ':') cp++;
127 if(*cp == ':') c = atoi(cp+1);
128 else c = 2;
129 *cp = '\0';
130 if((k = find(argv[0], X10cmdnames)) < 0) {
131 warnx("invalid key/unit code: %s", argv[0]);
132 error++;
133 }
134 error = 0;
135 while(error < RETRIES) {
136 fprintf(daemon, "send %s %s %d\n", X10housenames[h], X10cmdnames[k], c);
137 fflush(daemon);
138 fgets(reply, CMDLEN, daemon);
139 if(strncmp(reply, "ERROR", 5)) break;
140 error++;
141 usleep(200000);
142 }
143 if(error == RETRIES) {
144 warnx("command failed: send %s %s %d",
145 X10housenames[h], X10cmdnames[k], c);
146 }
147 argc--;
148 argv++;
149 }
150 fprintf(daemon, "done\n");
151 fgets(reply, CMDLEN, daemon);
152 exit(0);
153}
154
155static void
44d05a04 156usage(void)
984263bc
MD
157{
158 fprintf(stderr,
159 "usage: xten house key[:cnt] [[house] key[:cnt] ...]\n");
160 exit(1);
161}
162
163int
44d05a04 164find(char *s, char *tab[])
984263bc
MD
165{
166 int i;
167
168 for(i = 0; tab[i] != NULL; i++) {
169 if(strcasecmp(s, tab[i]) == 0) return(i);
170 }
171 return(-1);
172}