File: configfile.c

package info (click to toggle)
cdde 0.3.1-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 504 kB
  • ctags: 73
  • sloc: sh: 995; ansic: 782; makefile: 17
file content (237 lines) | stat: -rw-r--r-- 5,899 bytes parent folder | download | duplicates (2)
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
/*
Compact Disc Detect & Execute

Copyright(C) 2002-04 Eric Lathrop <eric@ericlathrop.com>
Copyright(C) 2008, Stanislav Maslovski <stanislav.maslovski@gmail.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

For more details see the file COPYING.

Changes:
	2008/08/25, Stanislav Maslovski:
	    Fixed segfaults when properties "path" or "command" are not defined.
*/


#include <syslog.h>

#include "configfile.h"
#include "cdde.h"

#include <errno.h>
extern int errno;

// read a configuration file
// returns nonzero on error
int loadconfig(char * filename)
{
	xmlDocPtr doc = NULL;
	xmlNodePtr cur = NULL;
	char * temp = NULL;
	
	// parse the document
	doc = xmlParseFile(filename);
	if (doc == NULL)
	{
		syslog(LOG_ERR, "Error: Could not parse document");
		xmlFreeDoc(doc);
		return 1;
	}

	// get the root node
	cur = xmlDocGetRootElement(doc);
	if (cur == NULL)
	{
		syslog(LOG_ERR, "Error: Could not parse document: unable to find root element");
		xmlFreeDoc(doc);
		return 1;
	}

	// make sure the root is a "cdde"
	if (xmlStrcmp(cur->name, (const xmlChar *) "cdde"))
	{
		syslog(LOG_ERR, "Error: Could not parse document: root element is not \"cdde\"");
		xmlFreeDoc(doc);
		return 1;
	}

	// find the delay
	temp = xmlGetProp(cur, (const xmlChar *) "delay");
	if (temp == NULL)
	{
		syslog(LOG_WARNING, "Warning: Did not find polling delay in config file, using %d", DEFAULT_SLEEP_N);
		delay = DEFAULT_SLEEP_N;
	} else {
		errno = 0;
		delay = strtol(temp, NULL, 10);
		if (errno != 0)
		{
			syslog(LOG_WARNING, "Warning: Delay had under/overflow, using %d", DEFAULT_SLEEP_N);
			delay = DEFAULT_SLEEP_N;
		}
		if (delay < 0)
		{
			syslog(LOG_WARNING, "Warning: Delay can't be negative, using %d", DEFAULT_SLEEP_N);
			delay = DEFAULT_SLEEP_N;
		}
		
		xmlFree(temp);
	}
	if (verbose) syslog(LOG_INFO, "Using polling delay of %d microseconds", delay);
	
	// find all of the children
	cur = cur->xmlChildrenNode;
	while (cur != NULL)
	{
		if (!xmlStrcmp(cur->name, (const xmlChar *) "drive"))
		{
			parsedrive(cur);
		}
		cur = cur->next;
	}
	xmlFreeDoc(doc);
	
	return 0;
}

// parse the <drive> nodes
// and their children
void parsedrive(xmlNodePtr cur)
{
	int i;
	drive * d;
	xmlChar * data;
	
	data = xmlGetProp(cur, "path");
	if (!data)
	{
		syslog(LOG_WARNING,
			"Warning: no \"path\" property defined for drive, skipping.");
		return;
	}

	d = malloc(sizeof(drive));
	if (!d)
	{
		syslog(LOG_ERR, "Error: Couldn't allocate memory for drive.");
		exit(1);
	}

	d->filename = data;
	for (i=0; i<NUM_DATA_TYPES; i++)
	{
		d->commands[i] = NULL;
	}
	d->dontexecute = dontrunfirst;
	d->mediachange = 1;
	
	// search it's children for commands, and add them
	cur = cur->xmlChildrenNode;
	while (cur != NULL)
	{
		for (i=0; i<NUM_DATA_TYPES; i++)
		{
			if (!xmlStrcmp(cur->name, (const xmlChar *) datatags[i]))
			{
				if (data = xmlGetProp(cur, "command"))
				{
					d->commands[i] = list_push(d->commands[i], data);
					if (d->commands[i] == NULL)
					{
						syslog(LOG_ERR, "Error: Couldn't allocate memory");
						exit(1);
					}
				}
				break;
			}
		}
		cur = cur->next;
	}
	drives = list_push(drives, d);
	if (drives == NULL)
	{
		syslog(LOG_ERR, "Error: Couldn't allocate memory");
		exit(1);
	}
}

// write a template configuation file
// returns zero on success
int saveconfig(char * filename)
{
	xmlDocPtr doc;
	xmlNodePtr cur;
	xmlNodePtr root;
	xmlNodePtr subroot;
	int retval = 0;
	
	// create the root
	root = xmlNewNode(NULL, (xmlChar *) "cdde");
	xmlSetProp(root, (xmlChar *) "delay", (xmlChar *) DEFAULT_SLEEP_S);

	// add a drive
	// FIXME: try to guess cdrom device paths by looking at fstab
	subroot = xmlNewNode(NULL, (xmlChar *) "drive");
	xmlSetProp(subroot, (xmlChar *) "path", (xmlChar *) "/dev/cdrom");
	xmlAddChild(root, subroot);

	// add commands
	cur = xmlNewNode(NULL, (xmlChar *) "audio");
	xmlSetProp(cur, (xmlChar *) "command", (xmlChar *) "echo An audio cd was inserted.");
	xmlAddChild(subroot, cur);

	// add commands
	cur = xmlNewNode(NULL, (xmlChar *) "data");
	xmlSetProp(cur, (xmlChar *) "command", (xmlChar *) "echo A data cd was inserted.");
	xmlAddChild(subroot, cur);

	// add commands
	cur = xmlNewNode(NULL, (xmlChar *) "dvd");
	xmlSetProp(cur, (xmlChar *) "command", (xmlChar *) "echo A dvd was inserted.");
	xmlAddChild(subroot, cur);

	// add commands
	cur = xmlNewNode(NULL, (xmlChar *) "vcd");
	xmlSetProp(cur, (xmlChar *) "command", (xmlChar *) "echo A vcd was inserted.");
	xmlAddChild(subroot, cur);

	// add commands
	cur = xmlNewNode(NULL, (xmlChar *) "svcd");
	xmlSetProp(cur, (xmlChar *) "command", (xmlChar *) "echo A svcd was inserted.");
	xmlAddChild(subroot, cur);

	// add commands
	cur = xmlNewNode(NULL, (xmlChar *) "blank");
	xmlSetProp(cur, (xmlChar *) "command", (xmlChar *) "echo A blank cdr/dvdr was inserted.");
	xmlAddChild(subroot, cur);

	// add commands
	cur = xmlNewNode(NULL, (xmlChar *) "mixed");
	xmlSetProp(cur, (xmlChar *) "command", (xmlChar *) "echo A mixed (audio/data) cd was inserted.");
	xmlAddChild(subroot, cur);

	// create the document
	doc = xmlNewDoc("1.0");
	xmlDocSetRootElement(doc, root);

	// write out the xml document
	if (xmlSaveFormatFile (filename, doc, 1) == -1) retval = -1;

	// free memory
	xmlFreeDoc(doc);
	
	return retval;
}