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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
|
/*
* psql - the PostgreSQL interactive terminal
*
* Copyright (c) 2000-2023, PostgreSQL Global Development Group
*
* src/bin/psql/startup.c
*/
#include "postgres_fe.h"
#ifndef WIN32
#include <unistd.h>
#else /* WIN32 */
#include <io.h>
#include <win32.h>
#endif /* WIN32 */
#include "command.h"
#include "common.h"
#include "common/logging.h"
#include "common/string.h"
#include "describe.h"
#include "fe_utils/print.h"
#include "getopt_long.h"
#include "help.h"
#include "input.h"
#include "mainloop.h"
#include "settings.h"
/*
* Global psql options
*/
PsqlSettings pset;
#ifndef WIN32
#define SYSPSQLRC "psqlrc"
#define PSQLRC ".psqlrc"
#else
#define SYSPSQLRC "psqlrc"
#define PSQLRC "psqlrc.conf"
#endif
/*
* Structures to pass information between the option parsing routine
* and the main function
*/
enum _actions
{
ACT_SINGLE_QUERY,
ACT_SINGLE_SLASH,
ACT_FILE
};
typedef struct SimpleActionListCell
{
struct SimpleActionListCell *next;
enum _actions action;
char *val;
} SimpleActionListCell;
typedef struct SimpleActionList
{
SimpleActionListCell *head;
SimpleActionListCell *tail;
} SimpleActionList;
struct adhoc_opts
{
char *dbname;
char *host;
char *port;
char *username;
char *logfilename;
bool no_readline;
bool no_psqlrc;
bool single_txn;
bool list_dbs;
SimpleActionList actions;
};
static void parse_psql_options(int argc, char *argv[],
struct adhoc_opts *options);
static void simple_action_list_append(SimpleActionList *list,
enum _actions action, const char *val);
static void process_psqlrc(char *argv0);
static void process_psqlrc_file(char *filename);
static void showVersion(void);
static void EstablishVariableSpace(void);
#define NOPAGER 0
static void
log_pre_callback(void)
{
if (pset.queryFout && pset.queryFout != stdout)
fflush(pset.queryFout);
}
static void
log_locus_callback(const char **filename, uint64 *lineno)
{
if (pset.inputfile)
{
*filename = pset.inputfile;
*lineno = pset.lineno;
}
else
{
*filename = NULL;
*lineno = 0;
}
}
#ifndef WIN32
static void
empty_signal_handler(SIGNAL_ARGS)
{
}
#endif
/*
*
* main
*
*/
int
main(int argc, char *argv[])
{
struct adhoc_opts options;
int successResult;
char *password = NULL;
bool new_pass;
pg_logging_init(argv[0]);
pg_logging_set_pre_callback(log_pre_callback);
pg_logging_set_locus_callback(log_locus_callback);
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
if (argc > 1)
{
if ((strcmp(argv[1], "-?") == 0) || (argc == 2 && (strcmp(argv[1], "--help") == 0)))
{
usage(NOPAGER);
exit(EXIT_SUCCESS);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
showVersion();
exit(EXIT_SUCCESS);
}
}
pset.progname = get_progname(argv[0]);
pset.db = NULL;
pset.dead_conn = NULL;
setDecimalLocale();
pset.encoding = PQenv2encoding();
pset.queryFout = stdout;
pset.queryFoutPipe = false;
pset.copyStream = NULL;
pset.last_error_result = NULL;
pset.cur_cmd_source = stdin;
pset.cur_cmd_interactive = false;
/* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
pset.popt.topt.format = PRINT_ALIGNED;
pset.popt.topt.border = 1;
pset.popt.topt.pager = 1;
pset.popt.topt.pager_min_lines = 0;
pset.popt.topt.start_table = true;
pset.popt.topt.stop_table = true;
pset.popt.topt.default_footer = true;
pset.popt.topt.csvFieldSep[0] = DEFAULT_CSV_FIELD_SEP;
pset.popt.topt.csvFieldSep[1] = '\0';
pset.popt.topt.unicode_border_linestyle = UNICODE_LINESTYLE_SINGLE;
pset.popt.topt.unicode_column_linestyle = UNICODE_LINESTYLE_SINGLE;
pset.popt.topt.unicode_header_linestyle = UNICODE_LINESTYLE_SINGLE;
refresh_utf8format(&(pset.popt.topt));
/* We must get COLUMNS here before readline() sets it */
pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
pset.getPassword = TRI_DEFAULT;
EstablishVariableSpace();
/* Create variables showing psql version number */
SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
SetVariable(pset.vars, "VERSION_NAME", PG_VERSION);
SetVariable(pset.vars, "VERSION_NUM", CppAsString2(PG_VERSION_NUM));
/* Initialize variables for last error */
SetVariable(pset.vars, "LAST_ERROR_MESSAGE", "");
SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", "00000");
/* Default values for variables (that don't match the result of \unset) */
SetVariableBool(pset.vars, "AUTOCOMMIT");
SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
SetVariableBool(pset.vars, "SHOW_ALL_RESULTS");
parse_psql_options(argc, argv, &options);
/*
* If no action was specified and we're in non-interactive mode, treat it
* as if the user had specified "-f -". This lets single-transaction mode
* work in this case.
*/
if (options.actions.head == NULL && pset.notty)
simple_action_list_append(&options.actions, ACT_FILE, NULL);
/* Bail out if -1 was specified but will be ignored. */
if (options.single_txn && options.actions.head == NULL)
pg_fatal("-1 can only be used in non-interactive mode");
if (!pset.popt.topt.fieldSep.separator &&
!pset.popt.topt.fieldSep.separator_zero)
{
pset.popt.topt.fieldSep.separator = pg_strdup(DEFAULT_FIELD_SEP);
pset.popt.topt.fieldSep.separator_zero = false;
}
if (!pset.popt.topt.recordSep.separator &&
!pset.popt.topt.recordSep.separator_zero)
{
pset.popt.topt.recordSep.separator = pg_strdup(DEFAULT_RECORD_SEP);
pset.popt.topt.recordSep.separator_zero = false;
}
if (pset.getPassword == TRI_YES)
{
/*
* We can't be sure yet of the username that will be used, so don't
* offer a potentially wrong one. Typical uses of this option are
* noninteractive anyway. (Note: since we've not yet set up our
* cancel handler, there's no need to use simple_prompt_extended.)
*/
password = simple_prompt("Password: ", false);
}
/* loop until we have a password if requested by backend */
do
{
#define PARAMS_ARRAY_SIZE 8
const char **keywords = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
const char **values = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
keywords[0] = "host";
values[0] = options.host;
keywords[1] = "port";
values[1] = options.port;
keywords[2] = "user";
values[2] = options.username;
keywords[3] = "password";
values[3] = password;
keywords[4] = "dbname"; /* see do_connect() */
values[4] = (options.list_dbs && options.dbname == NULL) ?
"postgres" : options.dbname;
keywords[5] = "fallback_application_name";
values[5] = pset.progname;
keywords[6] = "client_encoding";
values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
keywords[7] = NULL;
values[7] = NULL;
new_pass = false;
pset.db = PQconnectdbParams(keywords, values, true);
free(keywords);
free(values);
if (PQstatus(pset.db) == CONNECTION_BAD &&
PQconnectionNeedsPassword(pset.db) &&
!password &&
pset.getPassword != TRI_NO)
{
/*
* Before closing the old PGconn, extract the user name that was
* actually connected with --- it might've come out of a URI or
* connstring "database name" rather than options.username.
*/
const char *realusername = PQuser(pset.db);
char *password_prompt;
if (realusername && realusername[0])
password_prompt = psprintf(_("Password for user %s: "),
realusername);
else
password_prompt = pg_strdup(_("Password: "));
PQfinish(pset.db);
password = simple_prompt(password_prompt, false);
free(password_prompt);
new_pass = true;
}
} while (new_pass);
if (PQstatus(pset.db) == CONNECTION_BAD)
{
pg_log_error("%s", PQerrorMessage(pset.db));
PQfinish(pset.db);
exit(EXIT_BADCONN);
}
psql_setup_cancel_handler();
#ifndef WIN32
/*
* do_watch() needs signal handlers installed (otherwise sigwait() will
* filter them out on some platforms), but doesn't need them to do
* anything, and they shouldn't ever run (unless perhaps a stray SIGALRM
* arrives due to a race when do_watch() cancels an itimer).
*/
pqsignal(SIGCHLD, empty_signal_handler);
pqsignal(SIGALRM, empty_signal_handler);
#endif
PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);
SyncVariables();
if (options.list_dbs)
{
int success;
if (!options.no_psqlrc)
process_psqlrc(argv[0]);
success = listAllDbs(NULL, false);
PQfinish(pset.db);
exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
}
if (options.logfilename)
{
pset.logfile = fopen(options.logfilename, "a");
if (!pset.logfile)
pg_fatal("could not open log file \"%s\": %m",
options.logfilename);
}
if (!options.no_psqlrc)
process_psqlrc(argv[0]);
/*
* If any actions were given by user, process them in the order in which
* they were specified. Note single_txn is only effective in this mode.
*/
if (options.actions.head != NULL)
{
PGresult *res;
SimpleActionListCell *cell;
successResult = EXIT_SUCCESS; /* silence compiler */
if (options.single_txn)
{
if ((res = PSQLexec("BEGIN")) == NULL)
{
if (pset.on_error_stop)
{
successResult = EXIT_USER;
goto error;
}
}
else
PQclear(res);
}
for (cell = options.actions.head; cell; cell = cell->next)
{
if (cell->action == ACT_SINGLE_QUERY)
{
pg_logging_config(PG_LOG_FLAG_TERSE);
if (pset.echo == PSQL_ECHO_ALL)
puts(cell->val);
successResult = SendQuery(cell->val)
? EXIT_SUCCESS : EXIT_FAILURE;
}
else if (cell->action == ACT_SINGLE_SLASH)
{
PsqlScanState scan_state;
ConditionalStack cond_stack;
pg_logging_config(PG_LOG_FLAG_TERSE);
if (pset.echo == PSQL_ECHO_ALL)
puts(cell->val);
scan_state = psql_scan_create(&psqlscan_callbacks);
psql_scan_setup(scan_state,
cell->val, strlen(cell->val),
pset.encoding, standard_strings());
cond_stack = conditional_stack_create();
psql_scan_set_passthrough(scan_state, (void *) cond_stack);
successResult = HandleSlashCmds(scan_state,
cond_stack,
NULL,
NULL) != PSQL_CMD_ERROR
? EXIT_SUCCESS : EXIT_FAILURE;
psql_scan_destroy(scan_state);
conditional_stack_destroy(cond_stack);
}
else if (cell->action == ACT_FILE)
{
successResult = process_file(cell->val, false);
}
else
{
/* should never come here */
Assert(false);
}
if (successResult != EXIT_SUCCESS && pset.on_error_stop)
break;
}
if (options.single_txn)
{
/*
* Rollback the contents of the single transaction if the caller
* has set ON_ERROR_STOP and one of the steps has failed. This
* check needs to match the one done a couple of lines above.
*/
res = PSQLexec((successResult != EXIT_SUCCESS && pset.on_error_stop) ?
"ROLLBACK" : "COMMIT");
if (res == NULL)
{
if (pset.on_error_stop)
{
successResult = EXIT_USER;
goto error;
}
}
else
PQclear(res);
}
error:
;
}
/*
* or otherwise enter interactive main loop
*/
else
{
pg_logging_config(PG_LOG_FLAG_TERSE);
connection_warnings(true);
if (!pset.quiet)
printf(_("Type \"help\" for help.\n\n"));
initializeInput(options.no_readline ? 0 : 1);
successResult = MainLoop(stdin);
}
/* clean up */
if (pset.logfile)
fclose(pset.logfile);
if (pset.db)
PQfinish(pset.db);
if (pset.dead_conn)
PQfinish(pset.dead_conn);
setQFout(NULL);
return successResult;
}
/*
* Parse command line options
*/
static void
parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
{
static struct option long_options[] =
{
{"echo-all", no_argument, NULL, 'a'},
{"no-align", no_argument, NULL, 'A'},
{"command", required_argument, NULL, 'c'},
{"dbname", required_argument, NULL, 'd'},
{"echo-queries", no_argument, NULL, 'e'},
{"echo-errors", no_argument, NULL, 'b'},
{"echo-hidden", no_argument, NULL, 'E'},
{"file", required_argument, NULL, 'f'},
{"field-separator", required_argument, NULL, 'F'},
{"field-separator-zero", no_argument, NULL, 'z'},
{"host", required_argument, NULL, 'h'},
{"html", no_argument, NULL, 'H'},
{"list", no_argument, NULL, 'l'},
{"log-file", required_argument, NULL, 'L'},
{"no-readline", no_argument, NULL, 'n'},
{"single-transaction", no_argument, NULL, '1'},
{"output", required_argument, NULL, 'o'},
{"port", required_argument, NULL, 'p'},
{"pset", required_argument, NULL, 'P'},
{"quiet", no_argument, NULL, 'q'},
{"record-separator", required_argument, NULL, 'R'},
{"record-separator-zero", no_argument, NULL, '0'},
{"single-step", no_argument, NULL, 's'},
{"single-line", no_argument, NULL, 'S'},
{"tuples-only", no_argument, NULL, 't'},
{"table-attr", required_argument, NULL, 'T'},
{"username", required_argument, NULL, 'U'},
{"set", required_argument, NULL, 'v'},
{"variable", required_argument, NULL, 'v'},
{"version", no_argument, NULL, 'V'},
{"no-password", no_argument, NULL, 'w'},
{"password", no_argument, NULL, 'W'},
{"expanded", no_argument, NULL, 'x'},
{"no-psqlrc", no_argument, NULL, 'X'},
{"help", optional_argument, NULL, 1},
{"csv", no_argument, NULL, 2},
{NULL, 0, NULL, 0}
};
int optindex;
int c;
memset(options, 0, sizeof *options);
while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
long_options, &optindex)) != -1)
{
switch (c)
{
case 'a':
SetVariable(pset.vars, "ECHO", "all");
break;
case 'A':
pset.popt.topt.format = PRINT_UNALIGNED;
break;
case 'b':
SetVariable(pset.vars, "ECHO", "errors");
break;
case 'c':
if (optarg[0] == '\\')
simple_action_list_append(&options->actions,
ACT_SINGLE_SLASH,
optarg + 1);
else
simple_action_list_append(&options->actions,
ACT_SINGLE_QUERY,
optarg);
break;
case 'd':
options->dbname = pg_strdup(optarg);
break;
case 'e':
SetVariable(pset.vars, "ECHO", "queries");
break;
case 'E':
SetVariableBool(pset.vars, "ECHO_HIDDEN");
break;
case 'f':
simple_action_list_append(&options->actions,
ACT_FILE,
optarg);
break;
case 'F':
pset.popt.topt.fieldSep.separator = pg_strdup(optarg);
pset.popt.topt.fieldSep.separator_zero = false;
break;
case 'h':
options->host = pg_strdup(optarg);
break;
case 'H':
pset.popt.topt.format = PRINT_HTML;
break;
case 'l':
options->list_dbs = true;
break;
case 'L':
options->logfilename = pg_strdup(optarg);
break;
case 'n':
options->no_readline = true;
break;
case 'o':
if (!setQFout(optarg))
exit(EXIT_FAILURE);
break;
case 'p':
options->port = pg_strdup(optarg);
break;
case 'P':
{
char *value;
char *equal_loc;
bool result;
value = pg_strdup(optarg);
equal_loc = strchr(value, '=');
if (!equal_loc)
result = do_pset(value, NULL, &pset.popt, true);
else
{
*equal_loc = '\0';
result = do_pset(value, equal_loc + 1, &pset.popt, true);
}
if (!result)
pg_fatal("could not set printing parameter \"%s\"", value);
free(value);
break;
}
case 'q':
SetVariableBool(pset.vars, "QUIET");
break;
case 'R':
pset.popt.topt.recordSep.separator = pg_strdup(optarg);
pset.popt.topt.recordSep.separator_zero = false;
break;
case 's':
SetVariableBool(pset.vars, "SINGLESTEP");
break;
case 'S':
SetVariableBool(pset.vars, "SINGLELINE");
break;
case 't':
pset.popt.topt.tuples_only = true;
break;
case 'T':
pset.popt.topt.tableAttr = pg_strdup(optarg);
break;
case 'U':
options->username = pg_strdup(optarg);
break;
case 'v':
{
char *value;
char *equal_loc;
value = pg_strdup(optarg);
equal_loc = strchr(value, '=');
if (!equal_loc)
{
if (!DeleteVariable(pset.vars, value))
exit(EXIT_FAILURE); /* error already printed */
}
else
{
*equal_loc = '\0';
if (!SetVariable(pset.vars, value, equal_loc + 1))
exit(EXIT_FAILURE); /* error already printed */
}
free(value);
break;
}
case 'V':
showVersion();
exit(EXIT_SUCCESS);
case 'w':
pset.getPassword = TRI_NO;
break;
case 'W':
pset.getPassword = TRI_YES;
break;
case 'x':
pset.popt.topt.expanded = true;
break;
case 'X':
options->no_psqlrc = true;
break;
case 'z':
pset.popt.topt.fieldSep.separator_zero = true;
break;
case '0':
pset.popt.topt.recordSep.separator_zero = true;
break;
case '1':
options->single_txn = true;
break;
case '?':
if (optind <= argc &&
strcmp(argv[optind - 1], "-?") == 0)
{
/* actual help option given */
usage(NOPAGER);
exit(EXIT_SUCCESS);
}
else
{
/* getopt error (unknown option or missing argument) */
goto unknown_option;
}
break;
case 1:
{
if (!optarg || strcmp(optarg, "options") == 0)
usage(NOPAGER);
else if (optarg && strcmp(optarg, "commands") == 0)
slashUsage(NOPAGER);
else if (optarg && strcmp(optarg, "variables") == 0)
helpVariables(NOPAGER);
else
goto unknown_option;
exit(EXIT_SUCCESS);
}
break;
case 2:
pset.popt.topt.format = PRINT_CSV;
break;
default:
unknown_option:
/* getopt_long already emitted a complaint */
pg_log_error_hint("Try \"%s --help\" for more information.",
pset.progname);
exit(EXIT_FAILURE);
}
}
/*
* if we still have arguments, use it as the database name and username
*/
while (argc - optind >= 1)
{
if (!options->dbname)
options->dbname = argv[optind];
else if (!options->username)
options->username = argv[optind];
else if (!pset.quiet)
pg_log_warning("extra command-line argument \"%s\" ignored",
argv[optind]);
optind++;
}
}
/*
* Append a new item to the end of the SimpleActionList.
* Note that "val" is copied if it's not NULL.
*/
static void
simple_action_list_append(SimpleActionList *list,
enum _actions action, const char *val)
{
SimpleActionListCell *cell;
cell = pg_malloc_object(SimpleActionListCell);
cell->next = NULL;
cell->action = action;
if (val)
cell->val = pg_strdup(val);
else
cell->val = NULL;
if (list->tail)
list->tail->next = cell;
else
list->head = cell;
list->tail = cell;
}
/*
* Load .psqlrc file, if found.
*/
static void
process_psqlrc(char *argv0)
{
char home[MAXPGPATH];
char rc_file[MAXPGPATH];
char my_exec_path[MAXPGPATH];
char etc_path[MAXPGPATH];
char *envrc = getenv("PSQLRC");
if (find_my_exec(argv0, my_exec_path) < 0)
pg_fatal("could not find own program executable");
get_etc_path(my_exec_path, etc_path);
snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
process_psqlrc_file(rc_file);
if (envrc != NULL && strlen(envrc) > 0)
{
/* might need to free() this */
char *envrc_alloc = pstrdup(envrc);
expand_tilde(&envrc_alloc);
process_psqlrc_file(envrc_alloc);
}
else if (get_home_path(home))
{
snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
process_psqlrc_file(rc_file);
}
}
static void
process_psqlrc_file(char *filename)
{
char *psqlrc_minor,
*psqlrc_major;
#if defined(WIN32) && (!defined(__MINGW32__))
#define R_OK 4
#endif
psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
/* check for minor version first, then major, then no version */
if (access(psqlrc_minor, R_OK) == 0)
(void) process_file(psqlrc_minor, false);
else if (access(psqlrc_major, R_OK) == 0)
(void) process_file(psqlrc_major, false);
else if (access(filename, R_OK) == 0)
(void) process_file(filename, false);
free(psqlrc_minor);
free(psqlrc_major);
}
/* showVersion
*
* This output format is intended to match GNU standards.
*/
static void
showVersion(void)
{
puts("psql (PostgreSQL) " PG_VERSION);
}
/*
* Substitute hooks and assign hooks for psql variables.
*
* This isn't an amazingly good place for them, but neither is anywhere else.
*
* By policy, every special variable that controls any psql behavior should
* have one or both hooks, even if they're just no-ops. This ensures that
* the variable will remain present in variables.c's list even when unset,
* which ensures that it's known to tab completion.
*/
static char *
bool_substitute_hook(char *newval)
{
if (newval == NULL)
{
/* "\unset FOO" becomes "\set FOO off" */
newval = pg_strdup("off");
}
else if (newval[0] == '\0')
{
/* "\set FOO" becomes "\set FOO on" */
pg_free(newval);
newval = pg_strdup("on");
}
return newval;
}
static bool
autocommit_hook(const char *newval)
{
return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
}
static bool
on_error_stop_hook(const char *newval)
{
return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
}
static bool
quiet_hook(const char *newval)
{
return ParseVariableBool(newval, "QUIET", &pset.quiet);
}
static bool
singleline_hook(const char *newval)
{
return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
}
static bool
singlestep_hook(const char *newval)
{
return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
}
static char *
fetch_count_substitute_hook(char *newval)
{
if (newval == NULL)
newval = pg_strdup("0");
return newval;
}
static bool
fetch_count_hook(const char *newval)
{
return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
}
static bool
histfile_hook(const char *newval)
{
/*
* Someday we might try to validate the filename, but for now, this is
* just a placeholder to ensure HISTFILE is known to tab completion.
*/
return true;
}
static char *
histsize_substitute_hook(char *newval)
{
if (newval == NULL)
newval = pg_strdup("500");
return newval;
}
static bool
histsize_hook(const char *newval)
{
return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
}
static char *
ignoreeof_substitute_hook(char *newval)
{
int dummy;
/*
* This tries to mimic the behavior of bash, to wit "If set, the value is
* the number of consecutive EOF characters which must be typed as the
* first characters on an input line before bash exits. If the variable
* exists but does not have a numeric value, or has no value, the default
* value is 10. If it does not exist, EOF signifies the end of input to
* the shell." Unlike bash, however, we insist on the stored value
* actually being a valid integer.
*/
if (newval == NULL)
newval = pg_strdup("0");
else if (!ParseVariableNum(newval, NULL, &dummy))
newval = pg_strdup("10");
return newval;
}
static bool
ignoreeof_hook(const char *newval)
{
return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
}
static char *
echo_substitute_hook(char *newval)
{
if (newval == NULL)
newval = pg_strdup("none");
return newval;
}
static bool
echo_hook(const char *newval)
{
Assert(newval != NULL); /* else substitute hook messed up */
if (pg_strcasecmp(newval, "queries") == 0)
pset.echo = PSQL_ECHO_QUERIES;
else if (pg_strcasecmp(newval, "errors") == 0)
pset.echo = PSQL_ECHO_ERRORS;
else if (pg_strcasecmp(newval, "all") == 0)
pset.echo = PSQL_ECHO_ALL;
else if (pg_strcasecmp(newval, "none") == 0)
pset.echo = PSQL_ECHO_NONE;
else
{
PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
return false;
}
return true;
}
static bool
echo_hidden_hook(const char *newval)
{
Assert(newval != NULL); /* else substitute hook messed up */
if (pg_strcasecmp(newval, "noexec") == 0)
pset.echo_hidden = PSQL_ECHO_HIDDEN_NOEXEC;
else
{
bool on_off;
if (ParseVariableBool(newval, NULL, &on_off))
pset.echo_hidden = on_off ? PSQL_ECHO_HIDDEN_ON : PSQL_ECHO_HIDDEN_OFF;
else
{
PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
return false;
}
}
return true;
}
static bool
on_error_rollback_hook(const char *newval)
{
Assert(newval != NULL); /* else substitute hook messed up */
if (pg_strcasecmp(newval, "interactive") == 0)
pset.on_error_rollback = PSQL_ERROR_ROLLBACK_INTERACTIVE;
else
{
bool on_off;
if (ParseVariableBool(newval, NULL, &on_off))
pset.on_error_rollback = on_off ? PSQL_ERROR_ROLLBACK_ON : PSQL_ERROR_ROLLBACK_OFF;
else
{
PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
return false;
}
}
return true;
}
static char *
comp_keyword_case_substitute_hook(char *newval)
{
if (newval == NULL)
newval = pg_strdup("preserve-upper");
return newval;
}
static bool
comp_keyword_case_hook(const char *newval)
{
Assert(newval != NULL); /* else substitute hook messed up */
if (pg_strcasecmp(newval, "preserve-upper") == 0)
pset.comp_case = PSQL_COMP_CASE_PRESERVE_UPPER;
else if (pg_strcasecmp(newval, "preserve-lower") == 0)
pset.comp_case = PSQL_COMP_CASE_PRESERVE_LOWER;
else if (pg_strcasecmp(newval, "upper") == 0)
pset.comp_case = PSQL_COMP_CASE_UPPER;
else if (pg_strcasecmp(newval, "lower") == 0)
pset.comp_case = PSQL_COMP_CASE_LOWER;
else
{
PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
"lower, upper, preserve-lower, preserve-upper");
return false;
}
return true;
}
static char *
histcontrol_substitute_hook(char *newval)
{
if (newval == NULL)
newval = pg_strdup("none");
return newval;
}
static bool
histcontrol_hook(const char *newval)
{
Assert(newval != NULL); /* else substitute hook messed up */
if (pg_strcasecmp(newval, "ignorespace") == 0)
pset.histcontrol = hctl_ignorespace;
else if (pg_strcasecmp(newval, "ignoredups") == 0)
pset.histcontrol = hctl_ignoredups;
else if (pg_strcasecmp(newval, "ignoreboth") == 0)
pset.histcontrol = hctl_ignoreboth;
else if (pg_strcasecmp(newval, "none") == 0)
pset.histcontrol = hctl_none;
else
{
PsqlVarEnumError("HISTCONTROL", newval,
"none, ignorespace, ignoredups, ignoreboth");
return false;
}
return true;
}
static bool
prompt1_hook(const char *newval)
{
pset.prompt1 = newval ? newval : "";
return true;
}
static bool
prompt2_hook(const char *newval)
{
pset.prompt2 = newval ? newval : "";
return true;
}
static bool
prompt3_hook(const char *newval)
{
pset.prompt3 = newval ? newval : "";
return true;
}
static char *
verbosity_substitute_hook(char *newval)
{
if (newval == NULL)
newval = pg_strdup("default");
return newval;
}
static bool
verbosity_hook(const char *newval)
{
Assert(newval != NULL); /* else substitute hook messed up */
if (pg_strcasecmp(newval, "default") == 0)
pset.verbosity = PQERRORS_DEFAULT;
else if (pg_strcasecmp(newval, "verbose") == 0)
pset.verbosity = PQERRORS_VERBOSE;
else if (pg_strcasecmp(newval, "terse") == 0)
pset.verbosity = PQERRORS_TERSE;
else if (pg_strcasecmp(newval, "sqlstate") == 0)
pset.verbosity = PQERRORS_SQLSTATE;
else
{
PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
return false;
}
if (pset.db)
PQsetErrorVerbosity(pset.db, pset.verbosity);
return true;
}
static bool
show_all_results_hook(const char *newval)
{
return ParseVariableBool(newval, "SHOW_ALL_RESULTS", &pset.show_all_results);
}
static char *
show_context_substitute_hook(char *newval)
{
if (newval == NULL)
newval = pg_strdup("errors");
return newval;
}
static bool
show_context_hook(const char *newval)
{
Assert(newval != NULL); /* else substitute hook messed up */
if (pg_strcasecmp(newval, "never") == 0)
pset.show_context = PQSHOW_CONTEXT_NEVER;
else if (pg_strcasecmp(newval, "errors") == 0)
pset.show_context = PQSHOW_CONTEXT_ERRORS;
else if (pg_strcasecmp(newval, "always") == 0)
pset.show_context = PQSHOW_CONTEXT_ALWAYS;
else
{
PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
return false;
}
if (pset.db)
PQsetErrorContextVisibility(pset.db, pset.show_context);
return true;
}
static bool
hide_compression_hook(const char *newval)
{
return ParseVariableBool(newval, "HIDE_TOAST_COMPRESSION",
&pset.hide_compression);
}
static bool
hide_tableam_hook(const char *newval)
{
return ParseVariableBool(newval, "HIDE_TABLEAM", &pset.hide_tableam);
}
static void
EstablishVariableSpace(void)
{
pset.vars = CreateVariableSpace();
SetVariableHooks(pset.vars, "AUTOCOMMIT",
bool_substitute_hook,
autocommit_hook);
SetVariableHooks(pset.vars, "ON_ERROR_STOP",
bool_substitute_hook,
on_error_stop_hook);
SetVariableHooks(pset.vars, "QUIET",
bool_substitute_hook,
quiet_hook);
SetVariableHooks(pset.vars, "SINGLELINE",
bool_substitute_hook,
singleline_hook);
SetVariableHooks(pset.vars, "SINGLESTEP",
bool_substitute_hook,
singlestep_hook);
SetVariableHooks(pset.vars, "FETCH_COUNT",
fetch_count_substitute_hook,
fetch_count_hook);
SetVariableHooks(pset.vars, "HISTFILE",
NULL,
histfile_hook);
SetVariableHooks(pset.vars, "HISTSIZE",
histsize_substitute_hook,
histsize_hook);
SetVariableHooks(pset.vars, "IGNOREEOF",
ignoreeof_substitute_hook,
ignoreeof_hook);
SetVariableHooks(pset.vars, "ECHO",
echo_substitute_hook,
echo_hook);
SetVariableHooks(pset.vars, "ECHO_HIDDEN",
bool_substitute_hook,
echo_hidden_hook);
SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
bool_substitute_hook,
on_error_rollback_hook);
SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
comp_keyword_case_substitute_hook,
comp_keyword_case_hook);
SetVariableHooks(pset.vars, "HISTCONTROL",
histcontrol_substitute_hook,
histcontrol_hook);
SetVariableHooks(pset.vars, "PROMPT1",
NULL,
prompt1_hook);
SetVariableHooks(pset.vars, "PROMPT2",
NULL,
prompt2_hook);
SetVariableHooks(pset.vars, "PROMPT3",
NULL,
prompt3_hook);
SetVariableHooks(pset.vars, "VERBOSITY",
verbosity_substitute_hook,
verbosity_hook);
SetVariableHooks(pset.vars, "SHOW_ALL_RESULTS",
bool_substitute_hook,
show_all_results_hook);
SetVariableHooks(pset.vars, "SHOW_CONTEXT",
show_context_substitute_hook,
show_context_hook);
SetVariableHooks(pset.vars, "HIDE_TOAST_COMPRESSION",
bool_substitute_hook,
hide_compression_hook);
SetVariableHooks(pset.vars, "HIDE_TABLEAM",
bool_substitute_hook,
hide_tableam_hook);
}
|