lua_in_finsh.c 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
 * run lua interpreter from finsh
 */

#include "rtthread.h"
#include "finsh.h"
#include "shell.h"

struct
{
    struct rt_semaphore sem;
    rt_device_t device;
} dev4lua;

extern int lua_main(int argc, char **argv);

rt_err_t lua_rx_ind(rt_device_t dev, rt_size_t size)
{
    rt_sem_release(&dev4lua.sem);

    return RT_EOK;
}
23 24 25 26
struct para{
	int argc;
	char *argv[3];
};
27

28
void finsh_lua(struct para *parameters)
29 30 31 32 33 34 35 36 37
{
    rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);

    rt_sem_init(&(dev4lua.sem), "luasem", 0, 0);

    /* save old rx_indicate */
    rx_indicate = dev4lua.device->rx_indicate;

    /* set new rx_indicate */
38
    rt_device_set_rx_indicate(dev4lua.device, lua_rx_ind);
39 40

    {
41 42 43 44 45 46 47 48
	    int argc = parameters->argc;
	    char **argv = parameters->argv;
	    /*
	    rt_kprintf("argc =%d, argv[1] =%d\n", argc, argv[1]);
	    while(1);
	    */
	    /* run lua interpreter */
	    lua_main(argc, argv);
49
    }
50 51 52
    if (parameters->argc > 1)
	    rt_free(parameters->argv[1]);
    rt_free(parameters);
53 54 55 56 57

    /* recover old rx_indicate */
    rt_device_set_rx_indicate(dev4lua.device, rx_indicate);
}

58
static void lua(void *parameters)
59
{
60
    rt_thread_t lua_thread;
61 62 63 64 65 66 67 68 69
    const char* device_name = finsh_get_device();
    rt_device_t device = rt_device_find(device_name);
    if (device == RT_NULL)
    {
        rt_kprintf("%s not find\n", device_name);
        return;
    }
    dev4lua.device = device;

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    /*prepare parameters*/
    struct para *lua_parameters = rt_malloc(sizeof(struct para));
    if ( lua_parameters == NULL ){
	    rt_kprintf("malloc failed at file: %s,line: %d", __FILE__, __LINE__);
	    return;
    }
    lua_parameters->argc = 2;
    char **arg = lua_parameters->argv;

    arg[0] = "lua";
    if (parameters != NULL){
	    rt_size_t len = strnlen(parameters, 50);
	    arg[1] = rt_malloc(len + 1);
	    if (arg[1] == NULL ){
		    rt_kprintf("malloc failed at file: %s,line: %d", __FILE__, __LINE__);
		    return;
	    }
	    rt_memset(arg[1], 0, len+1);
	    strncpy(arg[1], parameters, len);
    }else{
	    arg[1] = NULL;
    }
    arg[2] = NULL;

94 95
    /* Run lua interpreter in separate thread */
    lua_thread = rt_thread_create("lua",
96 97 98
                                  (void (*)(void *))finsh_lua,
                                  (void*)lua_parameters,
                                  10240,
99 100 101 102 103 104
                                  rt_thread_self()->current_priority + 1,
                                  20);
    if (lua_thread != RT_NULL)
    {
        rt_thread_startup(lua_thread);
    }
105
    return;
106
}
107 108 109 110
FINSH_FUNCTION_EXPORT(lua, lua interpreter);

static  void lua_msh(int argc, char **argv)
{
111
	rt_thread_t lua_thread;
112 113 114 115 116 117 118 119 120 121
	const char* device_name = finsh_get_device();
	rt_device_t device = rt_device_find(device_name);
	if (device == RT_NULL)
	{
		rt_kprintf("%s not find\n", device_name);
		return;
	}
	dev4lua.device = device;

	/*prepare parameters*/
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
	struct para *parameters = rt_malloc(sizeof(struct para));
	if ( parameters == NULL ){
		rt_kprintf("malloc failed at file: %s,line: %d", __FILE__, __LINE__);
		return;
	}
	//parameters->argc = 2;
	parameters->argc = argc;
	char **arg = parameters->argv;

	arg[0] = "lua";
	if (argc > 1){
		rt_size_t len = strnlen(argv[1], 50);
		arg[1] = rt_malloc(len + 1);
		if (arg[1] == NULL ){
			rt_kprintf("malloc failed at file: %s,line: %d", __FILE__, __LINE__);
			return;
		}
		rt_memset(arg[1], 0, len+1);
		strncpy(arg[1], argv[1], len);
	}else{
		arg[1] = NULL;
	}
	arg[2] = NULL;

	
	/* Run lua interpreter in separate thread */
	lua_thread = rt_thread_create("lua_msh",
			(void (*)(void *))(finsh_lua),
			(void*)parameters,
			10240,
152
			rt_thread_self()->current_priority + 1,
153 154 155 156
			20);
	if (lua_thread != RT_NULL)
	{
		rt_thread_startup(lua_thread);
157 158 159 160
	}
	return;
}
MSH_CMD_EXPORT(lua_msh, lua in msh);
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

int readline4lua(const char *prompt, char *buffer, int buffer_size)
{
    char ch;
    int line_position;

start:
    /* show prompt */
    rt_kprintf(prompt);

    line_position = 0;
    memset(buffer, 0, buffer_size);
    while (1)
    {
        if (rt_sem_take(&dev4lua.sem, RT_WAITING_FOREVER) != RT_EOK)
        {
            return 0;
        }

        while (rt_device_read(dev4lua.device, 0, &ch, 1) == 1)
        {
            /* handle CR key */
            if (ch == '\r')
            {
                char next;
                if (rt_device_read(dev4lua.device, 0, &next, 1) == 1)
                    ch = next;
            }
            /* backspace key */
            else if (ch == 0x7f || ch == 0x08)
            {
                if (line_position > 0)
                {
                    rt_kprintf("%c %c", ch, ch);
                    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;
                rt_kprintf("\n");
                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 */
            rt_kprintf("%c", ch);
            buffer[line_position] = ch;
            ch = 0;
            line_position++;

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