2011-08-27 17:03:39 -04:00
|
|
|
#include <stdio.h>
|
2011-11-18 13:29:52 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2011-11-22 11:26:47 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2011-11-26 05:00:45 -05:00
|
|
|
#include <error.h>
|
2011-08-27 17:03:39 -04:00
|
|
|
#include <libmaxsi/sortix-keyboard.h>
|
|
|
|
|
2011-11-18 13:29:52 -05:00
|
|
|
int cat(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
int fd = open(argv[i], O_RDONLY);
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( fd < 0 )
|
|
|
|
{
|
2011-11-26 05:00:45 -05:00
|
|
|
error(0, errno, "%s", argv[i]);
|
2011-11-22 11:26:47 -05:00
|
|
|
result = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-18 13:29:52 -05:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
const size_t BUFFER_SIZE = 255;
|
|
|
|
char buffer[BUFFER_SIZE+1];
|
|
|
|
ssize_t bytesread = read(fd, buffer, BUFFER_SIZE);
|
|
|
|
if ( bytesread == 0 ) { break; }
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( bytesread < 0 )
|
|
|
|
{
|
2011-11-26 05:00:45 -05:00
|
|
|
error(0, errno, "read: %s", argv[i]);
|
2011-11-22 11:26:47 -05:00
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-26 13:14:08 -05:00
|
|
|
if ( writeall(1, buffer, bytesread) )
|
2011-11-22 18:18:04 -05:00
|
|
|
{
|
2011-11-26 05:00:45 -05:00
|
|
|
error(0, errno, "write: %s", argv[i]);
|
2011-11-22 18:18:04 -05:00
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 13:29:52 -05:00
|
|
|
} while ( true );
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-08-27 17:03:39 -04:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2011-11-18 13:29:52 -05:00
|
|
|
if ( 1 < argc )
|
|
|
|
{
|
|
|
|
return cat(argc, argv);
|
|
|
|
}
|
|
|
|
|
2011-08-27 17:03:39 -04:00
|
|
|
bool lastwasesc = false;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
unsigned method = System::Keyboard::POLL;
|
2011-10-26 18:05:20 -04:00
|
|
|
uint32_t codepoint = System::Keyboard::ReceiveKeystroke(method);
|
2011-08-27 17:03:39 -04:00
|
|
|
|
|
|
|
if ( codepoint == 0 ) { continue; }
|
|
|
|
if ( codepoint & Maxsi::Keyboard::DEPRESSED ) { continue; }
|
|
|
|
if ( codepoint == Maxsi::Keyboard::UP ) { printf("\e[A"); continue; }
|
|
|
|
if ( codepoint == Maxsi::Keyboard::DOWN ) { printf("\e[B"); continue; }
|
|
|
|
if ( codepoint == Maxsi::Keyboard::RIGHT ) { printf("\e[C"); continue; }
|
|
|
|
if ( codepoint == Maxsi::Keyboard::LEFT ) { printf("\e[D"); continue; }
|
|
|
|
if ( codepoint == Maxsi::Keyboard::ESC ) { printf("\e["); lastwasesc = true; continue; }
|
|
|
|
if ( lastwasesc && codepoint == '[' ) { continue; }
|
|
|
|
if ( codepoint >= 0x80 ) { continue; }
|
|
|
|
|
|
|
|
char msg[2]; msg[0] = codepoint; msg[1] = '\0';
|
|
|
|
printf(msg);
|
|
|
|
lastwasesc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|