| 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 |
else if( ( s2 = getenv( "HTTP_PROXY" ) ) != NULL )
|
| 152 |
strncpy( conf->http_proxy, s2, MAX_STRING );
|
| 153 |
|
| 154 |
if( !conf_loadfile( conf, ETCDIR "/axelrc" ) )
|
| 155 |
return( 0 );
|
| 156 |
|
| 157 |
if( ( s2 = getenv( "HOME" ) ) != NULL )
|
| 158 |
{
|
| 159 |
sprintf( s, "%s/%s", s2, ".axelrc" );
|
| 160 |
if( !conf_loadfile( conf, s ) )
|
| 161 |
return( 0 );
|
| 162 |
}
|
| 163 |
|
| 164 |
/* Convert no_proxy to a 0-separated-and-00-terminated list.. */
|
| 165 |
for( i = 0; conf->no_proxy[i]; i ++ )
|
| 166 |
if( conf->no_proxy[i] == ',' )
|
| 167 |
conf->no_proxy[i] = 0;
|
| 168 |
conf->no_proxy[i+1] = 0;
|
| 169 |
|
| 170 |
return( 1 );
|
| 171 |
}
|
| 172 |
|
| 173 |
int parse_interfaces( conf_t *conf, char *s )
|
| 174 |
{
|
| 175 |
char *s2;
|
| 176 |
if_t *iface;
|
| 177 |
|
| 178 |
iface = conf->interfaces->next;
|
| 179 |
while( iface != conf->interfaces )
|
| 180 |
{
|
| 181 |
if_t *i;
|
| 182 |
|
| 183 |
i = iface->next;
|
| 184 |
free( iface );
|
| 185 |
iface = i;
|
| 186 |
}
|
| 187 |
free( conf->interfaces );
|
| 188 |
|
| 189 |
if( !*s )
|
| 190 |
{
|
| 191 |
conf->interfaces = malloc( sizeof( if_t ) );
|
| 192 |
memset( conf->interfaces, 0, sizeof( if_t ) );
|
| 193 |
conf->interfaces->next = conf->interfaces;
|
| 194 |
return( 1 );
|
| 195 |
}
|
| 196 |
|
| 197 |
s[strlen(s)+1] = 0;
|
| 198 |
conf->interfaces = iface = malloc( sizeof( if_t ) );
|
| 199 |
while( 1 )
|
| 200 |
{
|
| 201 |
while( ( *s == ' ' || *s == '\t' ) && *s ) s ++;
|
| 202 |
for( s2 = s; *s2 != ' ' && *s2 != '\t' && *s2; s2 ++ );
|
| 203 |
*s2 = 0;
|
| 204 |
if( *s < '0' || *s > '9' )
|
| 205 |
get_if_ip( s, iface->text );
|
| 206 |
else
|
| 207 |
strcpy( iface->text, s );
|
| 208 |
s = s2 + 1;
|
| 209 |
if( *s )
|
| 210 |
{
|
| 211 |
iface->next = malloc( sizeof( if_t ) );
|
| 212 |
iface = iface->next;
|
| 213 |
}
|
| 214 |
else
|
| 215 |
{
|
| 216 |
iface->next = conf->interfaces;
|
| 217 |
break;
|
| 218 |
}
|
| 219 |
}
|
| 220 |
|
| 221 |
return( 1 );
|
| 222 |
}
|