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
la-ninpre leobrekalini@gmail.com
Sat, 20 Nov 2021 19:00:28 +0000
7 files changed,
92 insertions(+),
50 deletions(-)
M
CMakeLists.txt
→
CMakeLists.txt
@@ -3,7 +3,7 @@
# 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)
M
README.md
→
README.md
@@ -59,6 +59,11 @@
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 @@ and compile features
- **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
M
cgi_gemini.c
→
cgi_gemini.c
@@ -19,7 +19,7 @@ #endif
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);
M
main.c
→
main.c
@@ -29,38 +29,86 @@ #include <string.h>
#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; }
M
nimisewi.c
→
nimisewi.c
@@ -64,6 +64,13 @@ {
#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 @@ return strlen(dst);
#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 @@ }
} 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 @@ nn->nanpa_pi = -1;
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 @@ }
} 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) {
M
nimisewi.h
→
nimisewi.h
@@ -18,7 +18,7 @@ #define NIMI_SEWI_H
#include <stdlib.h> -char *nimi_sewi(void); +char *nimi_sewi(unsigned int nanpa_nimi_sewi); void weka_e_nimi_sewi(char *nimi_sewi);