blob: 8fe28143b48f23eda424a1dbab69f5cb196210ea [file] [log] [blame]
adamdunkelscd8acbe2003-08-06 22:54:39 +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.
adamdunkels5abe18e2004-08-09 21:00:28 +000013 * 3. The name of the author may not be used to endorse or promote
adamdunkelscd8acbe2003-08-06 22:54:39 +000014 * 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 *
oliverschmidt5b229762005-05-12 21:26:46 +000031 * $Id: config.c,v 1.11 2005/05/12 21:26:46 oliverschmidt Exp $
adamdunkelscd8acbe2003-08-06 22:54:39 +000032 *
33 */
34
35#include "program-handler.h"
36#include "loader.h"
adamdunkels5abe18e2004-08-09 21:00:28 +000037#include "cfs.h"
adamdunkelscd8acbe2003-08-06 22:54:39 +000038#include "uip.h"
adamdunkels2658b902004-02-24 09:53:44 +000039#include "uiplib.h"
adamdunkelscd8acbe2003-08-06 22:54:39 +000040#include "uip_arp.h"
41#include "resolv.h"
42
adamdunkelsb6006842003-08-20 19:51:00 +000043
adamdunkelscd8acbe2003-08-06 22:54:39 +000044struct 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
adamdunkelsb6006842003-08-20 19:51:00 +0000103 /* Remove trailing spaces. */
104 while(nt > str && *(nt - 1) == ' ') {
105 *(nt - 1) = 0;
106 --nt;
107 }
108
adamdunkelscd8acbe2003-08-06 22:54:39 +0000109 /* 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. */
adamdunkels7cde6092003-08-24 22:35:22 +0000121 program_handler_load(str, NULL);
adamdunkelscd8acbe2003-08-06 22:54:39 +0000122
123 return nt + 1;
124}
125/*-----------------------------------------------------------------------------------*/
adamdunkels93d8ea72003-08-09 23:27:57 +0000126static char *
127screensaverconf(char *str)
128{
129 char *nt;
130
131 nt = nullterminate(str);
oliverschmidt5b229762005-05-12 21:26:46 +0000132 program_handler_setscreensaver(str);
adamdunkels93d8ea72003-08-09 23:27:57 +0000133
134 return nt + 1;
135}
136/*-----------------------------------------------------------------------------------*/
adamdunkelscd8acbe2003-08-06 22:54:39 +0000137static u16_t addr[2];
138static char *
139ipaddrconf(char *str)
140{
141 char *nt;
142
143 nt = nullterminate(str);
adamdunkels2658b902004-02-24 09:53:44 +0000144 if(uiplib_ipaddrconv(str, (unsigned char *)addr)) {
adamdunkelscd8acbe2003-08-06 22:54:39 +0000145 uip_sethostaddr(addr);
146 }
147
148 return nt + 1;
149}
150/*-----------------------------------------------------------------------------------*/
151static char *
152netmaskconf(char *str)
153{
154 char *nt;
155
156 nt = nullterminate(str);
adamdunkels2658b902004-02-24 09:53:44 +0000157 if(uiplib_ipaddrconv(str, (unsigned char *)addr)) {
adamdunkelscd8acbe2003-08-06 22:54:39 +0000158 uip_setnetmask(addr);
159 }
160
161 return nt + 1;
162}
163/*-----------------------------------------------------------------------------------*/
164static char *
165drconf(char *str)
166{
167 char *nt;
168
169 nt = nullterminate(str);
adamdunkels2658b902004-02-24 09:53:44 +0000170 if(uiplib_ipaddrconv(str, (unsigned char *)addr)) {
adamdunkelscd8acbe2003-08-06 22:54:39 +0000171 uip_setdraddr(addr);
172 }
173
174 return nt + 1;
175}
176/*-----------------------------------------------------------------------------------*/
177static char *
178dnsconf(char *str)
179{
180 char *nt;
181
182 nt = nullterminate(str);
adamdunkels2658b902004-02-24 09:53:44 +0000183 if(uiplib_ipaddrconv(str, (unsigned char *)addr)) {
adamdunkelscd8acbe2003-08-06 22:54:39 +0000184 resolv_conf(addr);
185 }
186
187 return nt + 1;
188}
189/*-----------------------------------------------------------------------------------*/
190static struct ptentry configparsetab[] =
adamdunkels0ec151b2004-09-12 13:36:02 +0000191 {{'n', loadfile},
adamdunkelscd8acbe2003-08-06 22:54:39 +0000192 {'t', loadfile},
adamdunkels0ec151b2004-09-12 13:36:02 +0000193 {'c', loadfile},
adamdunkels93d8ea72003-08-09 23:27:57 +0000194 {'s', screensaverconf},
adamdunkelscd8acbe2003-08-06 22:54:39 +0000195 {'i', ipaddrconf},
adamdunkelsb6006842003-08-20 19:51:00 +0000196 {'m', netmaskconf},
adamdunkelscd8acbe2003-08-06 22:54:39 +0000197 {'r', drconf},
adamdunkelscd8acbe2003-08-06 22:54:39 +0000198 {'d', dnsconf},
adamdunkelscd8acbe2003-08-06 22:54:39 +0000199 {'#', skipnewline},
200
201 /* Default action */
202 {0, skipnewline}};
203static void
204configscript(void)
205{
206 static char line[40], *lineptr;
adamdunkels5abe18e2004-08-09 21:00:28 +0000207 /* static struct c64_fs_file f;*/
208 int f;
adamdunkelscd8acbe2003-08-06 22:54:39 +0000209
adamdunkels5abe18e2004-08-09 21:00:28 +0000210 if((f = cfs_open("config.cfg", 0)) == -1) {
adamdunkelscd8acbe2003-08-06 22:54:39 +0000211 return;
212 }
adamdunkels5abe18e2004-08-09 21:00:28 +0000213
adamdunkelscd8acbe2003-08-06 22:54:39 +0000214 line[0] = ' ';
215 while(line[0] != '.' &&
216 line[0] != 0) {
217 lineptr = line;
218 do {
adamdunkels5abe18e2004-08-09 21:00:28 +0000219 if(cfs_read(f, lineptr, 1) != 1) {
220 cfs_close(f);
adamdunkelscd8acbe2003-08-06 22:54:39 +0000221 return;
222 }
223 ++lineptr;
224 } while(*(lineptr - 1) != '\n' &&
225 *(lineptr - 1) != '\r');
226
227 *lineptr = 0;
228
229 if(line[0] != '.' &&
230 line[0] != 0) {
231 parse(line, configparsetab);
232 }
233
234 }
adamdunkels5abe18e2004-08-09 21:00:28 +0000235 cfs_close(f);
adamdunkelscd8acbe2003-08-06 22:54:39 +0000236 return;
237}
238/*-----------------------------------------------------------------------------------*/
adamdunkels7cde6092003-08-24 22:35:22 +0000239LOADER_INIT_FUNC(config_init, arg)
adamdunkelscd8acbe2003-08-06 22:54:39 +0000240{
adamdunkels7cde6092003-08-24 22:35:22 +0000241 arg_free(arg);
oliverschmidt5b229762005-05-12 21:26:46 +0000242 program_handler_setscreensaver(NULL);
adamdunkelscd8acbe2003-08-06 22:54:39 +0000243 configscript();
244 LOADER_UNLOAD();
245}
246/*-----------------------------------------------------------------------------------*/