Subversion Repositories tendra.SVN

Rev

Rev 7 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "filename.h"
33
#include "list.h"
34
#include "environ.h"
35
#include "flags.h"
36
#include "options.h"
37
#include "suffix.h"
38
#include "utility.h"
39
 
40
 
41
/*
42
    THE CURRENT ENVIRONMENTS PATH
43
 
44
    The environment path is a colon-separated list of directories which
45
    are searched for tcc environments.
46
*/
47
 
48
static char *envpath = "." ;
49
 
50
 
51
/*
52
    UPDATE THE ENVIRONMENTS PATH
53
 
54
    This routine initialises and updates the environments path.  This is
55
    given by the contents of the system variable TCCENV, plus the default
56
    directory (environ_dir), plus the current directory.
57
*/
58
 
59
void find_envpath
60
    PROTO_Z ()
61
{
62
    char *p = buffer ;
63
    char *tcc_env = getenv ( TCCENV_VAR ) ;
64
    if ( tcc_env ) {
65
	IGNORE sprintf ( p, "%s:", tcc_env ) ;
66
	p += strlen ( p ) ;
67
    }
68
    IGNORE sprintf ( p, "%s:.", environ_dir ) ;
69
    if ( !streq ( buffer, envpath ) ) envpath = string_copy ( buffer ) ;
70
    return ;
71
}
72
 
73
 
74
/*
75
    PRINT THE ENVIRONMENTS PATH
76
 
77
    This routine prints the environment path.
78
*/
79
 
80
void show_envpath
81
    PROTO_Z ()
82
{
83
    find_envpath () ;
84
    error ( INFO, "Environment path is '%s'", envpath ) ;
85
    return ;
86
}
87
 
88
 
89
/*
90
    CHARACTER TYPES
91
 
92
    These macros identify various character types.
93
*/
94
 
95
#define is_alphanum( X )	( ( ( X ) >= 'A' && ( X ) <= 'Z' ) ||\
96
				  ( ( X ) >= '0' && ( X ) <= '9' ) ||\
97
				  ( ( X ) == '_' ) )
98
#define is_whitespace( X )	( ( X ) == ' ' || ( X ) == '\t' )
99
#define is_quote( X )		( ( X ) == '"' )
100
#define is_newline( X )		( ( X ) == '\n' )
101
 
102
 
103
/*
104
    READ AN ENVIRONMENT - AUXILIARY ROUTINE
105
 
106
    This routine reads the environment named nm, returning zero if it
107
    is successful.  A return value of 1 indicates that the environment
108
    could not be found, otherwise 2 is returned.
109
*/
110
 
111
int read_env_aux
112
    PROTO_N ( ( nm ) )
113
    PROTO_T ( char *nm )
114
{
115
    /* Find the environment */
116
    FILE *f ;
117
    char *p, *q ;
118
    if ( *nm == 0 ) {
119
	return ( 1 ) ;
120
    } else if ( *nm == '/' ) {
121
	f = fopen ( nm, "r" ) ;
122
    } else {
123
	p = envpath ;
124
	do {
125
	    q = buffer ;
126
	    while ( *p && *p != ':' ) *( q++ ) = *( p++ ) ;
127
	    *( q++ ) = '/' ;
128
	    IGNORE strcpy ( q, nm ) ;
129
	    f = fopen ( buffer, "r" ) ;
130
	} while ( f == null && *( p++ ) ) ;
131
    }
132
    if ( f == null ) return ( 1 ) ;
133
 
134
    /* Read the environment one line at a time */
135
    while ( fgets ( buffer, buffer_size, f ) != null ) {
136
	char c = *buffer ;
137
	if ( c == '<' || c == '>' || c == '+' || c == '?' ) {
138
	    /* Only process lines beginning with these characters */
139
	    char *sp ;
140
	    list dummy ;
141
	    char line [1000] ;
142
	    line [0] = c ;
143
	    p = buffer + 1 ;
144
	    q = line + 1 ;
145
	    while ( c = *( p++ ), is_alphanum ( c ) ) *( q++ ) = c ;
146
	    sp = q ;
147
	    *( q++ ) = 0 ;
148
	    if ( !is_whitespace ( c ) ) {
149
		error ( WARNING, "Illegal environmental variable, '%s'",
150
			line ) ;
151
	    }
152
	    while ( c = *( p++ ), is_whitespace ( c ) ) /* empty */ ;
153
	    if ( !is_quote ( c ) ) {
154
		error ( WARNING, "Illegal environmental value for '%s'",
155
			line ) ;
156
	    }
157
	    while ( c = *( p++ ), !is_quote ( c ) ) {
158
		if ( c == '\\' ) c = *( p++ ) ;
159
		if ( c == 0 || is_newline ( c ) ) {
160
		    error ( WARNING, "Illegal environmental value for '%s'",
161
			    line ) ;
162
		    break ;
163
		}
164
		*( q++ ) = c ;
165
	    }
166
	    while ( c = *( p++ ), is_whitespace ( c ) ) /* empty */ ;
167
	    if ( !is_newline ( c ) ) {
168
		error ( WARNING, "Illegal environmental value for '%s'",
169
			line ) ;
170
	    }
171
	    *sp = ' ' ;
172
	    *q = 0 ;
173
	    dummy.item = string_copy ( line ) ;
174
	    dummy.next = null ;
175
	    process_options ( &dummy, environ_optmap ) ;
176
	}
177
    }
178
    IGNORE fclose ( f ) ;
179
    return ( 0 ) ;
180
}
181
 
182
 
183
/*
184
    READ AN ENVIRONMENT
185
 
186
    This routine reads the environment named nm, reporting an error if
187
    it is unsuccessful.
188
*/
189
 
190
void read_env
191
    PROTO_N ( ( nm ) )
192
    PROTO_T ( char *nm )
193
{
194
    int e = read_env_aux ( nm ) ;
195
    if ( e == 1 ) error ( WARNING, "Can't find environment, '%s'", nm ) ;
196
    return ;
197
}