PostgreSQL Source Code: src/bin/psql/mainloop.c Source File (original) (raw)

1

2

3

4

5

6

7

9

18

19

22};

23

24

25

26

27

28

29

30

31

32int

34{

35 PsqlScanState scan_state;

37 volatile PQExpBuffer query_buf;

38 volatile PQExpBuffer previous_buf;

39

40

41 PQExpBuffer history_buf;

42

43 char *line;

44 int added_nl_pos;

46 bool line_saved_in_history;

50 volatile bool need_redisplay = false;

51 volatile int count_eof = 0;

52 volatile bool die_on_error = false;

53 FILE *prev_cmd_source;

54 bool prev_cmd_interactive;

56

57

61

62

63

68

69

73

81

82

84 {

85

86

87

89 {

91 {

92

93

94

96 break;

97 }

98

100 }

101

102

103

104

105

106

108 {

109

110

111

116 count_eof = 0;

119 need_redisplay = false;

122

124 {

126

127

128

129

130

132 {

135 }

136 }

137 else

138 {

140 break;

141 }

142 }

143

145

146

147

148

150 {

151

152 if (query_buf->len == 0)

154

155 if (need_redisplay)

156 {

157 if (query_buf->len > 0)

158 {

162 }

163 need_redisplay = false;

164 }

165

167 query_buf);

168 }

169 else

170 {

172 if (!line && ferror(source))

174 }

175

176

177

178

179

180

181

182 if (line == NULL)

183 {

185 {

186

187 count_eof++;

188

190 {

193 continue;

194 }

195

197 }

198 break;

199 }

200

201 count_eof = 0;

202

204

205

207 memmove(line, line + 3, strlen(line + 3) + 1);

208

209

211 strncmp(line, "PGDMP", 5) == 0)

212 {

214 puts(_("The input is a PostgreSQL custom-format dump.\n"

215 "Use the pg_restore command-line client to restore this dump to a database.\n"));

218 break;

219 }

220

221

223 {

225 continue;

226 }

227

228

230 {

231 char *first_word = line;

232 char *rest_of_line = NULL;

233 bool found_help = false;

234 bool found_exit_or_quit = false;

235 bool found_q = false;

236

237

238

239

240

241

242

244 {

245 rest_of_line = first_word + 4;

246 found_help = true;

247 }

248 else if (pg_strncasecmp(first_word, "exit", 4) == 0 ||

250 {

251 rest_of_line = first_word + 4;

252 found_exit_or_quit = true;

253 }

254 else if (strncmp(first_word, "\\q", 2) == 0)

255 {

256 rest_of_line = first_word + 2;

257 found_q = true;

258 }

259

260

261

262

263

264

265

266 if (rest_of_line != NULL)

267 {

268

269

270

271

272 while (isspace((unsigned char) *rest_of_line))

273 ++rest_of_line;

274 if (*rest_of_line == ';')

275 ++rest_of_line;

276 while (isspace((unsigned char) *rest_of_line))

277 ++rest_of_line;

278 if (*rest_of_line != '\0')

279 {

280 found_help = false;

281 found_exit_or_quit = false;

282 }

283 }

284

285

286

287

288

289

290

291 if (found_help)

292 {

293 if (query_buf->len != 0)

294#ifndef WIN32

295 puts(_("Use \\? for help or press control-C to clear the input buffer."));

296#else

297 puts(_("Use \\? for help."));

298#endif

299 else

300 {

301 puts(_("You are using psql, the command-line interface to PostgreSQL."));

302 printf(_("Type: \\copyright for distribution terms\n"

303 " \\h for help with SQL commands\n"

304 " \\? for help with psql commands\n"

305 " \\g or terminate with semicolon to execute query\n"

306 " \\q to quit\n"));

309 continue;

310 }

311 }

312

313

314

315

316

317

318

319 if (found_exit_or_quit)

320 {

321 if (query_buf->len != 0)

322 {

326 puts(_("Use \\q to quit."));

327 else

328#ifndef WIN32

329 puts(_("Use control-D to quit."));

330#else

331 puts(_("Use control-C to quit."));

332#endif

333 }

334 else

335 {

336

340 break;

341 }

342 }

343

344

345

346

347

348 if (found_q && query_buf->len != 0 &&

352#ifndef WIN32

353 puts(_("Use control-D to quit."));

354#else

355 puts(_("Use control-C to quit."));

356#endif

357 }

358

359

361 {

362 puts(line);

364 }

365

366

367 if (query_buf->len > 0)

368 {

370 added_nl_pos = query_buf->len;

371 }

372 else

373 added_nl_pos = -1;

374

375

377

378

379

380

384 line_saved_in_history = false;

385

386 while (success || !die_on_error)

387 {

390 size_t pos_in_query;

391 char *tmp_line;

392

393 pos_in_query = query_buf->len;

394 scan_result = psql_scan(scan_state, query_buf, &prompt_tmp);

395 prompt_status = prompt_tmp;

396

399

400

401

402

403

404

405

406 tmp_line = query_buf->data + pos_in_query;

407 while (*tmp_line != '\0')

408 {

409 if (*(tmp_line++) == '\n')

411 }

412

415

416

417

418

419

422 {

423

424

425

426

427

428

430 {

433 line_saved_in_history = true;

434 }

435

436

438 {

442

443

444 {

446

447 previous_buf = query_buf;

448 query_buf = swap_buf;

449 }

451

452 added_nl_pos = -1;

453

454 }

455 else

456 {

457

459 pg_log_error("query ignored; use \\endif or Ctrl-C to exit current \\if block");

460

464

465 }

466 }

468 {

469

470

471

472

473

474

475

476

477

478

479

480 if (query_buf->len == added_nl_pos)

481 {

482 query_buf->data[--query_buf->len] = '\0';

484 }

485 added_nl_pos = -1;

486

487

489 {

492 line_saved_in_history = true;

493 }

494

495

497 cond_stack,

498 query_buf,

499 previous_buf);

500

502

503

504

505

506

507

509

511 {

512

514

516

517

518 {

520

521 previous_buf = query_buf;

522 query_buf = swap_buf;

523 }

525

526

528 }

530 {

531

533

534 if (query_buf->len > 0 &&

535 query_buf->data[query_buf->len - 1] != '\n')

537

542

546 line_saved_in_history = false;

548

549 need_redisplay = true;

550 }

552 break;

553 }

554

555

558 break;

559 }

560

561

562

563

564

565

566

567

569 {

570 if (!line_saved_in_history)

572 if (query_buf->len == 0)

574 }

575

578

580 {

582 break;

583 }

584

586 {

587 if (success && die_on_error)

589

592 }

593 }

