From 4486f1c1192572873b1ae475f71829eea7a6ddf1 Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Wed, 26 Jan 2011 14:04:13 -0500 Subject: [PATCH] Cleanup: consolidate swrite --- Makefile | 5 +++-- parse.cpp | 24 ++---------------------- parser.hpp | 4 ++++ swrite.cpp | 23 +++++++++++++++++++++++ swrite.hpp | 6 ++++++ termemu.cpp | 33 +++------------------------------ terminal.cpp | 18 +----------------- 7 files changed, 42 insertions(+), 71 deletions(-) create mode 100644 swrite.cpp create mode 100644 swrite.hpp diff --git a/Makefile b/Makefile index f0d7d63..bb646d7 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ -source = parse.cpp parserstate.cpp parser.cpp templates.cpp terminal.cpp termemu.cpp parseraction.cpp terminalcsi.cpp -objects = parserstate.o parser.o templates.o terminal.o parseraction.o terminalcsi.o +source = parse.cpp parserstate.cpp parser.cpp templates.cpp terminal.cpp termemu.cpp parseraction.cpp terminalcsi.cpp swrite.cpp +objects = parserstate.o parser.o templates.o terminal.o parseraction.o terminalcsi.o swrite.o +repos = templates.rpo executables = parse termemu CXX = g++ diff --git a/parse.cpp b/parse.cpp index 261bb57..637d4f8 100644 --- a/parse.cpp +++ b/parse.cpp @@ -15,10 +15,7 @@ #include #include "parser.hpp" - -#ifndef __STDC_ISO_10646__ -#error "Must have __STDC_ISO_10646__" -#endif +#include "swrite.hpp" const size_t buf_size = 1024; @@ -64,11 +61,6 @@ int main( int argc __attribute__((unused)), if ( child == 0 ) { /* child */ - if ( chdir( "/" ) < 0 ) { - perror( "chdir" ); - exit( 1 ); - } - char *my_argv[ 2 ]; my_argv[ 0 ] = strdup( "/bin/bash" ); assert( my_argv[ 0 ] ); @@ -147,21 +139,9 @@ int copy( int src, int dest ) } else if ( bytes_read < 0 ) { perror( "read" ); return -1; - } else { - ssize_t total_bytes_written = 0; - while ( total_bytes_written < bytes_read ) { - ssize_t bytes_written = write( dest, buf + total_bytes_written, - bytes_read - total_bytes_written ); - if ( bytes_written <= 0 ) { - perror( "write" ); - return -1; - } else { - total_bytes_written += bytes_written; - } - } } - return 0; + return swrite( dest, buf, bytes_read ); } int vt_parser( int fd, Parser::UTF8Parser *parser ) diff --git a/parser.hpp b/parser.hpp index a689f48..0726445 100644 --- a/parser.hpp +++ b/parser.hpp @@ -13,6 +13,10 @@ #include "parserstate.hpp" #include "parserstatefamily.hpp" +#ifndef __STDC_ISO_10646__ +#error "Must have __STDC_ISO_10646__" +#endif + namespace Parser { class Parser { private: diff --git a/swrite.cpp b/swrite.cpp new file mode 100644 index 0000000..2960971 --- /dev/null +++ b/swrite.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +#include "swrite.hpp" + +int swrite( int fd, const char *str, ssize_t len ) +{ + ssize_t total_bytes_written = 0; + ssize_t bytes_to_write = ( len >= 0 ) ? len : strlen( str ); + while ( total_bytes_written < bytes_to_write ) { + ssize_t bytes_written = write( fd, str + total_bytes_written, + bytes_to_write - total_bytes_written ); + if ( bytes_written <= 0 ) { + perror( "write" ); + return -1; + } else { + total_bytes_written += bytes_written; + } + } + + return 0; +} diff --git a/swrite.hpp b/swrite.hpp new file mode 100644 index 0000000..8538d32 --- /dev/null +++ b/swrite.hpp @@ -0,0 +1,6 @@ +#ifndef SWRITE_HPP +#define SWRITE_HPP + +int swrite( int fd, const char *str, ssize_t len = -1 ); + +#endif diff --git a/termemu.cpp b/termemu.cpp index 306823a..79b7c06 100644 --- a/termemu.cpp +++ b/termemu.cpp @@ -17,10 +17,7 @@ #include #include "terminal.hpp" - -#ifndef __STDC_ISO_10646__ -#error "Must have __STDC_ISO_10646__" -#endif +#include "swrite.hpp" const size_t buf_size = 1024; @@ -158,21 +155,9 @@ int copy( int src, int dest ) } else if ( bytes_read < 0 ) { perror( "read" ); return -1; - } else { - ssize_t total_bytes_written = 0; - while ( total_bytes_written < bytes_read ) { - ssize_t bytes_written = write( dest, buf + total_bytes_written, - bytes_read - total_bytes_written ); - if ( bytes_written <= 0 ) { - perror( "write" ); - return -1; - } else { - total_bytes_written += bytes_written; - } - } } - return 0; + return swrite( dest, buf, bytes_read ); } int termemu( int fd, Terminal::Emulator *terminal, int debug_fd ) @@ -198,19 +183,7 @@ int termemu( int fd, Terminal::Emulator *terminal, int debug_fd ) terminal->debug_printout( STDOUT_FILENO ); /* write writeback */ - ssize_t total_bytes_written = 0; - ssize_t bytes_to_write = terminal_to_host.length(); - const char *str = terminal_to_host.c_str(); - while ( total_bytes_written < bytes_to_write ) { - ssize_t bytes_written = write( fd, str + total_bytes_written, - bytes_to_write - total_bytes_written ); - if ( bytes_written <= 0 ) { - perror( "write" ); - return -1; - } else { - total_bytes_written += bytes_written; - } - } + return swrite( fd, terminal_to_host.c_str(), terminal_to_host.length() ); return 0; } diff --git a/terminal.cpp b/terminal.cpp index 57bcc09..08566ed 100644 --- a/terminal.cpp +++ b/terminal.cpp @@ -5,11 +5,10 @@ #include #include "terminal.hpp" +#include "swrite.hpp" using namespace Terminal; -static void swrite( int fd, const char *str ); - Cell::Cell() : overlapping_cell( NULL ), contents(), @@ -192,21 +191,6 @@ void Emulator::print( Parser::Print *act ) } } -static void swrite( int fd, const char *str ) -{ - ssize_t total_bytes_written = 0; - ssize_t bytes_to_write = strlen( str ); - while ( total_bytes_written < bytes_to_write ) { - ssize_t bytes_written = write( fd, str + total_bytes_written, - bytes_to_write - total_bytes_written ); - if ( bytes_written <= 0 ) { - perror( "write" ); - } else { - total_bytes_written += bytes_written; - } - } -} - void Emulator::debug_printout( int fd ) { std::string screen;