libk  Check-in [8d478e0b3c]

Overview
Comment:add posix signal numbers; continue work on kcli
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8d478e0b3c5cfb908682422070cf5bb969d92752b74ee24b30240b4841318464
User & Date: lexi on 2019-11-19 05:06:20
Other Links: manifest | tags
Context
2019-11-19
05:23
specialize signal numbers for MIPS check-in: 164a1a5cfe user: lexi tags: trunk
05:06
add posix signal numbers; continue work on kcli check-in: 8d478e0b3c user: lexi tags: trunk
2019-11-01
07:52
Disable PDF docs on NixOS. As it seems groff PDF support is currently broken on NixOS (https://github.com/NixOS/nixpkgs/issues/53056), disable the building of PDFs in the NixOS build. check-in: 9408112a57 user: glowpelt tags: trunk
Changes

Modified arch/posix.h from [4c89ca109f] to [093468646d].

39
40
41
42
43
44
45





































46
47
48
49
50
51
52
#elif KVos == KA_os_fbsd
	posix_flag_anonymous = 0x1000,
#endif

	/* platform flags */
	posix_flag_linux_hugetlb = 0x40000
};






































#endif
#ifdef KFplatform_define_types

/* platform types */

typedef s64 k_platform_syscall_return;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#elif KVos == KA_os_fbsd
	posix_flag_anonymous = 0x1000,
#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 */

typedef s64 k_platform_syscall_return;

Modified mod/kcli/cli.h from [5126e2cc55] to [fa27906c88].

71
72
73
74
75
76
77

78
79
80
81
82
83
84

typedef enum kcli_cond {
	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_overlong /* a string in the configuration structures is longer than allowed */,
} kcli_cond;

typedef enum kcli_flag {
	kcli_flag_off = 0,
	kcli_flag_on = 1,
} kcli_flag;







>







71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

typedef enum kcli_cond {
	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,
	kcli_flag_on = 1,
} kcli_flag;

Modified mod/kcli/kcli.md from [0390b41ce1] to [5e7f6f38dd].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

18
19
20
21
22
23
24
25
# 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.

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.
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.

**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"`.
2. if `-a` names a flag and `-b` and `-c` name string fields, `-abc b-field-value c-field-value parameter` sets the short flag `a`, set the field `b` to `"b-field-value"`, set `c` to `"c-field-value"`, and add the parameter `parameter`
3. `-s --long @args -- -s --long @args` sets the flags `s`, `long`, and parses the file `args` for further parameters, then adds the parameters `"-s"``"--long"``"@args"` to the parameter list.




|




|



|
<
|
|
>
|







1
2
3
4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22
23
24
25
# 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 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 `--` 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 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"`.
2. if `-a` names a flag and `-b` and `-c` name string fields, `-abc b-field-value c-field-value parameter` sets the short flag `a`, set the field `b` to `"b-field-value"`, set `c` to `"c-field-value"`, and add the parameter `parameter`
3. `-s --long @args -- -s --long @args` sets the flags `s`, `long`, and parses the file `args` for further parameters, then adds the parameters `"-s"``"--long"``"@args"` to the parameter list.

Modified mod/kcli/parse.fn.c from [1d4ed1c3cd] to [ff83a12d45].

1
2
3
4


5


6











































#include <k/cli.h>

kcli_cond
kcli_parse(kcli_set prg) {





}















































>
>

>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <k/cli.h>

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;
			}
		}
	}
}

Modified mod/knum/num.h from [982620d249] to [91c37b49e7].

14
15
16
17
18
19
20


21
22
23
24
25
26
27
..
57
58
59
60
61
62
63


64
65
66
67
68
69
70
71
72
73
74
75
76

77
78
79
80
81
82
83

84
85
86
87
88



89
90
91
92
93
94

	/* error states */
	kncond_fail,
	kncond_fail_io,
	kncond_overflow,
	kncond_underflow,
	kncond_bad_domain,


} kncond;

/* this "hard" random number generator
 * returns a block of random data that
 * can be used safely in cryptographic
 * pseudorandom number algorithms such
 * as e.g. knrands() */
................................................................................
 * fixed point & floating point numbers
 * should also be added eventually.
 * 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_signed   = 1 << 2, /* 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 */

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 knidiv(knmode, sz,
	/* output */ u8* ratio, u8* remainder,

	/*  input */ u8* src, u8* delta);

/* we should probably also offer a bignum
 * type eventually, tho this will need to
 * be integrated with kmem for allocation. */




#ifdef __cplusplus
	}
#endif

#endif







>
>







 







>
>
|
|
|

|






|
|
>
|

|
|
|

<
>
|




>
>
>






14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
..
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

	/* error states */
	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
 * pseudorandom number algorithms such
 * as e.g. knrands() */
................................................................................
 * fixed point & floating point numbers
 * should also be added eventually.
 * note that these functions use either
 * two's complement or one's complement
 * for signed */

typedef enum knmode {
	/* 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 << 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
 * ubyte, they are converted appropriately
 * with  regard  to sign  and  size within
 * the fn */

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 */ 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