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 /* OLD 95/08: extern BOOLEAN ExplicitHiSuffixGiven; */
59 This finds a module along the imports directory list.
63 find_module_on_imports_dirlist(char *module_name, BOOLEAN is_sys_import, char *returned_filename)
65 char try[FILENAME_SIZE];
70 struct stat sbuf[MAX_MATCH];
73 int no_of_matches = 0;
74 BOOLEAN tried_source_dir = FALSE;
77 char *suffix_to_use = (is_sys_import) ? PreludeHiSuffix : HiSuffix;
78 char *suffix_to_report = suffix_to_use; /* save this for reporting, because we
79 might change suffix_to_use later */
80 int modname_len = strlen(module_name);
83 Check every directory in (sys_)imports_dirlist for the imports file.
84 The first directory in the list is the source directory.
86 for (imports_dirs = (is_sys_import) ? sys_imports_dirlist : imports_dirlist;
87 tlist(imports_dirs) == lcons;
88 imports_dirs = ltl(imports_dirs))
90 char *dir = (char *) lhd(imports_dirs);
93 try_end = try + strlen(try);
95 #ifdef macintosh /* ToDo: use DIR_SEP_CHAR */
96 if (*(try_end - 1) != ':')
97 strcpy (try_end++, ":");
99 if (*(try_end - 1) != '/')
100 strcpy (try_end++, "/");
101 #endif /* ! macintosh */
103 strcpy(try_end, module_name);
105 strcpy(try_end+modname_len, suffix_to_use);
107 /* See whether the file exists and is readable. */
108 if (access (try,R_OK) == 0)
110 if ( no_of_matches == 0 )
111 strcpy(returned_filename, try);
113 /* Return as soon as a match is found in the source directory. */
114 if (!tried_source_dir)
118 if ( no_of_matches < MAX_MATCH && stat(try, sbuf + no_of_matches) == 0 )
121 for (i = 0; i < no_of_matches; i++)
123 if ( sbuf[no_of_matches].st_dev == sbuf[i].st_dev &&
124 sbuf[no_of_matches].st_ino == sbuf[i].st_ino)
125 goto next; /* Skip dups */
128 #endif /* HAVE_STAT */
131 else if (access (try,F_OK) == 0)
132 fprintf(stderr,"Warning: %s exists, but is not readable\n",try);
135 tried_source_dir = TRUE;
138 if ( no_of_matches == 0 && ! is_sys_import ) { /* Nothing so far */
140 /* If we are explicitly meddling about with .hi suffixes,
141 then some system-supplied modules may need to be looked
142 for with PreludeHiSuffix; unsavoury but true...
144 suffix_to_use = PreludeHiSuffix;
146 for (imports_dirs = sys_imports_dirlist;
147 tlist(imports_dirs) == lcons;
148 imports_dirs = ltl(imports_dirs))
150 char *dir = (char *) lhd(imports_dirs);
153 try_end = try + strlen(try);
155 #ifdef macintosh /* ToDo: use DIR_SEP_STRING */
156 if (*(try_end - 1) != ':')
157 strcpy (try_end++, ":");
159 if (*(try_end - 1) != '/')
160 strcpy (try_end++, "/");
161 #endif /* ! macintosh */
163 strcpy(try_end, module_name);
165 strcpy(try_end+modname_len, suffix_to_use);
167 /* See whether the file exists and is readable. */
168 if (access (try,R_OK) == 0)
170 if ( no_of_matches == 0 )
171 strcpy(returned_filename, try);
174 if ( no_of_matches < MAX_MATCH && stat(try, sbuf + no_of_matches) == 0 )
177 for (i = 0; i < no_of_matches; i++)
179 if ( sbuf[no_of_matches].st_dev == sbuf[i].st_dev &&
180 sbuf[no_of_matches].st_ino == sbuf[i].st_ino)
181 goto next_again; /* Skip dups */
184 #endif /* HAVE_STAT */
187 else if (access (try,F_OK) == 0)
188 fprintf(stderr,"Warning: %s exists, but is not readable\n",try);
196 switch ( no_of_matches ) {
198 fprintf(stderr,"Warning: found %d %s files for module \"%s\"\n",
199 no_of_matches, suffix_to_report, module_name);
203 char disaster_msg[MODNAME_SIZE+1000];
204 sprintf(disaster_msg,"can't find interface (%s) file for module \"%s\"%s",
205 suffix_to_report, module_name,
206 (strncmp(module_name, "PreludeGlaIO", 12) == 0)
207 ? "\n(The PreludeGlaIO interface no longer exists);"
209 (strncmp(module_name, "PreludePrimIO", 13) == 0)
210 ? "\n(The PreludePrimIO interface no longer exists -- just use PreludeGlaST);"
212 (strncmp(module_name, "Prelude", 7) == 0)
213 ? "\n(Perhaps you forgot a `-fglasgow-exts' flag?);"
216 hsperror(disaster_msg);
220 /* Everything is fine */