]> git.donarmstrong.com Git - lilypond.git/blob - lily/kpath.cc
(transform_heads): replace
[lilypond.git] / lily / kpath.cc
1 /*   
2   kpath.cc -- glue kpathsea to lily. Need some ugly kludges for gcc 2.96
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "kpath.hh"
10
11 #include <cstdio>
12 #include <cstring>
13
14 /*
15
16 The problem, as far as I can tell, is that MacOS X has its getopt
17 prototype in <unistd.h>, while I think other operating systems have it
18 in other places. <unistd.h> is included by kpathsea.h, so you end up
19 renaming both conflicting prototypes to YAKLUDGE.
20
21 I found a somewhat more elegant patch for this: Just #include
22 <unistd.h> before defining YAKLUDGE.
23
24 */
25
26 #include <unistd.h>     
27
28 #include "config.hh"
29
30 #define popen REALLYUGLYKLUDGE
31 #define pclose ANOTHERREALLYUGLYKLUDGE
32 #define getopt YAKLUDGE
33
34 #if HAVE_KPATHSEA_KPATHSEA_H
35 extern "C" {
36 #include <kpathsea/kpathsea.h>
37 #include <kpathsea/tex-file.h>
38 }
39 #endif
40
41 #include "file-path.hh"
42 #include "main.hh"
43 #include "source-file.hh"
44 #include "warn.hh"
45
46 String
47 kpathsea_find_afm (char const *name)
48 {
49 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
50
51   if (char *afm = kpse_find_file (name, kpse_afm_format, false))
52     return afm;
53 #if 0 /* Do not mutter about afms, since we try to find them first, and
54          lots of TFMs don't have AFMs. */
55   warning (_f ("kpathsea can not find AFM file `%s'", name));
56 #endif
57 #endif
58   return "";
59 }
60
61 String
62 kpathsea_find_tfm (char const *name)
63 {
64   String file_name = global_path.find (String (name) + ".tfm");
65 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
66   if (file_name.is_empty ())
67     {
68       /* If invoked for a TeX font, we could do TRUE (must exist).
69          We could also do:
70            p = kpse_find_file (name, kpse_mf_format, false);
71            if (p)
72              p = kpse_find_file (name, kpse_mf_format, true);
73
74              but we assume that if there is a .PFA, there is also a .TFM,
75          and it's no use generating TFMs on the fly, because PFAs cannot
76          be generated on the fly. */
77       if (char *tfm = kpse_find_file (name, kpse_tfm_format, false))
78         return tfm;
79       warning (_f ("kpathsea can not find TFM file: `%s'", name));
80     }
81 #endif
82   return file_name;
83 }
84
85 #if KPATHSEA
86 /* FIXME: this should be part of kpathsea */
87
88 static kpse_file_format_type
89 kpathsea_find_format (String name)
90 {
91   for (int i = 0; i < kpse_last_format; i++)
92     {
93       if (!kpse_format_info[i].type)
94         kpse_init_format ((kpse_file_format_type) i);
95
96       char const **suffixes[] = { kpse_format_info[i].suffix,
97                                   kpse_format_info[i].alt_suffix };
98       for (int j = 0; j < 2; j++)
99         for (char const **p = suffixes[j]; p && *p; p++)
100           {
101             String suffix = *p;
102             if (name.right_string (suffix.length ()) == suffix)
103               return (kpse_file_format_type) i;
104           }
105     }
106   return kpse_last_format;
107 }
108 #endif
109
110 String
111 kpathsea_gulp_file_to_string (String name)
112 {
113   String file_name = global_path.find (name);
114
115 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
116   if (file_name.is_empty ())
117     {
118       char *p = kpse_find_file (name.to_str0 (), kpathsea_find_format (name),
119         true);
120       if (p)
121         file_name = p;
122       else
123         warning (_f ("kpathsea can not find file: `%s'", name));
124     }
125 #endif
126
127   if (file_name.is_empty ())
128     error (_f ("can't find file: `%s'", name));
129
130   if (verbose_global_b)
131     progress_indication ("[" + file_name);
132
133   int filesize;
134   char *str = gulp_file (file_name, &filesize);
135   String string (str);
136   delete[] str;
137   
138   if (verbose_global_b)
139     progress_indication ("]");
140
141   return string;
142 }
143
144
145 LY_DEFINE (ly_find_file, "ly:find-file",
146            1, 0, 0, (SCM name),
147            "Return the absolute file name of @var{name},"
148            "or @code{#f} if not found.")
149 {
150   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
151
152   String nm = ly_scm2string (name);
153   String file_name = global_path.find (nm);
154   if (file_name.is_empty ())
155     return SCM_BOOL_F;
156   
157   return scm_makfrom0str (file_name.to_str0 ());
158 }
159
160 LY_DEFINE (ly_kpathsea_find_file, "ly:kpathsea-find-file",
161            1, 0, 0, (SCM name),
162            "Return the absolute file name of @var{name},"
163            "or @code{#f} if not found.")
164 {
165   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
166
167   String nm = ly_scm2string (name);
168   String file_name = global_path.find (nm);
169   if (file_name.is_empty ())
170     {
171       if (char *p = kpse_find_file (nm.to_str0 (), kpathsea_find_format (nm),
172                                     true))
173         return scm_makfrom0str (p);
174       return SCM_BOOL_F;
175     }
176   return scm_makfrom0str (file_name.to_str0 ());
177 }
178
179 LY_DEFINE (ly_kpathsea_expand_variable, "ly:kpathsea-expand-variable",
180            1, 0, 0, (SCM var),
181            "Return the expanded version  @var{var}.")
182 {
183   SCM_ASSERT_TYPE (scm_is_string (var), var, SCM_ARG1, __FUNCTION__, "string");
184
185   String nm = ly_scm2string (var);
186   char *result =  kpse_var_expand (nm.to_str0 ());
187   SCM ret =  scm_makfrom0str (result);
188   delete[] result;
189
190   return ret;
191 }
192
193 void
194 initialize_kpathsea (char *av0)
195 {
196 #if KPATHSEA && HAVE_KPATHSEA_KPATHSEA_H
197
198   /*
199    initialize kpathsea
200    */
201   kpse_set_program_name (av0, NULL);
202   kpse_maketex_option ("tfm", TRUE);
203 #endif
204 }