Initial import from FreeBSD RELENG_4:
[dragonfly.git] / libexec / xtend / user.c
1 /*-
2  * Copyright (c) 1992, 1993, 1995 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: src/libexec/xtend/user.c,v 1.9 1999/08/28 00:10:30 peter Exp $";
35 #endif /* not lint */
36
37 #include <ctype.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include "xtend.h"
44 #include "xten.h"
45 #include "paths.h"
46
47 MONENTRY Monitor[MAXMON];
48
49 int find __P((char *, char *[]));
50 void printstatus __P((FILE *, STATUS *));
51
52 /*
53  * Process a user command
54  */
55
56 int
57 user_command()
58 {
59   char h;
60   char *m;
61   int i, k, n, error;
62   char cmd[512], dumppath[MAXPATHLEN+1], pkt[3];
63   FILE *dumpf;
64
65   error = 0;
66   if(fgets(cmd, 512, User) != NULL) {
67     m = cmd;
68     while ( *m != '\0' ) {
69         if(isupper(*m))
70             *m = tolower(*m);
71         m++;
72     }
73     if(sscanf(cmd, "status %c %d", &h, &i) == 2
74                 && h >= 'a' && h <= 'p' && i >= 1 && i <= 16) {
75       h -= 'a';
76       i--;
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) {
80       h -= 'a';
81       pkt[0] = h;
82       pkt[1] = i;
83       pkt[2] = n;
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);
87         error++;
88       } else {
89         fprintf(User, "OK\n");
90       }
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]);
101             }
102           }
103         }
104         fclose(dumpf);
105         fprintf(User, "OK\n");
106       } else {
107         error++;
108       }
109     } else if(sscanf(cmd, "monitor %c %d", &h, &i) == 2
110               && h >= 'a' && h <= 'p' && i >= 1 && i <= 16) {
111       h -= 'a';
112       i--;
113       for(k = 0; k < MAXMON; k++) {
114         if(!Monitor[k].inuse) break;
115       }
116       if(k == MAXMON) {
117         error++;
118       } else {
119         Monitor[k].house = h;
120         Monitor[k].unit = i;
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");
126         fflush(User);
127         User = NULL;
128         return(0);  /* We don't want caller to close stream */
129       }
130     } else if(!strcmp("done\n", cmd)) {
131         fprintf(User, "OK\n");
132         fflush(User);
133         return(1);
134     } else {
135       if(feof(User)) {
136         return(1);
137       } else {
138         error++;
139       }
140     }
141   } else {
142     error++;
143   }
144   if(error) {
145     fprintf(User, "ERROR\n");
146   }
147   fflush(User);
148   return(0);
149 }
150
151 int
152 find(s, tab)
153 char *s;
154 char *tab[];
155 {
156         int i;
157
158         for(i = 0; tab[i] != NULL; i++) {
159           if(strcasecmp(s, tab[i]) == 0) return(i);
160         }
161         return(-1);
162 }
163
164 void
165 printstatus(f, s)
166 FILE *f;
167 STATUS *s;
168 {
169   fprintf(f, "%s:%d", s->onoff ? "On" : "Off", s->brightness);
170   switch(s->selected) {
171   case IDLE:
172     fprintf(f, " (normal) "); break;
173   case SELECTED:
174     fprintf(f, " (selected) "); break;
175   case DIMMING:
176     fprintf(f, " (dimming) "); break;
177   case BRIGHTENING:
178     fprintf(f, " (brightening) "); break;
179   case REQUESTED:
180     fprintf(f, " (requested) "); break;
181   case HAILED:
182     fprintf(f, " (hailed) "); break;
183   default:
184     fprintf(f, " (bogus) "); break;
185   }
186   fprintf(f, "%s", ctime(&s->lastchange));
187 }
188