util  Diff

Differences From Artifact [8e7c9355c5]:

To Artifact [9504e2c85c]:

  • File ord.c — part of check-in [6a14de1811] at 2019-07-20 23:37:03 on branch trunk — udpate xpriv to use sysv shmem by default; give iaia an option to generate its own types, and allow selecting between 7-bit and 8-bit encodings for ascii (defaulting to 8-bit); update mkpw to work with new iaia; update ord to add flag controlling byte length (7 or 8) for iaia's ascii mode (user: lexi, size: 10625) [annotate] [blame] [check-ins using]

81
82
83
84
85
86
87

88
89
90
91
92
93
94
...
124
125
126
127
128
129
130

131
132
133
134
135
136
137
...
138
139
140
141
142
143
144



145
146
147
148
149
150
151
...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
...
197
198
199
200
201
202
203

204
205
206
207
208
209
210
...
286
287
288
289
290
291
292
293
294
295
296
297





298
299
300
301
302
303
304
	arg_asc,

	arg_bin, arg_trn, arg_oct, arg_dec,
	arg_duo, arg_hex, arg_b32, arg_b64,

	switch_prefix, param_prefix,
	switch_lowercase,


	arg_ebcdic,
};

word bases[] = {
	[arg_asc] =  0,
	[arg_bin] =  2,
................................................................................
	{arg_duo, "duo"}, {arg_duo, "duodecimal"},
	{arg_hex, "hex"}, {arg_hex, "hexadecimal"},

	{arg_hex, "b32"}, {arg_hex, "base32"}, /* not padded! */

	{switch_prefix, "-p"}, {switch_prefix, "--prefix"},
	{switch_lowercase, "-l"}, {switch_lowercase, "--lowercase"},

	{param_prefix, "-m"}, {param_prefix, "--manual-prefix"},

	{arg_ebcdic, "ebcdic"},
};

/* import the conversion utilities */
typedef bad iaia_error_type;
................................................................................
typedef word iaia_word_type;
enum /* iaia synonyms */ {
	iaia_e_ok = ok,
	iaia_e_domain = bad_domain,
	iaia_e_base = bad_base,
	iaia_e_overflow = bad_overflow,
};



#include "clib/iaia.c"

bad run(const int argc, const char** argv) {
#	ifndef _POSIX_IO
		/* fuck your buffering, it only ever makes
		 * things worse */
		setvbuf(stdout,null,_IONBF);
................................................................................
	
	bool raw = false;
	bool prefix = false;
	bool lowercase = false;

	for (const char** arg = argv + 1; *arg != null; ++arg) {
		uint8_t tblval;
		if (*arg[0] == '%') { ++ *arg; goto number; } else
		if (!raw && (tblget(sz(argtbl),argtbl, *arg, &tblval) == ok)) {
			enum argument symbol = (enum argument) tblval;
			switch (symbol) {
				case arg_to: {
					if (curset == set_out) return bad_syntax;
					else curset = set_out;
				} break;
................................................................................
					forposix(pfxstrlen = strlen(pfxstr));
					++arg;
				} break;

				/* specify an automatic output prefix */
				case switch_prefix: { prefix = true; pfxstr = null; } break;
				case switch_lowercase: { lowercase = true; } break;


				default: {
					/* assume base shorthand */
					base[curset] = bases[symbol];
				}
			}
		} else /* bad_find */ number: {
................................................................................
					lit("base") " " box("0-9") plus OR "asc")),
		}, ints[] = {
			p("default base: \x1b[94m.+\x1b[m"),
			p("binary literal: "lit("0b") box("01") plus),
			p("ternary literal: "lit("0t") box("012") plus),
			p("hex literal: "lit("0x") box("0-9A-Fa-f") plus),
			p("ascii literal: "lit("@") "\x1b[94m.+\x1b[m"),
			p("interpret any string (e.g. a keyword) as integer: " lit("%") box("0-9A-Za-z") plus),
		}, opts[] = {
			p("-p --prefix       : print known prefix codes on output strings"),
			p("-m --manual-prefix: specify a manual prefix to print before each number"),
			p("-l --lowercase    : prefer lowercase for case-insensitive bases"),





		};
