From a09dfac1ad042d49ced01d79866878b6ce0a4045 Mon Sep 17 00:00:00 2001 From: la-ninpre Date: Sat, 20 Nov 2021 19:00:28 +0000 Subject: added ability to specify max amount of words generated by nimisewi added new flag to main program, that displays sad version of prompt removed unnecessary function that sets up random seed for systems where arc4random is not available updated readme FossilOrigin-Name: 1195601f8a81524006e8d3c9014abb26cf613ad995b60455d9553fd7bb50d337 --- CMakeLists.txt | 2 +- README.md | 8 ++++++ cgi.c | 2 +- cgi_gemini.c | 2 +- main.c | 90 ++++++++++++++++++++++++++++++++++++++++++++-------------- nimisewi.c | 36 +++++++---------------- nimisewi.h | 2 +- 7 files changed, 92 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ab653b7..db08b49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.10) # project specification project(nimisewi - VERSION 0.3.3 + VERSION 0.4.0 DESCRIPTION "small program that generates random toki pona noun phrase" HOMEPAGE_URL "https://fsl.aaoth.xyz/nimisewi.c/home" LANGUAGES C) diff --git a/README.md b/README.md index 3d39138..6240b45 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,11 @@ $ nimisewi musi taso jaki pi selo olin lukin ``` +options: + +- `-v` -- display version information and compilation features. +- `-i` -- display sad version of nimisewi prompt. + ## adding new words to add new words, edit `nimitoki.txt` file. add one word per line. @@ -91,6 +96,9 @@ $ make - **0.3.0** -- added support for arc4random function if it exists on the system - **0.3.1** -- added compiler name and version to `--version` flag of nimisewi - **0.3.2** -- fixed link flags on linux +- **0.3.3** -- fixed compiler warnings and build system +- **0.4.0** -- added max number of words to nimisewi function, added new prompt +to the main program ## bugs diff --git a/cgi.c b/cgi.c index e71df22..da9455f 100644 --- a/cgi.c +++ b/cgi.c @@ -87,7 +87,7 @@ main(void) print_html_header(); printf("

"); - ns = nimi_sewi(); + ns = nimi_sewi(6); printf("%s", ns); weka_e_nimi_sewi(ns); printf("

