]> git.donarmstrong.com Git - lilypond.git/blob - lily/kpath.cc
* configure.in: Test for and accept lmodern if EC fonts not found.
[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   char * name_ptr =  kpse_find_file (name, kpse_afm_format, false);
51
52   if (!name_ptr)
53     {
54   /*
55     don't mutter about afms, since we try to find them first, and lots of
56     TFMs don't have AFMs. 
57    */
58       //      warning (_f ("kpathsea couldn't find AFM file `%s'", name));
59     }
60   else
61     return name_ptr;
62   
63 #endif
64   return "";
65 }
66
67 String
68 kpathsea_find_tfm (char const *name)
69 {
70   String file_name = global_path.find (String (name) + ".tfm");
71 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
72   if (file_name.is_empty ())
73     {
74       /* If invoked for a TeX font, we could do TRUE (must exist).
75          We could also do:
76            p = kpse_find_file (name, kpse_mf_format, false);
77            if (p)
78              p = kpse_find_file (name, kpse_mf_format, true);
79
80              but we assume that if there is a .PFA, there is also a .TFM,
81          and it's no use generating TFMs on the fly, because PFAs cannot
82          be generated on the fly. */
83       char *p = kpse_find_file (name, kpse_tfm_format, false);
84       if (!p)
85         warning (_f ("kpathsea can not find TFM file: `%s'", name));
86       else
87         file_name = p;
88     }
89 #endif
90   return file_name;
91 }
92
93 #if KPATHSEA
94 /* FIXME: this should be part of kpathsea */
95
96 static kpse_file_format_type
97 kpathsea_find_format (String name)
98 {
99   for (int i = 0; i < kpse_last_format; i++)
100     {
101       if (!kpse_format_info[i].type)
102         kpse_init_format ((kpse_file_format_type) i);
103
104       char const **suffixes[] = { kpse_format_info[i].suffix,
105                                   kpse_format_info[i].alt_suffix };
106       for (int j = 0; j < 2; j++)
107         for (char const **p = suffixes[j]; p && *p; p++)
108           {
109             String suffix = *p;
110             if (name.right_string (suffix.length ()) == suffix)
111               return (kpse_file_format_type) i;
112           }
113     }
114   return kpse_last_format;
115 }
116 #endif
117
118 String
119 kpathsea_gulp_file_to_string (String name)
120 {
121   String file_name = global_path.find (name);
122
123 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
124   if (file_name.is_empty ())
125     {
126       char *p = kpse_find_file (name.to_str0 (), kpathsea_find_format (name),
127         true);
128       if (p)
129         file_name = p;
130       else
131         warning (_f ("kpathsea can not find file: `%s'", name));
132     }
133 #endif
134
135   if (file_name.is_empty ())
136     error (_f ("can't find file: `%s'", name));
137
138   if (verbose_global_b)
139     progress_indication ("[" + file_name);
140
141   int filesize;
142   char *str = gulp_file (file_name, &filesize);
143   String string (str);
144   delete[] str;
145   
146   if (verbose_global_b)
147     progress_indication ("]");
148
149   return string;
150 }
151
152 LY_DEFINE (ly_kpathsea_find_file, "ly:kpathsea-find-file",
153            1, 0, 0, (SCM name),
154            "Return the absolute file name of @var{name},"
155            "or @code{#f} if not found.")
156 {
157   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
158
159   String nm = ly_scm2string (name);
160   String file_name = global_path.find (nm);
161   if (file_name.is_empty ())
162     {
163       if (char *p = kpse_find_file (nm.to_str0 (), kpathsea_find_format (nm),
164                                     true))
165         return scm_makfrom0str (p);
166       return SCM_BOOL_F;
167     }
168   return scm_makfrom0str (file_name.to_str0 ());
169 }
170
171 LY_DEFINE (ly_kpathsea_expand_variable, "ly:kpathsea-expand-variable",
172            1, 0, 0, (SCM var),
173            "Return the expanded version  @var{var}.")
174 {
175   SCM_ASSERT_TYPE (scm_is_string (var), var, SCM_ARG1, __FUNCTION__, "string");
176
177   String nm = ly_scm2string (var);
178   char *result =  kpse_var_expand (nm.to_str0 ());
179   SCM ret =  scm_makfrom0str (result);
180   delete[] result;
181
182   return ret;
183 }
184
185 void
186 initialize_kpathsea (char *av0)
187 {
188 #if KPATHSEA && HAVE_KPATHSEA_KPATHSEA_H
189
190   /*
191    initialize kpathsea
192    */
193   kpse_set_program_name (av0, NULL);
194   kpse_maketex_option ("tfm", TRUE);
195 #endif
196 }