]> git.donarmstrong.com Git - lilypond.git/commitdiff
* ttftool/util.c (syserror): use errno for better error reporting.
authorHan-Wen Nienhuys <hanwen@xs4all.nl>
Wed, 8 Jun 2005 13:07:11 +0000 (13:07 +0000)
committerHan-Wen Nienhuys <hanwen@xs4all.nl>
Wed, 8 Jun 2005 13:07:11 +0000 (13:07 +0000)
* 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.

18 files changed:
ChangeLog
lily/binary-source-file.cc
lily/include/binary-source-file.hh
lily/include/input.hh
lily/include/source-file.hh
lily/input-scheme.cc
lily/input.cc
lily/lexer.ll
lily/lily-lexer.cc
lily/parse-scm.cc
lily/source-file.cc
scm/output-ps.scm
scm/safe-lily.scm
ttftool/include/proto.h
ttftool/include/ttftool.h
ttftool/parse.c
ttftool/ttfps.c
ttftool/util.c

index 3aee43cccf9ef37a6cfebb2115c67242f1b219b2..44107593669aa2d3c0355e10b4ddbff2c65fd5e3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2005-06-08  Han-Wen Nienhuys  <hanwen@xs4all.nl>
+
+       * 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  <janneke@gnu.org>
 
        * lily/main.cc (prepend_env_path): Bugfix: Check directory
index b1a78b16dc47ae48b18dc20ea352503eeb9ecdc0..93aca0ad2bec4cc891f869750ec384980721cc5a 100644 (file)
@@ -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))
index b47caf9270cab1dc5c6936966a59f5bf307a57d7..cccbb778555cfc9ee37ff74b1897dadf86c98dd8 100644 (file)
@@ -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;
 };
 
index 75bc07249d0f01c46e24968617bbc6305ee9bb20..e5ad3b42c5a44e17d561db13a1018a6f5e78c12e 100644 (file)
 */
 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 ();
 };
index 90ec4dd710d8d305fd96043077ee0e1a1d189f04..8d3aa4b4ba23d382be7d4711d2013311bae45b2a 100644 (file)
@@ -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.
index 4c771f0049b538ad63b90dc6d341fa9a74304ee3..51432ff9b9833886a645acbb757fe57b17634d25 100644 (file)
@@ -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),
index 5b283837eca4ec99dc5a8dbadc5ffbb48f8030d6..a9ad1c970e56b239c4c3d41339badd5c16602271 100644 (file)
@@ -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_;
 }
index 2cf4f3fb67f8c47e0aa105f738f0a743e76d5c61..47a25a30020d9fb9d52e0b199a4c926ff1463ffa 100644 (file)
@@ -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)
index a22086b6760e24b3a92b9ad09b6f99293db900a3..455bbfaea06c6ad645ed442496f0d982f136d89f 100644 (file)
@@ -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;
 }
 
index 2f8aa221d132976e0b95abff519623dc5e32c2a4..db09db8754e5ccdb42c27203c2c08853cdd48a86 100644 (file)
 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);
index 04f2bab93ad1d30fe1914d19f275cbd855f1da7c..b4d003b76431dc01db8a2ef35e2042540c0a8bc9 100644 (file)
@@ -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;
 }
index bbb0d0c381a9853537d20e98ebbe3de7f34b8663..fe168a103ffca1302e729a9f21d02881d7ed09ae 100644 (file)
                           (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
 
          (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)
index c0076d1768edad198d964ca2be1ea66aec565b63..5df324577cf9f5e520b80c979da3e8bbb7bdffea 100644 (file)
@@ -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
index 841cdca1baa8d46c7064e3f1e467947c860414f2..83d5f7a3ca3a60bd438e374450ac56a79d83c89f 100644 (file)
@@ -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);
index 7d5467f8345f771866d1e74ba930254d34fe8abf..e60bed72fd689d065d954f1302ccd3e2f1e9aeb6 100644 (file)
@@ -5,7 +5,6 @@ extern "C"
 
 
   void create_type42 (char const *, void *);
-
 #ifdef __cplusplus
 }
 #endif
index 8d7f0f8bc42eae87cb55651d36b2c33cc4a131ad..e6becb5586e6a818cf9328c39c3c5b352bea83b9 100644 (file)
@@ -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;
 }
 
index 1af881909a4e5a98c9e67bcc5884f602e9ab2276..0cdc73627edc4d002d39b3d918ad0219afa5eaaf 100644 (file)
@@ -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");
index 6c5fe9e88a359d86388449ac7b4e0a42da40c6d9..b64aae9bc893264f2a88ac991929823178ec9d19 100644 (file)
@@ -5,19 +5,21 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
+
 #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;
 }