aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2015-05-25 22:53:55 +0100
committerTomasz Kramkowski <tk@the-tk.com>2015-05-25 22:53:55 +0100
commit7bf25fb8f0e4643a67894417a95d39e5901b1824 (patch)
treef22e3b7d9bdb20c0aafdcad7b534130a8c465edb
parent0f981889b3a4d62104383c5a5c0efa3a1e9f5261 (diff)
downloadc-stuff-7bf25fb8f0e4643a67894417a95d39e5901b1824.tar.gz
c-stuff-7bf25fb8f0e4643a67894417a95d39e5901b1824.tar.xz
c-stuff-7bf25fb8f0e4643a67894417a95d39e5901b1824.zip
editor.c: a joke, an editor which writes a c program
-rw-r--r--editor.c95
1 files changed, 95 insertions, 0 deletions
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 <tk@the-tk.com>
+ *
+ * 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 <errno.h>
+#include <error.h>
+#include <ncurses.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+bool running = true;
+size_t position = 0;
+
+char *program = "#include <stdio.h>\n" \
+ "\n" \
+ "int main(void)\n" \
+ "{\n" \
+ "\tprintf(\"Hello World\\n\");\n" \
+ "\n" \
+ "\treturn 0;\n" \
+ "}";
+
+char *usage = "%sUsage:\n" \
+ "\t%s <filename>";
+
+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;
+}