aboutsummaryrefslogtreecommitdiff
path: root/core/vi.c
blob: 97fa02f8a70eae7051e80115b149f3e6bba3a7d9 (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
/*	vi/ex - visual/text editor
 *	Copyright (C) 2022 Ferass EL HAFIDI
 *
 *	Redistribution and use in source and binary forms, with or without 
 *	modification, are permitted provided that the following conditions are met:
 *	
 *	    1. Redistributions of source code must retain the above copyright notice, 
 *	       this list of conditions and the following disclaimer.
 *	    2. Redistributions in binary form must reproduce the above copyright 
 *	       notice, this list of conditions and the following disclaimer in the 
 *	       documentation and/or other materials provided with the distribution.
 *	    3. Neither the name of the copyright holder nor the names of its 
 *	       contributors may be used to endorse or promote products derived from 
 *	       this software without specific prior written permission.
 *	
 *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 *	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 *	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
 *	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 *	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 *	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 *	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 *	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 *	POSSIBILITY OF SUCH DAMAGE.
 */

#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[BUFSIZ], struct cursor_pos cposition, long int offset_position, 
		FILE *filstr);
void _vi_printUsage();
int _vi_print_buffer_line(char s[BUFSIZ], unsigned int numlines);

int _vi_main(int argc, char *argv[]) {
	int argument, offset;
	char cmd;
	FILE *filstr;
	char buffer[BUFSIZ];
	struct sigaction signal_action;
	struct termios oldattr, newattr;
	struct winsize w;
	struct cursor_pos cposition, fposition;
	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 (fposition.y > 0) fposition.y--;
				else break;
				if (cposition.y != 1)
					_vi_set_cursor_pos(cposition.x, (cposition.y = cposition.y - 1));
				else if (fposition.y == 0) {
					_vi_set_cursor_pos(1, 1);
					print_string(ANSI_CLEARALL); /* Clear screen */
					fposition.y--; /* Say, 118 */
					for (unsigned int i = lines - 1 /* 79 */; i > 0; i--) {
						_vi_print_buffer_line(buffer, fposition.y - i); /* 120 - 78 */
					}
					_vi_set_cursor_pos(cposition.x, cposition.y);
				}
				break;
			case 'j': /* down */
				if (cposition.y != (lines - 1) && fposition.y++)
					_vi_set_cursor_pos(cposition.x, (cposition.y = cposition.y + 1));
				else if (cposition.y == lines - 1) {
					_vi_set_cursor_pos(1, 1);
					print_string(ANSI_CLEARALL); /* Clear screen */
					fposition.y++; /* Say, 120 */
					for (unsigned int i = lines - 1 /* 79 */; i > 0; i--) {
						_vi_print_buffer_line(buffer, fposition.y - i); /* 120 - 78 */
					}
					_vi_set_cursor_pos(cposition.x, cposition.y);
				}
				break;
			case 'l': /* right */
				if (cposition.x != columns && fposition.x++)
					_vi_set_cursor_pos((cposition.x = cposition.x + 1), cposition.y);
				break;
			case 'h': /* left */
				if (cposition.x != 1 && fposition.x--)
					_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[BUFSIZ], struct cursor_pos cposition, 
		long int offset_position, FILE *filstr) {
	int status = 0;
	print_string(ANSI_CLEARALL); /* Clear screen */
	_vi_set_cursor_pos(1, 1);
	if (filstr) {
		fread(buffer, BUFSIZ, 1, filstr);
		//if (fgets(buffer, 4096, filstr) != NULL) {
		//	offset_position = ftell(filstr);
		//	fputs(buffer, stdout);
		//}
		for (unsigned int i = 0; i < lines - 1; i++)
			_vi_print_buffer_line(buffer, i);
	} 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;
}

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);
}

int _vi_print_buffer_line(char s[BUFSIZ], unsigned int numlines) {
	unsigned int lines_found, chars;
	/* Search for <numlines> - 1 lines and skip them entirely. */
	for (chars = 0, lines_found = 0; 
			s[chars] && lines_found < numlines; s[chars] == '\n' && lines_found++, chars++)
		/* TODO: Use strchr(). */ ;
	/* Print the last line found. */
	for (; s[chars] && s[chars] != '\n'; chars++) putchar((int)s[chars])
		/* TODO: Do not print character-by-character. */ ;
	putchar((int)'\n');
	return 0;
}

/************ 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. */
}