libk  Check-in [55dc614190]

Overview
Comment:minor tweaks; update docs to explain error handling
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 55dc614190d33ea66a8c2a81ce557739c07bde307095ddb147a0416b37e5c30f
User & Date: lexi on 2019-08-26 19:49:35
Other Links: manifest | tags
Context
2019-08-26
19:50
fix list formatting error check-in: 2aab529520 user: lexi tags: trunk
19:49
minor tweaks; update docs to explain error handling check-in: 55dc614190 user: lexi tags: trunk
2019-08-25
04:15
comment out non-standard use of enums pending rewrite with #defines :( check-in: 04ed009476 user: lexi tags: trunk
Changes

Modified libk.md from [58fe849cf0] to [602b81e913].

   188    188   
   189    189    1. it should be easy to write code that uses it.
   190    190    2. it should be easy to read code that uses it.
   191    191    3. the simple, obvious way of using libk should produce the most optimal code.
   192    192    4. code that uses libk should be idiomatic C.
   193    193   
   194    194   for these reasons, the codebase follows a number of strict rules.
   195         - 
   196         -### booleans are banned
          195  +
          196  +## booleans are banned
   197    197   there are a number of reasons for this.
   198    198   
   199    199   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.
   200    200   
   201    201   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.
   202    202   
   203    203   instead, libk uses enums liberally. so the above might be rewritten as e.g.:
................................................................................
   205    205       rule r = { 10, buf,
   206    206   		rule_kind_undialectical,
   207    207   		rule_action_expropriate,
   208    208   		rule_target_bourgeoisie
   209    209   	};
   210    210   
   211    211   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.
          212  +
          213  +## error handling
          214  +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:
          215  +
          216  +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`.
          217  +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`).
          218  +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.
          219  +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.
          220  +
          221  +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`.
          222  +
          223  +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.
          224  +
          225  +the value of each condition code is determined at compile time.
   212    226    
   213    227   # authors
   214    228   
   215    229    * lexi `velartrill` hale <lexi@hale.su>
   216    230    * lachs0r
   217    231    * glowpelt
   218    232   

Modified mod/kcore/magic.h from [a7232aabca] to [2cde04fb07].

    53     53   	kbad_osfile = 72,
    54     54   		/* # fbsd EX_OSFILE
    55     55   		 * a system file is fucked up or gone */
    56     56   	kbad_creat = 73,
    57     57   		/* # fbsd EX_CANTCREAT */
    58     58   	kbad_io = 74,
    59     59   		/* # fbsd EX_IOERR */
    60         -	kbad_try = 75,
           60  +	kbad_retry = 75,
    61     61   		/* # fbsd EX_TEMPFAIL
    62     62   		 * something went wrong this time. try again
    63     63   		 * some other time */
    64     64   	kbad_proto = 76,
    65     65   		/* # fbsd EX_PROTOCOL
    66     66   		 * failure to speak a protocol correctly */
    67     67   	kbad_perm = 77,

Modified mod/kcore/testbin.exe.c from [ba38860055] to [aec04b5f97].

     7      7   	u8 a;
     8      8   	s16 b;
     9      9   	bool c;
    10     10   };
    11     11   
    12     12   #define _slit(s) ((ksraw){Kmsz(s),s})
    13     13   
    14         -stat_long entry(kenv e) {
           14  +kbad entry(kenv e) {
    15     15   	const char msg[] = "hello from libk\n";
    16     16   	ksraw ptr = { Kmsz(msg), msg };
    17     17   
    18     18   	bool maybe = true;
    19     19   	maybe = no;
    20     20   
    21     21   	if (kiosend(e.std, ptr, null) == kiocond_ok) {
................................................................................
    35     35   	void* top = kmlini();
    36     36   	kmres rst = kmlina(1230);
    37     37   	if(rst.cond != kmcond_ok) return kbad_mem;
    38     38   
    39     39   	kmres rst2 = kmlina(789);
    40     40   	if(rst2.cond != kmcond_ok) return kbad_mem;
    41     41   
    42         -	const char varmsg[] = "printing environment variables\n";
    43         -	ksraw msgptr = { Kmsz(varmsg), varmsg };
    44         -	kiosend(e.std, msgptr, null);
           42  +	kiosend(e.std,  _slit("printing environment variables\n"), null);
    45     43   
    46     44   	for (sz i = 0; i < e.varc; ++i) {
    47     45   		kiosend(e.std, _slit(" - "), null);
    48     46   		kiosend(e.std, e.vars[i].name, null);
    49     47   		kiosend(e.std, _slit(" = ["), null);
    50     48   		kiosend(e.std, e.vars[i].val, null);
    51     49   		kiosend(e.std, _slit("]\n"), null);
    52     50   	}
    53     51   
    54     52   	return kbad_ok;
    55     53   }