Skip to content

Commit

Permalink
Merge branch 'master' of https://gitee.com/lyon1998/pikascript
Browse files Browse the repository at this point in the history
  • Loading branch information
pikasTech committed Apr 18, 2024
2 parents 073f300 + edd1c4f commit 485c3fe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/PikaObj.c
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,30 @@ char _await_getchar(sh_getchar fn_getchar) {
return ret;
}

#define PIKA_MAGIC_CODE_LEN 4

/* return file size */
PIKA_WEAK uint32_t _pikaShell_recv_file(ShellConfig* cfg, uint8_t *magic_code, uint8_t **pbuff){
uint32_t size = 0;
for (int i = 0; i < 4; i++) {
uint8_t* size_byte = (uint8_t*)&size;
size_byte[i] = cfg->fn_getchar();
}
if (magic_code[3] == 'o') {
size += sizeof(uint32_t) * 2;
}
uint8_t* buff = pikaMalloc(size);
/* save magic code and size */
memcpy(buff, magic_code, PIKA_MAGIC_CODE_LEN);
memcpy(buff + PIKA_MAGIC_CODE_LEN, &size, sizeof(size));

for (uint32_t i = sizeof(uint32_t) * 2; i < size; i++) {
buff[i] = cfg->fn_getchar();
}
*pbuff = buff;
return size;
}

void _do_pikaScriptShell(PikaObj* self, ShellConfig* cfg) {
/* init the shell */
_obj_runChar_beforeRun(self, cfg);
Expand Down Expand Up @@ -2000,28 +2024,13 @@ void _do_pikaScriptShell(PikaObj* self, ShellConfig* cfg) {

/* run xx.py.o */
if (inputChar[0] == 'p' && inputChar[1] == 0x0f) {
uint8_t magic_code[4] = {0x0f, 'p', 0x00, 0x00};
uint8_t magic_code[PIKA_MAGIC_CODE_LEN] = {0x0f, 'p', 0x00, 0x00};
for (int i = 0; i < 2; i++) {
/* eat 'yo' */
magic_code[2 + i] = cfg->fn_getchar();
}
uint32_t size = 0;
for (int i = 0; i < 4; i++) {
uint8_t* size_byte = (uint8_t*)&size;
size_byte[i] = cfg->fn_getchar();
}
if (magic_code[3] == 'o') {
size += sizeof(uint32_t) * 2;
}
uint8_t* buff = pikaMalloc(size);
/* save magic code and size */
memcpy(buff, magic_code, sizeof(magic_code));
memcpy(buff + sizeof(magic_code), &size, sizeof(size));

for (uint32_t i = sizeof(uint32_t) * 2; i < size; i++) {
buff[i] = cfg->fn_getchar();
}

uint8_t* buff = NULL;
uint32_t size = _pikaShell_recv_file(cfg, magic_code, &buff);
pika_platform_printf(
"\r\n=============== [File] ===============\r\n");
pika_platform_printf("[ Info] Recived size: %d\r\n", size);
Expand Down
5 changes: 5 additions & 0 deletions src/PikaPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ PIKA_WEAK char pika_platform_getchar(void) {
#endif
}

/* return -1 for no char received, 0 for received */
PIKA_WEAK int pika_platform_getchar_noblock(char* ch){
WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL();
}

PIKA_WEAK int pika_platform_repl_recv(uint8_t* buff,
size_t size,
uint32_t timeout) {
Expand Down
1 change: 1 addition & 0 deletions src/PikaPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ void pika_platform_wait(void);

/* support shell */
char pika_platform_getchar(void);
int pika_platform_getchar_noblock(char* ch);
int pika_platform_putchar(char ch);
int pika_platform_fflush(void* stream);

Expand Down

0 comments on commit 485c3fe

Please sign in to comment.