File: server_commands.c

package info (click to toggle)
lcdproc 0.5.7-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,012 kB
  • ctags: 6,532
  • sloc: ansic: 58,028; sh: 2,005; perl: 681; makefile: 482
file content (183 lines) | stat: -rw-r--r-- 4,242 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/** \file server/commands/server_commands.c
 * Implements handlers for client commands concerning the server settings.
 *
 * This contains definitions for all the functions which clients can run.
 * The functions here are to be called only from parse.c's interpreter.
 *
 * The client's available function set is defined here, as is the syntax
 * for each command.
 */

/* This file is part of LCDd, the lcdproc server.
 *
 * This file is released under the GNU General Public License.
 * Refer to the COPYING file distributed with this package.
 *
 * Copyright (c) 1999, William Ferrell, Selene Scriven
 *               2002, Joris Robijn
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include "shared/report.h"
#include "shared/sockets.h"

#include "client.h"
#include "render.h"
#include "server_commands.h"

#define ALL_OUTPUTS_ON -1
#define ALL_OUTPUTS_OFF 0

/**
 * Sets the state of the output port (such as on MtxOrb LCDs)
 *
 *\verbatim
 * Usage: output <on|off|int>
 *\endverbatim
 */
int
output_func(Client *c, int argc, char **argv)
{
	if (c->state != ACTIVE)
		return 1;

	if (argc != 2) {
		sock_send_error(c->sock, "Usage: output {on|off|<num>}\n");
		return 0;
	}

	if (0 == strcmp(argv[1], "on"))
		output_state = ALL_OUTPUTS_ON;
	else if (0 == strcmp(argv[1], "off"))
		output_state = ALL_OUTPUTS_OFF;
	else {
		long out;
		char *endptr;

		/* Note that there is no valid range set for
		 * output_state; thus a value in the 12 digits
		 * is not considered out of range.
		 */

		/* set errno to be able to detect errors in strtol() */
		errno = 0;

		out = strtol(argv[1], &endptr, 0);

		if (errno) {
			sock_printf_error(c->sock, "number argument: %s\n", strerror(errno));
			return 0;
		}
		else if ((*argv[1] != '\0') && (*endptr == '\0')) {
			output_state = out;
		}
		else {
			sock_send_error(c->sock, "invalid parameter...\n");
			return 0;
		}
	}

	sock_send_string(c->sock, "success\n");

	/* Makes sense to me to set the output immediately;
	 * however, the outputs are currently set in
	 * draw_screen(screen *s, int timer)
	 * Whatever for? */

	/* drivers_output(output_state); */

	report(RPT_NOTICE, "output states changed");
	return 0;
}

/**
 * The sleep_func was intended to make the server sleep for some seconds.
 * This function is currently ignored as making the server sleep actually
 * stalls it and disrupts other clients.
 *
 *\verbatim
 * Usage: sleep <seconds>
 *\endverbatim
 */
int
sleep_func(Client *c, int argc, char **argv)
{
	int secs;
	long out;
	char *endptr;

#define MAX_SECS 60
#define MIN_SECS 1

	if (c->state != ACTIVE)
		return 1;

	if (argc != 2) {
		sock_send_error(c->sock, "Usage: sleep <secs>\n");
		return 0;
	}

	/* set errno to be able to detect errors in strtol() */
	errno = 0;

	out = strtol(argv[1], &endptr, 0);

	/* From the man page for strtol(3)
	 *
	 * In particular, if *nptr is not `\0' but **endptr is
	 * `\0' on return, the entire string is valid.
	 *
	 * In this case, argv[1] is *nptr, and &endptr is **endptr.
	 */

	if (errno) {
		sock_printf_error(c->sock, "number argument: %s\n", strerror(errno));
		return 0;
	}
	else if ((*argv[1] != '\0') && (*endptr == '\0')) {
		/* limit seconds to range: MIN_SECS - MAX_SECS */
		out = (out > MAX_SECS) ? MAX_SECS : out;
		out = (out < MIN_SECS) ? MIN_SECS : out;
		secs = out;
	}
	else {
		sock_send_error(c->sock, "invalid parameter...\n");
		return 0;
	}

	/* Repeat until no more remains - should normally be zero
	 * on exit the first time...*/
	sock_printf(c->sock, "sleeping %d seconds\n", secs);

	/* whoops.... if this takes place as planned, ALL screens
	 * will "freeze" for the alloted time...
	 *
	 * while ((secs = sleep(secs)) > 0)
	 */	;

	sock_send_error(c->sock, "ignored (not fully implemented)\n");
	return 0;
}

/**
 * Does nothing, returns "noop complete" message.
 *
 * This is useful for shell scripts or programs that want to talk
 *    with LCDproc and not get deadlocked.  Send a noop after each
 *    command and look for the "noop complete" message.
 */
int
noop_func(Client *c, int argc, char **argv)
{
	if (c->state != ACTIVE)
		return 1;

	sock_send_string(c->sock, "noop complete\n");
	return 0;
}