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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (show annotations) (download)
Thu Apr 3 11:01:31 2008 UTC (5 years, 1 month ago) by appaji-guest
Original Path: trunk/conf.c
File MIME type: text/plain
File size: 5570 byte(s)
Custom header support, thanks Eli Yukelzon
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 get_config_number( add_header_count );
111 for(i=0;i<conf->add_header_count;i++)
112 get_config_string( add_header[i] );
113 }
114
115 fclose( fp );
116 return( 1 );
117 }
118
119 int conf_init( conf_t *conf )
120 {
121 char s[MAX_STRING], *s2;
122 int i;
123
124 /* Set defaults */
125 memset( conf, 0, sizeof( conf_t ) );
126 strcpy( conf->default_filename, "default" );
127 *conf->http_proxy = 0;
128 *conf->no_proxy = 0;
129 conf->strip_cgi_parameters = 1;
130 conf->save_state_interval = 10;
131 conf->connection_timeout = 45;
132 conf->reconnect_delay = 20;
133 conf->num_connections = 4;
134 conf->buffer_size = 5120;
135 conf->max_speed = 0;
136 conf->verbose = 1;
137 conf->alternate_output = 0;
138
139 conf->search_timeout = 10;
140 conf->search_threads = 3;
141 conf->search_amount = 15;
142 conf->search_top = 3;
143 conf->add_header_count = 0;
144
145 conf->interfaces = malloc( sizeof( if_t ) );
146 memset( conf->interfaces, 0, sizeof( if_t ) );
147 conf->interfaces->next = conf->interfaces;
148
149 if( ( s2 = getenv( "HTTP_PROXY" ) ) != NULL )
150 strncpy( conf->http_proxy, s2, MAX_STRING );
151
152 if( !conf_loadfile( conf, ETCDIR "/axelrc" ) )
153 return( 0 );
154
155 if( ( s2 = getenv( "HOME" ) ) != NULL )
156 {
157 sprintf( s, "%s/%s", s2, ".axelrc" );
158 if( !conf_loadfile( conf, s ) )
159 return( 0 );
160 }
161
162 /* Convert no_proxy to a 0-separated-and-00-terminated list.. */
163 for( i = 0; conf->no_proxy[i]; i ++ )
164 if( conf->no_proxy[i] == ',' )
165 conf->no_proxy[i] = 0;
166 conf->no_proxy[i+1] = 0;
167
168 return( 1 );
169 }
170
171 int parse_interfaces( conf_t *conf, char *s )
172 {
173 char *s2;
174 if_t *iface;
175
176 iface = conf->interfaces->next;
177 while( iface != conf->interfaces )
178 {
179 if_t *i;
180
181 i = iface->next;
182 free( iface );
183 iface = i;
184 }
185 free( conf->interfaces );
186
187 if( !*s )
188 {
189 conf->interfaces = malloc( sizeof( if_t ) );
190 memset( conf->interfaces, 0, sizeof( if_t ) );
191 conf->interfaces->next = conf->interfaces;
192 return( 1 );
193 }
194
195 s[strlen(s)+1] = 0;
196 conf->interfaces = iface = malloc( sizeof( if_t ) );
197 while( 1 )
198 {
199 while( ( *s == ' ' || *s == '\t' ) && *s ) s ++;
200 for( s2 = s; *s2 != ' ' && *s2 != '\t' && *s2; s2 ++ );
201 *s2 = 0;
202 if( *s < '0' || *s > '9' )
203 get_if_ip( s, iface->text );
204 else
205 strcpy( iface->text, s );
206 s = s2 + 1;
207 if( *s )
208 {
209 iface->next = malloc( sizeof( if_t ) );
210 iface = iface->next;
211 }
212 else
213 {
214 iface->next = conf->interfaces;
215 break;
216 }
217 }
218
219 return( 1 );
220 }

  ViewVC Help
Powered by ViewVC 1.1.5