| /* |
| * Copyright (c) 2003, Adam Dunkels. |
| * All rights reserved. |
| * |
| * Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions |
| * are met: |
| * 1. Redistributions of source code must retain the above copyright |
| * notice, this list of conditions and the following disclaimer. |
| * 2. Redistributions in binary form must reproduce the above copyright |
| * notice, this list of conditions and the following disclaimer in the |
| * documentation and/or other materials provided with the distribution. |
| * 3. All advertising materials mentioning features or use of this software |
| * must display the following acknowledgement: |
| * This product includes software developed by Adam Dunkels. |
| * 4. The name of the author may not be used to endorse or promote |
| * products derived from this software without specific prior |
| * written permission. |
| * |
| * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| * |
| * This file is part of the Contiki desktop OS. |
| * |
| * $Id: init.c,v 1.1 2003/08/05 22:02:16 adamdunkels Exp $ |
| * |
| */ |
| |
| #include "program-handler.h" |
| #include "loader.h" |
| #include "c64-fs.h" |
| |
| struct ptentry { |
| char c; |
| char * (* pfunc)(char *str); |
| }; |
| |
| /*-----------------------------------------------------------------------------------*/ |
| static char * |
| parse(char *str, struct ptentry *t) |
| { |
| struct ptentry *p; |
| |
| /* Loop over the parse table entries in t in order to find one that |
| matches the first character in str. */ |
| for(p = t; p->c != 0; ++p) { |
| if(*str == p->c) { |
| /* Skip rest of the characters up to the first space. */ |
| while(*str != ' ') { |
| ++str; |
| } |
| |
| /* Skip all spaces.*/ |
| while(*str == ' ') { |
| ++str; |
| } |
| |
| /* Call parse table entry function and return. */ |
| return p->pfunc(str); |
| } |
| } |
| |
| /* Did not find matching entry in parse table. We just call the |
| default handler supplied by the caller and return. */ |
| return p->pfunc(str); |
| } |
| /*-----------------------------------------------------------------------------------*/ |
| static char * |
| skipnewline(char *str) |
| { |
| /* Skip all characters until the newline. */ |
| while(*str != '\n') { |
| ++str; |
| } |
| |
| /* Return a pointer to the first character after the newline. */ |
| return str + 1; |
| } |
| /*-----------------------------------------------------------------------------------*/ |
| static char * |
| loadfile(char *str) |
| { |
| char *nt; |
| |
| /* Nullterminate string since the loader functioin expects filename |
| to end with a null chacter. Start with finding newline |
| character. */ |
| for(nt = str; *nt != '\n'; ++nt); |
| |
| /* Replace newline with a null char. */ |
| *nt = 0; |
| |
| /* Call loader function. */ |
| program_handler_load(str); |
| |
| return nt + 1; |
| } |
| /*-----------------------------------------------------------------------------------*/ |
| static char * |
| ipaddrconf(char *str) |
| { |
| return skipnewline(str); |
| } |
| /*-----------------------------------------------------------------------------------*/ |
| static char * |
| netmaskconf(char *str) |
| { |
| return skipnewline(str); |
| } |
| /*-----------------------------------------------------------------------------------*/ |
| static char * |
| drconf(char *str) |
| { |
| return skipnewline(str); |
| } |
| /*-----------------------------------------------------------------------------------*/ |
| static char * |
| dnsconf(char *str) |
| { |
| return skipnewline(str); |
| } |
| /*-----------------------------------------------------------------------------------*/ |
| static struct ptentry initparsetab[] = |
| {{'l', loadfile}, |
| {'L', loadfile}, |
| {'i', ipaddrconf}, |
| {'I', ipaddrconf}, |
| {'n', netmaskconf}, |
| {'N', netmaskconf}, |
| {'r', drconf}, |
| {'R', drconf}, |
| {'d', dnsconf}, |
| {'D', dnsconf}, |
| {'#', skipnewline}, |
| |
| /* Default action */ |
| {0, skipnewline}}; |
| static void |
| initscript(void) |
| { |
| static char line[40], *lineptr; |
| static struct c64_fs_file f; |
| |
| if(c64_fs_open("init.cfg", &f) == -1) { |
| return; |
| } |
| line[0] = ' '; |
| /* while(line[0] != '.' && |
| line[0] != 0)*/ { |
| lineptr = line; |
| do { |
| if(c64_fs_read(&f, lineptr, 1) != 1) { |
| c64_fs_close(&f); |
| return; |
| } |
| ++lineptr; |
| } while(*(lineptr - 1) != '\n' && |
| *(lineptr - 1) != '\r'); |
| |
| *lineptr = 0; |
| |
| parse(line, initparsetab); |
| |
| } |
| c64_fs_close(&f); |
| return; |
| } |
| /*-----------------------------------------------------------------------------------*/ |
| LOADER_INIT_FUNC(init_init) |
| { |
| initscript(); |
| LOADER_UNLOAD(); |
| } |
| /*-----------------------------------------------------------------------------------*/ |