tsuki 0.4.8

Lua 5.4 ported to Rust
Documentation
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#![allow(
    dead_code,
    mutable_transmutes,
    non_camel_case_types,
    non_snake_case,
    non_upper_case_globals,
    unused_assignments,
    unused_mut
)]

#[derive(Copy, Clone)]
#[repr(C)]
pub struct tm {
    pub tm_sec: libc::c_int,
    pub tm_min: libc::c_int,
    pub tm_hour: libc::c_int,
    pub tm_mday: libc::c_int,
    pub tm_mon: libc::c_int,
    pub tm_year: libc::c_int,
    pub tm_wday: libc::c_int,
    pub tm_yday: libc::c_int,
    pub tm_isdst: libc::c_int,
    pub tm_gmtoff: libc::c_long,
    pub tm_zone: *mut libc::c_char,
}

unsafe extern "C" fn os_execute(mut L: *mut lua_State) -> libc::c_int {
    let mut cmd: *const libc::c_char = luaL_optlstring(
        L,
        1 as libc::c_int,
        0 as *const libc::c_char,
        0 as *mut usize,
    );
    let mut stat: libc::c_int = 0;
    *__error() = 0 as libc::c_int;
    stat = system(cmd);
    if !cmd.is_null() {
        return luaL_execresult(L, stat);
    } else {
        lua_pushboolean(L, stat);
        return 1 as libc::c_int;
    };
}
unsafe extern "C" fn os_remove(mut L: *mut lua_State) -> libc::c_int {
    let mut filename: *const libc::c_char = luaL_checklstring(L, 1 as libc::c_int, 0 as *mut usize);
    *__error() = 0 as libc::c_int;
    return luaL_fileresult(
        L,
        (remove(filename) == 0 as libc::c_int) as libc::c_int,
        filename,
    );
}
unsafe extern "C" fn os_rename(mut L: *mut lua_State) -> libc::c_int {
    let mut fromname: *const libc::c_char = luaL_checklstring(L, 1 as libc::c_int, 0 as *mut usize);
    let mut toname: *const libc::c_char = luaL_checklstring(L, 2 as libc::c_int, 0 as *mut usize);
    *__error() = 0 as libc::c_int;
    return luaL_fileresult(
        L,
        (rename(fromname, toname) == 0 as libc::c_int) as libc::c_int,
        0 as *const libc::c_char,
    );
}
unsafe extern "C" fn os_tmpname(mut L: *mut lua_State) -> libc::c_int {
    let mut buff: [libc::c_char; 32] = [0; 32];
    let mut err: libc::c_int = 0;
    strcpy(
        buff.as_mut_ptr(),
        b"/tmp/lua_XXXXXX\0" as *const u8 as *const libc::c_char,
    );
    err = mkstemp(buff.as_mut_ptr());
    if err != -(1 as libc::c_int) {
        close(err);
    }
    err = (err == -(1 as libc::c_int)) as libc::c_int;
    if (err != 0 as libc::c_int) as libc::c_int as libc::c_long != 0 {
        return luaL_error(
            L,
            b"unable to generate a unique filename\0" as *const u8 as *const libc::c_char,
        );
    }
    lua_pushstring(L, buff.as_mut_ptr());
    return 1 as libc::c_int;
}
unsafe extern "C" fn os_getenv(mut L: *mut lua_State) -> libc::c_int {
    lua_pushstring(
        L,
        getenv(luaL_checklstring(L, 1 as libc::c_int, 0 as *mut usize)),
    );
    return 1 as libc::c_int;
}
unsafe extern "C" fn os_clock(mut L: *mut lua_State) -> libc::c_int {
    lua_pushnumber(L, clock() as f64 / 1000000 as libc::c_int as clock_t as f64);
    return 1 as libc::c_int;
}
unsafe extern "C" fn setfield(
    mut L: *mut lua_State,
    mut key: *const libc::c_char,
    mut value: libc::c_int,
    mut delta: libc::c_int,
) {
    lua_pushinteger(L, value as i64 + delta as i64);
    lua_setfield(L, -(2 as libc::c_int), key);
}
unsafe extern "C" fn setboolfield(
    mut L: *mut lua_State,
    mut key: *const libc::c_char,
    mut value: libc::c_int,
) {
    if value < 0 as libc::c_int {
        return;
    }
    lua_pushboolean(L, value);
    lua_setfield(L, -(2 as libc::c_int), key);
}
unsafe extern "C" fn setallfields(mut L: *mut lua_State, mut stm: *mut tm) {
    setfield(
        L,
        b"year\0" as *const u8 as *const libc::c_char,
        (*stm).tm_year,
        1900 as libc::c_int,
    );
    setfield(
        L,
        b"month\0" as *const u8 as *const libc::c_char,
        (*stm).tm_mon,
        1 as libc::c_int,
    );
    setfield(
        L,
        b"day\0" as *const u8 as *const libc::c_char,
        (*stm).tm_mday,
        0 as libc::c_int,
    );
    setfield(
        L,
        b"hour\0" as *const u8 as *const libc::c_char,
        (*stm).tm_hour,
        0 as libc::c_int,
    );
    setfield(
        L,
        b"min\0" as *const u8 as *const libc::c_char,
        (*stm).tm_min,
        0 as libc::c_int,
    );
    setfield(
        L,
        b"sec\0" as *const u8 as *const libc::c_char,
        (*stm).tm_sec,
        0 as libc::c_int,
    );
    setfield(
        L,
        b"yday\0" as *const u8 as *const libc::c_char,
        (*stm).tm_yday,
        1 as libc::c_int,
    );
    setfield(
        L,
        b"wday\0" as *const u8 as *const libc::c_char,
        (*stm).tm_wday,
        1 as libc::c_int,
    );
    setboolfield(
        L,
        b"isdst\0" as *const u8 as *const libc::c_char,
        (*stm).tm_isdst,
    );
}
unsafe extern "C" fn getboolfield(
    mut L: *mut lua_State,
    mut key: *const libc::c_char,
) -> libc::c_int {
    let mut res: libc::c_int = 0;
    res = if lua_getfield(L, -(1 as libc::c_int), key) == 0 as libc::c_int {
        -(1 as libc::c_int)
    } else {
        lua_toboolean(L, -(1 as libc::c_int))
    };
    lua_settop(L, -(1 as libc::c_int) - 1 as libc::c_int);
    return res;
}
unsafe extern "C" fn getfield(
    mut L: *mut lua_State,
    mut key: *const libc::c_char,
    mut d: libc::c_int,
    mut delta: libc::c_int,
) -> libc::c_int {
    let mut isnum: libc::c_int = 0;
    let mut t: libc::c_int = lua_getfield(L, -(1 as libc::c_int), key);
    let mut res: i64 = lua_tointegerx(L, -(1 as libc::c_int), &mut isnum);
    if isnum == 0 {
        if ((t != 0 as libc::c_int) as libc::c_int != 0 as libc::c_int) as libc::c_int
            as libc::c_long
            != 0
        {
            return luaL_error(
                L,
                b"field '%s' is not an integer\0" as *const u8 as *const libc::c_char,
                key,
            );
        } else if ((d < 0 as libc::c_int) as libc::c_int != 0 as libc::c_int) as libc::c_int
            as libc::c_long
            != 0
        {
            return luaL_error(
                L,
                b"field '%s' missing in date table\0" as *const u8 as *const libc::c_char,
                key,
            );
        }
        res = d as i64;
    } else {
        if if res >= 0 as libc::c_int as i64 {
            (res - delta as i64 <= 2147483647 as libc::c_int as i64) as libc::c_int
        } else {
            ((-(2147483647 as libc::c_int) - 1 as libc::c_int + delta) as i64 <= res) as libc::c_int
        } == 0
        {
            return luaL_error(
                L,
                b"field '%s' is out-of-bound\0" as *const u8 as *const libc::c_char,
                key,
            );
        }
        res -= delta as i64;
    }
    lua_settop(L, -(1 as libc::c_int) - 1 as libc::c_int);
    return res as libc::c_int;
}
unsafe extern "C" fn checkoption(
    mut L: *mut lua_State,
    mut conv: *const libc::c_char,
    mut convlen: isize,
    mut buff: *mut libc::c_char,
) -> *const libc::c_char {
    let mut option: *const libc::c_char =
        b"aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%||EcECExEXEyEYOdOeOHOIOmOMOSOuOUOVOwOWOy\0"
            as *const u8 as *const libc::c_char;
    let mut oplen: libc::c_int = 1 as libc::c_int;
    while *option as libc::c_int != '\0' as i32 && oplen as isize <= convlen {
        if *option as libc::c_int == '|' as i32 {
            oplen += 1;
            oplen;
        } else if memcmp(
            conv as *const libc::c_void,
            option as *const libc::c_void,
            oplen as libc::c_ulong,
        ) == 0 as libc::c_int
        {
            memcpy(
                buff as *mut libc::c_void,
                conv as *const libc::c_void,
                oplen as libc::c_ulong,
            );
            *buff.offset(oplen as isize) = '\0' as i32 as libc::c_char;
            return conv.offset(oplen as isize);
        }
        option = option.offset(oplen as isize);
    }
    luaL_argerror(
        L,
        1 as libc::c_int,
        lua_pushfstring(
            L,
            b"invalid conversion specifier '%%%s'\0" as *const u8 as *const libc::c_char,
            conv,
        ),
    );
    return conv;
}
unsafe extern "C" fn l_checktime(mut L: *mut lua_State, mut arg: libc::c_int) -> time_t {
    let mut t: i64 = luaL_checkinteger(L, arg);
    (((t as time_t as i64 == t) as libc::c_int != 0 as libc::c_int) as libc::c_int as libc::c_long
        != 0
        || luaL_argerror(
            L,
            arg,
            b"time out-of-bounds\0" as *const u8 as *const libc::c_char,
        ) != 0) as libc::c_int;
    return t as time_t;
}
unsafe extern "C" fn os_date(mut L: *mut lua_State) -> libc::c_int {
    let mut slen: usize = 0;
    let mut s: *const libc::c_char = luaL_optlstring(
        L,
        1 as libc::c_int,
        b"%c\0" as *const u8 as *const libc::c_char,
        &mut slen,
    );
    let mut t: time_t = if lua_type(L, 2 as libc::c_int) <= 0 as libc::c_int {
        time(0 as *mut time_t)
    } else {
        l_checktime(L, 2 as libc::c_int)
    };
    let mut se: *const libc::c_char = s.offset(slen as isize);
    let mut tmr: tm = tm {
        tm_sec: 0,
        tm_min: 0,
        tm_hour: 0,
        tm_mday: 0,
        tm_mon: 0,
        tm_year: 0,
        tm_wday: 0,
        tm_yday: 0,
        tm_isdst: 0,
        tm_gmtoff: 0,
        tm_zone: 0 as *mut libc::c_char,
    };
    let mut stm: *mut tm = 0 as *mut tm;
    if *s as libc::c_int == '!' as i32 {
        stm = gmtime_r(&mut t, &mut tmr);
        s = s.offset(1);
        s;
    } else {
        stm = localtime_r(&mut t, &mut tmr);
    }
    if stm.is_null() {
        return luaL_error(
            L,
            b"date result cannot be represented in this installation\0" as *const u8
                as *const libc::c_char,
        );
    }
    if strcmp(s, b"*t\0" as *const u8 as *const libc::c_char) == 0 as libc::c_int {
        lua_createtable(L, 0 as libc::c_int, 9 as libc::c_int);
        setallfields(L, stm);
    } else {
        let mut cc: [libc::c_char; 4] = [0; 4];
        let mut b: luaL_Buffer = luaL_Buffer {
            b: 0 as *mut libc::c_char,
            size: 0,
            n: 0,
            L: 0 as *mut lua_State,
            init: C2RustUnnamed { n: 0. },
        };
        cc[0 as libc::c_int as usize] = '%' as i32 as libc::c_char;
        luaL_buffinit(L, &mut b);
        while s < se {
            if *s as libc::c_int != '%' as i32 {
                (b.n < b.size || !(luaL_prepbuffsize(&mut b, 1 as libc::c_int as usize)).is_null())
                    as libc::c_int;
                let fresh0 = s;
                s = s.offset(1);
                let fresh1 = b.n;
                b.n = (b.n).wrapping_add(1);
                *(b.b).offset(fresh1 as isize) = *fresh0;
            } else {
                let mut reslen: usize = 0;
                let mut buff: *mut libc::c_char =
                    luaL_prepbuffsize(&mut b, 250 as libc::c_int as usize);
                s = s.offset(1);
                s;
                s = checkoption(
                    L,
                    s,
                    se.offset_from(s) as libc::c_long,
                    cc.as_mut_ptr().offset(1 as libc::c_int as isize),
                );
                reslen = strftime(buff, 250 as libc::c_int as usize, cc.as_mut_ptr(), stm);
                b.n = (b.n).wrapping_add(reslen);
            }
        }
        luaL_pushresult(&mut b);
    }
    return 1 as libc::c_int;
}
unsafe extern "C" fn os_time(mut L: *mut lua_State) -> libc::c_int {
    let mut t: time_t = 0;
    if lua_type(L, 1 as libc::c_int) <= 0 as libc::c_int {
        t = time(0 as *mut time_t);
    } else {
        let mut ts: tm = tm {
            tm_sec: 0,
            tm_min: 0,
            tm_hour: 0,
            tm_mday: 0,
            tm_mon: 0,
            tm_year: 0,
            tm_wday: 0,
            tm_yday: 0,
            tm_isdst: 0,
            tm_gmtoff: 0,
            tm_zone: 0 as *mut libc::c_char,
        };
        luaL_checktype(L, 1 as libc::c_int, 5 as libc::c_int);
        lua_settop(L, 1 as libc::c_int);
        ts.tm_year = getfield(
            L,
            b"year\0" as *const u8 as *const libc::c_char,
            -(1 as libc::c_int),
            1900 as libc::c_int,
        );
        ts.tm_mon = getfield(
            L,
            b"month\0" as *const u8 as *const libc::c_char,
            -(1 as libc::c_int),
            1 as libc::c_int,
        );
        ts.tm_mday = getfield(
            L,
            b"day\0" as *const u8 as *const libc::c_char,
            -(1 as libc::c_int),
            0 as libc::c_int,
        );
        ts.tm_hour = getfield(
            L,
            b"hour\0" as *const u8 as *const libc::c_char,
            12 as libc::c_int,
            0 as libc::c_int,
        );
        ts.tm_min = getfield(
            L,
            b"min\0" as *const u8 as *const libc::c_char,
            0 as libc::c_int,
            0 as libc::c_int,
        );
        ts.tm_sec = getfield(
            L,
            b"sec\0" as *const u8 as *const libc::c_char,
            0 as libc::c_int,
            0 as libc::c_int,
        );
        ts.tm_isdst = getboolfield(L, b"isdst\0" as *const u8 as *const libc::c_char);
        t = mktime(&mut ts);
        setallfields(L, &mut ts);
    }
    if t != t as i64 as time_t || t == -(1 as libc::c_int) as time_t {
        return luaL_error(
            L,
            b"time result cannot be represented in this installation\0" as *const u8
                as *const libc::c_char,
        );
    }
    lua_pushinteger(L, t as i64);
    return 1 as libc::c_int;
}
unsafe extern "C" fn os_difftime(mut L: *mut lua_State) -> libc::c_int {
    let mut t1: time_t = l_checktime(L, 1 as libc::c_int);
    let mut t2: time_t = l_checktime(L, 2 as libc::c_int);
    lua_pushnumber(L, difftime(t1, t2));
    return 1 as libc::c_int;
}
unsafe extern "C" fn os_setlocale(mut L: *mut lua_State) -> libc::c_int {
    static mut cat: [libc::c_int; 6] = [
        0 as libc::c_int,
        1 as libc::c_int,
        2 as libc::c_int,
        3 as libc::c_int,
        4 as libc::c_int,
        5 as libc::c_int,
    ];
    static mut catnames: [*const libc::c_char; 7] = [
        b"all\0" as *const u8 as *const libc::c_char,
        b"collate\0" as *const u8 as *const libc::c_char,
        b"ctype\0" as *const u8 as *const libc::c_char,
        b"monetary\0" as *const u8 as *const libc::c_char,
        b"numeric\0" as *const u8 as *const libc::c_char,
        b"time\0" as *const u8 as *const libc::c_char,
        0 as *const libc::c_char,
    ];
    let mut l: *const libc::c_char = luaL_optlstring(
        L,
        1 as libc::c_int,
        0 as *const libc::c_char,
        0 as *mut usize,
    );
    let mut op: libc::c_int = luaL_checkoption(
        L,
        2 as libc::c_int,
        b"all\0" as *const u8 as *const libc::c_char,
        catnames.as_ptr(),
    );
    lua_pushstring(L, setlocale(cat[op as usize], l));
    return 1 as libc::c_int;
}
unsafe extern "C" fn os_exit(mut L: *mut lua_State) -> libc::c_int {
    let mut status: libc::c_int = 0;
    if lua_type(L, 1 as libc::c_int) == 1 as libc::c_int {
        status = if lua_toboolean(L, 1 as libc::c_int) != 0 {
            0 as libc::c_int
        } else {
            1 as libc::c_int
        };
    } else {
        status = luaL_optinteger(L, 1 as libc::c_int, 0 as libc::c_int as i64) as libc::c_int;
    }
    if lua_toboolean(L, 2 as libc::c_int) != 0 {
        lua_close(L);
    }
    if !L.is_null() {
        exit(status);
    }
    return 0 as libc::c_int;
}
static mut syslib: [luaL_Reg; 12] = unsafe {
    [
        {
            let mut init = luaL_Reg {
                name: b"clock\0" as *const u8 as *const libc::c_char,
                func: Some(os_clock as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"date\0" as *const u8 as *const libc::c_char,
                func: Some(os_date as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"difftime\0" as *const u8 as *const libc::c_char,
                func: Some(os_difftime as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"execute\0" as *const u8 as *const libc::c_char,
                func: Some(os_execute as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"exit\0" as *const u8 as *const libc::c_char,
                func: Some(os_exit as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"getenv\0" as *const u8 as *const libc::c_char,
                func: Some(os_getenv as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"remove\0" as *const u8 as *const libc::c_char,
                func: Some(os_remove as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"rename\0" as *const u8 as *const libc::c_char,
                func: Some(os_rename as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"setlocale\0" as *const u8 as *const libc::c_char,
                func: Some(os_setlocale as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"time\0" as *const u8 as *const libc::c_char,
                func: Some(os_time as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"tmpname\0" as *const u8 as *const libc::c_char,
                func: Some(os_tmpname as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: 0 as *const libc::c_char,
                func: None,
            };
            init
        },
    ]
};