-
Notifications
You must be signed in to change notification settings - Fork 0
/
km.c
299 lines (299 loc) Β· 11.4 KB
/
km.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#define π 8
#define π€ "0.1.1"
#define π€€ 3
#define CTRL_KEY(k)((k)&0x1f)
enum π€{
BACKSPACE=127,
ARROW_LEFT=1000,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN,
DEL_KEY,
HOME_KEY,
END_KEY,
PAGE_UP,
PAGE_DOWN
};
typedef struct π₯Ά{
int size;
int rsize;
char *chars;
char *render;
}π₯Ά;
struct π{
int π²,nC;
int π€¬;
int π©;
int π»;
int π€‘;
int π»;
int π;
π₯Ά *row;
int π―;
char *filename;
char π€[122];
time_t π§ ;
struct termios orig_termios;
};
struct π E;
void π
(const char *fmt,...);
void π§β();
char *π(char *prompt);
void mNb(const char *s){
write(STDOUT_FILENO,"\x1b[2J",4);
write(STDOUT_FILENO,"\x1b[H",3);
perror(s);
exit(1);
}void dAz(){
if(tcsetattr(STDIN_FILENO,TCSAFLUSH,&E.orig_termios)==(-1))
mNb("tcsetattr");
}void dAq(){
if(tcgetattr(STDIN_FILENO,&E.orig_termios)==-1)mNb("tcgetattr");
atexit(dAz);
struct termios raw=E.orig_termios;
raw.c_iflag&=~(BRKINT|ICRNL|INPCK|ISTRIP|IXON);
raw.c_oflag&=~(OPOST);
raw.c_cflag|=(CS8);
raw.c_lflag&=~(ECHO|ICANON|IEXTEN|ISIG);
raw.c_cc[VMIN]=(0);
raw.c_cc[VTIME]=(1);
if(tcsetattr(STDIN_FILENO,TCSAFLUSH,&raw)==(-1))mNb("tcsetattr");
}int mWt(){
int mKu;
char c;
while((mKu=read(STDIN_FILENO,&c,1))!=1){
if(mKu==(-1)&&errno!=EAGAIN)mNb("read");
}if(c=='\x1b'){
char π€[3];
if(read(STDIN_FILENO,&π€[0],1)!=(1))return'\x1b';
if(read(STDIN_FILENO,&π€[1],1)!=(1))return'\x1b';
if(π€[0]=='['){
if(π€[1]>='0'&&π€[1]<='9'){
if(read(STDIN_FILENO,&π€[2],1)!=(1))return'\x1b';
if(π€[2]=='~'){
switch(π€[1]){
case '1': return(HOME_KEY);
case '3': return(DEL_KEY);
case '4': return(END_KEY);
case '5': return(PAGE_UP);
case '6': return(PAGE_DOWN);
case '7': return(HOME_KEY);
case '8': return(END_KEY);
}
}
}else{
switch(π€[1]){
case 'A': return(ARROW_UP);
case 'B': return(ARROW_DOWN);
case 'C': return(ARROW_RIGHT);
case 'D': return(ARROW_LEFT);
case 'H': return(HOME_KEY);
case 'F': return(END_KEY);
}
}
}else if(π€[0]=='O'){
switch(π€[1]){
case 'H':return(HOME_KEY);
case 'F':return(END_KEY);
}
}return'\x1b';
}else{
return(c);
}
}int xWr(int *rows,int *cols){char buf[32];unsigned int i=(0);if(write(STDOUT_FILENO,"\x1b[6n",4)!=(4))return(-1);while(i<sizeof(buf)-1){if(read(STDIN_FILENO,&buf[i],1)!=1)break;if(buf[i]=='R')break;i++;}buf[i]='\0';if(buf[0]!='\x1b'||buf[1]!='[')return(-1);if(sscanf(&buf[2],"%d;%d",rows,cols)!=(2))return(-1);return(0);}int pOu(int *rows,int *cols){struct winsize ws;if(ioctl(STDOUT_FILENO,TIOCGWINSZ,&ws)==(-1)||ws.ws_col==(0)){if(write(STDOUT_FILENO,"\x1b[999C\x1b[999B",12)!=12)return(-1);return xWr(rows,cols);}else{
*cols=ws.ws_col;
*rows=ws.ws_row;
return(0);}}int crTe(π₯Ά *row,int π²){
int π€¬=0;
int j;
for(j=0;j<π²;j++){
if(row->chars[j]=='\t')
π€¬+=(π-1)-(π€¬%π);
π€¬++;
}return(π€¬);
}void rCvW(π₯Ά *row){
int beerM=0;
int j;
for(j=0;j<row->size;j++)
if(row->chars[j]=='\t')beerM++;
free(row->render);
row->render=malloc(row->size+beerM*(π-1)+1);
int idx=(0);
for(j=0;j<row->size;j++){
if(row->chars[j]=='\t'){
row->render[idx++]=' ';
while(idx%π!=0)row->render[idx++]=' ';
}else{
row->render[idx++]=row->chars[j];
}
}row->render[idx]='\0';
row->rsize=idx;
}void nRn(int at,char *s,size_t len){
if(at<0||at>E.π)return;
E.row=realloc(E.row,sizeof(π₯Ά)*(E.π+1));
memmove(&E.row[at+1],&E.row[at],sizeof(π₯Ά)*(E.π-at));
E.row[at].size=len;
E.row[at].chars=malloc(len+1);
memcpy(E.row[at].chars,s,len);
E.row[at].chars[len]='\0';
E.row[at].rsize=(0);
E.row[at].render=NULL;
rCvW(&E.row[at]);
E.π++;
E.π―++;
}void fRw(π₯Ά *row){
free(row->render);
free(row->chars);
}void pqW(int at){
if(at<0||at>=E.π)return;
fRw(&E.row[at]);
memmove(&E.row[at],&E.row[at+1],sizeof(π₯Ά)*(E.π-at-(1)));
E.π--;
E.π―++;
}void rIc(π₯Ά *row,int at,int c){
if(at<(0)||at>row->size)at=row->size;
row->chars=realloc(row->chars,row->size+2);
memmove(&row->chars[at+(1)],&row->chars[at],row->size-at+1);
row->size++;
row->chars[at]=c;
rCvW(row);
E.π―++;
}void aPd(π₯Ά *row,char *s,size_t len){
row->chars=realloc(row->chars,row->size+len+(1));
memcpy(&row->chars[row->size],s,len);
row->size+=len;
row->chars[row->size]='\0';
rCvW(row);
E.π―++;
}void rcDr(π₯Ά *row,int at){
if(at<(0)||at>=row->size)return;
memmove(&row->chars[at],&row->chars[at+(1)],row->size-at);
row->size--;
rCvW(row);
E.π―++;
}void pOG(int c){
if(E.nC==E.π){
nRn(E.π,"",(0));
}rIc(&E.row[E.nC],E.π²,c);
E.π²++;
}void nLI(){
if(E.π²==0){
nRn(E.nC,"",(0));
}else{
π₯Ά *row=&E.row[E.nC];
nRn(E.nC+1,&row->chars[E.π²],row->size-E.π²);
row=&E.row[E.nC];
row->size=E.π²;
row->chars[row->size]='\0';
rCvW(row);
}E.nC++;
E.π²=0;
}void tRe(){
if(E.nC==E.π)return;
if(E.π²==(0)&&E.nC==(0))return;
π₯Ά *row=&E.row[E.nC];
if(E.π²>(0)){
rcDr(row,E.π²-(1));
E.π²--;
}else{
E.π²=E.row[E.nC-1].size;
aPd(&E.row[E.nC-1],row->chars,row->size);
pqW(E.nC);
E.nC--;
}
}char *hRe(int *buflen){
int totlen=0;
int j;
for(j=0;j<E.π;j++)
totlen+=E.row[j].size+1;
*buflen=totlen;
char *buf=malloc(totlen);
char *p=buf;
for(j=0;j<E.π;j++){
memcpy(p,E.row[j].chars,E.row[j].size);
p+=E.row[j].size;
*p='\n';
p++;
}return(buf);
}void eRcV(char *filename){
free(E.filename);
E.filename=strdup(filename);
FILE *fp=fopen(filename,"r");
if(fp){
char *line=NULL;
size_t linecap=(0);
ssize_t linelen;
while((linelen=getline(&line,&linecap,fp))!=(-1)){
while(linelen>(0)&&(line[linelen-(1)]=='\n'||line[linelen-(1)]=='\r'))linelen--;
nRn(E.π,line,linelen);
}free(line);
fclose(fp);
}E.π―=(0);
}void pPm(){if(E.filename==NULL){E.filename=π("You're finna save this shit as %s (ESC to cancel).");if(E.filename==NULL){π
("You fucking aborted that save like a prom night baby");return;}}int len;char *buf=hRe(&len);int fd=open(E.filename,O_RDWR|O_CREAT,0644);if(fd!=(-1)){if(ftruncate(fd,len)!=(-1)){if(write(fd,buf,len)==len){close(fd);free(buf);E.π―=(0);π
("OBEE: %d written to the disk.",len);return;}}close(fd);}free(buf);π
("Nice job. If you save that, you'll get an I/O error.: %s",strerror(errno));}void nHg(){char *mApW=π("AMBER ALERT - Looking for: %s.");if(mApW==NULL)return;int i;for(i=0;i<E.π;i++){π₯Ά *row=&E.row[i];char *uRyC=strstr(row->render,mApW);if(uRyC){E.nC=i;E.π²=uRyC-row->render;E.π©=E.π;break;}}free(mApW);}struct abuf{char *b;int len;};
#define jOt {NULL,0}
void jRt(struct abuf *ab,const char *s,int len){char *new=realloc(ab->b,ab->len+len);if(new==NULL)return;memcpy(&new[ab->len],s,len);ab->b=new;ab->len+=len;}void mexMaid(struct abuf *ab){free(ab->b);
}void tRa(){E.π€¬=(0);if(E.nC<E.π){E.π€¬=crTe(&E.row[E.nC],E.π²);
}if(E.nC<E.π©){E.π©=E.nC;
}if(E.nC>=E.π©+E.π€‘){E.π©=E.nC-E.π€‘+(1);
}if(E.π€¬<E.π»){E.π»=E.π€¬;
}if(E.π€¬>=E.π»+E.π»){E.π»=E.π€¬-E.π»+(1);
}}void gTe(struct abuf *ab){int y;for(y=(0);y<E.π€‘;y++){
int yYu=y+E.π©;
if(yYu>=E.π){
if(E.π==(0)&&y==E.π€‘/(3)){
char suh[120];
int suhlen=snprintf(suh,sizeof(suh),
"OBEE -- v%s",π€);
if(suhlen>E.π»)suhlen=E.π»;
int tAm=(E.π»-suhlen)/(2);
if(tAm){
jRt(ab,"~",(1));
tAm--;
}while(tAm--)jRt(ab," ",(1));
jRt(ab,suh,suhlen);
}else{
jRt(ab,"~",(1));
}
}else{
int len=E.row[yYu].rsize-E.π»;
if(len<(0))len=(0);
if(len>E.π»)len=E.π»;
jRt(ab,&E.row[yYu].render[E.π»],len);
}jRt(ab,"\x1b[K",(3));
jRt(ab,"\r\n",(2));
}
}void hSb(struct abuf *ab){
jRt(ab,"\x1b[7m",(4));
char rrO[80],rrrO[80];
int len=snprintf(rrO,sizeof(rrO),"%.20s - %d lines %s",
E.filename?E.filename:"[Untitled]",E.π,
E.π―?"(modded)":"");
int rlen=snprintf(rrrO,sizeof(rrrO),"%d/%d",
E.nC+(1),E.π);
if(len>E.π»)len=E.π»;
jRt(ab,rrO,len);
while(len<E.π»){
if(E.π»-len==rlen){
jRt(ab,rrrO,rlen);
break;
}else{
jRt(ab," ",(1));
len++;
}
}jRt(ab,"\x1b[m",(3));
jRt(ab,"\r\n",(2));}void gBrM(struct abuf *ab){jRt(ab,"\x1b[K",(3));int uuuQ=strlen(E.π€);if(uuuQ>E.π»)uuuQ=E.π»;if(uuuQ&&time(NULL)-E.π§ <(5)){jRt(ab,E.π€,uuuQ);}}void π§β(){tRa();struct abuf ab=jOt;jRt(&ab,"\x1b[?25l",(6));jRt(&ab,"\x1b[H",(3));gTe(&ab);hSb(&ab);gBrM(&ab);char buf[32];snprintf(buf,sizeof(buf),"\x1b[%d;%dH",(E.nC-E.π©)+(1),(E.π€¬-E.π»)+(1));jRt(&ab,buf,strlen(buf));jRt(&ab,"\x1b[?25h",(6));write(STDOUT_FILENO,ab.b,ab.len);mexMaid(&ab);}void π
(const char *fmt,...){va_list ap;va_start(ap,fmt);vsnprintf(E.π€,sizeof(E.π€),fmt,ap);va_end(ap);E.π§ =time(NULL);}char *π(char *prompt){size_t bufsize=(128);char *buf=malloc(bufsize);size_t buflen=(0);buf[0]='\0';while(1){π
(prompt,buf);π§β();int c=mWt();if(c==DEL_KEY||c==CTRL_KEY('h')||c==BACKSPACE){if(buflen!=0)buf[--buflen]='\0';}else if(c=='\x1b'){π
("");free(buf);return(NULL);}else if(c=='\r'){if(buflen!=0){π
("");return(buf);}}else if(!iscntrl(c)&&c<(128)){if(buflen==bufsize-1){bufsize *=(2);buf=realloc(buf,bufsize);}buf[buflen++]=c;buf[buflen]='\0';}}}void eMCi(int key){π₯Ά *row=(E.nC>=E.π)?NULL:&E.row[E.nC];switch(key){case(ARROW_LEFT):if(E.π²!=0){E.π²--;}else if(E.nC>(0)){E.nC--;E.π² = E.row[E.nC].size;}break;case(ARROW_RIGHT):if(row&&E.π²<row->size){E.π²++;}else if(row&&E.π²==row->size){E.nC++;E.π²=(0);}break;case(ARROW_UP):if(E.nC!=(0)){E.nC--;}break;case(ARROW_DOWN):if(E.nC<E.π){E.nC++;}break;}row=(E.nC>=E.π)?NULL:&E.row[E.nC];int rowlen=row?row->size:(0);if(E.π²>rowlen){E.π²=rowlen;}}void kpAo(){static int qRtF=π€€;int c=mWt();switch(c){case '\r':nLI();break;case(CTRL_KEY('q')):if(E.π―&&qRtF>(0)){π
("Are you serious? You really wanna quit when there's unsaved files running about? ",qRtF);qRtF--;return;}write(STDOUT_FILENO,"\x1b[2J",(4));write(STDOUT_FILENO,"\x1b[H",(3));exit(0);break;case(CTRL_KEY('s')):pPm();break;case(HOME_KEY):E.π²=(0);break;case(END_KEY):if(E.nC<E.π)E.π²=E.row[E.nC].size;break;case(BACKSPACE):case(CTRL_KEY('h')):case(DEL_KEY):if(c==DEL_KEY)eMCi(ARROW_RIGHT);tRe();break;case(PAGE_UP):case(PAGE_DOWN):{if(c==PAGE_UP){E.nC=E.π©;}else if(c==PAGE_DOWN){E.nC=E.π©+E.π€‘-(1);if(E.nC>E.π)E.nC=E.π;}int nTrX=E.π€‘;while(nTrX--)eMCi(c==PAGE_UP?ARROW_UP:ARROW_DOWN);}break;case(ARROW_UP):case(ARROW_DOWN):case(ARROW_LEFT):case(ARROW_RIGHT):eMCi(c);break;case(CTRL_KEY('l')):case '\x1b':break;default:pOG(c);break;}qRtF=π€€;}void iOp(){E.π²=(0);E.nC=(0);E.π€¬=(0);E.π©=(0);E.π»=(0);E.π=(0);E.row=(NULL);E.π―=(0);E.filename=(NULL);E.π€[0]='\0';E.π§ =(0);if(pOu(&E.π€‘,&E.π»)==(-1))mNb("pOu");E.π€‘-=(2);}int main(int argc,char *argv[]){dAq();iOp();if(argc>=(2)){eRcV(argv[1]);}π
("^S to Save, ^Q to Quit, ^F to Search. ");while(1){π§β();kpAo();}return(0);}