Index: arch/posix.h ================================================================== --- arch/posix.h +++ arch/posix.h @@ -41,10 +41,47 @@ #endif /* platform flags */ posix_flag_linux_hugetlb = 0x40000 }; + +enum posix_signal { + /* these numbers appear to be consistent across all + * platforms; we may have to specialize them if this + * assumption turns out to be untrue however. */ + posix_signal_hangup = 1, + posix_signal_interrupt = 2, + posix_signal_quit = 3, + posix_signal_illegal = 4, + posix_signal_trap = 5, + posix_signal_abort = 6, + posix_signal_bus = 7, + posix_signal_float = 8, + posix_signal_kill = 9, + posix_signal_user_a = 10, + posix_signal_user_b = 12, + posix_signal_segfault = 11, + posix_signal_pipe = 13, + posix_signal_alarm = 14, + posix_signal_terminate = 15, + posix_signal_stack_fault = 16, + posix_signal_child = 17, + posix_signal_continue = 18, + posix_signal_stop = 19, + posix_signal_terminal_stop = 20, + posix_signal_tty_input = 21, + posix_signal_tty_output = 22, + posix_signal_io_urgent = 23, + posix_signal_limit_cpu = 24, + posix_signal_limit_space = 25, + posix_signal_vt_alarm = 26, + posix_signal_profile = 27, + posix_signal_winch = 28, + posix_signal_poll = 29, + posix_signal_power = 30, + posix_signal_system = 31, +}; #endif #ifdef KFplatform_define_types /* platform types */ Index: mod/kcli/cli.h ================================================================== --- mod/kcli/cli.h +++ mod/kcli/cli.h @@ -73,10 +73,11 @@ kcli_cond_ok = kcli_cond_id, kcli_cond_extra /* parse succeded, but arguments or flags were left over */, kcli_cond_fail /* unspecified error */, kcli_cond_parse /* bad syntax, parse failed */, + kcli_cond_spec /* the configuration structure is invalid */, kcli_cond_overlong /* a string in the configuration structures is longer than allowed */, } kcli_cond; typedef enum kcli_flag { kcli_flag_off = 0, Index: mod/kcli/kcli.md ================================================================== --- mod/kcli/kcli.md +++ mod/kcli/kcli.md @@ -1,23 +1,23 @@ # kcli **kcli** is a module that implements common routines used by command-line utilities, such as option parsing, usage display, and more. # syntax -kcli implements the unspoken standard for command line parsing that is adhered to by many UNIX programs. it operates on an array of tokens, whose role depends on their content and on preceeding flags. note that in flag names, dashes and underscores are equivalent. this is to prevent obnoxious behavior and enable the macros to define multi-word long flags. +kcli implements the unwritten standard for command line parsing that is adhered to by many UNIX programs. it operates on an array of tokens, whose role depends on their content and on preceeding flags. note that in flag names, dashes and underscores are equivalent. this is to prevent obnoxious behavior and enable the macros to define multi-word long flags. 1. if the argument `--` preceded the current argument, it is interpreted as a parameter. 2. if an argument begins with `-` and is followed by any symbol besides `-`, the remainder of the its characters are interpreted as short flags. 3. if an argument consists solely of `--`, it is ignored, and all further arguments are treated as parameters regardless of their form. -4. if an argument begins with `--` and is longer than two characters, it is interpreted as a long flag. +4. if an argument begins with `--` is longer than two characters, and does not begin with `--@`, it is interpreted as a long flag. 5. if a flag takes a parameter, the cursor is incremented and the field it points to is interpreted as its parameter. if there are not enough arguments, parse fails. 6. if an argument consists solely of `@`, it is treated as in step 7, except flags and parameters are read from standard in. 7. if an argument begins with `@`, the remainder is interpreted as a filename. this file is loaded and interpreted according to this same set of rules, where spaces separate each parameter, and double or single quote marks can be used to escape strings. newlines are ignored. -8. if an argument consists solely of `=@`, it is treated as in step 9, except flags and parameters are read from standard in. -9. if an argument begins with `=@`, the remainder is interpreted as a filename. this file is loaded and interpreted according to this same set of rules, where newlines separate each parameter. -8. if an argument consists solely of `:@`, lines will be read from stdin and interpreted as individual parameters, not as flags. -8. if an argument begins with `:@`, the remainder is interpreted as a filename. all of the file's lines will be interpreted as individual parameters, not as flags. -10. if no other rules apply, the argument is added to the parameter list. +8. if an argument consists solely of `-@`, it is treated as in step 11, except flags and parameters are read from standard in. +9. if an argument consists solely of `--@`, lines will be read from stdin and interpreted as individual parameters, not as flags. +10. if an argument begins with `--@`, the remainder is interpreted as a filename. all of the file's lines will be interpreted as individual parameters, not as flags. +11. if an argument begins with `-@`, the remainder is interpreted as a filename. this file is loaded and interpreted under the assumption that newlines separate each parameter. +12. if no other rules apply, the argument is added to the parameter list. **note:** in the future, `kcore` will parse arguments at startup before passing them on to the application. all libk control arguments are prefixed with `-:`. these will not be passed onto the application, and so will not be available for kcli to parse. consider the following examples: 1. `--no-arg-flag --arg-flag --other-flag` will set the flag `no-arg-flag`, and set the field `arg-flag` to `"--other-flag"`. Index: mod/kcli/parse.fn.c ================================================================== --- mod/kcli/parse.fn.c +++ mod/kcli/parse.fn.c @@ -1,6 +1,53 @@ #include kcli_cond kcli_parse(kcli_set prg) { + bool no_more_opts = false; + const kcli_param* param = prg.params; + + for (sz i = 0; (i < prg.argc) && (prg.args[i] != null); ++i) { + if (no_more_opts) goto is_param; + + const char* const arg = prg.args[i]; + if (arg[0] == '-') { + if(arg[1] == '-') { + if (arg[2] == 0) { no_more_opts = true; continue; } + else if (arg[2] == '@') { + // TODO: implement reading parameters from files + continue; + } else goto is_long_opt; + } else if (arg[1] == '@') { + // TODO: implement newline-separated file reading behavior + continue; + } else if (arg[1] == 0) { + goto is_param; + } else { + goto is_short_opt; + } + } else if (arg[0] == '@') { + // TODO: implement parsing file for options and parameters + continue; + } else { + goto is_param; + } + is_short_opt: { + continue; + } + is_long_opt: { + continue; + } + is_param: { + void* val = param -> val; + if (param -> kind & kcli_param_int) { + ubyte base = param->kind & ~kcli_param_int; + /* TODO: parse integer */ + } else switch (param -> kind) { + case kcli_param_none: continue; + case kcli_param_string: *(char const**)val = arg; break; + case kcli_param_enum: /* TODO */ break; + default: return kcli_cond_spec; + } + } + } } Index: mod/knum/num.h ================================================================== --- mod/knum/num.h +++ mod/knum/num.h @@ -16,10 +16,12 @@ kncond_fail, kncond_fail_io, kncond_overflow, kncond_underflow, kncond_bad_domain, + kncond_bad_base, + } kncond; /* this "hard" random number generator * returns a block of random data that * can be used safely in cryptographic @@ -59,36 +61,42 @@ * note that these functions use either * two's complement or one's complement * for signed */ typedef enum knmode { - knmode_saturate = 0x0001, /* 0b001 */ - knmode_wrap = 0x0002, /* 0b010 */ - knmode_fail = 0x0003, /* 0b011 */ + /* knmode operations may be or'd with a base; + * base will be inferred by prefix otherwise */ + knmode_saturate = 0x0001 << 7, /* 0b001 */ + knmode_wrap = 0x0002 << 7, /* 0b010 */ + knmode_fail = 0x0003 << 7, /* 0b011 */ - knmode_signed = 1 << 2, /* 0b100*/ + knmode_signed = 1 << 6, /* 0b100*/ knmode_unsigned = 0 /* default */ } knmode; -/* it is legal for src and dest to be the - * same location in memory. this will not - * produce incorrect output. although the - * functions formally take pointers to u8, - * they are converted appropriately with - * regard to sign and size within the fn */ +/* it is legal for src and dest to be the + * same location in memory. this will not + * produce incorrect output. although the + * functions formally take pointers to + * ubyte, they are converted appropriately + * with regard to sign and size within + * the fn */ -kncond kniadd(knmode, sz, u8* dest, u8* src, u8* delta); -kncond knisub(knmode, sz, u8* dest, u8* src, u8* delta); -kncond knimul(knmode, sz, u8* dest, u8* src, u8* delta); +kncond kniadd(knmode, sz, ubyte* dest, ubyte* src, ubyte* delta); +kncond knisub(knmode, sz, ubyte* dest, ubyte* src, ubyte* delta); +kncond knimul(knmode, sz, ubyte* dest, ubyte* src, ubyte* delta); kncond knidiv(knmode, sz, - /* output */ u8* ratio, u8* remainder, - /* input */ u8* src, u8* delta); + /* output */ ubyte* ratio, ubyte* remainder, + /* input */ ubyte* src, ubyte delta); /* we should probably also offer a bignum * type eventually, tho this will need to * be integrated with kmem for allocation. */ + +kncond knstr(knmode, sz, char* dest_begin, char* dest_end, ubyte* src); +kncond knparse(knmode, sz, ubyte* dest, ksraw src); #ifdef __cplusplus } #endif #endif