-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpicol.h
5489 lines (5136 loc) · 160 KB
/
picol.h
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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Tcl in ~5000 lines of code. BSD-licensed (see LICENSE).
*
* 2007-03-15
* The original ~500 line release by Salvatore Sanfilippo (antirez).
* 2007-04 - 2007-05
* Many features, tests, and a test framework implemented by Richard
* Suchenwirth. See ABOUT.txt and CHANGELOG-2007.txt.
* 2016-2021
* New features and a lot of bugfixes by D. Bohdan. Picol refactored into
* a header library. See the Fossil timeline.
*
* This header file contains both the interface and the implementation for
* Picol. To instantiate the implementation put the line
* #define PICOL_IMPLEMENTATION
* in a single source code file of your project above where you include this
* file. To override the default compilation options place a modified copy of
* the entire PICOL_CONFIGURATION section above where you include this file.
*/
/* -------------------------------------------------------------------------- */
#ifndef PICOL_CONFIGURATION
#define PICOL_CONFIGURATION
#define PICOL_MAX_STR 4096
#define PICOL_EVAL_BUF_SIZE (PICOL_MAX_STR*2)
#define PICOL_SOURCE_BUF_SIZE (PICOL_MAX_STR*64)
#ifndef PICOL_SMALL_STACK
# define PICOL_SMALL_STACK 1
#endif
#ifdef _MSC_VER
# define PICOL_MAX_LEVEL 10
#else
# define PICOL_MAX_LEVEL 30
#endif
#ifdef __MINGW32__
# include <_mingw.h> /* For __MINGW64_VERSION_MAJOR. */
#endif
/* Optional features. Define as zero to disable. */
#define PICOL_FEATURE_ARRAYS 1
#if defined(_MSC_VER) || defined(__MINGW64_VERSION_MAJOR)
/* ^^^ MinGW-w64 lacks glob.h. */
# define PICOL_FEATURE_GLOB 0
#else
# define PICOL_FEATURE_GLOB 1
#endif
#define PICOL_FEATURE_INTERP 1
#define PICOL_FEATURE_IO 1
#define PICOL_FEATURE_PUTS 1
#endif /* PICOL_CONFIGURATION */
#ifndef PICOL_MEMORY_MANAGEMENT
# define PICOL_MEMORY_MANAGEMENT
# define PICOL_FREE free
# define PICOL_MALLOC malloc
# define PICOL_CALLOC calloc
# define PICOL_REALLOC realloc
#endif /* PICOL_MEMORY_MANAGEMENT */
/* -------------------------------------------------------------------------- */
#ifndef PICOL_H
#define PICOL_H
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if PICOL_FEATURE_IO
#include <sys/stat.h>
#endif
#define PICOL_PATCHLEVEL "0.6.1"
#if PICOL_SMALL_STACK
# define PICOL_BUFFER_CREATE(name, size) \
char *name = PICOL_CALLOC((size), sizeof(char)); \
int name##_SIZE = (size) * sizeof(char)
# define PICOL_BUFFER_DESTROY(name) PICOL_FREE(name)
# define PICOL_BUFFER_SIZE(name) name##_SIZE
#else
# define PICOL_BUFFER_CREATE(name, size) \
char name[(size)] = "";
# define PICOL_BUFFER_DESTROY(name)
# define PICOL_BUFFER_SIZE(name) sizeof(name)
#endif
/* MSVC compatibility. */
#ifdef _MSC_VER
# define PICOL_GETCWD _getcwd
# define PICOL_GETPID GetCurrentProcessId
# define PICOL_POPEN _popen
# define PICOL_PCLOSE _pclose
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
# if _MSC_VER < 1900
# define PICOL_SNPRINTF(str, size, format, ...) \
_snprintf_s(str, size, _TRUNCATE, format, __VA_ARGS__)
# endif
/* We also include <windows.h> below, but for all compilers on Windows,
not just MSVC. */
#else
# include <unistd.h>
# define PICOL_GETCWD getcwd
# define PICOL_GETPID getpid
# define PICOL_POPEN popen
# define PICOL_PCLOSE pclose
# define PICOL_SNPRINTF(str, size, format, ...) \
snprintf(str, size, format, __VA_ARGS__)
#endif /* _MSC_VER */
#if PICOL_FEATURE_GLOB
# include <glob.h>
#endif
/* The value for ::tcl_platform(engine), which Tcl code can use to distinguish
between Tcl implementations. */
#define PICOL_TCL_PLATFORM_ENGINE_STRING "Picol"
/* Crudely detect the platform we're being compiled on. Choose the value for
::tcl_platform(platform) and a corresponding numerical value we can use for
conditional compilation accordingly. */
#define PICOL_TCL_PLATFORM_UNKNOWN 0
#define PICOL_TCL_PLATFORM_UNIX 1
#define PICOL_TCL_PLATFORM_WINDOWS 2
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || \
defined(_MSC_VER)
/* These names are ugly, but consistent and arguably self-explanatory. */
# define PICOL_TCL_PLATFORM_PLATFORM PICOL_TCL_PLATFORM_WINDOWS
# define PICOL_TCL_PLATFORM_PLATFORM_STRING "windows"
# include <windows.h>
#elif defined(_POSIX_VERSION) && !defined(__MSDOS__)
/* A workaround for DJGPP ^^^, which defines _POSIX_VERSION. */
# define PICOL_TCL_PLATFORM_PLATFORM PICOL_TCL_PLATFORM_UNIX
# define PICOL_TCL_PLATFORM_PLATFORM_STRING "unix"
# if !defined(PICOL_CAN_USE_CLOCK_GETTIME)
# if _POSIX_C_SOURCE >= 199309L && (!defined(__GNU_LIBRARY__) || \
(__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 17))
# define PICOL_CAN_USE_CLOCK_GETTIME 1
# else
# define PICOL_CAN_USE_CLOCK_GETTIME 0
# endif
# endif
#elif !defined(PICOL_TCL_PLATFORM_PLATFORM)
# define PICOL_TCL_PLATFORM_PLATFORM PICOL_TCL_PLATFORM_UNKNOWN
# define PICOL_TCL_PLATFORM_PLATFORM_STRING "unknown"
#endif
#define PICOL_INFO_SCRIPT_VAR "::_script_"
/* --------- Most macros need the picol_* environment: argc, argv, and interp */
#define PICOL_ERROR_TOO_LONG "string too long"
#define PICOL_ERROR_ARGS "wrong # args for \"%s\""
#define PICOL_ERROR_ARGS_HELP "wrong # args: should be \"%s\""
#define PICOL_APPEND(dst, src) \
do { \
if ((strlen(dst) + strlen(src)) > sizeof(dst) - 1) { \
return picolErr(interp, PICOL_ERROR_TOO_LONG); \
} \
strcat(dst, src); \
} while (0)
#define PICOL_APPEND_BREAK(dst, src, too_long) \
{ \
if ((strlen(dst) + strlen(src)) > sizeof(dst) - 1) { \
too_long = 1; break; \
} \
strcat(dst, src); \
}
#define PICOL_ARITY(x) \
do { \
if (!(x)) { \
return picolErrFmt(interp, PICOL_ERROR_ARGS, argv[0]); \
} \
} while (0);
#define PICOL_ARITY2(x,s) \
do { \
if (!(x)) { \
return picolErrFmt(interp, PICOL_ERROR_ARGS_HELP, s); \
} \
} while (0)
#define PICOL_COLONED(_p) \
(*(_p) == ':' && *(_p+1) == ':') /* indicates global */
#define PICOL_COMMAND(c) \
picolResult picol_##c(picolInterp* interp, int argc, \
const char* argv[], void* pd)
#define PICOL_EQ(a,b) \
((*a)==(*b) && !strcmp(a, b))
#define PICOL_FOLD(init,step,n) \
do {init; for(p=n;p<argc;p++) { \
PICOL_SCAN_INT(a, argv[p]);step;}} while (0)
#define PICOL_FOREACH(_v, _size, _p, _s) \
for(_p = picolListHead(_s, _v, _size); \
_p; \
_p = picolListHead(_p, _v, _size))
#define PICOL_LAPPEND(dst, src) \
do { \
int needbraces = picolNeedsBraces(src); \
if (*dst != '\0') {PICOL_APPEND(dst, " ");} \
if (needbraces) {PICOL_APPEND(dst, "{");} \
PICOL_APPEND(dst, src); \
if (needbraces) {PICOL_APPEND(dst, "}");} \
} while (0)
#define PICOL_LAPPEND_BREAK(dst, src, too_long) \
{ \
int needbraces = picolNeedsBraces(src); \
if (*dst != '\0') {PICOL_APPEND_BREAK(dst, " ", too_long);} \
if (needbraces) {PICOL_APPEND_BREAK(dst, "{", too_long);} \
PICOL_APPEND_BREAK(dst, src, too_long); \
if (needbraces) {PICOL_APPEND_BREAK(dst, "}", too_long);} \
}
/* This is the unchecked version, for functions without access to 'interp'. */
#define PICOL_LAPPEND_X(dst,src) \
do { \
int needbraces = picolNeedsBraces(src); \
if (*dst != '\0') strcat(dst, " "); \
if (needbraces) {strcat(dst, "{");} \
strcat(dst, src); \
if (needbraces) {strcat(dst, "}");} \
} while (0)
#define PICOL_GET_VAR(_v,_n) \
_v = picolGetVar(interp, _n); \
if (_v == NULL) return picolErrFmt(interp, \
"can't read \"%s\": no such variable", _n);
#define PICOL_PARSED(_t) \
do {p->end = p->pos - 1; p->type = _t;} while (0)
#define PICOL_RETURN_PARSED(_t) \
do {PICOL_PARSED(_t); return PICOL_OK;} while (0)
#define PICOL_SCAN_INT(v,x) \
do { \
int base = picolIsInt(x); \
if (base > 0) {v = picolScanInt(x, base);} \
else { return picolErrFmt(interp, "expected integer " \
"but got \"%s\"", x); } \
} while (0)
#define PICOL_SCAN_PTR(v,x) \
do { \
void* _p; \
if ((_p=picolScanPtr(x))) {v = _p;} \
else { return picolErrFmt(interp, "expected pointer but got \"%s\"", \
x);} \
} while (0)
#define PICOL_SUBCMD(x) \
(PICOL_EQ(argv[1], x))
#define PICOL_UNUSED(x) (void)(x)
typedef enum picolResult {
PICOL_OK, PICOL_ERR, PICOL_RETURN, PICOL_BREAK, PICOL_CONTINUE
} picolResult;
typedef enum picolBool {PICOL_FALSE = 0, PICOL_TRUE = 1} picolBool;
enum {PICOL_PT_ESC, PICOL_PT_STR, PICOL_PT_CMD, PICOL_PT_VAR, PICOL_PT_SEP,
PICOL_PT_EOL, PICOL_PT_EOF, PICOL_PT_XPND};
enum {PICOL_PTR_NONE, PICOL_PTR_CHAN, PICOL_PTR_ARRAY, PICOL_PTR_INTERP};
/* -------------------------------------------------------------------- types */
typedef struct picolParser {
const char* text;
const char* pos; /* current text position */
size_t len; /* remaining length */
const char* start; /* token start */
const char* end; /* token end */
int type; /* token type, PICOL_PT_... */
int insidequote; /* True if inside " " */
int expand; /* true after {*} */
} picolParser;
typedef struct picolVar {
struct picolVar* next;
char* name;
char* val;
} picolVar;
struct picolInterp; /* forward declaration */
typedef picolResult (*picolFunc)(
struct picolInterp *interp,
int argc,
const char *argv[],
void *pd
);
typedef struct picolCmd {
struct picolCmd* next;
char* name;
picolFunc func;
unsigned char isproc; /* is this command a procedure? */
/* Picol manages private data for procs. Managing private data for native
commands is left to the user. */
void* privdata;
} picolCmd;
typedef struct picolCallFrame {
picolVar* vars;
char* command;
struct picolCallFrame* parent; /* parent is NULL at top level */
} picolCallFrame;
typedef struct picolProc {
int rc; /* reference count */
char* args;
char* body;
} picolProc;
typedef struct picolPtr {
struct picolPtr* next;
void* ptr;
int type;
} picolPtr;
typedef struct picolInterp {
int level; /* level of scope nesting */
int maxlevel;
picolCallFrame* callframe;
picolCmd* commands;
char* current; /* currently executed command */
char* result;
int debug; /* 1 to display each command, 0 not to */
picolPtr* validptrs;
} picolInterp;
#define PICOL_ARR_BUCKETS 32
typedef struct picolArray {
picolVar* table[PICOL_ARR_BUCKETS];
int size;
} picolArray;
/* Ease of use macros. */
#define picolEval(_i, _t) picolEval2(_i, _t, 1)
#define picolGetGlobalVar(_i, _n) picolGetVar2(_i, _n, 1)
#define picolGetVar(_i, _n) picolGetVar2(_i, _n, 0)
#define picolSetBoolResult(_i, x) picolSetFmtResult(_i, "%d", !!x)
#define picolSetGlobalVar(_i, _n, _v) picolSetVar2(_i, _n, _v, 1)
#define picolSetIntResult(_i, x) picolSetFmtResult(_i, "%d", x)
#define picolSetVar(_i, _n, _v) picolSetVar2(_i, _n, _v, 0)
#define picolSubst(_i, _t) picolEval2(_i, _t, 0)
/* prototypes */
picolResult picolList(char* buf, size_t buf_size, int argc, const char** argv);
int picolNeedsBraces(const char* str);
const char* picolListHead(const char* start, char* target, size_t target_size);
const char* picolStrFirstTrailing(const char* str, char chr);
picolResult picolStrRev(char* dest, size_t dest_size, const char* str);
picolResult picolToLower(char* dest, size_t dest_size, const char* str);
picolResult picolToUpper(char* dest, size_t dest_size, const char* str);
PICOL_COMMAND(abs);
#if PICOL_TCL_PLATFORM_PLATFORM == PICOL_TCL_PLATFORM_UNIX || \
PICOL_TCL_PLATFORM_PLATFORM == PICOL_TCL_PLATFORM_WINDOWS
PICOL_COMMAND(after);
#endif
PICOL_COMMAND(append);
PICOL_COMMAND(apply);
PICOL_COMMAND(bitwise_not);
PICOL_COMMAND(break);
PICOL_COMMAND(catch);
PICOL_COMMAND(clock);
PICOL_COMMAND(concat);
PICOL_COMMAND(continue);
PICOL_COMMAND(debug);
PICOL_COMMAND(error);
PICOL_COMMAND(eval);
PICOL_COMMAND(expr);
/* [file delete|exists|isdirectory|isfile|size] are enabled with
PICOL_FEATURE_IO. */
PICOL_COMMAND(file);
PICOL_COMMAND(for);
PICOL_COMMAND(foreach);
PICOL_COMMAND(format);
PICOL_COMMAND(global);
PICOL_COMMAND(if);
PICOL_COMMAND(incr);
PICOL_COMMAND(info);
PICOL_COMMAND(join);
PICOL_COMMAND(lappend);
PICOL_COMMAND(lassign);
PICOL_COMMAND(lindex);
PICOL_COMMAND(linsert);
PICOL_COMMAND(list);
PICOL_COMMAND(llength);
PICOL_COMMAND(lmap);
PICOL_COMMAND(lrange);
PICOL_COMMAND(lrepeat);
PICOL_COMMAND(lreplace);
PICOL_COMMAND(lreverse);
PICOL_COMMAND(lsearch);
PICOL_COMMAND(lset);
PICOL_COMMAND(lsort);
PICOL_COMMAND(max);
PICOL_COMMAND(min);
PICOL_COMMAND(not);
PICOL_COMMAND(pid);
PICOL_COMMAND(proc);
PICOL_COMMAND(rand);
PICOL_COMMAND(rename);
PICOL_COMMAND(return);
PICOL_COMMAND(scan);
PICOL_COMMAND(set);
PICOL_COMMAND(split);
PICOL_COMMAND(string);
PICOL_COMMAND(subst);
PICOL_COMMAND(switch);
PICOL_COMMAND(time);
PICOL_COMMAND(trace);
PICOL_COMMAND(try);
PICOL_COMMAND(unset);
PICOL_COMMAND(uplevel);
PICOL_COMMAND(variable);
PICOL_COMMAND(while);
#if PICOL_FEATURE_ARRAYS
PICOL_COMMAND(array);
picolArray* picolArrCreate(picolInterp *interp, const char *name);
picolResult picolArrDestroy(picolArray* ap);
picolResult picolArrDestroyByName(picolInterp* interp, const char* name);
picolResult picolArrGetAll(picolArray *ap, const char* pat, char* buf,
size_t buf_size, int mode);
picolVar* picolArrGetKey(picolArray* ap, const char* key);
picolArray* picolArrFindByName(picolInterp* interp, const char* name,
int create, char* key_dest,
size_t key_dest_size);
picolResult picolArrUnset(picolArray* ap, const char* key);
picolResult picolArrUnsetByName(picolInterp *interp, const char *name);
picolVar* picolArrSet(picolArray* ap, const char* key, const char* value);
picolVar* picolArrSetByName(picolInterp *interp, const char *name,
const char *value);
char* picolArrStat(picolArray *ap, char* buf, size_t buf_size);
int picolHash(const char* key, int modulo);
#endif
#if PICOL_FEATURE_GLOB
PICOL_COMMAND(glob);
#endif
#if PICOL_FEATURE_INTERP
PICOL_COMMAND(interp);
#endif
#if PICOL_FEATURE_IO
PICOL_COMMAND(cd);
PICOL_COMMAND(exec);
PICOL_COMMAND(exit);
PICOL_COMMAND(gets);
PICOL_COMMAND(open);
PICOL_COMMAND(pwd);
PICOL_COMMAND(rawexec);
PICOL_COMMAND(read);
PICOL_COMMAND(source);
picolResult picol_FileUtil(picolInterp *interp, int argc,
const char *argv[], void *pd);
#endif
#if PICOL_FEATURE_PUTS
PICOL_COMMAND(puts);
#endif
picolBool picolValidPtr(picolInterp *interp, int type, void* ptr);
picolResult picolValidPtrAdd(picolInterp *interp, int type, void* ptr);
picolResult picolValidPtrRemove(picolInterp *interp, void* ptr);
picolResult picolCallProc(picolInterp *interp, int argc, const char **argv,
void *pd);
picolBool picolAppend(char *dst, int dstSize, const char *src);
picolBool picolLappend(char *dst, int dstSize, const char *src);
picolResult picolConcat(char* buf, size_t buf_size, int argc,
const char** argv);
picolResult picolCondition(picolInterp *interp, const char* str);
picolResult picolErr(picolInterp *interp, const char* str);
/* Backwards compatibility. */
#define picolErr1 picolErrFmt
picolResult picolErrFmt(picolInterp *interp, const char* format,
const char* arg);
picolResult picolEval2(picolInterp *interp, const char *script, int mode);
size_t picolExpandLC(char* dest, size_t num, const char* source);
picolResult picol_EqNe(picolInterp* interp, int argc, const char** argv,
void* pd);
picolResult picolGetToken(picolInterp *interp, picolParser *p);
picolResult picol_InNi(picolInterp *interp, int argc, const char **argv,
void *pd);
#if PICOL_FEATURE_IO
picolBool picolIsDirectory(const char* path);
#endif
int picolIsInt(const char* str);
picolResult picolLmap(picolInterp* interp, const char* vars, const char* list,
const char* body, int accumulate);
picolResult picol_Lsort(picolInterp *interp, int argc, const char **argv,
void *pd);
int picolMatch(const char* pat, const char* str);
picolResult picol_Math(picolInterp *interp, int argc, const char** argv,
void *pd);
picolResult picolParseBrace(picolParser *p);
picolResult picolParseCmd(picolParser *p);
picolResult picolParseComment(picolParser *p);
picolResult picolParseEOL(picolParser *p);
picolResult picolParseSep(picolParser *p);
picolResult picolParseString(picolParser *p);
picolResult picolParseVar(picolParser *p);
int picolQsortCompInt(const void* a, const void *b);
int picolQsortCompStr(const void* a, const void *b);
int picolQsortCompStrDecr(const void* a, const void *b);
picolResult picolQuoteForShell(char* dest, size_t dest_size, int argc,
const char** argv);
picolResult picolRegisterCmd(picolInterp *interp, const char *name,
picolFunc f, void *pd);
picolResult picolReplace(char* str, size_t str_size, char* from, char* to,
int nocase);
int picolScanInt(const char* str, int base);
picolResult picolSetFmtResult(picolInterp* interp, const char* fmt, int result);
picolResult picolSetIntVar(picolInterp *interp, const char *name, int value);
picolResult picolSetResult(picolInterp *interp, const char *s);
picolResult picolSetVar2(picolInterp *interp, const char *name, const char *val,
int global);
picolResult picolSource(picolInterp *interp, const char *filename);
int picolStrCompare(const char* a, const char* b, size_t len, int nocase);
picolResult picolUnsetVar(picolInterp* interp, const char* name);
picolBool picolWildEq(const char* pat, const char* str, int n);
picolCmd *picolGetCmd(picolInterp *interp, const char *name);
picolInterp* picolCreateInterp(void);
picolInterp* picolCreateInterp2(int register_core_cmds, int randomize);
picolVar *picolGetVar2(picolInterp *interp, const char *name, int global);
void picolDropCallFrame(picolInterp *interp);
void picolEscape(char *str, size_t str_size);
void picolFreeCmd(picolCmd *cmd);
void picolFreeInterp(picolInterp *interp);
void picolInitInterp(picolInterp *interp);
void picolInitParser(picolParser *p, const char *text);
void* picolScanPtr(const char* str);
void picolRegisterCoreCmds(picolInterp *interp);
picolResult picolRenameCmd(picolInterp *interp, const char *from,
const char *to);
#endif /* PICOL_H */
/* -------------------------------------------------------------------------- */
#ifdef PICOL_IMPLEMENTATION
/* --------------------------------------------------------- parser functions */
void picolInitParser(picolParser* p, const char* text) {
p->text = p->pos = text;
p->len = strlen(text);
p->start = 0;
p->end = 0;
p->insidequote = 0;
p->type = PICOL_PT_EOL;
p->expand = 0;
}
picolResult picolParseSep(picolParser* p) {
p->start = p->pos;
while (isspace(*p->pos) ||
(*p->pos == '\\' && *(p->pos + 1) == '\n')) {
p->pos++;
p->len--;
}
PICOL_RETURN_PARSED(PICOL_PT_SEP);
}
picolResult picolParseEOL(picolParser* p) {
p->start = p->pos;
while (isspace(*p->pos) || *p->pos == ';') {
p->pos++;
p->len--;
}
PICOL_RETURN_PARSED(PICOL_PT_EOL);
}
picolResult picolParseCmd(picolParser* p) {
int level = 1, blevel = 0;
p->start = ++p->pos;
p->len--;
while (p->len) {
if (!*p->pos) {
break;
} if (*p->pos == '[' && blevel == 0) {
level++;
} else if (*p->pos == ']' && blevel == 0) {
if (!--level) {
break;
}
} else if (*p->pos == '\\') {
p->pos++;
p->len--;
} else if (*p->pos == '{') {
blevel++;
} else if (*p->pos == '}') {
if (blevel != 0) {
blevel--;
}
}
if (p->len > 0) {
p->pos++;
p->len--;
}
}
p->end = p->pos - 1;
p->type = PICOL_PT_CMD;
if (*p->pos == ']') {
p->pos++;
p->len--;
}
return (level == 0 && blevel == 0) ? PICOL_OK : PICOL_ERR;
}
picolResult picolParseBrace(picolParser* p) {
int level = 1;
p->start = ++p->pos;
p->len--;
while (1) {
if (p->len >= 2 && *p->pos == '\\') {
p->pos++;
p->len--;
} else if (p->len == 0 || *p->pos == '}') {
level--;
if (level == 0 || p->len == 0) {
p->end = p->pos - 1;
if (p->len) {
p->pos++; /* skip the final close-brace */
p->len--;
}
p->type = PICOL_PT_STR;
return PICOL_OK;
}
} else if (*p->pos == '{') {
level++;
}
p->pos++;
p->len--;
}
}
picolResult picolParseString(picolParser* p) {
int newword = (p->type == PICOL_PT_SEP || p->type == PICOL_PT_EOL ||
p->type == PICOL_PT_STR);
if (p->len >= 3 && !strncmp(p->pos, "{*}", 3)) {
p->expand = 1;
p->pos += 3;
p->len -= 3; /* skip the {*} expand indicator */
}
if (newword && *p->pos == '{') {
return picolParseBrace(p);
} else if (newword && *p->pos == '"') {
p->insidequote = 1;
p->pos++;
p->len--;
}
for (p->start = p->pos; 1; p->pos++, p->len--) {
if (p->len == 0) {
PICOL_RETURN_PARSED(PICOL_PT_ESC);
}
switch(*p->pos) {
case '\\':
if (p->len >= 2) {
p->pos++;
p->len--;
};
break;
case '$':
case '[':
PICOL_RETURN_PARSED(PICOL_PT_ESC);
case ' ':
case '\t':
case '\n':
case '\r':
case ';':
if (!p->insidequote) {
PICOL_RETURN_PARSED(PICOL_PT_ESC);
}
break;
case '"':
if (p->insidequote) {
p->end = p->pos-1;
p->type = PICOL_PT_ESC;
p->pos++;
p->len--;
p->insidequote = 0;
return PICOL_OK;
}
break;
}
}
}
picolResult picolParseVar(picolParser* p) {
int parened = 0;
p->start = ++p->pos;
p->len--; /* skip the '$' */
if (*p->pos == '{') {
picolParseBrace(p);
p->type = PICOL_PT_VAR;
return PICOL_OK;
}
if (PICOL_COLONED(p->pos)) {
p->pos += 2;
p->len -= 2;
}
while (
isalnum(*p->pos) ||
*p->pos == '_' ||
*p->pos == '(' ||
*p->pos == ')'
) {
if (*p->pos=='(') {
parened = 1;
}
p->pos++;
p->len--;
}
if (!parened && *(p->pos-1) == ')') {
p->pos--;
p->len++;
}
if (p->start == p->pos) {
/* It's just the single-char string "$". */
picolParseString(p);
p->start--;
p->len++; /* back to the '$' character */
p->type = PICOL_PT_STR;
return PICOL_OK;
} else {
PICOL_RETURN_PARSED(PICOL_PT_VAR);
}
}
picolResult picolParseComment(picolParser* p) {
while (p->len && *p->pos != '\n') {
if (*p->pos=='\\' && *(p->pos+1)=='\n') {
p->pos++;
p->len--;
}
p->pos++;
p->len--;
}
return PICOL_OK;
} /* ----------------------------------- General functions: variables, errors */
picolBool picolAppend(char *dst, int dstSize, const char *src) {
if ((strlen(dst) + strlen(src)) > (unsigned long)dstSize - 1) {
return PICOL_FALSE;
}
strcat(dst, src);
return PICOL_TRUE;
}
picolBool picolLappend(char *dst, int dstSize, const char *src) {
int needbraces = picolNeedsBraces(src);
int notEmptyDestination = *dst != '\0' ? 1 : 0;
int requiredSize = needbraces * 2 + notEmptyDestination + strlen(src);
if ((strlen(dst) + requiredSize) > (unsigned long)dstSize - 1) {
return PICOL_FALSE;
}
if (notEmptyDestination) {
strcat(dst, " ");
}
if (needbraces) {
strcat(dst, "{");
}
strcat(dst, src);
if (needbraces) {
strcat(dst, "}");
}
return PICOL_TRUE;
}
picolResult picolSetResult(picolInterp* interp, const char* s) {
PICOL_FREE(interp->result);
interp->result = strdup(s);
return PICOL_OK;
}
picolResult picolSetFmtResult(
picolInterp* interp,
const char* fmt,
int result
) {
char buf[32];
PICOL_SNPRINTF(buf, sizeof(buf), fmt, result);
return picolSetResult(interp, buf);
}
#define PICOL_APPEND_BREAK_PICOLERR(src) \
{ \
size_t src_len = strlen(src); \
if ((len + added_len + src_len) >= sizeof(buf)) { \
too_long = 1; break; \
} \
strcat(buf, src); \
added_len += src_len; \
}
picolResult picolErr(picolInterp* interp, const char* str) {
char buf[PICOL_MAX_STR] = "";
int too_long = 0;
size_t len = 0, added_len = 0;
do {
picolCallFrame* cf;
PICOL_APPEND_BREAK_PICOLERR(str);
len += added_len; added_len = 0;
if (interp->current != NULL) {
PICOL_APPEND_BREAK_PICOLERR("\n while executing\n\"");
PICOL_APPEND_BREAK_PICOLERR(interp->current);
PICOL_APPEND_BREAK_PICOLERR("\"");
}
for (cf = interp->callframe;
cf->command != NULL && cf->parent != NULL;
cf = cf->parent) {
len += added_len; added_len = 0;
PICOL_APPEND_BREAK_PICOLERR("\n invoked from within\n\"");
PICOL_APPEND_BREAK_PICOLERR(cf->command);
PICOL_APPEND_BREAK_PICOLERR("\"");
}
} while (0);
if (too_long) {
/* Truncate the error message at the last complete chunk. */
buf[len] = '\0';
do {
PICOL_APPEND_BREAK_PICOLERR(len > 0 ? "\n..." : "...");
} while (0);
}
/* Not exactly the same as in Tcl. */
picolSetVar2(interp, "::errorInfo", buf, 1);
picolSetResult(interp, str);
return PICOL_ERR;
}
#undef PICOL_APPEND_BREAK_PICOLERR
picolResult picolErrFmt(
picolInterp* interp,
const char* format,
const char* arg
) {
/* The format line must contain exactly one "%s" specifier. */
char buf[PICOL_MAX_STR], truncated[PICOL_MAX_STR];
size_t max_len;
strncpy(truncated, arg, PICOL_MAX_STR);
/* The two chars are for the "%s". */
max_len = PICOL_MAX_STR - 1 - strlen(format) + 2;
if (strlen(truncated) > max_len) {
truncated[max_len - 3] = '.';
truncated[max_len - 2] = '.';
truncated[max_len - 1] = '.';
truncated[max_len] = '\0';
}
PICOL_SNPRINTF(buf, sizeof(buf), format, truncated);
return picolErr(interp, buf);
}
picolVar* picolGetVar2(picolInterp* interp, const char* name, int global) {
picolVar* v = interp->callframe->vars;
int coloned = PICOL_COLONED(name);
if (coloned || global) {
picolCallFrame* c = interp->callframe;
while (c->parent) {
c = c->parent;
}
v = c->vars;
if (coloned) {
name += 2; /* skip the "::" */
}
}
#if PICOL_FEATURE_ARRAYS
{
char buf[PICOL_MAX_STR], buf2[PICOL_MAX_STR], *cp, *cp2;
/* Array element syntax? */
if ((cp = strchr(name, '('))) {
picolArray* ap;
int found = 0;
strncpy(buf, name, cp - name);
buf[cp - name] = '\0';
for (; v != NULL; v = v->next) {
if (PICOL_EQ(v->name, buf)) {
found = 1;
break;
}
}
if (!found) {
return NULL;
}
ap = picolScanPtr(v->val);
if (ap == NULL ||
!picolValidPtr(interp, PICOL_PTR_ARRAY, (void*)ap)) {
return NULL;
}
/* Copy the key that starts after the opening paren. */
strncpy(buf2, cp + 1, sizeof(buf2));
cp = strchr(buf2, ')');
if (cp == NULL) {
return NULL;
}
/* Overwrite the closing paren. */
*cp = '\0';
v = picolArrGetKey(ap, buf2);
if (v == NULL) {
if ((coloned || interp->callframe->parent == NULL)
&& PICOL_EQ(buf, "env")) {
cp2 = getenv(buf2);
if (cp2 == NULL) {
return NULL;
}
strcpy(buf, "::env(");
strncat(buf, buf2, sizeof(buf) - strlen(buf2));
strncat(buf, ")", sizeof(buf) - 1);
return picolArrSetByName(interp, buf, cp2);
} else {
return NULL;
}
}
return v;
}
}
#endif /* PICOL_FEATURE_ARRAYS */
for (; v != NULL; v = v->next) {
if (PICOL_EQ(v->name, name)) {
return v;
}
}
return NULL;
}
picolResult picolSetVar2(
picolInterp* interp,
const char* name,
const char* val,
int global
) {
picolVar* v = picolGetVar(interp, name);
picolCallFrame* c = interp->callframe, *localc = c;
int coloned = PICOL_COLONED(name);
if (global || coloned) v = picolGetGlobalVar(interp, name);
if (v != NULL) {
/* existing variable case */
if (v->val != NULL) {
/* local value */
PICOL_FREE(v->val);
} else {
return picolSetGlobalVar(interp, name, val);
}
} else {
/* nonexistent variable */
#if PICOL_FEATURE_ARRAYS
if (strchr(name, '(')) {
if (picolArrSetByName(interp, name, val) == NULL) {
return picolErrFmt(
interp,
"can't set \"%s\": variable isn't array",
name
);
} else {
return PICOL_OK;
}
}
#endif
if (global || coloned) {
if (coloned) name += 2;
while (c->parent) {
c = c->parent;
}
}
v = PICOL_MALLOC(sizeof(*v));
v->name = strdup(name);
v->next = c->vars;
c->vars = v;
interp->callframe = localc;
}
v->val = (val == NULL ? NULL : strdup(val));
return PICOL_OK;
}
picolResult picolSetIntVar(picolInterp* interp, const char* name, int value) {
char buf[32];
PICOL_SNPRINTF(buf, sizeof(buf), "%d", value);
return picolSetVar(interp, name, buf);
}
picolResult picolGetToken(picolInterp* interp, picolParser* p) {
int rc;
while (1) {
if (!p->len) {
if (p->type != PICOL_PT_EOL && p->type != PICOL_PT_EOF) {
p->type = PICOL_PT_EOL;
} else {
p->type = PICOL_PT_EOF;
}
return PICOL_OK;
}