-
Notifications
You must be signed in to change notification settings - Fork 7
/
lang-c-header.c
1363 lines (1226 loc) · 35.3 KB
/
lang-c-header.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
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
/* $Id$ */
/*
* Copyright (c) 2017, 2020 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and 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.
*/
#include "config.h"
#if HAVE_SYS_QUEUE
# include <sys/queue.h>
#endif
#include <ctype.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ort.h"
#include "ort-lang-c.h"
#include "ort-version.h"
#include "lang.h"
#include "lang-c.h"
static const char *const optypes[OPTYPE__MAX] = {
"equals", /* OPTYPE_EQUAL */
"greater-than equals", /* OPTYPE_GE */
"greater-than", /* OPTYPE_GT */
"less-than equals", /* OPTYPE_LE */
"less-than", /* OPTYPE_LT */
"does not equal", /* OPTYPE_NEQUAL */
"\"like\"", /* OPTYPE_LIKE */
"logical and", /* OPTYPE_AND */
"logical or", /* OPTYPE_OR */
"string equals", /* OPTYPE_STREQ */
"string does not equal", /* OPTYPE_STRNEQ */
/* Unary types... */
"is null", /* OPTYPE_ISNULL */
"is not null" /* OPTYPE_NOTNULL */
};
/*
* Emit all characters as uppercase.
* Return zero on failure, non-zero on success.
*/
static int
gen_upper(FILE *f, const char *cp)
{
for ( ; *cp != '\0'; cp++)
if (fputc(toupper((unsigned char)*cp), f) == EOF)
return 0;
return 1;
}
/*
* Generate the structure field and documentation for a given field.
* Return zero on failure, non-zero on success.
*/
static int
gen_field(FILE *f, const struct field *fd)
{
int c = 0;
const struct field *rfd;
if (!gen_comment(f, 1, COMMENT_C, fd->doc))
return 0;
switch (fd->type) {
case FTYPE_STRUCT:
c = fprintf(f, "\tstruct %s %s;\n",
fd->ref->target->parent->name, fd->name);
break;
case FTYPE_REAL:
c = fprintf(f, "\tdouble\t %s;\n", fd->name);
break;
case FTYPE_BLOB:
c = fprintf(f, "\tvoid\t*%s;\n\tsize_t\t %s_sz;\n",
fd->name, fd->name);
break;
case FTYPE_BIT:
case FTYPE_BITFIELD:
case FTYPE_DATE:
case FTYPE_EPOCH:
case FTYPE_INT:
rfd = fd->ref != NULL ? fd->ref->target : fd;
c = fprintf(f, "\t%s_%s\t %s;\n",
rfd->parent->name, rfd->name, fd->name);
break;
case FTYPE_TEXT:
case FTYPE_EMAIL:
case FTYPE_PASSWORD:
c = fprintf(f, "\tchar\t*%s;\n", fd->name);
break;
case FTYPE_ENUM:
c = fprintf(f, "\tenum %s %s;\n",
fd->enm->name, fd->name);
break;
default:
break;
}
return c >= 0;
}
/*
* Generate user-defined bit-field enumration.
* These are either the field index (BITI) or the mask (BITF).
* Return zero on failure, non-zero on success.
*/
static int
gen_bitfield(FILE *f, const struct bitf *b)
{
const struct bitidx *bi;
int64_t maxv = -INT64_MAX;
enum cmtt c = COMMENT_C;
if (b->doc != NULL) {
if (!gen_comment(f, 0, COMMENT_C_FRAG_OPEN, b->doc))
return 0;
c = COMMENT_C_FRAG_CLOSE;
}
if (!gen_comment(f, 0, c,
"This defines the bit indices for this bit-field.\n"
"The BITI fields are the bit indices (0--63) and "
"the BITF fields are the masked integer values."))
return 0;
if (fprintf(f, "enum\t%s {\n", b->name) < 0)
return 0;
TAILQ_FOREACH(bi, &b->bq, entries) {
if (!gen_comment(f, 1, COMMENT_C, bi->doc))
return 0;
if (fputs("\tBITI_", f) == EOF)
return 0;
if (!gen_upper(f, b->name))
return 0;
if (fprintf(f, "_%s = %" PRId64 ",\n\tBITF_",
bi->name, bi->value) < 0)
return 0;
if (!gen_upper(f, b->name))
return 0;
if (fprintf(f, "_%s = UINT64_C(1) << %" PRId64 ",\n",
bi->name, bi->value) < 0)
return 0;
if (bi->value > maxv)
maxv = bi->value;
}
if (fputs("\tBITI_", f) == EOF)
return 0;
if (!gen_upper(f, b->name))
return 0;
if (fprintf(f, "__MAX = %" PRId64 ",\n};\n\n", maxv + 1) < 0)
return 0;
return 1;
}
/*
* Generate user-defined enumeration.
* Return zero on failure, non-zero on success.
*/
static int
gen_enum(FILE *f, const struct enm *e)
{
const struct eitem *ei;
if (!gen_comment(f, 0, COMMENT_C, e->doc))
return 0;
if (fprintf(f, "enum\t%s {\n", e->name) < 0)
return 0;
TAILQ_FOREACH(ei, &e->eq, entries) {
if (!gen_comment(f, 1, COMMENT_C, ei->doc))
return 0;
if (fputc('\t', f) == EOF)
return 0;
if (!gen_upper(f, e->name))
return 0;
if (fprintf(f, "_%s = %" PRId64 "%s\n", ei->name,
ei->value, TAILQ_NEXT(ei, entries) ? "," : "") < 0)
return 0;
}
return fputs("};\n\n", f) != EOF;
}
/*
* Generate setters and getters for variables having safe typing,
* although we define "dummy" operations for non-safe typing to make
* migration easier. References don't get the full treatment, as their
* setters and getters will be defined for the referenced variable.
* Return TRUE on success, FALSE on failure.
*/
static int
gen_type_funcs(FILE *f, const struct ort_lang_c *args,
const struct config *cfg, const struct strct *s)
{
const struct field *fd, *rfd;
const char *type;
if (args->flags & ORT_LANG_C_SAFE_TYPES) {
TAILQ_FOREACH(fd, &s->fq, entries) {
if (fd->type != FTYPE_BIT &&
fd->type != FTYPE_BITFIELD &&
fd->type != FTYPE_DATE &&
fd->type != FTYPE_EPOCH &&
fd->type != FTYPE_INT)
continue;
if (fputc('\n', f) == EOF)
return 0;
/*
* If we're a reference (fd->ref != NULL), then
* don't print the type's setters and getters,
* as they're defined on the referenced type and
* not the reference.
*/
rfd = fd->ref != NULL ? fd->ref->target : fd;
if (fd->ref == NULL &&
fprintf(f,
"#define ORT_%s_%s(_src) "
"(%s_%s){ .val = (_src) }\n",
s->name, fd->name,
s->name, fd->name) < 0)
return 0;
if (fd->type == FTYPE_DATE ||
fd->type == FTYPE_EPOCH)
type = "time_t";
else
type = "int64_t";
if (fprintf(f,
"static inline %s ORT_GET_%s_%s"
"(const struct %s *dst) "
"{ return dst->%s.val; }\n"
"static inline %s ORT_GETV_%s_%s"
"(const %s_%s dst) "
"{ return dst.val; }\n"
"static inline void ORT_SET_%s_%s"
"(struct %s *dst, %s src) "
"{ dst->%s.val = src; }\n"
"static inline void ORT_SETV_%s_%s"
"(%s_%s *dst, %s src) "
"{ dst->val = src; }\n",
type, s->name, fd->name, s->name, fd->name,
type, s->name, fd->name,
rfd->parent->name, rfd->name, s->name,
fd->name, s->name, type, fd->name, s->name,
fd->name, rfd->parent->name, rfd->name,
type) < 0)
return 0;
}
} else
TAILQ_FOREACH(fd, &s->fq, entries) {
if (fd->type != FTYPE_BIT &&
fd->type != FTYPE_BITFIELD &&
fd->type != FTYPE_DATE &&
fd->type != FTYPE_EPOCH &&
fd->type != FTYPE_INT)
continue;
if (fputc('\n', f) == EOF)
return 0;
/* Like above, don't expand references. */
if (fd->ref == NULL &&
fprintf(f,
"#define ORT_%s_%s(_src) (_src)\n",
s->name, fd->name) < 0)
return 0;
if (fd->type == FTYPE_DATE ||
fd->type == FTYPE_EPOCH)
type = "time_t";
else
type = "int64_t";
if (fprintf(f,
"static inline %s ORT_GET_%s_%s"
"(const struct %s *dst) "
"{ return dst->%s; }\n"
"#define ORT_GETV_%s_%s(_dst) (_dst)\n"
"static inline void ORT_SET_%s_%s"
"(struct %s *dst, %s src) "
"{ dst->%s = src; }\n"
"#define ORT_SETV_%s_%s(_dst, _src) "
"*(_dst) = (_src)\n",
type, s->name, fd->name, s->name,
fd->name, s->name, fd->name, s->name,
fd->name, s->name, type, fd->name,
s->name, fd->name) < 0)
return 0;
}
return 1;
}
/*
* Emit our typing first. If we don't use safe types, emit typedefs to
* the native type for all types we care about; otherwise, emit the
* struct wrappers. Also emit the macros for setting, getting, and
* static init. Return TRUE on success, FALSE on failure.
*/
static int
gen_types(FILE *f, const struct ort_lang_c *args,
const struct config *cfg, const struct strct *s)
{
const struct field *fd;
const char *type;
if (args->flags & ORT_LANG_C_SAFE_TYPES) {
TAILQ_FOREACH(fd, &s->fq, entries)
switch (fd->type) {
case FTYPE_BIT:
case FTYPE_BITFIELD:
case FTYPE_DATE:
case FTYPE_EPOCH:
case FTYPE_INT:
if (fd->type == FTYPE_DATE ||
fd->type == FTYPE_EPOCH)
type = "time_t";
else
type = "int64_t";
if (fd->ref == NULL &&
fprintf(f,
"typedef struct %s_%s "
"{ %s val; } %s_%s;\n",
s->name, fd->name, type,
s->name, fd->name) < 0)
return 0;
break;
default:
break;
}
} else {
TAILQ_FOREACH(fd, &s->fq, entries)
switch (fd->type) {
case FTYPE_BIT:
case FTYPE_BITFIELD:
case FTYPE_DATE:
case FTYPE_EPOCH:
case FTYPE_INT:
if (fd->type == FTYPE_DATE ||
fd->type == FTYPE_EPOCH)
type = "time_t";
else
type = "int64_t";
if (fd->ref == NULL &&
fprintf(f,
"typedef %s %s_%s;\n",
type, s->name, fd->name) < 0)
return 0;
break;
default:
break;
}
}
return 1;
}
/*
* Generate the C API for a given structure.
* This generates the TAILQ_ENTRY listing if the structure has any
* listings declared on it.
* Return zero on failure, non-zero on success.
*/
static int
gen_struct(FILE *f, const struct ort_lang_c *args,
const struct config *cfg, const struct strct *s)
{
const struct field *fd;
if (fputc('\n', f) == EOF)
return 0;
if (!gen_comment(f, 0, COMMENT_C, s->doc))
return 0;
if (fprintf(f, "struct\t%s {\n", s->name) < 0)
return 0;
TAILQ_FOREACH(fd, &s->fq, entries)
if (!gen_field(f, fd))
return 0;
TAILQ_FOREACH(fd, &s->fq, entries) {
if (fd->type == FTYPE_STRUCT &&
(fd->ref->source->flags & FIELD_NULL)) {
if (!gen_commentv(f, 1, COMMENT_C,
"Non-zero if \"%s\" has been set "
"from \"%s\".", fd->name,
fd->ref->source->name))
return 0;
if (fprintf(f, "\tint has_%s;\n", fd->name) < 0)
return 0;
continue;
} else if (!(fd->flags & FIELD_NULL))
continue;
if (!gen_commentv(f, 1, COMMENT_C,
"Non-zero if \"%s\" field is null/unset.",
fd->name))
return 0;
if (fprintf(f, "\tint has_%s;\n", fd->name) < 0)
return 0;
}
if ((s->flags & STRCT_HAS_QUEUE) &&
fprintf(f, "\tTAILQ_ENTRY(%s) _entries;\n", s->name) < 0)
return 0;
if (!TAILQ_EMPTY(&cfg->rq)) {
if (!gen_comment(f, 1, COMMENT_C,
"Private data used for role analysis."))
return 0;
if (fputs("\tstruct ort_store *priv_store;\n", f) == EOF)
return 0;
}
if (fputs("};\n", f) == EOF)
return 0;
if (s->flags & STRCT_HAS_QUEUE) {
if (fputc('\n', f) == EOF)
return 0;
if (!gen_commentv(f, 0, COMMENT_C,
"Queue of %s for listings.", s->name))
return 0;
if (fprintf(f, "TAILQ_HEAD(%s_q, %s);\n",
s->name, s->name) < 0)
return 0;
}
if (s->flags & STRCT_HAS_ITERATOR) {
if (fputc('\n', f) == EOF)
return 0;
if (!gen_commentv(f, 0, COMMENT_C,
"Callback of %s for iteration.\n"
"The arg parameter is the opaque pointer "
"passed into the iterate function.",
s->name))
return 0;
if (fprintf(f, "typedef void (*%s_cb)(const struct %s "
"*v, void *arg);\n", s->name, s->name) < 0)
return 0;
}
return 1;
}
/*
* Generate update/delete functions for a structure.
* Returns zero on failure, non-zero on success.
*/
static int
gen_update(FILE *f, const struct config *cfg, const struct update *up)
{
const struct uref *ref;
enum cmtt ct = COMMENT_C_FRAG_OPEN;
size_t pos = 1;
if (fputc('\n', f) == EOF)
return 0;
if (up->doc != NULL) {
if (!gen_comment(f, 0, COMMENT_C_FRAG_OPEN, up->doc))
return 0;
ct = COMMENT_C_FRAG;
}
/* Only update functions have this part. */
if (up->type == UP_MODIFY) {
if (!gen_commentv(f, 0, ct,
"Update fields in struct %s.\n"
"Updated fields:", up->parent->name))
return 0;
TAILQ_FOREACH(ref, &up->mrq, entries)
if (ref->field->type == FTYPE_PASSWORD) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"\tv%zu: %s (password)",
pos++, ref->field->name))
return 0;
} else {
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"\tv%zu: %s",
pos++, ref->field->name))
return 0;
}
} else {
if (!gen_commentv(f, 0, ct,
"Delete fields in struct %s.\n",
up->parent->name))
return 0;
}
if (!gen_comment(f, 0, COMMENT_C_FRAG, "Constraint fields:"))
return 0;
TAILQ_FOREACH(ref, &up->crq, entries)
if (ref->op == OPTYPE_NOTNULL) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"\t%s (not an argument: "
"checked not null)", ref->field->name))
return 0;
} else if (ref->op == OPTYPE_ISNULL) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"\t%s (not an argument: "
"checked null)", ref->field->name))
return 0;
} else {
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"\tv%zu: %s (%s)", pos++,
ref->field->name, optypes[ref->op]))
return 0;
}
if (!gen_comment(f, 0, COMMENT_C_FRAG_CLOSE,
"Returns zero on constraint violation, "
"non-zero on success."))
return 0;
return gen_func_db_update(f, up, 1);
}
/*
* Generate query (list, iterate, etc.) function declaration.
* Returns zero on failure, non-zero on success.
*/
static int
gen_search(FILE *f, const struct config *cfg, const struct search *s)
{
const struct sent *sent;
const struct strct *rc;
size_t pos = 1;
if (fputc('\n', f) == EOF)
return 0;
rc = s->dst != NULL ? s->dst->strct : s->parent;
if (s->doc != NULL) {
if (!gen_comment(f, 0, COMMENT_C_FRAG_OPEN, s->doc))
return 0;
} else if (s->type == STYPE_SEARCH) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG_OPEN,
"Search for a specific %s.", rc->name))
return 0;
} else if (s->type == STYPE_LIST) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG_OPEN,
"Search for a set of %s.", rc->name))
return 0;
} else if (s->type == STYPE_COUNT) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG_OPEN,
"Count results of a search in %s.", rc->name))
return 0;
} else {
if (!gen_commentv(f, 0, COMMENT_C_FRAG_OPEN,
"Iterate over results in %s.", rc->name))
return 0;
}
if (s->dst != NULL) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"This %s distinct query results.",
s->type == STYPE_ITERATE ?
"iterates over" :
s->type == STYPE_COUNT ?
"counts" : "returns"))
return 0;
if (s->dst->strct != s->parent)
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"The results are limited "
"to the nested structure of \"%s\" "
"within %s.", s->dst->fname,
s->parent->name))
return 0;
}
if (s->type == STYPE_ITERATE && !gen_comment
(f, 0, COMMENT_C_FRAG,
"This callback function is called during an "
"implicit transaction: thus, it should not "
"invoke any database modifications or risk "
"deadlock."))
return 0;
if ((rc->flags & STRCT_HAS_NULLREFS) && !gen_comment
(f, 0, COMMENT_C_FRAG,
"This search involves nested null structure "
"linking, which involves multiple database "
"calls per invocation.\n"
"Use this sparingly!"))
return 0;
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"Queries on the following fields in struct %s:",
s->parent->name))
return 0;
TAILQ_FOREACH(sent, &s->sntq, entries)
if (sent->op == OPTYPE_NOTNULL) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"\t%s (not an argument: "
"checked not null)", sent->fname))
return 0;
} else if (sent->op == OPTYPE_ISNULL) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"\t%s (not an argument: "
"checked is null)", sent->fname))
return 0;
} else {
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"\tv%zu: %s (%s%s)", pos++,
sent->fname,
sent->field->type == FTYPE_PASSWORD ?
"pre-hashed password, " : "",
optypes[sent->op]))
return 0;
}
if (s->type == STYPE_SEARCH) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG_CLOSE,
"Returns a pointer or NULL on fail.\n"
"Free the pointer with db_%s_free().",
rc->name))
return 0;
} else if (s->type == STYPE_LIST) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG_CLOSE,
"Always returns a queue pointer.\n"
"Free this with db_%s_freeq().",
rc->name))
return 0;
} else if (s->type == STYPE_COUNT) {
if (!gen_comment(f, 0, COMMENT_C_FRAG_CLOSE,
"Returns the count of results."))
return 0;
} else {
if (!gen_comment(f, 0, COMMENT_C_FRAG_CLOSE,
"Invokes the given callback with "
"retrieved data."))
return 0;
}
return gen_func_db_search(f, s, 1);
}
/*
* Top-level functions for interfacing with the database.
* Returns zero on failure, non-zero on success.
*/
static int
gen_database(FILE *f, const struct config *cfg, const struct strct *p)
{
const struct search *s;
const struct field *fd;
const struct update *u;
size_t pos;
if (fputc('\n', f) == EOF)
return 0;
if (!gen_comment(f, 0, COMMENT_C,
"Clear resources and free \"p\".\n"
"Has no effect if \"p\" is NULL."))
return 0;
if (!gen_func_db_free(f, p, 1))
return 0;
if (fputs("\n", f) == EOF)
return 0;
if (STRCT_HAS_QUEUE & p->flags) {
if (!gen_comment(f, 0, COMMENT_C,
"Unfill and free all queue members.\n"
"Has no effect if \"q\" is NULL."))
return 0;
if (!gen_func_db_freeq(f, p, 1))
return 0;
if (fputs("\n", f) == EOF)
return 0;
}
if (p->ins != NULL) {
if (!gen_comment(f, 0, COMMENT_C_FRAG_OPEN,
"Insert a new row into the database.\n"
"Only native (and non-rowid) fields may "
"be set."))
return 0;
pos = 1;
TAILQ_FOREACH(fd, &p->fq, entries) {
if (fd->type == FTYPE_STRUCT ||
(fd->flags & FIELD_ROWID))
continue;
if (fd->type == FTYPE_PASSWORD) {
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"\tv%zu: %s (pre-hashed password)",
pos++, fd->name))
return 0;
} else {
if (!gen_commentv(f, 0, COMMENT_C_FRAG,
"\tv%zu: %s", pos++, fd->name))
return 0;
}
}
if (!gen_comment(f, 0, COMMENT_C_FRAG_CLOSE,
"Returns the new row's identifier on "
"success or <0 otherwise."))
return 0;
if (!gen_func_db_insert(f, p, 1))
return 0;
}
TAILQ_FOREACH(s, &p->sq, entries)
if (!gen_search(f, cfg, s))
return 0;
TAILQ_FOREACH(u, &p->uq, entries)
if (!gen_update(f, cfg, u))
return 0;
TAILQ_FOREACH(u, &p->dq, entries)
if (!gen_update(f, cfg, u))
return 0;
return 1;
}
/*
* Emit sections for JSMN parsing of JSON.
* Return zero on failure, non-zero on success.
*/
static int
gen_json_parse(FILE *f, const struct config *cfg, const struct strct *p)
{
if (fputc('\n', f) == EOF)
return 0;
if (!gen_comment(f, 0, COMMENT_C,
"Deserialise the parsed JSON buffer \"buf\", which "
"need not be NUL terminated, with parse tokens "
"\"t\" of length \"toksz\", into \"p\".\n"
"Returns 0 on parse failure, <0 on memory allocation "
"failure, or the count of tokens parsed on success."))
return 0;
if (!gen_func_json_parse(f, p, 1))
return 0;
if (fputs("\n", f) == EOF)
return 0;
if (!gen_commentv(f, 0, COMMENT_C,
"Deserialise the parsed JSON buffer \"buf\", which "
"need not be NUL terminated, with parse tokens "
"\"t\" of length \"toksz\", into an array \"p\" "
"allocated with \"sz\" elements.\n"
"The array must be freed with jsmn_%s_free_array().\n"
"Returns 0 on parse failure, <0 on memory allocation "
"failure, or the count of tokens parsed on success.",
p->name))
return 0;
if (!gen_func_json_parse_array(f, p, 1))
return 0;
if (fputs("\n", f) == EOF)
return 0;
if (!gen_commentv(f, 0, COMMENT_C,
"Free an array from jsmn_%s_array(). "
"Frees the pointer as well.\n"
"May be passed NULL.", p->name))
return 0;
if (!gen_func_json_free_array(f, p, 1))
return 0;
if (fputs("\n", f) == EOF)
return 0;
if (!gen_commentv(f, 0, COMMENT_C,
"Clear memory from jsmn_%s(). "
"Does not touch the pointer itself.\n"
"May be passed NULL.", p->name))
return 0;
return gen_func_json_clear(f, p, 1);
}
/*
* Emit functions for JSON output via kcgi.
* Return zero on failure, non-zero on success.
*/
static int
gen_json_out(FILE *f, const struct config *cfg, const struct strct *p)
{
if (fputc('\n', f) == EOF)
return 0;
if (!gen_commentv(f, 0, COMMENT_C,
"Print out the fields of a %s in JSON "
"including nested structures.\n"
"Omits any password entries or those "
"marked \"noexport\".\n"
"See json_%s_obj() for the full object.",
p->name, p->name))
return 0;
if (!gen_func_json_data(f, p, 1))
return 0;
if (fputs("\n", f) == EOF)
return 0;
if (!gen_commentv(f, 0, COMMENT_C,
"Emit the JSON key-value pair for the "
"object:\n"
"\t\"%s\" : { [data]+ }\n"
"See json_%s_data() for the data.",
p->name, p->name))
return 0;
if (!gen_func_json_obj(f, p, 1))
return 0;
if (fputs("\n", f) == EOF)
return 0;
if (STRCT_HAS_QUEUE & p->flags) {
if (!gen_commentv(f, 0, COMMENT_C,
"Emit the JSON key-value pair for the "
"array:\n"
"\t\"%s_q\" : [ [{data}]+ ]\n"
"See json_%s_data() for the data.",
p->name, p->name))
return 0;
if (!gen_func_json_array(f, p, 1))
return 0;
}
if (STRCT_HAS_ITERATOR & p->flags) {
if (!gen_commentv(f, 0, COMMENT_C,
"Emit the object as a standalone "
"part of (presumably) an array:\n"
"\t\"{ data }\n"
"See json_%s_data() for the data.\n"
"The \"void\" argument is taken "
"to be a kjsonreq as if were invoked "
"from an iterator.", p->name))
return 0;
if (!gen_func_json_iterate(f, p, 1))
return 0;
}
return 1;
}
/*
* Generate the validation function for all fields in the structure.
* Return zero on failure, non-zero on success.
*/
static int
gen_valids(FILE *f, const struct config *cfg, const struct strct *p)
{
const struct field *fd;
TAILQ_FOREACH(fd, &p->fq, entries) {
if (fputc('\n', f) == EOF)
return 0;
if (!gen_commentv(f, 0, COMMENT_C,
"Validation routines for the %s "
"field in struct %s.", fd->name, p->name))
return 0;
if (!gen_func_valid(f, fd, 1))
return 0;
}
return 1;
}
/*
* Generate validation enum names for the structure.
* Return zero on failure, non-zero on success.
*/
static int
gen_valid_enums(FILE *f, const struct strct *p)
{
const struct field *fd;
TAILQ_FOREACH(fd, &p->fq, entries) {
if (fd->type == FTYPE_STRUCT)
continue;
if (fputs("\tVALID_", f) == EOF)
return 0;
if (!gen_upper(f, p->name))
return 0;
if (fputc('_', f) == EOF)
return 0;
if (!gen_upper(f, fd->name))
return 0;
if (fputs(",\n", f) == EOF)
return 0;
}
return 1;
}
/*
* Generate database transaction functions.
* Return zero on failure, non-zero on success.
*/
static int
gen_transaction(FILE *f, const struct config *cfg)
{
if (fputc('\n', f) == EOF)
return 0;
if (!gen_comment(f, 0, COMMENT_C,
"Open a transaction with identifier \"id\".\n"
"If \"mode\" is 0, the transaction is opened in "
"\"deferred\" mode, meaning that the database is "
"read-locked (no writes allowed) on the first read "
"operation, and write-locked on the first write "
"(only the current process can write).\n"
"If \"mode\" is >0, the transaction immediately "
"starts a write-lock.\n"
"If \"mode\" is <0, the transaction starts in a "
"write-pending, where no other locks can be held "
"at the same time.\n"
"The DB_TRANS_OPEN_IMMEDIATE, "
"DB_TRANS_OPEN_DEFERRED, and "
"DB_TRANS_OPEN_EXCLUSIVE macros accomplish the "
"same but with the \"mode\" being explicit in the "
"name and not needing to be specified."))
return 0;
if (!gen_func_db_trans_open(f, 1))
return 0;
if (fputs("\n", f) == EOF)
return 0;
if (fputs("#define DB_TRANS_OPEN_IMMEDIATE(_ctx, _id) \\\n"
"\tdb_trans_open((_ctx), (_id), 1)\n"
"#define DB_TRANS_OPEN_DEFERRED(_ctx, _id)\\\n"
"\tdb_trans_open((_ctx), (_id), 0)\n"
"#define DB_TRANS_OPEN_EXCLUSIVE(_ctx, _id)\\\n"
"\tdb_trans_open((_ctx), (_id), -1)\n\n", f) == EOF)
return 0;
if (!gen_comment(f, 0, COMMENT_C,
"Roll-back an open transaction."))
return 0;
if (!gen_func_db_trans_rollback(f, 1))
return 0;
if (fputs("\n", f) == EOF)
return 0;
if (!gen_comment(f, 0, COMMENT_C,
"Commit an open transaction."))
return 0;
if (!gen_func_db_trans_commit(f, 1))
return 0;
return fputs("\n", f) != EOF;
}
/*
* Generate open and logging open (and auxiliary) functions.
* Return zero on failure, non-zero on success.
*/
static int
gen_open(FILE *f, const struct config *cfg)
{
if (fputc('\n', f) == EOF)
return 0;
if (!gen_comment(f, 0, COMMENT_C,
"Forward declaration of opaque pointer."))
return 0;
if (fputs("struct ort;\n\n", f) == EOF)
return 0;
if (!gen_comment(f, 0, COMMENT_C,
"Set the argument given to the logging function "
"specified to db_open_logging().\n"
"Has no effect if no logging function has been "
"set.\n"
"The buffer is copied into a child process, so "
"serialised objects may not have any pointers "
"in the current address space or they will fail "
"(at best).\n"
"Set length to zero to unset the logging function "