2 * Copyright (c) 2001 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 #ident "$Id: load_module.cc,v 1.13 2005/03/22 15:53:48 steve Exp $"
25 # include "parse_api.h"
26 # include "compiler.h"
30 # include <sys/types.h>
36 * The module library items are maps of key names to file name within
39 struct module_library
{
41 bool key_case_sensitive
;
42 map
<string
,const char*>name_map
;
43 struct module_library
*next
;
46 static struct module_library
*library_list
= 0;
47 static struct module_library
*library_last
= 0;
49 const char dir_character
= '/';
50 extern FILE *depend_file
;
53 * Use the type name as a key, and search the module library for a
54 * file name that has that key.
56 bool load_module(const char*type
)
59 char*ltype
= strdup(type
);
61 for (char*tmp
= ltype
; *tmp
; tmp
+= 1)
64 for (struct module_library
*lcur
= library_list
65 ; lcur
!= 0 ; lcur
= lcur
->next
) {
67 const char*key
= lcur
->key_case_sensitive
? type
: ltype
;
68 map
<string
,const char*>::const_iterator cur
;
69 cur
= lcur
->name_map
.find(key
);
70 if (cur
== lcur
->name_map
.end())
73 sprintf(path
, "%s%c%s", lcur
->dir
, dir_character
, (*cur
).second
);
76 fprintf(depend_file
, "%s\n", path
);
80 char*cmdline
= (char*)malloc(strlen(ivlpp_string
) +
82 strcpy(cmdline
, ivlpp_string
);
84 strcat(cmdline
, path
);
85 FILE*file
= popen(cmdline
, "r");
88 cerr
<< "Executing: " << cmdline
<< endl
;
90 pform_parse(path
, file
);
96 cerr
<< "Loading library file "
97 << path
<< "." << endl
;
99 FILE*file
= fopen(path
, "r");
101 pform_parse(path
, file
);
113 * This function takes the name of a library directory that the caller
114 * passed, and builds a name index for it.
116 int build_library_index(const char*path
, bool key_case_sensitive
)
118 DIR*dir
= opendir(path
);
123 cerr
<< "Indexing library: " << path
<< endl
;
126 struct module_library
*mlp
= new struct module_library
;
127 mlp
->dir
= strdup(path
);
128 mlp
->key_case_sensitive
= key_case_sensitive
;
130 /* Scan the director for files. check each file name to see if
131 it has one of the configured suffixes. If it does, then use
132 the root of the name as the key and index the file name. */
133 while (struct dirent
*de
= readdir(dir
)) {
134 unsigned namsiz
= strlen(de
->d_name
);
137 for (list
<const char*>::iterator suf
= library_suff
.begin()
138 ; suf
!= library_suff
.end()
140 const char*sufptr
= *suf
;
141 unsigned sufsiz
= strlen(sufptr
);
143 if (sufsiz
>= namsiz
)
146 /* If the directory is case insensitive, then so
148 if (key_case_sensitive
) {
149 if (strcmp(de
->d_name
+ (namsiz
-sufsiz
),
153 if (strcasecmp(de
->d_name
+ (namsiz
-sufsiz
),
158 key
= new char[namsiz
-sufsiz
+1];
159 strncpy(key
, de
->d_name
, namsiz
-sufsiz
);
160 key
[namsiz
-sufsiz
] = 0;
168 /* If the key is not to be case sensitive, then change
170 if (! key_case_sensitive
)
171 for (char*tmp
= key
; *tmp
; tmp
+= 1)
172 *tmp
= tolower(*tmp
);
174 mlp
->name_map
[key
] = strdup(de
->d_name
);
181 assert(library_list
);
182 library_last
->next
= mlp
;
195 * $Log: load_module.cc,v $
196 * Revision 1.13 2005/03/22 15:53:48 steve
197 * popen must be matched by pclose.
199 * Revision 1.12 2003/06/05 04:31:09 steve
200 * INclude missing assert.h in load_module.
202 * Revision 1.11 2002/08/12 01:34:59 steve
203 * conditional ident string using autoconfig.
205 * Revision 1.10 2002/08/03 22:30:00 steve
206 * Fix suffix parsing of library index.
208 * Revision 1.9 2002/07/10 04:34:18 steve
209 * Library dir case insensitivity includes the suffix.
211 * Revision 1.8 2002/06/06 18:57:18 steve
212 * Use standard name for iostream.
214 * Revision 1.7 2002/05/28 20:40:37 steve
215 * ivl indexes the search path for libraries, and
216 * supports case insensitive module-to-file lookup.
218 * Revision 1.6 2002/05/28 00:50:39 steve
219 * Add the ivl -C flag for bulk configuration
220 * from the driver, and use that to run library
221 * modules through the preprocessor.
223 * Revision 1.5 2002/04/04 05:26:13 steve
224 * Add dependency generation.
226 * Revision 1.4 2001/11/20 23:36:34 steve
227 * Close library files after parsing.
229 * Revision 1.3 2001/11/16 05:07:19 steve
230 * Add support for +libext+ in command files.
232 * Revision 1.2 2001/10/22 02:05:21 steve
233 * Handle activating tasks in another root.
235 * Revision 1.1 2001/10/20 23:02:40 steve
236 * Add automatic module libraries.