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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
#include "ihe.h"
#include "core.h"
/* Global variables */
cmd_mng_t cmd_mng;
static int cmd_loop = 1;
extern cmd_table_t tab[];
#define CMK_KEYMAP1(KEY) (in_buf[0]==(KEY)&&(ret_read==1))
#define CMD_IN_BUF_SIZE 32
typedef struct cmd_in_buf_t
{
char buf[CMD_IN_BUF_SIZE+2];//enought space to put "'\n',NULL"
int cur_sz;
} cmd_in_buf_t;
int cmd_buf_clean( cmd_in_buf_t *buf );
/*
type == 1, usual printable chars
type == 2, print in hex
*/
int cmd_buf_print( cmd_in_buf_t *buf, int type );
int cmd_buf_add( cmd_in_buf_t *buf, char *ch, int size );
int cmd_buf_rem1( cmd_in_buf_t *buf );
int cmd_buf_full( cmd_in_buf_t *buf );
int cmd_buf_clean( cmd_in_buf_t *buf )
{
memset( buf, 0, sizeof(cmd_in_buf_t));
return 0;
}
int cmd_buf_print( cmd_in_buf_t *buf, int type )
{
int i;
//printf("sz:%d\n",buf->cur_sz);
for (i=0;(i<CMD_IN_BUF_SIZE);i++)
{
if ( i < buf->cur_sz )
{
if (type == 1)
{
if (isprint(buf->buf[i]))
{
printf("%c",(char)(buf->buf[i]&0xff));
} else
{
printf(".");
}
} else if (type == 2)
{
printf("%02x ", (unsigned char)(buf->buf[i]&0xff) );
}
} else
{
printf(" ");
}
}
fflush(stdout);
return 0;
}
int term_set_std_mode( term_screen *ts )
{
int ret = 0;
if ( tcgetattr( ts->ifd, &ts->orig_i ) == -1 )
{
ERROR("Cannot get input terminal attributes\n");
goto exit_error;
}
ts->raw_i = ts->orig_i; /* modify the original mode */
/* input modes: no break, no CR to NL, no parity check, no strip char,
* no start/stop output control. */
//ts->raw_i.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
ts->raw_i.c_iflag = IUTF8|ICRNL;
/* output modes - disable post raw */
//ts->raw_i.c_oflag &= ~(OPOST);
ts->raw_i.c_oflag = OPOST|ONLCR;
/* control modes - set 8 bit chars */
//ts->raw_i.c_cflag |= (CS8);
ts->raw_i.c_cflag = CS8|CREAD;
/* local modes - choing off, canonical off, no extended functions,
* no signal chars (^Z,^C) */
//ts->raw_i.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
//if ICANNON ignore ESC char
//ts->raw_i.c_lflag = ISIG|ECHO;
ts->raw_i.c_lflag = ISIG;
/* control chars - set return condition: min number of bytes and timer.
* We want read to return every single byte, without timeout. */
ts->raw_i.c_cc[VMIN] = 1;
ts->raw_i.c_cc[VTIME] = 0; /* 1 byte, no timer */
/* put terminal in raw mode after flushing */
if (tcsetattr( ts->ifd, TCSAFLUSH, &ts->raw_i) < 0)
{
ERROR("Cannot set new terminal input attribures\n");
goto exit_error;
}
return ret;
exit_error:
errno = ENOTTY;
return -1;
}
int custom_term_echo( char *keybuf )
{
if (isprint(keybuf[0]))
{
write(1,keybuf,1);
}
if (keybuf[0]=='\n')
{
write(1,"\r\n",2);
}
return 0;
}
int cmd_tab_autocomplete( char *in_buf )
{
cmd_arg_t *args;
cmd_tok_t tl, *ptr = &tl, *iter = NULL;
struct cmd_acq_t *iter_sugg = NULL;
memset( &tl, 0, sizeof( cmd_tok_t ));
int i;
//printf("[%s]\n", in_buf);
#warning "Fix this"
/*
if ( parse_cmd( ptr, in_buf) == -1)
{
printf("Cmd problems\n");
return -1;
}
*/
iter = ptr->next;
args = cmd_arg_create( iter );
for (i=0; i<args->argc; i++)
{
printf("ARG:%d TYPE:%d %s\n", i, args->type[i], args->argv[i]);
}
//printf("Unkn command\n");
//if command not found offer auto complete options
if (args->argc > 0)
{
//printf("asd\n");
struct cmd_acq_head_t *ac = cmd_acq(tab,args->argv[0]);
if (ac != NULL)
{
printf("\n>");
//printf("Did you mean ");
SLIST_FOREACH(iter_sugg,ac,next_sugg)
{
printf("%s ", iter_sugg->suggestion);
}
printf("\n");
}
cmd_acq_free( ac );
}
cmd_tok_destroy( ptr->next );
ptr->next = NULL;
cmd_arg_destroy( args );
return 0;
}
int cmd_buf_add( cmd_in_buf_t *buf, char *ch, int size )
{
int i,j;
/*
for (i=0;i<size;i++)
{
printf("|%c|",ch[i]);
} printf("\n");
*/
i=buf->cur_sz;
for ( j=0;
j<size;
j+=1 )
{
if ( (i+j) < CMD_IN_BUF_SIZE )
{
//printf("%d %c",(i+j),ch[j]);
buf->buf[i+j] = ch[j];
buf->cur_sz = i+j+1;
//printf("%d\n", buf->cur_sz );
//printf("\n");
}
}
return 0;
}
int cmd_buf_rem1( cmd_in_buf_t *buf )
{
int ret = -1;
if ( buf->cur_sz > 0 )
{
buf->cur_sz -= 1;
ret = 0;
}
return ret;
}
int cmd_buf_full( cmd_in_buf_t *buf )
{
int ret = 0;
if ( buf->cur_sz == (CMD_IN_BUF_SIZE-1) )
{
return 1;
}
return ret;
}
/*
GLOBAL VARIABLES
*/
file_t *g_file = NULL;
buf_t *g_buf = NULL;
int g_flags = FD_RW;
int c_version( cmd_arg_t *arg )
{
int argc = arg->argc;
if ( argc != 0 )
{
printf("Command should not have arguments mister\n");
return -1;
}
printf("Version 0.0.2\n");
return 0;
}
int c_arg( cmd_arg_t *arg )
{
int i;
int argc = arg->argc;
char **argv = arg->argv;
for (i=0;i<argc;i++)
{
printf("arg %d: val :%s\n", i, argv[i]);
}
return 0;
}
int c_quit( cmd_arg_t *arg )
{
cmd_loop = 0;
exit(0);
return 0;
}
int c_help( cmd_arg_t *args )
{
int i;
cmd_table_t *iter = &tab[0];
//printf("0\n");
i = 0;
while(iter[i].cmd != NULL)
{
printf("%s ", iter[i].cmd);
if (iter[i].clb)
{
//printf("CLB ");
}
if (iter[i].pre)
{
//printf("PRE ");
}
if (iter[i].hlp)
{
//printf("HLP ");
iter[i].hlp(NULL);
}
printf("\n");
i++;
}
return 0;
}
int c_manifesto( cmd_arg_t *arg )
{
printf("""\
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\
+ MANIFESTO +\n\
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\
+ 1. All hardware and software that you are owner of belongs to you no software+\n\
+ neither hardware patents can stop you to reverse engineer your property. +\n\
+ Software/Hardware that you are owning should be obtained in legal way +\n\
+ except situation under chapter 7 +\n\
+ 2. Any license or patent that disagree with that is unlawfull as it against +\n\
+ to personal freedomes. +\n\
+ 3. Any compiled or non compiled code on your device is your property. +\n\
+ 4. This software is made for any kind of knowledge gaining about software or +\n\
+ hardware that are belong to you. No commercial distribution of gained +\n\
+ knowledge should be made. Only free knowledge distribution allowed. +\n\
+ 5. This software should not be used to harm any living been this doesnt apply+\n\
+ to intelectual property of any kind. With this software personal info like+\n\
+ adresses, credit cards numbers, names, surnames, passwords should not be +\n\
+ gained as it may harm living beans that owns them and surands them. +\n\
+ 6. This software is made to explore system, gain understanding of system and +\n\
+ protect from system. +\n\
+ 7. This software may be used even if any patented or licensed +\n\
+ hardware/software is used in hardware or software that may treat human +\n\
+ life (rockets, warships, missles, army robots, electronics optical +\n\
+ devices, guns, SCADA malware and so on) physicaly doesnt belong to this +\n\
+ software user but treatening this software user life. +\n\
+ 8. By using this software you take all responsibity of result that may occure+\n\
+ while you use it on yourself. +\n\
+ 9. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+\n\
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +\n\
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +\n\
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+\n\
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +\n\
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +\n\
+ DEALINGS IN THE SOFTWARE. +\n\
+ 10. If you disagree with any point that mentioned here please stop using this+\n\
+ peace of software. +\n\
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\
""");
return 0;
}
int c_th( cmd_arg_t *arg )
{
/*
int argc = arg->argc;
int th = -1;
if ( argc > 0 )
{
printf("No arguments needed\n");
return -1;
}
th = term_cur_get_r();
if (th == -1)
{
printf("Cannot get current term height\n");
return -1;
}
printf("%d\n",th);
*/
return 0;
}
int c_tw( cmd_arg_t *arg )
{
/*
int argc = arg->argc;
int tw = -1;
if ( argc > 0 )
{
printf("No arguments needed\n");
return -1;
}
tw = term_cur_get_c();
if (tw == -1)
{
printf("Cannot get current term width\n");
return -1;
}
printf("%d\n",tw);
*/
return 0;
}
/*
CMD COMMANDS
*/
cmd_table_t tab[] = {
{"version", c_version , NULL, NULL, NULL},
{"arg", c_arg , NULL, NULL, NULL},
//{"quit", c_quit},
{"help", c_help, NULL, NULL, NULL},
{"?", c_help, NULL, NULL, NULL},
{"open", c_open, h_open, NULL, NULL},
{"close", c_close, h_close, NULL, NULL},
{"info", c_info, h_info, NULL, NULL},
{"seek", c_seek, h_seek, NULL, NULL},
{"pos", c_pos, h_pos, NULL, NULL},
{"size", c_size, h_size, NULL, NULL},
{"blk", c_blk, h_blk, NULL, NULL},
{"read", c_read, h_read, NULL, NULL},
{"dump", c_dump, h_dump, NULL, NULL},
{"dumpx", c_dumpx, h_dumpx, NULL, NULL},
{"dumps", c_dumps, h_dumps, NULL, NULL},
{"write", c_write, h_write, NULL, NULL},
{"writes", c_writes, h_writes, NULL, NULL},
{"resize", c_resize, h_resize, NULL, NULL},
{"flags", c_flags, h_flags, NULL, NULL},
{"manifesto", c_manifesto, NULL, NULL, NULL},
{"ls", c_ls, h_ls, NULL, NULL},
{"pwd", c_pwd, h_pwd, NULL, NULL},
{"cd", c_cd, h_cd, NULL, NULL},
//{"create", c_create},
//{"th", c_th},
//{"tw", c_tw},
{NULL, NULL , NULL, NULL, NULL},
};
int main( int argc, char **argv )
{
const int sz_buf = 1024;
int err;
uint8_t in_buf[sz_buf];
int ret_read;
int new_c=0, new_r=0, old_r=0, old_c=0;
cmd_arg_t *args;
cmd_tok_t tl, *ptr = &tl, *iter = NULL;
memset( &tl, 0, sizeof( cmd_tok_t ));
//preapre global stuff
g_file = file_init();
//init basic buffer
g_buf = buf_init();
buf_size( g_buf, DEFAULT_BLK_SIZE );
buf_zero( g_buf );
struct term_screen ts; memset( &ts, 0, sizeof(ts) );
cmd_in_buf_t cmd_in;
cmd_buf_clean( &cmd_in );
//clean cmd manager structure that is global
memset(&cmd_mng, 0, sizeof(cmd_mng));
//register command table
cmd_mng.table = tab;
cmd_mng.flag_history = 0;
cmd_mng.flag_autocomplete = 1;
if ( term_init( &ts ) == -1 )
printf("Some err when init\n");
//term_set_raw_mode( &ts );
term_set_std_mode( &ts );
term_clr_scr( &ts );
new_c = term_get_maxcol( &ts );
new_r = term_get_maxrow( &ts );
old_r = new_r;
old_c = new_c;
term_cur_set_c( &ts, 0);
term_cur_set_r( &ts, old_r);
write(1,"$",1);
while ( cmd_loop )
{
//write(1,"$",1);
ret_read = read(2, in_buf, sz_buf );
if (ret_read < 0)
{
printf("Cannot read\n");
continue;
}
in_buf[ret_read] = 0;
//custom echoing service to ignore some special chars like TAB
custom_term_echo( (char *)in_buf );
//printf("(%d)%s\n",ret_read,in_buf);
if ( (isprint(in_buf[0])) && (in_buf[0]!='?') && (in_buf[0]!='\t'))
{
//printf("asd %d\n", ret_read);
cmd_buf_add( &cmd_in, (char *)in_buf, ret_read );
}
//auto complete advice
if (CMK_KEYMAP1(9))
{
if ( cmd_in.cur_sz == 0 )
{
continue;
}
//cmd_buf_print( &cmd_in, 2 );
cmd_in.buf[cmd_in.cur_sz] = '\n';
cmd_in.buf[cmd_in.cur_sz+1] = 0;
#warning "Auto complete"
cmd_tab_autocomplete( cmd_in.buf );
//cmd_buf_print( &cmd_in, 2 );
write(1,"$",1);
cmd_buf_print( &cmd_in, 1 );
term_cur_set_c( &ts, cmd_in.cur_sz+2 );
continue;
}
/*
if (CMK_KEYMAP1('?'))
{
cmd_buf_print( &cmd_in, 1 );
}
*/
if (CMK_KEYMAP1(8) || CMK_KEYMAP1(127))
{
cmd_buf_rem1( &cmd_in );
term_cur_set_c( &ts, 0);
write(1,"$",1);
cmd_buf_print( &cmd_in, 1 );
term_cur_set_c( &ts, cmd_in.cur_sz+2 );
continue;
}
if (CMK_KEYMAP1(27))
{
cmd_buf_print( &cmd_in, 1 );
break;
}
//exec cmd
if (CMK_KEYMAP1('\n'))
{
int i;
cmd_in.buf[cmd_in.cur_sz] = '\n';
cmd_in.buf[cmd_in.cur_sz+1] = 0;
#warning "Fix this"
/*
if ( parse_cmd( ptr, cmd_in.buf) == -1)
{
continue;
}
*/
/*
iter = ptr->next;
args = cmd_arg_create( iter );
cmd_tok_destroy( ptr->next );
ptr->next = NULL;
*/
/*
for (i=0; i<args->argc; i++)
{
printf("ARG:%d TYPE:%d %s\n", i, args->type[i], args->argv[i]);
}
*/
/*
if (cmd_exec(tab, args) != 0)
{
printf("!Unknown command\n");
}*/
err = cmd_mng_exec( &cmd_mng, cmd_in.buf, cmd_in.cur_sz );
if (err!=CMD_EOK)
{
printf("Unknow command\n");
}
//cmd_arg_destroy( args );
cmd_buf_clean( &cmd_in );
write(1,"$",1);
}
}
cmd_mng_free( &cmd_mng );
term_clr_scr( &ts );
term_set_orig_mode( &ts );
return 0;
}
|