示例#1
0
void
ft_dotsaves()
{
    wordlist *iline, *wl = NULL;
    char *s;

    if (!ft_curckt) /* Shouldn't happen. */
        return;

    for (iline = ft_curckt->ci_commands; iline; iline = iline->wl_next) {
        if (ciprefix(".save", iline->wl_word)) {
            s = iline->wl_word;
            (void) gettok(&s);
            wl = wl_append(wl, gettoks(s));
        }
    }

    com_save(wl);
    return;
}
示例#2
0
void
ft_dotsaves(void)
{
    wordlist *iline, *wl = NULL;
    char *s, *fr;

    if (!ft_curckt) /* Shouldn't happen. */
        return;

    for (iline = ft_curckt->ci_commands; iline; iline = iline->wl_next)
        if (ciprefix(".save", iline->wl_word)) {
            s = iline->wl_word;
            /* skip .save */
            fr = gettok(&s);
            tfree(fr);
            wl = wl_append(wl, gettoks(s));
        }

    com_save(wl);
    wl_free(wl);
}
示例#3
0
void com_exit(char *text[], char *path)
{
	//询问是否保存
	if (changed == 1)
	{
		printf(1, "save the file? y/n\n");
		char input[MAX_LINE_LENGTH] = {};
		gets(input, MAX_LINE_LENGTH);
		input[strlen(input)-1] = '\0';
		if (strcmp(input, "y") == 0)
			com_save(text, path);
	}
	//释放内存
	int i = 0;
	for (; text[i] != NULL; i++)
	{
		free(text[i]);
		text[i] = 0;
	}
	//退出
	setProgramStatus(SHELL);
	exit();
}
示例#4
0
int main(int argc, char *argv[])
{
	setProgramStatus(EDITOR);
	if (argc == 1)
	{
		printf(1, "please input the command as [editor file_name]\n");
		setProgramStatus(SHELL);
		exit();
	}
	//存放文件内容
	char *text[MAX_LINE_NUMBER] = {};
	text[0] = malloc(MAX_LINE_LENGTH);
	memset(text[0], 0, MAX_LINE_LENGTH);
	//存储当前最大的行号,从0开始。即若line_number == x,则从text[0]到text[x]可用
	int line_number = 0;
	//尝试打开文件
	int fd = open(argv[1], O_RDONLY);
	//如果文件存在,则打开并读取里面的内容
	if (fd != -1)
	{
		char buf[BUF_SIZE] = {};
		int len = 0;
		while ((len = read(fd, buf, BUF_SIZE)) > 0)
		{
			int i = 0;
			int next = 0;
			int is_full = 0;
			while (i < len)
			{
				//拷贝"\n"之前的内容
				for (i = next; i < len && buf[i] != '\n'; i++)
					;
				strcat_n(text[line_number], buf+next, i-next);
				//必要时新建一行
				if (i < len && buf[i] == '\n')
				{
					if (line_number >= MAX_LINE_NUMBER - 1)
						is_full = 1;
					else
					{
						line_number++;
						text[line_number] = malloc(MAX_LINE_LENGTH);
						memset(text[line_number], 0, MAX_LINE_LENGTH);
					}
				}
				if (is_full == 1 || i >= len - 1)
					break;
				else
					next = i + 1;
			}
			if (is_full == 1)
				break;
		}
		close(fd);
	}
	
	//输出文件内容
	show_text(text);
	//输出帮助
	com_help(text);
	
	//处理命令
	char input[MAX_LINE_LENGTH] = {};
	while (1)
	{
		printf(1, "\nplease input command:\n");
		memset(input, 0, MAX_LINE_LENGTH);
		gets(input, MAX_LINE_LENGTH);
		int len = strlen(input);
		input[len-1] = '\0';
		len --;
		//寻找命令中第一个空格
		int pos = MAX_LINE_LENGTH - 1;
		int j = 0;
		for (; j < 8; j++)
		{
			if (input[j] == ' ')
			{
				pos = j + 1;
				break;
			}
		}
		//ins
		if (input[0] == 'i' && input[1] == 'n' && input[2] == 's')
		{
			if (input[3] == '-')
				com_ins(text, atoi(&input[4]), &input[pos]);
			else
				com_ins(text, line_number+1, &input[pos]);
			//插入操作需要更新行号
			line_number = get_line_number(text);
		}
		//mod
		else if (input[0] == 'm' && input[1] == 'o' && input[2] == 'd')
		{
			if (input[3] == '-')
				com_mod(text, atoi(&input[4]), &input[pos]);
			else
				com_mod(text, line_number + 1, &input[pos]);
		}
		//del
		else if (input[0] == 'd' && input[1] == 'e' && input[2] == 'l')
		{
			if (input[3] == '-')
				com_del(text, atoi(&input[4]));
			else
				com_del(text, line_number + 1);
			//删除操作需要更新行号
			line_number = get_line_number(text);
		}
		else if (strcmp(input, "show") == 0)
		{
			auto_show = 1;
			printf(1, "enable show current contents after text changed.\n");
		}
		else if (strcmp(input, "hide") == 0)
		{
			auto_show = 0;
			printf(1, "disable show current contents after text changed.\n");
		}
		else if (strcmp(input, "help") == 0)
			com_help(text);
		else if (strcmp(input, "save") == 0 || strcmp(input, "CTRL+S\n") == 0)
			com_save(text, argv[1]);
		else if (strcmp(input, "exit") == 0)
			com_exit(text, argv[1]);
		else
		{
			printf(1, "invalid command.\n");
			com_help(text);
		}
	}
	setProgramStatus(SHELL);
	exit();
}