1/* $Id: options.c,v 1.11 2009/11/19 03:22:35 jmaggard Exp $ */
2/* MiniUPnP project
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * author: Ryan Wagoner
5 * (c) 2006 Thomas Bernard
6 * This software is subject to the conditions detailed
7 * in the LICENCE file provided within the distribution */
8
9#include <stdio.h>
10#include <string.h>
11#include <stdlib.h>
12#include <ctype.h>
13#include "options.h"
14#include "upnpglobalvars.h"
15
16struct option * ary_options = NULL;
17int num_options = 0;
18
19static const struct {
20	enum upnpconfigoptions id;
21	const char * name;
22} optionids[] = {
23	{ UPNPIFNAME, "network_interface" },
24	{ UPNPLISTENING_IP, "listening_ip" },
25	{ UPNPPORT, "port" },
26	{ UPNPPRESENTATIONURL, "presentation_url" },
27	{ UPNPNOTIFY_INTERVAL, "notify_interval" },
28	{ UPNPSYSTEM_UPTIME, "system_uptime" },
29	{ UPNPUUID, "uuid"},
30	{ UPNPSERIAL, "serial"},
31	{ UPNPMODEL_NUMBER, "model_number"},
32	{ UPNPFRIENDLYNAME, "friendly_name"},
33	{ UPNPMEDIADIR, "media_dir"},
34	{ UPNPALBUMART_NAMES, "album_art_names"},
35	{ UPNPINOTIFY, "inotify" },
36	{ UPNPDBDIR, "db_dir" },
37	{ ENABLE_TIVO, "enable_tivo" },
38	{ ENABLE_DLNA_STRICT, "strict_dlna" }
39};
40
41int
42readoptionsfile(const char * fname)
43{
44	FILE *hfile = NULL;
45	char buffer[1024];
46	char *equals;
47	char *name;
48	char *value;
49	char *t;
50	int linenum = 0;
51	int i;
52	enum upnpconfigoptions id;
53
54	if(!fname || (strlen(fname) == 0))
55		return -1;
56
57	memset(buffer, 0, sizeof(buffer));
58
59#ifdef DEBUG
60	printf("Reading configuration from file %s\n", fname);
61#endif
62
63	if(!(hfile = fopen(fname, "r")))
64		return -1;
65
66	if(ary_options != NULL)
67	{
68		free(ary_options);
69		num_options = 0;
70	}
71
72	while(fgets(buffer, sizeof(buffer), hfile))
73	{
74		linenum++;
75		t = strchr(buffer, '\n');
76		if(t)
77		{
78			*t = '\0';
79			t--;
80			while((t >= buffer) && isspace(*t))
81			{
82				*t = '\0';
83				t--;
84			}
85		}
86
87		/* skip leading whitespaces */
88		name = buffer;
89		while(isspace(*name))
90			name++;
91
92		/* check for comments or empty lines */
93		if(name[0] == '#' || name[0] == '\0') continue;
94
95		if(!(equals = strchr(name, '=')))
96		{
97			fprintf(stderr, "parsing error file %s line %d : %s\n",
98			        fname, linenum, name);
99			continue;
100		}
101
102		/* remove ending whitespaces */
103		for(t=equals-1; t>name && isspace(*t); t--)
104			*t = '\0';
105
106		*equals = '\0';
107		value = equals+1;
108
109		/* skip leading whitespaces */
110		while(isspace(*value))
111			value++;
112
113		id = UPNP_INVALID;
114		for(i=0; i<sizeof(optionids)/sizeof(optionids[0]); i++)
115		{
116			/*printf("%2d %2d %s %s\n", i, optionids[i].id, name,
117			       optionids[i].name); */
118
119			if(0 == strcmp(name, optionids[i].name))
120			{
121				id = optionids[i].id;
122				break;
123			}
124		}
125
126		if(id == UPNP_INVALID)
127		{
128			fprintf(stderr, "parsing error file %s line %d : %s=%s\n",
129			        fname, linenum, name, value);
130		}
131		else
132		{
133			num_options += 1;
134			ary_options = (struct option *) realloc(ary_options, num_options * sizeof(struct option));
135
136			ary_options[num_options-1].id = id;
137			strncpy(ary_options[num_options-1].value, value, MAX_OPTION_VALUE_LEN);
138		}
139
140	}
141
142	fclose(hfile);
143
144	return 0;
145}
146
147void
148freeoptions(void)
149{
150	if(ary_options)
151	{
152		free(ary_options);
153		ary_options = NULL;
154		num_options = 0;
155	}
156}
157
158