Index: libk.md ================================================================== --- libk.md +++ libk.md @@ -190,12 +190,12 @@ 2. it should be easy to read code that uses it. 3. the simple, obvious way of using libk should produce the most optimal code. 4. code that uses libk should be idiomatic C. for these reasons, the codebase follows a number of strict rules. - -### booleans are banned + +## booleans are banned there are a number of reasons for this. the first is simply that the boolean type in C is a bit messy and libk headers are intended to import as few extra files as possible. the second is that boolean-using code can be hard to read. consider a struct declaration of the form `rule r = { 10, buf, true, false, true }`: the meaning of this declaration is opaque unless you've memorized the structure's definition. @@ -207,10 +207,24 @@ rule_action_expropriate, rule_target_bourgeoisie }; this makes code much more legible and has the added benefit of making the definitions easier to expand at a later date if new functionality is needed without breaking the API or ABI. + +## error handling +every module has a `cond` type (e.g. `kscond` for `kstr` or `kconf_cond` for `kconf`). this is an enumeration type that represents every possible error a module can return, and every `cond` type obeys a number of invariants (in addition to the usual namespacing rules: + +1. every member of a cond type has a globally unique integer value. this means that `kscond_ok` has an integer value is not equal to `kmcond_ok`. +2. every member of a cond type has a member `kmcond_ok` which represents success. this member's integer value is always an exact multiple of the "module offset", the number of condition values allocated to each module (currently `0x7F`). +3. the `kokay()` function, defined in `kcore`, when called on a member of a `cond` type will return true if that member represents total success and false otherwise. +4. every cond type has a value `*cond_fail`, for instance `kconf_cond_fail`. if a condition is greater than or equal to its module's `fail` value, it represents a total failure. if it is lesser than the fail value, it represents either success, partial success, or some other condition that does not equate to total failure. + +the reason each error value is unique is that this allows us to map every single condition code unambiguously to an error message. a forthcoming `kcore` function will do exactly that and produce an error string that can be displayed to a user or for debugging purposes. another useful property is that each integer value has at most only one possible meaning in the context of error codes, allowing for centralized error handling - a `kscond` and a `kmcond` can both be turned into an `int` without loss of information; and e.g. `kscond_fail` != `kmcond_fail` != `kconf_cond_fail`. + +this is particularly useful as C does not have exceptions, and thus the only viable error-handling mechanisms are early-exit and early-return; any `cond` value can be propagated up the function stack without losing its unique meaning. + +the value of each condition code is determined at compile time. # authors * lexi `velartrill` hale * lachs0r Index: mod/kcore/magic.h ================================================================== --- mod/kcore/magic.h +++ mod/kcore/magic.h @@ -55,11 +55,11 @@ * a system file is fucked up or gone */ kbad_creat = 73, /* # fbsd EX_CANTCREAT */ kbad_io = 74, /* # fbsd EX_IOERR */ - kbad_try = 75, + kbad_retry = 75, /* # fbsd EX_TEMPFAIL * something went wrong this time. try again * some other time */ kbad_proto = 76, /* # fbsd EX_PROTOCOL Index: mod/kcore/testbin.exe.c ================================================================== --- mod/kcore/testbin.exe.c +++ mod/kcore/testbin.exe.c @@ -9,11 +9,11 @@ bool c; }; #define _slit(s) ((ksraw){Kmsz(s),s}) -stat_long entry(kenv e) { +kbad entry(kenv e) { const char msg[] = "hello from libk\n"; ksraw ptr = { Kmsz(msg), msg }; bool maybe = true; maybe = no; @@ -37,13 +37,11 @@ if(rst.cond != kmcond_ok) return kbad_mem; kmres rst2 = kmlina(789); if(rst2.cond != kmcond_ok) return kbad_mem; - const char varmsg[] = "printing environment variables\n"; - ksraw msgptr = { Kmsz(varmsg), varmsg }; - kiosend(e.std, msgptr, null); + kiosend(e.std, _slit("printing environment variables\n"), null); for (sz i = 0; i < e.varc; ++i) { kiosend(e.std, _slit(" - "), null); kiosend(e.std, e.vars[i].name, null); kiosend(e.std, _slit(" = ["), null);