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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 77 - (hide annotations) (download)
Mon Dec 29 13:10:12 2008 UTC (4 years, 4 months ago) by phihag-guest
File MIME type: text/plain
File size: 5767 byte(s)
Synchronize trunk and 2.x branch: Backport UA

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

  ViewVC Help
Powered by ViewVC 1.1.5