forked from kilobyte/kbtin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
876 lines (760 loc) · 23.1 KB
/
string.c
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
/*********************************************************************/
/* file: variables.c - functions related the the variables */
/* TINTIN ++ */
/* (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t */
/* coded by peter unold 1992 */
/*********************************************************************/
#include "tintin.h"
#include <math.h>
#include "protos/action.h"
#include "protos/alias.h"
#include "protos/chinese.h"
#include "protos/colors.h"
#include "protos/globals.h"
#include "protos/hash.h"
#include "protos/eval.h"
#include "protos/math.h"
#include "protos/print.h"
#include "protos/parse.h"
#include "protos/path.h"
#include "protos/session.h"
#include "protos/ticks.h"
#include "protos/unicode.h"
#include "protos/utils.h"
#include "protos/vars.h"
extern struct session *if_command(const char *arg, struct session *ses);
/*************************/
/* the #variable command */
/*************************/
void variable_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE];
arg = get_arg(arg, left, 0, ses);
bool r=*space_out(arg);
arg = get_arg(arg, right, 1, ses);
if (*left && r)
{
set_variable(left, right, ses);
return;
}
show_hashlist(ses, ses->myvars, left,
"#THESE VARIABLES HAVE BEEN SET:",
"#THAT VARIABLE IS NOT DEFINED.");
}
/**********************/
/* the #unvar command */
/**********************/
void unvariable_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE];
do
{
arg = get_arg(arg, left, 0, ses);
delete_hashlist(ses, ses->myvars, left,
ses->mesvar[MSG_VARIABLE]? "#Ok. $%s is no longer a variable." : 0,
ses->mesvar[MSG_VARIABLE]? "#THAT VARIABLE (%s) IS NOT DEFINED." : 0);
} while (*arg);
}
/************************/
/* the #tolower command */
/************************/
void tolower_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE];
WC txt[BUFFER_SIZE];
arg = get_arg(arg, left, 0, ses);
arg = get_arg(arg, right, 1, ses);
if (!*left)
return tintin_eprintf(ses, "#Syntax: #tolower <var> <text>");
TO_WC(txt, right);
for (WC *p = txt; *p; p++)
*p = towlower(*p);
WRAP_WC(right, txt);
set_variable(left, right, ses);
}
/************************/
/* the #toupper command */
/************************/
void toupper_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE];
WC txt[BUFFER_SIZE];
arg = get_arg(arg, left, 0, ses);
arg = get_arg(arg, right, 1, ses);
if (!*left)
return tintin_eprintf(ses, "#Syntax: #toupper <var> <text>");
TO_WC(txt, right);
for (WC *p = txt; *p; p++)
*p = towupper(*p);
WRAP_WC(right, txt);
set_variable(left, right, ses);
}
/***************************/
/* the #firstupper command */
/***************************/
void firstupper_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE];
WC txt[BUFFER_SIZE];
arg = get_arg(arg, left, 0, ses);
arg = get_arg(arg, right, 1, ses);
if (!*left)
return tintin_eprintf(ses, "#Syntax: #firstupper <var> <text>");
TO_WC(txt, right);
*txt=towupper(*txt);
WRAP_WC(right, txt);
set_variable(left, right, ses);
}
/***************************/
/* the #firstlower command */
/***************************/
void firstlower_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE];
WC txt[BUFFER_SIZE];
arg = get_arg(arg, left, 0, ses);
arg = get_arg(arg, right, 1, ses);
if (!*left)
return tintin_eprintf(ses, "#Syntax: #firstlower <var> <text>");
TO_WC(txt, right);
*txt=towlower(*txt);
WRAP_WC(right, txt);
set_variable(left, right, ses);
}
/***********************/
/* the #strlen command */
/***********************/
void strlen_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE];
arg = get_arg(arg, left, 0, ses);
arg = get_arg(arg, right, 1, ses);
if (!*left)
return tintin_eprintf(ses, "#Syntax: #strlen <var> <text>");
sprintf(right, "%d", utf8_width(right));
set_variable(left, right, ses);
}
/**********************/
/* the #strlen inline */
/**********************/
int strlen_inline(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE];
arg = get_arg(arg, left, 1, ses);
return utf8_width(left);
}
/****************************************************/
/* the #reverse command * By Sverre Normann */
/* with little changes by Jakub Narebski */
/****************************************************/
/* Syntax: #reverse {destination variable} {string} */
/****************************************************/
/* Will return the reverse of a string. */
/****************************************************/
/* can be used to get return path using #savepath */
/****************************************************/
/* FUNCTION: reverse - reverses string */
/* ARGUMENTS: src - string to be reversed */
/* dest - destination string */
/* RESULT: reversed string */
/* NOTE: no check */
static WC* revstr(WC *dest, WC *src)
{
int ilast = WClen(src) - 1;
for (int i = ilast; i >= 0; i--)
dest[ilast - i] = src[i];
dest[ilast + 1] = '\0';
return dest;
}
/************************/
/* the #reverse command */
/************************/
void reverse_command(const char *arg, struct session *ses)
{
char destvar[BUFFER_SIZE], strvar[BUFFER_SIZE];
WC origstring[BUFFER_SIZE], revstring[BUFFER_SIZE];
arg = get_arg(arg, destvar, 0, ses);
arg = get_arg(arg, strvar, 1, ses);
if (!*destvar)
return tintin_eprintf(ses, "#Error - Syntax: #reverse {destination variable} {string}");
TO_WC(origstring, strvar);
revstr(revstring, origstring);
WRAP_WC(strvar, revstring);
set_variable(destvar, strvar, ses);
}
/***********************/
/* the #random command */
/***********************/
void random_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE], dummy;
int low, high, number;
arg = get_arg(arg, left, 0, ses);
arg = get_arg(arg, right, 1, ses);
if (!*left || !*right)
return tintin_eprintf(ses, "#Syntax: #random <var> <low,high>");
if (sscanf(right, "%d, %d%c", &low, &high, &dummy) != 2)
return tintin_eprintf(ses, "#Wrong number of range arguments in #random: got {%s}.", right);
if (low < 0 || high < 0)
return tintin_eprintf(ses, "#Both arguments of range in #random should be ≥0, got %d,%d.", low, high);
if (low > high)
{
number = low;
low = high, high = number;
}
number = low + rand() % (high - low + 1);
sprintf(right, "%d", number);
set_variable(left, right, ses);
}
/**********************/
/* the #random inline */
/**********************/
int random_inline(const char *arg, struct session *ses)
{
char right[BUFFER_SIZE];
int low, high, tmp;
arg = get_arg(arg, right, 1, ses);
if (!*right)
tintin_eprintf(ses, "#Syntax: #random <low,high>");
else if (sscanf(right, "%d, %d", &low, &high) != 2)
tintin_eprintf(ses, "#Wrong number of range arguments in #random: got {%s}.", right);
else if (low < 0 || high < 0)
tintin_eprintf(ses, "#Both arguments of range in #random should be ≥0, got %d,%d.", low, high);
else
{
if (low > high)
{
tmp = low;
low = high, high = tmp;
}
return low + rand() % (high - low + 1);
}
return 0;
}
/************************************************************************************/
/* cut a string to width len, putting the cut point into *rstr and return the width */
/************************************************************************************/
static int cutws(WC *str, int len, WC **rstr, int *color)
{
char cbuf[BUFFER_SIZE], *cp=cbuf;
for (WC *sp=str; *sp; sp++)
*cp++ = (*sp>'~')? 'x' : *sp; // only color codes count
cp=cbuf;
int s=0;
while (*str)
{
if (*str=='~')
{
const char *cend = cp;
if (getcolor(&cend, color, true))
{
str+=(cend-cp)+1;
cp+=(cend-cp)+1;
continue;
}
}
int w=wcwidth(*str);
if (w<0)
w=0;
if (w && s+w>len)
break;
str++; cp++;
s+=w;
}
*rstr=str;
return s;
}
/*****************************************************/
/* Syntax: #postpad {dest var} {length} {text} */
/*****************************************************/
/* Truncates text if it's too long for the specified */
/* length. Pads with spaces at the end if the text */
/* isn't long enough. */
/*****************************************************/
void postpad_command(const char *arg, struct session *ses)
{
char destvar[BUFFER_SIZE], lengthstr[BUFFER_SIZE], astr[BUFFER_SIZE], *aptr;
WC bstr[BUFFER_SIZE], *bptr;
int len, length, dummy=-1;
arg = get_arg(arg, destvar, 0, ses);
arg = get_arg(arg, lengthstr, 0, ses);
arg = get_arg(arg, astr, 1, ses);
if (!*lengthstr)
return tintin_eprintf(ses, "#Error - Syntax: #postpad {dest var} {length} {text}");
if (!sscanf(lengthstr, "%d", &length) || (length < 1) || (length > BUFFER_SIZE-10))
return tintin_eprintf(ses, "#Error in #postpad - length has to be a positive number >0, got {%s}.", lengthstr);
TO_WC(bstr, astr);
len=cutws(bstr, length, &bptr, &dummy);
aptr=astr+wc_to_utf8(astr, bstr, bptr-bstr, BUFFER_SIZE-3);
while (len<length)
len++, *aptr++=' ';
*aptr=0;
set_variable(destvar, astr, ses);
}
/*****************************************************/
/* the #prepad command * By Sverre Normann */
/* with little changes by Jakub Narebski */
/*****************************************************/
/* Syntax: #prepad {dest var} {length} {text} */
/*****************************************************/
/* Truncates text if it's too long for the specified */
/* length. Pads with spaces at the start if the text */
/* isn't long enough. */
/*****************************************************/
void prepad_command(const char *arg, struct session *ses)
{
char destvar[BUFFER_SIZE], astr[BUFFER_SIZE], lengthstr[BUFFER_SIZE], *aptr;
WC bstr[BUFFER_SIZE], *bptr;
int len, length, dummy=-1;
arg = get_arg(arg, destvar, 0, ses);
arg = get_arg(arg, lengthstr, 0, ses);
arg = get_arg(arg, astr, 1, ses);
if (!*lengthstr)
return tintin_eprintf(ses, "#Error - Syntax: #prepad {dest var} {length} {text}");
if (!sscanf(lengthstr, "%d", &length) || (length < 1) || (length>BUFFER_SIZE-10))
return tintin_eprintf(ses, "#Error in #prepad - length has to be a positive number >0, got {%s}.", lengthstr);
TO_WC(bstr, astr);
len=cutws(bstr, length, &bptr, &dummy);
aptr=astr;
while (len<length)
len++, *aptr++=' ';
aptr+=wc_to_utf8(aptr, bstr, bptr-bstr, BUFFER_SIZE-3);
*aptr=0;
set_variable(destvar, astr, ses);
}
/**************************/
/* the #substring command */
/**************************/
void substring_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], mid[BUFFER_SIZE], right[BUFFER_SIZE], *p;
WC buf[BUFFER_SIZE];
int l, r;
arg = get_arg(arg, left, 0, ses);
arg = get_arg(arg, mid, 0, ses);
arg = get_arg(arg, right, 1, ses);
l=strtol(mid, &p, 10);
if (*p==',')
r=strtol(p+1, &p, 10);
else
r=l;
if (l<1)
l=1;
if (!*left || (p==mid) || (*p) || (r<0))
return tintin_eprintf(ses, "#SYNTAX: substr <var> <l>[,<r>] <string>");
p=mid;
TO_WC(buf, right);
WC *lb=buf;
int color=-1;
if (l>1)
{
int lw=cutws(buf, l-1, &lb, &color);
if (color!=-1)
p+=setcolor(p, color);
if (lw==l-2 && *lb) /* 1 column short and not end of string → half of a CJK char */
{
*p++=' ';
l++;
lb++;
while (*lb && !wcwidth(*lb)) /* eat combining chars as well */
lb++;
}
}
WC *rb;
int rw=cutws(lb, r+1-l, &rb, &color);
p+=wc_to_utf8(p, lb, rb-lb, mid+BUFFER_SIZE-3-p);
if (rw==r-l && *rb) /* half of a CJK char */
*p++=' ', *p=0;
set_variable(left, mid, ses);
}
/***********************/
/* the #strcmp command */
/***********************/
struct session *strcmp_command(const char *line, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE], cmd[BUFFER_SIZE];
line = get_arg(line, left, 0, ses);
line = get_arg(line, right, 0, ses);
line = get_arg_in_braces(line, cmd, 1);
if (!*cmd)
{
tintin_eprintf(ses, "#Syntax: #strcmp <a> <b> <command> [#else <command>]");
return ses;
}
if (!strcmp(left, right))
return parse_input(cmd, true, ses);
line = get_arg_in_braces(line, left, 0);
if (*left == tintin_char)
{
if (is_abrev(left + 1, "else"))
{
line = get_arg_in_braces(line, right, 1);
ses=parse_input(right, true, ses);
}
if (is_abrev(left + 1, "elif"))
ses=if_command(line, ses);
}
return ses;
}
/**********************/
/* the #strcmp inline */
/**********************/
int strcmp_inline(const char *line, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE];
line = get_arg(line, left, 0, ses);
line = get_arg(line, right, 1, ses);
return !strcmp(left, right);
}
/***************************************/
/* the #ifstrequal command */
/***************************************/
/* (mainstream tintin++ compatibility) */
/***************************************/
struct session *ifstrequal_command(const char *line, struct session *ses)
{
return strcmp_command(line, ses);
}
/*************************/
/* the #ifexists command */
/*************************/
struct session *ifexists_command(const char *line, struct session *ses)
{
char left[BUFFER_SIZE], cmd[BUFFER_SIZE];
line = get_arg(line, left, 0, ses);
line = get_arg_in_braces(line, cmd, 1);
if (!*cmd)
{
tintin_eprintf(ses, "#Syntax: #ifexists <varname> <command> [#else <command>]");
return ses;
}
if (get_hash(ses->myvars, left))
return parse_input(cmd, true, ses);
line = get_arg_in_braces(line, left, 0);
if (*left == tintin_char)
{
if (is_abrev(left + 1, "else"))
{
line = get_arg_in_braces(line, cmd, 1);
ses=parse_input(cmd, true, ses);
}
if (is_abrev(left + 1, "elif"))
ses=if_command(line, ses);
}
return ses;
}
/*********************/
/* the #ctoi command */
/*********************/
void ctoi_command(const char* arg, struct session* ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE];
arg=get_arg(arg, left, 0, ses);
arg=get_arg(arg, right, 1, ses);
if (!*left || !*right)
return tintin_eprintf(ses, "#Syntax: #ctoi <var> <text>");
ctoi(right);
set_variable(left, right, ses);
}
/*****************************/
/* the #initvariable command */
/*****************************/
void initvariable_command(const char* arg, struct session* ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE];
arg=get_arg(arg, left, 0, ses);
arg=get_arg(arg, right, 1, ses);
if (!*left)
tintin_eprintf(ses, "#Syntax: #initvar <var> <value>");
else if (!get_hash(ses->myvars, left))
set_variable(left, right, ses);
}
/*********************/
/* the #trim command */
/*********************/
void trim_command(const char *arg, struct session *ses)
{
char destvar[BUFFER_SIZE], str[BUFFER_SIZE], *s, *e, *p;
arg = get_arg(arg, destvar, 0, ses);
arg = get_arg(arg, str, 1, ses);
if (!*destvar)
return tintin_eprintf(ses, "#Error - syntax: #trim {dest var} {text}");
s = str;
while (isaspace(*s))
s++;
for (p=e=s; *p; p++)
if (!isaspace(*p))
e = p+1;
*e=0;
set_variable(destvar, s, ses);
}
#define INVALID_TIME (int)0x80000000
/************************************************************/
/* parse time, return # of seconds or INVALID_TIME on error */
/************************************************************/
static int time2secs(const char *tt, struct session *ses)
{
int w, t=0;
if (!*tt)
{
bad:
tintin_eprintf(ses, "#time format should be: <#y[ears][, ] #w #d #h #m [and] #[s]> or just <#> of seconds.");
tintin_eprintf(ses, "#got: {%s}.", tt);
return INVALID_TIME;
}
t=0;
for (;;)
{
char *err;
w=strtol(tt, &err, 10);
if (tt==err)
goto bad;
tt=err;
while (isaspace(*tt))
tt++;
switch (toalower(*tt))
{
case 'w':
w*=7;
goto day;
case 'y':
w*=365;
day:
case 'd':
w*=24;
case 'h':
w*=60;
case 'm':
w*=60;
case 's':
while (isalpha(*tt))
tt++;
case 0:
t+=w;
break;
default:
goto bad;
}
if (*tt==',')
tt++;
while (isaspace(*tt))
tt++;
if (!strncmp(tt, "and", 3))
tt+=3;
if (!*tt)
break;
}
return t;
}
/**********************/
/* the #ctime command */
/**********************/
void ctime_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], arg2[BUFFER_SIZE], *ct, *p;
time_t tt;
arg = get_arg(arg, left, 0, ses);
arg = get_arg(arg, arg2, 1, ses);
if (!*arg2)
tt=time(0);
else if ((tt=time2secs(arg2, ses))==INVALID_TIME)
return;
p = ct = ctime_r(&tt, arg2);
while (p && *p)
{
if (*p == '\n')
{
*p = '\0';
break;
}
p++;
}
if (!*left)
tintin_printf(ses, "#%s.", ct);
else
set_variable(left, ct, ses);
}
/*********************/
/* the #time command */
/*********************/
void time_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], ct[BUFFER_SIZE];
arg = get_arg(arg, left, 0, ses);
arg = get_arg(arg, ct, 1, ses);
if (*ct)
{
int t=time2secs(ct, ses);
if (t==INVALID_TIME)
return;
sprintf(ct, "%d", t);
}
else
sprintf(ct, "%d", (int)time(0));
if (!*left)
tintin_printf(ses, "#%s.", ct);
else
set_variable(left, ct, ses);
}
/**************************/
/* the #localtime command */
/**************************/
void localtime_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], ct[BUFFER_SIZE];
time_t t;
struct tm ts;
arg = get_arg(arg, left, 0, ses);
arg = get_arg(arg, ct, 1, ses);
if (*ct)
{
t=time2secs(ct, ses);
if (t==INVALID_TIME)
return;
sprintf(ct, "%ld", (long)t);
}
else
t=time(0);
localtime_r(&t, &ts);
sprintf(ct, "%02d %02d %02d %02d %02d %04d %d %d %d",
ts.tm_sec, ts.tm_min, ts.tm_hour,
ts.tm_mday, ts.tm_mon+1, ts.tm_year+1900,
ts.tm_wday, ts.tm_yday+1, ts.tm_isdst);
if (!*left)
tintin_printf(ses, "#%s.", ct);
else
set_variable(left, ct, ses);
}
/***********************/
/* the #gmtime command */
/***********************/
void gmtime_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], ct[BUFFER_SIZE];
time_t t;
struct tm ts;
arg = get_arg(arg, left, 0, ses);
arg = get_arg(arg, ct, 1, ses);
if (*ct)
{
t=time2secs(ct, ses);
if (t==INVALID_TIME)
return;
sprintf(ct, "%ld", (long)t);
}
else
t=time(0);
gmtime_r(&t, &ts);
sprintf(ct, "%02d %02d %02d %02d %02d %04d %d %d %d",
ts.tm_sec, ts.tm_min, ts.tm_hour,
ts.tm_mday, ts.tm_mon+1, ts.tm_year+1900,
ts.tm_wday, ts.tm_yday+1, ts.tm_isdst);
if (!*left)
tintin_printf(ses, "#%s.", ct);
else
set_variable(left, ct, ses);
}
/*********************/
/* the #angle inline */
/*********************/
num_t angle_inline(const char *line, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE];
line = get_arg(line, left, 0, ses);
line = get_arg(line, right, 1, ses);
if (!*left || !*right)
{
tintin_eprintf(ses, "#Error: #angle requires two args.");
return 0;
}
num_t x = eval_expression(left, ses);
num_t y = eval_expression(right, ses);
num_t a = atan2(y, x) * 180 * M_1_PI * DENOM;
if (a < 0)
a += 360*DENOM;
return a;
}
/*******************/
/* the #sin inline */
/*******************/
num_t sinus_inline(const char *line, struct session *ses)
{
char arg[BUFFER_SIZE];
line = get_arg(line, arg, 1, ses);
if (!*arg)
{
tintin_eprintf(ses, "#Error: #sin requires an argument.");
return 0;
}
num_t x = eval_expression(arg, ses);
return sin(x * M_PI / 180 / DENOM) * DENOM;
}
/*******************/
/* the #cos inline */
/*******************/
num_t cosinus_inline(const char *line, struct session *ses)
{
char arg[BUFFER_SIZE];
line = get_arg(line, arg, 1, ses);
if (!*arg)
{
tintin_eprintf(ses, "#Error: #cos requires an argument.");
return 0;
}
num_t x = eval_expression(arg, ses);
return cos(x * M_PI / 180 / DENOM) * DENOM;
}
/********************/
/* the #sqrt inline */
/********************/
num_t sqrt_inline(const char *line, struct session *ses)
{
char arg[BUFFER_SIZE];
line = get_arg(line, arg, 1, ses);
if (!*arg)
{
tintin_eprintf(ses, "#Error: #sqrt requires an argument.");
return 0;
}
num_t x = eval_expression(arg, ses);
if (x < 0)
{
tintin_eprintf(ses, "#Error: you're imagining that sqrt can take arg < 0");
return 0;
}
return sqrt(x * 1.0 / DENOM) * DENOM;
}
/*******************/
/* the #abs inline */
/*******************/
num_t abs_inline(const char *line, struct session *ses)
{
char arg[BUFFER_SIZE];
line = get_arg(line, arg, 1, ses);
if (!*arg)
{
tintin_eprintf(ses, "#Error: #abs requires an argument.");
return 0;
}
num_t x = eval_expression(arg, ses);
if (x < 0)
x = -x;
return x;
}
/*********************/
/* the #round inline */
/*********************/
num_t round_inline(const char *line, struct session *ses)
{
char arg[BUFFER_SIZE];
line = get_arg(line, arg, 1, ses);
if (!*arg)
{
tintin_eprintf(ses, "#Error: #round requires an argument.");
return 0;
}
num_t x = eval_expression(arg, ses);
// round away from 0
x += (x >= 0)? DENOM/2 : -DENOM/2;
return x / DENOM * DENOM;
}