-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.c
131 lines (112 loc) · 2.21 KB
/
cmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "cedit.h"
/*
* creates new Cmd structure and draws it
*/
void cmd_new(char *msg, char *type, char input)
{
size_t c;
size_t h;
CMD = newCmd();
CMD->msg = msg;
CMD->type = type;
CMD->input = input;
draw_all();
}
/*
* deletes the current Cmd structure
*/
void cmd_del()
{
free(CMD->c);
free(CMD);
CMD = 0;
draw_all();
}
/*
* executes and deletes the current Cmd structure
*/
void cmd_exec()
{
char *buf;
char *type;
size_t num;
type = CMD->type;
if(strlen(CMD->c) > 0){
buf = malloc(strlen(CMD->c));
buf[0] = '\0';
strncpy(buf, CMD->c, strlen(CMD->c));
if(CMD->input == 1) num = atoi(buf);
} else {
num = 0;
buf = 0;
}
cmd_del();
if (type == "savefile") file_save(buf);
if (type == "openfile") file_open(buf);
free(buf);
}
/*
* adds an utf-8 character to the current Cmd struct by shifting all characters
* from the right side of the cursor len to the right and writing in the gap
* aftwards
*/
void cmd_input_add(uint32_t ch)
{
char buf[6];
size_t i;
size_t len;
size_t bpos;
len = tb_utf8_unicode_to_char(buf, ch);
bpos = utf8_bytepos(CMD->c, CMD->cur, CMD->blen);
cmd_ensure_cap(CMD->blen+len);
for(i = CMD->blen+len; i >= bpos+len; i--) CMD->c[i] = CMD->c[i-len];
for(i = bpos; i < bpos+len; i++) CMD->c[i] = buf[i-bpos];
CMD->blen += len;
CMD->clen += 1;
CMD->cur += 1;
draw_all();
}
/*
* removes an utf-8 character from the current Cmd struct by shifting all
* characters from the right side of the cursor len to the left overwriting the
* character
*/
void cmd_input_del()
{
size_t i;
size_t len;
size_t bpos;
bpos = utf8_bytepos(CMD->c, CMD->cur-1, CMD->blen);
len = tb_utf8_char_length(CMD->c[bpos]);
for(i = bpos; i < CMD->blen; i++) CMD->c[i] = CMD->c[i+len];
CMD->blen -= len;
CMD->clen -= 1;
CMD->cur -= 1;
draw_all();
}
/*
* cursor handling: right, left
*/
void cmd_input_left()
{
if(0 < CMD->cur) CMD->cur--;
draw_all();
}
void cmd_input_right()
{
if(CMD->clen > CMD->cur) CMD->cur++;
draw_all();
}
/*
* ensures that there are always enough bytes to write new content to
*/
void cmd_ensure_cap(size_t cap)
{
size_t i;
i = 0;
while (CMD->mlen+i < cap) i += LINESIZE;
if (i > 0){
CMD->mlen += i;
CMD->c = realloc(CMD->c,CMD->mlen);
}
}