Skip to content

Commit cc6c138

Browse files
committed
Fixed command identification
Fixed a bug: If the stored command was shorter than the executed command and they are matched the interpreter executed it. Example: candyCrush executed when candy was stored.
1 parent 00f54b7 commit cc6c138

File tree

6 files changed

+85
-2
lines changed

6 files changed

+85
-2
lines changed

inc/interpreter.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <stdint.h>
88
#include <string.h>
99

10-
#define NUM_OF_API_FUNCS 7
10+
#define NUM_OF_API_FUNCS 8
11+
#define MAX_RESP_LEN 100
12+
#define CMD_BUFF_LEN 30
1113

1214
// structure for instruction data
1315
typedef struct API_t{
@@ -18,6 +20,7 @@ typedef struct API_t{
1820
const char **name; // Name of the instruction
1921
const char **desc; // Description of the command
2022
void(*func)(char*,char*); // Function pointer to the command function
23+
//void(*resp_fn)(char*, ...)
2124

2225
}API_t;
2326

@@ -40,6 +43,8 @@ void swap_api_elements( uint16_t index, uint16_t place );
4043

4144
void recursive_optimiser( int32_t start_index, int32_t stop_index );
4245

46+
void execute( char *cmd, char *response );
47+
4348
void teszt(void);
4449

4550
#endif

myapp.exe

677 Bytes
Binary file not shown.

obj/interpreter.o

516 Bytes
Binary file not shown.

obj/main.o

196 Bytes
Binary file not shown.

src/interpreter.c

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ create_instruction_data(majom, "majom fuggveny leirasa, jaaaa :)");
1818
create_instruction_data(eger, "eger fuggveny leirasa, gyoooo :)");
1919
create_instruction_data(okor, "eger fuggveny leirasa, gyaaa :)");
2020
create_instruction_data(csiga, "eger fuggveny leirasa, dikk :)");
21+
create_instruction_data(v, "Vagod csotany, hihihiiii ;)");
2122

2223
#define add_instruction(name, func) add_interpreter_instruction(&name##_API_NAME, &name##_API_DESC, func);
2324

@@ -46,6 +47,7 @@ void init_interpreter(void)
4647
add_instruction(eger, kutya_func);
4748
add_instruction(okor, kutya_func);
4849
add_instruction(csiga, kutya_func);
50+
add_instruction(v, kutya_func);
4951

5052
// Index eache element to find their place in alphabetic order
5153
index_apis_in_order( &API_tree[0] );
@@ -97,7 +99,7 @@ void add_interpreter_instruction(const char **name, const char **desc, void (*fu
9799
// if it is ot the first command we have to find it's place in the tree
98100
else
99101
{
100-
prev = &API_tree[0]; // get the address of the root element
102+
prev = &API_tree[0]; // get the address of the root element
101103
comp_res = strcmp( (char*)*(prev->name), (char*)*name ); // compare the names and save the result to 'comp_res'
102104

103105
// compare( ABC order ) the root element name and the new element name
@@ -274,3 +276,66 @@ void recursive_optimiser( int32_t start_index, int32_t stop_index ){
274276

275277

276278
}
279+
280+
void execute( char *cmd, char *response ){
281+
282+
API_t *next;
283+
API_t *prev;
284+
int8_t comp_res;
285+
char *arg;
286+
uint32_t cmd_name_cntr;
287+
288+
arg = cmd; // 'arg' variable will hold the start address of the argument list
289+
cmd_name_cntr = 0;
290+
291+
// find the first space character or a string-end character.
292+
// At this time count how long is the command name( in characters )
293+
while( ( *arg != '\0' ) && ( *arg != ' ' ) ){
294+
295+
cmd_name_cntr++;
296+
arg++;
297+
298+
}
299+
300+
// If space character found increment the 'arg'.
301+
// It is important to do unless the arg list will contain
302+
// a space character at the beginning.
303+
if( *arg == ' ' ){
304+
305+
*arg = '\0';
306+
arg++;
307+
308+
}
309+
310+
311+
prev = &API_tree[0];
312+
313+
//comp_res = strncmp( (char*)*(prev->name), cmd, cmd_name_cntr );
314+
315+
comp_res = strncmp( (char*)*(prev->name), cmd, strlen( (char*)*(prev->name) ) );
316+
317+
(comp_res > 0) ? (next = (prev->left)) : ( next = (prev->right));
318+
319+
while( ( comp_res !=0 ) && ( next != NULL ) ){
320+
321+
prev = next;
322+
//comp_res = strncmp( (char*)*(prev->name), cmd, cmd_name_cntr );
323+
comp_res = strncmp( (char*)*(prev->name), cmd, strlen( (char*)*(prev->name) ) );
324+
325+
(comp_res > 0) ? (next = (prev->left)) : ( next = (prev->right));
326+
327+
}
328+
329+
if( comp_res == 0 ){
330+
331+
(prev -> func)( arg, response );
332+
333+
}
334+
335+
else{
336+
337+
printf( "Command \'%s\' not found!!!\r\n", cmd );
338+
339+
}
340+
341+
}

src/main.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@
55

66
int main(){
77

8+
char response[100];
9+
10+
char cmd[100] = "cica 1";
11+
812
printf("Program START...\r\n");
913

1014

1115
init_interpreter();
1216

1317

18+
execute( cmd, response );
19+
20+
strcpy( cmd, "kutya 10" );
21+
execute( cmd, response );
22+
23+
strcpy( cmd, "retek 10" );
24+
execute( cmd, response );
25+
26+
1427
return 0;
1528
}

0 commit comments

Comments
 (0)