adamdunkels | 5ca1327 | 2003-08-06 22:57:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2002, 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 |
| 11 | * copyright notice, this list of conditions and the following |
| 12 | * disclaimer in the documentation and/or other materials provided |
| 13 | * with the distribution. |
| 14 | * 3. All advertising materials mentioning features or use of this |
| 15 | * software must display the following acknowledgement: |
| 16 | * This product includes software developed by Adam Dunkels. |
| 17 | * 4. The name of the author may not be used to endorse or promote |
| 18 | * products derived from this software without specific prior |
| 19 | * written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 22 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 25 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 27 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | * This file is part of the Contiki desktop environment |
| 34 | * |
adamdunkels | e89927e | 2003-08-06 23:14:20 +0000 | [diff] [blame^] | 35 | * $Id: configedit.c,v 1.2 2003/08/06 23:14:20 adamdunkels Exp $ |
adamdunkels | 5ca1327 | 2003-08-06 22:57:05 +0000 | [diff] [blame] | 36 | * |
| 37 | */ |
| 38 | |
| 39 | #include "uip_main.h" |
| 40 | #include "uip.h" |
| 41 | #include "uip_arp.h" |
| 42 | #include "resolv.h" |
| 43 | #include "ctk.h" |
| 44 | #include "ctk-draw.h" |
| 45 | #include "dispatcher.h" |
| 46 | |
| 47 | #include "uip-signal.h" |
| 48 | |
| 49 | #include "program-handler.h" |
| 50 | |
| 51 | #include "c64-fs.h" |
| 52 | |
| 53 | #include "loader.h" |
| 54 | |
| 55 | /* TCP/IP configuration window. */ |
| 56 | static struct ctk_window window; |
| 57 | |
| 58 | static struct ctk_label themelabel = |
| 59 | {CTK_LABEL(0, 1, 9, 1, "CTK theme")}; |
| 60 | static char theme[25]; |
| 61 | static struct ctk_textentry themetextentry = |
| 62 | {CTK_TEXTENTRY(11, 1, 16, 1, theme, 24)}; |
| 63 | |
| 64 | static struct ctk_label driverlabel = |
| 65 | {CTK_LABEL(0, 3, 10, 1, "Net driver")}; |
| 66 | static char driver[25]; |
| 67 | static struct ctk_textentry drivertextentry = |
| 68 | {CTK_TEXTENTRY(11, 3, 16, 1, driver, 24)}; |
| 69 | |
| 70 | |
| 71 | static struct ctk_label ipaddrlabel = |
| 72 | {CTK_LABEL(0, 5, 10, 1, "IP address")}; |
| 73 | static char ipaddr[25]; |
| 74 | static struct ctk_textentry ipaddrtextentry = |
| 75 | {CTK_TEXTENTRY(11, 5, 16, 1, ipaddr, 24)}; |
| 76 | static struct ctk_label netmasklabel = |
| 77 | {CTK_LABEL(0, 7, 10, 1, "Netmask")}; |
| 78 | static char netmask[25]; |
| 79 | static struct ctk_textentry netmasktextentry = |
| 80 | {CTK_TEXTENTRY(11, 7, 16, 1, netmask, 24)}; |
| 81 | static struct ctk_label gatewaylabel = |
| 82 | {CTK_LABEL(0, 9, 10, 1, "Gateway")}; |
| 83 | static char gateway[25]; |
| 84 | static struct ctk_textentry gatewaytextentry = |
| 85 | {CTK_TEXTENTRY(11, 9, 16, 1, gateway, 24)}; |
| 86 | static struct ctk_label dnsserverlabel = |
| 87 | {CTK_LABEL(0, 11, 10, 1, "DNS server")}; |
| 88 | static char dnsserver[25]; |
| 89 | static struct ctk_textentry dnsservertextentry = |
| 90 | {CTK_TEXTENTRY(11, 11, 16, 1, dnsserver, 24)}; |
| 91 | |
| 92 | static struct ctk_button savebutton = |
| 93 | {CTK_BUTTON(0, 13, 12, "Save & close")}; |
| 94 | |
| 95 | |
| 96 | static struct ctk_button applybutton = |
| 97 | {CTK_BUTTON(15, 13, 13, "Apply & close")}; |
| 98 | |
| 99 | static DISPATCHER_SIGHANDLER(configedit_sighandler, s, data); |
| 100 | static struct dispatcher_proc p = |
| 101 | {DISPATCHER_PROC("Config editor", NULL, configedit_sighandler, NULL)}; |
| 102 | static ek_id_t id; |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | /*-----------------------------------------------------------------------------------*/ |
| 108 | struct ptentry { |
| 109 | char c; |
| 110 | char * (* pfunc)(char *str); |
| 111 | }; |
| 112 | |
| 113 | /*-----------------------------------------------------------------------------------*/ |
| 114 | static char * |
| 115 | parse(char *str, struct ptentry *t) |
| 116 | { |
| 117 | struct ptentry *p; |
| 118 | |
| 119 | /* Loop over the parse table entries in t in order to find one that |
| 120 | matches the first character in str. */ |
| 121 | for(p = t; p->c != 0; ++p) { |
| 122 | if(*str == p->c) { |
| 123 | /* Skip rest of the characters up to the first space. */ |
| 124 | while(*str != ' ') { |
| 125 | ++str; |
| 126 | } |
| 127 | |
| 128 | /* Skip all spaces.*/ |
| 129 | while(*str == ' ') { |
| 130 | ++str; |
| 131 | } |
| 132 | |
| 133 | /* Call parse table entry function and return. */ |
| 134 | return p->pfunc(str); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* Did not find matching entry in parse table. We just call the |
| 139 | default handler supplied by the caller and return. */ |
| 140 | return p->pfunc(str); |
| 141 | } |
| 142 | /*-----------------------------------------------------------------------------------*/ |
| 143 | static char * |
| 144 | skipnewline(char *str) |
| 145 | { |
| 146 | /* Skip all characters until the newline. */ |
| 147 | while(*str != '\n') { |
| 148 | ++str; |
| 149 | } |
| 150 | |
| 151 | /* Return a pointer to the first character after the newline. */ |
| 152 | return str + 1; |
| 153 | } |
| 154 | /*-----------------------------------------------------------------------------------*/ |
| 155 | static char * |
| 156 | nullterminate(char *str) |
| 157 | { |
| 158 | char *nt; |
| 159 | |
| 160 | /* Nullterminate string. Start with finding newline character. */ |
| 161 | for(nt = str; *nt != '\r' && |
| 162 | *nt != '\n'; ++nt); |
| 163 | |
| 164 | /* Replace newline with a null char. */ |
| 165 | *nt = 0; |
| 166 | |
| 167 | /* Return pointer to null char. */ |
| 168 | return nt; |
| 169 | } |
| 170 | /*-----------------------------------------------------------------------------------*/ |
| 171 | static char * |
| 172 | loaddriver(char *str) |
| 173 | { |
| 174 | char *nt = nullterminate(str); |
| 175 | strncpy(driver, str, sizeof(driver)); |
| 176 | return nt + 1; |
| 177 | } |
| 178 | /*-----------------------------------------------------------------------------------*/ |
| 179 | static char * |
| 180 | loadtheme(char *str) |
| 181 | { |
| 182 | char *nt = nullterminate(str); |
| 183 | strncpy(theme, str, sizeof(theme)); |
| 184 | return nt + 1; |
| 185 | } |
| 186 | /*-----------------------------------------------------------------------------------*/ |
| 187 | static char * |
| 188 | ipaddrconf(char *str) |
| 189 | { |
| 190 | char *nt = nullterminate(str); |
| 191 | strncpy(ipaddr, str, sizeof(ipaddr)); |
| 192 | return nt + 1; |
| 193 | } |
| 194 | /*-----------------------------------------------------------------------------------*/ |
| 195 | static char * |
| 196 | netmaskconf(char *str) |
| 197 | { |
| 198 | char *nt = nullterminate(str); |
| 199 | strncpy(netmask, str, sizeof(netmask)); |
| 200 | return nt + 1; |
| 201 | } |
| 202 | /*-----------------------------------------------------------------------------------*/ |
| 203 | static char * |
| 204 | drconf(char *str) |
| 205 | { |
| 206 | char *nt = nullterminate(str); |
| 207 | strncpy(gateway, str, sizeof(gateway)); |
| 208 | return nt + 1; |
| 209 | } |
| 210 | /*-----------------------------------------------------------------------------------*/ |
| 211 | static char * |
| 212 | dnsconf(char *str) |
| 213 | { |
| 214 | char *nt = nullterminate(str); |
| 215 | strncpy(dnsserver, str, sizeof(dnsserver)); |
| 216 | return nt + 1; |
| 217 | } |
| 218 | /*-----------------------------------------------------------------------------------*/ |
| 219 | static struct ptentry initparsetab[] = |
| 220 | {{'l', loaddriver}, |
| 221 | {'L', loaddriver}, |
| 222 | {'t', loadtheme}, |
| 223 | {'T', loadtheme}, |
| 224 | {'i', ipaddrconf}, |
| 225 | {'I', ipaddrconf}, |
| 226 | {'n', netmaskconf}, |
| 227 | {'N', netmaskconf}, |
| 228 | {'r', drconf}, |
| 229 | {'R', drconf}, |
| 230 | {'d', dnsconf}, |
| 231 | {'D', dnsconf}, |
| 232 | {'#', skipnewline}, |
| 233 | |
| 234 | /* Default action */ |
| 235 | {0, skipnewline}}; |
| 236 | static void |
| 237 | initscript(void) |
| 238 | { |
| 239 | char line[40], *lineptr; |
| 240 | struct c64_fs_file f; |
| 241 | |
adamdunkels | e89927e | 2003-08-06 23:14:20 +0000 | [diff] [blame^] | 242 | if(c64_fs_open("config.cfg", &f) == -1) { |
adamdunkels | 5ca1327 | 2003-08-06 22:57:05 +0000 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | line[0] = ' '; |
| 246 | while(line[0] != '.' && |
| 247 | line[0] != 0) { |
| 248 | lineptr = line; |
| 249 | do { |
| 250 | if(c64_fs_read(&f, lineptr, 1) != 1) { |
| 251 | c64_fs_close(&f); |
| 252 | return; |
| 253 | } |
| 254 | ++lineptr; |
| 255 | } while(*(lineptr - 1) != '\n' && |
| 256 | *(lineptr - 1) != '\r'); |
| 257 | |
| 258 | *lineptr = 0; |
| 259 | |
| 260 | if(line[0] != '.' && |
| 261 | line[0] != 0) { |
| 262 | parse(line, initparsetab); |
| 263 | } |
| 264 | |
| 265 | } |
| 266 | c64_fs_close(&f); |
| 267 | return; |
| 268 | } |
| 269 | /*-----------------------------------------------------------------------------------*/ |
| 270 | static void |
| 271 | savescript(void) |
| 272 | { |
| 273 | char line[40]; |
| 274 | struct c64_fs_file f; |
| 275 | int len; |
| 276 | |
adamdunkels | e89927e | 2003-08-06 23:14:20 +0000 | [diff] [blame^] | 277 | if(c64_fs_open("config.cfg", &f) == -1) { |
adamdunkels | 5ca1327 | 2003-08-06 22:57:05 +0000 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | if(theme[0] != 0) { |
| 281 | sprintf(line, "t %s\n", theme); |
| 282 | len = strlen(line); |
| 283 | c64_fs_write(&f, line, len); |
| 284 | } |
| 285 | if(driver[0] != 0) { |
| 286 | sprintf(line, "l %s\n", driver); |
| 287 | len = strlen(line); |
| 288 | c64_fs_write(&f, line, len); |
| 289 | } |
| 290 | if(ipaddr[0] != 0) { |
| 291 | sprintf(line, "i %s\n", ipaddr); |
| 292 | len = strlen(line); |
| 293 | c64_fs_write(&f, line, len); |
| 294 | } |
| 295 | if(netmask[0] != 0) { |
| 296 | sprintf(line, "n %s\n", netmask); |
| 297 | len = strlen(line); |
| 298 | c64_fs_write(&f, line, len); |
| 299 | } |
| 300 | if(gateway[0] != 0) { |
| 301 | sprintf(line, "r %s\n", gateway); |
| 302 | len = strlen(line); |
| 303 | c64_fs_write(&f, line, len); |
| 304 | } |
| 305 | if(dnsserver[0] != 0) { |
| 306 | sprintf(line, "d %s\n", dnsserver); |
| 307 | len = strlen(line); |
| 308 | c64_fs_write(&f, line, len); |
| 309 | } |
| 310 | |
| 311 | sprintf(line, ".\n\0\n\n\n"); |
| 312 | len = strlen(line); |
| 313 | c64_fs_write(&f, line, len); |
| 314 | |
| 315 | c64_fs_close(&f); |
| 316 | |
| 317 | } |
| 318 | /*-----------------------------------------------------------------------------------*/ |
| 319 | LOADER_INIT_FUNC(configedit_init) |
| 320 | { |
| 321 | if(id == EK_ID_NONE) { |
| 322 | id = dispatcher_start(&p); |
| 323 | |
| 324 | /* Create window. */ |
| 325 | ctk_window_new(&window, 30, 14, "Config editor"); |
| 326 | |
| 327 | CTK_WIDGET_ADD(&window, &themelabel); |
| 328 | CTK_WIDGET_ADD(&window, &themetextentry); |
| 329 | |
| 330 | CTK_WIDGET_ADD(&window, &driverlabel); |
| 331 | CTK_WIDGET_ADD(&window, &drivertextentry); |
| 332 | |
| 333 | CTK_WIDGET_ADD(&window, &ipaddrlabel); |
| 334 | CTK_WIDGET_ADD(&window, &ipaddrtextentry); |
| 335 | CTK_WIDGET_ADD(&window, &netmasklabel); |
| 336 | CTK_WIDGET_ADD(&window, &netmasktextentry); |
| 337 | CTK_WIDGET_ADD(&window, &gatewaylabel); |
| 338 | CTK_WIDGET_ADD(&window, &gatewaytextentry); |
| 339 | CTK_WIDGET_ADD(&window, &dnsserverlabel); |
| 340 | CTK_WIDGET_ADD(&window, &dnsservertextentry); |
| 341 | |
| 342 | CTK_WIDGET_ADD(&window, &savebutton); |
| 343 | CTK_WIDGET_ADD(&window, &applybutton); |
| 344 | |
| 345 | CTK_WIDGET_FOCUS(&window, &themetextentry); |
| 346 | |
| 347 | /* Fill the configuration strings with values from the current |
| 348 | configuration */ |
| 349 | initscript(); |
| 350 | |
| 351 | dispatcher_listen(ctk_signal_button_activate); |
| 352 | dispatcher_listen(ctk_signal_window_close); |
| 353 | } |
| 354 | ctk_window_open(&window); |
| 355 | } |
| 356 | /*-----------------------------------------------------------------------------------*/ |
| 357 | |
| 358 | /*-----------------------------------------------------------------------------------*/ |
| 359 | static void |
| 360 | configedit_quit(void) |
| 361 | { |
| 362 | dispatcher_exit(&p); |
| 363 | id = EK_ID_NONE; |
| 364 | LOADER_UNLOAD(); |
| 365 | } |
| 366 | /*-----------------------------------------------------------------------------------*/ |
| 367 | static |
| 368 | DISPATCHER_SIGHANDLER(configedit_sighandler, s, data) |
| 369 | { |
| 370 | DISPATCHER_SIGHANDLER_ARGS(s, data); |
| 371 | |
| 372 | if(s == ctk_signal_button_activate) { |
| 373 | if(data == (ek_data_t)&savebutton) { |
| 374 | savescript(); |
| 375 | ctk_window_close(&window); |
| 376 | configedit_quit(); |
| 377 | } else if(data == (ek_data_t)&applybutton) { |
| 378 | savescript(); |
| 379 | ctk_window_close(&window); |
| 380 | configedit_quit(); |
| 381 | dispatcher_emit(uip_signal_uninstall, NULL, |
| 382 | DISPATCHER_BROADCAST); |
adamdunkels | e89927e | 2003-08-06 23:14:20 +0000 | [diff] [blame^] | 383 | program_handler_load("config.prg"); |
adamdunkels | 5ca1327 | 2003-08-06 22:57:05 +0000 | [diff] [blame] | 384 | } |
| 385 | } else if(s == ctk_signal_window_close || |
| 386 | s == dispatcher_signal_quit) { |
| 387 | ctk_window_close(&window); |
| 388 | configedit_quit(); |
| 389 | } |
| 390 | } |
| 391 | /*-----------------------------------------------------------------------------------*/ |