⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shell.c

📁 中国人自己的c语言
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <finsh/finsh.h>

void hello()
{
	printf("Hello world\n");
	fflush(stdout);
}

void _c(unsigned char ch)
{
	printf("%c\n", ch);
}

void _i(unsigned int i)
{
	printf("%d\n", i);
}

void _x(unsigned int i)
{
	printf("0x%04x\n", i);
}

int _double(unsigned int i)
{
	printf("%d double is %d\n", i, i + i);

	return i + i;
}

int _two(int i, int j)
{
	printf("i is %d\n", i);
	printf("j is %d\n", j);

	return 0;
}

struct finsh_syscall global_syscall_table[] =
{
	{"printf", (syscall_func)printf},
	{"hello", hello},
	{"_c", (syscall_func)_c},
	{"_i", (syscall_func)_i},
	{"_x", (syscall_func)_x},
	{"_double", (syscall_func)_double},
	{"_two", (syscall_func)_two}
};

struct finsh_syscall* finsh_syscall_lookup(const u_char* name)
{
	register int i;

	for (i = 0; i < sizeof(global_syscall_table) / sizeof(struct finsh_syscall);
		i++)
	{
		if (strncmp(global_syscall_table[i].name, name, FINSH_NAME_MAX) == 0)
			break;
	}

	if (i != sizeof(global_syscall_table) / sizeof(struct finsh_syscall)) return &global_syscall_table[i];

	return NULL;
}

void copyright()
{
	printf("FinSH -- Fava in Shell, version 0.1.0\n");
	printf("2005.10 release under GPL.\n");
	printf("2005 (C)opyright by Xiong puxiang(midnight.xiong@gmail.com).\n");

	fflush(stdout);
}

int is_quit(unsigned char* line)
{
    char buf[5], *p; /*quit*/
    int i;

    p = buf;
    i = 0;

    while ( line && i<5 )
    {
		if (isalpha(line[i]))
		{
			*p++ = line[i];
		}
		i++;
    }
    *p = '\0';

    if (strcmp(buf, "q") == 0) return 0;
    if (strcmp(buf, "quit") == 0) return 0;

    return -1;
}

int main(int argc, char** argv)
{
	struct finsh_parser parser;
    unsigned char line[256];

    copyright();

    finsh_init(&parser);

	while ( 1 )
	{
		printf("finsh>>");
		fflush(stdout);

		memset(line, 0, sizeof(line));
		fgets(line, sizeof(line), stdin);
		if (is_quit(line) == 0) break;

		/* add an ';' in the end of line */
		if (line[strlen(line) - 1] == '\n')
		{
			line[strlen(line) - 1] = ';';
			line[strlen(line)    ] = '\0';
		}
		else
		{
			line[strlen(line)    ] = ';';
			line[strlen(line) + 1] = '\0';
		}

		finsh_parser_run(&parser, &line[0]);

		/* compile node root */
		if (finsh_errno() == 0)
		{
			finsh_compiler_run(parser.root);
		}
		else
		{
			printf("%s\n", finsh_error_string(finsh_errno()));
		}

		/* run virtual machine */
		if (finsh_errno() == 0)
		{
			finsh_vm_run();

			if (finsh_stack_bottom() != 0)
			{
                if (isprint((char)finsh_stack_bottom()))
                {
                    printf("\t'%c', %d, 0x%04x\n",
                        (char)finsh_stack_bottom(),
                        (unsigned int)finsh_stack_bottom(),
                        (unsigned int)finsh_stack_bottom());
                }
                else
                {
                    printf("\t%d, 0x%04x\n",
                        (unsigned int)finsh_stack_bottom(),
                        (unsigned int)finsh_stack_bottom());
                }
			}
		}

        finsh_flush(&parser);
	}

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -