]> git.donarmstrong.com Git - lilypond.git/blob - lily/kpath.cc
ca73ae052f995b1ae977e0790d032fc374a4988c
[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 #include "kpath-private.hh"
46
47 String
48 kpathsea_find_afm (char const *name)
49 {
50 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
51
52   if (char *afm = kpse_find_file (name, kpse_afm_format, false))
53     return afm;
54 #if 0 /* Do not mutter about afms, since we try to find them first, and
55          lots of TFMs don't have AFMs. */
56   warning (_f ("kpathsea can not find AFM file `%s'", name));
57 #endif
58 #endif
59   return "";
60 }
61
62 String
63 kpathsea_find_tfm (char const *name)
64 {
65   String file_name = global_path.find (String (name) + ".tfm");
66 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
67   if (file_name.is_empty ())
68     {
69       /* If invoked for a TeX font, we could do TRUE (must exist).
70          We could also do:
71            p = kpse_find_file (name, kpse_mf_format, false);
72            if (p)
73              p = kpse_find_file (name, kpse_mf_format, true);
74
75              but we assume that if there is a .PFA, there is also a .TFM,
76          and it's no use generating TFMs on the fly, because PFAs cannot
77          be generated on the fly. */
78       if (char *tfm = kpse_find_file (name, kpse_tfm_format, false))
79         return tfm;
80       warning (_f ("kpathsea can not find TFM file: `%s'", name));
81     }
82 #endif
83   return file_name;
84 }
85
86 #if KPATHSEA
87 /* FIXME: this should be part of kpathsea */
88
89 kpse_file_format_type
90 kpathsea_find_format (String name)
91 {
92   for (int i = 0; i < kpse_last_format; i++)
93     {
94       if (!kpse_format_info[i].type)
95         kpse_init_format ((kpse_file_format_type) i);
96
97       char const **suffixes[] = { kpse_format_info[i].suffix,
98                                   kpse_format_info[i].alt_suffix };
99       for (int j = 0; j < 2; j++)
100         for (char const **p = suffixes[j]; p && *p; p++)
101           {
102             String suffix = *p;
103             if (name.right_string (suffix.length ()) == suffix)
104               return (kpse_file_format_type) i;
105           }
106     }
107   return kpse_last_format;
108 }
109 #endif
110
111 String
112 kpathsea_gulp_file_to_string (String name)
113 {
114   String file_name = global_path.find (name);
115
116 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
117   if (file_name.is_empty ())
118     {
119       char *p = kpse_find_file (name.to_str0 (), kpathsea_find_format (name),
120         true);
121       if (p)
122         file_name = p;
123       else
124         warning (_f ("kpathsea can not find file: `%s'", name));
125     }
126 #endif
127
128   if (file_name.is_empty ())
129     error (_f ("can't find file: `%s'", name));
130
131   if (be_verbose_global)
132     progress_indication ("[" + file_name);
133
134   int filesize;
135   char *str = gulp_file (file_name, &filesize);
136   String string (str);
137   delete[] str;
138   
139   if (be_verbose_global)
140     progress_indication ("]");
141
142   return string;
143 }
144
145
146
147 void
148 initialize_kpathsea (char *av0)
149 {
150 #if KPATHSEA && HAVE_KPATHSEA_KPATHSEA_H
151
152   /*
153    initialize kpathsea
154    */
155   kpse_set_program_name (av0, NULL);
156   kpse_maketex_option ("tfm", TRUE);
157 #endif
158 }