/* fopen by file system codepage */ FILE * fsyscp_fopen (const char *filename, const char *mode) { FILE *f; wchar_t *fnamew, modew[4]; int i; #if defined (KPSE_COMPAT_API) kpathsea kpse; #endif assert(filename && mode); fnamew = get_wstring_from_fsyscp(filename, fnamew=NULL); for(i=0; (modew[i]=(wchar_t)mode[i]); i++) {} /* mode[i] must be ASCII */ f = _wfopen(fnamew, modew); #if defined (KPSE_COMPAT_API) if (f != NULL) { kpse = kpse_def; if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) { DEBUGF_START (); fprintf (stderr, "fsyscp_fopen(%s [", filename); WriteConsoleW( GetStdHandle( STD_ERROR_HANDLE ), fnamew, wcslen( fnamew ), NULL, NULL ); #if defined(_WIN64) fprintf (stderr, "], %s) => 0x%I64x\n", mode, (unsigned __int64) f); #else fprintf (stderr, "], %s) => 0x%lx\n", mode, (unsigned long) f); #endif DEBUGF_END (); } } #endif free(fnamew); return f; }
/* xfopen by file system codepage */ FILE * kpathsea_fsyscp_xfopen (kpathsea kpse, const char *filename, const char *mode) { FILE *f; wchar_t *fnamew, modew[4]; int i; assert(filename && mode); fnamew = get_wstring_from_mbstring(kpse->File_system_codepage, filename, fnamew=NULL); for(i=0; (modew[i]=(wchar_t)mode[i]); i++) {} /* mode[i] must be ASCII */ f = _wfopen(fnamew, modew); if (f == NULL) FATAL_PERROR(filename); if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) { DEBUGF_START (); fprintf (stderr, "fsyscp_xfopen(%s [", filename); WriteConsoleW( GetStdHandle( STD_ERROR_HANDLE ), fnamew, wcslen( fnamew ), NULL, NULL ); #if defined(_WIN64) fprintf (stderr, "], %s) => 0x%I64x\n", mode, (unsigned __int64) f); #else fprintf (stderr, "], %s) => 0x%lx\n", mode, (unsigned long) f); #endif DEBUGF_END (); } free(fnamew); return f; }
FILE * fsyscp_popen (const char *command, const char *mode) { FILE *f; wchar_t *commandw, modew[4]; int i; #if defined (KPSE_COMPAT_API) kpathsea kpse; #endif assert(command && mode); if (is_include_space (command)) { const char *p; char *command2, *q; command2 = xmalloc (strlen (command) + 3); p = command; q = command2; *q++ = '\"'; while (*p) *q++ = *p++; *q++ = '\"'; *q = '\0'; commandw = get_wstring_from_fsyscp(command2, commandw=NULL); free (command2); } else { commandw = get_wstring_from_fsyscp(command, commandw=NULL); } for(i=0; (modew[i]=(wchar_t)mode[i]); i++) {} /* mode[i] must be ASCII */ f = _wpopen(commandw, modew); #if defined (KPSE_COMPAT_API) if (f != NULL) { kpse = kpse_def; if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) { DEBUGF_START (); fprintf (stderr, "fsyscp_popen(%s [", command); WriteConsoleW( GetStdHandle( STD_ERROR_HANDLE ), commandw, wcslen( commandw ), NULL, NULL ); #if defined(_WIN64) fprintf (stderr, "], %s) => 0x%I64x\n", mode, (unsigned __int64) f); #else fprintf (stderr, "], %s) => 0x%lx\n", mode, (unsigned long) f); #endif DEBUGF_END (); } } #endif free (commandw); /* We use always binary mode on Windows */ if(f) _setmode (fileno (f), _O_BINARY); return f; }
static boolean alias_build (kpathsea kpse, hash_table_type *table, const_string alias_filename) { string line, real, alias; unsigned count = 0; FILE *alias_file = fopen (alias_filename, FOPEN_R_MODE); if (alias_file) { while ((line = read_line (alias_file)) != NULL) { /* comments or empty */ if (*line == 0 || *line == '%' || *line == '#') { ; } else { /* Each line should have two fields: realname aliasname. */ real = line; while (*real && ISSPACE (*real)) real++; alias = real; while (*alias && !ISSPACE (*alias)) alias++; *alias++ = 0; while (*alias && ISSPACE (*alias)) alias++; /* Is the check for errors strong enough? Should we warn the user for potential errors? */ if (strlen (real) != 0 && strlen (alias) != 0) { /* Stuff in the alias file should be normalized. */ hash_insert_normalized (table, xstrdup (alias), xstrdup (real)); count++; } } free (line); } #ifdef KPSE_DEBUG if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH)) { /* As with ls-R above ... */ boolean hash_summary_only = true; DEBUGF2 ("%s: %u aliases.\n", alias_filename, count); DEBUGF ("alias hash table:"); hash_print (*table, hash_summary_only); fflush (stderr); } #endif /* KPSE_DEBUG */ xfclose (alias_file, alias_filename); } return alias_file != NULL; }
int fclose (FILE * f) { #undef fclose int ret = fclose (f); #if defined (KPSE_COMPAT_API) kpathsea kpse = kpse_def; if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) #if defined(_WIN64) DEBUGF2 ("fclose(0x%I64x) => %d\n", (unsigned __int64) f, ret); #else DEBUGF2 ("fclose(0x%lx) => %d\n", (unsigned long) f, ret); #endif #endif return ret; }
FILE * fopen (const char *filename, const char *mode) { #undef fopen FILE *ret = fopen (filename, mode); #if defined (KPSE_COMPAT_API) kpathsea kpse = kpse_def; if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) #if defined(_WIN64) DEBUGF3 ("fopen(%s, %s) => 0x%I64x\n", filename, mode, (unsigned __int64) ret); #else DEBUGF3 ("fopen(%s, %s) => 0x%lx\n", filename, mode, (unsigned long) ret); #endif #endif return ret; }
const_string * hash_lookup (hash_table_type table, const_string key) { hash_element_type *p; cstr_list_type ret; unsigned n = hash (table, key); ret = cstr_list_init (); /* Look at everything in this bucket. */ for (p = table.buckets[n]; p != NULL; p = p->next) if (FILESTRCASEEQ (key, p->key)) cstr_list_add (&ret, p->value); /* If we found anything, mark end of list with null. */ if (STR_LIST (ret)) cstr_list_add (&ret, NULL); #ifdef KPSE_DEBUG #if defined (KPSE_COMPAT_API) { kpathsea kpse = kpse_def; if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH)) { DEBUGF1 ("hash_lookup(%s) =>", key); if (!STR_LIST (ret)) fputs (" (nil)\n", stderr); else { const_string *r; for (r = STR_LIST (ret); *r; r++) { putc (' ', stderr); if (kpse->debug_hash_lookup_int) fprintf (stderr, "%ld", (long) *r); else fputs (*r, stderr); } putc ('\n', stderr); } fflush (stderr); } } #endif #endif return STR_LIST (ret); }
str_llist_type * kpathsea_element_dirs (kpathsea kpse, string elt) { str_llist_type *ret; unsigned i; /* If given nothing, return nothing. */ if (!elt || !*elt) return NULL; /* Normalize ELT before looking for a cached value. */ i = kpathsea_normalize_path (kpse, elt); /* If we've already cached the answer for ELT, return it. */ ret = cached (kpse, elt); if (ret) return ret; /* We're going to have a real directory list to return. */ ret = XTALLOC1 (str_llist_type); *ret = NULL; /* We handle the hard case in a subroutine. */ expand_elt (kpse, ret, elt, i); /* Remember the directory list we just found, in case future calls are made with the same ELT. */ cache (kpse, elt, ret); #ifdef KPSE_DEBUG if (KPATHSEA_DEBUG_P (KPSE_DEBUG_EXPAND)) { DEBUGF1 ("path element %s =>", elt); if (ret) { str_llist_elt_type *e; for (e = *ret; e; e = STR_LLIST_NEXT (*e)) fprintf (stderr, " %s", STR_LLIST (*e)); } putc ('\n', stderr); fflush (stderr); } #endif /* KPSE_DEBUG */ return ret; }
string kpathsea_var_value (kpathsea kpse, const_string var) { string vtry, ret; const_string value; assert (kpse->program_name); /* First look for VAR.progname. */ vtry = concat3 (var, ".", kpse->program_name); value = getenv (vtry); free (vtry); if (!value || !*value) { /* Now look for VAR_progname. */ vtry = concat3 (var, "_", kpse->program_name); value = getenv (vtry); free (vtry); } /* Just plain VAR. */ if (!value || !*value) value = getenv (var); /* Not in the environment; check a config file. */ if (!value || !*value) value = kpathsea_cnf_get (kpse, var); /* We have a value; do variable and tilde expansion. We want to use ~ in the cnf files, to adapt nicely to Windows and to avoid extra /'s (see tilde.c), but we also want kpsewhich -var-value=foo to not have any literal ~ characters, so our shell scripts don't have to worry about doing the ~ expansion. */ ret = value ? kpathsea_expand (kpse, value) : NULL; #ifdef KPSE_DEBUG if (KPATHSEA_DEBUG_P (KPSE_DEBUG_VARS)) DEBUGF2("variable: %s = %s\n", var, ret ? ret : "(nil)"); #endif return ret; }
unsigned kpathsea_normalize_path (kpathsea kpse, string elt) { unsigned ret; unsigned i; #if defined(WIN32) for (i = 0; elt[i]; i++) { if (elt[i] == '\\') elt[i] = '/'; else if (IS_KANJI(elt + i)) i++; } #endif if (NAME_BEGINS_WITH_DEVICE(elt)) { if (*elt >= 'A' && *elt <= 'Z') *elt += 'a' - 'A'; ret = 2; } else if (IS_UNC_NAME(elt)) { for (ret = 2; elt[ret] && !IS_DIR_SEP_CH(elt[ret]); ret++) ; } else ret = 0; for (i = ret; IS_DIR_SEP_CH(elt[i]); ++i) ; if (i > ret + 1) { #ifdef KPSE_DEBUG if (KPATHSEA_DEBUG_P (KPSE_DEBUG_STAT)) DEBUGF2 ("kpse_normalize_path (%s) => %u\n", elt, ret); #endif /* KPSE_DEBUG */ memmove (elt + ret + 1, elt + i, strlen (elt + i) + 1); } return ret; }
static boolean db_build (kpathsea kpse, hash_table_type *table, const_string db_filename) { string line; unsigned dir_count = 0, file_count = 0, ignore_dir_count = 0; unsigned len = strlen (db_filename) - sizeof (DB_NAME) + 1; /* Keep the /. */ string top_dir = (string)xmalloc (len + 1); string cur_dir = NULL; /* First thing in ls-R might be a filename. */ FILE *db_file = fopen (db_filename, FOPEN_R_MODE); #if defined(WIN32) string pp; #endif strncpy (top_dir, db_filename, len); top_dir[len] = 0; if (db_file) { while ((line = read_line (db_file)) != NULL) { len = strlen (line); #if defined(WIN32) for (pp = line; *pp; pp++) { if (IS_KANJI(pp)) pp++; else *pp = TRANSFORM(*pp); } #endif /* A line like `/foo:' = new dir foo. Allow both absolute (/...) and explicitly relative (./...) names here. It's a kludge to pass in the directory name with the trailing : still attached, but it doesn't actually hurt. */ if (len > 0 && line[len - 1] == ':' && kpathsea_absolute_p (kpse, line, true)) { /* New directory line. */ if (!ignore_dir_p (line)) { /* If they gave a relative name, prepend full directory name now. */ line[len - 1] = DIR_SEP; /* Skip over leading `./', it confuses `match' and is just a waste of space, anyway. This will lose on `../', but `match' won't work there, either, so it doesn't matter. */ cur_dir = *line == '.' ? concat (top_dir, line + 2) : xstrdup (line); dir_count++; } else { cur_dir = NULL; ignore_dir_count++; } /* Ignore blank, `.' and `..' lines. */ } else if (*line != 0 && cur_dir /* a file line? */ && !(*line == '.' && (line[1] == 0 || (line[1] == '.' && line[2] == 0)))) { /* Make a new hash table entry with a key of `line' and a data of `cur_dir'. An already-existing identical key is ok, since a file named `foo' can be in more than one directory. Share `cur_dir' among all its files (and hence never free it). Note that we assume that all names in the ls-R file have already been case-smashed to lowercase where appropriate. */ hash_insert_normalized (table, xstrdup (line), cur_dir); file_count++; } /* else ignore blank lines or top-level files or files in ignored directories*/ free (line); } xfclose (db_file, db_filename); if (file_count == 0) { WARNING1 ("kpathsea: %s: No usable entries in ls-R", db_filename); WARNING ("kpathsea: See the manual for how to generate ls-R"); db_file = NULL; } else { str_list_add (&(kpse->db_dir_list), xstrdup (top_dir)); } #ifdef KPSE_DEBUG if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH)) { /* Don't make this a debugging bit, since the output is so voluminous, and being able to specify -1 is too useful. Instead, let people who want it run the program under a debugger and change the variable that way. */ boolean hash_summary_only = true; DEBUGF4 ("%s: %u entries in %d directories (%d hidden).\n", db_filename, file_count, dir_count, ignore_dir_count); DEBUGF ("ls-R hash table:"); hash_print (*table, hash_summary_only); fflush (stderr); } #endif /* KPSE_DEBUG */ } free (top_dir); return db_file != NULL; }
str_list_type * kpathsea_db_search_list (kpathsea kpse, string* names, const_string path_elt, boolean all) { const_string *db_dirs, *orig_dirs; const_string last_slash, name, path; string temp_str = NULL; boolean done; unsigned e; const_string *aliases, *r; int n; str_list_type *ret = NULL; boolean relevant = false; /* If we failed to build the database (or if this is the recursive call to build the db path), quit. */ if (kpse->db.buckets == NULL) return NULL; /* Don't bother doing any lookups if this `path_elt' isn't covered by any of database directories. We do this not so much because the extra couple of hash lookups matter -- they don't -- but rather because we want to return NULL in this case, so path_search can know to do a disk search. */ for (e = 0; !relevant && e < STR_LIST_LENGTH (kpse->db_dir_list); e++) { relevant = elt_in_db (STR_LIST_ELT (kpse->db_dir_list, e), path_elt); } if (!relevant) return NULL; done = false; ret = XTALLOC1 (str_list_type); *ret = str_list_init (); /* Handle each name. */ for (n = 0; !done && names[n]; n++) { name = names[n]; /* Absolute names should have been caught in our caller. */ if (kpathsea_absolute_p(kpse, name, true)) continue; /* When tex-glyph.c calls us looking for, e.g., dpi600/cmr10.pk, we won't find it unless we change NAME to just `cmr10.pk' and append `/dpi600' to PATH_ELT. We are justified in using a literal `/' here, since that's what tex-glyph.c unconditionally uses in DPI_BITMAP_SPEC. But don't do anything if the / begins NAME; that should never happen. */ last_slash = strrchr (name, '/'); if (last_slash && last_slash != name) { unsigned len = last_slash - name + 1; string dir_part = (string)xmalloc (len); strncpy (dir_part, name, len - 1); dir_part[len - 1] = 0; path = temp_str = concat3 (path_elt, "/", dir_part); name = last_slash + 1; free (dir_part); } else { path = path_elt; } /* If we have aliases for this name, use them. */ if (kpse->alias_db.buckets) aliases = hash_lookup (kpse->alias_db, name); else aliases = NULL; if (!aliases) { aliases = XTALLOC1 (const_string); aliases[0] = NULL; } { /* Push aliases up by one and insert the original name at front. */ unsigned i; unsigned len = 1; /* Have NULL element already allocated. */ for (r = aliases; *r; r++) len++; aliases = (const_string *) xrealloc ((void *) aliases, (len + 1) * sizeof(const_string)); for (i = len; i > 0; i--) { aliases[i] = aliases[i - 1]; } aliases[0] = name; } for (r = aliases; !done && *r; r++) { const_string ctry = *r; /* We have an ls-R db. Look up `try'. */ orig_dirs = db_dirs = hash_lookup (kpse->db, ctry); /* For each filename found, see if it matches the path element. For example, if we have .../cx/cmr10.300pk and .../ricoh/cmr10.300pk, and the path looks like .../cx, we don't want the ricoh file. */ while (!done && db_dirs && *db_dirs) { string db_file = concat (*db_dirs, ctry); boolean matched = match (db_file, path); #ifdef KPSE_DEBUG if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) DEBUGF3 ("db:match(%s,%s) = %d\n", db_file, path, matched); #endif /* We got a hit in the database. Now see if the file actually exists, possibly under an alias. */ if (matched) { string found = NULL; if (kpathsea_readable_file (kpse, db_file)) { found = db_file; } else { const_string *a; free (db_file); /* `db_file' wasn't on disk. */ /* The hit in the DB doesn't exist in disk. Now try all its aliases. For example, suppose we have a hierarchy on CD, thus `mf.bas', but ls-R contains `mf.base'. Find it anyway. Could probably work around this with aliases, but this is pretty easy and shouldn't hurt. The upshot is that if one of the aliases actually exists, we use that. */ for (a = aliases + 1; *a && !found; a++) { string atry = concat (*db_dirs, *a); if (kpathsea_readable_file (kpse, atry)) found = atry; else free (atry); } } /* If we have a real file, add it to the list, maybe done. */ if (found) { str_list_add (ret, found); if (!all && found) done = true; } } else { /* no match in the db */ free (db_file); } /* On to the next directory, if any. */ db_dirs++; } /* This is just the space for the pointers, not the strings. */ if (orig_dirs && *orig_dirs) free (orig_dirs); } free ((void *) aliases); if (temp_str) free (temp_str); } return ret; }
void kpathsea_init_db (kpathsea kpse) { const_string db_path; string *db_files; string *orig_db_files; str_list_type unique_list; int dbi; boolean ok = false; assert (sizeof(DB_NAME) == sizeof(DB_NAME_LC)); db_path = kpathsea_init_format (kpse, kpse_db_format); db_files = kpathsea_path_search_list_generic (kpse, db_path, db_names, true, true); orig_db_files = db_files; /* Mac OS X and others can use a case-insensitive, case-preserving filesystem by default, in which case ls-R and ls-r point to the same file. Also, Windows is case-insensitive. In these cases, we want to avoid reading the same file multiple times. */ dbi = 0; unique_list = str_list_init (); while (db_files[dbi] != NULL) { string path1 = db_files[dbi]; string path2 = db_files[dbi + 1]; /* first-pass check in case path1/path2 aren't even potentially equal; mainly in case the order from kpathsea_path_search_list_generic changes. */ if (path2 && strcasecmp (path1, path2) == 0 && same_file_p (path1, path2)) { /* they are the same, skip over path1, we'll add path2 on the next iteration (when it's path1). */ #ifdef KPSE_DEBUG if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH)) { DEBUGF2 ("db:init(): skipping db same_file_p %s, will add %s.\n", path1, path2); } #endif free (path1); } else { /* they are not the same, add path1. */ #ifdef KPSE_DEBUG if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH)) { DEBUGF1 ("db:init(): using db file %s.\n", path1); } #endif str_list_add (&unique_list, path1); } /* could be more clever and increment by two, but then would have to avoid jumping off the end of db_files */ dbi++; } /* always add a NULL terminator. */ str_list_add (&unique_list, NULL); free (orig_db_files); db_files = STR_LIST (unique_list); orig_db_files = db_files; /* Must do this after the path searching (which ends up calling kpse_db_search recursively), so kpse->db.buckets stays NULL. */ kpse->db = hash_create (DB_HASH_SIZE); while (db_files && *db_files) { if (db_build (kpse, &(kpse->db), *db_files)) ok = true; free (*db_files); db_files++; } if (!ok) { /* If db can't be built, leave `size' nonzero (so we don't rebuild it), but clear `buckets' (so we don't look in it). */ free (kpse->db.buckets); kpse->db.buckets = NULL; } free (orig_db_files); /* Add the content of any alias databases. There may exist more than one alias file along DB_NAME files. This duplicates the above code -- should be a function. */ ok = false; db_files = kpathsea_all_path_search (kpse, db_path, ALIAS_NAME); orig_db_files = db_files; kpse->alias_db = hash_create (ALIAS_HASH_SIZE); while (db_files && *db_files) { if (alias_build (kpse, &(kpse->alias_db), *db_files)) ok = true; free (*db_files); db_files++; } if (!ok) { free (kpse->alias_db.buckets); kpse->alias_db.buckets = NULL; } free (orig_db_files); }