Moar verbose tests
This commit is contained in:
parent
c4024fd495
commit
40d6244983
161
test.c
161
test.c
|
@ -38,6 +38,11 @@ void my_putchar(char c)
|
|||
write(1, &c, 1);
|
||||
}
|
||||
|
||||
void my_putstr(char *str)
|
||||
{
|
||||
write(1, str, strlen(str));
|
||||
}
|
||||
|
||||
void my_putnbr(int nb)
|
||||
{
|
||||
if (nb >= 0)
|
||||
|
@ -49,23 +54,31 @@ void my_putnbr(int nb)
|
|||
my_putchar(-(nb % 10) + '0');
|
||||
}
|
||||
|
||||
void dump(char *title, struct vt100_emul *vt100,
|
||||
struct headless_terminal *term)
|
||||
void my_strdump(char *str)
|
||||
{
|
||||
while (*str != '\0')
|
||||
{
|
||||
if (*str >= ' ' && *str <= '~')
|
||||
{
|
||||
write(1, str, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
write(1, "[", 1);
|
||||
my_putnbr(*str);
|
||||
write(1, "]", 1);
|
||||
}
|
||||
str += 1;
|
||||
}
|
||||
write(1, "\n", 1);
|
||||
}
|
||||
|
||||
void disp(struct headless_terminal *term)
|
||||
{
|
||||
char c;
|
||||
unsigned int x;
|
||||
unsigned int y;
|
||||
char c;
|
||||
char d;
|
||||
int argc;
|
||||
|
||||
write(1, title, strlen(title));
|
||||
write(1, " ", 1);
|
||||
for (argc = 0; argc < vt100->argc; ++argc)
|
||||
{
|
||||
my_putnbr(vt100->argv[argc]);
|
||||
if (argc != vt100->argc - 1)
|
||||
write(1, ", ", 2);
|
||||
}
|
||||
write(1, "\n", 1);
|
||||
for (x = 0; x < term->width + 1; ++x)
|
||||
write(1, "-", 1);
|
||||
|
@ -82,14 +95,14 @@ void dump(char *title, struct vt100_emul *vt100,
|
|||
c = get(term, x, y);
|
||||
if (c == '\0')
|
||||
c = ' ';
|
||||
if (c > 31)
|
||||
if (c > 31 || c == '\t')
|
||||
write(1, &c, 1);
|
||||
else
|
||||
{
|
||||
d = '0' + c / 10;
|
||||
write(1, &d, 1);
|
||||
d = '0' + c % 10;
|
||||
write(1, &d, 1);
|
||||
fprintf(stderr,
|
||||
"Don't know how to print char '%c' (%i)\n",
|
||||
c, (int)c);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -101,6 +114,22 @@ void dump(char *title, struct vt100_emul *vt100,
|
|||
write(1, "\n\n", 2);
|
||||
}
|
||||
|
||||
void dump(char *title, struct vt100_emul *vt100,
|
||||
struct headless_terminal *term)
|
||||
{
|
||||
unsigned int argc;
|
||||
|
||||
write(1, title, strlen(title));
|
||||
write(1, " ", 1);
|
||||
for (argc = 0; argc < vt100->argc; ++argc)
|
||||
{
|
||||
my_putnbr(vt100->argv[argc]);
|
||||
if (argc != vt100->argc - 1)
|
||||
write(1, ", ", 2);
|
||||
}
|
||||
disp(term);
|
||||
}
|
||||
|
||||
/*
|
||||
DECSC – Save Cursor (DEC Private)
|
||||
|
||||
|
@ -482,7 +511,7 @@ static void vt100_write(struct vt100_emul *vt100, char c __attribute__((unused))
|
|||
{
|
||||
term->x = 0;
|
||||
vt100->argc = 0;
|
||||
CUD(vt100);
|
||||
NEL(vt100);
|
||||
return ;
|
||||
}
|
||||
set(term, term->x, term->y, c);
|
||||
|
@ -492,10 +521,76 @@ static void vt100_write(struct vt100_emul *vt100, char c __attribute__((unused))
|
|||
term->x = 0;
|
||||
term->y += 1;
|
||||
}
|
||||
/*dump("WRITE", term);*/
|
||||
my_putstr("Writing(");
|
||||
my_putchar(c);
|
||||
my_putstr(", ");
|
||||
my_putnbr((unsigned int)c);
|
||||
my_putstr(")\n");
|
||||
dump("WRITE", vt100, term);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
int main_loop(struct vt100_emul *vt100, int master)
|
||||
{
|
||||
char buffer[4096];
|
||||
fd_set rfds;
|
||||
int retval;
|
||||
ssize_t read_size;
|
||||
|
||||
while (42)
|
||||
{
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(master, &rfds);
|
||||
FD_SET(0, &rfds);
|
||||
retval = select(master + 1, &rfds, NULL, NULL, NULL);
|
||||
if (retval == -1)
|
||||
{
|
||||
perror("select()");
|
||||
}
|
||||
if (FD_ISSET(0, &rfds))
|
||||
{
|
||||
read_size = read(0, &buffer, 4096);
|
||||
if (read_size == -1)
|
||||
{
|
||||
perror("read");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
buffer[read_size] = '\0';
|
||||
write(master, buffer, read_size);
|
||||
}
|
||||
if (FD_ISSET(master, &rfds))
|
||||
{
|
||||
read_size = read(master, &buffer, 4096);
|
||||
if (read_size == -1)
|
||||
{
|
||||
perror("read");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
buffer[read_size] = '\0';
|
||||
my_strdump(buffer);
|
||||
vt100_read_str(vt100, buffer);
|
||||
disp(vt100->user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct termios backup;
|
||||
void set_non_canonical(int fd)
|
||||
{
|
||||
struct termios termios;
|
||||
ioctl(fd, TCGETS, &backup);
|
||||
ioctl(fd, TCGETS, &termios);
|
||||
termios.c_iflag |= ICANON;
|
||||
termios.c_cc[VMIN] = 1;
|
||||
termios.c_cc[VTIME] = 0;
|
||||
ioctl(fd, TCSETS, &termios);
|
||||
}
|
||||
|
||||
void restore_termios(int fd)
|
||||
{
|
||||
ioctl(fd, TCSETS, &backup);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
struct vt100_emul *vt100;
|
||||
struct vt100_callbacks callbacks;
|
||||
|
@ -503,9 +598,13 @@ int main(void)
|
|||
int master;
|
||||
int child;
|
||||
struct winsize winsize;
|
||||
char buffer[4096];
|
||||
ssize_t read_size;
|
||||
|
||||
if (ac == 1)
|
||||
{
|
||||
puts("Usage : test PROGNAME");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
set_non_canonical(0);
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
winsize.ws_row = terminal.height = 24;
|
||||
winsize.ws_col = terminal.width = 80;
|
||||
|
@ -518,7 +617,8 @@ int main(void)
|
|||
if (child == CHILD)
|
||||
{
|
||||
setsid();
|
||||
execlp("top", "top");
|
||||
putenv("TERM=vt100");
|
||||
execvp(av[1], av + 1);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
else
|
||||
|
@ -539,17 +639,8 @@ int main(void)
|
|||
vt100 = vt100_init(80, 24, &callbacks, vt100_write);
|
||||
vt100->user_data = &terminal;
|
||||
ioctl(master, TIOCSWINSZ, &winsize);
|
||||
while (42)
|
||||
{
|
||||
read_size = read(master, &buffer, 4096);
|
||||
if (read_size == -1)
|
||||
{
|
||||
perror("read");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
buffer[read_size] = '\0';
|
||||
vt100_read_str(vt100, buffer);
|
||||
}
|
||||
main_loop(vt100, master);
|
||||
}
|
||||
restore_termios(0);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user