blob: 027f803fc3517bd476caef3dc7da3e5a65db67be [file] [log] [blame]
kthacker62e146c2006-04-17 15:11:35 +00001/*
2 * Copyright (c) 2003, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior
15 * written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * This file is part of the Contiki desktop OS.
30 *
31 * $Id: config.c,v 1.1 2006/04/17 15:18:15 kthacker Exp $
32 *
33 */
34
35#include "program-handler.h"
36#include "loader.h"
37#include "cfs.h"
38#include "uip.h"
39#include "uiplib.h"
40#include "uip_arp.h"
41#include "resolv.h"
42
43
44struct ptentry {
45 char c;
46 char * (* pfunc)(char *str);
47};
48
49/*-----------------------------------------------------------------------------------*/
50static char *
51parse(char *str, struct ptentry *t)
52{
53 struct ptentry *p;
54
55 /* Loop over the parse table entries in t in order to find one that
56 matches the first character in str. */
57 for(p = t; p->c != 0; ++p) {
58 if(*str == p->c) {
59 /* Skip rest of the characters up to the first space. */
60 while(*str != ' ') {
61 ++str;
62 }
63
64 /* Skip all spaces.*/
65 while(*str == ' ') {
66 ++str;
67 }
68
69 /* Call parse table entry function and return. */
70 return p->pfunc(str);
71 }
72 }
73
74 /* Did not find matching entry in parse table. We just call the
75 default handler supplied by the caller and return. */
76 return p->pfunc(str);
77}
78/*-----------------------------------------------------------------------------------*/
79static char *
80skipnewline(char *str)
81{
82 /* Skip all characters until the newline. */
83 while(*str != '\n') {
84 ++str;
85 }
86
87 /* Return a pointer to the first character after the newline. */
88 return str + 1;
89}
90/*-----------------------------------------------------------------------------------*/
91static char *
92nullterminate(char *str)
93{
94 char *nt;
95
96 /* Nullterminate string. Start with finding newline character. */
97 for(nt = str; *nt != '\r' &&
98 *nt != '\n'; ++nt);
99
100 /* Replace newline with a null char. */
101 *nt = 0;
102
103 /* Remove trailing spaces. */
104 while(nt > str && *(nt - 1) == ' ') {
105 *(nt - 1) = 0;
106 --nt;
107 }
108
109 /* Return pointer to null char. */
110 return nt;
111}
112/*-----------------------------------------------------------------------------------*/
113static char *
114loadfile(char *str)
115{
116 char *nt;
117
118 nt = nullterminate(str);
119
120 /* Call loader function. */
121 program_handler_load(str, NULL);
122
123 return nt + 1;
124}
125/*-----------------------------------------------------------------------------------*/
PulkoMandy8fc71d52014-06-28 17:04:50 +0200126#if CTK_CONF_SCREENSAVER
kthacker62e146c2006-04-17 15:11:35 +0000127static char *
128screensaverconf(char *str)
129{
130 char *nt;
131
132 nt = nullterminate(str);
PulkoMandy65a775b2014-06-26 13:30:54 +0200133 program_handler_setscreensaver(str);
kthacker62e146c2006-04-17 15:11:35 +0000134
135 return nt + 1;
136}
PulkoMandy8fc71d52014-06-28 17:04:50 +0200137#endif
kthacker62e146c2006-04-17 15:11:35 +0000138/*-----------------------------------------------------------------------------------*/
139static u16_t addr[2];
140static char *
141ipaddrconf(char *str)
142{
143 char *nt;
144
145 nt = nullterminate(str);
kthacker62e146c2006-04-17 15:11:35 +0000146// if(uiplib_ipaddrconv(str, (unsigned char *)addr)) {
147// uip_sethostaddr(addr);
148// }
149
150 return nt + 1;
151}
152/*-----------------------------------------------------------------------------------*/
153static char *
154netmaskconf(char *str)
155{
156 char *nt;
157
158 nt = nullterminate(str);
159// if(uiplib_ipaddrconv(str, (unsigned char *)addr)) {
160// uip_setnetmask(addr);
161// }
162
163 return nt + 1;
164}
165/*-----------------------------------------------------------------------------------*/
166static char *
167drconf(char *str)
168{
169 char *nt;
170
171 nt = nullterminate(str);
172// if(uiplib_ipaddrconv(str, (unsigned char *)addr)) {
173// uip_setdraddr(addr);
174// }
175
176 return nt + 1;
177}
178/*-----------------------------------------------------------------------------------*/
179static char *
180dnsconf(char *str)
181{
182 char *nt;
183
184 nt = nullterminate(str);
185// if(uiplib_ipaddrconv(str, (unsigned char *)addr)) {
186// resolv_conf(addr);
187// }
188
189 return nt + 1;
190}
191/*-----------------------------------------------------------------------------------*/
192const static struct ptentry configparsetab[] =
193 {{'n', loadfile},
194 {'t', loadfile},
195 {'c', loadfile},
PulkoMandy8fc71d52014-06-28 17:04:50 +0200196#if CTK_CONF_SCREENSAVER
kthacker62e146c2006-04-17 15:11:35 +0000197 {'s', screensaverconf},
PulkoMandy8fc71d52014-06-28 17:04:50 +0200198#endif
kthacker62e146c2006-04-17 15:11:35 +0000199 {'i', ipaddrconf},
200 {'m', netmaskconf},
201 {'r', drconf},
202 {'d', dnsconf},
203 {'#', skipnewline},
204
205 /* Default action */
206 {0, skipnewline}};
207static void
208configscript(void)
209{
210 static char line[40], *lineptr;
211 /* static struct c64_fs_file f;*/
212 int f;
213
214 if((f = cfs_open("config.cfg", 0)) == -1) {
215 return;
216 }
217
218 line[0] = ' ';
219 while(line[0] != '.' &&
220 line[0] != 0) {
221 lineptr = line;
222 do {
223 if(cfs_read(f, lineptr, 1) != 1) {
224 cfs_close(f);
225 return;
226 }
227 ++lineptr;
228 } while(*(lineptr - 1) != '\n' &&
229 *(lineptr - 1) != '\r');
230
231 *lineptr = 0;
232
233 if(line[0] != '.' &&
234 line[0] != 0) {
235 parse(line, configparsetab);
236 }
237
238 }
239 cfs_close(f);
240 return;
241}
242/*-----------------------------------------------------------------------------------*/
243LOADER_INIT_FUNC(config_init, arg)
244{
245 arg_free(arg);
PulkoMandy8fc71d52014-06-28 17:04:50 +0200246#if CTK_CONF_SCREENSAVER
PulkoMandy65a775b2014-06-26 13:30:54 +0200247 program_handler_setscreensaver(NULL);
PulkoMandy8fc71d52014-06-28 17:04:50 +0200248#endif
kthacker62e146c2006-04-17 15:11:35 +0000249 configscript();
250 LOADER_UNLOAD();
251}
252/*-----------------------------------------------------------------------------------*/