]> git.donarmstrong.com Git - lilypond.git/blob - lily/parse-scm.cc
* scm/output-tex.scm (output-tex-string): tighten safe security.
[lilypond.git] / lily / parse-scm.cc
1 #include <stdio.h>
2
3 #include "ly-module.hh"
4 #include "lily-guile.hh"
5 #include "main.hh"
6 #include "paper-book.hh"
7 #include "parse-scm.hh"
8 #include "string.hh"
9 #include "source-file.hh"
10
11 /* Pass string to scm parser, evaluate one expression.
12    Return result value and #chars read.
13    
14    Thanks to Gary Houston <ghouston@freewire.co.uk>  */
15 SCM
16 internal_ly_parse_scm (Parse_start * ps)
17 {
18   Source_file* sf =ps->start_location_.source_file_;
19   SCM port = sf->get_port ();
20
21   int off = ps->start_location_.defined_str0_ - sf->to_str0();
22   
23   scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
24   SCM from = scm_ftell (port);
25
26   SCM form;
27   SCM answer = SCM_UNSPECIFIED;
28
29   /* Read expression from port */
30   if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
31     {
32       if (ps->safe_)
33         answer = scm_eval  (form,
34                             scm_call_0 (ly_scheme_function ("make-safe-lilypond-module")));
35       else
36         answer = scm_primitive_eval (form);
37     }
38  
39   /* Reset read_buf for scm_ftell.
40      Shouldn't scm_read () do this for us?  */
41   scm_fill_input (port);
42   SCM to = scm_ftell (port);
43   ps->nchars = ly_scm2int (to) - ly_scm2int (from);
44
45   /* Don't close the port here; if we re-enter this function via a
46      continuation, then the next time we enter it, we'll get an error.
47      It's a string port anyway, so there's no advantage to closing it
48      early. */
49   // scm_close_port (port);
50
51   return answer;
52 }
53
54 SCM
55 catch_protected_parse_body (void *p)
56 {
57   Parse_start *ps = (Parse_start*) p;
58   
59   return internal_ly_parse_scm (ps);
60 }
61
62 SCM 
63 parse_handler (void * data, SCM tag, SCM args)
64 {
65   Parse_start* ps = (Parse_start*) data;
66   (void) tag;                   // prevent warning
67   
68   ps->start_location_.error (_("GUILE signaled an error for the expression beginning here"));
69
70   if (scm_ilength (args) > 2)
71     scm_display_error_message (ly_cadr (args), ly_caddr (args), scm_current_error_port ());
72
73   /*
74     The following is a kludge; we should probably search for
75     [a-z][0-9] (a note), and start before that.
76    */
77   ps->nchars = 1;
78     
79   return SCM_UNDEFINED;
80 }
81
82 /*
83   Do some magical incantations: if not, lily will exit on the first
84   GUILE error, leaving no location trace. 
85  */
86
87
88 #if GUILE_MINOR_VERSION < 7
89   #define READ_ERROR "misc-error"
90   #else
91   #define READ_ERROR "read-error"
92 #endif
93
94 SCM
95 protected_ly_parse_scm (Parse_start *ps)
96 {
97   return scm_internal_catch (ly_symbol2scm (READ_ERROR),
98                              &catch_protected_parse_body,
99                              (void*) ps,
100                              &parse_handler, (void*) ps);
101 }
102
103 bool parse_protect_global = true; 
104
105 /* Try parsing.  Upon failure return SCM_UNDEFINED. */
106 SCM
107 ly_parse_scm (char const* s, int *n, Input i, bool safe)
108 {
109   Parse_start ps ;
110   ps.str = s;
111   ps.start_location_ = i;
112   ps.safe_ = safe;
113   
114   SCM ans = parse_protect_global ? protected_ly_parse_scm (&ps)
115     : internal_ly_parse_scm (&ps);
116   *n = ps.nchars;
117
118   return ans;  
119 }
120