typr

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/typr.git
Log | Files | Refs

commit 5211c044e98da94d4eb1acacfe68854d27b25987
Author: Willy Goiffon <wgoiffon@LPWGF01.centro.fr>
Date:   Tue, 31 Dec 2019 11:25:38 +0100

Initial commit

Diffstat:
Atypr.c | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 83 insertions(+), 0 deletions(-)

diff --git a/typr.c b/typr.c @@ -0,0 +1,83 @@ +/* + * cc typr.c -o typr + * ./typr [filename] + * + * Type the text that is under the cursor. + * The cursor will stop on errors, no need to backspace + */ + +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <fcntl.h> +#include <paths.h> +#include <termios.h> + +#include <sys/types.h> +#include <sys/stat.h> + +int +wpm(int fd, char *line, int len) +{ + char c; + int i, rst, errors; + + i = rst = errors = 0; + while (i < len) { + read(fd, &c, 1); + if (c == line[i]) { + i++; + dprintf(fd, "%c", c); + if (rst) { + rst = 0; + dprintf(fd, ""); + } + } else { + rst = 1; + errors++; + dprintf(fd, "%c\b", line[i]); + } + } + + /* wait for a newline */ + while (read(fd, &c, 1) > 0 && c != '\n'); + dprintf(fd, "\n"); + + return errors; +} + +int +main(int argc, char *argv[]) +{ + int tty, tot; + char line[BUFSIZ], *p; + FILE *f; + struct termios term, save; + + f = argc > 1 ? fopen(argv[1], "r") : stdin; + + tty = open(_PATH_TTY, O_RDWR); + + tcgetattr(tty, &term); + tcgetattr(tty, &save); + term.c_lflag &= ~(ECHO|ICANON); + tcsetattr(tty, TCSANOW, &term); + + tot = 0; + while (fgets(line, BUFSIZ, f)) { + line[strlen(line)-1] = 0; + if (strlen(line) > 0) { + dprintf(tty, "%s\r", line); + tot += wpm(tty, line, strlen(line)); + } + } + + tcsetattr(tty, TCSANOW, &save); + fclose(f); + close(tty); + + printf(">> errors: %d\n", tot); + + return 0; +}