annotate liboctave/kpse.cc @ 10411:479cc8a0a846

use gnulib namespace
author John W. Eaton <jwe@octave.org>
date Mon, 15 Mar 2010 15:57:23 -0400
parents 07ebe522dac2
children 6ccf1130f23c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1 // This file is not compiled to a separate object file. It is
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2 // included in pathsearch.cc.
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
3
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
4 /* Look up a filename in a path.
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
5
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
6 Copyright (C) 1993, 94, 95, 96, 97, 98 Karl Berry.
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
7 Copyright (C) 1993, 94, 95, 96, 97 Karl Berry & O. Weber.
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
8 Copyright (C) 1992, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
9
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
10 This library is free software; you can redistribute it and/or
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
11 modify it under the terms of the GNU Library General Public
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
12 License as published by the Free Software Foundation; either
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6306
diff changeset
13 version 2 of the License, or (at your option) any later version.
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
14
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
15 This library is distributed in the hope that it will be useful,
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
18 Library General Public License for more details.
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
19
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
20 You should have received a copy of the GNU Library General Public
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
21 License along with this library; if not, write to the Free Software
5307
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5137
diff changeset
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5137
diff changeset
23 02110-1301, USA. */
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
24
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
25 #if defined (HAVE_CONFIG_H)
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
26 #include <config.h>
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
27 #endif
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
28
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
29 #include <map>
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
30 #include <string>
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
31
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
32 /* System defines are for non-Unix systems only. (Testing for all Unix
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
33 variations should be done in configure.) Presently the defines used
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
34 are: DOS OS2 WIN32. I do not use any of these systems
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
35 myself; if you do, I'd be grateful for any changes. --kb@mail.tug.org */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
36
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
37 /* If we have either DOS or OS2, we are DOSISH. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
38 #if defined (DOS) || defined (OS2) || defined (WIN32) || defined(__MSDOS__)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
39 #define DOSISH
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
40 #endif
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
41
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
42 #if defined (DOSISH)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
43 #define MONOCASE_FILENAMES /* case-insensitive filename comparisons */
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
44 #endif
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
45
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
46 extern "C" {
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
47 #if defined(__MINGW32__)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
48 #include <windows.h>
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
49 #include <fcntl.h>
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
50 #include <dirent.h>
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
51 #elif defined(WIN32)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
52 #define __STDC__ 1
6106
82fff8a063c2 [project @ 2006-10-26 21:52:14 by jwe]
jwe
parents: 5765
diff changeset
53 #ifndef _MSC_VER
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
54 #include "win32lib.h"
6106
82fff8a063c2 [project @ 2006-10-26 21:52:14 by jwe]
jwe
parents: 5765
diff changeset
55 #endif
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
56 #endif /* not WIN32 */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
57
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
58 #ifdef __DJGPP__
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
59 #include <fcntl.h> /* for long filenames' stuff */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
60 #include <dir.h> /* for `getdisk' */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
61 #include <io.h> /* for `setmode' */
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
62 #endif
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
63 }
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
64
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
65 /* Some drivers have partially integrated kpathsea changes. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
66 #ifndef KPATHSEA
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
67 #define KPATHSEA 32
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
68 #endif
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
69
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
70 /* System dependencies that are figured out by `configure'. If we are
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
71 compiling standalone, we get our c-auto.h. Otherwise, the package
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
72 containing us must provide this (unless it can somehow generate ours
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
73 from c-auto.in). We use <...> instead of "..." so that the current
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
74 cpp directory (i.e., kpathsea/) won't be searched. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
75
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
76 /* If you want to find subdirectories in a directory with non-Unix
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
77 semantics (specifically, if a directory with no subdirectories does
5622
8b7046423d1e [project @ 2006-02-15 16:36:32 by jwe]
jwe
parents: 5618
diff changeset
78 not have exactly two links), define this. */
8b7046423d1e [project @ 2006-02-15 16:36:32 by jwe]
jwe
parents: 5618
diff changeset
79 #if defined(__DJGPP__) || ! defined (DOSISH)
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
80 /* Surprise! DJGPP returns st_nlink exactly like on Unix. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
81 #define ST_NLINK_TRICK
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
82 #endif /* either not DOSISH or __DJGPP__ */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
83
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
84 #ifdef OS2
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
85 #define access ln_access
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
86 #define fopen ln_fopen
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
87 #define rename ln_rename
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
88 #define stat ln_stat
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
89 #endif /* OS2 */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
90
4379
0cbcb9d8b4ff [project @ 2003-04-19 00:37:05 by jwe]
jwe
parents: 4378
diff changeset
91 #include "kpse-xfns.h"
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
92
4396
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
93 #include "lo-error.h"
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
94 #include "oct-env.h"
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
95 #include "oct-passwd.h"
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
96 #include "str-vec.h"
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
97
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
98 /* Header files that essentially all of our sources need, and
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
99 that all implementations have. We include these first, to help with
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
100 NULL being defined multiple times. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
101 #include <cstdio>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
102 #include <cstdarg>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
103 #include <cstdlib>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
104 #include <climits>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
105 #include <cerrno>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
106 #include <cassert>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
107
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
108 #include <sys/types.h>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
109 #include <unistd.h>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
110
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
111 #include "sysdir.h"
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
112 #include "statdefs.h"
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
113
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
114 /* define NAME_MAX, the maximum length of a single
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
115 component in a filename. No such limit may exist, or may vary
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
116 depending on the filesystem. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
117
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
118 /* Most likely the system will truncate filenames if it is not POSIX,
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
119 and so we can use the BSD value here. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
120 #ifndef _POSIX_NAME_MAX
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
121 #define _POSIX_NAME_MAX 255
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
122 #endif
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
123
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
124 #ifndef NAME_MAX
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
125 #define NAME_MAX _POSIX_NAME_MAX
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
126 #endif
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
127
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
128 #include <cctype>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
129
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
130 /* What separates elements in environment variable path lists? */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
131 #ifndef ENV_SEP
5451
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
132 #if defined (SEPCHAR) && defined (SEPCHAR_STR)
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
133 #define ENV_SEP SEPCHAR
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
134 #define ENV_SEP_STRING SEPCHAR_STR
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
135 #elif defined (DOSISH)
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
136 #define ENV_SEP ';'
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
137 #define ENV_SEP_STRING ";"
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
138 #else
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
139 #define ENV_SEP ':'
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
140 #define ENV_SEP_STRING ":"
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
141 #endif /* not DOS */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
142 #endif /* not ENV_SEP */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
143
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
144 #ifndef IS_ENV_SEP
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
145 #define IS_ENV_SEP(ch) ((ch) == ENV_SEP)
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
146 #endif
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
147
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
148 /* define PATH_MAX, the maximum length of a filename. Since no such
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
149 limit may exist, it's preferable to dynamically grow filenames as
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
150 needed. */
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
151
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
152 /* Cheat and define this as a manifest constant no matter what, instead
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
153 of using pathconf. I forget why we want to do this. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
154
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
155 #ifndef _POSIX_PATH_MAX
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
156 #define _POSIX_PATH_MAX 255
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
157 #endif
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
158
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
159 #ifndef PATH_MAX
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
160 #ifdef MAXPATHLEN
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
161 #define PATH_MAX MAXPATHLEN
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
162 #else
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
163 #define PATH_MAX _POSIX_PATH_MAX
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
164 #endif
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
165 #endif /* not PATH_MAX */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
166
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
167 /* If NO_DEBUG is defined (not recommended), skip all this. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
168 #ifndef NO_DEBUG
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
169
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
170 /* OK, we'll have tracing support. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
171 #define KPSE_DEBUG
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
172
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
173 /* Test if a bit is on. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
174 #define KPSE_DEBUG_P(bit) (kpathsea_debug & (1 << (bit)))
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
175
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
176 #define KPSE_DEBUG_STAT 0 /* stat calls */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
177 #define KPSE_DEBUG_HASH 1 /* hash lookups */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
178 #define KPSE_DEBUG_FOPEN 2 /* fopen/fclose calls */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
179 #define KPSE_DEBUG_PATHS 3 /* search path initializations */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
180 #define KPSE_DEBUG_EXPAND 4 /* path element expansion */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
181 #define KPSE_DEBUG_SEARCH 5 /* searches */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
182 #define KPSE_DEBUG_VARS 6 /* variable values */
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
183 #define KPSE_LAST_DEBUG KPSE_DEBUG_VARS
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
184
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
185 /* A printf for the debugging. */
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
186 #define DEBUGF_START() do { gnulib::fputs ("kdebug:", stderr)
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
187 #define DEBUGF_END() fflush (stderr); } while (0)
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
188
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
189 #define DEBUGF(str) \
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
190 DEBUGF_START (); gnulib::fputs (str, stderr); DEBUGF_END ()
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
191 #define DEBUGF1(str, e1) \
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
192 DEBUGF_START (); gnulib::fprintf (stderr, str, e1); DEBUGF_END ()
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
193 #define DEBUGF2(str, e1, e2) \
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
194 DEBUGF_START (); gnulib::fprintf (stderr, str, e1, e2); DEBUGF_END ()
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
195 #define DEBUGF3(str, e1, e2, e3) \
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
196 DEBUGF_START (); gnulib::fprintf (stderr, str, e1, e2, e3); DEBUGF_END ()
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
197 #define DEBUGF4(str, e1, e2, e3, e4) \
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
198 DEBUGF_START (); gnulib::fprintf (stderr, str, e1, e2, e3, e4); DEBUGF_END ()
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
199
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
200 #undef fopen
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
201 #define fopen kpse_fopen_trace
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
202 static FILE *fopen (const char *filename, const char *mode);
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
203
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
204 #endif /* not NO_DEBUG */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
205
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
206 #ifdef KPSE_DEBUG
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
207 static unsigned int kpathsea_debug = 0;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
208 #endif
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
209
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
210 #if defined (WIN32) && !defined (__MINGW32__)
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
211
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
212 /* System description file for Windows NT. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
213
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
214 /*
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
215 * Define symbols to identify the version of Unix this is.
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
216 * Define all the symbols that apply correctly.
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
217 */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
218
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
219 #ifndef DOSISH
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
220 #define DOSISH
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
221 #endif
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
222
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
223 #ifndef MAXPATHLEN
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
224 #define MAXPATHLEN _MAX_PATH
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
225 #endif
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
226
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
227 /* These have to be defined because our compilers treat __STDC__ as being
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
228 defined (most of them anyway). */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
229
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
230 #define access _access
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
231 #define stat _stat
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
232 #define strdup _strdup
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
233
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
234 #define S_IFMT _S_IFMT
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
235 #define S_IFDIR _S_IFDIR
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
236
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
237 /* Define this so that winsock.h definitions don't get included when
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
238 windows.h is... For this to have proper effect, config.h must
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
239 always be included before windows.h. */
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
240 #define _WINSOCKAPI_ 1
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
241
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
242 #include <windows.h>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
243
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
244 /* For proper declaration of environ. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
245 #include <io.h>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
246 #include <fcntl.h>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
247 #include <process.h>
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
248
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
249 /* ============================================================ */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
250
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
251 #endif /* WIN32 */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
252
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
253 /* Define common sorts of messages. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
254
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
255 /* This should be called only after a system call fails. Don't exit
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
256 with status `errno', because that might be 256, which would mean
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
257 success (exit statuses are truncated to eight bits). */
4396
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
258 #define FATAL_PERROR(str) \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
259 do \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
260 { \
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
261 gnulib::fputs ("pathsearch: ", stderr); \
4396
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
262 perror (str); exit (EXIT_FAILURE); \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
263 } \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
264 while (0)
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
265
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
266 #define FATAL(str) \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
267 do \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
268 { \
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
269 gnulib::fputs ("pathsearch: fatal: ", stderr); \
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
270 gnulib::fputs (str, stderr); \
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
271 gnulib::fputs (".\n", stderr); \
4396
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
272 exit (1); \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
273 } \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
274 while (0)
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
275
4386
112a509bd2e6 [project @ 2003-04-24 18:41:02 by jwe]
jwe
parents: 4385
diff changeset
276 #ifndef WIN32
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
277 static void xclosedir (DIR *d);
4386
112a509bd2e6 [project @ 2003-04-24 18:41:02 by jwe]
jwe
parents: 4385
diff changeset
278 #endif
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
279
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
280 /* It's a little bizarre to be using the same type for the list and the
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
281 elements of the list, but no reason not to in this case, I think --
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
282 we never need a NULL string in the middle of the list, and an extra
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
283 NULL/NULL element always at the end is inconsequential. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
284
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
285 struct str_llist_elt
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
286 {
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
287 std::string str;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
288 int moved;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
289 struct str_llist_elt *next;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
290 };
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
291
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
292 typedef str_llist_elt str_llist_elt_type;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
293 typedef str_llist_elt *str_llist_type;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
294
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
295 #define STR_LLIST(sl) ((sl).str)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
296 #define STR_LLIST_MOVED(sl) ((sl).moved)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
297 #define STR_LLIST_NEXT(sl) ((sl).next)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
298
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
299 static void str_llist_add (str_llist_type *l, const std::string& str);
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
300
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
301 static void str_llist_float (str_llist_type *l, str_llist_elt_type *mover);
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
302
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
303 static std::string kpse_var_expand (const std::string& src);
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
304
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
305 static str_llist_type *kpse_element_dirs (const std::string& elt);
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
306
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
307 static std::string kpse_expand (const std::string& s);
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
308
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
309 static std::string kpse_expand_default (const std::string& path,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
310 const std::string& dflt);
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
311
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
312 static string_vector kpse_db_search (const std::string& name,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
313 const std::string& path_elt, bool all);
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
314
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
315 #include <ctime> /* for `time' */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
316
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
317 static bool
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
318 kpse_is_env_sep (char c)
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
319 {
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
320 return IS_ENV_SEP (c);
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
321 }
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
322
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
323 /* These routines just check the return status from standard library
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
324 routines and abort if an error happens. */
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
325
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
326 static FILE *
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
327 xfopen (const std::string& filename, const char *mode)
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
328 {
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
329 FILE *f;
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
330
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
331 assert (! filename.empty () && mode);
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
332
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
333 f = fopen (filename.c_str (), mode);
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
334
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
335 if (! f)
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
336 FATAL_PERROR (filename.c_str ());
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
337
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
338 return f;
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
339 }
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
340
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
341 /* A single (key,value) pair. */
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
342
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
343 struct hash_element_type
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
344 {
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
345 std::string key;
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
346 std::string value;
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
347 struct hash_element_type *next;
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
348 };
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
349
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
350 /* The usual arrangement of buckets initialized to null. */
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
351
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
352 struct hash_table_type
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
353 {
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
354 hash_element_type **buckets;
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
355 unsigned size;
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
356 };
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
357
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
358 static unsigned
5764
80409c2defcc [project @ 2006-04-16 19:10:00 by jwe]
jwe
parents: 5760
diff changeset
359 kpse_hash (hash_table_type table, const std::string& key)
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
360 {
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
361 unsigned n = 0;
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
362
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
363 /* Our keys aren't often anagrams of each other, so no point in
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
364 weighting the characters. */
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
365 size_t len = key.length ();
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
366 for (size_t i = 0; i < len; i++)
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
367 n = (n + n + key[i]) % table.size;
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
368
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
369 return n;
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
370 }
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
371
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
372 /* Look up STR in MAP. Return a (dynamically-allocated) list of the
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
373 corresponding strings or NULL if no match. */
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
374
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
375 static string_vector
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
376 hash_lookup (hash_table_type table, const std::string& key)
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
377 {
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
378 hash_element_type *p;
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
379 string_vector ret;
5764
80409c2defcc [project @ 2006-04-16 19:10:00 by jwe]
jwe
parents: 5760
diff changeset
380 unsigned n = kpse_hash (table, key);
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
381
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
382 /* Look at everything in this bucket. */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
383 for (p = table.buckets[n]; p; p = p->next)
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
384 if (key == p->key)
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
385 ret.append (p->value);
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
386
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
387 #ifdef KPSE_DEBUG
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
388 if (KPSE_DEBUG_P (KPSE_DEBUG_HASH))
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
389 {
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
390 DEBUGF1 ("hash_lookup (%s) =>", key.c_str ());
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
391 if (ret.empty ())
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
392 gnulib::fputs (" (nil)\n", stderr);
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
393 else
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
394 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
395 int len = ret.length ();
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
396 for (int i = 0; i < len; i++)
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
397 {
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
398 gnulib::putc (' ', stderr);
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
399 gnulib::fputs (ret[i].c_str (), stderr);
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
400 }
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
401 gnulib::putc ('\n', stderr);
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
402 }
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
403 fflush (stderr);
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
404 }
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
405 #endif
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
406
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
407 return ret;
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
408 }
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
409
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
410 /* A way to step through a path, extracting one directory name at a
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
411 time. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
412
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
413 class kpse_path_iterator
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
414 {
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
415 public:
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
416
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
417 kpse_path_iterator (const std::string& p)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
418 : path (p), b (0), e (0), len (path.length ()) { set_end (); }
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
419
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
420 kpse_path_iterator (const kpse_path_iterator& pi)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
421 : path (pi.path), b (pi.b), e (pi.e), len (pi.len) { }
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
422
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
423 kpse_path_iterator operator ++ (int)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
424 {
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
425 kpse_path_iterator retval (*this);
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
426 next ();
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
427 return retval;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
428 }
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
429
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
430 std::string operator * (void) { return path.substr (b, e-b); }
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
431
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
432 bool operator != (const size_t sz) { return b != sz; }
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
433
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
434 private:
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
435
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
436 const std::string& path;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
437 size_t b;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
438 size_t e;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
439 size_t len;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
440
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
441 void set_end (void)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
442 {
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
443 e = b + 1;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
444
4412
34438519fac3 [project @ 2003-05-14 22:23:37 by jwe]
jwe
parents: 4409
diff changeset
445 if (e == len)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
446 ; /* OK, we have found the last element. */
4412
34438519fac3 [project @ 2003-05-14 22:23:37 by jwe]
jwe
parents: 4409
diff changeset
447 else if (e > len)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
448 b = e = std::string::npos;
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
449 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
450 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
451 /* Find the next colon not enclosed by braces (or the end of
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
452 the path). */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
453
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
454 int brace_level = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
455 while (e < len && ! (brace_level == 0 && kpse_is_env_sep (path[e])))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
456 e++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
457 }
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
458 }
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
459
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
460 void next (void)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
461 {
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
462 b = e + 1;
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
463
4412
34438519fac3 [project @ 2003-05-14 22:23:37 by jwe]
jwe
parents: 4409
diff changeset
464 /* Skip any consecutive colons. */
5618
36074b79a73b [project @ 2006-02-13 18:23:59 by jwe]
jwe
parents: 5617
diff changeset
465 while (b < len && kpse_is_env_sep (path[b]))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
466 b++;
4412
34438519fac3 [project @ 2003-05-14 22:23:37 by jwe]
jwe
parents: 4409
diff changeset
467
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
468 if (b >= len)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
469 b = e = std::string::npos;
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
470 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
471 set_end ();
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
472 }
5617
e92411b7eb21 [project @ 2006-02-13 17:21:31 by jwe]
jwe
parents: 5451
diff changeset
473
e92411b7eb21 [project @ 2006-02-13 17:21:31 by jwe]
jwe
parents: 5451
diff changeset
474 // No assignment.
e92411b7eb21 [project @ 2006-02-13 17:21:31 by jwe]
jwe
parents: 5451
diff changeset
475 kpse_path_iterator& operator = (const kpse_path_iterator&);
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
476 };
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
477
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
478 /* Here's the simple one, when a program just wants a value. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
479
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
480 static std::string
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
481 kpse_var_value (const std::string& var)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
482 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
483 std::string ret;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
484
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
485 std::string tmp = octave_env::getenv (var);
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
486
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
487 if (! tmp.empty ())
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
488 ret = kpse_var_expand (tmp);
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
489
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
490 #ifdef KPSE_DEBUG
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
491 if (KPSE_DEBUG_P (KPSE_DEBUG_VARS))
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
492 DEBUGF2 ("variable: %s = %s\n", var.c_str (),
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
493 tmp.empty () ? "(nil)" : tmp.c_str ());
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
494 #endif
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
495
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
496 return ret;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
497 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
498
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
499 /* Truncate any too-long components in NAME, returning the result. It's
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
500 too bad this is necessary. See comments in readable.c for why. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
501
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
502 static std::string
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
503 kpse_truncate_filename (const std::string& name)
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
504 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
505 unsigned c_len = 0; /* Length of current component. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
506 unsigned ret_len = 0; /* Length of constructed result. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
507
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
508 std::string ret = name;
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
509
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
510 size_t len = name.length ();
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
511
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
512 for (size_t i = 0; i < len; i++)
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
513 {
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
514 if (IS_DIR_SEP (name[i]) || IS_DEVICE_SEP (name[i]))
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
515 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
516 /* At a directory delimiter, reset component length. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
517 c_len = 0;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
518 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
519 else if (c_len > NAME_MAX)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
520 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
521 /* If past the max for a component, ignore this character. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
522 continue;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
523 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
524
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
525 /* Copy this character. */
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
526 ret[ret_len++] = name[i];
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
527 c_len++;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
528 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
529
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
530 ret.resize (ret_len);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
531
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
532 return ret;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
533 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
534
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
535 /* If access can read FN, run stat (assigning to stat buffer ST) and
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
536 check that fn is not a directory. Don't check for just being a
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
537 regular file, as it is potentially useful to read fifo's or some
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
538 kinds of devices. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
539
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
540 #ifdef WIN32
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
541 static inline bool
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
542 READABLE (const std::string& fn, struct stat&)
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
543 {
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
544 const char *t = fn.c_str ();
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
545 return (GetFileAttributes (t) != 0xFFFFFFFF
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
546 && ! (GetFileAttributes (t) & FILE_ATTRIBUTE_DIRECTORY));
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
547 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
548 #else
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
549 static inline bool
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
550 READABLE (const std::string& fn, struct stat& st)
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
551 {
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
552 const char *t = fn.c_str ();
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
553 return (access (t, R_OK) == 0
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
554 && stat (t, &(st)) == 0 && ! S_ISDIR (st.st_mode));
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
555 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
556 #endif
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
557
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
558 /* POSIX invented the brain-damage of not necessarily truncating
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
559 filename components; the system's behavior is defined by the value of
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
560 the symbol _POSIX_NO_TRUNC, but you can't change it dynamically!
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
561
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
562 Generic const return warning. See extend-fname.c. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
563
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
564 static std::string
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
565 kpse_readable_file (const std::string& name)
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
566 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
567 struct stat st;
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
568 std::string ret;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
569
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
570 if (READABLE (name, st))
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
571 {
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
572 ret = name;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
573
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
574 #ifdef ENAMETOOLONG
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
575 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
576 else if (errno == ENAMETOOLONG)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
577 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
578 ret = kpse_truncate_filename (name);
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
579
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
580 /* Perhaps some other error will occur with the truncated name,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
581 so let's call access again. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
582
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
583 if (! READABLE (ret, st))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
584 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
585 /* Failed. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
586 ret = std::string ();
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
587 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
588 #endif /* ENAMETOOLONG */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
589
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
590 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
591 else
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
592 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
593 /* Some other error. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
594 if (errno == EACCES)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
595 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
596 /* Maybe warn them if permissions are bad. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
597 perror (name.c_str ());
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
598 }
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
599
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
600 ret = std::string ();
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
601 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
602
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
603 return ret;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
604 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
605
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
606 /* Sorry this is such a system-dependent mess, but I can't see any way
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
607 to usefully generalize. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
608
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
609 static bool
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
610 kpse_absolute_p (const std::string& filename, int relative_ok)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
611 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
612 size_t len = filename.length ();
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
613
4409
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
614 int absolute = (len > 0 && IS_DIR_SEP (filename[0]))
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
615 #ifdef DOSISH
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
616 /* Novell allows non-alphanumeric drive letters. */
4409
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
617 || (len > 0 && IS_DEVICE_SEP (filename[1]))
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
618 #endif /* DOSISH */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
619 #ifdef WIN32
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
620 /* UNC names */
4409
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
621 || (len > 1 && filename[0] == '\\' && filename[1] == '\\')
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
622 #endif
4409
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
623 ;
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
624
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
625 int explicit_relative
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
626 = relative_ok
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
627 && (len > 1
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
628 && filename[0] == '.'
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
629 && (IS_DIR_SEP (filename[1])
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
630 || (len > 2 && filename[1] == '.' && IS_DIR_SEP (filename[2]))));
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
631
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
632 return absolute || explicit_relative;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
633 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
634
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
635 /* The very first search is for texmf.cnf, called when someone tries to
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
636 initialize the TFM path or whatever. init_path calls kpse_cnf_get
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
637 which calls kpse_all_path_search to find all the texmf.cnf's. We
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
638 need to do various special things in this case, since we obviously
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
639 don't yet have the configuration files when we're searching for the
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
640 configuration files. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
641 static bool first_search = true;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
642
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
643 /* This function is called after every search (except the first, since
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
644 we definitely want to allow enabling the logging in texmf.cnf) to
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
645 record the filename(s) found in $TEXMFLOG. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
646
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
647 static void
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
648 log_search (const string_vector& filenames)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
649 {
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
650 static FILE *log_file = 0;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
651 static bool first_time = true; /* Need to open the log file? */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
652
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
653 if (first_time)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
654 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
655 first_time = false;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
656
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
657 /* Get name from either envvar or config file. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
658 std::string log_name = kpse_var_value ("TEXMFLOG");
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
659
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
660 if (! log_name.empty ())
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
661 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
662 log_file = xfopen (log_name.c_str (), "a");
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
663
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
664 if (! log_file)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
665 perror (log_name.c_str ());
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
666 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
667 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
668
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
669 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH) || log_file)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
670 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
671 /* FILENAMES should never be null, but safety doesn't hurt. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
672 for (int e = 0; e < filenames.length () && ! filenames[e].empty (); e++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
673 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
674 std::string filename = filenames[e];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
675
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
676 /* Only record absolute filenames, for privacy. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
677 if (log_file && kpse_absolute_p (filename.c_str (), false))
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
678 gnulib::fprintf (log_file, "%lu %s\n",
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
679 static_cast<unsigned long> (time (0)),
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
680 filename.c_str ());
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
681
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
682 /* And show them online, if debugging. We've already started
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
683 the debugging line in `search', where this is called, so
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
684 just print the filename here, don't use DEBUGF. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
685 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH))
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
686 gnulib::fputs (filename.c_str (), stderr);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
687 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
688 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
689 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
690
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
691 /* Concatenate each element in DIRS with NAME (assume each ends with a
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
692 /, to save time). If SEARCH_ALL is false, return the first readable
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
693 regular file. Else continue to search for more. In any case, if
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
694 none, return a list containing just NULL.
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
695
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
696 We keep a single buffer for the potential filenames and reallocate
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
697 only when necessary. I'm not sure it's noticeably faster, but it
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
698 does seem cleaner. (We do waste a bit of space in the return
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
699 value, though, since we don't shrink it to the final size returned.) */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
700
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
701 static string_vector
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
702 dir_list_search (str_llist_type *dirs, const std::string& name,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
703 bool search_all)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
704 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
705 str_llist_elt_type *elt;
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
706 string_vector ret;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
707
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
708 for (elt = *dirs; elt; elt = STR_LLIST_NEXT (*elt))
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
709 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
710 const std::string dir = STR_LLIST (*elt);
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
711
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
712 std::string potential = dir + name;
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
713
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
714 std::string tmp = kpse_readable_file (potential);
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
715
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
716 if (! tmp.empty ())
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
717 {
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
718 ret.append (potential);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
719
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
720 /* Move this element towards the top of the list. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
721 str_llist_float (dirs, elt);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
722
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
723 if (! search_all)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
724 return ret;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
725 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
726 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
727
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
728 return ret;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
729 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
730
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
731 /* This is called when NAME is absolute or explicitly relative; if it's
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
732 readable, return (a list containing) it; otherwise, return NULL. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
733
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
734 static string_vector
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
735 absolute_search (const std::string& name)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
736 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
737 string_vector ret_list;
4393
f92093e0ff78 [project @ 2003-04-26 20:58:45 by jwe]
jwe
parents: 4392
diff changeset
738 std::string found = kpse_readable_file (name);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
739
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
740 /* Add `found' to the return list even if it's null; that tells
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
741 the caller we didn't find anything. */
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
742 ret_list.append (found);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
743
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
744 return ret_list;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
745 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
746
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
747 /* This is the hard case -- look for NAME in PATH. If ALL is false,
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
748 return the first file found. Otherwise, search all elements of PATH. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
749
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
750 static string_vector
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
751 path_search (const std::string& path, const std::string& name,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
752 bool /* must_exist */, bool all)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
753 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
754 string_vector ret_list;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
755 bool done = false;
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
756
8021
85184151822e fix typo in NPOS change
John W. Eaton <jwe@octave.org>
parents: 8019
diff changeset
757 for (kpse_path_iterator pi (path); ! done && pi != std::string::npos; pi++)
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
758 {
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
759 std::string elt = *pi;
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
760
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
761 string_vector found;
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
762 bool allow_disk_search = true;
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
763
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
764 if (elt.length () > 1 && elt[0] == '!' && elt[1] == '!')
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
765 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
766 /* Those magic leading chars in a path element means don't
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
767 search the disk for this elt. And move past the magic to
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
768 get to the name. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
769 allow_disk_search = false;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
770 elt = elt.substr (2);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
771 }
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
772
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
773 /* Do not touch the device if present */
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
774 if (NAME_BEGINS_WITH_DEVICE (elt))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
775 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
776 while (elt.length () > 3
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
777 && IS_DIR_SEP (elt[2]) && IS_DIR_SEP (elt[3]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
778 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
779 elt[2] = elt[1];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
780 elt[1] = elt[0];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
781 elt = elt.substr (1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
782 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
783 }
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
784 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
785 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
786 /* We never want to search the whole disk. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
787 while (elt.length () > 1
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
788 && IS_DIR_SEP (elt[0]) && IS_DIR_SEP (elt[1]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
789 elt = elt.substr (1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
790 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
791
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
792 /* Try ls-R, unless we're searching for texmf.cnf. Our caller
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
793 (search), also tests first_search, and does the resetting. */
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
794 found = first_search
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
795 ? string_vector () : kpse_db_search (name, elt, all);
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
796
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
797 /* Search the filesystem if (1) the path spec allows it, and either
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
798 (2a) we are searching for texmf.cnf ; or
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
799 (2b) no db exists; or
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
800 (2c) no db's are relevant to this elt; or
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
801 (3) MUST_EXIST && NAME was not in the db.
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
802 In (2*), `found' will be NULL.
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
803 In (3), `found' will be an empty list. */
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
804
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
805 if (allow_disk_search && found.empty ())
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
806 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
807 str_llist_type *dirs = kpse_element_dirs (elt);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
808
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
809 if (dirs && *dirs)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
810 found = dir_list_search (dirs, name, all);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
811 }
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
812
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
813 /* Did we find anything anywhere? */
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
814 if (! found.empty ())
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
815 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
816 if (all)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
817 ret_list.append (found);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
818 else
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
819 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
820 ret_list.append (found[0]);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
821 done = true;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
822 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
823 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
824 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
825
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
826 return ret_list;
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
827 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
828
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
829 /* Search PATH for ORIGINAL_NAME. If ALL is false, or ORIGINAL_NAME is
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
830 absolute_p, check ORIGINAL_NAME itself. Otherwise, look at each
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
831 element of PATH for the first readable ORIGINAL_NAME.
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
832
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
833 Always return a list; if no files are found, the list will
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
834 contain just NULL. If ALL is true, the list will be
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
835 terminated with NULL. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
836
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
837 static string_vector
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
838 search (const std::string& path, const std::string& original_name,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
839 bool must_exist, bool all)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
840 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
841 string_vector ret_list;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
842 bool absolute_p;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
843
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
844 /* Make a leading ~ count as an absolute filename, and expand $FOO's. */
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
845 std::string name = kpse_expand (original_name);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
846
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
847 /* If the first name is absolute or explicitly relative, no need to
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
848 consider PATH at all. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
849 absolute_p = kpse_absolute_p (name, true);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
850
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
851 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH))
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
852 DEBUGF4 ("start search (file=%s, must_exist=%d, find_all=%d, path=%s).\n",
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
853 name.c_str (), must_exist, all, path.c_str ());
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
854
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
855 /* Find the file(s). */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
856 ret_list = absolute_p ? absolute_search (name)
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
857 : path_search (path, name, must_exist, all);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
858
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
859 /* The very first search is for texmf.cnf. We can't log that, since
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
860 we want to allow setting TEXMFLOG in texmf.cnf. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
861 if (first_search)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
862 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
863 first_search = false;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
864 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
865 else
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
866 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
867 /* Record the filenames we found, if desired. And wrap them in a
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
868 debugging line if we're doing that. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
869
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
870 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
871 DEBUGF1 ("search (%s) =>", original_name.c_str ());
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
872
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
873 log_search (ret_list);
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
874
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
875 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH))
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
876 gnulib::putc ('\n', stderr);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
877 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
878
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
879 return ret_list;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
880 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
881
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
882 /* Search PATH for the first NAME. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
883
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
884 /* Call `kpse_expand' on NAME. If the result is an absolute or
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
885 explicitly relative filename, check whether it is a readable
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
886 (regular) file.
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
887
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
888 Otherwise, look in each of the directories specified in PATH (also do
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
889 tilde and variable expansion on elements in PATH), using a prebuilt
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
890 db (see db.h) if it's relevant for a given path element.
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
891
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
892 If the prebuilt db doesn't exist, or if MUST_EXIST is true and NAME
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
893 isn't found in the prebuilt db, look on the filesystem. (I.e., if
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
894 MUST_EXIST is false, and NAME isn't found in the db, do *not* look on
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
895 the filesystem.)
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
896
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
897 The caller must expand PATH. This is because it makes more sense to
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
898 do this once, in advance, instead of for every search using it.
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
899
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
900 In any case, return the complete filename if found, otherwise NULL. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
901
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
902 static std::string
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
903 kpse_path_search (const std::string& path, const std::string& name,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
904 bool must_exist)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
905 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
906 string_vector ret_list = search (path, name, must_exist, false);
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
907
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
908 return ret_list.empty () ? std::string () : ret_list[0];
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
909 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
910
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
911 /* Search all elements of PATH for files named NAME. Not sure if it's
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
912 right to assert `must_exist' here, but it suffices now. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
913
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
914 /* Like `kpse_path_search' with MUST_EXIST true, but return a list of
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
915 all the filenames (or NULL if none), instead of taking the first. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
916
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
917 static string_vector
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
918 kpse_all_path_search (const std::string& path, const std::string& name)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
919 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
920 return search (path, name, true, true);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
921 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
922
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
923 /* This is the hard case -- look in each element of PATH for each
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
924 element of NAMES. If ALL is false, return the first file found.
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
925 Otherwise, search all elements of PATH. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
926
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
927 static string_vector
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
928 path_find_first_of (const std::string& path, const string_vector& names,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
929 bool /* must_exist */, bool all)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
930 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
931 string_vector ret_list;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
932 bool done = false;
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
933
8021
85184151822e fix typo in NPOS change
John W. Eaton <jwe@octave.org>
parents: 8019
diff changeset
934 for (kpse_path_iterator pi (path); ! done && pi != std::string::npos; pi++)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
935 {
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
936 std::string elt = *pi;
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
937
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
938 str_llist_type *dirs;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
939 str_llist_elt_type *dirs_elt;
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
940 string_vector found;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
941 bool allow_disk_search = true;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
942
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
943 if (elt.length () > 1 && elt[0] == '!' && elt[1] == '!')
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
944 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
945 /* Those magic leading chars in a path element means don't
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
946 search the disk for this elt. And move past the magic to
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
947 get to the name. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
948
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
949 allow_disk_search = false;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
950 elt = elt.substr (2);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
951 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
952
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
953 /* Do not touch the device if present */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
954
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
955 if (NAME_BEGINS_WITH_DEVICE (elt))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
956 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
957 while (elt.length () > 3
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
958 && IS_DIR_SEP (elt[2]) && IS_DIR_SEP (elt[3]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
959 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
960 elt[2] = elt[1];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
961 elt[1] = elt[0];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
962 elt = elt.substr (1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
963 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
964 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
965 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
966 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
967 /* We never want to search the whole disk. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
968 while (elt.length () > 1
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
969 && IS_DIR_SEP (elt[0]) && IS_DIR_SEP (elt[1]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
970 elt = elt.substr (1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
971 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
972
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
973 /* We have to search one directory at a time. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
974 dirs = kpse_element_dirs (elt);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
975 for (dirs_elt = *dirs; dirs_elt; dirs_elt = STR_LLIST_NEXT (*dirs_elt))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
976 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
977 const std::string dir = STR_LLIST (*dirs_elt);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
978
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
979 int len = names.length ();
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
980 for (int i = 0; i < len && !done; i++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
981 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
982 std::string name = names[i];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
983
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
984 /* Try ls-R, unless we're searching for texmf.cnf. Our caller
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
985 (find_first_of), also tests first_search, and does the
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
986 resetting. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
987 found = first_search
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
988 ? string_vector () : kpse_db_search (name, dir.c_str (), all);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
989
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
990 /* Search the filesystem if (1) the path spec allows it,
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
991 and either
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
992
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
993 (2a) we are searching for texmf.cnf ; or
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
994 (2b) no db exists; or
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
995 (2c) no db's are relevant to this elt; or
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
996 (3) MUST_EXIST && NAME was not in the db.
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
997
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
998 In (2*), `found' will be NULL.
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
999 In (3), `found' will be an empty list. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1000
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1001 if (allow_disk_search && found.empty ())
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1002 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1003 static str_llist_type *tmp = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1004
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1005 if (! tmp)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1006 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1007 tmp = new str_llist_type;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1008 *tmp = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1009 str_llist_add (tmp, "");
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1010 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1011
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1012 STR_LLIST (*(*tmp)) = dir;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1013
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1014 found = dir_list_search (tmp, name, all);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1015 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1016
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1017 /* Did we find anything anywhere? */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1018 if (! found.empty ())
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1019 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1020 if (all)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1021 ret_list.append (found);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1022 else
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1023 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1024 ret_list.append (found[0]);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1025 done = true;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1026 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1027 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1028 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1029 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1030 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1031
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1032 return ret_list;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1033 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1034
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1035 static string_vector
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1036 find_first_of (const std::string& path, const string_vector& names,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1037 bool must_exist, bool all)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1038 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1039 string_vector ret_list;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1040
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1041 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH))
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1042 {
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
1043 gnulib::fputs ("start find_first_of ((", stderr);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1044
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1045 int len = names.length ();
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1046
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1047 for (int i = 0; i < len; i++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1048 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1049 if (i == 0)
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
1050 gnulib::fputs (names[i].c_str (), stderr);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1051 else
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
1052 gnulib::fprintf (stderr, ", %s", names[i].c_str ());
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1053 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1054
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
1055 gnulib::fprintf (stderr, "), path=%s, must_exist=%d).\n",
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
1056 path.c_str (), must_exist);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1057 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1058
4409
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
1059 for (int i = 0; i < names.length (); i++)
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
1060 {
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
1061 std::string name = names[i];
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
1062
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
1063 if (kpse_absolute_p (name, true))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1064 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1065 /* If the name is absolute or explicitly relative, no need
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1066 to consider PATH at all. If we find something, then we
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1067 are done. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1068
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1069 ret_list = absolute_search (name);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1070
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1071 if (! ret_list.empty ())
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1072 return ret_list;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1073 }
4409
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
1074 }
6b191c6e6875 [project @ 2003-05-11 16:41:10 by jwe]
jwe
parents: 4407
diff changeset
1075
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1076 /* Find the file. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1077 ret_list = path_find_first_of (path, names, must_exist, all);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1078
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1079 /* The very first search is for texmf.cnf. We can't log that, since
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1080 we want to allow setting TEXMFLOG in texmf.cnf. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1081 if (first_search)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1082 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1083 first_search = false;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1084 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1085 else
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1086 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1087 /* Record the filenames we found, if desired. And wrap them in a
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1088 debugging line if we're doing that. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1089
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1090 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1091 {
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
1092 gnulib::fputs ("find_first_of (", stderr);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1093
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1094 int len = names.length ();
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1095
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1096 for (int i = 0; i < len; i++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1097 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1098 if (i == 0)
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
1099 gnulib::fputs (names[i].c_str (), stderr);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1100 else
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
1101 gnulib::fprintf (stderr, ", %s", names[i].c_str ());
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1102 }
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
1103
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
1104 gnulib::fputs (") =>", stderr);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1105 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1106
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1107 log_search (ret_list);
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1108
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1109 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH))
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
1110 gnulib::putc ('\n', stderr);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1111 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1112
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1113 return ret_list;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1114 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1115
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1116 /* Search each element of PATH for each element of NAMES. Return the
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1117 first one found. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1118
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1119 /* Search each element of PATH for each element in the list of NAMES.
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1120 Return the first one found. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1121
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1122 static std::string
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1123 kpse_path_find_first_of (const std::string& path, const string_vector& names,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1124 bool must_exist)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1125 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1126 string_vector ret_list = find_first_of (path, names, must_exist, false);
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1127
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1128 return ret_list.empty () ? std::string () : ret_list[0];
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1129 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1130
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1131 /* Search each element of PATH for each element of NAMES and return a
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1132 list containing everything found, in the order found. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1133
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1134 /* Like `kpse_path_find_first_of' with MUST_EXIST true, but return a
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1135 list of all the filenames (or NULL if none), instead of taking the
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1136 first. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1137
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1138 static string_vector
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1139 kpse_all_path_find_first_of (const std::string& path,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1140 const string_vector& names)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1141 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1142 return find_first_of (path, names, true, true);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1143 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1144
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1145 /* General expansion. Some of this file (the brace-expansion
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1146 code from bash) is covered by the GPL; this is the only GPL-covered
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1147 code in kpathsea. The part of the file that I wrote (the first
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1148 couple of functions) is covered by the LGPL. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1149
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1150 /* If NAME has a leading ~ or ~user, Unix-style, expand it to the user's
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1151 home directory, and return a new malloced string. If no ~, or no
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1152 <pwd.h>, just return NAME. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1153
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1154 static std::string
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1155 kpse_tilde_expand (const std::string& name)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1156 {
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1157 std::string expansion;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1158
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1159 /* If no leading tilde, do nothing. */
5137
5ee7da8b8a4b [project @ 2005-02-09 17:59:19 by jwe]
jwe
parents: 5085
diff changeset
1160 if (name.empty () || name[0] != '~')
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1161 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1162 expansion = name;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1163
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1164 /* If a bare tilde, return the home directory or `.'. (Very
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1165 unlikely that the directory name will do anyone any good, but
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1166 ... */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1167 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1168 else if (name.length () == 1)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1169 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1170 expansion = octave_env::getenv ("HOME");
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1171
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1172 if (expansion.empty ())
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1173 expansion = ".";
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1174
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1175 /* If `~/', remove any trailing / or replace leading // in $HOME.
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1176 Should really check for doubled intermediate slashes, too. */
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1177 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1178 else if (IS_DIR_SEP (name[1]))
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1179 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1180 unsigned c = 1;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1181 std::string home = octave_env::getenv ("HOME");
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1182
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1183 if (home.empty ())
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1184 home = ".";
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1185
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1186 size_t home_len = home.length ();
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1187
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1188 /* handle leading // */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1189 if (home_len > 1 && IS_DIR_SEP (home[0]) && IS_DIR_SEP (home[1]))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1190 home = home.substr (1);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1191
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1192 /* omit / after ~ */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1193 if (IS_DIR_SEP (home[home_len - 1]))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1194 c++;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1195
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1196 expansion = home + name.substr (c);
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1197
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1198 /* If `~user' or `~user/', look up user in the passwd database (but
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1199 OS/2 doesn't have this concept. */
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1200 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1201 else
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1202 #ifdef HAVE_PWD_H
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1203 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1204 unsigned c = 2;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1205
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1206 /* find user name */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1207 while (name.length () > c && ! IS_DIR_SEP (name[c]))
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1208 c++;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1209
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1210 std::string user = name.substr (1, c-1);
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1211
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1212 /* We only need the cast here for (deficient) systems
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1213 which do not declare `getpwnam' in <pwd.h>. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1214 octave_passwd p = octave_passwd::getpwnam (user);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1215
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1216 /* If no such user, just use `.'. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1217 std::string home = p ? p.dir () : std::string (".");
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1218
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1219 if (home.empty ())
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1220 home = ".";
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1221
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1222 /* handle leading // */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1223 if (home.length () > 1 && IS_DIR_SEP (home[0]) && IS_DIR_SEP (home[1]))
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1224 home = home.substr (1);
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1225
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1226 /* If HOME ends in /, omit the / after ~user. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1227 if (name.length () > c && IS_DIR_SEP (home[home.length () - 1]))
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1228 c++;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1229
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1230 expansion = name.length () > c ? home : home + name.substr (c);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1231 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1232 #else /* not HAVE_PWD_H */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1233 expansion = name;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1234 #endif /* not HAVE_PWD_H */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1235
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1236 return expansion;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1237 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1238
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1239 /* Do variable expansion first so ~${USER} works. (Besides, it's what the
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1240 shells do.) */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1241
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1242 /* Call kpse_var_expand and kpse_tilde_expand (in that order). Result
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1243 is always in fresh memory, even if no expansions were done. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1244
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1245 static std::string
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1246 kpse_expand (const std::string& s)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1247 {
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1248 std::string var_expansion = kpse_var_expand (s);
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1249 return kpse_tilde_expand (var_expansion);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1250 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1251
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1252 /* Forward declarations of functions from the original expand.c */
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1253 static string_vector brace_expand (const std::string&);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1254
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1255 /* If $KPSE_DOT is defined in the environment, prepend it to any relative
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1256 path components. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1257
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1258 static std::string
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1259 kpse_expand_kpse_dot (const std::string& path)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1260 {
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1261 std::string ret;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1262 std::string kpse_dot = octave_env::getenv ("KPSE_DOT");
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1263
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1264 if (kpse_dot.empty ())
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1265 return path;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1266
8021
85184151822e fix typo in NPOS change
John W. Eaton <jwe@octave.org>
parents: 8019
diff changeset
1267 for (kpse_path_iterator pi (path); pi != std::string::npos; pi++)
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1268 {
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1269 std::string elt = *pi;
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1270
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1271 /* We assume that the !! magic is only used on absolute components.
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1272 Single "." get special treatment, as does "./" or its equivalent. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1273
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1274 size_t elt_len = elt.length ();
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1275
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1276 if (kpse_absolute_p (elt, false)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1277 || (elt_len > 1 && elt[0] == '!' && elt[1] == '!'))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1278 ret += elt + ENV_SEP_STRING;
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1279 else if (elt_len == 1 && elt[0] == '.')
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1280 ret += kpse_dot + ENV_SEP_STRING;
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1281 else if (elt_len > 1 && elt[0] == '.' && IS_DIR_SEP (elt[1]))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1282 ret += kpse_dot + elt.substr (1) + ENV_SEP_STRING;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1283 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1284 ret += kpse_dot + DIR_SEP_STRING + elt + ENV_SEP_STRING;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1285 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1286
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1287 int len = ret.length ();
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1288 if (len > 0)
4395
cc6d369355ec [project @ 2003-04-28 18:14:56 by jwe]
jwe
parents: 4394
diff changeset
1289 ret.resize (len-1);
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1290
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1291 return ret;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1292 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1293
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1294 /* Do brace expansion on ELT; then do variable and ~ expansion on each
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1295 element of the result; then do brace expansion again, in case a
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1296 variable definition contained braces (e.g., $TEXMF). Return a
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1297 string comprising all of the results separated by ENV_SEP_STRING. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1298
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1299 static std::string
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1300 kpse_brace_expand_element (const std::string& elt)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1301 {
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1302 std::string ret;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1303
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1304 string_vector expansions = brace_expand (elt);
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1305
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1306 for (int i = 0; i < expansions.length (); i++)
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1307 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1308 /* Do $ and ~ expansion on each element. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1309 std::string x = kpse_expand (expansions[i]);
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1310
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1311 if (x != expansions[i])
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1312 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1313 /* If we did any expansions, do brace expansion again. Since
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1314 recursive variable definitions are not allowed, this recursion
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1315 must terminate. (In practice, it's unlikely there will ever be
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1316 more than one level of recursion.) */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1317 x = kpse_brace_expand_element (x);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1318 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1319
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1320 ret += x + ENV_SEP_STRING;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1321 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1322
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1323 ret.resize (ret.length () - 1);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1324
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1325 return ret;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1326 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1327
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1328 /* Do brace expansion and call `kpse_expand' on each element of the
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1329 result; return the final expansion (always in fresh memory, even if
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1330 no expansions were done). We don't call `kpse_expand_default'
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1331 because there is a whole sequence of defaults to run through; see
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1332 `kpse_init_format'. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1333
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1334 static std::string
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1335 kpse_brace_expand (const std::string& path)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1336 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1337 /* Must do variable expansion first because if we have
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1338 foo = .:~
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1339 TEXINPUTS = $foo
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1340 we want to end up with TEXINPUTS = .:/home/karl.
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1341 Since kpse_path_element is not reentrant, we must get all
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1342 the path elements before we start the loop. */
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1343 std::string tmp = kpse_var_expand (path);
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1344
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1345 std::string ret;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1346
8021
85184151822e fix typo in NPOS change
John W. Eaton <jwe@octave.org>
parents: 8019
diff changeset
1347 for (kpse_path_iterator pi (tmp); pi != std::string::npos; pi++)
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1348 {
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1349 std::string elt = *pi;
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1350
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1351 /* Do brace expansion first, so tilde expansion happens in {~ka,~kb}. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1352 std::string expansion = kpse_brace_expand_element (elt);
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1353 ret += expansion + ENV_SEP_STRING;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1354 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1355
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1356 size_t len = ret.length ();
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1357 if (len > 0)
4395
cc6d369355ec [project @ 2003-04-28 18:14:56 by jwe]
jwe
parents: 4394
diff changeset
1358 ret.resize (len-1);
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1359
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1360 return kpse_expand_kpse_dot (ret);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1361 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
1362
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1363 /* Expand all special constructs in a path, and include only the actually
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1364 existing directories in the result. */
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1365
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1366 /* Do brace expansion and call `kpse_expand' on each argument of the
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1367 result, then expand any `//' constructs. The final expansion (always
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1368 in fresh memory) is a path of all the existing directories that match
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1369 the pattern. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1370
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1371 static std::string
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1372 kpse_path_expand (const std::string& path)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1373 {
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
1374 std::string ret;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1375 unsigned len;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1376
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1377 len = 0;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1378
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1379 /* Expand variables and braces first. */
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
1380 std::string tmp = kpse_brace_expand (path);
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
1381
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1382 /* Now expand each of the path elements, printing the results */
8021
85184151822e fix typo in NPOS change
John W. Eaton <jwe@octave.org>
parents: 8019
diff changeset
1383 for (kpse_path_iterator pi (tmp); pi != std::string::npos; pi++)
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1384 {
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1385 std::string elt = *pi;
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1386
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1387 str_llist_type *dirs;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1388
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1389 /* Skip and ignore magic leading chars. */
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1390 if (elt.length () > 1 && elt[0] == '!' && elt[1] == '!')
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1391 elt = elt.substr (2);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1392
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1393 /* Do not touch the device if present */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1394 if (NAME_BEGINS_WITH_DEVICE (elt))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1395 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1396 while (elt.length () > 3
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1397 && IS_DIR_SEP (elt[2]) && IS_DIR_SEP (elt[3]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1398 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1399 elt[2] = elt[1];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1400 elt[1] = elt[0];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1401 elt = elt.substr (1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1402 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1403 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1404 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1405 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1406 /* We never want to search the whole disk. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1407 while (elt.length () > 1
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1408 && IS_DIR_SEP (elt[0]) && IS_DIR_SEP (elt[1]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1409 elt = elt.substr (1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1410 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1411
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1412 /* Search the disk for all dirs in the component specified.
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1413 Be faster to check the database, but this is more reliable. */
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1414 dirs = kpse_element_dirs (elt);
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1415
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1416 if (dirs && *dirs)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1417 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1418 str_llist_elt_type *dir;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1419
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1420 for (dir = *dirs; dir; dir = STR_LLIST_NEXT (*dir))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1421 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1422 const std::string thedir = STR_LLIST (*dir);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1423 unsigned dirlen = thedir.length ();
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1424
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1425 ret += thedir;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1426 len += dirlen;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1427
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1428 /* Retain trailing slash if that's the root directory. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1429 if (dirlen == 1
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1430 || (dirlen == 3 && NAME_BEGINS_WITH_DEVICE (thedir)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1431 && IS_DIR_SEP (thedir[2])))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1432 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1433 ret += ENV_SEP_STRING;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1434 len++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1435 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1436
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1437 ret[len-1] = ENV_SEP;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1438 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1439 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1440 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1441
4395
cc6d369355ec [project @ 2003-04-28 18:14:56 by jwe]
jwe
parents: 4394
diff changeset
1442 if (len > 0)
cc6d369355ec [project @ 2003-04-28 18:14:56 by jwe]
jwe
parents: 4394
diff changeset
1443 ret.resize (len-1);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1444
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1445 return ret;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1446 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
1447
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1448 /* braces.c -- code for doing word expansion in curly braces. Taken from
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1449 bash 1.14.5. [Ans subsequently modified for kpatshea.]
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1450
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1451 Copyright (C) 1987,1991 Free Software Foundation, Inc.
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1452
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1453 This program is free software; you can redistribute it and/or modify it
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1454 under the terms of the GNU General Public License as published by
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1455 the Free Software Foundation; either version 1, or (at your option)
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1456 any later version.
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1457
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1458 This program is distributed in the hope that it will be useful, but
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1459 WITHOUT ANY WARRANTY; without even the implied warranty of
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1460 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1461 General Public License for more details.
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1462
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1463 You should have received a copy of the GNU General Public License
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1464 along with this program; see the file COPYING. If not, write to the
5307
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5137
diff changeset
1465 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5137
diff changeset
1466 MA 02110-1301, USA. */
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1467
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1468 #define brace_whitespace(c) (! (c) || (c) == ' ' || (c) == '\t' || (c) == '\n')
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1469
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1470 /* Basic idea:
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1471
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1472 Segregate the text into 3 sections: preamble (stuff before an open brace),
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1473 postamble (stuff after the matching close brace) and amble (stuff after
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1474 preamble, and before postamble). Expand amble, and then tack on the
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1475 expansions to preamble. Expand postamble, and tack on the expansions to
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1476 the result so far. */
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1477
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1478 /* Return a new array of strings which is the result of appending each
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1479 string in ARR2 to each string in ARR1. The resultant array is
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1480 len (arr1) * len (arr2) long. For convenience, ARR1 (and its contents)
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1481 are free ()'ed. ARR1 can be NULL, in that case, a new version of ARR2
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1482 is returned. */
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1483
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1484 static string_vector
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1485 array_concat (const string_vector& arr1, const string_vector& arr2)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1486 {
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1487 string_vector result;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1488
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1489 if (arr1.empty ())
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1490 result = arr2;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1491 else if (arr2.empty ())
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1492 result = arr1;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1493 else
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1494 {
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1495 int len1 = arr1.length ();
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1496 int len2 = arr2.length ();
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1497
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1498 result = string_vector (len1 * len2);
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1499
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1500 int k = 0;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1501 for (int i = 0; i < len2; i++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1502 for (int j = 0; j < len1; j++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1503 result[k++] = arr1[j] + arr2[i];
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1504 }
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1505
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1506 return result;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1507 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1508
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1509 static int brace_gobbler (const std::string&, int&, int);
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1510 static string_vector expand_amble (const std::string&);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1511
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1512 /* Return an array of strings; the brace expansion of TEXT. */
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1513 static string_vector
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1514 brace_expand (const std::string& text)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1515 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1516 /* Find the text of the preamble. */
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1517 int i = 0;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1518 int c = brace_gobbler (text, i, '{');
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1519
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1520 std::string preamble = text.substr (0, i);
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1521
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1522 string_vector result = string_vector (preamble);
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1523
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1524 if (c == '{')
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1525 {
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1526 /* Find the amble. This is the stuff inside this set of braces. */
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1527 int start = ++i;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1528 c = brace_gobbler (text, i, '}');
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1529
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1530 /* What if there isn't a matching close brace? */
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1531 if (! c)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1532 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1533 (*current_liboctave_warning_handler)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1534 ("%s: Unmatched {", text.c_str ());
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1535
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1536 result = string_vector (text);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1537 }
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1538 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1539 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1540 std::string amble = text.substr (start, i-start);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1541 result = array_concat (result, expand_amble (amble));
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1542
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1543 std::string postamble = text.substr (i+1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1544 result = array_concat (result, brace_expand (postamble));
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1545 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1546 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1547
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1548 return result;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1549 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1550
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1551 /* The character which is used to separate arguments. */
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1552 static int brace_arg_separator = ',';
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1553
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1554 /* Expand the text found inside of braces. We simply try to split the
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1555 text at BRACE_ARG_SEPARATORs into separate strings. We then brace
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1556 expand each slot which needs it, until there are no more slots which
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1557 need it. */
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1558 static string_vector
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1559 expand_amble (const std::string& text)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1560 {
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1561 string_vector result;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1562
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1563 size_t text_len = text.length ();
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1564 size_t start;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1565 int i, c;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1566
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1567 for (start = 0, i = 0, c = 1; c && start < text_len; start = ++i)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1568 {
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1569 int i0 = i;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1570 int c0 = brace_gobbler (text, i0, brace_arg_separator);
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1571 int i1 = i;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1572 int c1 = brace_gobbler (text, i1, ENV_SEP);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1573 c = c0 | c1;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1574 i = (i0 < i1 ? i0 : i1);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1575
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1576 std::string tem = text.substr (start, i-start);
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1577
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1578 string_vector partial = brace_expand (tem);
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1579
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1580 if (result.empty ())
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1581 result = partial;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1582 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1583 result.append (partial);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1584 }
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1585
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1586 return result;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1587 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1588
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1589 /* Start at INDEX, and skip characters in TEXT. Set INDEX to the
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1590 index of the character matching SATISFY. This understands about
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1591 quoting. Return the character that caused us to stop searching;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1592 this is either the same as SATISFY, or 0. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1593 static int
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1594 brace_gobbler (const std::string& text, int& indx, int satisfy)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1595 {
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1596 int c = 0, level = 0, quoted = 0, pass_next = 0;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1597
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1598 size_t text_len = text.length ();
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1599
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1600 size_t i = indx;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1601
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1602 for (; i < text_len; i++)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1603 {
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1604 c = text[i];
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1605
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1606 if (pass_next)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1607 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1608 pass_next = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1609 continue;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1610 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1611
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1612 /* A backslash escapes the next character. This allows backslash to
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1613 escape the quote character in a double-quoted string. */
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1614 if (c == '\\' && (quoted == 0 || quoted == '"' || quoted == '`'))
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1615 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1616 pass_next = 1;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1617 continue;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1618 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1619
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1620 if (quoted)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1621 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1622 if (c == quoted)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1623 quoted = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1624 continue;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1625 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1626
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1627 if (c == '"' || c == '\'' || c == '`')
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1628 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1629 quoted = c;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1630 continue;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1631 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1632
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1633 if (c == satisfy && !level && !quoted)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1634 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1635 /* We ignore an open brace surrounded by whitespace, and also
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1636 an open brace followed immediately by a close brace, that
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1637 was preceded with whitespace. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1638 if (c == '{' &&
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1639 ((i == 0 || brace_whitespace (text[i-1])) &&
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1640 (i+1 < text_len &&
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1641 (brace_whitespace (text[i+1]) || text[i+1] == '}'))))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1642 continue;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1643 /* If this is being compiled as part of bash, ignore the `{'
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1644 in a `${}' construct */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1645 if ((c != '{') || i == 0 || (text[i-1] != '$'))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1646 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1647 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1648
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1649 if (c == '{')
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1650 level++;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1651 else if (c == '}' && level)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1652 level--;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1653 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1654
4397
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1655 indx = i;
f682f9258271 [project @ 2003-04-29 03:06:08 by jwe]
jwe
parents: 4396
diff changeset
1656 return c;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1657 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1658
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1659 /* For each file format, we record the following information. The main
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1660 thing that is not part of this structure is the environment variable
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1661 lists. They are used directly in tex-file.c. We could incorporate
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1662 them here, but it would complicate the code a bit. We could also do
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1663 it via variable expansion, but not now, maybe not ever:
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1664 ${PKFONTS-${TEXFONTS-/usr/local/lib/texmf/fonts//}}. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1665
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1666 struct kpse_format_info_type
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1667 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1668 std::string type; /* Human-readable description. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1669 std::string path; /* The search path to use. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1670 std::string raw_path; /* Pre-$~ (but post-default) expansion. */
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1671 std::string path_source; /* Where the path started from. */
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1672 std::string override_path; /* From client environment variable. */
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1673 std::string client_path; /* E.g., from dvips's config.ps. */
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1674 std::string cnf_path; /* From texmf.cnf. */
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1675 std::string default_path; /* If all else fails. */
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1676 string_vector suffix; /* For kpse_find_file to check for/append. */
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1677 };
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1678
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1679 /* The sole variable of that type, indexed by `kpse_file_format_type'.
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1680 Initialized by calls to `kpse_find_file' for `kpse_init_format'. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1681 static kpse_format_info_type kpse_format_info;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1682
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1683 /* And EXPAND_DEFAULT calls kpse_expand_default on try_path and the
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1684 present info->path. */
4396
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
1685 #define EXPAND_DEFAULT(try_path, source_string) \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
1686 do \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
1687 { \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
1688 if (! try_path.empty ()) \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
1689 { \
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1690 info.raw_path = try_path; \
4396
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
1691 info.path = kpse_expand_default (try_path, info.path); \
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1692 info.path_source = source_string; \
4396
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
1693 } \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
1694 } \
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
1695 while (0)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1696
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1697 static hash_table_type db; /* The hash table for all the ls-R's. */
4928
1cf16fb3459a [project @ 2004-08-03 19:00:24 by jwe]
jwe
parents: 4730
diff changeset
1698
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1699 static hash_table_type alias_db;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1700
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1701 static string_vector db_dir_list;
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
1702
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1703 /* Return true if FILENAME could be in PATH_ELT, i.e., if the directory
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1704 part of FILENAME matches PATH_ELT. Have to consider // wildcards, but
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1705 $ and ~ expansion have already been done. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1706
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1707 static bool
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1708 match (const std::string& filename_arg, const std::string& path_elt_arg)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1709 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1710 const char *filename = filename_arg.c_str ();
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1711 const char *path_elt = path_elt_arg.c_str ();
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1712
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1713 const char *original_filename = filename;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1714 bool matched = false;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1715
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1716 for (; *filename && *path_elt; filename++, path_elt++)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1717 {
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1718 if (*filename == *path_elt) /* normal character match */
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1719 ;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1720
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1721 else if (IS_DIR_SEP (*path_elt) /* at // */
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1722 && original_filename < filename && IS_DIR_SEP (path_elt[-1]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1723 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1724 while (IS_DIR_SEP (*path_elt))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1725 path_elt++; /* get past second and any subsequent /'s */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1726
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1727 if (*path_elt == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1728 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1729 /* Trailing //, matches anything. We could make this
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1730 part of the other case, but it seems pointless to do
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1731 the extra work. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1732 matched = true;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1733 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1734 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1735 else
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1736 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1737 /* Intermediate //, have to match rest of PATH_ELT. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1738 for (; !matched && *filename; filename++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1739 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1740 /* Try matching at each possible character. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1741 if (IS_DIR_SEP (filename[-1]) && *filename == *path_elt)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1742 matched = match (filename, path_elt);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1743 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1744
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1745 /* Prevent filename++ when *filename='\0'. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1746 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1747 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1748 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1749 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1750 /* normal character nonmatch, quit */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1751 break;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1752 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1753
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1754 /* If we've reached the end of PATH_ELT, check that we're at the last
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1755 component of FILENAME, we've matched. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1756 if (! matched && *path_elt == 0)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1757 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1758 /* Probably PATH_ELT ended with `vf' or some such, and FILENAME
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1759 ends with `vf/ptmr.vf'. In that case, we'll be at a
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1760 directory separator. On the other hand, if PATH_ELT ended
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1761 with a / (as in `vf/'), FILENAME being the same `vf/ptmr.vf',
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1762 we'll be at the `p'. Upshot: if we're at a dir separator in
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1763 FILENAME, skip it. But if not, that's ok, as long as there
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1764 are no more dir separators. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1765
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1766 if (IS_DIR_SEP (*filename))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1767 filename++;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1768
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1769 while (*filename && !IS_DIR_SEP (*filename))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1770 filename++;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1771
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1772 matched = *filename == 0;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1773 }
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1774
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1775 return matched;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1776 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1777
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1778 /* If DB_DIR is a prefix of PATH_ELT, return true; otherwise false.
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1779 That is, the question is whether to try the db for a file looked up
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1780 in PATH_ELT. If PATH_ELT == ".", for example, the answer is no. If
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1781 PATH_ELT == "/usr/local/lib/texmf/fonts//tfm", the answer is yes.
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1782
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1783 In practice, ls-R is only needed for lengthy subdirectory
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1784 comparisons, but there's no gain to checking PATH_ELT to see if it is
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1785 a subdir match, since the only way to do that is to do a string
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1786 search in it, which is all we do anyway. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1787
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1788 static bool
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1789 elt_in_db (const std::string& db_dir, const std::string& path_elt)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1790 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1791 bool found = false;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1792
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1793 size_t db_dir_len = db_dir.length ();
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1794 size_t path_elt_len = path_elt.length ();
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1795
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1796 size_t i = 0;
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1797
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1798 while (! found && db_dir[i] == path_elt[i])
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1799 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1800 i++;
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1801 /* If we've matched the entire db directory, it's good. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1802 if (i == db_dir_len)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1803 found = true;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1804
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1805 /* If we've reached the end of PATH_ELT, but not the end of the db
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1806 directory, it's no good. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1807 else if (i == path_elt_len)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1808 break;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1809 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1810
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1811 return found;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1812 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
1813
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1814 /* Avoid doing anything if this PATH_ELT is irrelevant to the databases. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1815
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1816 /* Return list of matches for NAME in the ls-R file matching PATH_ELT. If
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1817 ALL is set, return (null-terminated list) of all matches, else just
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1818 the first. If no matches, return a pointer to an empty list. If no
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1819 databases can be read, or PATH_ELT is not in any of the databases,
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1820 return NULL. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1821
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1822 static string_vector
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1823 kpse_db_search (const std::string& name_arg,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1824 const std::string& orig_path_elt, bool all)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1825 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1826 bool done;
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1827 string_vector ret;
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1828 string_vector aliases;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1829 bool relevant = false;
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1830
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1831 std::string name = name_arg;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1832
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1833 /* If we failed to build the database (or if this is the recursive
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1834 call to build the db path), quit. */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
1835 if (! db.buckets)
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1836 return ret;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1837
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1838 /* When tex-glyph.c calls us looking for, e.g., dpi600/cmr10.pk, we
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1839 won't find it unless we change NAME to just `cmr10.pk' and append
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1840 `/dpi600' to PATH_ELT. We are justified in using a literal `/'
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1841 here, since that's what tex-glyph.c unconditionally uses in
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1842 DPI_BITMAP_SPEC. But don't do anything if the / begins NAME; that
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1843 should never happen. */
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1844 std::string path_elt;
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1845 size_t last_slash = name.rfind ('/');
8021
85184151822e fix typo in NPOS change
John W. Eaton <jwe@octave.org>
parents: 8019
diff changeset
1846 if (last_slash != std::string::npos && last_slash != 0)
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1847 {
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1848 std::string dir_part = name.substr (0, last_slash);
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1849 name = name.substr (last_slash + 1);
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1850 }
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1851 else
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1852 path_elt = orig_path_elt;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1853
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1854 /* Don't bother doing any lookups if this `path_elt' isn't covered by
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1855 any of database directories. We do this not so much because the
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1856 extra couple of hash lookups matter -- they don't -- but rather
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1857 because we want to return NULL in this case, so path_search can
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1858 know to do a disk search. */
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1859 for (int e = 0; ! relevant && e < db_dir_list.length (); e++)
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1860 relevant = elt_in_db (db_dir_list[e], path_elt);
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1861
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1862 if (! relevant)
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1863 return ret;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1864
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1865 /* If we have aliases for this name, use them. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1866 if (alias_db.buckets)
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1867 aliases = hash_lookup (alias_db, name);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1868
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1869 /* Push aliases up by one and insert the original name at the front. */
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1870 int len = aliases.length ();
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1871 aliases.resize (len+1);
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1872 for (int i = len; i > 0; i--)
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1873 aliases[i] = aliases[i - 1];
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1874 aliases[0] = name;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1875
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1876 done = false;
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1877 len = aliases.length ();
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1878 for (int i = 0; i < len && !done; i++)
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1879 {
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1880 std::string atry = aliases[i];
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1881
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1882 /* We have an ls-R db. Look up `atry'. */
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1883 string_vector db_dirs = hash_lookup (db, atry);
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1884
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1885 /* For each filename found, see if it matches the path element. For
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1886 example, if we have .../cx/cmr10.300pk and .../ricoh/cmr10.300pk,
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1887 and the path looks like .../cx, we don't want the ricoh file. */
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1888
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1889 int db_dirs_len = db_dirs.length ();
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
1890 for (int j = 0; j < db_dirs_len && !done; j++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1891 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1892 std::string db_file = db_dirs[j] + atry;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1893 bool matched = match (db_file, path_elt);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1894
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1895 #ifdef KPSE_DEBUG
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1896 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1897 DEBUGF3 ("db:match (%s,%s) = %d\n", db_file.c_str (), path_elt.c_str (), matched);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1898 #endif
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1899
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1900 /* We got a hit in the database. Now see if the file actually
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1901 exists, possibly under an alias. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1902 if (matched)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1903 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1904 std::string found;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1905 std::string tmp = kpse_readable_file (db_file);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1906 if (! tmp.empty ())
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1907 found = db_file;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1908 else
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1909 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1910 /* The hit in the DB doesn't exist in disk. Now try
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1911 all its aliases. For example, suppose we have a
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1912 hierarchy on CD, thus `mf.bas', but ls-R contains
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1913 `mf.base'. Find it anyway. Could probably work
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1914 around this with aliases, but this is pretty easy
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1915 and shouldn't hurt. The upshot is that if one of
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1916 the aliases actually exists, we use that. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1917
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1918 int aliases_len = aliases.length ();
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1919
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1920 for (int k = 1; k < aliases_len && found.empty (); k++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1921 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1922 std::string aatry = db_dirs[j] + aliases[k];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1923 tmp = kpse_readable_file (aatry);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1924 if (! tmp.empty ())
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1925 found = aatry;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1926 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1927 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1928
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1929 /* If we have a real file, add it to the list, maybe done. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1930 if (! found.empty ())
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1931 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1932 ret.append (found);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1933
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1934 if (! (all || found.empty ()))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1935 done = true;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1936 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1937 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1938 }
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1939 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1940
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1941 return ret;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1942 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1943
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1944 /* Expand extra colons. */
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1945
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1946 /* Check for leading colon first, then trailing, then doubled, since
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1947 that is fastest. Usually it will be leading or trailing. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1948
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1949 /* Replace a leading or trailing or doubled : in PATH with DFLT. If
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1950 no extra colons, return PATH. Only one extra colon is replaced.
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1951 DFLT may not be NULL. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1952
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
1953 static std::string
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1954 kpse_expand_default (const std::string& path, const std::string& fallback)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1955 {
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1956 std::string expansion;
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1957
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1958 size_t path_len = path.length ();
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1959
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1960 if (path_len == 0)
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1961 expansion = fallback;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1962
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1963 /* Solitary or leading :? */
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1964 else if (IS_ENV_SEP (path[0]))
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1965 {
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1966 expansion = path_len == 1 ? fallback : fallback + path;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1967 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1968
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1969 /* Sorry about the assignment in the middle of the expression, but
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1970 conventions were made to be flouted and all that. I don't see the
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1971 point of calling strlen twice or complicating the logic just to
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1972 avoid the assignment (especially now that I've pointed it out at
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1973 such great length). */
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1974 else if (IS_ENV_SEP (path[path_len-1]))
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1975 expansion = path + fallback;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1976
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1977 /* OK, not leading or trailing. Check for doubled. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1978 else
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1979 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1980 /* What we'll return if we find none. */
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1981 expansion = path;
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1982
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1983 for (size_t i = 0; i < path_len; i++)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1984 {
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1985 if (i + 1 < path_len
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1986 && IS_ENV_SEP (path[i]) && IS_ENV_SEP (path[i+1]))
4394
13619163e159 [project @ 2003-04-28 17:40:07 by jwe]
jwe
parents: 4393
diff changeset
1987 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1988 /* We have a doubled colon. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1989
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1990 /* Copy stuff up to and including the first colon. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1991 /* Copy in FALLBACK, and then the rest of PATH. */
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1992 expansion = path.substr (0, i+1) + fallback + path.substr (i+1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1993
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
1994 break;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1995 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1996 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1997 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
1998
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
1999 return expansion;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2000 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2001
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2002 /* Translate a path element to its corresponding director{y,ies}. */
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2003
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2004 /* To avoid giving prototypes for all the routines and then their real
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2005 definitions, we give all the subroutines first. The entry point is
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2006 the last routine in the file. */
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
2007
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2008 /* Make a copy of DIR (unless it's null) and save it in L. Ensure that
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2009 DIR ends with a DIR_SEP for the benefit of later searches. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2010
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2011 static void
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2012 dir_list_add (str_llist_type *l, const std::string& dir)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2013 {
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2014 char last_char = dir[dir.length () - 1];
4407
16e8acbd19d5 [project @ 2003-05-05 19:00:56 by jwe]
jwe
parents: 4399
diff changeset
2015
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2016 std::string saved_dir = dir;
4407
16e8acbd19d5 [project @ 2003-05-05 19:00:56 by jwe]
jwe
parents: 4399
diff changeset
2017
16e8acbd19d5 [project @ 2003-05-05 19:00:56 by jwe]
jwe
parents: 4399
diff changeset
2018 if (! (IS_DIR_SEP (last_char) || IS_DEVICE_SEP (last_char)))
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2019 saved_dir += DIR_SEP_STRING;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2020
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2021 str_llist_add (l, saved_dir);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2022 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2023
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2024 /* Return true if FN is a directory or a symlink to a directory,
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2025 false if not. */
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2026
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2027 static bool
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2028 dir_p (const std::string& fn)
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2029 {
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2030 #ifdef WIN32
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2031 unsigned int fa = GetFileAttributes (fn.c_str ());
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2032 return (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY));
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2033 #else
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2034 struct stat stats;
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2035 return stat (fn.c_str (), &stats) == 0 && S_ISDIR (stats.st_mode);
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2036 #endif
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2037 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2038
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2039 /* If DIR is a directory, add it to the list L. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2040
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2041 static void
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2042 checked_dir_list_add (str_llist_type *l, const std::string& dir)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2043 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2044 if (dir_p (dir))
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2045 dir_list_add (l, dir);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2046 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
2047
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2048 /* The cache. Typically, several paths have the same element; for
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2049 example, /usr/local/lib/texmf/fonts//. We don't want to compute the
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2050 expansion of such a thing more than once. Even though we also cache
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2051 the dir_links call, that's not enough -- without this path element
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2052 caching as well, the execution time doubles. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2053
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2054 struct cache_entry
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2055 {
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2056 std::string key;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2057 str_llist_type *value;
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2058 };
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2059
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2060 static cache_entry *the_cache = 0;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2061 static unsigned cache_length = 0;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2062
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2063 /* Associate KEY with VALUE. We implement the cache as a simple linear
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2064 list, since it's unlikely to ever be more than a dozen or so elements
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2065 long. We don't bother to check here if PATH has already been saved;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2066 we always add it to our list. We copy KEY but not VALUE; not sure
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2067 that's right, but it seems to be all that's needed. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2068
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2069 static void
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2070 cache (const std::string key, str_llist_type *value)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2071 {
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2072 cache_entry *new_cache = new cache_entry [cache_length+1];
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2073
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2074 for (unsigned i = 0; i < cache_length; i++)
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2075 {
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2076 new_cache[i].key = the_cache[i].key;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2077 new_cache[i].value = the_cache[i].value;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2078 }
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2079
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2080 delete [] the_cache;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2081
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2082 the_cache = new_cache;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2083
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2084 the_cache[cache_length].key = key;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2085 the_cache[cache_length].value = value;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2086
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2087 cache_length++;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2088 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2089
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2090 /* To retrieve, just check the list in order. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2091
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2092 static str_llist_type *
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2093 cached (const std::string& key)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2094 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2095 unsigned p;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2096
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2097 for (p = 0; p < cache_length; p++)
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2098 {
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2099 if (key == the_cache[p].key)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2100 return the_cache[p].value;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2101 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2102
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2103 return 0;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2104 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
2105
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2106 /* Handle the magic path constructs. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2107
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2108 /* Declare recursively called routine. */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2109 static void expand_elt (str_llist_type *, const std::string&, unsigned);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2110
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2111 /* POST is a pointer into the original element (which may no longer be
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2112 ELT) to just after the doubled DIR_SEP, perhaps to the null. Append
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2113 subdirectories of ELT (up to ELT_LENGTH, which must be a /) to
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2114 STR_LIST_PTR. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2115
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2116 #ifdef WIN32
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2117
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2118 /* Shared across recursive calls, it acts like a stack. */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2119 static std::string dirname;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2120
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2121 #else /* WIN32 */
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2122
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2123 /* Return -1 if FN isn't a directory, else its number of links.
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2124 Duplicate the call to stat; no need to incur overhead of a function
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2125 call for that little bit of cleanliness. */
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2126
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2127 static int
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2128 dir_links (const std::string& fn)
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2129 {
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2130 std::map<std::string, long> link_table;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2131
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2132 long ret;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2133
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2134 if (link_table.find (fn) != link_table.end ())
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2135 ret = link_table[fn];
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2136 else
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2137 {
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2138 struct stat stats;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2139
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2140 ret = stat (fn.c_str (), &stats) == 0 && S_ISDIR (stats.st_mode)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2141 ? stats.st_nlink : static_cast<unsigned> (-1);
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2142
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2143 link_table[fn] = ret;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2144
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2145 #ifdef KPSE_DEBUG
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2146 if (KPSE_DEBUG_P (KPSE_DEBUG_STAT))
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2147 DEBUGF2 ("dir_links (%s) => %ld\n", fn.c_str (), ret);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2148 #endif
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2149 }
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2150
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2151 return ret;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2152 }
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2153
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2154 #endif /* WIN32 */
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2155
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2156 static void
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2157 do_subdir (str_llist_type *str_list_ptr, const std::string& elt,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2158 unsigned elt_length, const std::string& post)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2159 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2160 #ifdef WIN32
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2161 WIN32_FIND_DATA find_file_data;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2162 HANDLE hnd;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2163 int proceed;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2164 #else
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2165 DIR *dir;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2166 struct dirent *e;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2167 #endif /* not WIN32 */
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2168
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2169 std::string name = elt.substr (0, elt_length);
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2170
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2171 assert (IS_DIR_SEP (elt[elt_length - 1])
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2172 || IS_DEVICE_SEP (elt[elt_length - 1]));
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2173
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2174 #if defined (WIN32)
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2175
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2176 dirname = name + "/*.*"; /* "*.*" or "*" -- seems equivalent. */
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2177
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2178 hnd = FindFirstFile (dirname.c_str (), &find_file_data);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2179
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2180 if (hnd == INVALID_HANDLE_VALUE)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2181 return;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2182
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2183 /* Include top level before subdirectories, if nothing to match. */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2184 if (post.empty ())
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2185 dir_list_add (str_list_ptr, name);
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2186 else
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2187 {
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2188 /* If we do have something to match, see if it exists. For
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2189 example, POST might be `pk/ljfour', and they might have a
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2190 directory `$TEXMF/fonts/pk/ljfour' that we should find. */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2191 name += post;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2192 expand_elt (str_list_ptr, name, elt_length);
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2193 name.resize (elt_length);
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2194 }
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2195
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2196 proceed = 1;
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2197
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2198 while (proceed)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2199 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2200 if (find_file_data.cFileName[0] != '.')
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2201 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2202 /* Construct the potential subdirectory name. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2203 name += find_file_data.cFileName;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2204
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2205 if (find_file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2206 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2207 /* It's a directory, so append the separator. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2208 name += DIR_SEP_STRING;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2209 unsigned potential_len = name.length ();
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2210
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2211 do_subdir (str_list_ptr, name, potential_len, post);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2212 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2213 name.resize (elt_length);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2214 }
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2215
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2216 proceed = FindNextFile (hnd, &find_file_data);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2217 }
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2218
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2219 FindClose (hnd);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2220
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2221 #else /* not WIN32 */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2222
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2223 /* If we can't open it, quit. */
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
2224 dir = gnulib::opendir (name.c_str ());
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2225
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2226 if (! dir)
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2227 return;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2228
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2229 /* Include top level before subdirectories, if nothing to match. */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2230 if (post.empty ())
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2231 dir_list_add (str_list_ptr, name);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2232 else
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2233 {
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2234 /* If we do have something to match, see if it exists. For
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2235 example, POST might be `pk/ljfour', and they might have a
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2236 directory `$TEXMF/fonts/pk/ljfour' that we should find. */
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2237 name += post;
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2238 expand_elt (str_list_ptr, name, elt_length);
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2239 name.resize (elt_length);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2240 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2241
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2242 while ((e = readdir (dir)))
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2243 {
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2244 /* If it begins with a `.', never mind. (This allows ``hidden''
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2245 directories that the algorithm won't find.) */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2246
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2247 if (e->d_name[0] != '.')
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2248 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2249 int links;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2250
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2251 /* Construct the potential subdirectory name. */
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2252 name += e->d_name;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2253
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2254 /* If we can't stat it, or if it isn't a directory, continue. */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2255 links = dir_links (name);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2256
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2257 if (links >= 0)
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2258 {
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2259 /* It's a directory, so append the separator. */
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2260 name += DIR_SEP_STRING;
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2261 unsigned potential_len = name.length ();
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2262
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2263 /* Should we recurse? To see if the subdirectory is a
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2264 leaf, check if it has two links (one for . and one for
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2265 ..). This means that symbolic links to directories do
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2266 not affect the leaf-ness. This is arguably wrong, but
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2267 the only alternative I know of is to stat every entry
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2268 in the directory, and that is unacceptably slow.
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2269
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2270 The #ifdef here makes all this configurable at
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2271 compile-time, so that if we're using VMS directories or
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2272 some such, we can still find subdirectories, even if it
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2273 is much slower. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2274 #ifdef ST_NLINK_TRICK
5622
8b7046423d1e [project @ 2006-02-15 16:36:32 by jwe]
jwe
parents: 5618
diff changeset
2275 if (links != 2)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2276 #endif /* not ST_NLINK_TRICK */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2277 /* All criteria are met; find subdirectories. */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2278 do_subdir (str_list_ptr, name, potential_len, post);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2279 #ifdef ST_NLINK_TRICK
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2280 else if (post.empty ())
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2281 /* Nothing to match, no recursive subdirectories to
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2282 look for: we're done with this branch. Add it. */
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2283 dir_list_add (str_list_ptr, name);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2284 #endif
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2285 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2286
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2287 /* Remove the directory entry we just checked from `name'. */
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2288 name.resize (elt_length);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2289 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2290 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2291
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2292 xclosedir (dir);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2293 #endif /* not WIN32 */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2294 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2295
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2296 /* Assume ELT is non-empty and non-NULL. Return list of corresponding
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2297 directories (with no terminating NULL entry) in STR_LIST_PTR. Start
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2298 looking for magic constructs at START. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2299
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2300 static void
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2301 expand_elt (str_llist_type *str_list_ptr, const std::string& elt,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2302 unsigned /* start */)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2303 {
6306
1ef6ac757951 [project @ 2007-02-15 03:39:10 by jwe]
jwe
parents: 6106
diff changeset
2304 #if 0
1ef6ac757951 [project @ 2007-02-15 03:39:10 by jwe]
jwe
parents: 6106
diff changeset
2305 // We don't want magic constructs.
1ef6ac757951 [project @ 2007-02-15 03:39:10 by jwe]
jwe
parents: 6106
diff changeset
2306
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2307 size_t elt_len = elt.length ();
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2308
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2309 size_t dir = start;
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2310
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2311
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2312 while (dir < elt_len)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2313 {
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2314 if (IS_DIR_SEP (elt[dir]))
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2315 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2316 /* If two or more consecutive /'s, find subdirectories. */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2317 if (++dir < elt_len && IS_DIR_SEP (elt[dir]))
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2318 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2319 size_t i = dir;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2320 while (i < elt_len && IS_DIR_SEP (elt[i]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2321 i++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2322
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2323 std::string post = elt.substr (i);
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2324
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2325 do_subdir (str_list_ptr, elt, dir, post);
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2326
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2327 return;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2328 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2329
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2330 /* No special stuff at this slash. Keep going. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2331 }
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2332 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2333 dir++;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2334 }
6306
1ef6ac757951 [project @ 2007-02-15 03:39:10 by jwe]
jwe
parents: 6106
diff changeset
2335 #endif
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2336
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2337 /* When we reach the end of ELT, it will be a normal filename. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2338 checked_dir_list_add (str_list_ptr, elt);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2339 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
2340
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2341 /* Here is the entry point. Returns directory list for ELT. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2342
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2343 /* Given a path element ELT, return a pointer to a NULL-terminated list
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2344 of the corresponding (existing) directory or directories, with
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2345 trailing slashes, or NULL. If ELT is the empty string, check the
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2346 current working directory.
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2347
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2348 It's up to the caller to expand ELT. This is because this routine is
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2349 most likely only useful to be called from `kpse_path_search', which
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2350 has already assumed expansion has been done. */
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2351
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2352 static str_llist_type *
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2353 kpse_element_dirs (const std::string& elt)
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2354 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2355 str_llist_type *ret;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2356
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2357 /* If given nothing, return nothing. */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2358 if (elt.empty ())
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2359 return 0;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2360
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2361 /* If we've already cached the answer for ELT, return it. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2362 ret = cached (elt);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2363 if (ret)
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2364 return ret;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2365
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2366 /* We're going to have a real directory list to return. */
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2367 ret = new str_llist_type;
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2368 *ret = 0;
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2369
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2370 /* We handle the hard case in a subroutine. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2371 expand_elt (ret, elt, 0);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2372
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2373 /* Remember the directory list we just found, in case future calls are
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2374 made with the same ELT. */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2375 cache (elt, ret);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2376
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2377 #ifdef KPSE_DEBUG
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2378 if (KPSE_DEBUG_P (KPSE_DEBUG_EXPAND))
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2379 {
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2380 DEBUGF1 ("path element %s =>", elt.c_str ());
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2381 if (ret)
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2382 {
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2383 str_llist_elt_type *e;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2384 for (e = *ret; e; e = STR_LLIST_NEXT (*e))
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
2385 gnulib::fprintf (stderr, " %s", (STR_LLIST (*e)).c_str ());
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2386 }
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
2387 gnulib::putc ('\n', stderr);
4378
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2388 fflush (stderr);
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2389 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2390 #endif /* KPSE_DEBUG */
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2391
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2392 return ret;
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2393 }
7d48a8fba1d4 [project @ 2003-04-19 00:03:47 by jwe]
jwe
parents:
diff changeset
2394
4386
112a509bd2e6 [project @ 2003-04-24 18:41:02 by jwe]
jwe
parents: 4385
diff changeset
2395 #ifndef WIN32
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2396 void
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2397 xclosedir (DIR *d)
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2398 {
10411
479cc8a0a846 use gnulib namespace
John W. Eaton <jwe@octave.org>
parents: 10314
diff changeset
2399 int ret = gnulib::closedir (d);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2400
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2401 if (ret != 0)
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2402 FATAL ("closedir failed");
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2403 }
4386
112a509bd2e6 [project @ 2003-04-24 18:41:02 by jwe]
jwe
parents: 4385
diff changeset
2404 #endif
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2405
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2406 /* Help the user discover what's going on. */
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2407
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2408 #ifdef KPSE_DEBUG
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2409
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2410 /* If the real definitions of fopen or fclose are macros, we lose -- the
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2411 #undef won't restore them. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2412
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2413 static FILE *
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2414 fopen (const char *filename, const char *mode)
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2415 {
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2416 #undef fopen
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2417 FILE *ret = fopen (filename, mode);
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2418
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2419 if (KPSE_DEBUG_P (KPSE_DEBUG_FOPEN))
5760
8d7162924bd3 [project @ 2006-04-14 04:01:37 by jwe]
jwe
parents: 5622
diff changeset
2420 DEBUGF3 ("fopen (%s, %s) => 0x%lx\n", filename, mode,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2421 reinterpret_cast<unsigned long> (ret));
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2422
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2423 return ret;
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2424 }
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2425
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2426 #endif
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2427
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2428 /* Implementation of a linked list of strings. */
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2429
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2430 /* Add the new string STR to the end of the list L. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2431
5085
6861a84b1d47 [project @ 2004-11-19 03:26:32 by jwe]
jwe
parents: 4928
diff changeset
2432 static void
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2433 str_llist_add (str_llist_type *l, const std::string& str)
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2434 {
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2435 str_llist_elt_type *e;
4390
c378263ef911 [project @ 2003-04-25 21:05:06 by jwe]
jwe
parents: 4389
diff changeset
2436 str_llist_elt_type *new_elt = new str_llist_elt_type;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2437
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2438 /* The new element will be at the end of the list. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2439 STR_LLIST (*new_elt) = str;
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2440 STR_LLIST_MOVED (*new_elt) = 0;
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2441 STR_LLIST_NEXT (*new_elt) = 0;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2442
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2443 /* Find the current end of the list. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2444 for (e = *l; e && STR_LLIST_NEXT (*e); e = STR_LLIST_NEXT (*e))
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2445 ;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2446
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2447 if (! e)
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2448 *l = new_elt;
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2449 else
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2450 STR_LLIST_NEXT (*e) = new_elt;
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2451 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
2452
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2453 /* Move an element towards the top. The idea is that when a file is
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2454 found in a given directory, later files will likely be in that same
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2455 directory, and looking for the file in all the directories in between
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2456 is thus a waste. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2457
5085
6861a84b1d47 [project @ 2004-11-19 03:26:32 by jwe]
jwe
parents: 4928
diff changeset
2458 static void
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2459 str_llist_float (str_llist_type *l, str_llist_elt_type *mover)
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2460 {
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2461 str_llist_elt_type *last_moved, *unmoved;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2462
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2463 /* If we've already moved this element, never mind. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2464 if (STR_LLIST_MOVED (*mover))
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2465 return;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2466
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2467 /* Find the first unmoved element (to insert before). We're
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2468 guaranteed this will terminate, since MOVER itself is currently
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2469 unmoved, and it must be in L (by hypothesis). */
4398
cd8bf2c6797a [project @ 2003-04-29 04:21:01 by jwe]
jwe
parents: 4397
diff changeset
2470 for (last_moved = 0, unmoved = *l; STR_LLIST_MOVED (*unmoved);
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2471 last_moved = unmoved, unmoved = STR_LLIST_NEXT (*unmoved))
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2472 ;
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2473
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2474 /* If we are the first unmoved element, nothing to relink. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2475 if (unmoved != mover)
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2476 { /* Remember `mover's current successor, so we can relink `mover's
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2477 predecessor to it. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2478 str_llist_elt_type *before_mover;
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2479 str_llist_elt_type *after_mover = STR_LLIST_NEXT (*mover);
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2480
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2481 /* Find `mover's predecessor. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2482 for (before_mover = unmoved; STR_LLIST_NEXT (*before_mover) != mover;
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2483 before_mover = STR_LLIST_NEXT (*before_mover))
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2484 ;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2485
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2486 /* `before_mover' now links to `after_mover'. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2487 STR_LLIST_NEXT (*before_mover) = after_mover;
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2488
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2489 /* Insert `mover' before `unmoved' and after `last_moved' (or at
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2490 the head of the list). */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2491 STR_LLIST_NEXT (*mover) = unmoved;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2492 if (! last_moved)
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2493 *l = mover;
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2494 else
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2495 STR_LLIST_NEXT (*last_moved) = mover;
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2496 }
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2497
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2498 /* We've moved it. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2499 STR_LLIST_MOVED (*mover) = 1;
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2500 }
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2501
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4398
diff changeset
2502 /* Variable expansion. */
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2503
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2504 /* We have to keep track of variables being expanded, otherwise
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2505 constructs like TEXINPUTS = $TEXINPUTS result in an infinite loop.
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2506 (Or indirectly recursive variables, etc.) Our simple solution is to
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2507 add to a list each time an expansion is started, and check the list
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2508 before expanding. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2509
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2510 static std::map <std::string, bool> expansions;
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2511
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2512 static void
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2513 expanding (const std::string& var, bool xp)
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2514 {
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2515 expansions[var] = xp;
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2516 }
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2517
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2518 /* Return whether VAR is currently being expanding. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2519
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2520 static bool
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2521 expanding_p (const std::string& var)
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2522 {
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2523 return (expansions.find (var) != expansions.end ())
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2524 ? expansions[var] : false;
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2525 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
2526
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2527 /* Append the result of value of `var' to EXPANSION, where `var' begins
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2528 at START and ends at END. If `var' is not set, do not complain.
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2529 This is a subroutine for the more complicated expansion function. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2530
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2531 static void
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2532 expand (std::string &expansion, const std::string& var)
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2533 {
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2534 if (expanding_p (var))
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2535 {
4396
452698dd6ed9 [project @ 2003-04-28 19:04:05 by jwe]
jwe
parents: 4395
diff changeset
2536 (*current_liboctave_warning_handler)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2537 ("kpathsea: variable `%s' references itself (eventually)",
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2538 var.c_str ());
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2539 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2540 else
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2541 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2542 /* Check for an environment variable. */
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2543 std::string value = octave_env::getenv (var);
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2544
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2545 if (! value.empty ())
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2546 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2547 expanding (var, true);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2548 std::string tmp = kpse_var_expand (value);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2549 expanding (var, false);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2550 expansion += tmp;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2551 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2552 }
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2553 }
4392
dd108fa566fa [project @ 2003-04-26 17:00:24 by jwe]
jwe
parents: 4391
diff changeset
2554
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2555 /* Can't think of when it would be useful to change these (and the
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2556 diagnostic messages assume them), but ... */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2557 #ifndef IS_VAR_START /* starts all variable references */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2558 #define IS_VAR_START(c) ((c) == '$')
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2559 #endif
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2560 #ifndef IS_VAR_CHAR /* variable name constituent */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2561 #define IS_VAR_CHAR(c) (isalnum (c) || (c) == '_')
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2562 #endif
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2563 #ifndef IS_VAR_BEGIN_DELIMITER /* start delimited variable name (after $) */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2564 #define IS_VAR_BEGIN_DELIMITER(c) ((c) == '{')
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2565 #endif
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2566 #ifndef IS_VAR_END_DELIMITER
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2567 #define IS_VAR_END_DELIMITER(c) ((c) == '}')
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2568 #endif
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2569
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2570 /* Maybe we should support some or all of the various shell ${...}
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2571 constructs, especially ${var-value}. */
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2572
5085
6861a84b1d47 [project @ 2004-11-19 03:26:32 by jwe]
jwe
parents: 4928
diff changeset
2573 static std::string
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2574 kpse_var_expand (const std::string& src)
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2575 {
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2576 std::string expansion;
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2577
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2578 size_t src_len = src.length ();
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2579
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2580 /* Copy everything but variable constructs. */
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2581 for (size_t i = 0; i < src_len; i++)
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2582 {
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2583 if (IS_VAR_START (src[i]))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2584 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2585 i++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2586
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2587 /* Three cases: `$VAR', `${VAR}', `$<anything-else>'. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2588 if (IS_VAR_CHAR (src[i]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2589 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2590 /* $V: collect name constituents, then expand. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2591 size_t var_end = i;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2592
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2593 do
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2594 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2595 var_end++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2596 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2597 while (IS_VAR_CHAR (src[var_end]));
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2598
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2599 var_end--; /* had to go one past */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2600 expand (expansion, src.substr (i, var_end - i + 1));
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2601 i = var_end;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2602
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2603 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2604 else if (IS_VAR_BEGIN_DELIMITER (src[i]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2605 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2606 /* ${: scan ahead for matching delimiter, then expand. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2607 size_t var_end = ++i;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2608
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2609 while (var_end < src_len && !IS_VAR_END_DELIMITER (src[var_end]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2610 var_end++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2611
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2612 if (var_end == src_len)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2613 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2614 (*current_liboctave_warning_handler)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2615 ("%s: No matching } for ${", src.c_str ());
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2616 i = var_end - 1; /* will incr to eos at top of loop */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2617 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2618 else
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2619 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2620 expand (expansion, src.substr (i, var_end - i));
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2621 i = var_end; /* will incr past } at top of loop*/
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2622 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2623 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2624 else
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2625 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2626 /* $<something-else>: error. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2627 (*current_liboctave_warning_handler)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2628 ("%s: Unrecognized variable construct `$%c'",
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2629 src.c_str (), src[i]);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2630
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2631 /* Just ignore those chars and keep going. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2632 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2633 }
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2634 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2635 expansion += src[i];
4391
17530e977bec [project @ 2003-04-26 04:50:12 by jwe]
jwe
parents: 4390
diff changeset
2636 }
4389
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2637
fa9f6dde6c24 [project @ 2003-04-25 04:45:56 by jwe]
jwe
parents: 4386
diff changeset
2638 return expansion;
4385
de8c1d2ee728 [project @ 2003-04-24 03:27:41 by jwe]
jwe
parents: 4379
diff changeset
2639 }