594

595

596

597

598

599

602 {

603

604

605#ifdef NOT_USED

608#endif

609

610

612 {

614 }

615 else

616 {

618 pg_log_error("query ignored; use \\endif or Ctrl-C to exit current \\if block");

620 }

621

622 if (success && die_on_error)

624 else if (pset.db == NULL)

626 }

627

628

629

630

631

635 {

636 pg_log_error("reached EOF without finding closing \\endif(s)");

639 }

640

641

642

643

644

645

646

647

649

653

656

660

661 return successResult;

662}

volatile sig_atomic_t sigint_interrupt_enabled

char * psql_get_variable(const char *varname, PsqlScanQuoteType quote, void *passthrough)

sigjmp_buf sigint_interrupt_jmp

bool standard_strings(void)

bool SendQuery(const char *query)

backslashResult HandleSlashCmds(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)

enum _backslashResult backslashResult

ConditionalStack conditional_stack_create(void)

bool conditional_stack_pop(ConditionalStack cstack)

void conditional_stack_destroy(ConditionalStack cstack)

bool conditional_active(ConditionalStack cstack)

bool conditional_stack_empty(ConditionalStack cstack)

char * pg_strdup(const char *in)

volatile sig_atomic_t cancel_pressed

Assert(PointerIsAligned(start, uint64))

void pg_send_history(PQExpBuffer history_buf)

char * gets_interactive(const char *prompt, PQExpBuffer query_buf)

char * gets_fromFile(FILE *source)

void pg_append_history(const char *s, PQExpBuffer history_buf)

#define pg_log_error(...)

const PsqlScanCallbacks psqlscan_callbacks

int MainLoop(FILE *source)

static rewind_source * source

int pg_strncasecmp(const char *s1, const char *s2, size_t n)

PQExpBuffer createPQExpBuffer(void)

void resetPQExpBuffer(PQExpBuffer str)

void destroyPQExpBuffer(PQExpBuffer str)

void appendPQExpBufferChar(PQExpBuffer str, char ch)

#define PQExpBufferBroken(str)

char * get_prompt(promptStatus_t status, ConditionalStack cstack)

enum _promptStatus promptStatus_t

void psql_scan_reset(PsqlScanState state)

void psql_scan_destroy(PsqlScanState state)

PsqlScanResult psql_scan(PsqlScanState state, PQExpBuffer query_buf, promptStatus_t *prompt)

PsqlScanState psql_scan_create(const PsqlScanCallbacks *callbacks)

void psql_scan_set_passthrough(PsqlScanState state, void *passthrough)

void psql_scan_setup(PsqlScanState state, const char *line, int line_len, int encoding, bool std_strings)

bool psql_scan_in_quote(PsqlScanState state)

void psql_scan_finish(PsqlScanState state)