Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 206 lines (169 sloc) 5.346 kB
6466d02 Add automatic module libraries.
steve authored
1 /*
1993bf6 @caryr Remove malloc.h support and for C++ files use <c...> include files.
caryr authored
2 * Copyright (c) 2001-2010 Stephen Williams (steve@icarus.com)
6466d02 Add automatic module libraries.
steve authored
3 *
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)
8 * any later version.
9 *
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.
14 *
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
18 */
19
990df42 Handle activating tasks in another root.
steve authored
20 # include "config.h"
6466d02 Add automatic module libraries.
steve authored
21 # include "util.h"
22 # include "parse_api.h"
23 # include "compiler.h"
28e0616 Use standard name for iostream.
steve authored
24 # include <iostream>
5aa6951 ivl indexes the search path for libraries, and
steve authored
25 # include <map>
8ea3b6b @ldoolitt header includes for gcc-4.3 compatibility
ldoolitt authored
26 # include <cstdlib>
27 # include <cstring>
5aa6951 ivl indexes the search path for libraries, and
steve authored
28 # include <string>
29 # include <sys/types.h>
30 # include <dirent.h>
1993bf6 @caryr Remove malloc.h support and for C++ files use <c...> include files.
caryr authored
31 # include <cctype>
32 # include <cassert>
a4a7b0a @caryr Make ivl_alloc.h the last include so it doesn't effect any system inc…
caryr authored
33 # include "ivl_alloc.h"
6466d02 Add automatic module libraries.
steve authored
34
5aa6951 ivl indexes the search path for libraries, and
steve authored
35 /*
36 * The module library items are maps of key names to file name within
37 * the directory.
38 */
39 struct module_library {
40 char*dir;
41 bool key_case_sensitive;
42 map<string,const char*>name_map;
43 struct module_library*next;
44 };
45
46 static struct module_library*library_list = 0;
47 static struct module_library*library_last = 0;
48
6466d02 Add automatic module libraries.
steve authored
49 const char dir_character = '/';
d0cb963 @martinwhitaker Provide different modes for dependency list output.
martinwhitaker authored
50 extern char depfile_mode;
284c6fd Add dependency generation.
steve authored
51 extern FILE *depend_file;
6466d02 Add automatic module libraries.
steve authored
52
5aa6951 ivl indexes the search path for libraries, and
steve authored
53 /*
54 * Use the type name as a key, and search the module library for a
55 * file name that has that key.
56 */
6466d02 Add automatic module libraries.
steve authored
57 bool load_module(const char*type)
58 {
59 char path[4096];
5aa6951 ivl indexes the search path for libraries, and
steve authored
60 char*ltype = strdup(type);
6466d02 Add automatic module libraries.
steve authored
61
5aa6951 ivl indexes the search path for libraries, and
steve authored
62 for (char*tmp = ltype ; *tmp ; tmp += 1)
63 *tmp = tolower(*tmp);
6466d02 Add automatic module libraries.
steve authored
64
5aa6951 ivl indexes the search path for libraries, and
steve authored
65 for (struct module_library*lcur = library_list
66 ; lcur != 0 ; lcur = lcur->next) {
6466d02 Add automatic module libraries.
steve authored
67
5aa6951 ivl indexes the search path for libraries, and
steve authored
68 const char*key = lcur->key_case_sensitive? type : ltype;
69 map<string,const char*>::const_iterator cur;
70 cur = lcur->name_map.find(key);
71 if (cur == lcur->name_map.end())
72 continue;
6466d02 Add automatic module libraries.
steve authored
73
5aa6951 ivl indexes the search path for libraries, and
steve authored
74 sprintf(path, "%s%c%s", lcur->dir, dir_character, (*cur).second);
9a4ee87 Add the ivl -C flag for bulk configuration
steve authored
75
5aa6951 ivl indexes the search path for libraries, and
steve authored
76 if(depend_file) {
d0cb963 @martinwhitaker Provide different modes for dependency list output.
martinwhitaker authored
77 if (depfile_mode == 'p') {
78 fprintf(depend_file, "M %s\n", path);
79 } else if (depfile_mode != 'i') {
80 fprintf(depend_file, "%s\n", path);
81 }
a10bd13 @steveicarus Fix dependency file (-M) generation.
steveicarus authored
82 fflush(depend_file);
5aa6951 ivl indexes the search path for libraries, and
steve authored
83 }
9a4ee87 Add the ivl -C flag for bulk configuration
steve authored
84
5aa6951 ivl indexes the search path for libraries, and
steve authored
85 if (ivlpp_string) {
86 char*cmdline = (char*)malloc(strlen(ivlpp_string) +
dba2a6e @caryr Add support for spaces in many paths/files names
caryr authored
87 strlen(path) + 4);
5aa6951 ivl indexes the search path for libraries, and
steve authored
88 strcpy(cmdline, ivlpp_string);
dba2a6e @caryr Add support for spaces in many paths/files names
caryr authored
89 strcat(cmdline, " \"");
5aa6951 ivl indexes the search path for libraries, and
steve authored
90 strcat(cmdline, path);
dba2a6e @caryr Add support for spaces in many paths/files names
caryr authored
91 strcat(cmdline, "\"");
94fa895 @steveicarus Minor code cleanup.
steveicarus authored
92
93 if (verbose_flag)
94 cerr << "Executing: " << cmdline << endl<< flush;
95
5aa6951 ivl indexes the search path for libraries, and
steve authored
96 FILE*file = popen(cmdline, "r");
9a4ee87 Add the ivl -C flag for bulk configuration
steve authored
97
5aa6951 ivl indexes the search path for libraries, and
steve authored
98 if (verbose_flag)
94fa895 @steveicarus Minor code cleanup.
steveicarus authored
99 cerr << "...parsing output from preprocessor..." << endl << flush;
9a4ee87 Add the ivl -C flag for bulk configuration
steve authored
100
5aa6951 ivl indexes the search path for libraries, and
steve authored
101 pform_parse(path, file);
becda0e popen must be matched by pclose.
steve authored
102 pclose(file);
5aa6951 ivl indexes the search path for libraries, and
steve authored
103 free(cmdline);
9a4ee87 Add the ivl -C flag for bulk configuration
steve authored
104
5aa6951 ivl indexes the search path for libraries, and
steve authored
105 } else {
106 if (verbose_flag)
107 cerr << "Loading library file "
108 << path << "." << endl;
da4901b Close library files after parsing.
steve authored
109
5aa6951 ivl indexes the search path for libraries, and
steve authored
110 FILE*file = fopen(path, "r");
111 assert(file);
112 pform_parse(path, file);
113 fclose(file);
0be4838 Add support for +libext+ in command files.
steve authored
114 }
5aa6951 ivl indexes the search path for libraries, and
steve authored
115
94fa895 @steveicarus Minor code cleanup.
steveicarus authored
116 if (verbose_flag)
117 cerr << "... Load module complete." << endl << flush;
118
5aa6951 ivl indexes the search path for libraries, and
steve authored
119 return true;
6466d02 Add automatic module libraries.
steve authored
120 }
121
5aa6951 ivl indexes the search path for libraries, and
steve authored
122
6466d02 Add automatic module libraries.
steve authored
123 return false;
124 }
125
126 /*
5aa6951 ivl indexes the search path for libraries, and
steve authored
127 * This function takes the name of a library directory that the caller
128 * passed, and builds a name index for it.
129 */
130 int build_library_index(const char*path, bool key_case_sensitive)
131 {
132 DIR*dir = opendir(path);
133 if (dir == 0)
134 return -1;
135
136 if (verbose_flag) {
137 cerr << "Indexing library: " << path << endl;
138 }
139
140 struct module_library*mlp = new struct module_library;
141 mlp->dir = strdup(path);
142 mlp->key_case_sensitive = key_case_sensitive;
143
a748604 Library dir case insensitivity includes the suffix.
steve authored
144 /* Scan the director for files. check each file name to see if
145 it has one of the configured suffixes. If it does, then use
146 the root of the name as the key and index the file name. */
5aa6951 ivl indexes the search path for libraries, and
steve authored
147 while (struct dirent*de = readdir(dir)) {
148 unsigned namsiz = strlen(de->d_name);
149 char*key = 0;
150
151 for (list<const char*>::iterator suf = library_suff.begin()
225ca1e @caryr Change iterators to use prefix ++ since it is more efficient.
caryr authored
152 ; suf != library_suff.end() ; ++ suf ) {
5aa6951 ivl indexes the search path for libraries, and
steve authored
153 const char*sufptr = *suf;
154 unsigned sufsiz = strlen(sufptr);
155
156 if (sufsiz >= namsiz)
157 continue;
158
a748604 Library dir case insensitivity includes the suffix.
steve authored
159 /* If the directory is case insensitive, then so
160 is the suffix. */
dd8da91 Fix suffix parsing of library index.
steve authored
161 if (key_case_sensitive) {
162 if (strcmp(de->d_name + (namsiz-sufsiz),
163 sufptr) != 0)
a748604 Library dir case insensitivity includes the suffix.
steve authored
164 continue;
dd8da91 Fix suffix parsing of library index.
steve authored
165 } else {
166 if (strcasecmp(de->d_name + (namsiz-sufsiz),
167 sufptr) != 0)
a748604 Library dir case insensitivity includes the suffix.
steve authored
168 continue;
dd8da91 Fix suffix parsing of library index.
steve authored
169 }
5aa6951 ivl indexes the search path for libraries, and
steve authored
170
171 key = new char[namsiz-sufsiz+1];
172 strncpy(key, de->d_name, namsiz-sufsiz);
173 key[namsiz-sufsiz] = 0;
dd8da91 Fix suffix parsing of library index.
steve authored
174
5aa6951 ivl indexes the search path for libraries, and
steve authored
175 break;
176 }
177
178 if (key == 0)
179 continue;
180
181 /* If the key is not to be case sensitive, then change
182 it to lowercase. */
183 if (! key_case_sensitive)
184 for (char*tmp = key ; *tmp ; tmp += 1)
185 *tmp = tolower(*tmp);
186
187 mlp->name_map[key] = strdup(de->d_name);
188 delete[]key;
189 }
190
191 closedir(dir);
192
193 if (library_last) {
194 assert(library_list);
195 library_last->next = mlp;
196 mlp->next = 0;
197 library_last = mlp;
198 } else {
199 library_list = mlp;
200 library_last = mlp;
201 mlp->next = 0;
202 }
203
204 return 0;
205 }
Something went wrong with that request. Please try again.