aboutsummaryrefslogtreecommitdiffstats
path: root/nimisewi.c
blob: de2fed9d13c9ab3e93c97bb82a8957d19b09cd85 (plain) (blame)
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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (c) 2021, la-ninpre
 * 
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * *************************************************************************
 * 
 * this file contains main code for nimisewi.
 * 
 * nimisewi is a small program that generates random toki pona noun phrase.
 * toki pona is a small constructed language created by sonja lang 
 * (see https://tokipona.org).
 *
 * functions and variables here a primarily named in tokipona, just because i
 * can. but just in case there are annotations in english.
 *
 * sona nanpa li ike. taso ona li pali e nimi pona.
 */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/time.h>

#include "nimisewi.h"
#include "nimitoki.h" /* nimi_toki, suli_pi_nimi_toki */

void
open_e_nanpa_sewi()
{
    /* 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);
}

const char
*pana_e_nimi(const char *nimi_mute[],
             const int suli_pi_nimi_mute,
             const int nanpa_nimi)
{
    /* wrapper function to get word from array
     *
     * noun phrases in toki pona could have arbitrary amount of words and they
     * are left grouped (the leftmost word is main, and words to the right
     * are modifying it:
     *
     * ((nimi) sewi)
     *
     * special word 'pi' could be used to alter this grouping order to achieve
     * something like english preposition 'of'
     *
     * ((suli) pi ((nimi) mute))
     *
     * this functions inserts 'pi' in the middle to avoid generating
     * very heavy phrases.
     */

    if (nanpa_nimi > suli_pi_nimi_mute) {
        err(EXIT_FAILURE, "index out of bounds");
    } else if (nanpa_nimi == -1) {
        return "pi";
    } else {
        return nimi_mute[nanpa_nimi];
    }
}

void
pana_e_nanpa_nimi(struct nanpa_nimi *nn)
{
    /* generate random number, which is later used as number of words
     * in the phrase
     *
     * also here is made decision, whether to insert 'pi'
     */
    nn->pi_li_lon = 0;
    nn->nanpa_pi = -1;
    nn->nanpa_nimi = (rand() % 6);

    if (nn->nanpa_nimi == 2) {
        nn->pi_li_lon = rand() % 2;
    } else if (nn->nanpa_nimi > 2) {
        nn->pi_li_lon = 1;
    }
    if (nn->pi_li_lon) {
        nn->nanpa_nimi++;
        nn->nanpa_pi = (nn->nanpa_nimi / 2);
    }
}

void
pana_e_nanpa_pi_nimi_sewi(int *nanpa_sewi_mute,
                          const int suli_nimi,
                          struct nanpa_nimi *nn)
{
    /* create specified amount of random numbers
     *
     * they are used later as indices to get words from wordlist
     */

    int i;

    for (i = 0; i <= nn->nanpa_nimi; i++) {
        nanpa_sewi_mute[i] = rand() % suli_nimi;
    }

    if (nn->pi_li_lon) {
        nanpa_sewi_mute[nn->nanpa_pi] = -1;
    }
}

int
main(void)
{
    int *nanpa_sewi_nimi; /* container for random indices */
    int i;
    struct nanpa_nimi nn; /* see nimisewi.h */

#ifdef __OpenBSD__
    if (pledge("stdio", NULL) == -1) {
        err(EXIT_FAILURE, "pledge");
    }
#endif
    open_e_nanpa_sewi();

    pana_e_nanpa_nimi(&nn);

    nanpa_sewi_nimi = (int *) calloc(nn.nanpa_nimi + 1, sizeof(int));
    if (nanpa_sewi_nimi == NULL) {
        err(EXIT_FAILURE, "calloc");
    }

    pana_e_nanpa_pi_nimi_sewi(nanpa_sewi_nimi, suli_pi_nimi_toki, &nn);

    /* i think that this section could be improved. i had an idea that
     * it would be cool to include options to output in cgi format instead
     * of just text
     */
    for (i = 0; i < nn.nanpa_nimi; i++) {
        printf("%s ",
                pana_e_nimi(nimi_toki, suli_pi_nimi_toki, nanpa_sewi_nimi[i]));
    }
    printf("%s", pana_e_nimi(nimi_toki, suli_pi_nimi_toki, nanpa_sewi_nimi[i]));
    printf("\n");

    free(nanpa_sewi_nimi);

    return 0;
}