gpt4 book ai didi

c - 错误是在语法中还是在代码中?

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:17 25 4
gpt4 key购买 nike

我不确定这个语法对于应该也能够执行单引号和双引号的 shell 命令语言是否正确。似乎非平凡的命令有效,例如ls -al |排序 | wc -l 但简单的方法不适用于单引号:echo 'foo bar' 不起作用。

%{
#include "shellparser.h"
%}

%option reentrant
%option noyywrap

%x SINGLE_QUOTED
%x DOUBLE_QUOTED

%%

"|" { return PIPE; }

[ \t\r] { }
[\n] { return EOL; }

[a-zA-Z0-9_\.\-]+ { return FILENAME; }

['] { BEGIN(SINGLE_QUOTED); }
<SINGLE_QUOTED>[^']+ { }
<SINGLE_QUOTED>['] { BEGIN(INITIAL); return ARGUMENT; }
<SINGLE_QUOTED><<EOF>> { return -1; }

["] { BEGIN(DOUBLE_QUOTED); }
<DOUBLE_QUOTED>[^"]+ { }
<DOUBLE_QUOTED>["] { BEGIN(INITIAL); return ARGUMENT; }
<DOUBLE_QUOTED><<EOF>> { return -1; }

[^ \t\r\n|'"]+ { return ARGUMENT; }

%%

我扫描和解析shell的代码是

 params[0] = NULL;
printf("> ");
i=1;
do {
lexCode = yylex(scanner);
text = strdup(yyget_text(scanner));//yyget_text(scanner);
/*printf("lexCode %d command %s inc:%d", lexCode, text, i);*/
ca = text;
if (lexCode != EOL) {
params[i++] = text;
}
Parse(shellParser, lexCode, text);
if (lexCode == EOL) {
dump_argv("Before exec_arguments", i, params);
exec_arguments(i, params);
corpse_collector();
Parse(shellParser, 0, NULL);
i=1;
}
} while (lexCode > 0);

if (-1 == lexCode) {
fprintf(stderr, "The scanner encountered an error.\n");
}

CMake 构建文件是

cmake_minimum_required(VERSION 3.0)
project(openshell)
find_package(FLEX)
FLEX_TARGET(ShellScanner shellscanner.l shellscanner.c)
set(CMAKE_VERBOSE_MAKEFILE on)
include_directories(/usr/include/readline)
ADD_EXECUTABLE(lemon lemon.c)
add_custom_command(OUTPUT shellparser.c COMMAND lemon -s shellparser.y DEPENDS shellparser.y)
add_executable(openshell shellparser.c ${FLEX_ShellScanner_OUTPUTS} main.c openshell.h errors.c errors.h util.c util.h stack.c stack.h shellscanner.l shellscanner.h)
file(GLOB SOURCES "./*.c")
target_link_libraries(openshell ${READLINE_LIBRARY} ${FLEX_LIBRARIES})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -std=c99")

我的项目在 my github 上可用.一个典型的 shell session ,由于一些错误,只有一些命令可以工作,如下所示。

> ls -al | sort | wc
argument ::= FILENAME .
argumentList ::= argument .
command ::= FILENAME argumentList .
command ::= FILENAME .
command ::= FILENAME .
commandList ::= command .
commandList ::= command PIPE commandList .
commandList ::= command PIPE commandList .
{(null)} {ls} {-al} {|} {sort} {|} {wc}
45 398 2270
3874: child 3881 status 0x0000
in ::= in commandList EOL .
> who
command ::= FILENAME .
commandList ::= command .
{(null)} {who}
dac :0 2016-04-18 05:17 (:0)
dac pts/2 2016-04-18 05:20 (:0)
3874: child 3887 status 0x0000
in ::= in commandList EOL .
> ls -al | awk '{print $1}'
argument ::= FILENAME .
argumentList ::= argument .
command ::= FILENAME argumentList .
argument ::= ARGUMENT .
argumentList ::= argument .
command ::= FILENAME argumentList .
commandList ::= command .
commandList ::= command PIPE commandList .
{(null)} {ls} {-al} {|} {awk} {'}
awk: cmd. line:1: '
awk: cmd. line:1: ^ invalid char ''' in expression
3874: child 3896 status 0x0100
in ::= in commandList EOL .
>

我可以观察到这两个命令都有相同的错误:echo 'foo bar' 在我们希望得到 {echo} {'} 时出现乱码 {echo} {foo bar} 以便 shell 去除引号并像这样执行命令

char *cmd[] = { "/usr/bin/echo", "foo bar", 0 };

最佳答案

问题出在规则上

<SINGLE_QUOTED>[^']+ { }

因为它删除了引号内的所有字符。所有你得到的“yytext”是结束报价(由于规则 <SINGLE_QUOTED>['] ... )。您必须将文本存储在某处,并在检测到结束引号时使用它。例如。 (非常糟糕的编码风格,错误检查等省略,抱歉)

<SINGLE_QUOTED>[^']+    { mystring = strdup(yytext); }

<SINGLE_QUOTED>['] { BEGIN(INITIAL);
/* mystring contains the whole string now,
yytext contains only "'" */
return ARGUMENT; }

关于c - 错误是在语法中还是在代码中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36696071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com