]> git.donarmstrong.com Git - lilypond.git/blob - kpath-guile/kpath.c
01f3fde5581e5318eb00ca93e5e03c5477f8ee81
[lilypond.git] / kpath-guile / kpath.c
1 /*
2   kpath.c --  implement kpathsea bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9
10 #include <libguile.h>
11
12 #include "config.hh"
13 #include "guile-compatibility.hh"
14
15 #if KPATHSEA && HAVE_KPATHSEA_KPATHSEA_H
16
17 #include <stdio.h>
18 #include <string.h>
19
20 /*
21
22 The problem, as far as I can tell, is that MacOS X has its getopt
23 prototype in <unistd.h>, while I think other operating systems have it
24 in other places. <unistd.h> is included by kpathsea.h, so you end up
25 renaming both conflicting prototypes to YAKLUDGE.
26
27 I found a somewhat more elegant patch for this: Just #include
28 <unistd.h> before defining YAKLUDGE.
29
30 */
31
32 #include <unistd.h>     
33
34 #define popen REALLYUGLYKLUDGE
35 #define pclose ANOTHERREALLYUGLYKLUDGE
36 #define getopt YAKLUDGE
37
38 #if HAVE_KPATHSEA_KPATHSEA_H
39 #include <kpathsea/kpathsea.h>
40 #include <kpathsea/tex-file.h>
41 #endif
42
43
44 #if KPATHSEA
45 /* FIXME: this should be part of kpathsea */
46
47 kpse_file_format_type
48 kpathsea_find_format (const char* name)
49 {
50   int i;
51   int len = strlen (name);
52   for (i = 0; i < kpse_last_format; i++)
53     {
54       if (!kpse_format_info[i].type)
55         kpse_init_format ((kpse_file_format_type) i);
56
57       char const **suffixes[] = { kpse_format_info[i].suffix,
58                                   kpse_format_info[i].alt_suffix };
59       for (int j = 0; j < 2; j++)
60         for (char const **p = suffixes[j]; p && *p; p++)
61           {
62             int suflen = strlen (*p);
63             
64             if (!strncmp (name + len - suflen, *p, suflen))
65               return (kpse_file_format_type) i;
66           }
67     }
68   return kpse_last_format;
69 }
70 #endif
71
72
73
74
75
76
77 //         "Return the absolute file name of @var{name}, "
78 //         "or @code{#f} if not found.")
79 SCM
80 ly_kpathsea_find_file(SCM name)
81 {
82   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
83
84   char const * nm = scm_i_string_chars (name);
85   char *p = kpse_find_file (nm, kpathsea_find_format (nm),
86                             true);
87   if (p)
88     return scm_makfrom0str (p);
89   return SCM_BOOL_F;
90 }
91
92 //   "Return the expanded version  @var{var}.")
93 SCM ly_kpathsea_expand_variable(SCM var)
94 {
95   SCM_ASSERT_TYPE (scm_is_string (var), var, SCM_ARG1, __FUNCTION__, "string");
96
97   char const * nm = scm_i_string_chars (var);
98   char *result =  kpse_var_expand (nm);
99   SCM ret =  scm_makfrom0str (result);
100   free (result);
101
102   return ret;
103 }
104
105
106
107 void
108 initialize_kpathsea ()
109 {
110   /*
111    initialize kpathsea
112    */
113   kpse_set_program_name ("lilypond", NULL);
114   kpse_maketex_option ("tfm", TRUE);
115
116   SCM find = scm_c_define_gsubr ("ly:kpathsea-find-file", 1, 0, 0, ly_kpathsea_find_file);
117   scm_c_export ("ly:kpathsea-find-file", NULL);
118   SCM expand = scm_c_define_gsubr ("ly:kpathsea-expand-variable", 1, 0, 0, ly_kpathsea_find_file);
119   scm_c_export ("ly:kpathsea-expand-variable", NULL);
120 }
121
122 #endif