From 7bf25fb8f0e4643a67894417a95d39e5901b1824 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Mon, 25 May 2015 22:53:55 +0100 Subject: editor.c: a joke, an editor which writes a c program --- editor.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 editor.c diff --git a/editor.c b/editor.c new file mode 100644 index 0000000..afea796 --- /dev/null +++ b/editor.c @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2015 Tomasz Kramkowski + * + * This program is free software. It is licensed under version 3 of the + * GNU General Public License. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see [http://www.gnu.org/licenses/]. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +bool running = true; +size_t position = 0; + +char *program = "#include \n" \ + "\n" \ + "int main(void)\n" \ + "{\n" \ + "\tprintf(\"Hello World\\n\");\n" \ + "\n" \ + "\treturn 0;\n" \ + "}"; + +char *usage = "%sUsage:\n" \ + "\t%s "; + +void print_screen(void) +{ + mvaddnstr(0, 0, program, position); +} + +int getch_nodelay(void) +{ + int retval; + + nodelay(stdscr, TRUE); + retval = getch(); + nodelay(stdscr, FALSE); + + return retval; +} + + +void poll_input(void) +{ + int c = getch(); + + if (c == 27 && getch_nodelay() == ERR) { + running = false; + return; + } + + if (program[position] == c) + position++; +} + +int main(int argc, char **argv) +{ + FILE *file; + + if (argc != 2) + error(1, 0, usage, "Incorrect number of arguments\n", + program_invocation_name); + + cbreak(); + initscr(); + noecho(); + + intrflush(stdscr, FALSE); + + print_screen(); + while (running) { + poll_input(); + print_screen(); + } + + endwin(); + + if (file = fopen(argv[1], "w"), file == NULL) + error(1, errno, "Could not access %s", argv[1]); + + fwrite(program, 1, position, file); + + fclose(file); + + printf("Saved.\n"); + + return 0; +} -- cgit v1.2.3-54-g00ecf