blob: f3bb6052e6e6c148455b998f94b1ce3012d69962 [file] [log] [blame]
kthacker62e146c2006-04-17 15:11:35 +00001/*
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. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * This file is part of the Contiki desktop environment
31 *
32 * $Id: configedit.c,v 1.1 2006/04/17 15:18:16 kthacker Exp $
33 *
34 */
35
36#include "contiki.h"
37
38#include "ctk-draw-service.h"
39#include "packet-service.h"
40
41#include "uip.h"
42#include "uip_arp.h"
43#include "resolv.h"
44#include "ctk.h"
45#include "ctk-draw.h"
46
47#include "program-handler.h"
48
49#include "cfs.h"
50
51//#include "cfs-init.h"
52#include "cfs-cpc.h"
53
54#include "loader.h"
55
56#include "log.h"
57
58
59#include <stdio.h>
60
61#include <string.h>
62
63/* TCP/IP configuration window. */
64static struct ctk_window window;
65
66#define LABELMAXWIDTH 12
67
68const static struct ctk_label cfslabel =
69 {CTK_LABEL(0, 1, LABELMAXWIDTH, 1, "Disk driver")};
70static char cfs[25];
71static struct ctk_textentry cfstextentry =
72 {CTK_TEXTENTRY(LABELMAXWIDTH + 1, 1, 16, 1, cfs, 24)};
73
74const static struct ctk_label themelabel =
75 {CTK_LABEL(0, 3, LABELMAXWIDTH, 1, "CTK theme")};
76static char theme[25];
77static struct ctk_textentry themetextentry =
78 {CTK_TEXTENTRY(LABELMAXWIDTH + 1, 3, 16, 1, theme, 24)};
79
80const static struct ctk_label driverlabel =
81 {CTK_LABEL(0, 5, LABELMAXWIDTH, 1, "Net driver")};
82static char driver[25];
83static struct ctk_textentry drivertextentry =
84 {CTK_TEXTENTRY(LABELMAXWIDTH + 1, 5, 16, 1, driver, 24)};
85
86const static struct ctk_label screensaverlabel =
87 {CTK_LABEL(0, 7, LABELMAXWIDTH, 1, "Screensaver")};
88static char screensaver[25];
89struct ctk_textentry screensavertextentry =
90 {CTK_TEXTENTRY(LABELMAXWIDTH + 1, 7, 16, 1, screensaver, 24)};
91
92
93const static struct ctk_label ipaddrlabel =
94 {CTK_LABEL(0, 9, LABELMAXWIDTH, 1, "IP address")};
95static char ipaddr[25];
96struct ctk_textentry ipaddrtextentry =
97 {CTK_TEXTENTRY(LABELMAXWIDTH + 1, 9, 16, 1, ipaddr, 24)};
98const static struct ctk_label netmasklabel =
99 {CTK_LABEL(0, 11, LABELMAXWIDTH, 1, "Netmask")};
100static char netmask[25];
101static struct ctk_textentry netmasktextentry =
102 {CTK_TEXTENTRY(LABELMAXWIDTH + 1, 11, 16, 1, netmask, 24)};
103const static struct ctk_label gatewaylabel =
104 {CTK_LABEL(0, 13, LABELMAXWIDTH, 1, "Gateway")};
105static char gateway[25];
106static struct ctk_textentry gatewaytextentry =
107 {CTK_TEXTENTRY(LABELMAXWIDTH + 1, 13, 16, 1, gateway, 24)};
108const static struct ctk_label dnsserverlabel =
109 {CTK_LABEL(0, 15, LABELMAXWIDTH, 1, "DNS server")};
110static char dnsserver[25];
111static struct ctk_textentry dnsservertextentry =
112 {CTK_TEXTENTRY(LABELMAXWIDTH + 1, 15, 16, 1, dnsserver, 24)};
113
114const static struct ctk_button savebutton =
115 {CTK_BUTTON(0, 17, 12, "Save & close")};
116const static struct ctk_button cancelbutton =
117 {CTK_BUTTON(24, 17, 6, "Cancel")};
118
119/*static DISPATCHER_SIGHANDLER(configedit_sighandler, s, data);
120static struct dispatcher_proc p =
121 {DISPATCHER_PROC("Config editor", NULL, configedit_sighandler, NULL)};
122 static ek_id_t id;*/
123
124EK_EVENTHANDLER(configedit_eventhandler, ev, data);
125EK_PROCESS(p, "Config editor", EK_PRIO_NORMAL,
126 configedit_eventhandler, NULL, NULL);
127static ek_id_t id = EK_ID_NONE;
128
129
130/*-----------------------------------------------------------------------------------*/
131struct ptentry {
132 char c;
133 char * (* pfunc)(char *str);
134};
135
136/*-----------------------------------------------------------------------------------*/
137static char *
138parse(char *str, struct ptentry *t)
139{
140 struct ptentry *p;
141
142 /* Loop over the parse table entries in t in order to find one that
143 matches the first character in str. */
144 for(p = t; p->c != 0; ++p) {
145 if(*str == p->c) {
146 /* Skip rest of the characters up to the first space. */
147 while(*str != ' ') {
148 ++str;
149 }
150
151 /* Skip all spaces.*/
152 while(*str == ' ') {
153 ++str;
154 }
155
156 /* Call parse table entry function and return. */
157 return p->pfunc(str);
158 }
159 }
160
161 /* Did not find matching entry in parse table. We just call the
162 default handler supplied by the caller and return. */
163 return p->pfunc(str);
164}
165/*-----------------------------------------------------------------------------------*/
166static char *
167skipnewline(char *str)
168{
169 /* Skip all characters until the newline. */
170 while(*str != '\n') {
171 ++str;
172 }
173
174 /* Return a pointer to the first character after the newline. */
175 return str + 1;
176}
177/*-----------------------------------------------------------------------------------*/
178static char *
179nullterminate(char *str)
180{
181 char *nt;
182
183 /* Nullterminate string. Start with finding newline character. */
184 for(nt = str; *nt != '\r' &&
185 *nt != '\n'; ++nt);
186
187 /* Replace newline with a null char. */
188 *nt = 0;
189
190 /* Remove trailing spaces. */
191 while(nt > str && *(nt - 1) == ' ') {
192 *(nt - 1) = 0;
193 --nt;
194 }
195
196 /* Return pointer to null char. */
197 return nt;
198}
199/*-----------------------------------------------------------------------------------*/
Adrien Destugues0ff774a2016-01-22 23:27:44 +0100200static char *
kthacker62e146c2006-04-17 15:11:35 +0000201copystr(char *dst, char *src, int len)
202{
203 char *nt = nullterminate(src);
204 strncpy(dst, src, len);
205 return nt + 1;
206}
207/*-----------------------------------------------------------------------------------*/
208static char *
209loaddriver(char *str)
210{
211 return copystr(driver, str, sizeof(driver));
212}
213/*-----------------------------------------------------------------------------------*/
214static char *
215loadtheme(char *str)
216{
217 return copystr(theme, str, sizeof(theme));
218}
219/*-----------------------------------------------------------------------------------*/
220static char *
221loadcfs(char *str)
222{
223 return copystr(cfs, str, sizeof(cfs));
224}
225/*-----------------------------------------------------------------------------------*/
226static char *
227loadscreensaver(char *str)
228{
229 return copystr(screensaver, str, sizeof(screensaver));
230}
231/*-----------------------------------------------------------------------------------*/
232static char *
233ipaddrconf(char *str)
234{
235 return copystr(ipaddr, str, sizeof(ipaddr));
236}
237/*-----------------------------------------------------------------------------------*/
238static char *
239netmaskconf(char *str)
240{
241 return copystr(netmask, str, sizeof(netmask));
242}
243/*-----------------------------------------------------------------------------------*/
244static char *
245drconf(char *str)
246{
247 return copystr(gateway, str, sizeof(gateway));
248}
249/*-----------------------------------------------------------------------------------*/
250static char *
251dnsconf(char *str)
252{
253 return copystr(dnsserver, str, sizeof(dnsserver));
254}
255/*-----------------------------------------------------------------------------------*/
256const static struct ptentry initparsetab[] =
257 {{'n', loaddriver},
258 {'t', loadtheme},
259 {'c', loadcfs},
260 {'s', loadscreensaver},
261 {'i', ipaddrconf},
262 {'m', netmaskconf},
263 {'r', drconf},
264 {'d', dnsconf},
265 {'#', skipnewline},
266
267 /* Default action */
268 {0, skipnewline}};
269static void
270initscript(void)
271{
272 char line[40], *lineptr;
273 /* struct c64_fs_file f;*/
274 int f;
275
276 if((f = cfs_open("config.cfg", 0)) == -1) {
277 return;
278 }
279 line[0] = ' ';
280 while(line[0] != '.' &&
281 line[0] != 0) {
282 lineptr = line;
283 do {
284 if(cfs_read(f, lineptr, 1) != 1) {
285 cfs_close(f);
286 return;
287 }
288 ++lineptr;
289 } while(*(lineptr - 1) != '\n' &&
290 *(lineptr - 1) != '\r');
291
292 *lineptr = 0;
293
294 if(line[0] != '.' &&
295 line[0] != 0) {
296 parse(line, initparsetab);
297 }
298
299 }
300 cfs_close(f);
301 return;
302}
303/*-----------------------------------------------------------------------------------*/
304static int makeline(char *line, char c, char *str)
305{
306 int len;
307
308 len = strlen(str);
309
310 line[0] = c;
311 line[1] = ' ';
312 strcpy(&line[2], str);
313 line[2 + len] = '\n';
314 line[3 + len] = 0;
315 return len + 3;
316}
317
318#if 0
319struct scriptvar
320{
321 const char *var;
322 char chr;
323};
324
325cstatic struct scriptvar scriptvars[]=
326{
327{cfs,'c'},
328
329
330};
331#endif
332
333
334/*-----------------------------------------------------------------------------------*/
335static void
336savescript(void)
337{
338 char line[40];
339 /* struct c64_fs_file f;*/
340 int f;
341
342 f = cfs_open("@:config.cfg", CFS_WRITE);
343 if(f == -1) {
PulkoMandy81050b82023-05-06 22:16:49 +0200344 //log_message("Could not open config.cfg", "");
kthacker62e146c2006-04-17 15:11:35 +0000345 return;
346 }
347 if(cfs[0] != 0) {
348 int len = makeline(line, 'c',cfs);
349 cfs_write(f, line, len);
350// cfs_write(f, line, makeline(line, 'c', cfs));
351 }
352 if(theme[0] != 0) {
353 int len = makeline(line, 't',theme);
354 cfs_write(f, line, len);
355// cfs_write(f, line, makeline(line, 't', theme));
356 }
357 if(driver[0] != 0) {
358 int len = makeline(line, 'n',driver);
359 cfs_write(f, line, len);
360// cfs_write(f, line, makeline(line, 'n', driver));
361 }
362 if(ipaddr[0] != 0) {
363 int len = makeline(line, 'i',ipaddr);
364 cfs_write(f, line, len);
365// cfs_write(f, line, makeline(line, 'i', ipaddr));
366 }
367 if(netmask[0] != 0) {
368 int len = makeline(line, 'm',netmask);
369 cfs_write(f, line, len);
370// cfs_write(f, line, makeline(line, 'm', netmask));
371 }
372 if(gateway[0] != 0) {
373 int len = makeline(line, 'r',gateway);
374 cfs_write(f, line, len);
375// cfs_write(f, line, makeline(line, 'r', gateway));
376 }
377 if(dnsserver[0] != 0) {
378 int len = makeline(line, 'd',dnsserver);
379 cfs_write(f, line, len);
380// cfs_write(f, line, makeline(line, 'd', dnsserver));
381 }
382
383 if(screensaver[0] != 0) {
384 int len = makeline(line, 's',screensaver);
385 cfs_write(f, line, len);
386// cfs_write(f, line, makeline(line, 's', screensaver));
387 }
388
389 strcpy(line, ".\n\0\n\n\n");
390 cfs_write(f, line, strlen(line));
391
392 cfs_close(f);
393
394}
395/*-----------------------------------------------------------------------------------*/
396LOADER_INIT_FUNC(configedit_init, arg)
397{
398 arg_free(arg);
399
400 if(id == EK_ID_NONE) {
401 id = ek_start(&p);
402 }
403}
404/*-----------------------------------------------------------------------------------*/
405
406/*-----------------------------------------------------------------------------------*/
407static void
408configedit_quit(void)
409{
410 ek_exit();
411 id = EK_ID_NONE;
412 LOADER_UNLOAD();
413}
414/*-----------------------------------------------------------------------------------*/
415static void
416quit_services(void)
417{
418 cfs_cpc_init(NULL);
419 ctk_draw_quit();
420}
421/*-----------------------------------------------------------------------------------*/
422EK_EVENTHANDLER(configedit_eventhandler, ev, data)
423{
424 EK_EVENTHANDLER_ARGS(ev, data);
425
426 if(ev == EK_EVENT_INIT) {
427 /* Create window. */
428 ctk_window_new(&window, 32, 18, "Config editor");
429
430 CTK_WIDGET_ADD(&window, &cfslabel);
431 CTK_WIDGET_ADD(&window, &cfstextentry);
432 CTK_TEXTENTRY_CLEAR(&cfstextentry);
433
434 CTK_WIDGET_ADD(&window, &themelabel);
435 CTK_WIDGET_ADD(&window, &themetextentry);
436 CTK_TEXTENTRY_CLEAR(&themetextentry);
437
438 CTK_WIDGET_ADD(&window, &driverlabel);
439 CTK_WIDGET_ADD(&window, &drivertextentry);
440 CTK_TEXTENTRY_CLEAR(&drivertextentry);
441
442 CTK_WIDGET_ADD(&window, &screensaverlabel);
443 CTK_WIDGET_ADD(&window, &screensavertextentry);
444 CTK_TEXTENTRY_CLEAR(&screensavertextentry);
445
446 CTK_WIDGET_ADD(&window, &ipaddrlabel);
447 CTK_WIDGET_ADD(&window, &ipaddrtextentry);
448 CTK_TEXTENTRY_CLEAR(&ipaddrtextentry);
449
450 CTK_WIDGET_ADD(&window, &netmasklabel);
451 CTK_WIDGET_ADD(&window, &netmasktextentry);
452 CTK_TEXTENTRY_CLEAR(&netmasktextentry);
453
454 CTK_WIDGET_ADD(&window, &gatewaylabel);
455 CTK_WIDGET_ADD(&window, &gatewaytextentry);
456 CTK_TEXTENTRY_CLEAR(&gatewaytextentry);
457
458 CTK_WIDGET_ADD(&window, &dnsserverlabel);
459 CTK_WIDGET_ADD(&window, &dnsservertextentry);
460 CTK_TEXTENTRY_CLEAR(&dnsservertextentry);
461
462
463 CTK_WIDGET_ADD(&window, &savebutton);
464 CTK_WIDGET_ADD(&window, &cancelbutton);
465
466 CTK_WIDGET_FOCUS(&window, &cfstextentry);
467
468 /* Fill the configuration strings with values from the current
469 configuration */
470 initscript();
471
472 ctk_window_open(&window);
473
474 } else if(ev == ctk_signal_button_activate) {
475 if(data == (ek_data_t)&savebutton) {
476 savescript();
477 quit_services();
478 ctk_window_close(&window);
479 configedit_quit();
480 program_handler_load("config.prg", NULL);
481 } else if(data == (ek_data_t)&cancelbutton) {
482 ctk_window_close(&window);
483 configedit_quit();
484 }
485 } else if(ev == ctk_signal_window_close ||
486 ev == EK_EVENT_REQUEST_EXIT) {
487 ctk_window_close(&window);
488 configedit_quit();
489 }
490}
491/*-----------------------------------------------------------------------------------*/