aboutsummaryrefslogtreecommitdiff
path: root/src/command.c
blob: 5f4712644e8a56ab948f8efa6e780550a5e27954 (plain)
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
/**
 * @file command.c
 * @author Joe Wingbermuehle
 * @date 2004-2006
 *
 * @brief Handle running startup, shutdown, and restart commands.
 *
 */

#include "jwm.h"
#include "command.h"
#include "root.h"
#include "misc.h"
#include "main.h"

/** Structure to represent a list of commands. */
typedef struct CommandNode {
	char *command;             /**< The command. */
	struct CommandNode *next;  /**< The next command in the list. */
} CommandNode;

static CommandNode *startupCommands;
static CommandNode *shutdownCommands;
static CommandNode *restartCommands;

static void RunCommands(CommandNode *commands);
static void ReleaseCommands(CommandNode **commands);
static void AddCommand(CommandNode **commands, const char *command);

/** Initialize the command lists. */
void
InitializeCommands()
{
	startupCommands = NULL;
	shutdownCommands = NULL;
	restartCommands = NULL;
}

/** Process startup/restart commands. */
void
StartupCommands()
{

	if(isRestarting) {
		RunCommands(restartCommands);
	} else {
		RunCommands(startupCommands);
	}

}

/** Process shutdown commands. */
void
ShutdownCommands()
{

	if(!shouldRestart) {
		RunCommands(shutdownCommands);
	}

}

/** Destroy the command lists. */
void
DestroyCommands()
{
	ReleaseCommands(&startupCommands);
	ReleaseCommands(&shutdownCommands);
	ReleaseCommands(&restartCommands);
}

/** Run the commands in a command list. */
void
RunCommands(CommandNode *commands)
{

	CommandNode *cp;

	for(cp = commands; cp; cp = cp->next) {
		RunCommand(cp->command);
	}

}

/** Release a command list. */
void
ReleaseCommands(CommandNode **commands)
{

	CommandNode *cp;

	Assert(commands);

	while(*commands) {
		cp = (*commands)->next;
		Release((*commands)->command);
		Release(*commands);
		*commands = cp;
	}

}

/** Add a command to a command list. */
void
AddCommand(CommandNode **commands, const char *command)
{

	CommandNode *cp;

	Assert(commands);

	if(!command) {
		return;
	}

	cp = Allocate(sizeof(CommandNode));
	cp->next = *commands;
	*commands = cp;

	cp->command = CopyString(command);

}

/** Add a startup command. */
void
AddStartupCommand(const char *command)
{
	AddCommand(&startupCommands, command);
}

/** Add a shutdown command. */
void
AddShutdownCommand(const char *command)
{
	AddCommand(&shutdownCommands, command);
}

/** Add a restart command. */
void
AddRestartCommand(const char *command)
{
	AddCommand(&restartCommands, command);
}