about summary refs log tree commit diff
path: root/src/libterm
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-01-29 17:39:12 -0800
committerAlex Crichton <alex@alexcrichton.com>2014-02-03 09:32:33 -0800
commitef00c6a278cdd3bd00f44133573e1f5e2e951520 (patch)
tree1e74bbc9ca8909aced3cbb4e105bf9e41799b8f9 /src/libterm
parentece8a8f520697be50cbe543bebe065c5198dae4d (diff)
downloadrust-ef00c6a278cdd3bd00f44133573e1f5e2e951520.tar.gz
rust-ef00c6a278cdd3bd00f44133573e1f5e2e951520.zip
extra: Remove io_error usage
Diffstat (limited to 'src/libterm')
-rw-r--r--src/libterm/lib.rs46
-rw-r--r--src/libterm/terminfo/parser/compiled.rs33
-rw-r--r--src/libterm/terminfo/searcher.rs4
3 files changed, 47 insertions, 36 deletions
diff --git a/src/libterm/lib.rs b/src/libterm/lib.rs
index 01ebf58628f..98dd2b20a5f 100644
--- a/src/libterm/lib.rs
+++ b/src/libterm/lib.rs
@@ -141,45 +141,48 @@ impl<T: Writer> Terminal<T> {
     /// If the color is a bright color, but the terminal only supports 8 colors,
     /// the corresponding normal color will be used instead.
     ///
-    /// Returns true if the color was set, false otherwise.
-    pub fn fg(&mut self, color: color::Color) -> bool {
+    /// Returns Ok(true) if the color was set, Ok(false) otherwise, and Err(e)
+    /// if there was an I/O error
+    pub fn fg(&mut self, color: color::Color) -> io::IoResult<bool> {
         let color = self.dim_if_necessary(color);
         if self.num_colors > color {
             let s = expand(*self.ti.strings.find_equiv(&("setaf")).unwrap(),
                            [Number(color as int)], &mut Variables::new());
             if s.is_ok() {
-                self.out.write(s.unwrap());
-                return true
+                if_ok!(self.out.write(s.unwrap()));
+                return Ok(true)
             } else {
                 warn!("{}", s.unwrap_err());
             }
         }
-        false
+        Ok(false)
     }
     /// Sets the background color to the given color.
     ///
     /// If the color is a bright color, but the terminal only supports 8 colors,
     /// the corresponding normal color will be used instead.
     ///
-    /// Returns true if the color was set, false otherwise.
-    pub fn bg(&mut self, color: color::Color) -> bool {
+    /// Returns Ok(true) if the color was set, Ok(false) otherwise, and Err(e)
+    /// if there was an I/O error
+    pub fn bg(&mut self, color: color::Color) -> io::IoResult<bool> {
         let color = self.dim_if_necessary(color);
         if self.num_colors > color {
             let s = expand(*self.ti.strings.find_equiv(&("setab")).unwrap(),
                            [Number(color as int)], &mut Variables::new());
             if s.is_ok() {
-                self.out.write(s.unwrap());
-                return true
+                if_ok!(self.out.write(s.unwrap()));
+                return Ok(true)
             } else {
                 warn!("{}", s.unwrap_err());
             }
         }
-        false
+        Ok(false)
     }
 
     /// Sets the given terminal attribute, if supported.
-    /// Returns true if the attribute was supported, false otherwise.
-    pub fn attr(&mut self, attr: attr::Attr) -> bool {
+    /// Returns Ok(true) if the attribute was supported, Ok(false) otherwise,
+    /// and Err(e) if there was an I/O error.
+    pub fn attr(&mut self, attr: attr::Attr) -> io::IoResult<bool> {
         match attr {
             attr::ForegroundColor(c) => self.fg(c),
             attr::BackgroundColor(c) => self.bg(c),
@@ -189,13 +192,13 @@ impl<T: Writer> Terminal<T> {
                 if parm.is_some() {
                     let s = expand(*parm.unwrap(), [], &mut Variables::new());
                     if s.is_ok() {
-                        self.out.write(s.unwrap());
-                        return true
+                        if_ok!(self.out.write(s.unwrap()));
+                        return Ok(true)
                     } else {
                         warn!("{}", s.unwrap_err());
                     }
                 }
-                false
+                Ok(false)
             }
         }
     }
@@ -214,7 +217,7 @@ impl<T: Writer> Terminal<T> {
     }
 
     /// Resets all terminal attributes and color to the default.
-    pub fn reset(&mut self) {
+    pub fn reset(&mut self) -> io::IoResult<()> {
         let mut cap = self.ti.strings.find_equiv(&("sgr0"));
         if cap.is_none() {
             // are there any terminals that have color/attrs and not sgr0?
@@ -228,7 +231,7 @@ impl<T: Writer> Terminal<T> {
             expand(*op, [], &mut Variables::new())
         });
         if s.is_ok() {
-            self.out.write(s.unwrap());
+            return self.out.write(s.unwrap())
         } else if self.num_colors > 0 {
             warn!("{}", s.unwrap_err());
         } else {
@@ -236,6 +239,7 @@ impl<T: Writer> Terminal<T> {
             // but it's not worth testing all known attributes just for this.
             debug!("{}", s.unwrap_err());
         }
+        Ok(())
     }
 
     fn dim_if_necessary(&self, color: color::Color) -> color::Color {
@@ -252,11 +256,11 @@ impl<T: Writer> Terminal<T> {
 }
 
 impl<T: Writer> Writer for Terminal<T> {
-    fn write(&mut self, buf: &[u8]) {
-        self.out.write(buf);
+    fn write(&mut self, buf: &[u8]) -> io::IoResult<()> {
+        self.out.write(buf)
     }
 
-    fn flush(&mut self) {
-        self.out.flush();
+    fn flush(&mut self) -> io::IoResult<()> {
+        self.out.flush()
     }
 }
diff --git a/src/libterm/terminfo/parser/compiled.rs b/src/libterm/terminfo/parser/compiled.rs
index b0104faf4b5..31f12bd45e6 100644
--- a/src/libterm/terminfo/parser/compiled.rs
+++ b/src/libterm/terminfo/parser/compiled.rs
@@ -162,6 +162,10 @@ pub static stringnames: &'static[&'static str] = &'static[ "cbt", "_", "cr", "cs
 /// Parse a compiled terminfo entry, using long capability names if `longnames` is true
 pub fn parse(file: &mut io::Reader,
              longnames: bool) -> Result<~TermInfo, ~str> {
+    macro_rules! if_ok( ($e:expr) => (
+        match $e { Ok(e) => e, Err(e) => return Err(format!("{}", e)) }
+    ) )
+
     let bnames;
     let snames;
     let nnames;
@@ -177,17 +181,17 @@ pub fn parse(file: &mut io::Reader,
     }
 
     // Check magic number
-    let magic = file.read_le_u16();
+    let magic = if_ok!(file.read_le_u16());
     if magic != 0x011A {
         return Err(format!("invalid magic number: expected {:x} but found {:x}",
                            0x011A, magic as uint));
     }
 
-    let names_bytes          = file.read_le_i16() as int;
-    let bools_bytes          = file.read_le_i16() as int;
-    let numbers_count        = file.read_le_i16() as int;
-    let string_offsets_count = file.read_le_i16() as int;
-    let string_table_bytes   = file.read_le_i16() as int;
+    let names_bytes          = if_ok!(file.read_le_i16()) as int;
+    let bools_bytes          = if_ok!(file.read_le_i16()) as int;
+    let numbers_count        = if_ok!(file.read_le_i16()) as int;
+    let string_offsets_count = if_ok!(file.read_le_i16()) as int;
+    let string_table_bytes   = if_ok!(file.read_le_i16()) as int;
 
     assert!(names_bytes          > 0);
 
@@ -216,18 +220,21 @@ pub fn parse(file: &mut io::Reader,
     }
 
     // don't read NUL
-    let names_str = str::from_utf8_owned(file.read_bytes(names_bytes as uint - 1)).unwrap();
+    let bytes = if_ok!(file.read_bytes(names_bytes as uint - 1));
+    let names_str = match str::from_utf8_owned(bytes) {
+        Some(s) => s, None => return Err(~"input not utf-8"),
+    };
 
     let term_names: ~[~str] = names_str.split('|').map(|s| s.to_owned()).collect();
 
-    file.read_byte(); // consume NUL
+    if_ok!(file.read_byte()); // consume NUL
 
     debug!("term names: {:?}", term_names);
 
     let mut bools_map = HashMap::new();
     if bools_bytes != 0 {
         for i in range(0, bools_bytes) {
-            let b = file.read_byte().unwrap();
+            let b = if_ok!(file.read_byte());
             if b < 0 {
                 error!("EOF reading bools after {} entries", i);
                 return Err(~"error: expected more bools but hit EOF");
@@ -242,13 +249,13 @@ pub fn parse(file: &mut io::Reader,
 
     if (bools_bytes + names_bytes) % 2 == 1 {
         debug!("adjusting for padding between bools and numbers");
-        file.read_byte(); // compensate for padding
+        if_ok!(file.read_byte()); // compensate for padding
     }
 
     let mut numbers_map = HashMap::new();
     if numbers_count != 0 {
         for i in range(0, numbers_count) {
-            let n = file.read_le_u16();
+            let n = if_ok!(file.read_le_u16());
             if n != 0xFFFF {
                 debug!("{}\\#{}", nnames[i], n);
                 numbers_map.insert(nnames[i].to_owned(), n);
@@ -263,12 +270,12 @@ pub fn parse(file: &mut io::Reader,
     if string_offsets_count != 0 {
         let mut string_offsets = vec::with_capacity(10);
         for _ in range(0, string_offsets_count) {
-            string_offsets.push(file.read_le_u16());
+            string_offsets.push(if_ok!(file.read_le_u16()));
         }
 
         debug!("offsets: {:?}", string_offsets);
 
-        let string_table = file.read_bytes(string_table_bytes as uint);
+        let string_table = if_ok!(file.read_bytes(string_table_bytes as uint));
 
         if string_table.len() != string_table_bytes as uint {
             error!("EOF reading string table after {} bytes, wanted {}", string_table.len(),
diff --git a/src/libterm/terminfo/searcher.rs b/src/libterm/terminfo/searcher.rs
index 8cbb0902697..421f8581b58 100644
--- a/src/libterm/terminfo/searcher.rs
+++ b/src/libterm/terminfo/searcher.rs
@@ -77,8 +77,8 @@ pub fn open(term: &str) -> Result<File, ~str> {
     match get_dbpath_for_term(term) {
         Some(x) => {
             match File::open(x) {
-                Some(file) => Ok(file),
-                None => Err(~"error opening file"),
+                Ok(file) => Ok(file),
+                Err(e) => Err(format!("error opening file: {}", e)),
             }
         }
         None => Err(format!("could not find terminfo entry for {}", term))