From c18c615b66c21914b2c76888aef3a70d2b53a35f Mon Sep 17 00:00:00 2001 From: hanwen Date: Wed, 8 Jun 2005 13:07:11 +0000 Subject: [PATCH] * ttftool/util.c (syserror): use errno for better error reporting. * lily/source-file.cc (get_counts): new function. Calc column, line and char count in one go. * lily/binary-source-file.cc (quote_input): rename to quote_input * lily/input.cc (set): new function. --- ChangeLog | 11 ++++ lily/binary-source-file.cc | 2 +- lily/include/binary-source-file.hh | 2 +- lily/include/input.hh | 11 +++- lily/include/source-file.hh | 5 +- lily/input-scheme.cc | 14 ++++-- lily/input.cc | 49 +++++++++++++++--- lily/lexer.ll | 4 +- lily/lily-lexer.cc | 6 +-- lily/parse-scm.cc | 4 +- lily/source-file.cc | 81 ++++++++++++++++-------------- scm/output-ps.scm | 7 +-- scm/safe-lily.scm | 2 +- ttftool/include/proto.h | 2 +- ttftool/include/ttftool.h | 1 - ttftool/parse.c | 13 ++--- ttftool/ttfps.c | 2 +- ttftool/util.c | 50 ++++++++++-------- 18 files changed, 168 insertions(+), 98 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3aee43cccf..4410759366 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2005-06-08 Han-Wen Nienhuys + + * ttftool/util.c (syserror): use errno for better error reporting. + + * lily/source-file.cc (get_counts): new function. Calc column, + line and char count in one go. + + * lily/binary-source-file.cc (quote_input): rename to quote_input + + * lily/input.cc (set): new function. + 2005-06-08 Jan Nieuwenhuizen * lily/main.cc (prepend_env_path): Bugfix: Check directory diff --git a/lily/binary-source-file.cc b/lily/binary-source-file.cc index b1a78b16dc..93aca0ad2b 100644 --- a/lily/binary-source-file.cc +++ b/lily/binary-source-file.cc @@ -21,7 +21,7 @@ Binary_source_file::~Binary_source_file () } String -Binary_source_file::error_string (char const *pos_str0) const +Binary_source_file::quote_input (char const *pos_str0) const { assert (this); if (!contains (pos_str0)) diff --git a/lily/include/binary-source-file.hh b/lily/include/binary-source-file.hh index b47caf9270..cccbb77855 100644 --- a/lily/include/binary-source-file.hh +++ b/lily/include/binary-source-file.hh @@ -20,7 +20,7 @@ public: Byte get_Byte () {return get_U8 (); } int get_int () { return get_U32 (); } - virtual String error_string (char const *pos_str0) const; + virtual String quote_input (char const *pos_str0) const; virtual int get_line (char const *pos_str0) const; }; diff --git a/lily/include/input.hh b/lily/include/input.hh index 75bc07249d..e5ad3b42c5 100644 --- a/lily/include/input.hh +++ b/lily/include/input.hh @@ -16,11 +16,16 @@ */ class Input { -public: char const *start_; char const *end_; Source_file *source_file_; +public: + Source_file *get_source_file () const; + char const *start () const; + char const *end () const; + + void set (Source_file *, char const *, char const *); void warning (String) const; // should use member func? void non_fatal_error (String) const; void error (String) const; @@ -31,13 +36,15 @@ public: Input spot () const; String location_string () const; String line_number_string () const; - String file_string ()const; + int line_number ()const; int column_number ()const; int end_line_number ()const; int end_column_number ()const; + void get_counts (int*line, int *char_count, int *col) const; + Input (Input const &i); Input (); }; diff --git a/lily/include/source-file.hh b/lily/include/source-file.hh index 90ec4dd710..8d3aa4b4ba 100644 --- a/lily/include/source-file.hh +++ b/lily/include/source-file.hh @@ -32,7 +32,7 @@ public: virtual ~Source_file (); char const *to_str0 () const; - virtual String error_string (char const *pos_str0) const; + virtual String quote_input (char const *pos_str0) const; std::istream *get_istream (); bool contains (char const *pos_str0) const; int length () const; @@ -52,8 +52,7 @@ public: 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_of_line (char const *pos_str0) const; + void get_counts (char const *pos_str0, int*, int*, int*) const; /* JUNKME. diff --git a/lily/input-scheme.cc b/lily/input-scheme.cc index 4c771f0049..51432ff9b9 100644 --- a/lily/input-scheme.cc +++ b/lily/input-scheme.cc @@ -32,14 +32,18 @@ LY_DEFINE (ly_input_message, "ly:input-message", 2, 0, 0, (SCM sip, SCM msg), return SCM_UNSPECIFIED; } -LY_DEFINE (ly_input_file_line_column, "ly:input-file-line-column", 1, 0, 0, (SCM sip), - "Return input location in @var{sip} as (file-name line column).") +LY_DEFINE (ly_input_file_line_column, "ly:input-file-line-char-column", 1, 0, 0, (SCM sip), + "Return input location in @var{sip} as (file-name line char column).") { Input *ip = unsmob_input (sip); SCM_ASSERT_TYPE (ip, sip, SCM_ARG1, __FUNCTION__, "input location"); - return scm_list_3 (scm_makfrom0str (ip->file_string ().to_str0 ()), - scm_int2num (ip->line_number ()), - scm_int2num (ip->column_number ())); + + int l, ch, col; + ip->get_counts (&l, &ch, &col); + return scm_list_4 (scm_makfrom0str (ip->file_string ().to_str0 ()), + scm_int2num (l), + scm_int2num (ch), + scm_int2num (col)); } LY_DEFINE (ly_input_both_locations, "ly:input-both-locations", 1, 0, 0, (SCM sip), diff --git a/lily/input.cc b/lily/input.cc index 5b283837ec..a9ad1c970e 100644 --- a/lily/input.cc +++ b/lily/input.cc @@ -69,7 +69,7 @@ Input::message (String s) const { if (source_file_) s = location_string () + ": " + s + "\n" - + source_file_->error_string (start_); + + source_file_->quote_input (start_); ::message (s); } @@ -128,9 +128,10 @@ Input::line_number () const int Input::column_number () const { - if (source_file_) - return source_file_->get_column (start_); - return 0; + int line, chr, col = 0; + source_file_->get_counts (start_, &line, &chr, &col); + + return col; } int @@ -144,7 +145,41 @@ Input::end_line_number () const int Input::end_column_number () const { - if (source_file_) - return source_file_->get_column (end_); - return 0; + int line, chr, col = 0; + source_file_->get_counts (end_, &line, &chr, &col); + + return col; +} + +void +Input::get_counts (int *line, int *chr, int*col) const +{ + source_file_->get_counts (start_, line, chr, col); +} + +void +Input::set (Source_file *sf, char const *start, char const *end) +{ + source_file_ = sf; + start_ = start; + end_ = end; +} + +Source_file* +Input::get_source_file () const +{ + return source_file_; +} + + +char const * +Input::start () const +{ + return start_; +} + +char const * +Input::end () const +{ + return end_; } diff --git a/lily/lexer.ll b/lily/lexer.ll index 2cf4f3fb67..47a25a3002 100644 --- a/lily/lexer.ll +++ b/lily/lexer.ll @@ -200,7 +200,7 @@ BOM_UTF8 \357\273\277 s = s.left_string (s.index_last ('\"')); yy_pop_state (); - this->here_input().source_file_->name_ = s; + this->here_input().get_source_file ()->name_ = s; message (_f ("Renaming input to: `%s'", s.to_str0 ())); progress_indication ("\n"); scm_module_define (scm_car (scopes_), @@ -289,7 +289,7 @@ BOM_UTF8 \357\273\277 int n = 0; Input hi = here_input(); hi.step_forward (); - SCM sval = ly_parse_scm (hi.start_, &n, hi, + SCM sval = ly_parse_scm (hi.start (), &n, hi, be_safe_global && is_main_input_); if (sval == SCM_UNDEFINED) diff --git a/lily/lily-lexer.cc b/lily/lily-lexer.cc index a22086b676..455bbfaea0 100644 --- a/lily/lily-lexer.cc +++ b/lily/lily-lexer.cc @@ -268,9 +268,9 @@ Lily_lexer::prepare_for_next_token () void Lily_lexer::add_lexed_char (int count) { - lexloc->source_file_ = get_source_file (); - lexloc->start_ = here_str0 (); - lexloc->end_ = lexloc->start_ + count; + char const * start = here_str0 (); + lexloc->set (get_source_file (), + start, start + count); char_count_stack_.top () += count; } diff --git a/lily/parse-scm.cc b/lily/parse-scm.cc index 2f8aa221d1..db09db8754 100644 --- a/lily/parse-scm.cc +++ b/lily/parse-scm.cc @@ -21,10 +21,10 @@ SCM internal_ly_parse_scm (Parse_start *ps) { - Source_file *sf = ps->start_location_.source_file_; + Source_file *sf = ps->start_location_.get_source_file (); SCM port = sf->get_port (); - int off = ps->start_location_.start_ - sf->to_str0 (); + int off = ps->start_location_.start () - sf->to_str0 (); scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET)); SCM from = scm_ftell (port); diff --git a/lily/source-file.cc b/lily/source-file.cc index 04f2bab93a..b4d003b764 100644 --- a/lily/source-file.cc +++ b/lily/source-file.cc @@ -143,8 +143,31 @@ 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_column (context_str0)); + { + int l, ch, col; + get_counts (context_str0, &l, &ch, &col); + + return name_string () + ":" + to_string (l) + + ":" + to_string (col); + } +} + + + +String +Source_file::quote_input (char const* pos_str0) const +{ + if (!contains (pos_str0)) + return " (" + _ ("position unknown") + ")"; + + int l, ch, col; + get_counts (pos_str0, &l, &ch, &col); + String line = line_string (pos_str0); + String context = line.left_string (ch) + + to_string ('\n') + + to_string (' ', col) + + line.cut_string (ch, INT_MAX); + return context; } String @@ -201,22 +224,18 @@ Source_file::line_string (char const* pos_str0) const return String ((Byte const *)data_str0 + line[LEFT], line.length ()); } -int -Source_file::get_char_of_line (char const *pos_str0) const -{ - if (!contains (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 +void +Source_file::get_counts (char const *pos_str0, + int *line_number, + int *line_char, + int *column) const { if (!contains (pos_str0)) - return 0; + return; + *line_number = get_line (pos_str0); + Slice line = line_slice (pos_str0); char const *data = to_str0 (); Byte const *line_start = (Byte const *)data + line[LEFT]; @@ -225,7 +244,9 @@ Source_file::get_column (char const *pos_str0) const String line_begin (line_start, left); char const *line_chars = line_begin.to_str0(); - int column = 0; + *column = 0; + *line_char = 0; + mbstate_t state; /* Initialize the state. */ @@ -252,34 +273,18 @@ Source_file::get_column (char const *pos_str0) const thislen = 1; if (thislen == 1 && line_chars[0] == '\t') - column = (column / 8 + 1) * 8; + (*column) = (*column / 8 + 1) * 8; else - column ++; - + (*column) ++; + + (*line_char) ++; /* Advance past this character. */ line_chars += thislen; left -= thislen; } - return column; -} - -String -Source_file::error_string (char const* pos_str0) const -{ - if (!contains (pos_str0)) - return " (" + _ ("position unknown") + ")"; - - int ch_i = get_char_of_line (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::contains (char const* pos_str0) const { @@ -332,7 +337,7 @@ Source_file::set_pos (char const * pos_str0) if (contains (pos_str0)) pos_str0_ = pos_str0; else - error (error_string (pos_str0) + "invalid pos"); + error (quote_input (pos_str0) + "invalid pos"); } char const * @@ -344,7 +349,7 @@ Source_file::seek_str0 (int n) if (contains (new_str0)) pos_str0_ = new_str0; else - error (error_string (new_str0) + "seek past eof"); + error (quote_input (new_str0) + "seek past eof"); return pos_str0_; } @@ -357,7 +362,7 @@ Source_file::forward_str0 (int n) if (contains (new_str0)) pos_str0_ = new_str0; else - error (error_string (new_str0) + "forward past eof"); + error (quote_input (new_str0) + "forward past eof"); return old_pos; } diff --git a/scm/output-ps.scm b/scm/output-ps.scm index bbb0d0c381..fe168a103f 100644 --- a/scm/output-ps.scm +++ b/scm/output-ps.scm @@ -197,7 +197,7 @@ (ly:music-property cause 'origin)))) (if (not (ly:input-location? music-origin)) "" - (let* ((location (ly:input-file-line-column music-origin)) + (let* ((location (ly:input-file-line-char-column music-origin)) (raw-file (car location)) (file (if (is-absolute? raw-file) raw-file @@ -207,14 +207,15 @@ (if (and (< 0 (interval-length x-ext)) (< 0 (interval-length y-ext))) - (format "~a ~a ~a ~a (textedit://~a:~a:~a) mark_URI\n" + (format "~a ~a ~a ~a (textedit://~a:~a:~a:~a) mark_URI\n" (+ (car offset) (car x-ext)) (+ (cdr offset) (car y-ext)) (+ (car offset) (cdr x-ext)) (+ (cdr offset) (cdr y-ext)) file (cadr location) - (caddr location)) + (caddr location) + (cadddr location)) ""))))) (define (lily-def key val) diff --git a/scm/safe-lily.scm b/scm/safe-lily.scm index c0076d1768..5df324577c 100644 --- a/scm/safe-lily.scm +++ b/scm/safe-lily.scm @@ -60,7 +60,7 @@ ly:grob-system ly:grob-translate-axis! ly:grob? - ly:input-file-line-column + ly:input-file-line-char-column ly:input-location? ly:input-message ly:intlog2 diff --git a/ttftool/include/proto.h b/ttftool/include/proto.h index 841cdca1ba..83d5f7a3ca 100644 --- a/ttftool/include/proto.h +++ b/ttftool/include/proto.h @@ -50,7 +50,7 @@ extern char *adobeStandardEncoding[]; void *mymalloc (size_t size); void *mycalloc (size_t nelem, size_t elsize); void *myrealloc (void *ptr, size_t size); -void error (char *string); +void ttf_error (char *string); void syserror (char *string); ssize_t surely_read (int fildes, void *buf, size_t nbyte); char *unistrncpy (char *dst, char *str, size_t length); diff --git a/ttftool/include/ttftool.h b/ttftool/include/ttftool.h index 7d5467f834..e60bed72fd 100644 --- a/ttftool/include/ttftool.h +++ b/ttftool/include/ttftool.h @@ -5,7 +5,6 @@ extern "C" void create_type42 (char const *, void *); - #ifdef __cplusplus } #endif diff --git a/ttftool/parse.c b/ttftool/parse.c index 8d7f0f8bc4..e6becb5586 100644 --- a/ttftool/parse.c +++ b/ttftool/parse.c @@ -43,7 +43,8 @@ readNamingTable (int fd) surely_read (fd, &format, sizeof (USHORT)); FIX_UH (format); if (format != 0) - error ("Bad TTF file. Format should be 0\n"); + ttf_error ("Format should be 0\n"); + surely_read (fd, &nrecords, sizeof (USHORT)); FIX_UH (nrecords); surely_read (fd, &offset, sizeof (USHORT)); @@ -137,7 +138,7 @@ readHeadTable (int fd, struct HeadTable *ht) ht->fontRevision.mantissa, ht->fontRevision.fraction); } if (ht->magicNumber != 0x5F0F3CF5) - error ("Bad magic number in TTF file"); + ttf_error ("Bad magic number"); if (verbosity >= 2) fprintf (stderr, " %d units per Em\n", ht->unitsPerEm); } @@ -165,11 +166,11 @@ readPostTable (int fd, int nglyphs, struct PostTable *pt, return 1; /* MacGlyphEncoding */ case 2: if (pt->formatType.fraction != 0) - error ("Unsupported `post' table format"); + ttf_error ("Unsupported `post' table format"); surely_read (fd, &nglyphspost, sizeof (USHORT)); FIX_UH (nglyphspost); if (nglyphspost != nglyphs) - error ("Inconsistency between `maxp' and `nglyphs' tables!"); + ttf_error ("Inconsistency between `maxp' and `nglyphs' tables!"); if (verbosity >= 2) fprintf (stderr, " %d glyphs\n", nglyphs); glyphNameIndex = mymalloc (sizeof (USHORT) * nglyphs); @@ -244,7 +245,7 @@ readLocaTable (int fd, int nglyphs, int format) return offsets; } /*NOTREACHED*/ default: - error ("Unknown `loca' table format"); + ttf_error ("Unknown `loca' table format"); /*NOTREACHED*/} /*NOTREACHED*/} @@ -297,7 +298,7 @@ readHheaTable (int fd) fprintf (stderr, " version %d.%u\n", hhea->version.mantissa, hhea->version.fraction); if (hhea->metricDataFormat != 0) - error ("Unknown metric data format"); + ttf_error ("Unknown metric data format"); return hhea; } diff --git a/ttftool/ttfps.c b/ttftool/ttfps.c index 1af881909a..0cdc73627e 100644 --- a/ttftool/ttfps.c +++ b/ttftool/ttfps.c @@ -88,7 +88,7 @@ create_type42 (const char *infile, void *out) } } if (maxpOff == 0 || headOff == 0 || postOff == 0 || nameOff == 0) - error ("Incomplete TTF file\n"); + ttf_error ("Incomplete TTF file\n"); if (verbosity >= 1) fprintf (stderr, "Processing `maxp' table\n"); diff --git a/ttftool/util.c b/ttftool/util.c index 6c5fe9e88a..b64aae9bc8 100644 --- a/ttftool/util.c +++ b/ttftool/util.c @@ -5,19 +5,21 @@ #include #include #include +#include + #include "types.h" #include "proto.h" -#define ALIAS_FILE_TO_FILECOOKIE - #include "libc-extension.hh" + + void * mymalloc (size_t size) { void *p; if ((p = malloc (size)) == NULL) - error ("Unable to allocate memory\n"); + ttf_error ("Unable to allocate memory\n"); return p; } @@ -26,16 +28,34 @@ mycalloc (size_t nelem, size_t elsize) { void *p; if ((p = calloc (nelem, elsize)) == NULL) - error ("Unable to allocate memory\n"); + ttf_error ("Unable to allocate memory\n"); return p; } +void +ttf_error (char *string) +{ + fprintf (stderr, "TTF tool: %s\n", string); + exit (3); + /*NOTREACHED*/ +} + +void +syserror (char *string) +{ + char *sys_err = strerror (errno); + fprintf (stderr, "TTF tool: %s (%s)\n", + string, + sys_err); + exit (3); +} + void * myrealloc (void *ptr, size_t size) { void *p; if ((p = realloc (ptr, size)) == NULL) - error ("Unable to allocate memory\n"); + ttf_error ("Unable to allocate memory\n"); return p; } @@ -44,30 +64,18 @@ surely_lseek (int fildes, off_t offset, int whence) { off_t result; if ((result = lseek (fildes, offset, whence)) < 0) - error ("Bad TTF file. Cannot seek"); + syserror ("Cannot seek"); return result; } -void -error (char *string) -{ - fprintf (stderr, "%s\n", string); - exit (3); - /*NOTREACHED*/} - -void -syserror (char *string) -{ - perror (string); - exit (3); - /*NOTREACHED*/} - ssize_t surely_read (int fildes, void *buf, size_t nbyte) { ssize_t n; if ((n = read (fildes, buf, nbyte)) < nbyte) - error ("Bad TTF file. Read too little bytes in surely_read()"); + { + syserror ("read too little in surely_read()"); + } return n; } -- 2.39.5