2 * Copyright (c) 1992, 1993, 1995 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[] =
34 "$FreeBSD: src/libexec/xtend/user.c,v 1.9 1999/08/28 00:10:30 peter Exp $";
41 #include <sys/param.h>
47 MONENTRY Monitor[MAXMON];
49 int find __P((char *, char *[]));
50 void printstatus __P((FILE *, STATUS *));
53 * Process a user command
62 char cmd[512], dumppath[MAXPATHLEN+1], pkt[3];
66 if(fgets(cmd, 512, User) != NULL) {
68 while ( *m != '\0' ) {
73 if(sscanf(cmd, "status %c %d", &h, &i) == 2
74 && h >= 'a' && h <= 'p' && i >= 1 && i <= 16) {
77 printstatus(User, &Status[h][i]);
78 } else if(sscanf(cmd, "send %c %s %d", &h, cmd, &n) == 3
79 && h >= 'a' && h <= 'p' && (i = find(cmd, X10cmdnames)) >= 0) {
84 if(write(tw523, pkt, 3) != 3) {
85 fprintf(Log, "%s: Transmission error (packet [%s %s]:%d).\n",
86 thedate(), X10housenames[h], X10cmdnames[i], n);
89 fprintf(User, "OK\n");
91 } else if(!strcmp("dump\n", cmd)) {
92 strcpy(dumppath, X10DIR);
93 strcat(dumppath, "/");
94 strcat(dumppath, X10DUMPNAME);
95 if((dumpf = fopen(dumppath, "w")) != NULL) {
96 for(h = 0; h < 16; h++) {
97 for(i = 0; i < 16; i++) {
98 if(Status[h][i].lastchange) {
99 fprintf(dumpf, "%s%d\t", X10housenames[h], i+1);
100 printstatus(dumpf, &Status[h][i]);
105 fprintf(User, "OK\n");
109 } else if(sscanf(cmd, "monitor %c %d", &h, &i) == 2
110 && h >= 'a' && h <= 'p' && i >= 1 && i <= 16) {
113 for(k = 0; k < MAXMON; k++) {
114 if(!Monitor[k].inuse) break;
119 Monitor[k].house = h;
121 Monitor[k].user = User;
122 Monitor[k].inuse = 1;
123 fprintf(Log, "%s: Adding %c %d to monitor list (entry %d)\n",
124 thedate(), h+'A', i+1, k);
125 fprintf(User, "OK\n");
128 return(0); /* We don't want caller to close stream */
130 } else if(!strcmp("done\n", cmd)) {
131 fprintf(User, "OK\n");
145 fprintf(User, "ERROR\n");
158 for(i = 0; tab[i] != NULL; i++) {
159 if(strcasecmp(s, tab[i]) == 0) return(i);
169 fprintf(f, "%s:%d", s->onoff ? "On" : "Off", s->brightness);
170 switch(s->selected) {
172 fprintf(f, " (normal) "); break;
174 fprintf(f, " (selected) "); break;
176 fprintf(f, " (dimming) "); break;
178 fprintf(f, " (brightening) "); break;
180 fprintf(f, " (requested) "); break;
182 fprintf(f, " (hailed) "); break;
184 fprintf(f, " (bogus) "); break;
186 fprintf(f, "%s", ctime(&s->lastchange));