aboutsummaryrefslogtreecommitdiff
path: root/core/vi.c
blob: 9335e5cd5bc514da9546aa588e246b539d6e7374 (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*	vi/ex - visual/text editor
 *	Copyright (C) 2022 Ferass EL HAFIDI
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <libgen.h>
#include "version.h"

#ifndef COMPILETIME
#define COMPILETIME
#endif

/*** ANSI VT100 escape codes ***/
/*           Colors            */
#define ANSI_WHITEBG "\33[38;5;0;48;5;255m"
#define ANSI_RESET   "\33[m"
/*     Other escape codes      */
#define ANSI_CLEARLINE "\33[2K\r"
#define ANSI_CLEARALL  "\e[1;1H\e[2J"

/********** Common - used by both vi and ex **********/
int print_string(char *string);

int print_string(char *string) {
	return write(STDOUT_FILENO, string, strlen(string));
}

/********************** ex mode **********************/
int _ex_main(int argc, char *argv[]);

int _ex_main(int argc, char *argv[]) {
	print_string("<Not implemented>\n");
	return 0; /* Not implemented yet. */
}

/********************** vi mode **********************/
struct cursor_pos {
	/* Neat way to keep track of the cursor's position. */
	unsigned int x;
	unsigned int y;
};

int  _vi_main(int argc, char *argv[]);
int  _vi_set_cursor_pos(unsigned int x, unsigned int y);
int  _vi_init(unsigned int lines, unsigned int columns, 
		char buffer[4096], struct cursor_pos cposition, long int offset_position, 
		FILE *filstr);
long int _vi_go_up(unsigned int lines, unsigned int columns, 
		char buffer[4096], FILE *filstr, long int offset_position);
long int _vi_go_down(unsigned int lines, unsigned int columns, 
		char buffer[4096], FILE *filstr, long int offset_position);
void _vi_printUsage();

int _vi_main(int argc, char *argv[]) {
	int argument, offset;
	char cmd;
	FILE *filstr;
	char buffer[4096];
	struct sigaction signal_action;
	struct termios oldattr, newattr;
	struct winsize w;
	struct cursor_pos cposition;
	ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
	unsigned int lines   = w.ws_row - 1;
	unsigned int columns = w.ws_col - 1;

	while ((argument = getopt(argc, argv, "")) != -1) {
		if (argument == '?') {
			_vi_printUsage();
			return 1;
		}
	}
	argc -= optind;
	argv += optind;

	signal_action.sa_handler = SIG_IGN;
	sigemptyset(&signal_action.sa_mask);
	sigaction(SIGINT, &signal_action, NULL);
	setvbuf(stdout, NULL, _IONBF, 0);
	/* Initialise vi */
	if (argv[0]) {
		filstr = fopen(argv[0], "r+");
		if (errno) {
			if (errno != ENOENT)
				return errno;
		}
	} _vi_init(lines, columns, buffer, cposition, offset, filstr);	
	for (;;) {
		/* Get a character */
		tcgetattr(0, &oldattr); /* Get previous terminal parameters */
		newattr = oldattr;
		newattr.c_lflag &= ~( ICANON | ECHO ); /* Disable ECHO and ICANON */
		tcsetattr(0, TCSANOW, &newattr); /* Set net parameters */
		cmd = getchar();
		switch (cmd) {
			/*********************** Moving the cursor ***********************/
			case 'k': /* up */
				if (cposition.y != 1)
					_vi_set_cursor_pos(cposition.x, (cposition.y = cposition.y - 1));
				else {
					_vi_set_cursor_pos(1, 1);
					offset = _vi_go_up(lines, columns, buffer, filstr, offset);	
					_vi_set_cursor_pos(cposition.x, cposition.y);
				}
				break;
			case 'j': /* down */
				if (cposition.y != lines - 1)
					_vi_set_cursor_pos(cposition.x, (cposition.y = cposition.y + 1));
				else if (!feof(filstr)) {
					_vi_set_cursor_pos(1, 1);
					offset = _vi_go_down(lines, columns, buffer, filstr, offset);	
					_vi_set_cursor_pos(cposition.x, cposition.y);
				}
				break;
			case 'l': /* right */
				if (cposition.x != columns)
					_vi_set_cursor_pos((cposition.x = cposition.x + 1), cposition.y);
				break;
			case 'h': /* left */
				if (cposition.x != 0)
					_vi_set_cursor_pos((cposition.x = cposition.x - 1), cposition.y);
				break;
			/************************* Command mode: *************************/
			case ':':
				_vi_set_cursor_pos(1, lines);
				print_string(ANSI_CLEARLINE ":");
				tcsetattr(0, TCSANOW, &oldattr); /* Restore old parameters */
				cmd = getchar();
				switch (cmd) { 
					case '\33':
						_vi_set_cursor_pos(1, lines);
						print_string(ANSI_CLEARLINE);
						break;
					case 'q':
						print_string(ANSI_CLEARALL); /* Clear screen */
						_vi_set_cursor_pos(1, 1); /* Reset the cursor position */
						if (filstr) fclose(filstr);
						return 0;
					case '\n':
						break;
					default:
						_vi_set_cursor_pos(1, lines);
						print_string("unknown command");
						break;
				}
				_vi_set_cursor_pos(cposition.x, cposition.y);
		}
		tcsetattr(0, TCSANOW, &oldattr); /* Restore old parameters */
	}
	return 0;
}

int _vi_init(unsigned int lines, unsigned int columns, 
		char buffer[4096], struct cursor_pos cposition, 
		long int offset_position, FILE *filstr) {
	int status = 0;
	print_string(ANSI_CLEARALL); /* Clear screen */
	_vi_set_cursor_pos(1, 0);
	if (filstr) {
		if (fgets(buffer, 4096, filstr) != NULL) {
			offset_position = ftell(filstr);
			fputs(buffer, stdout);
		}
		for (unsigned int i = 1; i < (lines - 1); i++) {
			if (fgets(buffer, 4096, filstr) != NULL) {
				fputs(buffer, stdout);
			}
			else {
				fputs("~\n", stdout);
				status = 1; /* EOF */
			}
		}
	} else {
		for (unsigned int i = 1; i < (lines ); i++)
			fputs("~\n", stdout);
		status = 2; /* No file */
	} _vi_set_cursor_pos((cposition.x = 1), (cposition.y = 1));
	return status;
}

long int _vi_go_up(unsigned int lines, unsigned int columns, 
		char buffer[4096], FILE *filstr, long int offset_position) {
	/* Really hacky stuff. TODO: Rewrite. */
	int status = 0, offset_one_line_before;
	print_string(ANSI_CLEARALL); /* Clear screen */
	rewind(filstr);
	if (ftell(filstr) == offset_position) return offset_position;
	for (; fgets(buffer, 4096, filstr) != NULL && 
			(ftell(filstr) != offset_position && 
			 (offset_one_line_before = ftell(filstr)));)
		/* Do nothing. */ ;
	for (unsigned int i = 0; i < (lines - 1); i++) {
		if (fgets(buffer, 4096, filstr) != NULL) {
			fputs(buffer, stdout);
		}
		else {
			fputs("~\n", stdout);
			status = 1; /* EOF */
		}
	}	/* Hacky hack */
	_vi_set_cursor_pos(1, lines);
	print_string(ANSI_CLEARLINE);
	return offset_one_line_before;
}

long int _vi_go_down(unsigned int lines, unsigned int columns, 
		char buffer[4096], FILE *filstr, long int offset_position) {
	int status = 0;
	print_string(ANSI_CLEARALL); /* Clear screen */
	fseek(filstr, offset_position, SEEK_SET);
	if (fgets(buffer, 4096, filstr) != NULL)
		offset_position = ftell(filstr);

	for (unsigned int i = 0; i < (lines); i++) {
		if (fgets(buffer, 4096, filstr) != NULL) {
			fputs(buffer, stdout);
		}
		else {
			fputs("~\n", stdout);
			status = 1; /* EOF */
		}
	}
	//fposition.start = ftell(filstr);
	//fposition_start_backup = ftell(filstr);
	//if (fgets(buffer, 4096, filstr) != NULL)
	//	fposition.end = ftell(filstr);
	/* Hacky hack */
	_vi_set_cursor_pos(1, lines);
	print_string(ANSI_CLEARLINE);
	return offset_position;
}

int _vi_set_cursor_pos(unsigned int x, unsigned int y) {
	return printf("\033[%u;%uH", (y), (x));
}

void _vi_printUsage() {
	printf("Ferass' Base System. (%s)\n\n"
	"Usage: vi [filename]\n\n"
	"Visual editor.\n\n", COMPILETIME);
}

/************ main() ************/
int main(int argc, char *argv[]) {
	if (!strcmp(basename(argv[0]), "ex")) return _ex_main(argc, argv);
	else /* Default to vi */              return _vi_main(argc, argv);
	return 0; /* Never reached. */
}