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/>.
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
;
56 char buffer
[LINE_MAX_LEN
];
80 unsigned cmd_handler (char *buffer
, unsigned len
);
82 line_t
*line_add (char *buf
, unsigned len
)
84 if (cmd_handler (buf
, len
))
87 /* alloc and init context */
88 line_t
*line
= (line_t
*) malloc (sizeof (line_t
));
93 line
->buffer
= len
? strndup (buf
, len
) : 0;
97 line
->next
= &line_list
;
98 line
->prev
= line_list
.prev
;
99 line
->prev
->next
= line
;
100 line
->next
->prev
= line
;
105 int line_print (line_t
*line
)
118 for (line
= line_list
.next
; line
!= &line_list
; line
= line
->next
)
124 int file_load (char *name
)
126 FILE *f
= fopen (name
, "r");
129 printf ("err: %s\n", name
);
134 char *r
= fgets (page
.buffer
, LINE_MAX_LEN
, f
);
139 unsigned l
= strlen (page
.buffer
);
141 if (l
>= LINE_MAX_LEN
)
144 line_add (page
.buffer
, l
-1);
152 int file_save (char *name
)
154 FILE *f
= fopen (name
, "w");
157 printf ("err: %s\n", name
);
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));
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
);
184 printf ("ERROR -> fwrite ()\n");
194 unsigned cmd_handler (char *buffer
, unsigned len
)
197 if (!strncmp (buffer
, ":quit", len
)) {
202 if (!strncmp (buffer
, ":save", 4)) {
204 if (buffer
[5] != ' ')
207 //creat (buffer+6, O_CREAT);
209 file_save (buffer
+6);
210 printf ("Saved: '%s'\n", buffer
+6);
220 void cursor_update (unsigned short x
, unsigned short y
)
243 //cursor_update (page.csr_x, page.csr_y);
248 page
.buffer
[page
.len
] = '\0';
250 line_add (page
.buffer
, page
.len
);
252 memset (page
.buffer
, 0, page
.len
);
255 //cursor_update (page.csr_x, page.csr_y);
267 unsigned text_handler ()
269 char c
= kbd_getchar ();
272 page
.buffer
[page
.csr_x
- 1] = c
;
300 line_list
.next
= &line_list
;
301 line_list
.prev
= &line_list
;
308 int main (int argc
, char **argv
)
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");