-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgdbm.c
1593 lines (1453 loc) · 48.7 KB
/
tgdbm.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
/*
* tgdbm.c --
*
* This file contains a simple Tcl-Wrapper for gdbm-1.8.3.
* Copyright (C) 2000 - 2005 Stefan Vogel
*
* $Log: tgdbm.c,v $
* Revision 1.5 2005/04/14 14:13:01 svogel
* Added changes from Thomas Maeder
*
* Revision 1.4 2003/12/16 12:29:31 svogel
* gdbm_open without args returns list of open handles
*
* Revision 1.3 2003/12/12 11:56:23 svogel
* completely rewritten, added array, more modularized
*
* Revision 1.2 2000/10/15 16:25:53 svogel
* Changed Tcl_SetResult from TCL_STATIC to TCL_VOLATILE
* in count/maxkey. Thanks to Uwe Klein for his patch.
*
* Revision 1.1 2000/10/15 16:25:38 svogel
* Initial revision
*
*/
/* TGDBM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
TGDBM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
If you don't have a copy of the GNU General Public License,
write to the Free Software Foundation, 675 Mass Ave, Cambridge,
MA 02139, USA.
You may contact the author by:
e-mail: [email protected]
*************************************************************************/
static char rcsid[] = "$Id: tgdbm.c,v 1.5 2005/04/14 14:13:01 svogel Exp $";
// Sync to tcl.h
#if (defined(__WIN32__) && (defined(_MSC_VER) || (__BORLANDC__ >= 0x0550) || defined(__LCC__) || defined(__WATCOMC__) || (defined(__GNUC__) && defined(__declspec))))
# define HAVE_DECLSPEC 1
# ifdef STATIC_BUILD
# define DLLIMPORT
# define DLLEXPORT
# ifdef _DLL
# define CRTIMPORT __declspec(dllimport)
# else
# define CRTIMPORT
# endif
# else
# define DLLIMPORT __declspec(dllimport)
# define DLLEXPORT __declspec(dllexport)
# define CRTIMPORT __declspec(dllimport)
# endif
#else
# define DLLIMPORT
# if defined(__GNUC__) && __GNUC__ > 3
# define DLLEXPORT __attribute__ ((visibility("default")))
# else
# define DLLEXPORT
# endif
# define CRTIMPORT
#endif
/*
* For C++ compilers, use extern "C"
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <tcl.h>
#include <gdbm.h>
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
# define ANYARGS (...)
#else
# define ANYARGS ()
#endif
/* These external variables are needed by tgdbm */
// Define in gdbm.h, disable it
//extern const char* gdbm_version;
//extern gdbm_error gdbm_errno;
#define TGDBM_VERSION "0.7"
/* 0x10 */
#define GDBM_SETVAR TCL_TRACE_READS
/* 0x20 */
#define GDBM_DELVAR TCL_TRACE_WRITES
/* 0x40 */
#define GDBM_DELALL TCL_TRACE_UNSETS
#define GDBM_TRACE_ALL (TCL_TRACE_WRITES | TCL_TRACE_READS | TCL_TRACE_UNSETS | TCL_TRACE_ARRAY)
typedef int (*Gdbm_Proc) ANYARGS;
/* -------------------- global variables -------------------- */
typedef struct {
char *name; /* Name of minor command */
Gdbm_Proc proc;
int minArgs; /* Minimum # args required */
int maxArgs; /* Maximum # args required */
char *usage; /* Usage message */
} Gdbm_ProcSpec;
/* handle to opened gdbm-file, used in Gdbm_Cmd */
Tcl_HashTable thtbl;
/* pointer to the tcl-string (if provided in gdbm_open call),
the tcl-function specified with this string is called
in fatal-error-case (hopefully never) */
char* gFatalFunc = NULL;
/* Tcl-Interpreter for fatal-function */
Tcl_Interp* gInterp = NULL;
/* used for returning errors, see defines SET_ERROR */
char sErrno[5];
/* the structure which holds all information about the
gdbm-file and additional infos about the array */
typedef struct {
int has_array; /* is there a corresponding array? */
int full_cache; /* is the whole gdbm-file in the array?
* 0 == no; 1 == yes*/
int save_with_null; /* has the file trailing-nulls in it? */
GDBM_FILE gdbm_file; /* The gdbm-file-handle */
} Gdbm_Array, *PGdbm_Array;
/*
* Prototypes for procedures defined later in this file:
*/
static char* VariableProc (ClientData, Tcl_Interp*, char*, char*, int);
int Gdbm_Open_Cmd (ClientData, Tcl_Interp*, int, char**);
int Gdbm_Cmd (ClientData, Tcl_Interp*, int, char**);
int Gdbm_Error_Cmd (ClientData, Tcl_Interp*, int, char**);
/*
* some defines to make checking of arguments and returning
* and setting Errors easier (don't insert backslashes and newlines, because
* it will break vc++ windows-compiler)
*/
#define CHECK_ARGS(ARGC, MIN, MAX, USG) if ((ARGC<MIN+1)||(ARGC>MAX+1)) {Tcl_AppendResult(interp, "Usage: ", USG, NULL);return (TCL_ERROR);}
#define SET_ERROR(errno) sprintf(sErrno, "%d", errno);Tcl_SetVar(interp, "GDBM_ERRNO", (char*)sErrno, TCL_GLOBAL_ONLY);return TCL_ERROR
#define SET_OK Tcl_SetVar(interp, "GDBM_ERRNO", "0", TCL_GLOBAL_ONLY);return TCL_OK
#define SET_LENGTH(gdbm, key) if(gdbm->save_with_null) key.dsize=strlen(key.dptr)+1; else key.dsize=strlen(key.dptr)
/*----------------------------------------------------------------------
* GetIndexFromArg -- search for a specified string in given string-table
*
* Because of performance no Tcl-Obj-Command was used here (does this
* still hold?).
* This has the affect that no Obj-Command-Utility-Functions could be
* used, too.
* This one is the only Utility-Function needed, so it is reimplemented
* here.
*
* Results:
* position/index of string in string-table
* -1 : string not found in string-table
*----------------------------------------------------------------------
*/
int
GetIndexFromArg(arg, tablePtr)
char* arg;
char** tablePtr;
{
int i;
char **entryPtr;
for (entryPtr = tablePtr, i = 0; *entryPtr != NULL;
entryPtr = (char**) ((long) entryPtr + sizeof(char*)), i++) {
if (!strcmp(*entryPtr, arg)) return i;
}
return -1;
}
/*----------------------------------------------------------------------
* GetIndexFromSpec -- see GetIndexFromArg
*
* only difference is that index is retrieved from "spec"
*
* Results:
* position/index of string in spec
* -1 : string not found in spec
*----------------------------------------------------------------------
*/int
GetIndexFromSpec(arg, numArgs, procSpec)
char* arg;
int numArgs;
Gdbm_ProcSpec procSpec[];
{
int i;
for (i = 0; i < numArgs; i++) {
if (!strcmp(procSpec[i].name, arg)) return i;
}
return -1;
}
/*----------------------------------------------------------------------
* toNumber -- convert a given string if it is a valid non-negative
* integer
*
* Results:
* 1 : string contains only digits
* 0 : string is no positive integer
*
* Side effects:
* *result is assigned the result of the conversion
*----------------------------------------------------------------------
*/
int
toNumber(string, result)
char* string;
int* result;
{
char* end;
unsigned long tmp = strtoul(string,&end,10);
if (end==string+strlen(string) && tmp<=INT_MAX)
{
*result = tmp;
return 1;
}
else
return 0;
}
/*----------------------------------------------------------------------
* copyResultNull -- copy the result with trailing null
*
* This is used when gdbm_open is specified with "no_trailing_null".
* We have to copy the results, so that they are "C"-conform-strings
* (with trailing null).
* The result has to be freed by the caller.
* Attention the input-string is freed by this function
*
* Results:
* string - copy/original from parameter str
*
* Side effects:
* str is copied if there is no trailing null. str is freed if
* necessary (and freeStr == true)
*----------------------------------------------------------------------
*/
char*
copyResultNull(str, size, freeStr)
char* str;
int size;
int freeStr;
{
char* result;
if (size>0 && (char)str[size-1] == (char)NULL) {
result = str;
}
else {
result = malloc(size+1);
strncpy(result, str, size);
result[size] = (char) NULL;
if (freeStr)
free(str);
}
return result;
}
/*----------------------------------------------------------------------
* fillCompleteArray -- do a full-cache of the gdbm-file
*
* force = 1 -> don't check if values in array exists (for initial filling
* of array)!
*----------------------------------------------------------------------
*/
int
fillCompleteArray(interp, gdbm, array_name, force)
Tcl_Interp* interp;
PGdbm_Array gdbm;
char* array_name;
int force;
{
int size;
datum key, key2, result;
char* res_key;
char* res_res;
size = 0;
key = gdbm_firstkey(gdbm->gdbm_file);
while (key.dptr != NULL) {
size++;
/* dont free key.dptr here, because we need it for nextkey
and we free it ourself */
res_key = copyResultNull(key.dptr, key.dsize, 0);
if (force
|| Tcl_GetVar2(interp, array_name, res_key, 0) == NULL) {
result = gdbm_fetch(gdbm->gdbm_file, key);
res_res = copyResultNull(result.dptr, result.dsize, 1);
if (res_res != NULL
&& Tcl_SetVar2(interp, array_name, res_key, res_res, TCL_LEAVE_ERR_MSG) == NULL) {
/* TODO error should be returned here?? */
printf("Error setting %s(%s) to %s\n", array_name, res_key, res_res);
}
free(res_res);
}
key2 = key;
key = gdbm_nextkey(gdbm->gdbm_file, key2);
if (key2.dptr != res_key) {
free(key2.dptr);
free(res_key);
}
else {
free(key2.dptr);
}
}
return size;
}
/*----------------------------------------------------------------------
* gdbmFatalFunc -- standard function called from gdbm in fatal error
*
* If a Tcl-Function has been provided with -fatal_func in call to
* gdbm_open, this function will be called here.
* Hopefully this should be never.
*----------------------------------------------------------------------
*/
void
gdbmFatalFunc(message)
char* message;
{
char* script;
if (gFatalFunc && gInterp) {
script = malloc(strlen(gFatalFunc)+strlen(message)+5);
sprintf(script, "%s {%s}", gFatalFunc, message);
Tcl_Eval(gInterp, script);
free(script);
}
exit(1);
}
/*----------------------------------------------------------------------
* Gdbm_Error_Cmd -- Return a textual error-description for error-number
*
* This is the aequivalent for gdbm_strerror. If given an errorcode
* the value of the global Tcl-Variable GDBM_ERRNO an english error-message
* is returned.
*
* Results:
* Error-message.
*
* Side effects:
* None.
*----------------------------------------------------------------------
*/
int
Gdbm_Error_Cmd(dummy, interp, argc, argv)
ClientData dummy;
Tcl_Interp* interp;
int argc;
char* argv[];
{
CHECK_ARGS(argc, 1, 1, "gdbm_strerror $GDBM_ERRNO");
Tcl_SetResult(interp, gdbm_strerror(atoi(argv[1])), TCL_STATIC);
SET_OK;
}
/*----------------------------------------------------------------------
* VariableProc -- Callback for Array-Access
*
* This is the heart of the persistant arrays. Every READ/WRITE- or ARRAY-
* Trace is handled here.
*
* Results:
* NULL when no error occurs
* errorstring - else
*
* Side effects:
* The values of the array is set or read in case of a TCL_TRACE_ARRAY
* the whole gdbm-file is read in the array.
*----------------------------------------------------------------------
*/
static char*
VariableProc(clientData, interp, part1, part2, flags)
ClientData clientData;
Tcl_Interp* interp;
char *part1, *part2;
int flags;
{
datum key, result;
static char errMsg[255] = "";
PGdbm_Array gdbm_array = (PGdbm_Array) clientData;
int iResult;
char* res_res;
if (flags & TCL_TRACE_WRITES) {
if (part2 == NULL) {
/* this error is never shown because Tcl already handles this error */
return "variable is array";
}
key.dptr = part2;
/* TODO: handle null here? */
result.dptr = (char*) Tcl_GetVar2(interp, part1, part2, 0);
SET_LENGTH(gdbm_array, key);
SET_LENGTH(gdbm_array, result);
/* test if we can do an insert, if not: replace item */
if ((gdbm_store(gdbm_array->gdbm_file, key, result, GDBM_INSERT) != 0)
&& (gdbm_store(gdbm_array->gdbm_file, key, result, GDBM_REPLACE) != 0)) {
sprintf(errMsg, "%d: %s", gdbm_errno, gdbm_strerror(gdbm_errno));
return errMsg;
}
return NULL;
} else if (flags & TCL_TRACE_READS) {
if (part2 == NULL) {
/* this message is like the standard tcl-error-message
* but "can't read xx:" is prefixed by tcl? */
return "variable is array";
}
if (Tcl_GetVar2(interp, part1, part2, 0) == NULL) {
key.dptr = part2;
SET_LENGTH(gdbm_array, key);
result = gdbm_fetch(gdbm_array->gdbm_file, key);
if (result.dptr == NULL) {
Tcl_AppendResult(interp, "can't read \"",
part1, "(", part2, ")\": no such variable",
NULL);
return NULL;
}
else {
res_res = copyResultNull(result.dptr, result.dsize, 1);
Tcl_SetVar2(interp, part1, part2, res_res, 0);
Tcl_SetResult(interp, res_res, TCL_STATIC /* NULL */);
free(res_res);
return NULL;
}
}
} else if (flags & TCL_TRACE_UNSETS) {
if (part2 != NULL) {
/* remove only this one entry */
key.dptr = part2;
SET_LENGTH(gdbm_array, key);
/* retcode == -1 --> no such key or requester is reader
* == 0 --> successful delete
*/
iResult = gdbm_delete(gdbm_array->gdbm_file, key);
if (iResult != 0) {
Tcl_AppendResult(interp, "error: gdbmN delete - ",
gdbm_strerror(gdbm_errno),
NULL);
/* TODO: throw an error here. This doesn't work correctly???!!! */
sprintf(errMsg, "%d", gdbm_errno);
Tcl_SetVar(interp, "GDBM_ERRNO", (char*)errMsg, TCL_GLOBAL_ONLY);
sprintf(errMsg, "%d: %s", gdbm_errno, gdbm_strerror(gdbm_errno));
return errMsg;
}
}
else {
Tcl_HashEntry *thetr;
/* Unsetting everything */
if (gdbm_array->gdbm_file != NULL)
gdbm_close(gdbm_array->gdbm_file);
Tcl_DeleteCommand(interp, part1);
Tcl_UntraceVar2(interp, part1,
(char*)NULL,
(int) GDBM_TRACE_ALL,
(Tcl_VarTraceProc*) VariableProc,
(ClientData) gdbm_array);
/* now that the command is deleted we free memory,
gdbm is freed in gdbm_close */
thetr = Tcl_FindHashEntry(&thtbl, part1);
if (thetr != NULL)
Tcl_DeleteHashEntry(thetr);
free(gdbm_array);
}
} else if (flags & TCL_TRACE_ARRAY) {
/* Tcl_TRACE doesn't seem to be inactivated in this case, bug in sourceforge already fixed
dont know number/version ... therefore no need to remove Trace anymore*/
if (!gdbm_array->full_cache) {
fillCompleteArray(interp, gdbm_array, part1, 0);
gdbm_array->full_cache = 1;
}
/* else ... array is already fully cached, do nothing */
}
return NULL;
}
/*----------------------------------------------------------------------
* Gdbm_ArrayTrace_Proc -- every access to data calls this callback
*
* this traces access to all gdbm-data and synchronizes the gdbm-file
* resp. the attached array
*
* Results:
*
* Side effects:
*----------------------------------------------------------------------
*/
static int
Gdbm_ArrayTrace_Proc (clientData, interp, array_name, key, value, flags)
ClientData clientData;
Tcl_Interp* interp;
char *array_name, *key, *value;
int flags;
{
PGdbm_Array gdbm = (PGdbm_Array) clientData;
if (gdbm->has_array) {
/* This is because of a bug in Tcl 8.???
we have to remove the trace, set the array and afterwards
create the array-trace again
If not untraced this causes a core.
*/
Tcl_UntraceVar2(interp, array_name, (char*)NULL,
GDBM_TRACE_ALL,
(Tcl_VarTraceProc*) VariableProc,
(ClientData) gdbm);
switch (flags) {
case GDBM_SETVAR:
Tcl_SetVar2(interp, array_name, key, value, 0);
break;
case GDBM_DELVAR:
Tcl_UnsetVar2(interp, array_name, key, 0);
break;
case GDBM_DELALL:
Tcl_UnsetVar2(interp, array_name, (char*)NULL, 0);
break;
default:
Tcl_AppendResult(interp, "error: Wrong arguments for Gdbm_ArrayTrace_Proc\n",
"Must be 'GDBM_SETVAR' or 'GDBM_DELVAR'",
NULL);
SET_ERROR(GDBM_OPT_ILLEGAL);
}
if (flags != GDBM_DELALL) {
/* only set trace on again, if not completely deleted */
Tcl_TraceVar2(interp, array_name, (char*)NULL,
GDBM_TRACE_ALL,
(Tcl_VarTraceProc*) VariableProc,
(ClientData) gdbm);
}
}
SET_OK;
}
/*----------------------------------------------------------------------
* Gdbm_Create_Array -- initial creation of an array
*
* This function is called when an array is attached to a gdbm-file.
*
* Results:
* Always 1
*
* Side effects:
* The first element is always tried to be set directly into the array
*----------------------------------------------------------------------
*/
int
Gdbm_Create_Array(interp, name, gdbm_array)
Tcl_Interp* interp;
char* name;
PGdbm_Array gdbm_array;
{
datum result, key;
char* res_key;
char* res_res;
/* Setting one element so the trace-read does work.
Nevertheless: Trace-Unset doesn't work for not existing elements
*/
key = gdbm_firstkey(gdbm_array->gdbm_file);
if (key.dptr != NULL) {
result = gdbm_fetch(gdbm_array->gdbm_file, key);
/* remember ... key will be used below! */
res_key = copyResultNull(key.dptr, key.dsize, 1);
res_res = copyResultNull(result.dptr, result.dsize, 1);
Tcl_SetVar2(interp, name, res_key, res_res, TCL_LEAVE_ERR_MSG);
free(res_key);
free(res_res);
}
if (gdbm_array->full_cache) {
/* Read the whole file into the array */
fillCompleteArray(interp, gdbm_array, name, 1);
}
Tcl_TraceVar2(interp, name, (char*)NULL,
GDBM_TRACE_ALL,
(Tcl_VarTraceProc*) VariableProc,
(ClientData) gdbm_array);
return 1;
}
/*----------------------------------------------------------------------
* Gdbm_Open_Cmd -- Open/Create a GDBM-File
*
* This procedure is invoked to open/create a GDBM-Databasefile
* It expects several parameters:
* -writer,
* -reader: the calling process does only read from file or write
* -wrcreat: create the db-file, if it is not existent
* -newdb: create the db-file, regardless if one exists
* -nolock: do not lock the db-file (should be used with care)
* -sync: db-operations are synchronized to the disk
* -block_size: see gdbm-manual
* -mode: file-mode, if the db-file is created (directly
* passed to open(2))
* -fatal_func: a tcl-function-name (which gets one argument)
* -array: name of an array-variable
* -full_cache: only with parameter -array; immediately cache the
* whole file in the specified array
* -no_trailing_null no null is appended to keys and values either
* by reading or writing
* -help print the usage-message
* and the name of the gdbm-file.
*
* The parameters: -writer, -reader, -wrcreat, -newdb, -nolock
* represent the following gdbm-defines:
* GDBM_WRITER, GDBM_READER, GDBM_WRCREAT, GDBM_NEWDB
* For db-writers the following flags could be specified:
* GDBM_NOLOCK or GDBM_SYNC.
*
* Results:
* A unique gdbm-file-descriptor is returned. This is used for further
* access to the gdbm-file as a Tcl-Command.
*
* Side effects:
* A Tcl-Command is created.
*----------------------------------------------------------------------
*/
int
Gdbm_Open_Cmd(notused, interp, argc, argv)
ClientData notused;
Tcl_Interp* interp;
int argc;
char* argv[];
{
int i, index, flags, block_size, mode, already_created, full_cache;
char* array_name = NULL;
GDBM_FILE hd_gdbm = NULL;
Tcl_HashEntry *thetr;
PGdbm_Array gdbm_array = NULL;
static int db_number = 0;
int save_with_null;
char gdbm_name[32];
static char usage[] = "gdbm_open ?options? filename\n\
options=-reader, -writer, -wrcreat, -newdb, -nolock -sync\n\
-block_size <size> -mode <filemode> -fatal_func <command>\n\
-array <arrayname> -full_cache -no_trailing_null -help\n\
where <size> 512, ...\n\
<filemode> accessmode of file, e.g. [expr 0664]\n\
<command> tcl-proc with one parameter called in case of fatal error\n\
<arrayname> the (optional) name of an persistent array\n\
for further information see gdbm-man-page or infos on http:https://www.vogel-nest.de/tcl";
static char *switches[] =
{"-reader", "-writer", "-wrcreat", "-newdb", "-nolock", "-sync",
"-block_size", "-mode", "-fatal_func", "-array", "-full_cache", "-no_trailing_null", "-help", (char*) NULL};
static int gdbm_flags[] =
{ GDBM_READER, GDBM_WRITER, GDBM_WRCREAT, GDBM_NEWDB, GDBM_NOLOCK, GDBM_SYNC };
flags = 0;
block_size = 0;
mode = 0644;
full_cache = 0;
save_with_null = 1;
if (argc < 2) {
/* called gdbm_open without arguments: return a list of open handles */
Tcl_HashEntry *entryPtr;
Tcl_HashSearch searchPtr;
Tcl_Obj *keys;
keys = Tcl_NewListObj(0, NULL);
entryPtr = Tcl_FirstHashEntry(&thtbl, &searchPtr);
while (entryPtr) {
Tcl_ListObjAppendElement(NULL, keys,
Tcl_NewStringObj(Tcl_GetHashKey(&thtbl, entryPtr), -1));
entryPtr = Tcl_NextHashEntry(&searchPtr);
}
Tcl_SetObjResult(interp, keys);
SET_OK;
}
/*
* Parse arguments
*/
for (i = 1; i < argc-1; i++) {
index = GetIndexFromArg(argv[i], switches);
switch (index) {
case 0: /* -reader */
case 1: /* -writer */
case 2: /* -wrcreat */
case 3: /* -newdb */
case 4: /* -nolock */
case 5: /* -sync */
flags = flags | gdbm_flags[index];
break;
case 6: /* -block_size */
if ((i++ < argc-1) && toNumber(argv[i],&block_size)) {
// nothing
}
else {
Tcl_AppendResult(interp, "-block_size numbervalue\n\n", usage, NULL);
return TCL_ERROR;
}
break;
case 7: /* -mode */
if ((i++ < argc-1) && toNumber(argv[i],&mode)) {
// nothing
}
else {
Tcl_AppendResult(interp, "-mode numbervalue\n\n", usage, NULL);
return TCL_ERROR;
}
break;
case 8: /* -fatal_func */
/* Don't really check the next argument, it may be the filename
to be opened, if the user provided the wrong parameters to gdbm_open.
But that doesn't bother much. */
if (i++ < argc-1) {
free(gFatalFunc);
gFatalFunc = malloc(strlen(argv[i])+1);
strcpy(gFatalFunc, argv[i]);
gInterp = interp;
}
break;
case 9: /* -array */
if (i++ < argc-1) {
array_name = argv[i];
}
else {
Tcl_AppendResult(interp, "-array array_name\n\n", usage, NULL);
return TCL_ERROR;
}
break;
case 10: /* -full_cache */
full_cache = 1;
break;
case 11: /* -no_trailing_null */
save_with_null = 0;
break;
case 12: /* -help */
Tcl_AppendResult(interp, usage, NULL);
return TCL_OK;
break; /* just to be sure :-) */
default: /* error */
Tcl_AppendResult(interp, "error: gdbm_open - No such argument: ",
argv[i], "\n", usage, NULL);
SET_ERROR(GDBM_OPT_ILLEGAL);
}
}
if (full_cache && (array_name == NULL)) {
Tcl_AppendResult(interp, "error: gdbm_open - cannot specify '-full_cache', ",
"when not providing an arrayname with '-array'.", NULL);
SET_ERROR(GDBM_OPT_ILLEGAL);
}
/* call to gdbm_open with the collected parameters and filename argv[argc-1] */
/* check if "file"-argument is "-help"... than print usage */
if (strcmp(argv[argc-1], "-help") == 0) {
Tcl_AppendResult(interp, usage, NULL);
return TCL_OK;
}
hd_gdbm = gdbm_open(argv[argc-1], block_size, flags, mode, gdbmFatalFunc);
if (hd_gdbm == NULL) {
/* When opening a non-existing-file with -reader "No error" is returned
Therefore explicitly set the error to GDBM_FILE_OPEN_ERROR
Or called with: gdbm_open -xyz
*/
if (gdbm_errno == 0)
gdbm_errno = GDBM_FILE_OPEN_ERROR;
Tcl_AppendResult(interp, "error: gdbm_open (file: '", argv[argc-1], "') - ",
gdbm_strerror(gdbm_errno), NULL);
SET_ERROR(gdbm_errno);
}
gdbm_array = malloc(sizeof(Gdbm_Array));
gdbm_array->has_array = 0;
gdbm_array->full_cache = full_cache;
gdbm_array->save_with_null = save_with_null;
gdbm_array->gdbm_file = hd_gdbm;
if (array_name != NULL) {
if (strlen(array_name) > 31) {
/* remember to close the already opened file in case of this error */
gdbm_close(hd_gdbm);
free(gdbm_array);
Tcl_AppendResult(interp, "error: gdbm_open -array ...\n",
"arrayname ist too long (max. 31 chars).", NULL);
SET_ERROR(GDBM_OPT_ILLEGAL);
}
if ((thetr = Tcl_FindHashEntry(&thtbl, array_name)) != NULL) {
/* This array is already registered inside gdbm, remember to close the file */
gdbm_close(hd_gdbm);
free(gdbm_array);
Tcl_AppendResult(interp, "error: gdbm_open -array ...\n",
"arrayname '", array_name, "' is already in use.", NULL);
SET_ERROR(GDBM_OPT_ILLEGAL);
}
gdbm_array->has_array = 1;
Gdbm_Create_Array(interp, array_name, gdbm_array);
sprintf(gdbm_name, "%s", array_name);
}
else {
/* create the unique gdbm-descriptor */
sprintf(gdbm_name, "gdbm%d", db_number);
db_number++;
}
/* put gdbm-file-handle in HashTable: */
thetr = Tcl_CreateHashEntry(&thtbl, gdbm_name, &already_created);
if (thetr == NULL) gdbmFatalFunc("Could not create Tcl-Hashentry!");
Tcl_SetHashValue(thetr, gdbm_array);
/* create the tcl-command with this descriptor, pass the gdbm-handle */
Tcl_CreateCommand(interp, gdbm_name,
(Tcl_CmdProc*) Gdbm_Cmd,
(ClientData) gdbm_array, NULL);
/* Make a copy of this name */
Tcl_SetResult(interp, gdbm_name, TCL_VOLATILE);
SET_OK;
}
/*
*----------------------------------------------------------------------
* Gdbm_LookupProc --
*
* Find the command operation given a string name. This is useful
* where a group of command operations have the same argument
* signature.
*
* Results:
* If found, a pointer to the procedure (function pointer) is
* returned. Otherwise NULL is returned and an error message
* containing a list of the possible commands is returned in
* interp->result.
*----------------------------------------------------------------------
*/
Gdbm_Proc
Gdbm_LookupProc (interp, numSpecs, specArr, argIndex, numArgs, argArr)
Tcl_Interp *interp; /* Interpreter to report errors to */
int numSpecs; /* Number of specifications in array */
Gdbm_ProcSpec specArr[];/* Operation specification array */
int argIndex; /* Index of the operation name argument */
int numArgs; /* Number of arguments in the argument vector.
* This includes any prefixed arguments */
char *argArr[]; /* Argument vector */
{
Gdbm_ProcSpec *specPtr;
char *string;
register int i;
register int specIndex;
if (numArgs <= argIndex) { /* No operation argument */
Tcl_AppendResult(interp, "wrong # args: ", (char *)NULL);
usage:
Tcl_AppendResult(interp, "should be one of...",
(char *)NULL);
for (specIndex = 0; specIndex < numSpecs; specIndex++) {
Tcl_AppendResult(interp, "\n ", (char *)NULL);
for (i = 0; i < argIndex; i++) {
Tcl_AppendResult(interp, argArr[i], " ", (char *)NULL);
}
specPtr = specArr + specIndex;
Tcl_AppendResult(interp, specPtr->name, " ", specPtr->usage,
(char *)NULL);
}
return NULL;
}
string = argArr[argIndex];
specIndex = GetIndexFromSpec(string, numSpecs, specArr);
if (specIndex == -1) { /* Can't find operation, display help */
Tcl_AppendResult(interp, "bad", (char *)NULL);
if (argIndex > 2) {
Tcl_AppendResult(interp, " ", argArr[argIndex - 1], (char *)NULL);
}
Tcl_AppendResult(interp, " operation \"", string, "\": ", (char *)NULL);
goto usage;
}
specPtr = specArr + specIndex;
if ((numArgs < specPtr->minArgs) || ((specPtr->maxArgs > 0) &&
(numArgs > specPtr->maxArgs))) {
Tcl_AppendResult(interp, "wrong # args: should be \"", (char *)NULL);
for (i = 0; i < argIndex; i++) {
Tcl_AppendResult(interp, argArr[i], " ", (char *)NULL);
}
Tcl_AppendResult(interp, specPtr->name, " ", specPtr->usage, "\"",
(char *)NULL);
return NULL;
}
return (specPtr->proc);
}
/*
*----------------------------------------------------------------------
* Gdbm_Keys -- implementation of: $handle keys
*
* Results:
* a Tcl-list of all(!) keys of gdbm-file. Take care for large files.
*----------------------------------------------------------------------
*/
static int
Gdbm_Keys(gdbm, interp, argc, argv)
PGdbm_Array gdbm;
Tcl_Interp *interp;
int argc;
char **argv;
{
datum result, key;
char* res;
result = gdbm_firstkey(gdbm->gdbm_file);
while (result.dptr != NULL) {
res = copyResultNull(result.dptr, result.dsize, 0);
Tcl_AppendElement(interp, res);
if (res!=result.dptr)
free(res);
key = result;
result = gdbm_nextkey(gdbm->gdbm_file, key);
free(key.dptr);
}
SET_OK;
}
/*----------------------------------------------------------------------
* Gdbm_Count -- count all elements of gdbm-file
*
* Results:
* number of elements in gdbm-file
*----------------------------------------------------------------------
*/
static int
Gdbm_Count(gdbm, interp, argc, argv)
PGdbm_Array gdbm;
Tcl_Interp *interp;
int argc;
char **argv;
{
int count;
datum key1, key2;
char number[13];
count = 0;
key1 = gdbm_firstkey(gdbm->gdbm_file);
while (key1.dptr != NULL) {
count++;
key2 = key1;
key1 = gdbm_nextkey(gdbm->gdbm_file, key2);
free(key2.dptr);
}
sprintf(number, "%d", count);
Tcl_SetResult(interp, number, TCL_VOLATILE);
SET_OK;