aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mds/cstruct2luatable.md69
-rw-r--r--mds/lazymakefiles.md46
-rw-r--r--mds/oneclientforeverything.md32
-rw-r--r--put_in_db.js10
4 files changed, 121 insertions, 36 deletions
diff --git a/mds/cstruct2luatable.md b/mds/cstruct2luatable.md
index a8eb92c..8e5bdf5 100644
--- a/mds/cstruct2luatable.md
+++ b/mds/cstruct2luatable.md
@@ -1,22 +1,27 @@
# C Struct to Lua table
## Overview
+
For this tutorial we'll change a C struct into a Lua table. The structure we'll be using won't be the simplest structure you'll come across in the wild so hopefully the tutorial will do a little more than just cover the basics.<br/>
We'll add the structures as `userdata` and not as `lightuserdata`. Because of that, we won't have to manage the memory ourselves, instead we will let Lua's GC handle it for us.<br/>
Disclaimer:
-* This turotial is not supposed to be a full dive into lua tables, metatables and their implementation or behavior. The tutorial is meant as an entry point into implementing custom Lua tables.<br/>
+
+- This turotial is not supposed to be a full dive into lua tables, metatables and their implementation or behavior. The tutorial is meant as an entry point into implementing custom Lua tables.<br/>
### Yet Another One?
+
There are already a couple of tutorials on this, yes, but the ones I managed to find were all targeting older versions of lua and as the Lua devs have clearly stated, different Lua version are really different. The other reason I wrote this is I needed a structures that had structure members themselves and I couldn't find a tutorial for that.<br/>
This tutorial will be targeting Lua 5.3.<br/>
We'll also be using a not-so-simple structure to turn into a Lua table.<br/>
### What you'll need
-* A working C compiler(I'll be using clang)
-* Make
-* you can get the repo [here](https://github.com/bloodstalker/blogstuff/tree/master/src/cstruct2luatbale).<br>
+
+- A working C compiler(I'll be using clang)
+- Make
+- you can get the repo [here](https://github.com/bloodstalker/blogstuff/tree/master/src/cstruct2luatbale).<br>
## C Structs
+
First let's take a look at the C structures we'll be using.<br/>
The primary structure is called `a_t` which has, inside it, two more structures `b_t` and `c_t`:
@@ -29,30 +34,36 @@ typedef struct {
c_t** a_pp;
} a_t;
```
+
```c
typedef struct {
uint32_t b_int;
double b_float;
} b_t;
```
+
```c
typedef struct {
char* c_string;
uint32_t c_int;
} c_t;
```
+
The structures are purely artificial.<br/>
## First Step: Lua Types
+
First let's take a look at `a_t` and decide how we want to do this.<br/>
`a_t` has five members:<br/>
-* `a_int` which in Lua we can turn into an `integer`.
-* `a_float` which we can turn into a `number`.
-* `a_string` which will be a Lua `string`.
-* `a_p` which is a pointer to another structure. As previously stated, we will turn this into a `userdata`.<br/>
-* `a_pp` which is a double pointer. We will turn this into a table of `userdata`.<br/>
+
+- `a_int` which in Lua we can turn into an `integer`.
+- `a_float` which we can turn into a `number`.
+- `a_string` which will be a Lua `string`.
+- `a_p` which is a pointer to another structure. As previously stated, we will turn this into a `userdata`.<br/>
+- `a_pp` which is a double pointer. We will turn this into a table of `userdata`.<br/>
## Second Step: Helper Functions
+
Now let's think about what we need to do. First we need to think about how we will be using our structures. For this example we will go with a pointer, i.e., our library code will get a pointer to the structure so we need to turn the table into `userdata`.<br/>
Next, we want to be able to push and pop our new table from the Lua stack.<br/>
We can also use Lua's type check to make sure our library code complains when someone passes a bad type.<br/>
@@ -60,6 +71,7 @@ We will also add functions for pushing the structure arguments onto the stack, a
Let's start:
First we will write a function that checks the type and returns the C structure:<br/>
+
```c
static a_t* pop_a_t(lua_State* ls, int index) {
a_t* dummy;
@@ -68,6 +80,7 @@ static a_t* pop_a_t(lua_State* ls, int index) {
return dummy;
}
```
+
We check to see if the stack index we are getting is actually a userdata type and then check the type of the userdata we get to make sure we get the right userdata type. We check the type of the userdata by checking its metatable. We will get into that later.<br/>
This amounts to our "pop" functionality for our new type.<br/>
Now let's write a "push":<br/>
@@ -97,6 +110,7 @@ We will need a key. for simplicity's sake we use the pointer that `lua_newuserda
Lastly we just set our key-value pair with `LUA_REGISTRYINDEX`.<br/>
Next we will write a function that pushes the fields of the structure onto the stack:<br/>
+
```c
int a_t_push_args(lua_State* ls, a_t* a) {
if (!lua_checkstack(ls, 5)) {
@@ -111,9 +125,11 @@ int a_t_push_args(lua_State* ls, a_t* a) {
return 5;
}
```
+
Notice that we are returning 5, since our new next function which is the new function expects to see the 5 fields on top of the stack.<br/>
Next up is our new function:<br/>
+
```c
int new_a_t(lua_State* ls) {
if (!lua_checkstack(ls, 6)) {
@@ -135,10 +151,12 @@ int new_a_t(lua_State* ls) {
return 1;
}
```
+
We just push an `a_t` on top of stack and then populate the fields with the values already on top of stack.<br/>
The fact that we wrote tha two separate functions for pushing the arguments and returning a new table instance means we can use `new_a_t` as a constructor from lua as well. We'll later talk about that.<br>
## Third Step: Setters and Getters
+
Now lets move onto writing our setter and getter functions.<br/>
For the non-userdata types its fairly straightforward:<br/>
@@ -155,6 +173,7 @@ static int getter_a_string(lua_State* ls) {
return 1;
}
```
+
As for the setters:<br/>
```c
@@ -166,6 +185,7 @@ static int setter_a_int(lua_State* ls) {
```
Now for the 4th and 5th fields:<br/>
+
```c
static int getter_a_p(lua_State *ls) {
a_t* dummy = pop_a_t(ls, 1);
@@ -176,7 +196,7 @@ static int getter_a_p(lua_State *ls) {
}
```
-For the sake of laziness, let's assume `a_t->a_int` denotes the number of entries in `a_t->a_pp`.<br/>
+For the sake of laziness, let's assume `a_t->a_int` denotes the number of entries in `a_t->a_pp`.<br/>
```c
static int getter_a_pp(lua_State* ls) {
@@ -206,16 +226,19 @@ Since we register all our tables with `LUA_REGISTRYINDEX` we just retreive the k
As you can see, for setters we are assuming that the table itself is being passed as the first argument(the `pop_a_t` line assumes that).<br/>
Our setters methods would be called like this in Lua:<br/>
+
```lua
local a = a_t()
a:set_a_int(my_int)
```
+
The `:` operator in Lua is syntactic sugar. The second line from the above snippet is equivalent to `a.set_a_int(self, my_int)`.<br/>
As you can see, the table itself will always be our first argument. That's why our assumption above will always be true if the lua code is well-formed.<br/>
We do the same steps above for `b_t` and `c_t` getter functions.<br/>
Now let's look at our setters:<br/>
+
```c
static int setter_a_string(lua_State *ls) {
a_t* dummy = pop_a_t(ls, 1);
@@ -254,7 +277,9 @@ static int setter_a_pp(lua_State* ls) {
We are all done with the functions we needed for our new table. Now we need to register the metatable we kept using:<br/>
# Fourth Step: Metatable
+
First, if you haven't already, take a look at the chapter on metatable and metamethods on pil [here](https://www.lua.org/pil/13.html).<br/>
+
```c
static const luaL_Reg a_t_methods[] = {
{"new", new_a_t},
@@ -272,11 +297,13 @@ static const luaL_Reg a_t_methods[] = {
static const luaL_Reg a_t_meta[] = {{0, 0}};
```
+
We just list the functions we want to be accessible inside Lua code.<br/>
Lua expects the C functions that we register with Lua to have the form `(int)(func_ptr*)(lua_State*)`.<br/>
Also, it's a good idea to take a look at the metatable events that Lua 5.3 supports [here](http://lua-users.org/wiki/MetatableEvents). They provide customization options for our new table type(as an example we get the same functionality as C++ where we get to define what an operator does for our table type).<br/>
Now we move on to registering our metatable with Lua:<br/>
+
```c
int a_t_register(lua_State *ls) {
lua_checkstack(ls, 4);
@@ -295,6 +322,7 @@ int a_t_register(lua_State *ls) {
return 0;
}
```
+
Please note that we are registering the metatable as a global. It is generally not recommended to do so.Why you ask?<br/>
Adding a new enrty to the global table in Lua means you are already reserving that keyword, so if another library also needs that key, you are going to have lots of fun(the term `fun` here is borrowed from the Dwarf Fortress literature).<br/>
Entries in the global table will require Lua to look things up in the global table so it slows things down a bit, though whether the slow-down is signifacant enough really depends on you and your requirements.<br/>
@@ -306,21 +334,25 @@ Remember metatable events?<br/>
That's what we'll use.<br/>
Lua metatables support something called metatable events. Eeach event has a string key and the value is whatever you put as the value.<br/>
The values are used whenever that event happens. Some the events are:
-* `__call`
-* `__pairs`
-* `__sub`
-* `__add`
-* `__gc`
-The `__sub` event is triggered when your table is the operand of a suntraction operator. `__gc` is used when lua want to dispose of the table so if you are handling the memory yourself, in contrast to letting Lua handle it for you, here's where you free memory.<br/>
-The events are a powerful tool that help us customize how our new table behaves.<br/>
+
+- `__call`
+- `__pairs`
+- `__sub`
+- `__add`
+- `__gc`
+ The `__sub` event is triggered when your table is the operand of a suntraction operator. `__gc` is used when lua want to dispose of the table so if you are handling the memory yourself, in contrast to letting Lua handle it for you, here's where you free memory.<br/>
+ The events are a powerful tool that help us customize how our new table behaves.<br/>
For a constructor, we will use the `__call` event.<br/>
That means when someone calls our metatable in Lua, like this(call event is triggered when our table is called, syntactically speaking):<br/>
+
```lua
local a = a_t()
```
+
`a` will become a new instance of our table.<br/>
We can add a value for our metatable's `__call` key from either Lua or C. Since we are talking about Lua and haven't almost written anything in Lua, let's do it in Lua:<br/>
+
```lua
setmetatable(a_t, {__call =
function(self, arg1, arg2, arg3, arg4, arg5)
@@ -330,13 +362,16 @@ setmetatable(a_t, {__call =
}
)
```
+
We use our `new` method which we previously registered for our metatable. Note that Lua will pass `nil` for the argument if we don't provide any. That's how our cozy constructor works.<br/>
## Final Words
+
The tutorial's goal is to show you one way of doing the task and not necessarily the best way of doing it. Besides, depending on your situation, you might want to do things differently so by no means is this tutorial enough. It's an entry level tutorial.<br/>
Any feedback, suggestions and/or fixes to the tutorial is much appreciated.<br/>
## Shameless Plug
+
I needed to turn a C struct into a lua table for an application I'm working [on](https://github.com/bloodstalker/mutator/tree/master/bruiser). Further down the line, I needed to do the same for a lot more C structs with the possibility of me having to do the same for a lot more C structs. I just couldn't bring myself to do it manually for that many C structs so I decided to work on a code generator that does that for me. The result is [luatablegen](https://github.com/bloodstalker/luatablegen).<br/>
`luatablegen` is a simple script that takes the description of your C structures in an XML file and generates the C code for your new tables and metatables. It does everything we did by hand automatically for us.<br/>
`lautablegen` is in its early stages, so again, any feedback or help will be appreciated.<br/>
diff --git a/mds/lazymakefiles.md b/mds/lazymakefiles.md
index be71c46..735b40c 100644
--- a/mds/lazymakefiles.md
+++ b/mds/lazymakefiles.md
@@ -8,22 +8,26 @@ So I just decided to write a lazy makefile so I would never have to type in the
First off, you can find the makefiles [here](https://github.com/bloodstalker/lazymakefiles). They are licensed under the Unlicense. And I'm using plural because there's one for C and one for C++.<br/>
Now that we are done with the mandatory whimsical introduction, let's talk about the contents of the makefiles.<br/>
There are also a couple of things to note:<br/>
-* The makefiles have been written with gnu make in mind.
-* Most targets will be fine with gcc but the full functionality is achieved by using clang.<br/>
-* This is not a makefile 101.
-* I'm not going to try to copy the makefile contents here line by line. You are expected to have the makefile open while reading this.
-* I will be explaining some of the more, let's say, esoteric behaviours of make which can get the beginners confused.
-* gnu make variables are considered macros by C/C++ standards. I will use the term "variable" since it's what the gnu make documents use.
-* The makefiles are not supposed to be hands-off. I change bits here and there from project to project.<br/>
-* The makefile recognizes the following extensions: `.c` and `.cpp`. If you use different extensions, change the makefile accordingly.<br/>
+
+- The makefiles have been written with gnu make in mind.
+- Most targets will be fine with gcc but the full functionality is achieved by using clang.<br/>
+- This is not a makefile 101.
+- I'm not going to try to copy the makefile contents here line by line. You are expected to have the makefile open while reading this.
+- I will be explaining some of the more, let's say, esoteric behaviours of make which can get the beginners confused.
+- gnu make variables are considered macros by C/C++ standards. I will use the term "variable" since it's what the gnu make documents use.
+- The makefiles are not supposed to be hands-off. I change bits here and there from project to project.<br/>
+- The makefile recognizes the following extensions: `.c` and `.cpp`. If you use different extensions, change the makefile accordingly.<br/>
## The Macros
+
`TARGET` holds the target name. It uses the `?=` assignment operator so you can pass it a different value from a script, just in case.<br/>
There are a bunch of varibales that you can assign on the terminal to replace the makefile's defaults. Among those there are some that are first getting a default value assigned and then get the `?=` assignemnt operator so you can assign them values from the terminal, e.g:<br/>
+
```make
CC=clang
CC?=clang
```
+
It looks a bit backwards but there is a reason for that. The reason why we need to do that is because those variables are called `implicit variables` in gnu make terminology. Implicit variables are already defined by your makefile even if you havent defined them so they get some special treatment.<br/>
In order to assign them values from the terminal, we first assign them a value and then use the `?=` operator on them. We don't really need to assign the default value here again, but I felt like it would be more expressive to assign the default for a second time.<br/>
@@ -34,66 +38,87 @@ The variable `BUILD_MODE` is used for the sanitizer builds of clang. `ADDSAN` wi
## Targets
### default
+
The default target is `all`. `all` depends on `TARGET`.<br/>
### all
+
`all` is an aggregate target. calling it will build, or rather, try to build everything(given your source-code's sitation, some targets might not make any sense).<br/>
### depend
+
`depend` depends on `.depend` which is a file generated by the makefile that holds the header dependencies. This is how we are making the makefile sensitive to header changes.<br/>
The file's contents look like this:<br/>
+
```make
main.c:main.h
myfile1.c:myfile1.h myfile2.h
```
+
The inclusion directive is prefixed with a `-`. That's make lingo for ignore-if-error. My shell prompt has a `make -q` part in it so just `cd`ing into a folder will generate the `.depend` file for me.Lazy and Convinient.<br/>
### Objects
+
For the objects, there are three sets. You have the normal garden variety objects that end in `.o`. You get the debug enabled objects that end in `.odbg` and you get the instrumented objectes that are to be used for coverage that end in `.ocov`. I made the choice of having three distinct sets of objects since I personally sometimes struggle to remember whether the current objects are normal, debug or coverage. This way, I don't need to. That's the makefile's problem now.<br/>
### TARGET
+
Vanilla i.e. the dynamically-linked executable.<br/>
### TARGET-static
+
The statically-linked executable.<br/>
### TARGET-dbg
+
The dynamically-linked executble in debug mode.<br/>
### TARGET-cov
+
The instrumented-for-coverage executable, dynaimclly-linked.<br/>
### cov
+
The target generates the coverage report. it depend on `runcov` which itself, in turn, depends on `$(TARGET)-cov` so if you change `runcov` to how your executable should run, cov will handle rebuilding the objects and then running and generating the coverage report.<br/>
### covrep
+
The exact same as above but generates coverage report in a different format.<br/>
### ASM
+
Generates the assembly files for your objects, in intel style.<br/>
### SO
+
Will try to build your target as a shared object.<br/>
### A
+
Will try to build your target as an archive, i.e. static library.<br/>
### TAGS
+
Depends on the `tags` target, generates a tags file. The tags file includes tags from the header files included by your source as well.<br/>
### valgrind
-Depends on `$(TARGET)` by default, runs valgrind with `--leak-check=yes`. You probably need to change this for the makefile to run your executable correctly.<br/>
+
+Depends on `$(TARGET)` by default, runs valgrind with `--leak-check=yes`. You probably need to change this for the makefile to run your executable correctly.<br/>
### format
-Runs clang-format on all your source files and header files and **__EDITS THEM IN PLACE__**. Expects a clang format file to be present in the directory.<br/>
+
+Runs clang-format on all your source files and header files and \***\*EDITS THEM IN PLACE\*\***. Expects a clang format file to be present in the directory.<br/>
### js
+
Builds the target using emscripten and generates a javascript file.<br/>
### clean and deepclean
+
`clean` cleans almost everything. `deepclean` depends on `clean`. basically a two level scheme so you can have two different sets of clean commands.<br/>
### help
+
prints out the condensed version of what I've been trying to put into words.<br/>
Well that's about it.<br/>
@@ -343,6 +368,7 @@ help:
```
## Cpp
+
```make
TARGET?=main
SHELL=bash
diff --git a/mds/oneclientforeverything.md b/mds/oneclientforeverything.md
index 6b633ef..f11f646 100644
--- a/mds/oneclientforeverything.md
+++ b/mds/oneclientforeverything.md
@@ -1,15 +1,14 @@
# One Client for Everything
-
# Table of Contents
+
1. [Foreword](#foreword)
2. [Two ways of solving this](#two-ways-of-solving-this)
3. [The web app way](#the-web-app-way)
4. [gui or terminal client](#gui-or-terminal-client)
5. [Matrix or IRC](#matrix-or-irc)
-
+
## Foreword
-
First let's talk about the problem we're trying to solve here. I want to have a unified interface into all the communication forms that I use.<br/>
I can't be bothered to have different clients open all the time. I want to have one client that takes care of all things mostly well.<br/>
@@ -43,7 +42,7 @@ Also as an added bonus, starting from the next irssi release which should be irs
Matrix and IRC both have a rich ecosystem of bridges. Matrix has a growing fan base which means more and more bridges or tools with similar functionality will be releases for it. Contrast that with IRC where that number seems to be smaller than Matrix but still is very much alive and well.<br/>
## [bitlbee-libpurple](https://github.com/bitlbee/bitlbee)
-
+
```
it'll be bitlbee
```
@@ -52,8 +51,8 @@ bitlbee is a bridge software for IRC. The distinguishing feature for bitlbee is
You could also use libpurple as the backend for bitlbee ([link](https://wiki.bitlbee.org/HowtoPurple)).<br/>
libpurple has an origin story similar to libreadline. Basically it used to live inside pidgin, but later on it was turned into a library so that other applications could use it as well.<br/>
-
List of protocols supported by libpurple:<br/>
+
```
aim
bitlbee-discord
@@ -82,25 +81,29 @@ steam
telegram-tdlib
zephyr
```
-
+
## [matterbridge](https://github.com/42wim/matterbridge)
+
matterbridge is an everything-to-everything bridge.<br/>
Please keep in mind that with matterbridge, you don't get the full functionality of a protocol as in you get no private messages and such. You get the ability to join public chat rooms or whatever they call it in that protocol.<br/>
-
+
## bridge ircds
### [matterircd](https://github.com/42wim/matterircd)
+
a mattermost bridge that emulates an ircd as the name implies.
### [matrix2051](https://github.com/progval/matrix2051)
+
another bridge that emulates an ircd, but for matrix.
### [irslackd](https://github.com/adsr/irslackd)
-a bridge to slack that emulates an ircd.
+a bridge to slack that emulates an ircd.
### docker compose
+
[Here](https://github.com/ezkrg/docker-bitlbee-libpurple)'s the original Dockerfile. You can find mine [here](https://github.com/terminaldweller/docker-bitlbee-libpurple).<br/>
And here's the docker compose file I use that goes with that:<br/>
@@ -124,7 +127,18 @@ services:
- "172.17.0.1:8667:6667"
restart: unless-stopped
user: "bitlbee:bitlbee"
- command: ["/usr/sbin/bitlbee", "-F","-n","-u","bitlbee","-c","/var/lib/bitlbee/bitlbee.conf", "-d","/var/lib/bitlbee"]
+ command:
+ [
+ "/usr/sbin/bitlbee",
+ "-F",
+ "-n",
+ "-u",
+ "bitlbee",
+ "-c",
+ "/var/lib/bitlbee/bitlbee.conf",
+ "-d",
+ "/var/lib/bitlbee",
+ ]
dns:
- 9.9.9.9
volumes:
diff --git a/put_in_db.js b/put_in_db.js
index eb91672..cbc7f8d 100644
--- a/put_in_db.js
+++ b/put_in_db.js
@@ -32,6 +32,16 @@ blogs = [
keywords: "irc,matrix,mattermost,matterbridge,bitlbee,irssi",
lastUpdatedAt: Date.now(),
},
+ {
+ title: "How to Get your SMS on IRC",
+ slug: "how_to_get_your_sms_on_irc",
+ body: fs.readFileSync(
+ path.join(__dirname) + "/mds/howtogetyourSMSonIRC.md",
+ ),
+ teaser: "How to Get your SMS on IRC",
+ keywords: "irc,bot,sms",
+ lastUpdatedAt: Date.now(),
+ },
];
// db.blogposts.insertMany(blogs);