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
|
#include "term_io.h"
int term_fprint( screen_mode_e mode, FILE *f )
{
int ret=-1;
if (f == NULL)
return -1;
ERROR("\n");
switch ( mode )
{
case SCREEN_MODE_80x25:
{
}
break;
default:
printf("Unknown screen mode\n");
}
return ret;
}
int term_print( term_screen *ts, const char *s, size_t n)
{
int ret=0;
if ( ts == NULL )
return -1;
if ( s == NULL )
return -1;
if ( n < 1 )
return -1;
if ( n > 80*25 )
return -1;
ret = write( ts->ofd, s, n );
return ret;
}
//print data to terminal starting from x,y
int term_print_xy( term_screen *ts, const char *buf, size_t size,
int init_column, int init_row )
{
int ret=-1;
if ( buf == NULL )
{
return -1;
}
if ( size <= 0 )
{
return -1;
}
switch ( ts->mode )
{
case SCREEN_MODE_80x25:
{
}
break;
default:
printf("Unknown mode\n");
}
return ret;
}
int term_draw_hline( term_screen *ts, int pc, int pr, const int sz, char ch)
{
int ret=0;
char buf[sz]; memset( buf, ch, sz );
if ( sz > 0)
{
term_cur_set_c( ts, pc );
term_cur_set_r( ts, pr );
write( ts->ofd, buf, sz );
} else
{
ret = -1;
}
return ret;
}
//read one character from stream
int term_getc( term_screen *ts )
{
int ret=-1;
int fret=-1;
char buf;
fret = read( ts->ifd, &buf, 1 );
if ( fret == 1 )
ret = buf;
return ret;
}
int term_putc( term_screen *ts, char c )
{
int ret = 0;
int fret = -1;
fret = write( ts->ofd, &c, 1 );
if ( fret != 1 )
{
ret = -1;
}
return ret;
}
//return amoutn fo characters readed
//on error or if nothing is imputed 0 returned
int term_readline( term_screen *ts, char *str, size_t str_size, int flag )
{
int ret = 0;
int max_row, max_column;
char *buf = NULL;
int buf_size = str_size-1;
int buf_curent = 0;
int menu_input=0;
char menu_cmd = 0;
int orig_row;
int orig_col;
buf = malloc( buf_size );
memset( buf, 0, buf_size );
max_row = term_get_maxrow( ts );
max_column = term_get_maxcol( ts );
orig_row = term_cur_get_r( ts );
orig_col = term_cur_get_c( ts );
menu_cmd = 0;
buf_curent = 0;
while ( menu_cmd != TK_ESC )
{
menu_input = term_getc( ts );
if ( menu_input != -1 )
{
menu_cmd = (char)menu_input;
//add to buffer any pritable char
//if ( (isgraph( menu_cmd ) && flag == READLINE_TEXT ) ||
// (isalpha( menu_cmd ) && flag == READLINE_ALPHA ) )
if ( isalpha( menu_cmd ) )
{
if ( buf_curent < buf_size )
{
buf[ buf_curent ] = menu_cmd;
buf_curent += 1;
}
//deleteone char from buffer
} else if ( menu_cmd == TK_BACKSPACE )
{
if ( buf_curent > 0)
{
buf[ buf_curent ] = 0x0;
buf_curent -= 1;
}
//input ready
} else if ( menu_cmd == TK_ENTER )
{
ret = buf_curent;
memcpy( str, buf, buf_size );
str[ str_size-1 ] = 0x0;
break;
} else if ( menu_cmd == TK_ESC )
{
ret = -1;
}
{
int i;
term_cur_set_r( ts, orig_row );
term_cur_set_c( ts, orig_col );
for (i=0;i<buf_size;i++)
{
if ( i < buf_curent )
{
if ( (flag == READLINE_TEXT) || (flag == READLINE_ALPHA) )
{
term_putc( ts, buf[i] );
} else if ( flag == READLINE_HIDDEN )
{
term_putc( ts, '*');
}
} else
{
term_putc( ts, ' ' );
}
}
term_cur_set_c( ts, orig_col+buf_curent );
term_cur_set_r( ts, orig_row );
}
}
}
if ( buf != NULL)
{
free( buf );
buf = NULL;
}
return ret;
}
|