aboutsummaryrefslogtreecommitdiffstats
path: root/editor.c
blob: afea7969f84e6b53967a67cdf3718bb814f9ff29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;
}