From e913247fec162437920266182834aedadcb03004 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 31 Oct 2017 15:51:02 -0500 Subject: Rename test_*.c to demo_*.c (because "make test_$CMD" means something already), fluff out README, and add a comment to hostid explaining its deprecation. --- toys/example/README | 10 +++++ toys/example/demo_human_readable.c | 23 +++++++++++ toys/example/demo_many_options.c | 22 +++++++++++ toys/example/demo_scankey.c | 80 ++++++++++++++++++++++++++++++++++++++ toys/example/demo_utf8towc.c | 42 ++++++++++++++++++++ toys/example/hostid.c | 3 ++ toys/example/test_human_readable.c | 23 ----------- toys/example/test_many_options.c | 22 ----------- toys/example/test_scankey.c | 80 -------------------------------------- toys/example/test_utf8towc.c | 42 -------------------- 10 files changed, 180 insertions(+), 167 deletions(-) create mode 100644 toys/example/demo_human_readable.c create mode 100644 toys/example/demo_many_options.c create mode 100644 toys/example/demo_scankey.c create mode 100644 toys/example/demo_utf8towc.c delete mode 100644 toys/example/test_human_readable.c delete mode 100644 toys/example/test_many_options.c delete mode 100644 toys/example/test_scankey.c delete mode 100644 toys/example/test_utf8towc.c (limited to 'toys/example') diff --git a/toys/example/README b/toys/example/README index d0f6631a..a3af8519 100644 --- a/toys/example/README +++ b/toys/example/README @@ -2,3 +2,13 @@ Example commands You probably don't want to deploy this, but it shows how to use the toybox infrastructure and provides templates for new commands. + +The hello.c and skeleton.c commands provide templates: hello.c is clean and +simple, skeleton.c demonstrates the option parsing infrastructure and having +multiple commands per file. When writing a new command, copying hello.c or +skeleton.c to the new name may provide a good starting point. + +The demo_* commands demonstrate infrastructure. + +Other commands in here are obsolete versions still in some recent Linux systems +(and often still in posix), but not really useful on modern systems. diff --git a/toys/example/demo_human_readable.c b/toys/example/demo_human_readable.c new file mode 100644 index 00000000..9fff2626 --- /dev/null +++ b/toys/example/demo_human_readable.c @@ -0,0 +1,23 @@ +/* test_human_readable.c - Expose lib/lib.c human_readable() for testing. + * + * Copyright 2015 Rob Landley + +USE_TEST_HUMAN_READABLE(NEWTOY(test_human_readable, "<1>1ibs", TOYFLAG_BIN)) + +config TEST_HUMAN_READABLE + bool "test_human_readable" + default n + help + usage: test_human_readable [-sbi] NUMBER +*/ + +#define FOR_test_human_readable +#include "toys.h" + +void test_human_readable_main(void) +{ + char *c; + + human_readable(toybuf, strtoll(*toys.optargs, &c, 0), toys.optflags); + printf("%s\n", toybuf); +} diff --git a/toys/example/demo_many_options.c b/toys/example/demo_many_options.c new file mode 100644 index 00000000..e071d26c --- /dev/null +++ b/toys/example/demo_many_options.c @@ -0,0 +1,22 @@ +/* test_many_options.c - test more than 32 bits worth of option flags + * + * Copyright 2015 Rob Landley + +USE_TEST_MANY_OPTIONS(NEWTOY(test_many_options, "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba", TOYFLAG_BIN)) + +config TEST_MANY_OPTIONS + bool "test_many_options" + default n + help + usage: test_many_options -[a-zA-Z] + + Print the optflags value of the command arguments, in hex. +*/ + +#define FOR_test_many_options +#include "toys.h" + +void test_many_options_main(void) +{ + xprintf("optflags=%llx\n", toys.optflags); +} diff --git a/toys/example/demo_scankey.c b/toys/example/demo_scankey.c new file mode 100644 index 00000000..db900270 --- /dev/null +++ b/toys/example/demo_scankey.c @@ -0,0 +1,80 @@ +/* test_scankey.c - collate incoming ansi escape sequences. + * + * Copyright 2015 Rob Landley + * + * TODO sigwinch + +USE_TEST_SCANKEY(NEWTOY(test_scankey, 0, TOYFLAG_BIN)) + +config TEST_SCANKEY + bool "test_scankey" + default n + help + usage: test_scankey + + Move a letter around the screen. Hit ESC to exit. +*/ + +#define FOR_test_scankey +#include "toys.h" + +void test_scankey_main(void) +{ + time_t t[2]; + unsigned width, height, tick; + char c = 'X', scratch[16]; + int key, x, y; + + t[0] = t[1] = x = tick = 0; + memset(scratch, 0, 16); + y = 1; + + sigatexit(tty_sigreset); // Make ctrl-c restore tty + tty_esc("?25l"); // hide cursor + tty_esc("0m"); // reset color to default + tty_esc("2J"); // Clear screen + xset_terminal(1, 1, 0); // Raw mode + + for (;;) { + tty_jump(x, y); + xputc(c); + t[1&++tick] = time(0); + if (t[0] != t[1]) terminal_probesize(&width, &height); + // Don't block first time through, to force header print + key = scan_key_getsize(scratch, -1*!!t[0], &width, &height); + tty_jump(0, 0); + printf("ESC to exit: "); + // Print unknown escape sequence + if (*scratch) { + printf("key=[ESC"); + // Fetch rest of sequence after deviation, time gap determines end + while (0<(key = scan_key_getsize(scratch, 0, &width, &height))) + printf("%c", key); + printf("] "); + } else printf("key=%d ", key); + printf("x=%d y=%d width=%d height=%d\033[K", x, y, width, height); + fflush(0); + + if (key == -2) continue; + if (key <= ' ') break; + if (key>=256) { + tty_jump(x, y); + xputc(' '); + + key -= 256; + if (key==KEY_UP) y--; + else if (key==KEY_DOWN) y++; + else if (key==KEY_RIGHT) x++; + else if (key==KEY_LEFT) x--; + else if (key==KEY_PGUP) y = 0; + else if (key==KEY_PGDN) y = 999; + else if (key==KEY_HOME) x = 0; + else if (key==KEY_END) x = 999; + if (y<1) y = 1; + if (y>=height) y = height-1; + if (x<0) x = 0; + if (x>=width) x = width-1; + } else c = key; + } + tty_reset(); +} diff --git a/toys/example/demo_utf8towc.c b/toys/example/demo_utf8towc.c new file mode 100644 index 00000000..f939eaa7 --- /dev/null +++ b/toys/example/demo_utf8towc.c @@ -0,0 +1,42 @@ +/* test_utf8towc() against libc mbrtowc() + * + * Copyright 2017 Rob Landley + +USE_TEST_UTF8TOWC(NEWTOY(test_utf8towc, 0, TOYFLAG_USR|TOYFLAG_BIN)) + +config TEST_UTF8TOWC + bool "test_utf8towc" + default n + help + usage: test_utf8towc + + Print differences between toybox's utf8 conversion routines vs libc du jour. +*/ + +#include "toys.h" + +void test_utf8towc_main(void) +{ + mbstate_t mb; + int len1, len2; + unsigned u, h; + wchar_t wc1, wc2; + + setlocale(LC_ALL, "en_US.UTF-8"); + + memset(&mb, 0, sizeof(mb)); + for (u=1; u; u++) { + char *str = (void *)&h; + + wc1 = wc2 = 0; + len2 = 4; + h = htonl(u); + while (!*str) str++, len2--; + + len1 = mbrtowc(&wc1, str, len2, &mb); + if (len1<0) memset(&mb, 0, sizeof(mb)); + len2 = utf8towc(&wc2, str, len2); + if (len1 != len2 || wc1 != wc2) + printf("%x %d %x %d %x\n", u, len1, wc1, len2, wc2); + } +} diff --git a/toys/example/hostid.c b/toys/example/hostid.c index feef61bf..3d7a8a8c 100644 --- a/toys/example/hostid.c +++ b/toys/example/hostid.c @@ -3,6 +3,9 @@ * Copyright 2015 Ranjan Kumar * * No Standard. + * + * This is still in coreutils and gethostid() in posix, but a "globally unique + * 32 bit identifier" is a concept the Linux world has outgrown. USE_HOSTID(NEWTOY(hostid, ">0", TOYFLAG_USR|TOYFLAG_BIN)) diff --git a/toys/example/test_human_readable.c b/toys/example/test_human_readable.c deleted file mode 100644 index 9fff2626..00000000 --- a/toys/example/test_human_readable.c +++ /dev/null @@ -1,23 +0,0 @@ -/* test_human_readable.c - Expose lib/lib.c human_readable() for testing. - * - * Copyright 2015 Rob Landley - -USE_TEST_HUMAN_READABLE(NEWTOY(test_human_readable, "<1>1ibs", TOYFLAG_BIN)) - -config TEST_HUMAN_READABLE - bool "test_human_readable" - default n - help - usage: test_human_readable [-sbi] NUMBER -*/ - -#define FOR_test_human_readable -#include "toys.h" - -void test_human_readable_main(void) -{ - char *c; - - human_readable(toybuf, strtoll(*toys.optargs, &c, 0), toys.optflags); - printf("%s\n", toybuf); -} diff --git a/toys/example/test_many_options.c b/toys/example/test_many_options.c deleted file mode 100644 index e071d26c..00000000 --- a/toys/example/test_many_options.c +++ /dev/null @@ -1,22 +0,0 @@ -/* test_many_options.c - test more than 32 bits worth of option flags - * - * Copyright 2015 Rob Landley - -USE_TEST_MANY_OPTIONS(NEWTOY(test_many_options, "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba", TOYFLAG_BIN)) - -config TEST_MANY_OPTIONS - bool "test_many_options" - default n - help - usage: test_many_options -[a-zA-Z] - - Print the optflags value of the command arguments, in hex. -*/ - -#define FOR_test_many_options -#include "toys.h" - -void test_many_options_main(void) -{ - xprintf("optflags=%llx\n", toys.optflags); -} diff --git a/toys/example/test_scankey.c b/toys/example/test_scankey.c deleted file mode 100644 index db900270..00000000 --- a/toys/example/test_scankey.c +++ /dev/null @@ -1,80 +0,0 @@ -/* test_scankey.c - collate incoming ansi escape sequences. - * - * Copyright 2015 Rob Landley - * - * TODO sigwinch - -USE_TEST_SCANKEY(NEWTOY(test_scankey, 0, TOYFLAG_BIN)) - -config TEST_SCANKEY - bool "test_scankey" - default n - help - usage: test_scankey - - Move a letter around the screen. Hit ESC to exit. -*/ - -#define FOR_test_scankey -#include "toys.h" - -void test_scankey_main(void) -{ - time_t t[2]; - unsigned width, height, tick; - char c = 'X', scratch[16]; - int key, x, y; - - t[0] = t[1] = x = tick = 0; - memset(scratch, 0, 16); - y = 1; - - sigatexit(tty_sigreset); // Make ctrl-c restore tty - tty_esc("?25l"); // hide cursor - tty_esc("0m"); // reset color to default - tty_esc("2J"); // Clear screen - xset_terminal(1, 1, 0); // Raw mode - - for (;;) { - tty_jump(x, y); - xputc(c); - t[1&++tick] = time(0); - if (t[0] != t[1]) terminal_probesize(&width, &height); - // Don't block first time through, to force header print - key = scan_key_getsize(scratch, -1*!!t[0], &width, &height); - tty_jump(0, 0); - printf("ESC to exit: "); - // Print unknown escape sequence - if (*scratch) { - printf("key=[ESC"); - // Fetch rest of sequence after deviation, time gap determines end - while (0<(key = scan_key_getsize(scratch, 0, &width, &height))) - printf("%c", key); - printf("] "); - } else printf("key=%d ", key); - printf("x=%d y=%d width=%d height=%d\033[K", x, y, width, height); - fflush(0); - - if (key == -2) continue; - if (key <= ' ') break; - if (key>=256) { - tty_jump(x, y); - xputc(' '); - - key -= 256; - if (key==KEY_UP) y--; - else if (key==KEY_DOWN) y++; - else if (key==KEY_RIGHT) x++; - else if (key==KEY_LEFT) x--; - else if (key==KEY_PGUP) y = 0; - else if (key==KEY_PGDN) y = 999; - else if (key==KEY_HOME) x = 0; - else if (key==KEY_END) x = 999; - if (y<1) y = 1; - if (y>=height) y = height-1; - if (x<0) x = 0; - if (x>=width) x = width-1; - } else c = key; - } - tty_reset(); -} diff --git a/toys/example/test_utf8towc.c b/toys/example/test_utf8towc.c deleted file mode 100644 index f939eaa7..00000000 --- a/toys/example/test_utf8towc.c +++ /dev/null @@ -1,42 +0,0 @@ -/* test_utf8towc() against libc mbrtowc() - * - * Copyright 2017 Rob Landley - -USE_TEST_UTF8TOWC(NEWTOY(test_utf8towc, 0, TOYFLAG_USR|TOYFLAG_BIN)) - -config TEST_UTF8TOWC - bool "test_utf8towc" - default n - help - usage: test_utf8towc - - Print differences between toybox's utf8 conversion routines vs libc du jour. -*/ - -#include "toys.h" - -void test_utf8towc_main(void) -{ - mbstate_t mb; - int len1, len2; - unsigned u, h; - wchar_t wc1, wc2; - - setlocale(LC_ALL, "en_US.UTF-8"); - - memset(&mb, 0, sizeof(mb)); - for (u=1; u; u++) { - char *str = (void *)&h; - - wc1 = wc2 = 0; - len2 = 4; - h = htonl(u); - while (!*str) str++, len2--; - - len1 = mbrtowc(&wc1, str, len2, &mb); - if (len1<0) memset(&mb, 0, sizeof(mb)); - len2 = utf8towc(&wc2, str, len2); - if (len1 != len2 || wc1 != wc2) - printf("%x %d %x %d %x\n", u, len1, wc1, len2, wc2); - } -} -- cgit v1.2.3