LIBMAWK_REGISTER_FUNCTION(3libmawk)
β¨ LIBMAWK_REGISTER_FUNCTION β¨
the libmawk manual Β· section 3libmawk Β· bestie we are SO deep in the C weeds rn and I am thriving π
π NAME π
libmawk_register_function β registers a C function with a callback π«Ά like you're literally donating a function from your host app into mawk's world
πΈ SYNOPSIS πΈ
#include <libmawk.h>
typedef CELL *libmawk_c_function(mawk_state_t *m, CELL *sp, int a_args);
int libmawk_register_function(mawk_state_t *MAWK, const char *fname,
libmawk_c_function *callback);
CELL *libmawk_stackret(CELL *original_sp);
π DESCRIPTION π
okay so. libmawk_register_function() is the glow-up move where your host application donates a user defined function into a mawk context, and once it's in there it acts exactly like user defined functions written in awk. no cap. it's giving native.
the name of the new function goes in fname, and it should not match any of the user defined function names in the awk script β like, don't do that, that's a name collision and we don't want the drama π
when the user function gets called back, the argument sp is the stack pointer and a_args holds the number of arguments. and here's the part where you gotta lock in: the user function is responsible for managing the stack. it should pop all arguments before returning. yes bestie, you pop them, that's on you π
the user function should also generate a return value, which you do by calling libmawk_set_cell() on the stack slot returned by libmawk_stackret. and libmawk_stackret should be called with the modified sp after popping all arguments. order matters, periodt β¨
argument m is a libmawk context previously returned by libmawk_initialize() or libmawk_initialize_stage3().
for more info about user function callbacks β especially on stack handling, which is the whole vibe honestly β see the manual page example(3libmawk) π
π RETURN VALUE π
- the user function should return the stack pointer after popping all arguments
- the
libmawk_register_functioncall returns0on success π - call
libmawk_stackretreturns a stack pointer to the slot where the user function should store its return value
fr fr, read those twice. the stack pointer you hand back is not the same energy as the stack pointer you got handed. π«
π¦ SEE ALSO π¦
libmawk_initialize_stage(3libmawk)libmawk_initialize(3libmawk)libmawk_set_cell(3libmawk)libmawk_print_cell(3libmawk)
and that's the whole page, bestie β¨ this icon of a manual page is from libmawk, dated 2009-08-12 β yes she's vintage, and yes she still ate π go forth and pop your stack correctly, mwah π
π¦ where this page comes from
- debian bookworm: libmawk1 1.0.2-3
- debian trixie: libmawk1t64 1.0.4-4
- fedora 43: libmawk-devel 1.0.3-8.fc41 Β· GPLv2
- fedora 43: libmawk-devel 1.0.3-8.fc41 Β· GPLv2
- fedora 44: libmawk-devel 1.0.5-1.fc44 Β· GPL-2.0-only
- fedora 44: libmawk-devel 1.0.5-1.fc44 Β· GPL-2.0-only
- ubuntu jammy: libmawk1 1.0.2-3
- ubuntu noble: libmawk1t64 1.0.4-3build1
- ubuntu questing: libmawk1t64 1.0.4-5
- ubuntu resolute: libmawk1t64 1.0.5-1
- ubuntu stonking: libmawk1t64 1.0.5-1
- Copyright 2009 Tibor Palinkas (mawk@inno.bme.hu)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms