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