-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
5579 lines (5377 loc) · 139 KB
/
shell.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
/*
/ spatialite
/
/ a CLI backend for SpatiaLite
/
/ version 4.0, 2012 November 1
/
/ Author: Sandro Furieri [email protected]
/
/ Copyright (C) 2012 Alessandro Furieri
/
/ This program 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 3 of the License, or
/ (at your option) any later version.
/
/ This program 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.
/
/ You should have received a copy of the GNU General Public License
/ along with this program. If not, see <http://www.gnu.org/licenses/>.
/
*/
/*
DISCLAIMER
==========
This file is the original SQLite command-line backend
slightly modified by Alessandro Furieri in order to
fully support the SpatiaLite extensions
*/
#if defined(_WIN32) && !defined(__MINGW32__)
#include "config-msvc.h"
#else
#include "config.h"
#endif
/* Sandro Furieri 30 May 2008
/ #include "sqlite3.h"
*/
#ifdef SPATIALITE_AMALGAMATION
#include <spatialite/sqlite3.h>
#else
#include <sqlite3.h>
#endif
#include <spatialite.h>
#include <spatialite/gaiaaux.h>
#include <spatialite/gg_wfs.h>
#include <spatialite/gg_dxf.h>
#ifdef __MINGW32__
#define LIBICONV_STATIC
#endif
#include <iconv.h>
/* end Sandro Furieri 30 May 2008 */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#ifdef _WIN32
#define strcasecmp _stricmp
#endif /* not WIN32 */
#if !defined(_WIN32) && !defined(WIN32)
#include <signal.h>
#if !defined(__RTP__) && !defined(_WRS_KERNEL)
#include <pwd.h>
#endif
#include <unistd.h>
#include <sys/types.h>
#endif
#ifdef HAVE_EDITLINE
#include <editline/editline.h>
#endif
#if defined(HAVE_READLINE) && HAVE_READLINE==1
#include <readline/readline.h>
#include <readline/history.h>
#endif
#if !defined(HAVE_EDITLINE) && (!defined(HAVE_READLINE) || HAVE_READLINE!=1)
#define readline(p) local_getline(p,stdin,0)
#define add_history(X)
#define read_history(X)
#define write_history(X)
#define stifle_history(X)
#endif
#if defined(_WIN32) || defined(WIN32)
#include <io.h>
#define isatty(h) _isatty(h)
#define access(f,m) _access((f),(m))
#undef popen
#define popen(a,b) _popen((a),(b))
#undef pclose
#define pclose(x) _pclose(x)
#else
/* Make sure isatty() has a prototype.
*/
extern int isatty (int);
#endif
#if defined(_WIN32_WCE)
/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
* thus we always assume that we have a console. That can be
* overridden with the -batch command line option.
*/
#define isatty(x) 1
#endif
/* True if the timer is enabled */
static int enableTimer = 0;
/* sandro 2013-11-07 */
void *splite_cache = NULL;
/* end sandro 2013-11-07 */
/* ctype macros that work with signed characters */
#define IsSpace(X) isspace((unsigned char)X)
#define IsDigit(X) isdigit((unsigned char)X)
#define ToLower(X) (char)tolower((unsigned char)X)
#if !defined(_WIN32) && !defined(WIN32) && !defined(_WRS_KERNEL)
#include <sys/time.h>
#include <sys/resource.h>
/* Saved resource information for the beginning of an operation */
static struct rusage sBegin;
/*
** Begin timing an operation
*/
static void
beginTimer (void)
{
if (enableTimer)
{
getrusage (RUSAGE_SELF, &sBegin);
}
}
/* Return the difference of two time_structs in seconds */
static double
timeDiff (struct timeval *pStart, struct timeval *pEnd)
{
return (pEnd->tv_usec - pStart->tv_usec) * 0.000001 +
(double) (pEnd->tv_sec - pStart->tv_sec);
}
/*
** Print the timing results.
*/
static void
endTimer (void)
{
if (enableTimer)
{
struct rusage sEnd;
getrusage (RUSAGE_SELF, &sEnd);
printf ("CPU Time: user %f sys %f\n",
timeDiff (&sBegin.ru_utime, &sEnd.ru_utime),
timeDiff (&sBegin.ru_stime, &sEnd.ru_stime));
}
}
#define BEGIN_TIMER beginTimer()
#define END_TIMER endTimer()
#define HAS_TIMER 1
#elif (defined(_WIN32) || defined(WIN32))
#include <windows.h>
/* Saved resource information for the beginning of an operation */
static HANDLE hProcess;
static FILETIME ftKernelBegin;
static FILETIME ftUserBegin;
typedef BOOL (WINAPI * GETPROCTIMES) (HANDLE, LPFILETIME, LPFILETIME,
LPFILETIME, LPFILETIME);
static GETPROCTIMES getProcessTimesAddr = NULL;
/*
** Check to see if we have timer support. Return 1 if necessary
** support found (or found previously).
*/
static int
hasTimer (void)
{
if (getProcessTimesAddr)
{
return 1;
}
else
{
/* GetProcessTimes() isn't supported in WIN95 and some other Windows versions.
** See if the version we are running on has it, and if it does, save off
** a pointer to it and the current process handle.
*/
hProcess = GetCurrentProcess ();
if (hProcess)
{
HINSTANCE hinstLib = LoadLibrary (TEXT ("Kernel32.dll"));
if (NULL != hinstLib)
{
getProcessTimesAddr =
(GETPROCTIMES) GetProcAddress (hinstLib,
"GetProcessTimes");
if (NULL != getProcessTimesAddr)
{
return 1;
}
FreeLibrary (hinstLib);
}
}
}
return 0;
}
/*
** Begin timing an operation
*/
static void
beginTimer (void)
{
if (enableTimer && getProcessTimesAddr)
{
FILETIME ftCreation, ftExit;
getProcessTimesAddr (hProcess, &ftCreation, &ftExit, &ftKernelBegin,
&ftUserBegin);
}
}
/* Return the difference of two FILETIME structs in seconds */
static double
timeDiff (FILETIME * pStart, FILETIME * pEnd)
{
sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
return (double) ((i64End - i64Start) / 10000000.0);
}
/*
** Print the timing results.
*/
static void
endTimer (void)
{
if (enableTimer && getProcessTimesAddr)
{
FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
getProcessTimesAddr (hProcess, &ftCreation, &ftExit, &ftKernelEnd,
&ftUserEnd);
printf ("CPU Time: user %f sys %f\n",
timeDiff (&ftUserBegin, &ftUserEnd), timeDiff (&ftKernelBegin,
&ftKernelEnd));
}
}
#define BEGIN_TIMER beginTimer()
#define END_TIMER endTimer()
#define HAS_TIMER hasTimer()
#else
#define BEGIN_TIMER
#define END_TIMER
#define HAS_TIMER 0
#endif
/* sandro: 3 September 2012
** If the following flag is set, then SQL Log is enabled
*/
static int sql_log_enabled = 1;
/* end sandro: 3 September 2012 */
/*
** Used to prevent warnings about unused parameters
*/
#define UNUSED_PARAMETER(x) (void)(x)
/*
** If the following flag is set, then command execution stops
** at an error if we are not interactive.
*/
static int bail_on_error = 0;
/*
** sandro 2013-08-30
** If the following flag is set, no welcome message will be
** printed at all.
*/
static int splite_silent = 0;
/*
** Threat stdin as an interactive input if the following variable
** is true. Otherwise, assume stdin is connected to a file or pipe.
*/
static int stdin_is_interactive = 1;
/*
** The following is the open SQLite database. We make a pointer
** to this database a static variable so that it can be accessed
** by the SIGINT handler to interrupt database processing.
*/
static sqlite3 *db = 0;
/*
** True if an interrupt (Control-C) has been received.
*/
static volatile int seenInterrupt = 0;
/*
** This is the name of our program. It is set in main(), used
** in a number of other places, mostly for error messages.
*/
static char *Argv0;
/*
** Prompt strings. Initialized in main. Settable with
** .prompt main continue
*/
static char mainPrompt[20]; /* First line prompt. default: "spatialite> " */
static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
/*
** Write I/O traces to the following stream.
*/
#ifdef SQLITE_ENABLE_IOTRACE
static FILE *iotrace = 0;
#endif
/*
** This routine works like printf in that its first argument is a
** format string and subsequent arguments are values to be substituted
** in place of % fields. The result of formatting this string
** is written to iotrace.
*/
#ifdef SQLITE_ENABLE_IOTRACE
static void
iotracePrintf (const char *zFormat, ...)
{
va_list ap;
char *z;
if (iotrace == 0)
return;
va_start (ap, zFormat);
z = sqlite3_vmprintf (zFormat, ap);
va_end (ap);
fprintf (iotrace, "%s", z);
sqlite3_free (z);
}
#endif
/*
Sandro Furieri 2013-04-29
WFS progress handler callback
*/
static void
wfs_page_done (int features, void *ptr)
{
if (ptr != NULL)
ptr = NULL; /* silencing stupid compiler warnings */
if (isatty (1))
{
printf ("WFS Features loaded since now: %d\r", features);
fflush (stdout);
}
}
/*
Sandro Furieri 2008-11-20
implementing AUTO FDO
*/
struct auto_fdo_table
{
char *name;
struct auto_fdo_table *next;
};
struct auto_fdo_tables
{
struct auto_fdo_table *first;
struct auto_fdo_table *last;
};
static struct auto_fdo_tables *
fdo_tables_alloc ()
{
struct auto_fdo_tables *p = malloc (sizeof (struct auto_fdo_tables));
p->first = NULL;
p->last = NULL;
return p;
}
static void
fdo_tables_free (struct auto_fdo_tables *p)
{
struct auto_fdo_table *pt;
struct auto_fdo_table *ptn;
if (!p)
return;
pt = p->first;
while (pt)
{
ptn = pt->next;
free (pt->name);
free (pt);
pt = ptn;
}
free (p);
}
static void
add_to_fdo_tables (struct auto_fdo_tables *pt, const char *name, int len)
{
struct auto_fdo_table *p = malloc (sizeof (struct auto_fdo_table));
p->name = malloc (len + 1);
strcpy (p->name, name);
p->next = NULL;
if (!(pt->first))
pt->first = p;
if (pt->last)
pt->last->next = p;
pt->last = p;
}
static void
auto_fdo_start (sqlite3 * db)
{
/* trying to start the FDO-OGR auto-wrapper */
int ret;
const char *name;
int i;
char **results;
int rows;
int columns;
char sql[1024];
int count = 0;
int len;
int spatial_type = 0;
struct auto_fdo_tables *tables;
struct auto_fdo_table *p;
if (!db)
return;
strcpy (sql, "SELECT CheckSpatialMetadata()");
ret = sqlite3_get_table (db, sql, &results, &rows, &columns, NULL);
if (ret != SQLITE_OK)
goto error1;
if (rows < 1)
;
else
{
for (i = 1; i <= rows; i++)
spatial_type = atoi (results[(i * columns) + 0]);
}
sqlite3_free_table (results);
error1:
if (spatial_type == 2)
{
/* ok, creating VirtualFDO tables */
tables = fdo_tables_alloc ();
strcpy (sql, "SELECT DISTINCT f_table_name FROM geometry_columns");
ret = sqlite3_get_table (db, sql, &results, &rows, &columns, NULL);
if (ret != SQLITE_OK)
goto error;
if (rows < 1)
;
else
{
for (i = 1; i <= rows; i++)
{
name = results[(i * columns) + 0];
if (name)
{
len = strlen (name);
add_to_fdo_tables (tables, name, len);
}
}
}
sqlite3_free_table (results);
p = tables->first;
if (p)
printf
("\n================ FDO-OGR Spatial Metadata detected ===============\n");
while (p)
{
/* destroying the VirtualFDO table [if existing] */
sprintf (sql, "DROP TABLE IF EXISTS fdo_%s", p->name);
ret = sqlite3_exec (db, sql, NULL, 0, NULL);
if (ret != SQLITE_OK)
goto error;
/* creating the VirtualFDO table */
sprintf (sql,
"CREATE VIRTUAL TABLE fdo_%s USING VirtualFDO(%s)",
p->name, p->name);
ret = sqlite3_exec (db, sql, NULL, 0, NULL);
if (ret != SQLITE_OK)
goto error;
printf ("\tcreated VirtualFDO table 'fdo_%s'\n", p->name);
count++;
p = p->next;
}
error:
if (count++)
{
printf
("Accessing these fdo_XX tables you can take full advantage of\n");
printf ("FDO-OGR auto-wrapping facility\n");
printf
("This allows you to access any specific FDO-OGR Geometry as if it\n");
printf
("where native SpatiaLite ones in a completely transparent way\n");
printf
("==================================================================\n\n");
fdo_tables_free (tables);
}
return;
}
}
static void
auto_fdo_stop (sqlite3 * db)
{
/* trying to stop the FDO-OGR auto-wrapper */
int ret;
const char *name;
int i;
char **results;
int rows;
int columns;
char sql[1024];
int count = 0;
int len;
int spatial_type = 0;
struct auto_fdo_tables *tables;
struct auto_fdo_table *p;
if (!db)
return;
strcpy (sql, "SELECT CheckSpatialMetadata()");
ret = sqlite3_get_table (db, sql, &results, &rows, &columns, NULL);
if (ret != SQLITE_OK)
goto error1;
if (rows < 1)
;
else
{
for (i = 1; i <= rows; i++)
spatial_type = atoi (results[(i * columns) + 0]);
}
sqlite3_free_table (results);
error1:
if (spatial_type == 2)
{
/* ok, destroying VirtualFDO tables */
tables = fdo_tables_alloc ();
strcpy (sql, "SELECT DISTINCT f_table_name FROM geometry_columns");
ret = sqlite3_get_table (db, sql, &results, &rows, &columns, NULL);
if (ret != SQLITE_OK)
goto error;
if (rows < 1)
;
else
{
for (i = 1; i <= rows; i++)
{
name = results[(i * columns) + 0];
if (name)
{
len = strlen (name);
add_to_fdo_tables (tables, name, len);
}
}
}
sqlite3_free_table (results);
p = tables->first;
while (p)
{
/* destroying the VirtualFDO table [if existing] */
sprintf (sql, "DROP TABLE IF EXISTS fdo_%s", p->name);
ret = sqlite3_exec (db, sql, NULL, 0, NULL);
if (ret != SQLITE_OK)
goto error;
count++;
p = p->next;
}
error:
if (count++)
printf ("\n*** FDO-OGR auto-wrapping shutdown done ***\n");
fdo_tables_free (tables);
return;
}
}
/* end Sandro Furieri 11 July 2008 */
/*
Sandro Furieri 11 July 2008
implementing full UNICODE support
*/
static iconv_t locale_to_utf8 = NULL;
static iconv_t utf8_to_locale = NULL;
static iconv_t in_charset_to_utf8 = NULL;
static char spatialite_charset[1024] = "";
static void
create_utf8_converter (char *charset)
{
/* creating the UTF8 structs */
*spatialite_charset = '\0';
if (locale_to_utf8)
{
/* destroying old converter, if exists */
iconv_close (locale_to_utf8);
locale_to_utf8 = NULL;
}
if (utf8_to_locale)
{
/* destroying old converter, if exists */
iconv_close (utf8_to_locale);
utf8_to_locale = NULL;
}
/* creating new converters */
locale_to_utf8 = iconv_open ("UTF-8", charset);
if (locale_to_utf8 == (iconv_t) (-1))
{
locale_to_utf8 = NULL;
fprintf (stderr,
"*** charset ERROR *** cannot convert from '%s' to 'UTF-8'\n",
charset);
fflush (stderr);
return;
}
utf8_to_locale = iconv_open (charset, "UTF-8");
if (utf8_to_locale == (iconv_t) (-1))
{
utf8_to_locale = NULL;
fprintf (stderr,
"*** charset ERROR *** cannot convert from 'UTF-8' to '%s'\n",
charset);
fflush (stderr);
return;
}
strncpy (spatialite_charset, charset, sizeof (spatialite_charset) - 1);
spatialite_charset[sizeof (spatialite_charset) - 1] = '\0';
}
static void
create_input_utf8_converter (char *charset)
{
/* creating the UTF8 structs */
if (in_charset_to_utf8)
{
/* destroying old converter, if exists */
iconv_close (in_charset_to_utf8);
in_charset_to_utf8 = NULL;
}
/* creating new converter */
in_charset_to_utf8 = iconv_open ("UTF-8", charset);
if (in_charset_to_utf8 == (iconv_t) (-1))
{
in_charset_to_utf8 = NULL;
fprintf (stderr,
"*** charset ERROR *** cannot convert from '%s' to 'UTF-8'\n",
charset);
fflush (stderr);
return;
}
}
static void
convert_from_utf8 (char *buf, int maxlen)
{
/* converting from UTF8 to locale charset */
char *utf8buf = 0;
#if !defined(__MINGW32__) && defined(_WIN32)
const char *pBuf;
#else
char *pBuf;
#endif
size_t len;
size_t utf8len;
char *pUtf8buf;
if (!utf8_to_locale)
return;
utf8buf = malloc (maxlen);
if (utf8buf == 0)
{
fprintf (stderr, "out of memory!\n");
exit (1);
}
len = strlen (buf);
utf8len = maxlen;
pBuf = buf;
pUtf8buf = utf8buf;
if (iconv (utf8_to_locale, &pBuf, &len, &pUtf8buf, &utf8len) ==
(size_t) (-1))
{
fprintf (stderr, "\n*** ILLEGAL CHARACTER SEQUENCE ***\n\n");
fflush (stderr);
free (utf8buf);
return;
}
utf8buf[maxlen - utf8len] = '\0';
memcpy (buf, utf8buf, (maxlen - utf8len) + 1);
free (utf8buf);
}
static void
convert_to_utf8 (char *buf, int maxlen)
{
/* converting from locale charset to UTF8 */
char *utf8buf = 0;
#if !defined(__MINGW32__) && defined(_WIN32)
const char *pBuf;
#else
char *pBuf;
#endif
size_t len;
size_t utf8len;
char *pUtf8buf;
if (!locale_to_utf8)
return;
utf8buf = malloc (maxlen);
if (utf8buf == 0)
{
fprintf (stderr, "out of memory!\n");
exit (1);
}
len = strlen (buf);
utf8len = maxlen;
pBuf = buf;
pUtf8buf = utf8buf;
if (iconv (locale_to_utf8, &pBuf, &len, &pUtf8buf, &utf8len) ==
(size_t) (-1))
{
fprintf (stderr, "\n*** ILLEGAL CHARACTER SEQUENCE ***\n\n");
fflush (stderr);
free (utf8buf);
return;
}
utf8buf[maxlen - utf8len] = '\0';
memcpy (buf, utf8buf, (maxlen - utf8len) + 1);
free (utf8buf);
}
/* sandro 2013-11-17 */
static void
split_drop_name (const char *str, char **prefix, char **table)
{
int len1;
int len2;
const char *pt = NULL;
const char *p = str;
*prefix = NULL;
*table = NULL;
while (*p != '\0')
{
if (*p == '.')
{
pt = p;
break;
}
p++;
}
if (pt == NULL)
return;
len1 = pt - str;
len2 = strlen (pt + 1);
if (len1 > 0 && len2 > 0)
{
*prefix = malloc (len1 + 1);
memcpy (*prefix, str, len1);
*(*prefix + len1) = '\0';
*table = malloc (len2 + 1);
strcpy (*table, pt + 1);
}
}
/* end sandro 2013-11-17 */
static void
convert_input_to_utf8 (char *buf, int maxlen)
{
/* converting from required charset to UTF8 */
char *utf8buf = 0;
#if !defined(__MINGW32__) && defined(_WIN32)
const char *pBuf;
#else
char *pBuf;
#endif
size_t len;
size_t utf8len;
char *pUtf8buf;
if (!in_charset_to_utf8)
return;
utf8buf = malloc (maxlen);
if (utf8buf == 0)
{
fprintf (stderr, "out of memory!\n");
exit (1);
}
len = strlen (buf);
utf8len = maxlen;
pBuf = buf;
pUtf8buf = utf8buf;
if (iconv (in_charset_to_utf8, &pBuf, &len, &pUtf8buf, &utf8len) ==
(size_t) (-1))
{
fprintf (stderr, "\n*** ILLEGAL CHARACTER SEQUENCE ***\n\n");
fflush (stderr);
free (utf8buf);
return;
}
utf8buf[maxlen - utf8len] = '\0';
memcpy (buf, utf8buf, (maxlen - utf8len) + 1);
free (utf8buf);
}
/* end Sandro Furieri 11 July 2008 */
/*
** Determines if a string is a number of not.
*/
static int
isNumber (const char *z, int *realnum)
{
if (*z == '-' || *z == '+')
z++;
if (!IsDigit (*z))
{
return 0;
}
z++;
if (realnum)
*realnum = 0;
while (IsDigit (*z))
{
z++;
}
if (*z == '.')
{
z++;
if (!IsDigit (*z))
return 0;
while (IsDigit (*z))
{
z++;
}
if (realnum)
*realnum = 1;
}
if (*z == 'e' || *z == 'E')
{
z++;
if (*z == '+' || *z == '-')
z++;
if (!IsDigit (*z))
return 0;
while (IsDigit (*z))
{
z++;
}
if (realnum)
*realnum = 1;
}
return *z == 0;
}
/*
** A global char* and an SQL function to access its current value
** from within an SQL statement. This program used to use the
** sqlite_exec_printf() API to substitue a string into an SQL statement.
** The correct way to do this with sqlite3 is to use the bind API, but
** since the shell is built around the callback paradigm it would be a lot
** of work. Instead just use this hack, which is quite harmless.
*/
static const char *zShellStatic = 0;
static void
shellstaticFunc (sqlite3_context * context, int argc, sqlite3_value ** argv)
{
assert (0 == argc);
assert (zShellStatic);
UNUSED_PARAMETER (argc);
UNUSED_PARAMETER (argv);
sqlite3_result_text (context, zShellStatic, -1, SQLITE_STATIC);
}
/*
** This routine reads a line of text from FILE in, stores
** the text in memory obtained from malloc() and returns a pointer
** to the text. NULL is returned at end of file, or if malloc()
** fails.
**
** The interface is like "readline" but no command-line editing
** is done.
*/
static char *
local_getline (char *zPrompt, FILE * in, int csvFlag)
{
char *zLine;
int nLine;
int n;
int inQuote = 0;
if (zPrompt && *zPrompt)
{
printf ("%s", zPrompt);
fflush (stdout);
}
nLine = 100;
zLine = malloc (nLine);
if (zLine == 0)
return 0;
n = 0;
while (1)
{
if (n + 100 > nLine)
{
nLine = nLine * 2 + 100;
zLine = realloc (zLine, nLine);
if (zLine == 0)
return 0;
}
if (fgets (&zLine[n], nLine - n, in) == 0)
{
if (n == 0)
{
free (zLine);
return 0;
}
zLine[n] = 0;
break;
}
while (zLine[n])
{
if (zLine[n] == '"')
inQuote = !inQuote;
n++;
}
if (n > 0 && zLine[n - 1] == '\n' && (!inQuote || !csvFlag))
{
n--;
if (n > 0 && zLine[n - 1] == '\r')
n--;
zLine[n] = 0;
break;
}
}
zLine = realloc (zLine, n + 1);
return zLine;
}
/*
** Retrieve a single line of input text.
**
** zPrior is a string of prior text retrieved. If not the empty
** string, then issue a continuation prompt.
*/
static char *
one_input_line (const char *zPrior, FILE * in)
{
char *zPrompt;
char *zResult;
if (in != 0)
{
return local_getline (0, in, 0);
}
if (zPrior && zPrior[0])
{
zPrompt = continuePrompt;
}
else
{
zPrompt = mainPrompt;
}
zResult = readline (zPrompt);
#if defined(HAVE_READLINE) && HAVE_READLINE==1
if (zResult && *zResult)
add_history (zResult);
#endif
return zResult;
}