tape-kernel
1.0
a modular modern independent kernel
Loading...
Searching...
No Matches
editor.c
Go to the documentation of this file.
1
//src/usr/editor.c - line-based text editor
2
#include "
../io/vga.h
"
3
#include "
../lib/utils.h
"
4
#include "
editor.h
"
5
#include "
../io/cm.h
"
6
#include "
../fs/fs.h
"
7
#include "
../io/kb.h
"
8
#include "
../lib/pit.h
"
9
10
#define MAX_LINES 32
11
#define MAX_LINE_LEN 80
12
13
extern
arena_t
*
fs_arena
;
14
15
static
char
lines
[
MAX_LINES
][
MAX_LINE_LEN
+ 1];
16
static
int
line_count
= 0;
17
18
/**
19
* @brief editor_display renders the editor interface to the screen
20
*
21
* editor_display is a static function that clears the screen and
22
* draws the editor header, hotkeys bar, and all current lines
23
* @param filename, the name of the file being edited
24
*
25
* @see editor(), editor_load()
26
*/
27
static
void
editor_display
(
const
char
*filename) {
28
clscr
();
29
prt
(0, 0, filename, 0x0F);
30
prt
(0, 1,
"[e] edit [w] save [q] quit"
, 0x0E);
31
32
for
(
int
i = 0; i <
line_count
&& i < 20; i++) {
33
if
(
lines
[i][0] !=
'\0'
) {
34
prt
(0, i + 3,
lines
[i], 0x07);
35
}
36
}
37
38
cob
(0, 23);
39
}
40
41
/**
42
* @brief editor_load loads a files contents into the editor buffer
43
*
44
* editor_load is a static function that reads a file from the
45
* filesystem and splits its content into lines for editing
46
* @param filename, the name of the file to load
47
*
48
* @see editor_display(), editor_save(), fsread()
49
*/
50
static
void
editor_load
(
const
char
*filename) {
51
char
*content =
fsread
(filename);
52
int
line = 0;
53
int
pos = 0;
54
55
//clear lines
56
for
(
int
i = 0; i <
MAX_LINES
; i++) {
57
lines
[i][0] =
'\0'
;
58
}
59
60
if
(content !=
NULL
&& content[0] !=
'\0'
) {
61
for
(
int
i = 0; content[i] !=
'\0'
&& line <
MAX_LINES
; i++) {
62
if
(content[i] ==
'\n'
) {
63
lines
[line][pos] =
'\0'
;
64
line++;
65
pos = 0;
66
}
else
if
(pos <
MAX_LINE_LEN
- 1) {
67
lines
[line][pos++] = content[i];
68
}
69
}
70
if
(pos > 0) {
71
lines
[line][pos] =
'\0'
;
72
line++;
73
}
74
}
75
76
line_count
= (line == 0) ? 1 : line;
77
}
78
79
/**
80
* @brief editor_save writes the editor buffer to the filesystem
81
*
82
* editor_save is a static function that joins all lines with
83
* newlines and writes the combined content to disk as a file
84
* @param filename, the name of the file to save to
85
*
86
* @see editor_load(), fswrite()
87
*/
88
static
void
editor_save
(
const
char
*filename) {
89
char
buffer[
MAX_LINES
*
MAX_LINE_LEN
];
90
uint32_t
pos = 0;
91
92
for
(
int
i = 0; i <
line_count
; i++) {
93
for
(
int
j = 0;
lines
[i][j] !=
'\0'
&& pos <
sizeof
(buffer) - 2; j++) {
94
buffer[pos++] =
lines
[i][j];
95
}
96
if
(i <
line_count
- 1 && pos <
sizeof
(buffer) - 2) {
97
buffer[pos++] =
'\n'
;
98
}
99
}
100
buffer[pos] =
'\0'
;
101
102
if
(pos > 0) {
103
fswrite
(
fs_arena
, filename, buffer);
104
prt
(0, 24,
"saved! "
, 0x0A);
105
}
else
{
106
prt
(0, 24,
"empty, not saved "
, 0x0C);
107
}
108
}
109
110
/**
111
* @brief a simple multiline text editor
112
*
113
* to call this editor simply use
114
* @code{.c}
115
* #include "../usr/editor.h" //or simply editor.h
116
* editor(filename); //fill in the file to edit
117
* @endcode
118
* and it will automatically break back to your loop or func (not exit)
119
*/
120
void
__editor
(
const
char
*filename) {
121
if
(filename ==
NULL
|| filename[0] ==
'\0'
) {
122
prt
(0, 0,
"editor: no filename"
, 0x0C);
123
return
;
124
}
125
126
editor_load
(filename);
127
128
while
(1) {
129
editor_display
(filename);
130
131
uint8_t
key =
gtchr
();
132
133
if
(key ==
'e'
) {
134
clscr
();
135
prt
(0, 0,
"enter each line, empty line to stop"
, 0x0F);
136
137
line_count
= 0;
138
for
(
int
i = 0; i <
MAX_LINES
; i++) {
139
prt
(0, i + 3,
"> "
, 0x0E);
140
cob
(2, i + 3);
141
142
rdln
(
lines
[i],
MAX_LINE_LEN
);
143
144
if
(
lines
[i][0] ==
'\0'
) {
145
break
;
146
}
147
line_count
++;
148
}
149
150
if
(
line_count
== 0) {
151
lines
[0][0] =
'\0'
;
152
line_count
= 1;
153
}
154
}
155
else
if
(key ==
'w'
) {
156
editor_save
(filename);
157
delay
(100);
158
}
159
else
if
(key ==
'q'
) {
160
break
;
161
}
162
}
163
164
clscr
();
165
cob
(0, 0);
166
}
cm.h
cob
#define cob
Definition
cm.h:7
MAX_LINES
#define MAX_LINES
Definition
editor.c:10
MAX_LINE_LEN
#define MAX_LINE_LEN
Definition
editor.c:11
editor_load
static void editor_load(const char *filename)
editor_load loads a files contents into the editor buffer
Definition
editor.c:50
lines
static char lines[32][80+1]
Definition
editor.c:15
__editor
void __editor(const char *filename)
a simple multiline text editor
Definition
editor.c:120
editor_save
static void editor_save(const char *filename)
editor_save writes the editor buffer to the filesystem
Definition
editor.c:88
line_count
static int line_count
Definition
editor.c:16
editor_display
static void editor_display(const char *filename)
editor_display renders the editor interface to the screen
Definition
editor.c:27
editor.h
fs.h
fsread
#define fsread
Definition
fs.h:14
fswrite
#define fswrite
Definition
fs.h:15
kb.h
gtchr
#define gtchr
Definition
kb.h:6
fs_arena
arena_t * fs_arena
the filesystem child arena
Definition
main.c:56
pit.h
delay
#define delay
Definition
pit.h:8
arena_t
the arena_t type
Definition
heap.h:17
NULL
#define NULL
Definition
types.h:38
uint32_t
unsigned int uint32_t
Definition
types.h:30
uint8_t
unsigned char uint8_t
Definition
types.h:28
utils.h
vga.h
prt
#define prt
Definition
vga.h:6
clscr
#define clscr
Definition
vga.h:7
rdln
#define rdln
Definition
vga.h:9
src
usr
editor.c
Generated by
1.16.1