-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgranth.c
More file actions
167 lines (136 loc) · 2.57 KB
/
granth.c
File metadata and controls
167 lines (136 loc) · 2.57 KB
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
/**** includes ***/
#include <unistd.h>//read(),STDIN_FILENO
#include <termios.h>
#include <stdlib.h>//atexit()
#include <ctype.h>//iscntrl()
#include <stdio.h>//printf()
#include <errno.h>
#include <sys/ioctl.h>
/*** defines ***/
#define CTRL_KEY(k) ((k) & 0x1f)
/*** data ***/
struct editorConfig {
int screenrows;
int screencols;
struct termios orig_termios;
};
struct editorConfig E;
/*** terminal ***/
void die(const char *s)
{
write(STDOUT_FILENO, "\x1b[2J",4);
write(STDOUT_FILENO, "\x1b[H", 3);
perror(s);
exit(1);
}
void disableRawMode()
{
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1)
die("tcsetattr");
}
void enableRawMode()
{
if(tcgetattr(STDIN_FILENO, &E.orig_termios) == -1 )
die("tcgetattr") ;
atexit(disableRawMode);
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 | ISIG | IEXTEN);
raw.c_cc[VMIN] = 0;// min bytes required before read() can return
raw.c_cc[VTIME] = 1; //in tenth of a second so 1/10th
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
char editorReadKey()
{
int nread;
char c;
while((nread = read(STDIN_FILENO, &c, 1)) != 1)
{
if(nread == -1 && errno != EAGAIN)
die("read");
}
return c;
}
int getWindowSize(int *rows, int *cols)
{
struct winsize ws;
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0)
{
return -1;
}
else
{
*cols = ws.ws_col;
*rows = ws.ws_row;
return 0;
}
}
/***output***/
void editorDrawRows()
{
int i;
for(i = 0; i < E.screenrows ; i++)
{
write(STDOUT_FILENO, "~", 1);
if(i < E.screenrows - 1)
{
write(STDOUT_FILENO. "\r\n", 2);
}
}
}
void editorRefreshScreen()
{
write(STDOUT_FILENO, "\x1b[2J",4);
write(STDOUT_FILENO, "\x1b[H", 3);
editorDrawRows();
write(STDOUT_FILENO, "\x1b[H", 3);
}
/***input***/
void editorProcessKeypress()
{
char c = editorReadKey();
switch(c)
{
case CTRL_KEY('q'):
write(STDOUT_FILENO, "\x1b[2J",4);
write(STDOUT_FILENO, "\x1b[H", 3);
exit(0);
break;
}
}
/*** init ***/
void initEditor() {
if(getWindowSize(&E.screenrows, &E.screencols) == -1)
die("getWindowSize");
}
int main()
{
enableRawMode();
initEditor();
while(1)
{
editorRefreshScreen();
editorProcessKeypress();
}
/*
while(1)
{
char c = '\0';
if(read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN)
die("read");
{
if(iscntrl(c))
{
printf("%d\r\n",c);
}
else
{
printf("%d ('%c')\r\n", c, c);
}
}
if(c == CTRL_KEY('q')) break;
}*/
return 0;
}