kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 44 | struct ptentry { |
| 45 | char c; |
| 46 | char * (* pfunc)(char *str); |
| 47 | }; |
| 48 | |
| 49 | /*-----------------------------------------------------------------------------------*/ |
| 50 | static char * |
| 51 | parse(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 | /*-----------------------------------------------------------------------------------*/ |
| 79 | static char * |
| 80 | skipnewline(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 | /*-----------------------------------------------------------------------------------*/ |
| 91 | static char * |
| 92 | nullterminate(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 | /*-----------------------------------------------------------------------------------*/ |
| 113 | static char * |
| 114 | loadfile(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 | /*-----------------------------------------------------------------------------------*/ |
PulkoMandy | 8fc71d5 | 2014-06-28 17:04:50 +0200 | [diff] [blame] | 126 | #if CTK_CONF_SCREENSAVER |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 127 | static char * |
| 128 | screensaverconf(char *str) |
| 129 | { |
| 130 | char *nt; |
| 131 | |
| 132 | nt = nullterminate(str); |
PulkoMandy | 65a775b | 2014-06-26 13:30:54 +0200 | [diff] [blame] | 133 | program_handler_setscreensaver(str); |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 134 | |
| 135 | return nt + 1; |
| 136 | } |
PulkoMandy | 8fc71d5 | 2014-06-28 17:04:50 +0200 | [diff] [blame] | 137 | #endif |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 138 | /*-----------------------------------------------------------------------------------*/ |
| 139 | static u16_t addr[2]; |
| 140 | static char * |
| 141 | ipaddrconf(char *str) |
| 142 | { |
| 143 | char *nt; |
| 144 | |
| 145 | nt = nullterminate(str); |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 146 | // if(uiplib_ipaddrconv(str, (unsigned char *)addr)) { |
| 147 | // uip_sethostaddr(addr); |
| 148 | // } |
| 149 | |
| 150 | return nt + 1; |
| 151 | } |
| 152 | /*-----------------------------------------------------------------------------------*/ |
| 153 | static char * |
| 154 | netmaskconf(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 | /*-----------------------------------------------------------------------------------*/ |
| 166 | static char * |
| 167 | drconf(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 | /*-----------------------------------------------------------------------------------*/ |
| 179 | static char * |
| 180 | dnsconf(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 | /*-----------------------------------------------------------------------------------*/ |
| 192 | const static struct ptentry configparsetab[] = |
| 193 | {{'n', loadfile}, |
| 194 | {'t', loadfile}, |
| 195 | {'c', loadfile}, |
PulkoMandy | 8fc71d5 | 2014-06-28 17:04:50 +0200 | [diff] [blame] | 196 | #if CTK_CONF_SCREENSAVER |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 197 | {'s', screensaverconf}, |
PulkoMandy | 8fc71d5 | 2014-06-28 17:04:50 +0200 | [diff] [blame] | 198 | #endif |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 199 | {'i', ipaddrconf}, |
| 200 | {'m', netmaskconf}, |
| 201 | {'r', drconf}, |
| 202 | {'d', dnsconf}, |
| 203 | {'#', skipnewline}, |
| 204 | |
| 205 | /* Default action */ |
| 206 | {0, skipnewline}}; |
| 207 | static void |
| 208 | configscript(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 | /*-----------------------------------------------------------------------------------*/ |
| 243 | LOADER_INIT_FUNC(config_init, arg) |
| 244 | { |
| 245 | arg_free(arg); |
PulkoMandy | 8fc71d5 | 2014-06-28 17:04:50 +0200 | [diff] [blame] | 246 | #if CTK_CONF_SCREENSAVER |
PulkoMandy | 65a775b | 2014-06-26 13:30:54 +0200 | [diff] [blame] | 247 | program_handler_setscreensaver(NULL); |
PulkoMandy | 8fc71d5 | 2014-06-28 17:04:50 +0200 | [diff] [blame] | 248 | #endif |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 249 | configscript(); |
| 250 | LOADER_UNLOAD(); |
| 251 | } |
| 252 | /*-----------------------------------------------------------------------------------*/ |