Skip to content

Commit

Permalink
Add: feed transpile command
Browse files Browse the repository at this point in the history
Adds a new feature called transpile to feed of nasl-cli.

With it you can manipulate a feed, currently it is able to rename,
remove, add, push parameter or functions within a feed.

To demonstrate it there is an example within
`examples/nasl-cli/transpile.toml` to show case
- rename service `www` to `word-wide-web` in register_product
- `register_host_detail` to `add_host_detail`

to execute it call:

```
nasl-cli -v feed transpile -p /tmp/feed\
  -r examples/nasl-cli/transpile.toml
```

Additionally to line and colum number byte ranges got added. This allows
us to lookup complete statements without having to create line no lookup
tables or reading twice.

Usually the statements get discarded after execution so the additional
memory should not be a big issue.
  • Loading branch information
nichtsfrei committed Nov 15, 2023
1 parent 6775b61 commit 51ae0a4
Show file tree
Hide file tree
Showing 32 changed files with 2,615 additions and 1,248 deletions.
472 changes: 194 additions & 278 deletions rust/Cargo.lock

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions rust/examples/nasl-cli/transpile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This demo shows how to use a configuration to rename:
# `www` to `world-wide-web` when in register_product the parameter cpe, location, port are set and when service is set to `www`
# `register_host_detail` to `add_host_detail`
# To test it get a community feed and run:
# ```
# nasl-cli -v feed transpile -p /tmp/feed -r example/replace.toml
# ```

[[cmds]]

[cmds.find]
FunctionByNameAndParameter = [
"register_product",
[
{ Name = "cpe" },
{ Name = "location" },
{ Name = "port" },
{ NameValue = [
"service",
"\"www\"",
] },
],
]

[cmds.with.Parameter.Push]
Named = [
"service_to_be",
"\"world-wide-web\"",
]

[[cmds]]

[cmds.find]
FunctionByNameAndParameter = [
"register_product",
[
{ Name = "cpe" },
{ Name = "location" },
{ Name = "port" },
{ Name = "service" },
{ Name = "service_to_be" },
],
]

[cmds.with.Parameter]
RemoveNamed = "service"

[[cmds]]

[cmds.find]
FunctionByName = "register_product"

[cmds.with.Parameter.Rename]
previous = "service_to_be"
new = "service"

[[cmds]]

[cmds.find]
FunctionByName = "register_host_detail"

[cmds.with]
Name = "add_host_detail"
5 changes: 5 additions & 0 deletions rust/feed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ sequoia-ipc = "0.30.1"
sequoia-openpgp = { version ="1.16.1", default-features = false, features = ["crypto-openssl"] }
anyhow = "1.0.75"
tracing = "0.1.37"
glob = "0.3.1"
serde = { version = "1.0", features = ["derive"]}

[dev-dependencies]
toml = "0.8.8"
2 changes: 2 additions & 0 deletions rust/feed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
mod oid;
pub mod transpile;
mod update;
mod verify;

Expand All @@ -17,3 +18,4 @@ pub use verify::Error as VerifyError;
pub use verify::FileNameLoader;
pub use verify::HashSumNameLoader;
pub use verify::Hasher;
pub use verify::NaslFileFinder;
6 changes: 3 additions & 3 deletions rust/feed/src/oid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ where

fn script_oid(stmt: &Statement) -> Option<String> {
match stmt {
Statement::Call(t, stms) => match t.category() {
Statement::Call(t, stms, _) => match t.category() {
TokenCategory::Identifier(IdentifierType::Undefined(s)) => match s as &str {
"script_oid" => stms.first().map(|x| x.to_string()),
_ => None,
Expand All @@ -52,8 +52,8 @@ where
fn single(&self, key: String) -> Result<String, update::ErrorKind> {
let code = self.loader.load(key.as_ref())?;
for stmt in nasl_syntax::parse(&code) {
if let Statement::If(_, stmts, _) = stmt? {
if let Statement::Block(x) = &*stmts {
if let Statement::If(_, _, stmts, _, _) = stmt? {
if let Statement::Block(_, x, _) = &*stmts {
for stmt in x {
if let Some(oid) = Self::script_oid(stmt) {
return Ok(oid);
Expand Down
Loading

0 comments on commit 51ae0a4

Please sign in to comment.