Skip to content

Commit d9d23e1

Browse files
committed
Simple bash command
1 parent 5908cd7 commit d9d23e1

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
name = "bashplugin"
44
version = "0.0.1"
55
authors = ["Geoffrey Thomas <[email protected]>"]
6+
7+
[lib]
8+
9+
name = "bashplugin"
10+
crate-type = ["dylib"]

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This is an example bash loadable command written in Rust.
2+
3+
Usage:
4+
```
5+
geofft@cactuar:~/src/scratch/bashplugin$ cargo build
6+
geofft@cactuar:~/src/scratch/bashplugin$ enable -f target/libbashplugin-*.so lulz
7+
geofft@cactuar:~/src/scratch/bashplugin$ lulz
8+
lulz
9+
```

src/bash.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
extern crate libc;
2+
3+
// command.h
4+
5+
#[repr(C)]
6+
pub struct word_desc {
7+
word: *mut libc::c_char,
8+
flags: libc::c_int,
9+
}
10+
11+
pub struct word_list {
12+
next: *mut word_list,
13+
word: *mut word_desc,
14+
}
15+
16+
// general.h
17+
18+
type sh_builtin_func_t = extern fn (*mut word_list) -> libc::c_int;
19+
20+
// builtins.h
21+
22+
pub const BUILTIN_ENABLED: libc::c_int = 0x01;
23+
pub const BUILTIN_DELETED: libc::c_int = 0x02;
24+
pub const STATIC_BUILTIN: libc::c_int = 0x04;
25+
pub const SPECIAL_BUILTIN: libc::c_int = 0x08;
26+
pub const ASSIGNMENT_BUILTIN: libc::c_int = 0x10;
27+
pub const POSIX_BUILTIN: libc::c_int = 0x20;
28+
29+
#[repr(C)]
30+
pub struct builtin {
31+
pub name: *const libc::c_char,
32+
pub function: sh_builtin_func_t,
33+
pub flags: libc::c_int,
34+
pub long_doc: *const *const libc::c_char,
35+
pub short_doc: *const libc::c_char,
36+
pub handle: *const libc::c_char,
37+
}
38+
39+

src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1-
#[test]
2-
fn it_works() {
1+
extern crate libc;
2+
mod bash;
3+
4+
extern fn builtin_lulz(args: *mut bash::word_list) -> libc::c_int {
5+
println!("lulz");
6+
0
37
}
8+
9+
static NAME: [i8; 5] = ['l' as i8, 'u' as i8, 'l' as i8, 'z' as i8, 0 as i8];
10+
11+
#[no_mangle]
12+
pub static mut lulz_struct: bash::builtin = bash::builtin {
13+
name: &NAME as *const i8,
14+
function: builtin_lulz,
15+
flags: bash::BUILTIN_ENABLED,
16+
long_doc: 0 as *const *const i8,
17+
short_doc: &NAME as *const i8,
18+
handle: 0 as *const i8,
19+
};

0 commit comments

Comments
 (0)