EXAMPLE(7libmawk)
π EXAMPLE (7libmawk) π
okay bestie, this is the libmawk manual and it's literally the "how do I even use this library" page. sit down, hydrate, let's go π«Ά
β¨ NAME β¨
libmawk-example β the whole vibe of this page: how to use the library π
πΈ SYNOPSIS πΈ
#include <libmawk.h>
that's it. that's the entry fee. iconic.
π¦ DESCRIPTION π¦
Libmawk is a library that lets applications embed awk scripts using the code of the popular implementation mawk. no cap, that's the whole point of it.
the normal process is the pipeline, and it goes like this:
- call
libmawk_initialize()to set up a brand new mawk context (with your script(s) loaded) β¨ - then in the main loop, feed it using
libmawk_append_input()π - for "out of band" communication, the program may also call functions implemented in awk, and read (or modify) global variables of the awk script π
- the host application usually will also bind some of its own functions to the context using
libmawk_register_functionβ which lets the awk script call the host application's functions directly, as if they were awk builtins or user defined functions. it's giving main character energy fr fr π₯ - after the main loop, the application destroys the context, freeing up all the memory allocated for the script(s) π§
one context is for one awk program. periodt.
and here's the tea: one awk program may consist of multiple script files β just as with command line awk, with multiple -f filename arguments. π
Libmawk is instance safe π β the host application may create multiple instances of contexts, with the same or with a different set of awk scripts loaded. these contexts are totally separate, no variables, functions or any sort of states are shared. not one crumb. π
however β the host application may provide means of communication between those scripts, by custom functions or by copying variable contents between them. so they can still gossip, they just need a messenger π
π Example application π
the following example application creates a single context to demonstrate all the above mentioned functionality. she does it all. she's booked and busy π
#include <stdio.h>
#include <libmawk.h>
/*
Purpose: load and run a script using the command
line syntax of mawk but using a virtual
stdin buffer instead of the real stdin.
Run: ./app -f test.awk
*/
int main(int argc, char **argv)
{
mawk_state_t *m;
/* init a context, execute BEGIN */
m = libmawk_initialize(argc, argv);
if (m == NULL) {
fprintf(stderr, "libmawk_initialize failed, exiting\n");
return 1;
}
/* feed in some data on the virtual stdin */
libmawk_append_input(m, "This is a\nmultiline test input\nfor the artificial input buffer.\n");
/* run the MAIN part of the script as long as
there's data in the buffer of the virtual stdin */
libmawk_run_main(m);
/* run END and free the context */
libmawk_uninitialize(m);
return 0;
}
π bestie quick reality check: iflibmawk_initializehands you backNULL, she did not slay β the example printslibmawk_initialize failed, exitingtostderrand bails with a return of1. no script, no vibes, just exit.
okay that's the whole page, mwah π this was libmawk, dated 2009-08-10, and the manual you just read is EXAMPLE(7libmawk). go forth and embed some awk, bestie β you're gonna eat, no crumbs left πβππ
π¦ where this page comes from
- debian bookworm: libmawk1 1.0.2-3
- debian trixie: libmawk1t64 1.0.4-4
- 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