Skip to content

Commit 31084c3

Browse files
committed
Init
1 parent c8877f6 commit 31084c3

34 files changed

+2697
-1
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
tmp/*
2+
wsjcpp-safe-scripting
3+
.logs/*
4+
.vscode/*
5+
.wsjcpp/*
6+
17
# Prerequisites
28
*.d
39

.travis.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
language: cpp
2+
3+
branches:
4+
only:
5+
- master
6+
7+
dist: bionic
8+
9+
addons:
10+
apt:
11+
packages:
12+
- cmake
13+
- make
14+
- g++
15+
- pkg-config
16+
17+
# Build steps
18+
script:
19+
- ./build_simple.sh
20+
- ./wsjcpp-safe-scripting scripts.wsjcpp/generate.WSJCppSafeScriptingProc TestProc
21+
- cd unit-tests.wsjcpp
22+
- ./build_simple.sh
23+
- ./unit-tests

CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
3+
project(wsjcpp-safe-scripting C CXX)
4+
5+
include(${CMAKE_CURRENT_SOURCE_DIR}/src.wsjcpp/CMakeLists.txt)
6+
7+
set(CMAKE_CXX_STANDARD 11)
8+
set(EXECUTABLE_OUTPUT_PATH ${wsjcpp-safe-scripting_SOURCE_DIR})
9+
10+
# include header dirs
11+
list (APPEND WSJCPP_INCLUDE_DIRS "src")
12+
13+
list (APPEND WSJCPP_SOURCES "src/main.cpp")
14+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_safe_scripting.h")
15+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_safe_scripting.cpp")
16+
17+
#### BEGIN_WSJCPP_APPEND
18+
list (APPEND WSJCPP_SOURCES "./src/safe_scripting_proc_test_proc.h")
19+
list (APPEND WSJCPP_SOURCES "./src/safe_scripting_proc_test_proc.cpp")
20+
#### END_WSJCPP_APPEND
21+
22+
include_directories(${WSJCPP_INCLUDE_DIRS})
23+
24+
add_executable (wsjcpp-safe-scripting ${WSJCPP_SOURCES})
25+
26+
target_link_libraries(wsjcpp-safe-scripting ${WSJCPP_LIBRARIES})
27+
28+
install(
29+
TARGETS
30+
wsjcpp-safe-scripting
31+
RUNTIME DESTINATION
32+
/usr/bin
33+
)
34+
35+

README.md

Lines changed: 158 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,159 @@
11
# wsjcpp-safe-scripting
2-
Light script language for integration to your project on C++
2+
3+
[![Build Status](https://api.travis-ci.org/wsjcpp/wsjcpp-safe-scripting.svg?branch=master)](https://travis-ci.org/wsjcpp/wsjcpp-safe-scripting) [![Github Stars](https://img.shields.io/github/stars/wsjcpp/wsjcpp-safe-scripting.svg?label=github%20%E2%98%85)](https://github.com/wsjcpp/wsjcpp-safe-scripting/stargazers) [![Github Stars](https://img.shields.io/github/contributors/wsjcpp/wsjcpp-safe-scripting.svg)](https://github.com/wsjcpp/wsjcpp-safe-scripting/) [![Github Forks](https://img.shields.io/github/forks/wsjcpp/wsjcpp-safe-scripting.svg?label=github%20forks)](https://github.com/wsjcpp/wsjcpp-safe-scripting/network/members)
4+
5+
* Light script language for integration to your project
6+
* Script deny access read/write to out folders/files
7+
* Any variables or procedure is global
8+
* Used a procedure simple language
9+
* You can set variables before run script
10+
* You can get variables after run script
11+
* You can extend or disable procedures on c++
12+
* Multiline strings
13+
* no if, for, while
14+
15+
## Integrate to your project
16+
17+
Just include this files:
18+
19+
- src/wsjcpp_core/wsjcpp_core.h
20+
- src/wsjcpp_core/wsjcpp_core.cpp
21+
- src/wsjcpp_safe_scripting.h
22+
- src/wsjcpp_safe_scripting.cpp
23+
24+
## Integrate via wsjcpp
25+
26+
```
27+
$ wsjcpp install https://github.com/wsjcpp/wsjcpp-safe-scripting:master
28+
```
29+
30+
## List of default variables and procedures
31+
32+
Default Variables:
33+
34+
- `rootdir` - directory when will be run script
35+
- `script_filename` - name of script
36+
37+
Default procedures:
38+
39+
- `exit` - will be exit from script with exitcode 0
40+
- `error` - will be exit from script with exitcode -1 (error code)
41+
- `print_all_variables` - print to log all defined variables
42+
- `log_info [args]` - print args
43+
- `var <varname>` - define new variable
44+
- `set_value <varname1> "some"` - set string value to variable
45+
- `set_value <varname1> <varname2>` - set value to variable
46+
- `concat <varname1> [args]` - concatinate string values from all vars to <varname1>
47+
- `make_dir "dir_name"` - try create dirname
48+
- `normalize_class_name <varname1>` - will be removed characters which not in [a-zA-Z0-9]
49+
- `convert_CamelCase_to_snake_case <from> <to>` - convert from CamelCase to snake_case
50+
- `write_file <filepath> <content>` - write variable to file
51+
- `cmakelists_txt_append_wsjcpp <filepath>` - append string like 'list (APPEND WSJCPP_SOURCES "%filepath%")' to CMakeLists.txt into between '#### BEGIN_WSJCPP_APPEND' and '#### END_WSJCPP_APPEND'
52+
53+
## Example of exec script
54+
55+
```
56+
#include <wsjcpp_safe_scripting.h>
57+
58+
...
59+
60+
WSJCppSafeScriptingContext scriptContext;
61+
62+
std::string sCurrentDirectory = WSJCppCore::getCurrentDirectory();
63+
std::string sScriptFileName = "script_filename";
64+
std::vector<std::string> vScriptArgs = {"Some1", "Some2"}; // script args: arg1, arg2
65+
66+
// script content
67+
std::string sScriptContent =
68+
"var test1\n"
69+
"set_value test1 \"test string\"\n"
70+
"log_info test1\n"
71+
"log_info arg1\n"
72+
"log_info arg2\n";
73+
74+
int nResult = scriptContext.exec(
75+
sCurrentDirectory,
76+
sScriptFileName,
77+
sScriptContent,
78+
vScriptArgs
79+
);
80+
81+
if (nResult == 0) {
82+
std::string sTest1 = scriptContext.getVariable("test1")->getValue();
83+
}
84+
```
85+
86+
## Customizing procedures / variables
87+
88+
### New procedures via wsjcpp
89+
90+
```
91+
wsjcpp generate WSJCppSafeScriptingProc YourClassName
92+
```
93+
94+
After this will be generated files and added to your CMakeLists.txt:
95+
```
96+
#### BEGIN_WSJCPP_APPEND
97+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_safe_scripting_proc_your_class_name.h")
98+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_safe_scripting_proc_your_class_name.cpp")
99+
#### END_WSJCPP_APPEND
100+
```
101+
102+
Now you can write you code in this files or move to another place
103+
104+
### New procedures copy-paste-code
105+
106+
header `src/wsjcpp_safe_scripting_proc_your_class_name.h`:
107+
```
108+
#ifndef WSJCPP_SAFE_SCRIPTING_PROC_YOUR_CLASS_NAME_H
109+
#define WSJCPP_SAFE_SCRIPTING_PROC_YOUR_CLASS_NAME_H
110+
111+
#include <wsjcpp_safe_scripting.h>
112+
113+
class WSJCppSafeScriptingProcYourClassName : public WSJCppSafeScriptingProc {
114+
public:
115+
WSJCppSafeScriptingProcYourClassName();
116+
virtual bool exec(const std::vector<WSJCppSafeScriptingVariable *> &m_vArgs);
117+
};
118+
119+
120+
#endif // WSJCPP_SAFE_SCRIPTING_PROC_YOUR_CLASS_NAME_H
121+
```
122+
123+
source-code `src/wsjcpp_safe_scripting_proc_your_class_name.cpp`:
124+
```
125+
#include "wsjcpp_safe_scripting_proc_your_class_name.h"
126+
#include <wsjcpp_core.h>
127+
128+
// ---------------------------------------------------------------------
129+
// WSJCppSafeScriptingProcYourClassName
130+
131+
WSJCppSafeScriptingProcYourClassName::WSJCppSafeScriptingProcYourClassName()
132+
: WSJCppSafeScriptingProc("your_class_name") {
133+
134+
}
135+
136+
// ---------------------------------------------------------------------
137+
138+
bool WSJCppSafeScriptingProcYourClassName::exec(const std::vector<WSJCppSafeScriptingVariable *> &vArgs) {
139+
// you code here
140+
WSJCppLog::err(TAG, "Not implemented")
141+
return false;
142+
}
143+
```
144+
145+
### Add new procedures / variables to script context
146+
147+
Example:
148+
```
149+
WSJCppSafeScriptingContext scriptContext;
150+
151+
// variable
152+
WSJCppSafeScriptingVariable *pVar = new WSJCppSafeScriptingVariable("");
153+
scriptContext.addVar(pVar);
154+
155+
WSJCppSafeScriptingProcYourClassName *pProc = new WSJCppSafeScriptingProcYourClassName();
156+
scriptContext.addProc(pProc);
157+
158+
```
159+

build_simple.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
if [ ! -d tmp ]; then
4+
mkdir -p tmp
5+
fi
6+
7+
cd tmp
8+
cmake ..
9+
make
10+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/wsjcpp-safe-scripting
2+
3+
# log_info rootdir
4+
# log_info script_filename
5+
6+
make_dir "src"
7+
8+
var proc_name
9+
set_value proc_name arg1
10+
normalize_class_name proc_name
11+
convert_CamelCase_to_snake_case proc_name proc_name
12+
13+
var class_name
14+
concat class_name "SafeScriptingProc" arg1
15+
normalize_class_name class_name
16+
17+
var base_filename
18+
convert_CamelCase_to_snake_case class_name base_filename
19+
# log_info base_filename
20+
21+
var filename_cpp
22+
concat filename_cpp "./src/" base_filename ".cpp"
23+
24+
var filename_h
25+
concat filename_h "./src/" base_filename ".h"
26+
27+
var ifndef_header
28+
set_value ifndef_header base_filename
29+
concat ifndef_header "_H"
30+
31+
to_upper_case ifndef_header
32+
33+
var content_header
34+
concat content_header "#ifndef " ifndef_header "
35+
#define " ifndef_header "
36+
37+
#include <wsjcpp_safe_scripting.h>
38+
39+
class " class_name " : public WSJCppSafeScriptingProc {
40+
public:
41+
" class_name "();
42+
virtual bool exec(const std::vector<WSJCppSafeScriptingVariable *> &m_vArgs);
43+
};
44+
45+
#endif // " ifndef_header
46+
47+
48+
var content_source
49+
concat content_source "
50+
#include \"" base_filename ".h\"
51+
#include <wsjcpp_core.h>
52+
53+
// ---------------------------------------------------------------------
54+
// " class_name "
55+
56+
" class_name "::" class_name "()
57+
: WSJCppSafeScriptingProc(\"" proc_name "\") {
58+
59+
}
60+
61+
// ---------------------------------------------------------------------
62+
63+
bool " class_name "::exec(const std::vector<WSJCppSafeScriptingVariable *> &vArgs) {
64+
WSJCppLog::err(TAG, \"Not implemented\");
65+
return false;
66+
}
67+
"
68+
69+
var file_source
70+
concat file_source "src/" filename_cpp
71+
72+
write_file filename_h content_header
73+
write_file filename_cpp content_source
74+
75+
log_info "
76+
======
77+
Generated class:
78+
- " class_name "
79+
Generated files:
80+
- " filename_h "
81+
- " filename_cpp "
82+
======
83+
"
84+
85+
cmakelists_txt_append_wsjcpp filename_h
86+
cmakelists_txt_append_wsjcpp filename_cpp

src.wsjcpp/.gitkeep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src.wsjcpp/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Automaticly generated by wsjcpp@v0.0.1
2+
cmake_minimum_required(VERSION 3.0)
3+
4+
add_definitions(-DWSJCPP_VERSION="v0.0.1")
5+
add_definitions(-DWSJCPP_NAME="wsjcpp-safe-scripting")
6+
7+
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
8+
set(MACOSX TRUE)
9+
endif()
10+
11+
set(CMAKE_CXX_STANDARD 11)
12+
13+
set (WSJCPP_LIBRARIES "")
14+
set (WSJCPP_INCLUDE_DIRS "")
15+
set (WSJCPP_SOURCES "")
16+
17+
find_package(Threads REQUIRED)
18+
list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
19+
20+
# wsjcpp-core:v0.0.7
21+
list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/wsjcpp_core/")
22+
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp")
23+
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.h")
24+
25+

0 commit comments

Comments
 (0)