Version 0.6.7: Added support for hostname - hostname_get (), hostname_set (); Hostnam...
[ZeXOS.git] / apps / edit / main.c
blob314a87f69342d1642346b84f3e97b024da569726
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
28 #define ESC 1
29 #define ARROWLEFT 75
30 #define ARROWRIGHT 77
31 #define ARROWUP 72
32 #define ARROWDOWN 80
34 #define VERSION "0.0.7"
36 #define MENU_REQUEST_OPEN 0
37 #define MENU_REQUEST_CLOSE 1
40 #define KBD_BUF_LEN 32
42 #define LINE_MAX_LEN 512
45 typedef struct line_context {
46 struct line_context *next, *prev;
47 char *buffer;
48 unsigned short len;
49 } line_t;
50 line_t line_list;
52 typedef struct {
53 short csr_x;
54 short csr_y;
55 char buffer[LINE_MAX_LEN];
56 unsigned short len;
57 } page_t;
58 page_t page;
60 unsigned quit;
62 unsigned cmd_handler (char *buffer, unsigned len);
64 line_t *line_add (char *buf, unsigned len)
66 if (!len)
67 return 0;
69 if (cmd_handler (buf, len))
70 return 0;
72 line_t *line;
74 /* alloc and init context */
75 line = (line_t *) malloc (sizeof (line_t));
77 if (!line)
78 return 0;
80 line->buffer = strndup (buf, len);
81 line->len = len;
83 /* add into list */
84 line->next = &line_list;
85 line->prev = line_list.prev;
86 line->prev->next = line;
87 line->next->prev = line;
89 return line;
92 int line_print (line_t *line)
94 printf (line->buffer);
96 return 0;
99 int line_printall ()
101 line_t *line;
102 for (line = line_list.next; line != &line_list; line = line->next)
103 line_print (line);
105 return 0;
108 int file_load (char *name)
110 FILE *f = fopen (name, "r");
112 if (!f) {
113 printf ("err: %s\n", name);
114 return -1;
117 while (1) {
118 char *r = fgets (page.buffer, LINE_MAX_LEN, f);
120 if (!r)
121 break;
123 unsigned l = strlen (page.buffer);
125 if (l >= LINE_MAX_LEN)
126 l = LINE_MAX_LEN-1;
128 line_add (page.buffer, l);
131 fclose (f);
133 return 0;
136 int file_save (char *name)
138 FILE *f = fopen (name, "w");
140 if (!f) {
141 printf ("err: %s\n", name);
142 return -1;
145 unsigned len = 0;
147 line_t *line;
148 for (line = line_list.next; line != &line_list; line = line->next)
149 len += line->len + 1;
151 char *p = (char *) malloc (sizeof (char) * (len + 1));
153 if (!p) {
154 fclose (f);
155 return -1;
158 len = 0;
159 for (line = line_list.next; line != &line_list; line = line->next) {
160 memcpy (p+len, line->buffer, line->len);
161 p[len+line->len] = '\n';
162 len += line->len + 1;
165 int r = fwrite (p, len, 1, f);
167 if (r < 1) {
168 printf ("ERROR -> fwrite ()\n");
171 free (p);
173 fclose (f);
175 return 0;
178 unsigned cmd_handler (char *buffer, unsigned len)
180 if (len >= 4) {
181 if (!strncmp (buffer, ":quit", len)) {
182 quit = 1;
183 return 1;
186 if (!strncmp (buffer, ":save", 4)) {
187 if (len > 6) {
188 if (buffer[5] != ' ')
189 return 1;
191 creat (buffer+6, O_CREAT);
193 file_save (buffer+6);
194 printf ("Saved: '%s'\n", buffer+6);
197 return 1;
201 return 0;
204 void cursor_update (unsigned short x, unsigned short y)
206 gotoxy (x, y);
209 char kbd_getchar ()
211 char c = getchar ();
213 if (c == -1)
214 return 0;
216 switch (c) {
217 case '\b':
218 if (page.csr_x > 0)
219 page.csr_x -= 1;
221 if (page.csr_x > 79)
222 page.csr_x = 79;
224 if (page.len > 0)
225 page.len --;
227 //cursor_update (page.csr_x, page.csr_y);
228 return 0;
229 case '\n':
230 page.csr_y ++;
231 page.csr_x = 0;
232 page.buffer[page.len] = '\0';
234 line_add (page.buffer, page.len);
236 memset (page.buffer, 0, page.len);
237 page.len = 0;
239 //cursor_update (page.csr_x, page.csr_y);
240 return 0;
241 default:
242 page.csr_x ++;
243 page.len ++;
245 break;
248 return c;
251 unsigned text_handler ()
253 char c = kbd_getchar ();
255 if (c)
256 page.buffer[page.csr_x - 1] = c;
258 return c ? 1 : 0;
261 int loop ()
263 for (;;) {
264 text_handler ();
266 schedule ();
268 if (quit)
269 break;
272 return 0;
275 int init ()
277 quit = 0;
279 page.csr_x = 0;
280 page.csr_y = 0;
282 page.len = 0;
284 line_list.next = &line_list;
285 line_list.prev = &line_list;
287 cls ();
289 return 0;
292 int main (int argc, char **argv)
294 init ();
296 int oldFlag = fcntl (1, F_GETFL, 0);
297 if (fcntl (1, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
298 printf ("Cant set socket to nonblocking mode\n");
299 return 0;
302 //file_load ("file");
304 line_printall ();
306 loop ();
308 return 0;