From: Han-Wen Nienhuys Date: Sat, 17 Aug 2002 01:30:28 +0000 (+0000) Subject: * lily/source-file.cc (init_port): add an SCM port to the X-Git-Tag: release/1.5.74~7 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=a517dce054511941847288622f7840ff9d2bd353;p=lilypond.git * lily/source-file.cc (init_port): add an SCM port to the sourcefile as well. * lily/parse-scm.cc (parse_handler): don't construct a new strport for every parsing. * lily/include/input-file-results.hh: move from file-results. Rename Input_file_settings to Input_file_results. --- diff --git a/ChangeLog b/ChangeLog index aaaa0c6aea..4e948b5c38 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,17 @@ 2002-08-17 Han-Wen Nienhuys + * lily/source-file.cc (init_port): add an SCM port to the + sourcefile as well. + + * lily/parse-scm.cc (parse_handler): don't construct a new strport + for every parsing. + + * lily/include/input-file-results.hh: move from + file-results. Rename Input_file_settings to Input_file_results. + + * flower/ : remove simple-file-storage, mapped-file-storage, + string-storage. Move source-file, binary-source-file to lily/ + * Documentation/header.html.in: remove FAQ * Documentation/index.texi (Top): reorganise @@ -8,8 +20,6 @@ * Documentation/topdocs/FAQ.texi: remove. - - * stepmake/stepmake/documentation-targets.make (footify): must depend on all .html files, otherwise rebuilds are broken due to the time stamps diff --git a/flower/binary-source-file.cc b/flower/binary-source-file.cc deleted file mode 100644 index bd6a4f84d3..0000000000 --- a/flower/binary-source-file.cc +++ /dev/null @@ -1,95 +0,0 @@ -/* - binary-source-file.cc -- implement Binary_source_file - - source file of the LilyPond music typesetter - - (c) 1997--2000 Jan Nieuwenhuizen -*/ - - -#include // INT_MAX -#include - -#include "flower-proto.hh" -#include "string.hh" -#include "source-file.hh" -#include "binary-source-file.hh" -#include "string-convert.hh" - -Binary_source_file::Binary_source_file (String& filename_string) - : Source_file (filename_string) -{ -} - -Binary_source_file::~Binary_source_file () -{ -} - -String -Binary_source_file::error_string (char const* pos_str0) const -{ - assert (this); - if (!in_b (pos_str0)) - return ""; - - char const* begin_str0 = pos_str0 - 8 >? to_str0 (); - char const* end_str0 = pos_str0 + 7 - -#ifndef BINARY_SOURCE_FILE_HH -#define BINARY_SOURCE_FILE_HH - -#include "source-file.hh" - -class Binary_source_file : public Source_file -{ -public: - Binary_source_file (String& filename_string ); - virtual ~Binary_source_file (); - - U8 get_U8 (); - U16 get_U16 (); - U32 get_U32 (); - Byte get_Byte () {return get_U8 (); } - int get_int () { return get_U32 (); } - - virtual String error_string (char const* pos_str0 ) const; - virtual int get_line (char const* pos_str0 ) const; -}; - -#endif // BINARY_SOURCE_FILE_HH diff --git a/flower/include/input.hh b/flower/include/input.hh deleted file mode 100644 index 4b12e25fb8..0000000000 --- a/flower/include/input.hh +++ /dev/null @@ -1,42 +0,0 @@ -/* - input.hh -- declare Input - - source file of the LilyPond music typesetter - - (c) 1997--2000 Han-Wen Nienhuys -*/ - - -#ifndef INPUT_HH -#define INPUT_HH - -#include "flower-proto.hh" - -/** - Base class for anything that records its poisition in the parse file. - */ -class Input { - char const *defined_str0_ ; - Source_file * source_file_; -public: - - void warning (String) const; // should use member func? - void non_fatal_error (String) const; - void error (String) const; - void message (String) const; - void set_spot (Input const &); - Input spot () const; - String location_string () const; - String line_number_string () const; - - - String file_string ()const; - int line_number ()const; - int column_number ()const; - - - Input (Source_file*, char const*); - Input (); -}; - -#endif // INPUT_HH diff --git a/flower/include/mapped-file-storage.hh b/flower/include/mapped-file-storage.hh deleted file mode 100644 index a936401b6d..0000000000 --- a/flower/include/mapped-file-storage.hh +++ /dev/null @@ -1,40 +0,0 @@ -/* - mapped-file-storage.hh -- declare Mapped_file_storage - - source file of the GNU LilyPond music typesetter - - (c) 1997--2000 Han-Wen Nienhuys - - */ - -#ifndef MAPPED_FILE_STORAGE_HH -#define MAPPED_FILE_STORAGE_HH - - - -#include "file-storage.hh" - -/** - Use mmap to "copy" a file into memory - */ -class Mapped_file_storage:public File_storage -{ -public: - Mapped_file_storage (String); -protected: - virtual char const* to_str0 () const; - virtual int length () const; - virtual ~Mapped_file_storage (); -private: - void open (String name); - void close (); - - void map (); - void unmap (); - int fildes_; - off_t size_off_; - caddr_t data_caddr_; -}; - -#endif /* MAPPED_FILE_STORAGE_HH */ - diff --git a/flower/include/simple-file-storage.hh b/flower/include/simple-file-storage.hh deleted file mode 100644 index f551ed8c57..0000000000 --- a/flower/include/simple-file-storage.hh +++ /dev/null @@ -1,33 +0,0 @@ -/* - simple-file-storage.hh -- declare - - source file of the GNU LilyPond music typesetter - - (c) 1997--2000 Han-Wen Nienhuys - - */ - -#ifndef SIMPLE_FILE_STORAGE_HH -#define SIMPLE_FILE_STORAGE_HH - -#include "file-storage.hh" - -/** - read file char by char and copy into a malloc array. - */ -class Simple_file_storage : public File_storage -{ - char * data_; - int len_; - - void load_stdin (); - void load_file (String); -public: - virtual char const*to_str0 () const; - virtual int length () const; - virtual ~Simple_file_storage (); - Simple_file_storage (String); -}; - -#endif /* SIMPLE_FILE_STORAGE_HH */ - diff --git a/flower/include/source-file.hh b/flower/include/source-file.hh deleted file mode 100644 index 9e9d8c34c2..0000000000 --- a/flower/include/source-file.hh +++ /dev/null @@ -1,63 +0,0 @@ - -#ifndef SOURCE_FILE_HH -#define SOURCE_FILE_HH - -#include - -#include "flower-proto.hh" -#include "string.hh" -#include "interval.hh" - -/** - class for reading and mapping a file. - - duplicates a lot of Data_file and Text_stream. - should look at including Data_file's functionality: - get_line (), get_word () here. -*/ - -class Source_file -{ -public: - /** Ugh! filename gets changed! The path to the opened file may - change, since it might be searched in multiple directories. */ - Source_file (String filename_string_r ); - - Source_file (String name_string, String data_string); - virtual ~Source_file (); - - char const* to_str0 () const; - virtual String error_string (char const* pos_str0 ) const; - std::istream * get_istream (); - bool in_b (char const* pos_str0 ) const; - int length () const; - virtual int get_line (char const* pos_str0 ) const; - String name_string () const; - String file_line_column_string (char const* str0 ) const; - - // return start + n - char const* seek_str0 (int n); - // return here + n bytes - char const* forward_str0 (int n); - char const* pos_str0 () { return pos_str0_; } - String get_string (int n); - void set_pos (char const * pos_str0); -public: - Slice line_slice (char const* pos_str0) const; - String line_string (char const* pos_str0) const; - int get_column (char const* pos_str0) const; - int get_char (char const* pos_str0) const; - - /* - DOCUMENT-ME - */ - char const* pos_str0_; - -private: - String name_string_; - std::istream* istream_; - File_storage * storage_; -}; - -#endif // SOURCE_FILE_HH // - diff --git a/flower/include/source.hh b/flower/include/source.hh deleted file mode 100644 index 82b557b111..0000000000 --- a/flower/include/source.hh +++ /dev/null @@ -1,37 +0,0 @@ -// -// source.hh -- part of LilyPond -// -// copyright 1997 Jan Nieuwenhuizen - -#ifndef SOURCE_HH -#define SOURCE_HH -#include "cons.hh" -#include "flower-proto.hh" - -/** - a set of sourcefiles. - - TODO: - */ -class Sources -{ - Sources (Sources const&) {} -public: - Sources (); - ~Sources (); - - Source_file * get_file (String &filename ); - Source_file* get_sourcefile (char const* str0 ); - void add (Source_file* sourcefile ); - void set_path (File_path*p); - void set_binary (bool); - - const File_path * path_C_; -private: - Cons *sourcefile_p_list_; - bool binary_b_ ; -}; - - - -#endif // SOURCE_HH // diff --git a/flower/include/string-storage.hh b/flower/include/string-storage.hh deleted file mode 100644 index dfe49c8c47..0000000000 --- a/flower/include/string-storage.hh +++ /dev/null @@ -1,30 +0,0 @@ -/* - string-storage.hh -- declare String_storage - - source file of the GNU LilyPond music typesetter - - (c) 1998--2000 Jan Nieuwenhuizen - - */ - -#ifndef STRING_STORAGE_HH -#define STRING_STORAGE_HH - -#include "string.hh" -#include "file-storage.hh" - -/** - Urg, let String act as file storage. - */ -class String_storage : public File_storage, protected String -{ -public: - String_storage (String s) : String (s) { } - -protected: - virtual char const* to_str0 () const { return String::to_str0 (); } - virtual int length () const { return String::length (); } -}; - -#endif /* STRING_STORAGE_HH */ - diff --git a/flower/input.cc b/flower/input.cc deleted file mode 100644 index 381907d635..0000000000 --- a/flower/input.cc +++ /dev/null @@ -1,146 +0,0 @@ -/* - input.cc -- implement Input - - source file of the LilyPond music typesetter - - (c) 1997--2000 Han-Wen Nienhuys -*/ - -#include - -#include "flower-proto.hh" -#include "input.hh" -#include "string.hh" -#include "source.hh" -#include "source-file.hh" - -Input::Input (Source_file*s, char const *cl) -{ - source_file_=s; - defined_str0_=cl; -} - -Input::Input () -{ - source_file_ = 0; - defined_str0_ = 0; -} - -Input -Input::spot () const -{ - return *this; -} - -void -Input::set_spot (Input const &i) -{ - *this = i; -} - -/* - Produce GNU-compliant error message. Correcting lilypond source is - such a breeze if you ('re edidor) know (s) the error column too - - Format: - - [file:line:column:][warning:]message - - */ -void -Input::message (String message_string) const -{ - String str; - - /* - marked "Work in prgress" in GNU iostream - libg++ 2.7.2.8 - libstdc++ 2.8.1 - - why not just return always -1 (unknown), - iso breaking the interface? - - int col = cerr.rdbuf ()->column (); - - */ - - // well, we don't want to loose first warning... - int col = 1; - if (col > 0) - str += "\n"; - - if (source_file_) - str += location_string () + String (": "); - - str += message_string; - if (source_file_) - { - str += ":\n"; - str += source_file_->error_string (defined_str0_); - } - fprintf (stderr, "%s\n", str.to_str0 ()); -} - -void -Input::warning (String message_string) const -{ - message (_ ("warning: ") + message_string); -} -void -Input::error (String s) const -{ - message (_ ("error: ")+ s); -} - -void -Input::non_fatal_error (String s) const -{ - message (_ ("non fatal error: ") + s); -} -String -Input::location_string () const -{ - if (source_file_) - return source_file_->file_line_column_string (defined_str0_); - else - return " (" + _ ("position unknown") + ")"; -} - -String -Input::line_number_string () const -{ - if (source_file_) - return to_string (source_file_->get_line (defined_str0_)); - else - return "?"; -} - -String -Input::file_string () const -{ - if (source_file_) - return source_file_->name_string (); - else - return ""; -} - - -int -Input::line_number () const -{ - if (source_file_) - return source_file_->get_line (defined_str0_); - else - return 0; - -} - -int -Input::column_number () const -{ - if (source_file_) - return source_file_->get_column (defined_str0_); - else - return 0; - -} diff --git a/flower/mapped-file-storage.cc b/flower/mapped-file-storage.cc deleted file mode 100644 index f9baca9a5a..0000000000 --- a/flower/mapped-file-storage.cc +++ /dev/null @@ -1,140 +0,0 @@ -#ifdef HAIRY_STUFF - -/* - file-storage.cc -- implement Mapped_file_storage - - source file of the GNU LilyPond music typesetter - - (c) 1997--2000 Han-Wen Nienhuys - Jan Nieuwenhuizen . - - Nextstep fixes by tiggr@ics.ele.tue.nl -*/ - -#include // open, mmap -#include // open -#include // mmap -#include // INT_MAX -#include // open -#include // close, stat -#include // fdopen -#include // strerror -#include // errno - - - -#ifdef __NeXT__ -#include -#include -#include -#endif - -#include "string.hh" -#include "flower-proto.hh" -#include "warn.hh" -#include "file-storage.hh" - -Mapped_file_storage::Mapped_file_storage (String s) -{ - data_caddr_ = 0; - fildes_ = 0; - size_off_ = 0; - open (s); -} - -char const* -Mapped_file_storage::to_str0 () const -{ - return (char const*)data_caddr_; -} - -void -Mapped_file_storage::map () -{ - if (fildes_ == -1) - return; - -#ifdef __NeXT__ - /* Should be #if !HAVE_MMAP && HAVE_MAP_FD... */ - { - vm_offset_t address; - kern_return_t r; - - r = map_fd (fildes_, (vm_offset_t) 0, &address, TRUE, size_off_); - if (r != KERN_SUCCESS) - warning (String ("map_fd: ") + mach_error_string (r)); - else - data_caddr_ = (char *) address; - } -#else - - data_caddr_ = (caddr_t)mmap ((void*)0, size_off_, PROT_READ, MAP_SHARED, fildes_, 0); - - if ((int)data_caddr_ == -1) - warning (_ ("can't map file") + ": " + strerror (errno)); - -#endif -} - - -void -Mapped_file_storage::open (String name_string) -{ - fildes_ = ::open (name_string.to_str0 (), O_RDONLY); - - if (fildes_ == -1) - { - warning (_f ("can't open file: `%s'", name_string) - + ": " + strerror (errno)); - return; - } - - struct stat file_stat; - fstat (fildes_, &file_stat); - size_off_ = file_stat.st_size; - map (); -} - -void -Mapped_file_storage::unmap () -{ - if (data_caddr_) - { -#ifdef __NeXT__ - kern_return_t r; - - r = vm_deallocate (task_self (), (vm_address_t) data_caddr_, -size_off_); - if (r != KERN_SUCCESS) - warning (String ("vm_deallocate: ") + mach_error_string (r)); -#else - munmap (data_caddr_, size_off_); -#endif - - data_caddr_ = 0; - size_off_ = 0; - } -} - -void -Mapped_file_storage::close () -{ - unmap (); - if (fildes_) - { - ::close (fildes_); - fildes_ = 0; - } -} - -int -Mapped_file_storage::length () const -{ - return size_off_; -} - -Mapped_file_storage::~Mapped_file_storage () -{ - close (); -} -#endif diff --git a/flower/simple-file-storage.cc b/flower/simple-file-storage.cc deleted file mode 100644 index 10fd7902ff..0000000000 --- a/flower/simple-file-storage.cc +++ /dev/null @@ -1,99 +0,0 @@ -/* - simple-file-storage.cc -- implement Simple_file_storage - - source file of the GNU LilyPond music typesetter - - (c) 1997--2000 Han-Wen Nienhuys -*/ - -#include - -#ifndef SEEK_END -#define SEEK_END 2 -#endif - -#include "simple-file-storage.hh" -#include "array.hh" -#include "string.hh" -#include "warn.hh" - -void -Simple_file_storage::load_stdin () -{ - len_ = 0; - - int c; - Array chs; - while ((c = fgetc (stdin)) != EOF) - chs.push (c); - len_ = chs.size (); - data_ = chs.remove_array (); -} - -void -Simple_file_storage::load_file (String s) -{ - /* - let's hope that "b" opens anything binary, and does not apply - CR/LF translation - */ - FILE * f = fopen (s.to_str0 (), "rb"); - - if (!f) - { - warning (_f ("can't open file: `%s'", s)); - return ; - } - - int ret = fseek (f, 0, SEEK_END); - len_ = ftell (f); - rewind (f); - data_ = new char[len_+1]; - data_[len_] = 0; - ret = fread (data_, sizeof (char), len_, f); - - if (ret!=len_) - warning (_f ("Huh? Got %d, expected %d characters", ret, len_)); - - fclose (f); -} - -/** - Stupid but foolproof way of opening files. - - TODO - Should check IO status - - This is of course a build it yourself version of mmap, so we should - have been using that..., but this is simple & portable - -*/ - -Simple_file_storage::Simple_file_storage (String s) -{ - data_ = 0; - len_ = 0; - - if ((s == "-")) - load_stdin (); - else - load_file (s); -} - -char const* -Simple_file_storage::to_str0 () const -{ - return data_; -} - -int -Simple_file_storage::length () const -{ - return len_; -} - - -Simple_file_storage::~Simple_file_storage () -{ - delete []data_; -} diff --git a/flower/source-file.cc b/flower/source-file.cc deleted file mode 100644 index a27e1f7780..0000000000 --- a/flower/source-file.cc +++ /dev/null @@ -1,249 +0,0 @@ -/* - source-file.cc -- implement Source_file - - source file of the GNU LilyPond music typesetter - - (c) 1997--2000 Jan Nieuwenhuizen - & Han-Wen Nienhuys -*/ - - -#include -#if HAVE_SSTREAM -#include -#else -#include -#define istringstream(x) istrstream(x, length ()) -#endif - -#include "string.hh" -#include "flower-proto.hh" -#include "warn.hh" -#include "source-file.hh" -#include "simple-file-storage.hh" -#include "string-storage.hh" - -Source_file::Source_file (String filename_string) -{ - name_string_ = filename_string; - istream_ = 0; - storage_ = new Simple_file_storage (filename_string); - pos_str0_ = to_str0 (); -} - -Source_file::Source_file (String name_string, String data_string) -{ - name_string_ = name_string; - istream_ = 0; - storage_ = new String_storage (data_string); - pos_str0_ = to_str0 (); -} - -std::istream* -Source_file::get_istream () -{ - /* - if (!name_string_.length ()) - return &cin; - */ - - if (!istream_) - { - if (length ()) // can-t this be done without such a hack? - istream_ = new std::istringstream (to_str0 ()); - else - { - istream_ = new std::istringstream (""); - istream_->setstate (std::ios::eofbit); - // istream_->set (ios::eofbit); - } - } - return istream_; -} - -String -Source_file::file_line_column_string (char const *context_str0) const -{ - if (!to_str0 ()) - return " (" + _ ("position unknown") + ")"; - else - return name_string () + ":" + to_string (get_line (context_str0)) - + ":" + to_string (get_char (context_str0)); -} - -String -Source_file::name_string () const -{ - return name_string_; -} - -Source_file::~Source_file () -{ - delete istream_; - istream_ = 0; - delete storage_; -} - -Slice -Source_file::line_slice (char const* pos_str0) const -{ - if (!in_b (pos_str0)) - return Slice (0,0); - - char const* data_str0 = to_str0 (); - char const * eof_C_ = data_str0 + length (); - - if (pos_str0 == eof_C_) - pos_str0 --; - char const* begin_str0 = pos_str0; - while (begin_str0 > data_str0) - if (*--begin_str0 == '\n') - { - begin_str0++; - break; - } - - char const* end_str0 = pos_str0; - while (end_str0 < eof_C_) - if (*end_str0++ == '\n') - { - end_str0--; - break; - } - - return Slice (begin_str0 - data_str0, end_str0 - data_str0); -} - -String -Source_file::line_string (char const* pos_str0) const -{ - if (!in_b (pos_str0)) - return ""; - - Slice line = line_slice (pos_str0); - char const* data_str0 = to_str0 (); - return String ((Byte const*)data_str0 + line[LEFT], line.length ()); -} - -int -Source_file::get_char (char const* pos_str0) const -{ - if (!in_b (pos_str0)) - return 0; - - char const* data_str0 = to_str0 (); - return pos_str0 - (line_slice (pos_str0)[SMALLER] + data_str0); -} - -int -Source_file::get_column (char const* pos_str0) const -{ - if (!in_b (pos_str0)) - return 0; - - int ch_i = get_char (pos_str0); - String line = line_string (pos_str0); - - int col_i = 0; - for (int i = 0; i < ch_i; i++) - if (line[i] == '\t') - col_i = (col_i / 8 + 1) * 8; - else - col_i++; - - return col_i; -} - -String -Source_file::error_string (char const* pos_str0) const -{ - if (!in_b (pos_str0)) - return " (" + _ ("position unknown") + ")"; - - int ch_i = get_char (pos_str0); - String line = line_string (pos_str0); - String context = line.left_string (ch_i) - + to_string ('\n') - + to_string (' ', get_column (pos_str0)) - + line.cut_string (ch_i, INT_MAX); - - return context; -} - -bool -Source_file::in_b (char const* pos_str0) const -{ - return (pos_str0 && (pos_str0 >= to_str0 ()) && (pos_str0 <= to_str0 () + length ())); -} - -int -Source_file::get_line (char const* pos_str0) const -{ - if (!in_b (pos_str0)) - return 0; - - int i = 1; - char const* scan_str0 = to_str0 (); - if (!scan_str0) - return 0; - - while (scan_str0 < pos_str0) - if (*scan_str0++ == '\n') - i++; - return i; -} - -int -Source_file::length () const -{ - return storage_->length (); -} - -char const * -Source_file::to_str0 () const -{ - return storage_->to_str0 (); -} - -void -Source_file::set_pos (char const * pos_str0) -{ - if (in_b (pos_str0)) - pos_str0_ = pos_str0; - else - error (error_string (pos_str0) + "invalid pos"); -} - -char const* -Source_file::seek_str0 (int n) -{ - char const* new_str0 = to_str0 () + n; - if (n < 0) - new_str0 += length (); - if (in_b (new_str0)) - pos_str0_ = new_str0; - else - error (error_string (new_str0) + "seek past eof"); - - return pos_str0_; -} - -char const* -Source_file::forward_str0 (int n) -{ - char const* old_pos = pos_str0_; - char const* new_str0 = pos_str0_ + n; - if (in_b (new_str0)) - pos_str0_ = new_str0; - else - error (error_string (new_str0) + "forward past eof"); - - return old_pos; -} - -String -Source_file::get_string (int n) -{ - String str = String ((Byte const*)forward_str0 (n), n); - return str; -} diff --git a/flower/source.cc b/flower/source.cc deleted file mode 100644 index cffc7989ab..0000000000 --- a/flower/source.cc +++ /dev/null @@ -1,86 +0,0 @@ -/* - source.cc -- implement Sources - - source file of the LilyPond music typesetter - - (c) 1997--2000 Han-Wen Nienhuys -*/ - - -#include -#include "killing-cons.tcc" -#include "binary-source-file.hh" -#include "string.hh" -#include "flower-proto.hh" -#include "source-file.hh" -#include "source.hh" -#include "file-path.hh" - -Sources::Sources () -{ - sourcefile_p_list_ = 0; - path_C_= 0; - binary_b_ = false; -} - -void -Sources::set_binary (bool bo) -{ - binary_b_ = bo; -} - -void -Sources::set_path (File_path *f) -{ - path_C_ = f; -} - -/** - open a file - - @param file_string the file to be opened, name might be changed if it - is found in a search path. UGH! - - @return 0 if no file found - */ -Source_file* -Sources::get_file (String &file_string) //UGH -{ - if ((file_string != "-") && path_C_) - { - String file_string_o = path_C_->find (file_string); - if ((file_string_o == "") && (file_string != "")) - return 0; - file_string = file_string_o; - } - Source_file * f = (!binary_b_) ? - new Source_file (file_string) : new Binary_source_file (file_string); - add (f); - return f; -} - -void -Sources::add (Source_file* sourcefile) -{ - sourcefile_p_list_ = new Killing_cons (sourcefile, sourcefile_p_list_); -} - -Sources::~Sources () -{ - delete sourcefile_p_list_; -} -/** - search the list for file whose map contains pointer #str0# - - @return 0 if not found. - */ -Source_file* -Sources::get_sourcefile (char const* str0) -{ - - for (Cons *i = sourcefile_p_list_; i; i = i->next_) - if (i->car_->in_b (str0)) - return i->car_; - return 0; -} - diff --git a/lily/binary-source-file.cc b/lily/binary-source-file.cc new file mode 100644 index 0000000000..ab1c08488d --- /dev/null +++ b/lily/binary-source-file.cc @@ -0,0 +1,98 @@ +/* + binary-source-file.cc -- implement Binary_source_file + + source file of the LilyPond music typesetter + + (c) 1997--2000 Jan Nieuwenhuizen +*/ + + +#include // INT_MAX +#include + +#include "flower-proto.hh" +#include "string.hh" +#include "source-file.hh" +#include "binary-source-file.hh" +#include "string-convert.hh" + +Binary_source_file::Binary_source_file (String& filename_string) + : Source_file (filename_string) +{ +} + +Binary_source_file::~Binary_source_file () +{ +} + +String +Binary_source_file::error_string (char const* pos_str0) const +{ + assert (this); + if (!in_b (pos_str0)) + return ""; + + char const* begin_str0 = pos_str0 - 8 >? to_str0 (); + char const* end_str0 = pos_str0 + 7 get_istream (), YY_BUF_SIZE)); } + void Includable_lexer::new_input (String name, String data, Sources* sources) { diff --git a/lily/include/binary-source-file.hh b/lily/include/binary-source-file.hh new file mode 100644 index 0000000000..639e05ca1b --- /dev/null +++ b/lily/include/binary-source-file.hh @@ -0,0 +1,27 @@ +// +// binary-source-file.hh -- declare Binary_source_file +// +// copyright 1997 Jan Nieuwenhuizen + +#ifndef BINARY_SOURCE_FILE_HH +#define BINARY_SOURCE_FILE_HH + +#include "source-file.hh" + +class Binary_source_file : public Source_file +{ +public: + Binary_source_file (String& filename_string ); + virtual ~Binary_source_file (); + + U8 get_U8 (); + U16 get_U16 (); + U32 get_U32 (); + Byte get_Byte () {return get_U8 (); } + int get_int () { return get_U32 (); } + + virtual String error_string (char const* pos_str0 ) const; + virtual int get_line (char const* pos_str0 ) const; +}; + +#endif // BINARY_SOURCE_FILE_HH diff --git a/lily/include/file-results.hh b/lily/include/file-results.hh deleted file mode 100644 index 669eefe99e..0000000000 --- a/lily/include/file-results.hh +++ /dev/null @@ -1,45 +0,0 @@ -/* - file-results.hh -- declare functions for processing one input file. - - source file of the GNU LilyPond music typesetter - - (c) 1998--2002 Han-Wen Nienhuys - - */ - -#ifndef FILE_RESULTS_HH -#define FILE_RESULTS_HH - -#include "lily-proto.hh" -#include "source.hh" -#include "parray.hh" -#include "scm-hash.hh" - -class Input_file_settings -{ -public: - Sources sources_; - Array inclusion_names_; - Array target_strings_; - Link_array scores_; - /* Global? prefix is bit confusing */ - Scheme_hash_table * global_header_; - - void do_deps (); - void do_scores (); - - Input_file_settings (String file, String init); - ~Input_file_settings (); - -private: - /* Make sure we never get an implicit constructor.*/ - Input_file_settings (); -}; - -extern Input_file_settings* global_input_file; - -void do_one_file (String init_string, String file_string); - - -#endif /* FILE_RESULTS_HH */ - diff --git a/lily/include/input-file-results.hh b/lily/include/input-file-results.hh new file mode 100644 index 0000000000..8b6e31582c --- /dev/null +++ b/lily/include/input-file-results.hh @@ -0,0 +1,45 @@ +/* + file-results.hh -- declare functions for processing one input file. + + source file of the GNU LilyPond music typesetter + + (c) 1998--2002 Han-Wen Nienhuys + + */ + +#ifndef FILE_RESULTS_HH +#define FILE_RESULTS_HH + +#include "lily-proto.hh" +#include "source.hh" +#include "parray.hh" +#include "scm-hash.hh" + +class Input_file_results +{ +public: + Sources sources_; + Array inclusion_names_; + Array target_strings_; + Link_array scores_; + /* Global? prefix is bit confusing */ + Scheme_hash_table * header_; + + void do_deps (); + void do_scores (); + + Input_file_results (String file, String init); + ~Input_file_results (); + +private: + /* Make sure we never get an implicit constructor.*/ + Input_file_results (); +}; + +extern Input_file_results* global_input_file; + +void do_one_file (String init_string, String file_string); + + +#endif /* FILE_RESULTS_HH */ + diff --git a/lily/include/input.hh b/lily/include/input.hh new file mode 100644 index 0000000000..7dc33b2df1 --- /dev/null +++ b/lily/include/input.hh @@ -0,0 +1,42 @@ +/* + input.hh -- declare Input + + source file of the LilyPond music typesetter + + (c) 1997--2000 Han-Wen Nienhuys +*/ + + +#ifndef INPUT_HH +#define INPUT_HH + +#include "flower-proto.hh" + +/** + Base class for anything that records its poisition in the parse file. + */ +class Input { +public: + char const *defined_str0_ ; + Source_file * source_file_; + + void warning (String) const; // should use member func? + void non_fatal_error (String) const; + void error (String) const; + void message (String) const; + void set_spot (Input const &); + Input spot () const; + String location_string () const; + String line_number_string () const; + + + String file_string ()const; + int line_number ()const; + int column_number ()const; + + + Input (Source_file*, char const*); + Input (); +}; + +#endif // INPUT_HH diff --git a/lily/include/lily-proto.hh b/lily/include/lily-proto.hh index e09de5f1f6..e02c5120ab 100644 --- a/lily/include/lily-proto.hh +++ b/lily/include/lily-proto.hh @@ -68,7 +68,7 @@ class Hara_kiri_line_group_engraver; class Hyphen_req; class Includable_lexer; -class Input_file_settings; +class Input_file_results; class Input; class Item; class Key_change_req; diff --git a/lily/include/my-lily-parser.hh b/lily/include/my-lily-parser.hh index 38d39ac4c3..d71a7832f1 100644 --- a/lily/include/my-lily-parser.hh +++ b/lily/include/my-lily-parser.hh @@ -30,7 +30,7 @@ class My_lily_parser { public: - My_lily_parser (Input_file_settings * sources); + My_lily_parser (Input_file_results * sources); ~My_lily_parser (); void do_init_file (); @@ -42,7 +42,7 @@ public: Scheme_hash_table *default_header_; int fatal_error_; - Input_file_settings * input_file_; + Input_file_results * input_file_; int error_level_; My_lily_lexer * lexer_; diff --git a/lily/include/parse-scm.hh b/lily/include/parse-scm.hh index 9a33271b4e..3a51130e0a 100644 --- a/lily/include/parse-scm.hh +++ b/lily/include/parse-scm.hh @@ -9,7 +9,7 @@ struct Parse_start { char const* str; int nchars; - Input start_location; + Input start_location_; }; SCM catch_protected_parse_body (void *); diff --git a/lily/include/score.hh b/lily/include/score.hh index c401c57f73..2af42321e5 100644 --- a/lily/include/score.hh +++ b/lily/include/score.hh @@ -23,7 +23,7 @@ public: Link_array defs_; SCM music_; Scheme_hash_table * header_; - Input_file_settings* input_file_; + Input_file_results* input_file_; int errorlevel_; diff --git a/lily/include/source-file.hh b/lily/include/source-file.hh new file mode 100644 index 0000000000..b587a594fd --- /dev/null +++ b/lily/include/source-file.hh @@ -0,0 +1,73 @@ + +#ifndef SOURCE_FILE_HH +#define SOURCE_FILE_HH + +#include + +#include "flower-proto.hh" +#include "string.hh" +#include "interval.hh" +#include "protected-scm.hh" + +/** + class for reading and mapping a file. + + duplicates a lot of Data_file and Text_stream. + should look at including Data_file's functionality: + get_line (), get_word () here. +*/ + +class Source_file +{ +public: + Source_file (String fn); + Source_file (String, String ); + + virtual ~Source_file (); + + char const* to_str0 () const; + virtual String error_string (char const* pos_str0 ) const; + std::istream * get_istream (); + bool in_b (char const* pos_str0 ) const; + int length () const; + virtual int get_line (char const* pos_str0 ) const; + String name_string () const; + String file_line_column_string (char const* str0 ) const; + + // return start + n + char const* seek_str0 (int n); + + int tell () const; + // return here + n bytes + char const* forward_str0 (int n); + char const* pos_str0 () { return pos_str0_; } + String get_string (int n); + void set_pos (char const * pos_str0); +public: + Slice line_slice (char const* pos_str0) const; + String line_string (char const* pos_str0) const; + int get_column (char const* pos_str0) const; + int get_char (char const* pos_str0) const; + + /* + DOCUMENT-ME + */ + char const* pos_str0_; + + SCM get_port()const { return str_port_; } +private: + String name_string_; + std::istream* istream_; + char * contents_str0_; + int length_; + void load_stdin (); + void init_port (); + + Protected_scm str_port_; + +}; + +char * gulp_file (String fn, int *len); + +#endif // SOURCE_FILE_HH // + diff --git a/lily/include/source.hh b/lily/include/source.hh new file mode 100644 index 0000000000..82b557b111 --- /dev/null +++ b/lily/include/source.hh @@ -0,0 +1,37 @@ +// +// source.hh -- part of LilyPond +// +// copyright 1997 Jan Nieuwenhuizen + +#ifndef SOURCE_HH +#define SOURCE_HH +#include "cons.hh" +#include "flower-proto.hh" + +/** + a set of sourcefiles. + + TODO: + */ +class Sources +{ + Sources (Sources const&) {} +public: + Sources (); + ~Sources (); + + Source_file * get_file (String &filename ); + Source_file* get_sourcefile (char const* str0 ); + void add (Source_file* sourcefile ); + void set_path (File_path*p); + void set_binary (bool); + + const File_path * path_C_; +private: + Cons *sourcefile_p_list_; + bool binary_b_ ; +}; + + + +#endif // SOURCE_HH // diff --git a/lily/input-file-results.cc b/lily/input-file-results.cc new file mode 100644 index 0000000000..beb789209b --- /dev/null +++ b/lily/input-file-results.cc @@ -0,0 +1,198 @@ +/* + scores.cc -- implement some globals + + source file of the GNU LilyPond music typesetter + + (c) 1997--2002 Han-Wen Nienhuys +*/ +#include "config.h" + +#include +#include +#include + + +#if HAVE_SYS_STAT_H +#include +#endif +#include + + +#include "main.hh" +#include "score.hh" +#include "string.hh" +#include "paper-def.hh" +#include "warn.hh" +#include "parray.hh" +#include "file-path.hh" +#include "input-file-results.hh" +#include "my-lily-parser.hh" +#include "source.hh" +#include "lily-version.hh" +#include "scm-hash.hh" + + +LY_DEFINE(ly_set_point_and_click_x, "set-point-and-click!", 1, 0, 0, + (SCM what), + "Set the options for Point-and-click source specials output. The +argument is a symbol. Possible options are @code{none} (no source specials), +@code{line} and @code{line-column}") +{ + /* + UGH. + */ + SCM val = SCM_BOOL_F; + if (ly_symbol2scm ("line-column") == what) + val = gh_eval_str ("line-column-location"); + else if (what == ly_symbol2scm ("line")) + val = gh_eval_str ("line-location"); + + /* + UGH. + + How do you do set! from C ? + */ + scm_primitive_eval (scm_list_n (ly_symbol2scm ("set!"), + ly_symbol2scm ("point-and-click"), val, SCM_UNDEFINED)); + return SCM_UNSPECIFIED; +} + +void +write_dependency_file (String fn, + Array targets, + Array deps) +{ + const int WRAPWIDTH = 65; + + progress_indication (_f ("dependencies output to `%s'...", fn.to_str0 ())); + progress_indication ("\n"); + FILE * f = fopen (fn.to_str0 (), "w"); + if (!f) + warning (_f ("can't open file: `%s'", fn)); + + fprintf (f, "# Generated automatically by: %s\n", gnu_lilypond_version_string ().to_str0 ()); + + String out; + for (int i=0; i < targets.size (); i ++) + out += dependency_prefix_global + targets[i] + " "; + out += ": "; + + for (int i=0; i < deps.size (); i ++) + { + if (out.length () > WRAPWIDTH) + { + fprintf (f, "%s\\\n", out.to_str0 ()); + out = " "; + } + String dep = deps[i]; + if (!dependency_prefix_global.empty_b ()) + { +#if 0//thinko? + if (stat (dep.to_str0 (), &stat_buf) == -1 && errno == ENOENT) + ; //make emacs happy +#else + if (dep.index ('/') < 0) +#endif + dep = dependency_prefix_global + dep; + } + out += " " + dep; + } + fprintf (f, "%s\n", out.to_str0 ()); +} + + +void +Input_file_results::do_deps () +{ + if (dependency_global_b) + { + Path p = split_path (output_name_global); + p.ext = "dep"; + write_dependency_file (p.string (), + target_strings_, + inclusion_names_); + } +} + + +void +Input_file_results::do_scores () +{ + if (!header_) + header_ = new Scheme_hash_table; + + for (int i=0; i < scores_.size (); i++) + { + Score* is = scores_[i]; + is->input_file_ = this; + + if (is->errorlevel_) + { + is->warning (_ ("Score contains errors; will not process it")); + exit_status_global |= 1; + } + else + { + is->process (); + } + } + do_deps (); +} + +Input_file_results::~Input_file_results () +{ + for (int i=0; i < scores_.size (); i++) + scm_gc_unprotect_object (scores_[i]->self_scm ()); + scores_.clear (); + + inclusion_names_.clear (); + if (header_) + scm_gc_unprotect_object (header_ ->self_scm ()); + + global_input_file =0; +} + + +Input_file_results* global_input_file; + +Input_file_results::Input_file_results (String init_string, String file_string) +{ + header_ = 0; + global_input_file = this; + ly_set_point_and_click_x (SCM_BOOL_F); + + sources_.set_path (&global_path); + + My_lily_parser parser (this); + + progress_indication (_f ("Now processing: `%s'", file_string.to_str0 ())); + progress_indication ("\n"); + parser.parse_file (init_string, file_string); + + if (parser.error_level_) + { + exit_status_global = 1; + } + else + do_scores (); + +} + + +void +do_one_file (String init_string, String file_string) +{ + if (init_string.length () && global_path.find (init_string).empty_b ()) + { + warning (_f ("can't find file: `%s'", init_string)); + warning (_f ("(search path: `%s')", global_path.string ().to_str0 ())); + return; + } + if ((file_string != "-") && global_path.find (file_string).empty_b ()) + { + warning (_f ("can't find file: `%s'", file_string)); + return; + } + + Input_file_results inp_file(init_string, file_string); +} diff --git a/lily/input.cc b/lily/input.cc new file mode 100644 index 0000000000..381907d635 --- /dev/null +++ b/lily/input.cc @@ -0,0 +1,146 @@ +/* + input.cc -- implement Input + + source file of the LilyPond music typesetter + + (c) 1997--2000 Han-Wen Nienhuys +*/ + +#include + +#include "flower-proto.hh" +#include "input.hh" +#include "string.hh" +#include "source.hh" +#include "source-file.hh" + +Input::Input (Source_file*s, char const *cl) +{ + source_file_=s; + defined_str0_=cl; +} + +Input::Input () +{ + source_file_ = 0; + defined_str0_ = 0; +} + +Input +Input::spot () const +{ + return *this; +} + +void +Input::set_spot (Input const &i) +{ + *this = i; +} + +/* + Produce GNU-compliant error message. Correcting lilypond source is + such a breeze if you ('re edidor) know (s) the error column too + + Format: + + [file:line:column:][warning:]message + + */ +void +Input::message (String message_string) const +{ + String str; + + /* + marked "Work in prgress" in GNU iostream + libg++ 2.7.2.8 + libstdc++ 2.8.1 + + why not just return always -1 (unknown), + iso breaking the interface? + + int col = cerr.rdbuf ()->column (); + + */ + + // well, we don't want to loose first warning... + int col = 1; + if (col > 0) + str += "\n"; + + if (source_file_) + str += location_string () + String (": "); + + str += message_string; + if (source_file_) + { + str += ":\n"; + str += source_file_->error_string (defined_str0_); + } + fprintf (stderr, "%s\n", str.to_str0 ()); +} + +void +Input::warning (String message_string) const +{ + message (_ ("warning: ") + message_string); +} +void +Input::error (String s) const +{ + message (_ ("error: ")+ s); +} + +void +Input::non_fatal_error (String s) const +{ + message (_ ("non fatal error: ") + s); +} +String +Input::location_string () const +{ + if (source_file_) + return source_file_->file_line_column_string (defined_str0_); + else + return " (" + _ ("position unknown") + ")"; +} + +String +Input::line_number_string () const +{ + if (source_file_) + return to_string (source_file_->get_line (defined_str0_)); + else + return "?"; +} + +String +Input::file_string () const +{ + if (source_file_) + return source_file_->name_string (); + else + return ""; +} + + +int +Input::line_number () const +{ + if (source_file_) + return source_file_->get_line (defined_str0_); + else + return 0; + +} + +int +Input::column_number () const +{ + if (source_file_) + return source_file_->get_column (defined_str0_); + else + return 0; + +} diff --git a/lily/lexer.ll b/lily/lexer.ll index e02376b983..d9b61340f6 100644 --- a/lily/lexer.ll +++ b/lily/lexer.ll @@ -34,7 +34,7 @@ #include "string.hh" #include "string-convert.hh" #include "my-lily-lexer.hh" -#include "file-results.hh" +#include "input-file-results.hh" #include "interval.hh" #include "lily-guile.hh" #include "parser.hh" diff --git a/lily/lily-guile.cc b/lily/lily-guile.cc index dc4886681c..6b96a31315 100644 --- a/lily/lily-guile.cc +++ b/lily/lily-guile.cc @@ -16,7 +16,6 @@ #include "libc-extension.hh" #include "lily-guile.hh" #include "main.hh" -#include "simple-file-storage.hh" #include "file-path.hh" #include "warn.hh" #include "direction.hh" @@ -24,7 +23,7 @@ #include "interval.hh" #include "pitch.hh" #include "dimensions.hh" - +#include "source-file.hh" // #define TEST_GC @@ -104,10 +103,14 @@ gulp_file_to_string (String fn) progress_indication ("[" + s); - Simple_file_storage f (s); - String result (f.to_str0 ()); + int n; + char * str = gulp_file (s, &n); + String result (str); + delete str; + if (verbose_global_b) progress_indication ("]"); + return result; } diff --git a/lily/main.cc b/lily/main.cc index 1f992b0039..6bf6435889 100644 --- a/lily/main.cc +++ b/lily/main.cc @@ -26,7 +26,7 @@ #include "string.hh" #include "main.hh" #include "file-path.hh" -#include "file-results.hh" +#include "input-file-results.hh" #include "warn.hh" #include "lily-guile.hh" #include "paper-def.hh" diff --git a/lily/my-lily-lexer.cc b/lily/my-lily-lexer.cc index 121d676de1..cff5ae13e6 100644 --- a/lily/my-lily-lexer.cc +++ b/lily/my-lily-lexer.cc @@ -13,7 +13,7 @@ #include "lily-proto.hh" #include "scm-hash.hh" #include "interval.hh" -#include "file-results.hh" +#include "input-file-results.hh" #include "lily-guile.hh" #include "parser.hh" #include "keyword.hh" diff --git a/lily/my-lily-parser.cc b/lily/my-lily-parser.cc index ee06d6bf3f..f935f0f778 100644 --- a/lily/my-lily-parser.cc +++ b/lily/my-lily-parser.cc @@ -12,10 +12,10 @@ #include "warn.hh" #include "main.hh" #include "parser.hh" -#include "file-results.hh" +#include "input-file-results.hh" #include "scm-hash.hh" -My_lily_parser::My_lily_parser (Input_file_settings * source) +My_lily_parser::My_lily_parser (Input_file_results * source) { input_file_ = source; lexer_ = 0; diff --git a/lily/paper-def.cc b/lily/paper-def.cc index 03a4b322f8..e00444cf5d 100644 --- a/lily/paper-def.cc +++ b/lily/paper-def.cc @@ -16,7 +16,7 @@ #include "scaled-font-metric.hh" #include "main.hh" #include "scm-hash.hh" -#include "file-results.hh" // urg? header_global +#include "input-file-results.hh" // urg? header_global #include "paper-outputter.hh" /* diff --git a/lily/paper-outputter.cc b/lily/paper-outputter.cc index 74cd96acc5..2177afffcd 100644 --- a/lily/paper-outputter.cc +++ b/lily/paper-outputter.cc @@ -22,7 +22,7 @@ #include "scm-hash.hh" #include "lily-version.hh" #include "paper-def.hh" -#include "file-results.hh" +#include "input-file-results.hh" /* diff --git a/lily/paper-score.cc b/lily/paper-score.cc index eb30b41a81..bf7ce0e0b5 100644 --- a/lily/paper-score.cc +++ b/lily/paper-score.cc @@ -19,7 +19,7 @@ #include "scm-hash.hh" #include "gourlay-breaking.hh" #include "paper-outputter.hh" -#include "file-results.hh" +#include "input-file-results.hh" #include "misc.hh" #include "all-font-metrics.hh" @@ -98,10 +98,10 @@ Paper_score::process () progress_indication ("\n"); - if (global_input_file->global_header_) + if (global_input_file->header_) { - outputter_->output_scope (global_input_file->global_header_, "lilypond"); - outputter_->write_header_fields_to_file (global_input_file->global_header_); + outputter_->output_scope (global_input_file->header_, "lilypond"); + outputter_->write_header_fields_to_file (global_input_file->header_); } if (header_) diff --git a/lily/parse-scm.cc b/lily/parse-scm.cc index 7c50e772fc..9c49be4b5e 100644 --- a/lily/parse-scm.cc +++ b/lily/parse-scm.cc @@ -1,8 +1,9 @@ +#include #include "lily-guile.hh" #include "parse-scm.hh" #include "string.hh" - +#include "source-file.hh" /* Pass string to scm parser, evaluate one expression. @@ -15,22 +16,13 @@ SCM internal_ly_parse_scm (Parse_start * ps) { - /* - - This is actually pretty wasteful: we stuff the rest of the entire - file down GUILE, while we usually need only a bit of it. + Source_file* sf =ps->start_location_.source_file_; + SCM port = sf->get_port(); - TODO: implement read_only_string_port(), (preferably in GUILE ?) - - */ - SCM str = scm_makfrom0str (ps->str); - SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, - "ly_eval_scm_0str"); - - scm_set_port_filename_x (port, scm_makfrom0str (ps->start_location.file_string ().get_str0())); - scm_set_port_line_x (port, gh_int2scm (ps->start_location.line_number ())); - scm_set_port_column_x (port, gh_int2scm (ps->start_location.column_number())); + int off = ps->start_location_.defined_str0_ - sf->to_str0(); + + scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET)); SCM from = scm_ftell (port); SCM form; @@ -85,7 +77,7 @@ parse_handler (void * data, SCM tag, SCM args) { Parse_start* ps = (Parse_start*) data; - ps->start_location.error (_("GUILE signaled an error for the expression begining here")); + ps->start_location_.error (_("GUILE signaled an error for the expression begining here")); if (scm_ilength (args) > 2) scm_display_error_message (gh_cadr (args), gh_caddr(args), scm_current_error_port()); @@ -127,7 +119,7 @@ ly_parse_scm (char const* s, int *n, Input i) { Parse_start ps ; ps.str = s; - ps.start_location = i; + ps.start_location_ = i; SCM ans = protected_ly_parse_scm (&ps); *n = ps.nchars; diff --git a/lily/parser.yy b/lily/parser.yy index 9c1cee4c17..c314a96b17 100644 --- a/lily/parser.yy +++ b/lily/parser.yy @@ -44,7 +44,7 @@ this. #include "music-list.hh" #include "output-property-music-iterator.hh" #include "property-iterator.hh" -#include "file-results.hh" +#include "input-file-results.hh" #include "input.hh" #include "relative-music.hh" #include "lyric-combine-music.hh" @@ -349,9 +349,9 @@ toplevel_expression: THIS->lexer_->chordmodifier_tab_ = $1; } | lilypond_header { - if (THIS->input_file_->global_header_) - scm_gc_unprotect_object (THIS->input_file_->global_header_->self_scm ()); - THIS->input_file_->global_header_ = $1; + if (THIS->input_file_->header_) + scm_gc_unprotect_object (THIS->input_file_->header_->self_scm ()); + THIS->input_file_->header_ = $1; } | score_block { THIS->input_file_->scores_.push ($1); diff --git a/lily/performance.cc b/lily/performance.cc index f47aaa78d2..95268da54f 100644 --- a/lily/performance.cc +++ b/lily/performance.cc @@ -19,7 +19,7 @@ #include "audio-staff.hh" #include "performance.hh" #include "score.hh" -#include "file-results.hh" +#include "input-file-results.hh" #include "file-path.hh" #include "lily-version.hh" diff --git a/lily/scores.cc b/lily/scores.cc deleted file mode 100644 index 44bc253fce..0000000000 --- a/lily/scores.cc +++ /dev/null @@ -1,198 +0,0 @@ -/* - scores.cc -- implement some globals - - source file of the GNU LilyPond music typesetter - - (c) 1997--2002 Han-Wen Nienhuys -*/ -#include "config.h" - -#include -#include -#include - - -#if HAVE_SYS_STAT_H -#include -#endif -#include - - -#include "main.hh" -#include "score.hh" -#include "string.hh" -#include "paper-def.hh" -#include "warn.hh" -#include "parray.hh" -#include "file-path.hh" -#include "file-results.hh" -#include "my-lily-parser.hh" -#include "source.hh" -#include "lily-version.hh" -#include "scm-hash.hh" - - -LY_DEFINE(ly_set_point_and_click_x, "set-point-and-click!", 1, 0, 0, - (SCM what), - "Set the options for Point-and-click source specials output. The -argument is a symbol. Possible options are @code{none} (no source specials), -@code{line} and @code{line-column}") -{ - /* - UGH. - */ - SCM val = SCM_BOOL_F; - if (ly_symbol2scm ("line-column") == what) - val = gh_eval_str ("line-column-location"); - else if (what == ly_symbol2scm ("line")) - val = gh_eval_str ("line-location"); - - /* - UGH. - - How do you do set! from C ? - */ - scm_primitive_eval (scm_list_n (ly_symbol2scm ("set!"), - ly_symbol2scm ("point-and-click"), val, SCM_UNDEFINED)); - return SCM_UNSPECIFIED; -} - -void -write_dependency_file (String fn, - Array targets, - Array deps) -{ - const int WRAPWIDTH = 65; - - progress_indication (_f ("dependencies output to `%s'...", fn.to_str0 ())); - progress_indication ("\n"); - FILE * f = fopen (fn.to_str0 (), "w"); - if (!f) - warning (_f ("can't open file: `%s'", fn)); - - fprintf (f, "# Generated automatically by: %s\n", gnu_lilypond_version_string ().to_str0 ()); - - String out; - for (int i=0; i < targets.size (); i ++) - out += dependency_prefix_global + targets[i] + " "; - out += ": "; - - for (int i=0; i < deps.size (); i ++) - { - if (out.length () > WRAPWIDTH) - { - fprintf (f, "%s\\\n", out.to_str0 ()); - out = " "; - } - String dep = deps[i]; - if (!dependency_prefix_global.empty_b ()) - { -#if 0//thinko? - if (stat (dep.to_str0 (), &stat_buf) == -1 && errno == ENOENT) - ; //make emacs happy -#else - if (dep.index ('/') < 0) -#endif - dep = dependency_prefix_global + dep; - } - out += " " + dep; - } - fprintf (f, "%s\n", out.to_str0 ()); -} - - -void -Input_file_settings::do_deps () -{ - if (dependency_global_b) - { - Path p = split_path (output_name_global); - p.ext = "dep"; - write_dependency_file (p.string (), - target_strings_, - inclusion_names_); - } -} - - -void -Input_file_settings::do_scores () -{ - if (!global_header_) - global_header_ = new Scheme_hash_table; - - for (int i=0; i < scores_.size (); i++) - { - Score* is = scores_[i]; - is->input_file_ = this; - - if (is->errorlevel_) - { - is->warning (_ ("Score contains errors; will not process it")); - exit_status_global |= 1; - } - else - { - is->process (); - } - } - do_deps (); -} - -Input_file_settings::~Input_file_settings () -{ - for (int i=0; i < scores_.size (); i++) - scm_gc_unprotect_object (scores_[i]->self_scm ()); - scores_.clear (); - - inclusion_names_.clear (); - if (global_header_) - scm_gc_unprotect_object (global_header_ ->self_scm ()); - - global_input_file =0; -} - - -Input_file_settings* global_input_file; - -Input_file_settings::Input_file_settings (String init_string, String file_string) -{ - global_header_ = 0; - global_input_file = this; - ly_set_point_and_click_x (SCM_BOOL_F); - - sources_.set_path (&global_path); - - My_lily_parser parser (this); - - progress_indication (_f ("Now processing: `%s'", file_string.to_str0 ())); - progress_indication ("\n"); - parser.parse_file (init_string, file_string); - - if (parser.error_level_) - { - exit_status_global = 1; - } - else - do_scores (); - -} - - -void -do_one_file (String init_string, String file_string) -{ - if (init_string.length () && global_path.find (init_string).empty_b ()) - { - warning (_f ("can't find file: `%s'", init_string)); - warning (_f ("(search path: `%s')", global_path.string ().to_str0 ())); - return; - } - if ((file_string != "-") && global_path.find (file_string).empty_b ()) - { - warning (_f ("can't find file: `%s'", file_string)); - return; - } - - Input_file_settings inp_file(init_string, file_string); -} diff --git a/lily/source-file.cc b/lily/source-file.cc new file mode 100644 index 0000000000..cbe4967b52 --- /dev/null +++ b/lily/source-file.cc @@ -0,0 +1,328 @@ +/* + source-file.cc -- implement Source_file + + source file of the GNU LilyPond music typesetter + + (c) 1997--2000 Jan Nieuwenhuizen + & Han-Wen Nienhuys +*/ + +#include +#include +#if HAVE_SSTREAM +#include +#else +#include +#define istringstream(x) istrstream(x, length ()) +#endif + +#include "string.hh" +#include "flower-proto.hh" +#include "warn.hh" +#include "source-file.hh" +#include "array.hh" + +void +Source_file::load_stdin () +{ + length_ = 0; + + int c; + Array chs; // ugh. + while ((c = fgetc (stdin)) != EOF) + chs.push (c); + + length_ = chs.size (); + contents_str0_ = chs.remove_array (); +} + + + +char * +gulp_file (String fn, int* len) +{ + /* + let's hope that "b" opens anything binary, and does not apply + CR/LF translation + */ + FILE * f = fopen (fn.to_str0 (), "rb"); + + if (!f) + { + warning (_f ("can't open file: `%s'", fn.to_str0 ())); + return 0; + } + + int ret = fseek (f, 0, SEEK_END); + + *len = ftell (f); + rewind (f); + char * str = new char[*len+1]; + str[*len] = 0; + ret = fread (str, sizeof (char), *len, f); + + if (ret!=*len) + warning (_f ("Huh? Got %d, expected %d characters", ret, *len)); + + fclose (f); + + + return str; +} + + + +Source_file::Source_file (String filename, String data) +{ + name_string_ = ""; + istream_ = 0; + contents_str0_ = data.get_copy_str0(); + length_ = data.length(); + pos_str0_ = to_str0 (); + init_port(); +} + + + + +Source_file::Source_file (String filename_string) +{ + name_string_ = filename_string; + istream_ = 0; + contents_str0_ = 0; + + if (filename_string == "-") + load_stdin (); + else + contents_str0_ = gulp_file (filename_string, &length_); + + pos_str0_ = to_str0 (); + + init_port(); +} + +void +Source_file::init_port () +{ + SCM str =scm_makfrom0str (contents_str0_); + + str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, + __FUNCTION__); + scm_set_port_filename_x (str_port_, + scm_makfrom0str (name_string_.get_str0())); +} + +int +Source_file::tell () const +{ + return pos_str0_ - contents_str0_; +} + +std::istream* +Source_file::get_istream () +{ + /* + if (!name_string_.length ()) + return &cin; + */ + + if (!istream_) + { + if (length ()) // can-t this be done without such a hack? + istream_ = new std::istringstream (to_str0 ()); + else + { + istream_ = new std::istringstream (""); + istream_->setstate (std::ios::eofbit); + // istream_->set (ios::eofbit); + } + } + return istream_; +} + +String +Source_file::file_line_column_string (char const *context_str0) const +{ + if (!to_str0 ()) + return " (" + _ ("position unknown") + ")"; + else + return name_string () + ":" + to_string (get_line (context_str0)) + + ":" + to_string (get_char (context_str0)); +} + +String +Source_file::name_string () const +{ + return name_string_; +} + +Source_file::~Source_file () +{ + delete istream_; + istream_ = 0; + delete contents_str0_; +} + +Slice +Source_file::line_slice (char const* pos_str0) const +{ + if (!in_b (pos_str0)) + return Slice (0,0); + + char const* data_str0 = to_str0 (); + char const * eof_C_ = data_str0 + length (); + + if (pos_str0 == eof_C_) + pos_str0 --; + char const* begin_str0 = pos_str0; + while (begin_str0 > data_str0) + if (*--begin_str0 == '\n') + { + begin_str0++; + break; + } + + char const* end_str0 = pos_str0; + while (end_str0 < eof_C_) + if (*end_str0++ == '\n') + { + end_str0--; + break; + } + + return Slice (begin_str0 - data_str0, end_str0 - data_str0); +} + +String +Source_file::line_string (char const* pos_str0) const +{ + if (!in_b (pos_str0)) + return ""; + + Slice line = line_slice (pos_str0); + char const* data_str0 = to_str0 (); + return String ((Byte const*)data_str0 + line[LEFT], line.length ()); +} + +int +Source_file::get_char (char const* pos_str0) const +{ + if (!in_b (pos_str0)) + return 0; + + char const* data_str0 = to_str0 (); + return pos_str0 - (line_slice (pos_str0)[SMALLER] + data_str0); +} + +int +Source_file::get_column (char const* pos_str0) const +{ + if (!in_b (pos_str0)) + return 0; + + int ch_i = get_char (pos_str0); + String line = line_string (pos_str0); + + int col_i = 0; + for (int i = 0; i < ch_i; i++) + if (line[i] == '\t') + col_i = (col_i / 8 + 1) * 8; + else + col_i++; + + return col_i; +} + +String +Source_file::error_string (char const* pos_str0) const +{ + if (!in_b (pos_str0)) + return " (" + _ ("position unknown") + ")"; + + int ch_i = get_char (pos_str0); + String line = line_string (pos_str0); + String context = line.left_string (ch_i) + + to_string ('\n') + + to_string (' ', get_column (pos_str0)) + + line.cut_string (ch_i, INT_MAX); + + return context; +} + +bool +Source_file::in_b (char const* pos_str0) const +{ + return (pos_str0 && (pos_str0 >= to_str0 ()) && (pos_str0 <= to_str0 () + length ())); +} + +int +Source_file::get_line (char const* pos_str0) const +{ + if (!in_b (pos_str0)) + return 0; + + int i = 1; + char const* scan_str0 = to_str0 (); + if (!scan_str0) + return 0; + + while (scan_str0 < pos_str0) + if (*scan_str0++ == '\n') + i++; + return i; +} + +int +Source_file::length () const +{ + return length_; +} + +char const * +Source_file::to_str0 () const +{ + return contents_str0_; +} + +void +Source_file::set_pos (char const * pos_str0) +{ + if (in_b (pos_str0)) + pos_str0_ = pos_str0; + else + error (error_string (pos_str0) + "invalid pos"); +} + +char const* +Source_file::seek_str0 (int n) +{ + char const* new_str0 = to_str0 () + n; + if (n < 0) + new_str0 += length (); + if (in_b (new_str0)) + pos_str0_ = new_str0; + else + error (error_string (new_str0) + "seek past eof"); + + return pos_str0_; +} + +char const* +Source_file::forward_str0 (int n) +{ + char const* old_pos = pos_str0_; + char const* new_str0 = pos_str0_ + n; + if (in_b (new_str0)) + pos_str0_ = new_str0; + else + error (error_string (new_str0) + "forward past eof"); + + return old_pos; +} + +String +Source_file::get_string (int n) +{ + String str = String ((Byte const*)forward_str0 (n), n); + return str; +} diff --git a/lily/source.cc b/lily/source.cc new file mode 100644 index 0000000000..c4dde1cd14 --- /dev/null +++ b/lily/source.cc @@ -0,0 +1,85 @@ +/* + source.cc -- implement Sources + + source file of the LilyPond music typesetter + + (c) 1997--2000 Han-Wen Nienhuys +*/ + + +#include + +#include "killing-cons.tcc" +#include "string.hh" +#include "flower-proto.hh" +#include "source-file.hh" +#include "source.hh" +#include "file-path.hh" + +Sources::Sources () +{ + sourcefile_p_list_ = 0; + path_C_= 0; + binary_b_ = false; +} + +void +Sources::set_binary (bool bo) +{ + binary_b_ = bo; +} + +void +Sources::set_path (File_path *f) +{ + path_C_ = f; +} + +/** + open a file + + @param file_string the file to be opened, name might be changed if it + is found in a search path. UGH! + + @return 0 if no file found + */ +Source_file* +Sources::get_file (String &file_string) //UGH +{ + if ((file_string != "-") && path_C_) + { + String file_string_o = path_C_->find (file_string); + if ((file_string_o == "") && (file_string != "")) + return 0; + file_string = file_string_o; + } + Source_file * f = new Source_file (file_string) ; + add (f); + return f; +} + +void +Sources::add (Source_file* sourcefile) +{ + sourcefile_p_list_ = new Killing_cons (sourcefile, sourcefile_p_list_); +} + +Sources::~Sources () +{ + delete sourcefile_p_list_; +} +/** + search the list for file whose map contains pointer #str0# + + @return 0 if not found. + */ +Source_file* +Sources::get_sourcefile (char const* str0) +{ + + for (Cons *i = sourcefile_p_list_; i; i = i->next_) + if (i->car_->in_b (str0)) + return i->car_; + return 0; +} +