#	undef p
#	undef OR
#	undef plus

#	define hl_on  "\x1b[;1m" 
#	define hl_off "\x1b[m"







>







 







>







 







>
>
>







 







|







 







>







 







|




>
>
>
>
>







81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
	arg_asc,

	arg_bin, arg_trn, arg_oct, arg_dec,
	arg_duo, arg_hex, arg_b32, arg_b64,

	switch_prefix, param_prefix,
	switch_lowercase,
	switch_7bit,

	arg_ebcdic,
};

word bases[] = {
	[arg_asc] =  0,
	[arg_bin] =  2,
................................................................................
	{arg_duo, "duo"}, {arg_duo, "duodecimal"},
	{arg_hex, "hex"}, {arg_hex, "hexadecimal"},

	{arg_hex, "b32"}, {arg_hex, "base32"}, /* not padded! */

	{switch_prefix, "-p"}, {switch_prefix, "--prefix"},
	{switch_lowercase, "-l"}, {switch_lowercase, "--lowercase"},
	{switch_7bit, "-7"}, {switch_7bit, "--7bit"},
	{param_prefix, "-m"}, {param_prefix, "--manual-prefix"},

	{arg_ebcdic, "ebcdic"},
};

/* import the conversion utilities */
typedef bad iaia_error_type;
................................................................................
typedef word iaia_word_type;
enum /* iaia synonyms */ {
	iaia_e_ok = ok,
	iaia_e_domain = bad_domain,
	iaia_e_base = bad_base,
	iaia_e_overflow = bad_overflow,
};
bool ascii_7bit = false;
#define _IAIA_EXP_ASCFORM ascii_7bit
#define _IAIA_EXTERNAL_TYPES
#include "clib/iaia.c"

bad run(const int argc, const char** argv) {
#	ifndef _POSIX_IO
		/* fuck your buffering, it only ever makes
		 * things worse */
		setvbuf(stdout,null,_IONBF);
................................................................................
	
	bool raw = false;
	bool prefix = false;
	bool lowercase = false;

	for (const char** arg = argv + 1; *arg != null; ++arg) {
		uint8_t tblval;
		if (*arg[0] == '`') { ++ *arg; goto number; } else
		if (!raw && (tblget(sz(argtbl),argtbl, *arg, &tblval) == ok)) {
			enum argument symbol = (enum argument) tblval;
			switch (symbol) {
				case arg_to: {
					if (curset == set_out) return bad_syntax;
					else curset = set_out;
				} break;
................................................................................
					forposix(pfxstrlen = strlen(pfxstr));
					++arg;
				} break;

				/* specify an automatic output prefix */
				case switch_prefix: { prefix = true; pfxstr = null; } break;
				case switch_lowercase: { lowercase = true; } break;
				case switch_7bit: { ascii_7bit = true; } break;

				default: {
					/* assume base shorthand */
					base[curset] = bases[symbol];
				}
			}
		} else /* bad_find */ number: {
................................................................................
					lit("base") " " box("0-9") plus OR "asc")),
		}, ints[] = {
			p("default base: \x1b[94m.+\x1b[m"),
			p("binary literal: "lit("0b") box("01") plus),
			p("ternary literal: "lit("0t") box("012") plus),
			p("hex literal: "lit("0x") box("0-9A-Fa-f") plus),
			p("ascii literal: "lit("@") "\x1b[94m.+\x1b[m"),
			p("interpret any string (e.g. a keyword) as integer: " lit("`") box("0-9A-Za-z") plus),
		}, opts[] = {
			p("-p --prefix       : print known prefix codes on output strings"),
			p("-m --manual-prefix: specify a manual prefix to print before each number"),
			p("-l --lowercase    : prefer lowercase for case-insensitive bases"),
			p("-7 --7bit         : encode ascii in 7-bit space instead of keeping the"),
			p("                    eighth empty bit. if this option is used, ascii"),
			p("                    strings will be maximally compact, but will not match"),
			p("                    the way they are stored in computer memory!"),
			/* p("-u --utf           : allow non-ascii input"), */
		};
#	undef p
#	undef OR
#	undef plus

#	define hl_on  "\x1b[;1m" 
#	define hl_off "\x1b[m"