1 /**********************************************************************
4 * Import Directory List Handling *
7 **********************************************************************/
12 #include "constants.h"
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
27 #ifdef HAVE_SYS_STAT_H
31 #ifdef HAVE_SYS_FILE_H
39 access(const char *fileName, const char *mode)
41 FILE *fp = fopen(fileName, mode);
48 #endif /* HAVE_ACCESS */
51 list imports_dirlist, sys_imports_dirlist; /* The imports lists */
52 extern char HiSuffix[];
53 extern char PreludeHiSuffix[];
54 extern BOOLEAN ExplicitHiSuffixGiven;
59 This finds a module along the imports directory list.
63 find_module_on_imports_dirlist(module_name, is_sys_import, returned_filename)
65 BOOLEAN is_sys_import;
66 char *returned_filename;
68 char try[FILENAME_SIZE];
73 struct stat sbuf[MAX_MATCH];
76 int no_of_matches = 0;
77 BOOLEAN tried_source_dir = FALSE;
80 char *suffix_to_use = (is_sys_import) ? PreludeHiSuffix : HiSuffix;
81 int modname_len = strlen(module_name);
84 Check every directory in (sys_)imports_dirlist for the imports file.
85 The first directory in the list is the source directory.
87 for (imports_dirs = (is_sys_import) ? sys_imports_dirlist : imports_dirlist;
88 tlist(imports_dirs) == lcons;
89 imports_dirs = ltl(imports_dirs))
91 char *dir = (char *) lhd(imports_dirs);
94 try_end = try + strlen(try);
96 #ifdef macintosh /* ToDo: use DIR_SEP_CHAR */
97 if (*(try_end - 1) != ':')
98 strcpy (try_end++, ":");
100 if (*(try_end - 1) != '/')
101 strcpy (try_end++, "/");
102 #endif /* ! macintosh */
104 strcpy(try_end, module_name);
106 strcpy(try_end+modname_len, suffix_to_use);
108 /* See whether the file exists and is readable. */
109 if (access (try,R_OK) == 0)
111 if ( no_of_matches == 0 )
112 strcpy(returned_filename, try);
114 /* Return as soon as a match is found in the source directory. */
115 if (!tried_source_dir)
119 if ( no_of_matches < MAX_MATCH && stat(try, sbuf + no_of_matches) == 0 )
122 for (i = 0; i < no_of_matches; i++)
124 if ( sbuf[no_of_matches].st_dev == sbuf[i].st_dev &&
125 sbuf[no_of_matches].st_ino == sbuf[i].st_ino)
126 goto next; /* Skip dups */
129 #endif /* HAVE_STAT */
132 else if (access (try,F_OK) == 0)
133 fprintf(stderr,"Warning: %s exists, but is not readable\n",try);
136 tried_source_dir = TRUE;
139 if ( no_of_matches == 0 && ! is_sys_import ) { /* Nothing so far */
141 /* If we are explicitly meddling about with .hi suffixes,
142 then some system-supplied modules may need to be looked
143 for with PreludeHiSuffix; unsavoury but true...
145 suffix_to_use = PreludeHiSuffix;
147 for (imports_dirs = sys_imports_dirlist;
148 tlist(imports_dirs) == lcons;
149 imports_dirs = ltl(imports_dirs))
151 char *dir = (char *) lhd(imports_dirs);
154 try_end = try + strlen(try);
156 #ifdef macintosh /* ToDo: use DIR_SEP_STRING */
157 if (*(try_end - 1) != ':')
158 strcpy (try_end++, ":");
160 if (*(try_end - 1) != '/')
161 strcpy (try_end++, "/");
162 #endif /* ! macintosh */
164 strcpy(try_end, module_name);
166 strcpy(try_end+modname_len, suffix_to_use);
168 /* See whether the file exists and is readable. */
169 if (access (try,R_OK) == 0)
171 if ( no_of_matches == 0 )
172 strcpy(returned_filename, try);
175 if ( no_of_matches < MAX_MATCH && stat(try, sbuf + no_of_matches) == 0 )
178 for (i = 0; i < no_of_matches; i++)
180 if ( sbuf[no_of_matches].st_dev == sbuf[i].st_dev &&
181 sbuf[no_of_matches].st_ino == sbuf[i].st_ino)
182 goto next_again; /* Skip dups */
185 #endif /* HAVE_STAT */
188 else if (access (try,F_OK) == 0)
189 fprintf(stderr,"Warning: %s exists, but is not readable\n",try);
197 switch ( no_of_matches ) {
199 fprintf(stderr,"Warning: found %d %s files for module \"%s\"\n",
200 no_of_matches, suffix_to_use, module_name);
204 char disaster_msg[MODNAME_SIZE+1000];
205 sprintf(disaster_msg,"can't find interface (%s) file for module \"%s\"%s",
206 suffix_to_use, module_name,
207 (strncmp(module_name, "PreludeGlaIO", 12) == 0)
208 ? "\n(The PreludeGlaIO interface no longer exists);"
210 (strncmp(module_name, "PreludePrimIO", 13) == 0)
211 ? "\n(The PreludePrimIO interface no longer exists -- just use PreludeGlaST);"
213 (strncmp(module_name, "Prelude", 7) == 0)
214 ? "\n(Perhaps you forgot a `-fglasgow-exts' flag?);"
217 hsperror(disaster_msg);
221 /* Everything is fine */