Example #1
0
extern int _read(int file, char* ptr, int len)
{
    if(file < 3) {
        int i;
        for(i = 0; i < len; i++) {
            *ptr = retarget_getc();
            ptr++;
        }
        return len;
    } else {
        int read_bytes = len;
        int bytes;
        bytes = vm_fs_read(file, ptr, len, &read_bytes);
        LOG("_read(%d, %s, %d, %d) - %d\n", file, ptr, len, read_bytes, bytes);
        return bytes;
    }
}
extern int _read(int file, char *ptr, int len)
{
    if (file < 3) {
        int i;
        for (i = 0; i < len; i++) {
            *ptr = retarget_getc();
            ptr++;
        }
        return len;
    } else {
        int read_bytes = len;
        int bytes;
        bytes = vm_file_read(file, ptr, len, &read_bytes);

        // printf("_read() - file: %d, content: %X, len: %d, read: %d, read2: %d\n", file, (int)ptr, len, bytes, read_bytes);
        return bytes;
    }
}
int linenoise_getline( int id, char* buffer, int buffer_size, const char* prompt )
{
    int ch;
    int line_position;
start:
    /* show prompt */
    fputs(prompt, stdout);

    line_position = 0;
    memset(buffer, 0, buffer_size);
    while (1)
    {
        ch = retarget_getc(stdin);
        if (ch >= 0)
        {
            /* backspace key */
            if (ch == 0x7f || ch == 0x08)
            {
                if (line_position > 0)
                {
                    fputs("\x08 \x08", stdout);
                    line_position--;
                }
                buffer[line_position] = 0;
                continue;
            }
            /* EOF(ctrl+d) */
            else if (ch == 0x04)
            {
                if (line_position == 0)
                    /* No input which makes lua interpreter close */
                    return 0;
                else
                    continue;
            }

            /* end of line */
            if (ch == '\r' || ch == '\n')
            {
                buffer[line_position] = 0;
                fputc('\n', stdout);
                if (line_position == 0)
                {
                    /* Get a empty line, then go to get a new line */
                    goto start;
                }
                else
                {
                    return line_position;
                }
            }

            /* other control character or not an acsii character */
            if (ch < 0x20 || ch >= 0x80)
            {
                continue;
            }

            /* echo */
            fputc(ch, stdout);
            buffer[line_position] = ch;
            ch = 0;
            line_position++;

            /* it's a large line, discard it */
            if (line_position >= buffer_size)
                line_position = 0;
       }
    }

}