aboutsummaryrefslogtreecommitdiffstats
path: root/nimisewi.c
blob: 07d9fc4ab2701c41d5adbc93fce1190bb8d243ee (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* 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 ilo pona.
 */

#include <config.h>
#include <err.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/time.h>

#ifdef HAVE_BSD_STDLIB_H
    #include <bsd/stdlib.h>
#else
    #include <stdlib.h>
#endif

#ifdef HAVE_BSD_STRING_H
    #include <bsd/string.h>
#else
    #include <string.h>
#endif

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

/* struct to hold stuff useful for random index generation */
struct nanpa_nimi {
    int nanpa_nimi; /* number of words - 1 */
    int pi_li_lon;  /* whether to insert 'pi' or not */
    int nanpa_pi;   /* index to insert 'pi', if pi_li_lon is 1 */
    int *nanpa_sewi_nimi; /* random indices of words */
    size_t suli_pi_nimi_sewi; /* length of generated phrase */
};

static uint32_t
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
}

static size_t
catstr(char *dst, const char *src, size_t size)
{
#ifdef HAVE_STRLCAT
    return strlcat(dst, src, size);
#else
    strncat(dst, src, size);
    return strlen(dst);
#endif
}

static const char
*pana_e_nimi(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 == -1) {
        return "pi";
    } else if ((unsigned long) nanpa_nimi > suli_pi_nimi_toki) {
        err(EXIT_FAILURE, "index out of bounds");
    } else {
        return nimi_toki[nanpa_nimi];
    }
}

static void
nanpa_nimi_pana_e_pi(struct nanpa_nimi *nn)
{
    /* handle a 'pi' instertion */
    if (nn->nanpa_nimi == 2) {
        /* this is made to allow for phrases with following structures:
         *
         * - word1 word2 word3
         * - word1 pi word2 word3
         */
        nn->pi_li_lon = nsrand(2);
    } else if (nn->nanpa_nimi > 2) {
        nn->pi_li_lon = 1;
    }
    if (nn->pi_li_lon) {
        /* since we insert whole word, number of words must be increased too */
        nn->nanpa_nimi++;
        nn->nanpa_pi = (nn->nanpa_nimi / 2);
    }
}

static struct nanpa_nimi
*pana_e_nanpa_nimi(unsigned int nanpa_nimi_sewi)
{
    /* generate nanpa_nimi with all the values useful for phrase generation
     *
     * when used, one should call weka_e_nanpa_nimi();
     */

    int i;
    struct nanpa_nimi *nn;

    nn = malloc(sizeof(struct nanpa_nimi));
    if (nn == NULL) {
        return NULL;
    }

    /* initialize nanpa_nimi with default values */
    nn->pi_li_lon = 0;
    nn->nanpa_pi = -1;
    nn->nanpa_sewi_nimi = NULL;
    nn->suli_pi_nimi_sewi = 0;

    nn->nanpa_nimi = (nsrand(nanpa_nimi_sewi));

    /* to use with arbitrary wordlist, remove following function call */
    nanpa_nimi_pana_e_pi(nn);

    nn->nanpa_sewi_nimi = calloc(nn->nanpa_nimi + 1, sizeof(int));
    if (nn->nanpa_sewi_nimi == NULL) {
        free(nn);
        return NULL;
    }
    for (i = 0; i <= nn->nanpa_nimi; i++) {
        nn->nanpa_sewi_nimi[i] = nsrand(suli_pi_nimi_toki);
    }
    if (nn->pi_li_lon) {
        nn->nanpa_sewi_nimi[nn->nanpa_pi] = -1;
    }

    for (i = 0; i <= nn->nanpa_nimi; i++) {
        nn->suli_pi_nimi_sewi += sizeof(char);
        nn->suli_pi_nimi_sewi += strlen(pana_e_nimi(nn->nanpa_sewi_nimi[i]));
    }

    return nn;
}

static void
weka_e_nanpa_nimi(struct nanpa_nimi *nn)
{
    if (nn != NULL) {
        free(nn->nanpa_sewi_nimi);
        free(nn);
    }
}

char
*nimi_sewi(unsigned int nanpa_nimi_sewi)
{
    int i;
    char *nimi_pana;
    struct nanpa_nimi *nn;

    nn = pana_e_nanpa_nimi(nanpa_nimi_sewi);

    nimi_pana = calloc(nn->suli_pi_nimi_sewi + 1, sizeof(char));
    if (nimi_pana == NULL) {
        weka_e_nanpa_nimi(nn);
        err(EXIT_FAILURE, "memory allocation failed");
    }

    for (i = 0; i < nn->nanpa_nimi; i++) {
        catstr(nimi_pana,
                pana_e_nimi(nn->nanpa_sewi_nimi[i]),
                nn->suli_pi_nimi_sewi);
        catstr(nimi_pana, " ", nn->suli_pi_nimi_sewi);
    }
    catstr(nimi_pana,
            pana_e_nimi(nn->nanpa_sewi_nimi[i]),
            nn->suli_pi_nimi_sewi);

    weka_e_nanpa_nimi(nn);

    return nimi_pana;
}

void
weka_e_nimi_sewi(char *nimi_sewi)
{
    if (nimi_sewi != NULL) {
        free(nimi_sewi);
    }
}