\n"); diff --git a/cgi_gemini.c b/cgi_gemini.c index 171011e..087e22a 100644 --- a/cgi_gemini.c +++ b/cgi_gemini.c @@ -19,7 +19,7 @@ main(void) printf("20 text/gemini\r\n"); printf("sina o lukin e nimi sewi:\n\n"); - ns = nimi_sewi(); + ns = nimi_sewi(6); printf("# %s\n", ns); weka_e_nimi_sewi(ns); diff --git a/main.c b/main.c index fda5daf..b7d0489 100644 --- a/main.c +++ b/main.c @@ -29,38 +29,86 @@ #include "nimisewi.h" -int -main(int argc, char *argv[]) +#define NIMISEWI_MAMA 0x01 +#define NIMISEWI_IKE 0x02 + +static void +pana_sona_pali(void) { - char *ns; + printf("nimisewi [-v|-i]\n"); + exit(EXIT_SUCCESS); +} +static void +pana_sona_mama(void) +{ + printf("%s\n", PACKAGE_STRING); + printf("%s\n", PACKAGE_DESCRIPTION); + printf("compiled on %s %s using %s (%ld-bit)\n", + __DATE__, __TIME__, COMPILER_NAME, sizeof(void*)*8); #ifdef HAVE_PLEDGE - if (pledge("stdio", NULL) == -1) { - err(EXIT_FAILURE, "pledge"); - } -#endif - if (argc == 2) { - if (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-v")) { - printf("%s\n", PACKAGE_STRING); - printf("%s\n", PACKAGE_DESCRIPTION); - printf("compiled on %s %s using %s (%lu-bit)\n", - __DATE__, __TIME__, COMPILER_NAME, sizeof(void*)*8); -#ifdef HAVE_PLEDGE - printf("HAVE_PLEDGE\n"); + printf("HAVE_PLEDGE\n"); #endif #ifdef HAVE_STRLCAT - printf("HAVE_STRLCAT\n"); + printf("HAVE_STRLCAT\n"); #endif #ifdef HAVE_ARC4RANDOM_UNIFORM - printf("HAVE_ARC4RANDOM_UNIFORM\n"); + printf("HAVE_ARC4RANDOM_UNIFORM\n"); #endif - return EXIT_SUCCESS; - } - } + /*return EXIT_SUCCESS;*/ +} - ns = nimi_sewi(); +static void +nimisewi_pona(void) +{ + char *ns; + ns = nimi_sewi(6); printf("%s\n", ns); weka_e_nimi_sewi(ns); +} + +static void +nimisewi_pi_pilin_ike(void) +{ + char *ns; + ns = nimi_sewi(3); + printf("%s mute li lon\n" + "tan pi %s sin li seme?\n", ns, ns); + weka_e_nimi_sewi(ns); +} + +int +main(int argc, char *argv[]) +{ + int ch, f; +#ifdef HAVE_PLEDGE + if (pledge("stdio", NULL) == -1) { + err(EXIT_FAILURE, "pledge"); + } +#endif + f = 0; + while ((ch = getopt(argc, argv, "vi")) != -1) { + switch (ch) { + case 'v': + f |= NIMISEWI_MAMA; + break; + case 'i': + f |= NIMISEWI_IKE; + break; + default: + pana_sona_pali(); + } + } + argc -= optind; + argv += optind; + + if (f & NIMISEWI_MAMA) + pana_sona_mama(); + + if (f & NIMISEWI_IKE) + nimisewi_pi_pilin_ike(); + else + nimisewi_pona(); return EXIT_SUCCESS; } diff --git a/nimisewi.c b/nimisewi.c index 28d79a6..07d9fc4 100644 --- a/nimisewi.c +++ b/nimisewi.c @@ -64,6 +64,13 @@ nsrand(uint32_t limit) #ifdef HAVE_ARC4RANDOM_UNIFORM return arc4random_uniform(limit); #else + pid_t pid; + struct timeval time; + + gettimeofday(&time, NULL); + pid = getpid(); + srand(pid * time.tv_usec * time.tv_sec); + return rand() % limit; #endif } @@ -79,23 +86,6 @@ catstr(char *dst, const char *src, size_t size) #endif } -static void -open_e_nanpa_sewi(void) -{ - /* initialize pseudorandom generator using pid, microseconds and seconds - * - * for such silly program there's no need to implement cryptographically - * secure random number generator. - */ - - pid_t pid; - struct timeval time; - - gettimeofday(&time, NULL); - pid = getpid(); - srand(pid * time.tv_usec * time.tv_sec); -} - static const char *pana_e_nimi(const int nanpa_nimi) { @@ -147,7 +137,7 @@ nanpa_nimi_pana_e_pi(struct nanpa_nimi *nn) } static struct nanpa_nimi -*pana_e_nanpa_nimi(void) +*pana_e_nanpa_nimi(unsigned int nanpa_nimi_sewi) { /* generate nanpa_nimi with all the values useful for phrase generation * @@ -168,7 +158,7 @@ static struct nanpa_nimi nn->nanpa_sewi_nimi = NULL; nn->suli_pi_nimi_sewi = 0; - nn->nanpa_nimi = (nsrand(6)); + nn->nanpa_nimi = (nsrand(nanpa_nimi_sewi)); /* to use with arbitrary wordlist, remove following function call */ nanpa_nimi_pana_e_pi(nn); @@ -203,17 +193,13 @@ weka_e_nanpa_nimi(struct nanpa_nimi *nn) } char -*nimi_sewi() +*nimi_sewi(unsigned int nanpa_nimi_sewi) { int i; char *nimi_pana; struct nanpa_nimi *nn; -#ifndef HAVE_ARC4RANDOM_UNIFORM - open_e_nanpa_sewi(); -#endif - - nn = pana_e_nanpa_nimi(); + nn = pana_e_nanpa_nimi(nanpa_nimi_sewi); nimi_pana = calloc(nn->suli_pi_nimi_sewi + 1, sizeof(char)); if (nimi_pana == NULL) { diff --git a/nimisewi.h b/nimisewi.h index d3ca009..2bbf5ef 100644 --- a/nimisewi.h +++ b/nimisewi.h @@ -18,7 +18,7 @@ #include -char *nimi_sewi(void); +char *nimi_sewi(unsigned int nanpa_nimi_sewi); void weka_e_nimi_sewi(char *nimi_sewi); -- cgit v1.2.3