FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlcipher/src/shell.c at sqlite-release · PHPDOTSQL/sqlcipher · GitHub
PHPDOTSQL
/
sqlcipher
Public
forked from
sqlcipher/sqlcipher
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlcipher
/
src
/
shell.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
3233 lines (3049 loc) · 97.1 KB
Breadcrumbs
sqlcipher
/
src
/
shell.c
Copy path
File metadata and controls
3233 lines (3049 loc) · 97.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code to implement the "sqlite" command line
** utility for accessing SQLite databases.
*/
#if
(defined(
_WIN32
)
||
defined(
WIN32
))
&&
!defined(
_CRT_SECURE_NO_WARNINGS
)
/* This needs to come before any includes for MSVC compiler */
#define
_CRT_SECURE_NO_WARNINGS
#endif
/*
** Enable large-file support for fopen() and friends on unix.
*/
#ifndef
SQLITE_DISABLE_LFS
# define
_LARGE_FILE
1
# ifndef
_FILE_OFFSET_BITS
# define
_FILE_OFFSET_BITS
64
# endif
# define
_LARGEFILE_SOURCE
1
#endif
#include
<stdlib.h>
#include
<string.h>
#include
<stdio.h>
#include
<assert.h>
#include
"sqlite3.h"
#include
<ctype.h>
#include
<stdarg.h>
#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
;
/* 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
) \
&&
!defined(
__minux
)
#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
/*
** 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
;
/*
** 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: "sqlite> "*/
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
/*
** 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
;
}
struct
previous_mode_data
{
int
valid
;
/* Is there legit data in here? */
int
mode
;
int
showHeader
;
int
colWidth
[
100
];
};
/*
** An pointer to an instance of this structure is passed from
** the main program to the callback. This is used to communicate
** state and mode information.
*/
struct
callback_data
{
sqlite3
*
db
;
/* The database */
int
echoOn
;
/* True to echo input commands */
int
statsOn
;
/* True to display memory stats before each finalize */
int
cnt
;
/* Number of records displayed so far */
FILE
*
out
;
/* Write results here */
FILE
*
traceOut
;
/* Output for sqlite3_trace() */
int
nErr
;
/* Number of errors seen */
int
mode
;
/* An output mode setting */
int
writableSchema
;
/* True if PRAGMA writable_schema=ON */
int
showHeader
;
/* True to show column names in List or Column mode */
char
*
zDestTable
;
/* Name of destination table when MODE_Insert */
char
separator
[
20
];
/* Separator character for MODE_List */
int
colWidth
[
100
];
/* Requested width of each column when in column mode*/
int
actualWidth
[
100
];
/* Actual width of each column */
char
nullvalue
[
20
];
/* The text to print when a NULL comes back from
** the database */
struct
previous_mode_data
explainPrev
;
/* Holds the mode information just before
** .explain ON */
char
outfile
[
FILENAME_MAX
];
/* Filename for *out */
const
char
*
zDbFilename
;
/* name of the database file */
const
char
*
zVfs
;
/* Name of VFS to use */
sqlite3_stmt
*
pStmt
;
/* Current statement if any. */
FILE
*
pLog
;
/* Write log output here */
};
/*
** These are the allowed modes.
*/
#define
MODE_Line
0
/* One column per line. Blank line between records */
#define
MODE_Column
1
/* One record per line in neat columns */
#define
MODE_List
2
/* One record per line with a separator */
#define
MODE_Semi
3
/* Same as MODE_List but append ";" to each line */
#define
MODE_Html
4
/* Generate an XHTML table */
#define
MODE_Insert
5
/* Generate SQL "insert" statements */
#define
MODE_Tcl
6
/* Generate ANSI-C or TCL quoted elements */
#define
MODE_Csv
7
/* Quote strings, numbers are plain */
#define
MODE_Explain
8
/* Like MODE_Column, but do not truncate data */
static
const
char
*
modeDescr
[]
=
{
"line"
,
"column"
,
"list"
,
"semi"
,
"html"
,
"insert"
,
"tcl"
,
"csv"
,
"explain"
,
};
/*
** Number of elements in an array
*/
#define
ArraySize
(
X
) (int)(sizeof(X)/sizeof(X[0]))
/*
** Compute a string length that is limited to what can be stored in
** lower 30 bits of a 32-bit signed integer.
*/
static
int
strlen30
(
const
char
*
z
){
const
char
*
z2
=
z
;
while
(
*
z2
){
z2
++
; }
return
0x3fffffff
&
(
int
)(
z2
-
z
);
}
/*
** A callback for the sqlite3_log() interface.
*/
static
void
shellLog
(
void
*
pArg
,
int
iErrCode
,
const
char
*
zMsg
){
struct
callback_data
*
p
=
(
struct
callback_data
*
)
pArg
;
if
(
p
->
pLog
==
0
)
return
;
fprintf
(
p
->
pLog
,
"(%d) %s\n"
,
iErrCode
,
zMsg
);
fflush
(
p
->
pLog
);
}
/*
** Output the given string as a hex-encoded blob (eg. X'1234' )
*/
static
void
output_hex_blob
(
FILE
*
out
,
const
void
*
pBlob
,
int
nBlob
){
int
i
;
char
*
zBlob
=
(
char
*
)
pBlob
;
fprintf
(
out
,
"X'"
);
for
(
i
=
0
;
i
<
nBlob
;
i
++
){
fprintf
(
out
,
"%02x"
,
zBlob
[
i
]
&
0xff
); }
fprintf
(
out
,
"'"
);
}
/*
** Output the given string as a quoted string using SQL quoting conventions.
*/
static
void
output_quoted_string
(
FILE
*
out
,
const
char
*
z
){
int
i
;
int
nSingle
=
0
;
for
(
i
=
0
;
z
[
i
];
i
++
){
if
(
z
[
i
]
==
'\''
)
nSingle
++
;
}
if
(
nSingle
==
0
){
fprintf
(
out
,
"'%s'"
,
z
);
}
else
{
fprintf
(
out
,
"'"
);
while
(
*
z
){
for
(
i
=
0
;
z
[
i
]
&&
z
[
i
]
!=
'\''
;
i
++
){}
if
(
i
==
0
){
fprintf
(
out
,
"''"
);
z
++
;
}
else
if
(
z
[
i
]
==
'\''
){
fprintf
(
out
,
"%.*s''"
,
i
,
z
);
z
+=
i
+
1
;
}
else
{
fprintf
(
out
,
"%s"
,
z
);
break
;
}
}
fprintf
(
out
,
"'"
);
}
}
/*
** Output the given string as a quoted according to C or TCL quoting rules.
*/
static
void
output_c_string
(
FILE
*
out
,
const
char
*
z
){
unsigned
int
c
;
fputc
(
'"'
,
out
);
while
( (
c
=
*
(
z
++
))
!=
0
){
if
(
c
==
'\\'
){
fputc
(
c
,
out
);
fputc
(
c
,
out
);
}
else
if
(
c
==
'"'
){
fputc
(
'\\'
,
out
);
fputc
(
'"'
,
out
);
}
else
if
(
c
==
'\t'
){
fputc
(
'\\'
,
out
);
fputc
(
't'
,
out
);
}
else
if
(
c
==
'\n'
){
fputc
(
'\\'
,
out
);
fputc
(
'n'
,
out
);
}
else
if
(
c
==
'\r'
){
fputc
(
'\\'
,
out
);
fputc
(
'r'
,
out
);
}
else
if
( !
isprint
(
c
) ){
fprintf
(
out
,
"\\%03o"
,
c
&
0xff
);
}
else
{
fputc
(
c
,
out
);
}
}
fputc
(
'"'
,
out
);
}
/*
** Output the given string with characters that are special to
** HTML escaped.
*/
static
void
output_html_string
(
FILE
*
out
,
const
char
*
z
){
int
i
;
while
(
*
z
){
for
(
i
=
0
;
z
[
i
]
&&
z
[
i
]
!=
'<'
&&
z
[
i
]
!=
'&'
&&
z
[
i
]
!=
'>'
&&
z
[
i
]
!=
'\"'
&&
z
[
i
]
!=
'\''
;
i
++
){}
if
(
i
>
0
){
fprintf
(
out
,
"%.*s"
,
i
,
z
);
}
if
(
z
[
i
]
==
'<'
){
fprintf
(
out
,
"<"
);
}
else
if
(
z
[
i
]
==
'&'
){
fprintf
(
out
,
"&"
);
}
else
if
(
z
[
i
]
==
'>'
){
fprintf
(
out
,
">"
);
}
else
if
(
z
[
i
]
==
'\"'
){
fprintf
(
out
,
"""
);
}
else
if
(
z
[
i
]
==
'\''
){
fprintf
(
out
,
"'"
);
}
else
{
break
;
}
z
+=
i
+
1
;
}
}
/*
** If a field contains any character identified by a 1 in the following
** array, then the string must be quoted for CSV.
*/
static
const
char
needCsvQuote
[]
=
{
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
0
,
1
,
0
,
0
,
0
,
0
,
1
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
};
/*
** Output a single term of CSV. Actually, p->separator is used for
** the separator, which may or may not be a comma. p->nullvalue is
** the null value. Strings are quoted if necessary.
*/
static
void
output_csv
(
struct
callback_data
*
p
,
const
char
*
z
,
int
bSep
){
FILE
*
out
=
p
->
out
;
if
(
z
==
0
){
fprintf
(
out
,
"%s"
,
p
->
nullvalue
);
}
else
{
int
i
;
int
nSep
=
strlen30
(
p
->
separator
);
for
(
i
=
0
;
z
[
i
];
i
++
){
if
(
needCsvQuote
[((
unsigned
char
*
)
z
)[
i
]]
||
(
z
[
i
]
==
p
->
separator
[
0
]
&&
(
nSep
==
1
||
memcmp
(
z
,
p
->
separator
,
nSep
)
==
0
)) ){
i
=
0
;
break
;
}
}
if
(
i
==
0
){
putc
(
'"'
,
out
);
for
(
i
=
0
;
z
[
i
];
i
++
){
if
(
z
[
i
]
==
'"'
)
putc
(
'"'
,
out
);
putc
(
z
[
i
],
out
);
}
putc
(
'"'
,
out
);
}
else
{
fprintf
(
out
,
"%s"
,
z
);
}
}
if
(
bSep
){
fprintf
(
p
->
out
,
"%s"
,
p
->
separator
);
}
}
#ifdef
SIGINT
/*
** This routine runs when the user presses Ctrl-C
*/
static
void
interrupt_handler
(
int
NotUsed
){
UNUSED_PARAMETER
(
NotUsed
);
seenInterrupt
=
1
;
if
(
db
)
sqlite3_interrupt
(
db
);
}
#endif
/*
** This is the callback routine that the shell
** invokes for each row of a query result.
*/
static
int
shell_callback
(
void
*
pArg
,
int
nArg
,
char
*
*
azArg
,
char
*
*
azCol
,
int
*
aiType
){
int
i
;
struct
callback_data
*
p
=
(
struct
callback_data
*
)
pArg
;
switch
(
p
->
mode
){
case
MODE_Line
: {
int
w
=
5
;
if
(
azArg
==
0
)
break
;
for
(
i
=
0
;
i
<
nArg
;
i
++
){
int
len
=
strlen30
(
azCol
[
i
] ?
azCol
[
i
] :
""
);
if
(
len
>
w
)
w
=
len
;
}
if
(
p
->
cnt
++
>
0
)
fprintf
(
p
->
out
,
"\n"
);
for
(
i
=
0
;
i
<
nArg
;
i
++
){
fprintf
(
p
->
out
,
"%*s = %s\n"
,
w
,
azCol
[
i
],
azArg
[
i
] ?
azArg
[
i
] :
p
->
nullvalue
);
}
break
;
}
case
MODE_Explain
:
case
MODE_Column
: {
if
(
p
->
cnt
++
==
0
){
for
(
i
=
0
;
i
<
nArg
;
i
++
){
int
w
,
n
;
if
(
i
<
ArraySize
(
p
->
colWidth
) ){
w
=
p
->
colWidth
[
i
];
}
else
{
w
=
0
;
}
if
(
w
==
0
){
w
=
strlen30
(
azCol
[
i
] ?
azCol
[
i
] :
""
);
if
(
w
<
10
)
w
=
10
;
n
=
strlen30
(
azArg
&&
azArg
[
i
] ?
azArg
[
i
] :
p
->
nullvalue
);
if
(
w
<
n
)
w
=
n
;
}
if
(
i
<
ArraySize
(
p
->
actualWidth
) ){
p
->
actualWidth
[
i
]
=
w
;
}
if
(
p
->
showHeader
){
if
(
w
<
0
){
fprintf
(
p
->
out
,
"%*.*s%s"
,
-
w
,
-
w
,
azCol
[
i
],
i
==
nArg
-
1
?
"\n"
:
" "
);
}
else
{
fprintf
(
p
->
out
,
"%-*.*s%s"
,
w
,
w
,
azCol
[
i
],
i
==
nArg
-
1
?
"\n"
:
" "
);
}
}
}
if
(
p
->
showHeader
){
for
(
i
=
0
;
i
<
nArg
;
i
++
){
int
w
;
if
(
i
<
ArraySize
(
p
->
actualWidth
) ){
w
=
p
->
actualWidth
[
i
];
if
(
w
<
0
)
w
=
-
w
;
}
else
{
w
=
10
;
}
fprintf
(
p
->
out
,
"%-*.*s%s"
,
w
,
w
,
"-----------------------------------"
"----------------------------------------------------------"
,
i
==
nArg
-
1
?
"\n"
:
" "
);
}
}
}
if
(
azArg
==
0
)
break
;
for
(
i
=
0
;
i
<
nArg
;
i
++
){
int
w
;
if
(
i
<
ArraySize
(
p
->
actualWidth
) ){
w
=
p
->
actualWidth
[
i
];
}
else
{
w
=
10
;
}
if
(
p
->
mode
==
MODE_Explain
&&
azArg
[
i
]
&&
strlen30
(
azArg
[
i
])
>
w
){
w
=
strlen30
(
azArg
[
i
]);
}
if
(
w
<
0
){
fprintf
(
p
->
out
,
"%*.*s%s"
,
-
w
,
-
w
,
azArg
[
i
] ?
azArg
[
i
] :
p
->
nullvalue
,
i
==
nArg
-
1
?
"\n"
:
" "
);
}
else
{
fprintf
(
p
->
out
,
"%-*.*s%s"
,
w
,
w
,
azArg
[
i
] ?
azArg
[
i
] :
p
->
nullvalue
,
i
==
nArg
-
1
?
"\n"
:
" "
);
}
}
break
;
}
case
MODE_Semi
:
case
MODE_List
: {
if
(
p
->
cnt
++
==
0
&&
p
->
showHeader
){
for
(
i
=
0
;
i
<
nArg
;
i
++
){
fprintf
(
p
->
out
,
"%s%s"
,
azCol
[
i
],
i
==
nArg
-
1
?
"\n"
:
p
->
separator
);
}
}
if
(
azArg
==
0
)
break
;
for
(
i
=
0
;
i
<
nArg
;
i
++
){
char
*
z
=
azArg
[
i
];
if
(
z
==
0
)
z
=
p
->
nullvalue
;
fprintf
(
p
->
out
,
"%s"
,
z
);
if
(
i
<
nArg
-
1
){
fprintf
(
p
->
out
,
"%s"
,
p
->
separator
);
}
else
if
(
p
->
mode
==
MODE_Semi
){
fprintf
(
p
->
out
,
";\n"
);
}
else
{
fprintf
(
p
->
out
,
"\n"
);
}
}
break
;
}
case
MODE_Html
: {
if
(
p
->
cnt
++
==
0
&&
p
->
showHeader
){
fprintf
(
p
->
out
,
"<TR>"
);
for
(
i
=
0
;
i
<
nArg
;
i
++
){
fprintf
(
p
->
out
,
"<TH>"
);
output_html_string
(
p
->
out
,
azCol
[
i
]);
fprintf
(
p
->
out
,
"</TH>\n"
);
}
fprintf
(
p
->
out
,
"</TR>\n"
);
}
if
(
azArg
==
0
)
break
;
fprintf
(
p
->
out
,
"<TR>"
);
for
(
i
=
0
;
i
<
nArg
;
i
++
){
fprintf
(
p
->
out
,
"<TD>"
);
output_html_string
(
p
->
out
,
azArg
[
i
] ?
azArg
[
i
] :
p
->
nullvalue
);
fprintf
(
p
->
out
,
"</TD>\n"
);
}
fprintf
(
p
->
out
,
"</TR>\n"
);
break
;
}
case
MODE_Tcl
: {
if
(
p
->
cnt
++
==
0
&&
p
->
showHeader
){
for
(
i
=
0
;
i
<
nArg
;
i
++
){
output_c_string
(
p
->
out
,
azCol
[
i
] ?
azCol
[
i
] :
""
);
if
(
i
<
nArg
-
1
)
fprintf
(
p
->
out
,
"%s"
,
p
->
separator
);
}
fprintf
(
p
->
out
,
"\n"
);
}
if
(
azArg
==
0
)
break
;
for
(
i
=
0
;
i
<
nArg
;
i
++
){
output_c_string
(
p
->
out
,
azArg
[
i
] ?
azArg
[
i
] :
p
->
nullvalue
);
if
(
i
<
nArg
-
1
)
fprintf
(
p
->
out
,
"%s"
,
p
->
separator
);
}
fprintf
(
p
->
out
,
"\n"
);
break
;
}
case
MODE_Csv
: {
if
(
p
->
cnt
++
==
0
&&
p
->
showHeader
){
for
(
i
=
0
;
i
<
nArg
;
i
++
){
output_csv
(
p
,
azCol
[
i
] ?
azCol
[
i
] :
""
,
i
<
nArg
-
1
);
}
fprintf
(
p
->
out
,
"\n"
);
}
if
(
azArg
==
0
)
break
;
for
(
i
=
0
;
i
<
nArg
;
i
++
){
output_csv
(
p
,
azArg
[
i
],
i
<
nArg
-
1
);
}
fprintf
(
p
->
out
,
"\n"
);
break
;
}
case
MODE_Insert
: {
p
->
cnt
++
;
if
(
azArg
==
0
)
break
;
fprintf
(
p
->
out
,
"INSERT INTO %s VALUES("
,
p
->
zDestTable
);
for
(
i
=
0
;
i
<
nArg
;
i
++
){
char
*
zSep
=
i
>
0
?
","
:
""
;
if
( (
azArg
[
i
]
==
0
)
||
(
aiType
&&
aiType
[
i
]
==
SQLITE_NULL
) ){
fprintf
(
p
->
out
,
"%sNULL"
,
zSep
);
}
else
if
(
aiType
&&
aiType
[
i
]
==
SQLITE_TEXT
){
if
(
zSep
[
0
] )
fprintf
(
p
->
out
,
"%s"
,
zSep
);
output_quoted_string
(
p
->
out
,
azArg
[
i
]);
}
else
if
(
aiType
&&
(
aiType
[
i
]
==
SQLITE_INTEGER
||
aiType
[
i
]
==
SQLITE_FLOAT
) ){
fprintf
(
p
->
out
,
"%s%s"
,
zSep
,
azArg
[
i
]);
}
else
if
(
aiType
&&
aiType
[
i
]
==
SQLITE_BLOB
&&
p
->
pStmt
){
const
void
*
pBlob
=
sqlite3_column_blob
(
p
->
pStmt
,
i
);
int
nBlob
=
sqlite3_column_bytes
(
p
->
pStmt
,
i
);
if
(
zSep
[
0
] )
fprintf
(
p
->
out
,
"%s"
,
zSep
);
output_hex_blob
(
p
->
out
,
pBlob
,
nBlob
);
}
else
if
(
isNumber
(
azArg
[
i
],
0
) ){
fprintf
(
p
->
out
,
"%s%s"
,
zSep
,
azArg
[
i
]);
}
else
{
if
(
zSep
[
0
] )
fprintf
(
p
->
out
,
"%s"
,
zSep
);
output_quoted_string
(
p
->
out
,
azArg
[
i
]);
}
}
fprintf
(
p
->
out
,
");\n"
);
break
;
}
}
return
0
;
}
/*
** This is the callback routine that the SQLite library
** invokes for each row of a query result.
*/
static
int
callback
(
void
*
pArg
,
int
nArg
,
char
*
*
azArg
,
char
*
*
azCol
){
/* since we don't have type info, call the shell_callback with a NULL value */
return
shell_callback
(
pArg
,
nArg
,
azArg
,
azCol
,
NULL
);
}
/*
** Set the destination table field of the callback_data structure to
** the name of the table given. Escape any quote characters in the
** table name.
*/
static
void
set_table_name
(
struct
callback_data
*
p
,
const
char
*
zName
){
int
i
,
n
;
int
needQuote
;
char
*
z
;
if
(
p
->
zDestTable
){
free
(
p
->
zDestTable
);
p
->
zDestTable
=
0
;
}
if
(
zName
==
0
)
return
;
needQuote
=
!
isalpha
((
unsigned
char
)
*
zName
)
&&
*
zName
!=
'_'
;
for
(
i
=
n
=
0
;
zName
[
i
];
i
++
,
n
++
){
if
( !
isalnum
((
unsigned
char
)
zName
[
i
])
&&
zName
[
i
]
!=
'_'
){
needQuote
=
1
;
if
(
zName
[
i
]
==
'\''
)
n
++
;
}
}
if
(
needQuote
)
n
+=
2
;
z
=
p
->
zDestTable
=
malloc
(
n
+
1
);
if
(
z
==
0
){
fprintf
(
stderr
,
"Error: out of memory\n"
);
exit
(
1
);
}
n
=
0
;
if
(
needQuote
)
z
[
n
++
]
=
'\''
;
for
(
i
=
0
;
zName
[
i
];
i
++
){
z
[
n
++
]
=
zName
[
i
];
if
(
zName
[
i
]
==
'\''
)
z
[
n
++
]
=
'\''
;
}
if
(
needQuote
)
z
[
n
++
]
=
'\''
;
z
[
n
]
=
0
;
}
/* zIn is either a pointer to a NULL-terminated string in memory obtained
** from malloc(), or a NULL pointer. The string pointed to by zAppend is
** added to zIn, and the result returned in memory obtained from malloc().
** zIn, if it was not NULL, is freed.
**
** If the third argument, quote, is not '\0', then it is used as a
** quote character for zAppend.
*/
static
char
*
appendText
(
char
*
zIn
,
char
const
*
zAppend
,
char
quote
){
int
len
;
int
i
;
int
nAppend
=
strlen30
(
zAppend
);
int
nIn
=
(
zIn
?
strlen30
(
zIn
):
0
);
len
=
nAppend
+
nIn
+
1
;
if
(
quote
){
len
+=
2
;
for
(
i
=
0
;
i
<
nAppend
;
i
++
){
if
(
zAppend
[
i
]
==
quote
)
len
++
;
}
}
zIn
=
(
char
*
)
realloc
(
zIn
,
len
);
if
( !
zIn
){
return
0
;
}
if
(
quote
){
char
*
zCsr
=
&
zIn
[
nIn
];
*
zCsr
++
=
quote
;
for
(
i
=
0
;
i
<
nAppend
;
i
++
){
*
zCsr
++
=
zAppend
[
i
];
if
(
zAppend
[
i
]
==
quote
)
*
zCsr
++
=
quote
;
}
*
zCsr
++
=
quote
;
*
zCsr
++
=
'\0'
;
assert
( (
zCsr
-
zIn
)
==
len
);
}
else
{
memcpy
(
&
zIn
[
nIn
],
zAppend
,
nAppend
);
zIn
[
len
-
1
]
=
'\0'
;
}
return
zIn
;
}
/*
** Execute a query statement that will generate SQL output. Print
** the result columns, comma-separated, on a line and then add a
** semicolon terminator to the end of that line.
**
** If the number of columns is 1 and that column contains text "--"
** then write the semicolon on a separate line. That way, if a
** "--" comment occurs at the end of the statement, the comment
** won't consume the semicolon terminator.
*/
static
int
run_table_dump_query
(
struct
callback_data
*
p
,
/* Query context */
const
char
*
zSelect
,
/* SELECT statement to extract content */
const
char
*
zFirstRow
/* Print before first row, if not NULL */
){
sqlite3_stmt
*
pSelect
;
int
rc
;
int
nResult
;
int
i
;
const
char
*
z
;
rc
=
sqlite3_prepare
(
p
->
db
,
zSelect
,
-1
,
&
pSelect
,
0
);
if
(
rc
!=
SQLITE_OK
||
!
pSelect
){
fprintf
(
p
->
out
,
"/**** ERROR: (%d) %s *****/\n"
,
rc
,
sqlite3_errmsg
(
p
->
db
));
p
->
nErr
++
;
return
rc
;
}
rc
=
sqlite3_step
(
pSelect
);
nResult
=
sqlite3_column_count
(
pSelect
);
while
(
rc
==
SQLITE_ROW
){
if
(
zFirstRow
){
fprintf
(
p
->
out
,
"%s"
,
zFirstRow
);
zFirstRow
=
0
;
}
z
=
(
const
char
*
)
sqlite3_column_text
(
pSelect
,
0
);
fprintf
(
p
->
out
,
"%s"
,
z
);
for
(
i
=
1
;
i
<
nResult
;
i
++
){
fprintf
(
p
->
out
,
",%s"
,
sqlite3_column_text
(
pSelect
,
i
));
}
if
(
z
==
0
)
z
=
""
;
while
(
z
[
0
]
&&
(
z
[
0
]
!=
'-'
||
z
[
1
]
!=
'-'
) )
z
++
;
if
(
z
[
0
] ){
fprintf
(
p
->
out
,
"\n;\n"
);
}
else
{
fprintf
(
p
->
out
,
";\n"
);
}
rc
=
sqlite3_step
(
pSelect
);
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL