Updated and fixed edit app; system call sys_getch () was rewrited for using memory...
[ZeXOS.git] / apps / edit / main.c
blob79d50dde1a2f16678f226571e09d16a529f8707c
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
26 #define ESC 1
27 #define ARROWLEFT 75
28 #define ARROWRIGHT 77
29 #define ARROWUP 72
30 #define ARROWDOWN 80
32 #define F11 87
34 #define VERSION "0.0.5"
36 int cursor_x, cursor_y;
37 unsigned cur_pos;
38 char *buffer_cache;
39 int fd;
41 void cursor_refresh ()
43 gotoxy (cursor_x, cursor_y+3);
46 unsigned key_handler ()
48 int scancode = getkey ();
50 switch (scancode) {
51 case ESC:
52 exit (1);
53 return 0;
54 case ARROWLEFT:
55 if (cursor_x > 0)
56 cursor_x --;
57 cursor_refresh ();
58 return 1;
59 case ARROWRIGHT:
60 if (cursor_x < 79)
61 cursor_x ++;
62 cursor_refresh ();
63 return 1;
64 case ARROWUP:
65 if (cursor_y > 0)
66 cursor_y --;
67 cursor_refresh ();
68 return 1;
69 case ARROWDOWN:
70 if (cursor_y < 21)
71 cursor_y ++;
72 cursor_refresh ();
73 return 1;
74 case F11:
75 gotoxy (36, 2);
76 puts ("Saved !\n");
77 gotoxy (cursor_x, cursor_y+3);
79 write (fd, buffer_cache, 512);
80 return 1;
83 return 0;
88 int main (int argc, char **argv) // like main in a normal C program
90 cls ();
92 cursor_x = 0;
93 cursor_y = 0;
95 setcolor (7, 1);
97 putch (201);
98 unsigned p;
99 for (p = 1; p < 79; p ++)
100 putch (205);
102 putch (187);
105 putch (186);
106 setcolor (15, 1);
108 for (p = 1; p < 79; p ++)
109 putch (' ');
111 setcolor (7, 1);
112 putch (186);
114 putch (200);
116 for (p = 1; p < 79; p ++)
117 putch (205);
119 putch (188);
121 setcolor (10, 0);
123 buffer_cache = (char *) malloc (sizeof (char) * 512);
125 if (!buffer_cache)
126 return 0;
128 if (argc > 1) {
129 fd = open (argv[1], O_RDONLY);
131 if (!fd) {
132 puts ("error -> file '");
133 puts (argv[1]);
134 puts ("' not found\n");
135 return 0;
138 if (!read (fd, buffer_cache, 512)) {
139 puts ("error -> something was wrong !\n");
140 return 0;
143 puts (buffer_cache);
146 setcolor (15, 1);
147 gotoxy (30, 1);
148 puts ("Text editor v" VERSION);
150 cursor_refresh ();
153 setcolor (10, 0);
155 puts (buffer_cache);
157 cursor_x = 0;
158 cursor_y = 0;
160 unsigned char key = 0;
161 unsigned ref = 0;
162 while (1) {
163 key_handler ();
165 key = getch ();
167 if (key == '\n') {
168 cursor_y ++;
170 cursor_refresh ();
171 } else if (key == '\b') {
172 cursor_x --;
174 if (cursor_x < 0) {
175 cursor_x = 0;
176 cursor_y --;
179 if (cursor_y < 0)
180 cursor_y = 0;
182 putch (key);
183 //cursor_refresh ();
184 } else if (key) {
185 cursor_x ++;
187 if (cursor_x > 80) {
188 cursor_x = 0;
189 cursor_y ++;
192 if (cursor_y > 21)
193 cursor_y = 21;
195 //buffer_cache[cursor_x + (cursor_y * 80)] = key;
197 putch (key);
200 schedule ();
203 return 0;