App inst fixed on invalid image file; App zinstall for ZDE works - we've got GUI...
[ZeXOS.git] / apps / edit / main.c
blobf29dd42a0da1eb9932adca0c39e7ff451ee824f5
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
29 #define ESC 1
30 #define ARROWLEFT 75
31 #define ARROWRIGHT 77
32 #define ARROWUP 72
33 #define ARROWDOWN 80
35 #define VERSION "0.1.0"
37 #define MENU_REQUEST_OPEN 0
38 #define MENU_REQUEST_CLOSE 1
41 #define KBD_BUF_LEN 32
43 #define LINE_MAX_LEN 512
46 typedef struct line_context {
47 struct line_context *next, *prev;
48 char *buffer;
49 unsigned short len;
50 } line_t;
51 line_t line_list;
53 typedef struct {
54 short csr_x;
55 short csr_y;
56 char buffer[LINE_MAX_LEN];
57 unsigned short len;
58 } page_t;
59 page_t page;
61 #ifdef LINUX
62 void gotoxy ()
67 void cls ()
72 void schedule ()
76 #endif
78 unsigned quit;
80 unsigned cmd_handler (char *buffer, unsigned len);
82 line_t *line_add (char *buf, unsigned len)
84 if (cmd_handler (buf, len))
85 return 0;
87 /* alloc and init context */
88 line_t *line = (line_t *) malloc (sizeof (line_t));
90 if (!line)
91 return 0;
93 line->buffer = len ? strndup (buf, len) : 0;
94 line->len = len;
96 /* add into list */
97 line->next = &line_list;
98 line->prev = line_list.prev;
99 line->prev->next = line;
100 line->next->prev = line;
102 return line;
105 int line_print (line_t *line)
107 if (line->buffer)
108 puts (line->buffer);
109 else
110 putchar ('\n');
112 return 0;
115 int line_printall ()
117 line_t *line;
118 for (line = line_list.next; line != &line_list; line = line->next)
119 line_print (line);
121 return 0;
124 int file_load (char *name)
126 FILE *f = fopen (name, "r");
128 if (!f) {
129 printf ("err: %s\n", name);
130 return -1;
133 for (;;) {
134 char *r = fgets (page.buffer, LINE_MAX_LEN, f);
136 if (!r)
137 break;
139 unsigned l = strlen (page.buffer);
141 if (l >= LINE_MAX_LEN)
142 l = LINE_MAX_LEN-1;
144 line_add (page.buffer, l-1);
147 fclose (f);
149 return 0;
152 int file_save (char *name)
154 FILE *f = fopen (name, "w");
156 if (!f) {
157 printf ("err: %s\n", name);
158 return -1;
161 unsigned len = 0;
163 line_t *line;
164 for (line = line_list.next; line != &line_list; line = line->next)
165 len += line->len + 1;
167 char *p = (char *) malloc (sizeof (char) * (len + 1));
169 if (!p) {
170 fclose (f);
171 return -1;
174 len = 0;
175 for (line = line_list.next; line != &line_list; line = line->next) {
176 memcpy (p+len, line->buffer, line->len);
177 p[len+line->len] = '\n';
178 len += line->len + 1;
181 int r = fwrite (p, len, 1, f);
183 if (r < 1) {
184 printf ("ERROR -> fwrite ()\n");
187 free (p);
189 fclose (f);
191 return 0;
194 unsigned cmd_handler (char *buffer, unsigned len)
196 if (len >= 4) {
197 if (!strncmp (buffer, ":quit", len)) {
198 quit = 1;
199 return 1;
202 if (!strncmp (buffer, ":save", 4)) {
203 if (len > 6) {
204 if (buffer[5] != ' ')
205 return 1;
207 //creat (buffer+6, O_CREAT);
209 file_save (buffer+6);
210 printf ("Saved: '%s'\n", buffer+6);
213 return 1;
217 return 0;
220 void cursor_update (unsigned short x, unsigned short y)
222 gotoxy (x, y);
225 char kbd_getchar ()
227 char c = getchar ();
229 if (c == -1)
230 return 0;
232 switch (c) {
233 case '\b':
234 if (page.csr_x > 0)
235 page.csr_x -= 1;
237 if (page.csr_x > 79)
238 page.csr_x = 79;
240 if (page.len > 0)
241 page.len --;
243 //cursor_update (page.csr_x, page.csr_y);
244 return 0;
245 case '\n':
246 page.csr_y ++;
247 page.csr_x = 0;
248 page.buffer[page.len] = '\0';
250 line_add (page.buffer, page.len);
252 memset (page.buffer, 0, page.len);
253 page.len = 0;
255 //cursor_update (page.csr_x, page.csr_y);
256 return 0;
257 default:
258 page.csr_x ++;
259 page.len ++;
261 break;
264 return c;
267 unsigned text_handler ()
269 char c = kbd_getchar ();
271 if (c)
272 page.buffer[page.csr_x - 1] = c;
274 return c ? 1 : 0;
277 int loop ()
279 for (;;) {
280 text_handler ();
282 schedule ();
284 if (quit)
285 break;
288 return 0;
291 int init ()
293 quit = 0;
295 page.csr_x = 0;
296 page.csr_y = 0;
298 page.len = 0;
300 line_list.next = &line_list;
301 line_list.prev = &line_list;
303 cls ();
305 return 0;
308 int main (int argc, char **argv)
310 init ();
312 int oldFlag = fcntl (1, F_GETFL, 0);
313 if (fcntl (1, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
314 printf ("Cant set socket to nonblocking mode\n");
315 return 0;
318 if (argc > 1)
319 file_load (argv[1]);
321 line_printall ();
323 loop ();
325 return 0;