gold: Fix hardcoded library search path
[dragonfly.git] / libexec / xtend / user.c
CommitLineData
984263bc
MD
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.
1de703da
MD
30 *
31 * $FreeBSD: src/libexec/xtend/user.c,v 1.9 1999/08/28 00:10:30 peter Exp $
3eefb696 32 * $DragonFly: src/libexec/xtend/user.c,v 1.3 2003/11/14 03:54:32 dillon Exp $
984263bc
MD
33 */
34
984263bc
MD
35#include <ctype.h>
36#include <stdio.h>
37#include <string.h>
38#include <unistd.h>
39#include <sys/param.h>
40#include <sys/time.h>
41#include "xtend.h"
42#include "xten.h"
43#include "paths.h"
44
45MONENTRY Monitor[MAXMON];
46
3eefb696
MD
47int find (char *, char *[]);
48void printstatus (FILE *, STATUS *);
984263bc
MD
49
50/*
51 * Process a user command
52 */
53
54int
55user_command()
56{
57 char h;
58 char *m;
59 int i, k, n, error;
60 char cmd[512], dumppath[MAXPATHLEN+1], pkt[3];
61 FILE *dumpf;
62
63 error = 0;
64 if(fgets(cmd, 512, User) != NULL) {
65 m = cmd;
66 while ( *m != '\0' ) {
67 if(isupper(*m))
68 *m = tolower(*m);
69 m++;
70 }
71 if(sscanf(cmd, "status %c %d", &h, &i) == 2
72 && h >= 'a' && h <= 'p' && i >= 1 && i <= 16) {
73 h -= 'a';
74 i--;
75 printstatus(User, &Status[h][i]);
76 } else if(sscanf(cmd, "send %c %s %d", &h, cmd, &n) == 3
77 && h >= 'a' && h <= 'p' && (i = find(cmd, X10cmdnames)) >= 0) {
78 h -= 'a';
79 pkt[0] = h;
80 pkt[1] = i;
81 pkt[2] = n;
82 if(write(tw523, pkt, 3) != 3) {
83 fprintf(Log, "%s: Transmission error (packet [%s %s]:%d).\n",
84 thedate(), X10housenames[h], X10cmdnames[i], n);
85 error++;
86 } else {
87 fprintf(User, "OK\n");
88 }
89 } else if(!strcmp("dump\n", cmd)) {
90 strcpy(dumppath, X10DIR);
91 strcat(dumppath, "/");
92 strcat(dumppath, X10DUMPNAME);
93 if((dumpf = fopen(dumppath, "w")) != NULL) {
94 for(h = 0; h < 16; h++) {
95 for(i = 0; i < 16; i++) {
96 if(Status[h][i].lastchange) {
97 fprintf(dumpf, "%s%d\t", X10housenames[h], i+1);
98 printstatus(dumpf, &Status[h][i]);
99 }
100 }
101 }
102 fclose(dumpf);
103 fprintf(User, "OK\n");
104 } else {
105 error++;
106 }
107 } else if(sscanf(cmd, "monitor %c %d", &h, &i) == 2
108 && h >= 'a' && h <= 'p' && i >= 1 && i <= 16) {
109 h -= 'a';
110 i--;
111 for(k = 0; k < MAXMON; k++) {
112 if(!Monitor[k].inuse) break;
113 }
114 if(k == MAXMON) {
115 error++;
116 } else {
117 Monitor[k].house = h;
118 Monitor[k].unit = i;
119 Monitor[k].user = User;
120 Monitor[k].inuse = 1;
121 fprintf(Log, "%s: Adding %c %d to monitor list (entry %d)\n",
122 thedate(), h+'A', i+1, k);
123 fprintf(User, "OK\n");
124 fflush(User);
125 User = NULL;
126 return(0); /* We don't want caller to close stream */
127 }
128 } else if(!strcmp("done\n", cmd)) {
129 fprintf(User, "OK\n");
130 fflush(User);
131 return(1);
132 } else {
133 if(feof(User)) {
134 return(1);
135 } else {
136 error++;
137 }
138 }
139 } else {
140 error++;
141 }
142 if(error) {
143 fprintf(User, "ERROR\n");
144 }
145 fflush(User);
146 return(0);
147}
148
149int
150find(s, tab)
151char *s;
152char *tab[];
153{
154 int i;
155
156 for(i = 0; tab[i] != NULL; i++) {
157 if(strcasecmp(s, tab[i]) == 0) return(i);
158 }
159 return(-1);
160}
161
162void
163printstatus(f, s)
164FILE *f;
165STATUS *s;
166{
167 fprintf(f, "%s:%d", s->onoff ? "On" : "Off", s->brightness);
168 switch(s->selected) {
169 case IDLE:
170 fprintf(f, " (normal) "); break;
171 case SELECTED:
172 fprintf(f, " (selected) "); break;
173 case DIMMING:
174 fprintf(f, " (dimming) "); break;
175 case BRIGHTENING:
176 fprintf(f, " (brightening) "); break;
177 case REQUESTED:
178 fprintf(f, " (requested) "); break;
179 case HAILED:
180 fprintf(f, " (hailed) "); break;
181 default:
182 fprintf(f, " (bogus) "); break;
183 }
184 fprintf(f, "%s", ctime(&s->lastchange));
185}
186