Cleanup: consolidate swrite

This commit is contained in:
Keith Winstein
2011-01-26 14:04:13 -05:00
parent 65f7f3515c
commit 4486f1c119
7 changed files with 42 additions and 71 deletions
+3 -2
View File
@@ -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++
+2 -22
View File
@@ -15,10 +15,7 @@
#include <typeinfo>
#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 )
+4
View File
@@ -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:
+23
View File
@@ -0,0 +1,23 @@
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#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;
}
+6
View File
@@ -0,0 +1,6 @@
#ifndef SWRITE_HPP
#define SWRITE_HPP
int swrite( int fd, const char *str, ssize_t len = -1 );
#endif
+3 -30
View File
@@ -17,10 +17,7 @@
#include <fcntl.h>
#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;
}
+1 -17
View File
@@ -5,11 +5,10 @@
#include <typeinfo>
#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;