-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1) Introduced a source file for maintaining utilites (xutils.cpp) (#25)
2) Added test for extract_filename function
- Loading branch information
1 parent
15d4e99
commit d7e4f32
Showing
6 changed files
with
160 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/************************************************************************************ | ||
* Copyright (c) 2023, xeus-cpp contributors * | ||
* * | ||
* Distributed under the terms of the BSD 3-Clause License. * | ||
* * | ||
* The full license is in the file LICENSE, distributed with this software. * | ||
************************************************************************************/ | ||
|
||
#ifndef XEUS_CPP_UTILS_HPP | ||
#define XEUS_CPP_UTILS_HPP | ||
|
||
#include "xinterpreter.hpp" | ||
|
||
using interpreter_ptr = std::unique_ptr<xcpp::interpreter>; | ||
|
||
namespace xcpp | ||
{ | ||
|
||
#ifdef __GNUC__ | ||
XEUS_CPP_API | ||
void handler(int sig); | ||
#endif | ||
|
||
XEUS_CPP_API | ||
void stop_handler(int sig); | ||
|
||
XEUS_CPP_API | ||
bool should_print_version(int argc, char* argv[]); | ||
|
||
XEUS_CPP_API | ||
std::string extract_filename(int &argc, char* argv[]); | ||
|
||
XEUS_CPP_API | ||
interpreter_ptr build_interpreter(int argc, char** argv); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/************************************************************************************ | ||
* Copyright (c) 2023, xeus-cpp contributors * | ||
* * | ||
* Distributed under the terms of the BSD 3-Clause License. * | ||
* * | ||
* The full license is in the file LICENSE, distributed with this software. * | ||
************************************************************************************/ | ||
|
||
#include <cstddef> | ||
#include <memory> | ||
#include <string> | ||
#include <utility> | ||
|
||
#include <signal.h> | ||
|
||
#ifdef __GNUC__ | ||
#include <stdio.h> | ||
#ifndef XEUS_CPP_EMSCRIPTEN_WASM_BUILD | ||
#include <execinfo.h> | ||
#endif | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#endif | ||
|
||
|
||
#include "xeus-cpp/xutils.hpp" | ||
#include "xeus-cpp/xinterpreter.hpp" | ||
|
||
namespace xcpp | ||
{ | ||
|
||
#if defined(__GNUC__) && !defined(XEUS_CPP_EMSCRIPTEN_WASM_BUILD) | ||
void handler(int sig) | ||
{ | ||
void* array[10]; | ||
|
||
// get void*'s for all entries on the stack | ||
std::size_t size = backtrace(array, 10); | ||
|
||
// print out all the frames to stderr | ||
fprintf(stderr, "Error: signal %d:\n", sig); | ||
backtrace_symbols_fd(array, size, STDERR_FILENO); | ||
exit(1); | ||
} | ||
#endif | ||
|
||
void stop_handler(int /*sig*/) | ||
{ | ||
exit(0); | ||
} | ||
|
||
bool should_print_version(int argc, char* argv[]) | ||
{ | ||
for (int i = 0; i < argc; ++i) | ||
{ | ||
if (std::string(argv[i]) == "--version") | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
std::string extract_filename(int &argc, char* argv[]) | ||
{ | ||
std::string res = ""; | ||
for (int i = 0; i < argc; ++i) | ||
{ | ||
if ((std::string(argv[i]) == "-f") && (i + 1 < argc)) | ||
{ | ||
res = argv[i + 1]; | ||
for (int j = i; j < argc - 2; ++j) | ||
{ | ||
argv[j] = argv[j + 2]; | ||
} | ||
argc -= 2; | ||
break; | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
interpreter_ptr build_interpreter(int argc, char** argv) | ||
{ | ||
int interpreter_argc = argc; // + 1; | ||
const char** interpreter_argv = new const char*[interpreter_argc]; | ||
interpreter_argv[0] = "xeus-cpp"; | ||
// Copy all arguments in the new array excepting the process name. | ||
for (int i = 1; i < argc; i++) | ||
{ | ||
interpreter_argv[i] = argv[i]; | ||
} | ||
|
||
interpreter_ptr interp_ptr = std::make_unique<interpreter>(interpreter_argc, interpreter_argv); | ||
delete[] interpreter_argv; | ||
return interp_ptr; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters