mirror of
https://github.com/louisrubet/rpn
synced 2024-12-28 09:58:52 +01:00
Merge branch 'no-way-out-if-ctrlc' into develop
This commit is contained in:
commit
9b46aefd3a
6 changed files with 56 additions and 28 deletions
6
.gitmodules
vendored
Normal file
6
.gitmodules
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
[submodule "build/linenoise-ng"]
|
||||
path = build/linenoise-ng
|
||||
url = git@github.com:louisrubet/linenoise-ng.git
|
||||
[submodule "linenoise-ng"]
|
||||
path = linenoise-ng
|
||||
url = git@github.com:louisrubet/linenoise-ng.git
|
|
@ -29,18 +29,26 @@ if(CMAKE_COMPILER_IS_GNUCXX)
|
|||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${BASE_COMPILER_OPTIONS} -O3 -fomit-frame-pointer -s")
|
||||
endif()
|
||||
|
||||
# custom linenoise-ng
|
||||
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/linenoise-ng/.git")
|
||||
execute_process(COMMAND git submodule init ${PROJECT_SOURCE_DIR}/linenoise-ng)
|
||||
execute_process(COMMAND git submodule update ${PROJECT_SOURCE_DIR}/linenoise-ng)
|
||||
execute_process(COMMAND git checkout v1.0.1-rpn WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/linenoise-ng)
|
||||
endif()
|
||||
|
||||
# includes
|
||||
include_directories(${PROJECT_SOURCE_DIR}/src)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/linenoise-ng/include)
|
||||
|
||||
# build
|
||||
add_executable(
|
||||
rpn
|
||||
src/main.cpp
|
||||
src/object.cpp
|
||||
src/program.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/main.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/object.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/program.cpp
|
||||
${PROJECT_SOURCE_DIR}/linenoise-ng/src/ConvertUTF.cpp
|
||||
${PROJECT_SOURCE_DIR}/linenoise-ng/src/linenoise.cpp
|
||||
${PROJECT_SOURCE_DIR}/linenoise-ng/src/wcwidth.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(rpn liblinenoise.a)
|
||||
target_link_libraries(rpn mpfr)
|
||||
|
||||
# man
|
||||
|
|
1
linenoise-ng
Submodule
1
linenoise-ng
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 32d3c3dcd65e2ce622084cd8057b25271561512e
|
|
@ -55,6 +55,7 @@ typedef enum {
|
|||
ret_internal,
|
||||
ret_deadly,
|
||||
ret_good_bye,
|
||||
ret_abort_current_entry,
|
||||
ret_not_impl,
|
||||
ret_nop,
|
||||
ret_syntax,
|
||||
|
@ -65,7 +66,7 @@ typedef enum {
|
|||
|
||||
#define RET_VALUE_STRINGS { \
|
||||
"ok", "unknown command", "missing operand", "bad operand type", "out of range", "unknown variable", "internal error, aborting", \
|
||||
"deadly", "goodbye", "not implemented", "no operation", "syntax error", "division by zero", "runtime error" \
|
||||
"deadly", "goodbye", "aborted current entry", "not implemented", "no operation", "syntax error", "division by zero", "runtime error" \
|
||||
}
|
||||
|
||||
// command types
|
||||
|
|
14
src/main.cpp
14
src/main.cpp
|
@ -90,6 +90,7 @@ static void catch_signals(program* prog)
|
|||
int main(int argc, char* argv[])
|
||||
{
|
||||
int ret = 0;
|
||||
bool go_on = true;
|
||||
|
||||
// apply default configuration
|
||||
program::apply_default();
|
||||
|
@ -101,14 +102,18 @@ int main(int argc, char* argv[])
|
|||
init_interactive_rpn();
|
||||
|
||||
// entry loop
|
||||
for (;;)
|
||||
while(go_on)
|
||||
{
|
||||
// make program from interactive entry
|
||||
program prog;
|
||||
if (program::entry(prog) == ret_good_bye)
|
||||
break;
|
||||
else
|
||||
switch (program::entry(prog))
|
||||
{
|
||||
case ret_good_bye:
|
||||
go_on = false;
|
||||
break;
|
||||
case ret_abort_current_entry:
|
||||
break;
|
||||
default:
|
||||
// user could stop prog with CtrlC
|
||||
catch_signals(&prog);
|
||||
|
||||
|
@ -117,6 +122,7 @@ int main(int argc, char* argv[])
|
|||
break;
|
||||
else
|
||||
program::show_stack(s_global_stack);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,17 +32,23 @@ static ret_value parse(const char* entry, program& prog)
|
|||
static ret_value entry(program& prog)
|
||||
{
|
||||
char* entry;
|
||||
int entry_len;
|
||||
ret_value ret;
|
||||
|
||||
// linenoise for entry
|
||||
linenoiseSetCompletionCallback(program::entry_completion_generator);
|
||||
|
||||
// get user entry
|
||||
entry = linenoise(PROMPT);
|
||||
entry = linenoise(PROMPT, &entry_len);
|
||||
|
||||
// Ctrl-C
|
||||
if (linenoiseKeyType() == 1)
|
||||
{
|
||||
if (entry_len > 0)
|
||||
ret = ret_abort_current_entry;
|
||||
else
|
||||
ret = ret_good_bye;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (entry != NULL)
|
||||
|
|
Loading…
Reference in a new issue