/[axel]/branches/2.x/conf.c
ViewVC logotype

Contents of /branches/2.x/conf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Jan 11 11:57:58 2008 UTC (5 years, 4 months ago) by appaji-guest
Original Path: trunk/conf.c
File MIME type: text/plain
File size: 5421 byte(s)
Import 1.0a
1 /********************************************************************\
2 * Axel -- A lighter download accelerator for Linux and other Unices. *
3 * *
4 * Copyright 2001 Wilmer van der Gaast *
5 \********************************************************************/
6
7 /* Configuration handling file */
8
9 /*
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License with
21 the Debian GNU/Linux distribution in file /usr/doc/copyright/GPL;
22 if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 #include "axel.h"
27
28 /* Some nifty macro's.. */
29 #define get_config_string( name ) \
30 if( strcmp( key, #name ) == 0 ) \
31 { \
32 st = 1; \
33 strcpy( conf->name, value ); \
34 }
35 #define get_config_number( name ) \
36 if( strcmp( key, #name ) == 0 ) \
37 { \
38 st = 1; \
39 sscanf( value, "%i", &conf->name ); \
40 }
41
42 int parse_interfaces( conf_t *conf, char *s );
43
44 int conf_loadfile( conf_t *conf, char *file )
45 {
46 int i, line = 0;
47 FILE *fp;
48 char s[MAX_STRING], key[MAX_STRING], value[MAX_STRING];
49
50 fp = fopen( file, "r" );
51 if( fp == NULL )
52 return( 1 ); /* Not a real failure */
53
54 while( !feof( fp ) )
55 {
56 int st;
57
58 line ++;
59
60 *s = 0;
61 fscanf( fp, "%100[^\n#]s", s );
62 fscanf( fp, "%*[^\n]s" );
63 fgetc( fp ); /* Skip newline */
64 if( strchr( s, '=' ) == NULL )
65 continue; /* Probably empty? */
66 sscanf( s, "%[^= \t]s", key );
67 for( i = 0; s[i]; i ++ )
68 if( s[i] == '=' )
69 {
70 for( i ++; isspace( (int) s[i] ) && s[i]; i ++ );
71 break;
72 }
73 strcpy( value, &s[i] );
74 for( i = strlen( value ) - 1; isspace( (int) value[i] ); i -- )
75 value[i] = 0;
76
77 st = 0;
78
79 /* Long live macros!! */
80 get_config_string( default_filename );
81 get_config_string( http_proxy );
82 get_config_string( no_proxy );
83 get_config_number( strip_cgi_parameters );
84 get_config_number( save_state_interval );
85 get_config_number( connection_timeout );
86 get_config_number( reconnect_delay );
87 get_config_number( num_connections );
88 get_config_number( buffer_size );
89 get_config_number( max_speed );
90 get_config_number( verbose );
91 get_config_number( alternate_output );
92
93 get_config_number( search_timeout );
94 get_config_number( search_threads );
95 get_config_number( search_amount );
96 get_config_number( search_top );
97
98 /* Option defunct but shouldn't be an error */
99 if( strcmp( key, "speed_type" ) == 0 )
100 st = 1;
101
102 if( strcmp( key, "interfaces" ) == 0 )
103 st = parse_interfaces( conf, value );
104
105 if( !st )
106 {
107 fprintf( stderr, _("Error in %s line %i.\n"), file, line );
108 return( 0 );
109 }
110 }
111
112 fclose( fp );
113 return( 1 );
114 }
115
116 int conf_init( conf_t *conf )
117 {
118 char s[MAX_STRING], *s2;
119 int i;
120
121 /* Set defaults */
122 memset( conf, 0, sizeof( conf_t ) );
123 strcpy( conf->default_filename, "default" );
124 *conf->http_proxy = 0;
125 *conf->no_proxy = 0;
126 conf->strip_cgi_parameters = 1;
127 conf->save_state_interval = 10;
128 conf->connection_timeout = 45;
129 conf->reconnect_delay = 20;
130 conf->num_connections = 4;
131 conf->buffer_size = 5120;
132 conf->max_speed = 0;
133 conf->verbose = 1;
134 conf->alternate_output = 0;
135
136 conf->search_timeout = 10;
137 conf->search_threads = 3;
138 conf->search_amount = 15;
139 conf->search_top = 3;
140
141 conf->interfaces = malloc( sizeof( if_t ) );
142 memset( conf->interfaces, 0, sizeof( if_t ) );
143 conf->interfaces->next = conf->interfaces;
144
145 if( ( s2 = getenv( "HTTP_PROXY" ) ) != NULL )
146 strncpy( conf->http_proxy, s2, MAX_STRING );
147
148 if( !conf_loadfile( conf, ETCDIR "/axelrc" ) )
149 return( 0 );
150
151 if( ( s2 = getenv( "HOME" ) ) != NULL )
152 {
153 sprintf( s, "%s/%s", s2, ".axelrc" );
154 if( !conf_loadfile( conf, s ) )
155 return( 0 );
156 }
157
158 /* Convert no_proxy to a 0-separated-and-00-terminated list.. */
159 for( i = 0; conf->no_proxy[i]; i ++ )
160 if( conf->no_proxy[i] == ',' )
161 conf->no_proxy[i] = 0;
162 conf->no_proxy[i+1] = 0;
163
164 return( 1 );
165 }
166
167 int parse_interfaces( conf_t *conf, char *s )
168 {
169 char *s2;
170 if_t *iface;
171
172 iface = conf->interfaces->next;
173 while( iface != conf->interfaces )
174 {
175 if_t *i;
176
177 i = iface->next;
178 free( iface );
179 iface = i;
180 }
181 free( conf->interfaces );
182
183 if( !*s )
184 {
185 conf->interfaces = malloc( sizeof( if_t ) );
186 memset( conf->interfaces, 0, sizeof( if_t ) );
187 conf->interfaces->next = conf->interfaces;
188 return( 1 );
189 }
190
191 s[strlen(s)+1] = 0;
192 conf->interfaces = iface = malloc( sizeof( if_t ) );
193 while( 1 )
194 {
195 while( ( *s == ' ' || *s == '\t' ) && *s ) s ++;
196 for( s2 = s; *s2 != ' ' && *s2 != '\t' && *s2; s2 ++ );
197 *s2 = 0;
198 if( *s < '0' || *s > '9' )
199 get_if_ip( s, iface->text );
200 else
201 strcpy( iface->text, s );
202 s = s2 + 1;
203 if( *s )
204 {
205 iface->next = malloc( sizeof( if_t ) );
206 iface = iface->next;
207 }
208 else
209 {
210 iface->next = conf->interfaces;
211 break;
212 }
213 }
214
215 return( 1 );
216 }

  ViewVC Help
Powered by ViewVC 1.1.5