Parent Directory
|
Revision Log
|
Revision Graph
* starting to convert gmap.c
1 /* This file is part of Netsukuku 2 * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org> 3 * 4 * This source code is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License as published 6 * by the Free Software Foundation; either version 2 of the License, 7 * or (at your option) any later version. 8 * 9 * This source code is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 * Please refer to the GNU Public License for more details. 13 * 14 * You should have received a copy of the GNU Public License along with 15 * this source code; if not, write to: 16 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 * 18 * -- 19 * conf.c 20 * 21 * General configuration file loader and parser. To understand how to use it, 22 * see {-conf-usage-}. 23 */ 24 25 #include "includes.h" 26 #include <ctype.h> 27 28 #include "conf.h" 29 #include "common.h" 30 31 /* 32 * parse_config_line 33 * 34 * it reads the `line' string and sees if it has a valid option assignment 35 * that is in the form of "option = value". 36 * On success it stores the option name with its value in the environment. 37 * On failure fatal() is called, so it will never return ;) 38 * `file' and `pos' are used by fatal() to tell where the corrupted `line' was. 39 * 40 * `opt_head' is the head of the llist which keeps all the valid options. 41 */ 42 void parse_config_line(char *file, int pos, char *line, ntkopt *opt_head) 43 { 44 ntkopt *opt=opt_head; 45 size_t optlen; 46 int e=0; 47 char *value; 48 49 while(isspace(*line)) 50 if(!*(++line)) 51 /* It's just an empty line */ 52 return; 53 54 /* Check if `line' contains a valid option */ 55 list_for(opt) { 56 optlen=strlen(opt->opt); 57 if(!memcmp(line, opt->opt, optlen)) { 58 e=1; 59 break; 60 } 61 } 62 if(!e) 63 fatal("The line %s:%d does not contain a valid option. Aborting.", 64 file, pos); 65 66 /* Eat the remaining spaces */ 67 value=line+optlen+1; 68 while(isspace(*value)) 69 value++; 70 71 if(!*value) { 72 /* 73 * Consider this option as boolean and set it to "1" 74 */ 75 opt_add_value(opt->opt, "1", opt); 76 return; 77 } else if(*value == '=') 78 value++; 79 80 while(isspace(*value)) 81 value++; 82 if(!*value) { 83 /* 84 * We are in this case: 85 * opt = 86 * missing value after the '=' 87 */ 88 fatal("%s:%d: syntax error: no value has been assigned " 89 "to the \"%s\" option", 90 file, pos, opt->opt); 91 } 92 93 opt_add_value(opt->opt, value, opt); 94 } 95 96 /* 97 * load_config_file 98 * 99 * loads from `file' all the options that are written in it and stores them 100 * in the environment. See parse_config_line() above. 101 * If `file' cannot be opened -1 is returned, but if it is read and 102 * parse_config_line() detects a corrupted line, fatal() is directly called. 103 * 104 * `opt_head' is the head of the llist which keeps all the valid options. 105 * 106 * On success 0 is returned. 107 */ 108 int load_config_file(char *file, ntkopt *opt_head) 109 { 110 FILE *fd; 111 char buf[PATH_MAX+1], *p, *str; 112 size_t slen; 113 int e=0; 114 115 if(!(fd=fopen(file, "r"))) { 116 fatal("Cannot load the configuration file from %s: %s", 117 file, strerror(errno)); 118 return -1; 119 } 120 121 while(!feof(fd)) { 122 setzero(buf, PATH_MAX+1); 123 fgets(buf, PATH_MAX, fd); 124 e++; 125 126 if(feof(fd)) 127 break; 128 129 str=buf; 130 while(isspace(*str)) 131 str++; 132 if(*str=='#' || !*str) { 133 /* Strip off any comment or null lines */ 134 continue; 135 } else { 136 /* Remove the last part of the string where a side 137 * comment starts, #a comment like this. 138 */ 139 if((p=strrchr(str, '#'))) 140 *p='\0'; 141 142 /* Don't include the newline and spaces at the end of 143 * the string */ 144 slen=strlen(str); 145 for(p=&str[slen-1]; isspace(*p); p--) 146 *p='\0'; 147 148 149 parse_config_line(file, e, str, opt_head); 150 } 151 } 152 153 fclose(fd); 154 155 return 0; 156 }
| alpt (at) freaknet (dot) org | ViewVC Help |
| Powered by ViewVC 1.1-dev |