-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.c
265 lines (226 loc) · 5.35 KB
/
command.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
///
/// @file command.c @brief command functions.
///
/// Copyright (c) 2009, 2010, 2021 by Lutz Sammer. All Rights Reserved.
///
/// Contributor(s):
///
/// License: AGPLv3
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU Affero General Public License for more details.
///
/// $Id$
//////////////////////////////////////////////////////////////////////////////
///
/// @defgroup command The command module.
///
/// This module handles running startup, exiting, and restart commands,
/// And starting external commands.
///
///< @{
#define _GNU_SOURCE 1 ///< fix stpcpy strchrnul
#include <xcb/xcb.h>
#include "uwm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "core-array/core-array.h"
#include "core-rc/core-rc.h"
#include "command.h"
// ------------------------------------------------------------------------ //
/**
** command list typedef.
*/
typedef struct _commands_ Commands;
/**
** command list structure
**
** commands are separated by nul, an empty strings terminates the list.
*/
struct _commands_
{
char *Commands; ///< list of 0-terminated strings
};
/// Command which should be run at startup
static Commands CommandsStartup;
/// Command which should be run at shutdown
static Commands CommandsExiting;
/// Command which should be run at restart
static Commands CommandsRestart;
char *ExitCommand; ///< command to be run at exit
const char *Shell; ///< shell for commands
// ------------------------------------------------------------------------ //
/**
** Prepare environment for command execution.
**
** Add "DISPLAY=..." to environment, if needed.
*/
void CommandPrepareEnv(void)
{
// prepare environment
if (DisplayString && *DisplayString) {
char *str;
str = malloc(sizeof("DISPLAY=") + strlen(DisplayString));
stpcpy(stpcpy(str, "DISPLAY="), DisplayString);
putenv(str);
}
}
/**
** Execute an external program.
**
** @param command command string to be executed in shell
**
** @see DisplayString, Shell
*/
void CommandRun(const char *command)
{
if (!command) {
return;
}
if (!fork()) {
if (!fork()) {
close(xcb_get_file_descriptor(Connection));
CommandPrepareEnv();
execl(Shell, Shell, "-c", command, NULL);
Warning("exec failed: %s -c %s\n", Shell, command);
exit(-1);
}
exit(0);
}
wait(NULL);
}
/**
** Run commands in a command list.
**
** @param list list of 0-terminated strings
*/
static void CommandsRun(const Commands list)
{
const char *s;
if ((s = list.Commands)) {
do {
CommandRun(s);
while (*s++) {
}
} while (*s);
}
}
/**
** Free a command list.
**
** @param list list of 0-terminated strings
*/
static void CommandsFree(Commands * list)
{
free(list->Commands);
list->Commands = NULL;
}
/**
** Initialize command module.
**
** Run commands in startup on boot, in restart if restarting.
**
** @see KeepRunning.
*/
void CommandInit(void)
{
// prepare shell
if (!Shell && !(Shell = getenv("SHELL"))) {
Shell = SHELL;
}
if (KeepRunning) {
CommandsRun(CommandsRestart);
} else {
CommandsRun(CommandsStartup);
}
}
/**
** Cleanup command module.
**
** Run commands in exiting on exit.
**
** @see KeepRunning.
*/
void CommandExit(void)
{
if (!KeepRunning) {
CommandsRun(CommandsExiting);
}
CommandsFree(&CommandsStartup);
CommandsFree(&CommandsExiting);
CommandsFree(&CommandsRestart);
}
// ------------------------------------------------------------------------ //
// Config
#ifdef USE_RC // {
/**
** Parse configuration of a command list.
**
** @param config global config dictionary
** @param head command list head
** @param index config index key
*/
static void CommandAdd(const Config * config, Commands * head,
const char *index)
{
const ConfigObject *array;
if (ConfigStringsGetArray(ConfigDict(config), &array, index, NULL)) {
const ConfigObject *index;
const ConfigObject *value;
int len;
len = 0;
//
// Calculate length of commands
//
index = NULL;
value = ConfigArrayFirst(array, &index);
while (value) {
const char *command;
if (ConfigCheckString(value, &command)) {
len += strlen(command) + 1;
}
value = ConfigArrayNext(array, &index);
}
if (len) {
char *dst;
head->Commands = dst = malloc(len + 1);
//
// Copy the commands
//
index = NULL;
value = ConfigArrayFirst(array, &index);
while (value) {
const char *command;
if (ConfigCheckString(value, &command)) {
dst = stpcpy(dst, command);
++dst;
}
value = ConfigArrayNext(array, &index);
}
*dst = '\0'; // final nul to terminate list
}
}
}
/**
** Parse configuration for command module.
**
** @param config global config dictionary
*/
void CommandConfig(const Config * config)
{
CommandAdd(config, &CommandsStartup, "command-startup");
CommandAdd(config, &CommandsRestart, "command-restart");
CommandAdd(config, &CommandsExiting, "command-exiting");
}
#endif // } USE_RC
/// @}