console.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. * console.c - various interactive-prompt routines shared between
  3. * the Windows console PuTTY tools
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "putty.h"
  8. #include "storage.h"
  9. #include "ssh.h"
  10. #include "console.h"
  11. void cleanup_exit(int code)
  12. {
  13. /*
  14. * Clean up.
  15. */
  16. sk_cleanup();
  17. random_save_seed();
  18. exit(code);
  19. }
  20. void console_print_error_msg(const char *prefix, const char *msg)
  21. {
  22. fputs(prefix, stderr);
  23. fputs(": ", stderr);
  24. fputs(msg, stderr);
  25. fputc('\n', stderr);
  26. fflush(stderr);
  27. }
  28. /*
  29. * System for getting I/O handles to talk to the console for
  30. * interactive prompts.
  31. *
  32. * In PuTTY 0.78 and before, these prompts used the standard I/O
  33. * handles. But this means you can't redirect Plink's actual stdin
  34. * from a sensible data channel without the responses to login prompts
  35. * unwantedly being read from it too. Also, if you have a real
  36. * console handle then you can read from it in Unicode mode, which is
  37. * an option not available for any old file handle.
  38. *
  39. * However, many versions of PuTTY have worked the old way, so we need
  40. * a method of falling back to it for the sake of whoever's workflow
  41. * it turns out to break. So this structure equivocates between the
  42. * two systems.
  43. */
  44. static bool conio_use_standard_handles = false;
  45. bool console_set_stdio_prompts(bool newvalue)
  46. {
  47. conio_use_standard_handles = newvalue;
  48. return true;
  49. }
  50. static bool conio_use_utf8 = true;
  51. bool set_legacy_charset_handling(bool newvalue)
  52. {
  53. conio_use_utf8 = !newvalue;
  54. return true;
  55. }
  56. typedef struct ConsoleIO {
  57. HANDLE hin, hout;
  58. bool need_close_hin, need_close_hout;
  59. bool hin_is_console, hout_is_console;
  60. bool utf8;
  61. BinarySink_IMPLEMENTATION;
  62. } ConsoleIO;
  63. static void console_write(BinarySink *bs, const void *data, size_t len);
  64. static ConsoleIO *conio_setup(bool utf8)
  65. {
  66. ConsoleIO *conio = snew(ConsoleIO);
  67. conio->hin = conio->hout = INVALID_HANDLE_VALUE;
  68. conio->need_close_hin = conio->need_close_hout = false;
  69. conio->utf8 = utf8 && conio_use_utf8;
  70. /*
  71. * First try opening the console itself, so that prompts will go
  72. * there regardless of I/O redirection. We don't do this if the
  73. * user has deliberately requested a fallback to the old
  74. * behaviour. We also don't do it in batch mode, because in that
  75. * situation, any need for an interactive prompt will instead
  76. * noninteractively abort the connection, and in that situation,
  77. * the 'prompt' becomes more in the nature of an error message, so
  78. * it should go to standard error like everything else.
  79. */
  80. if (!conio_use_standard_handles && !console_batch_mode) {
  81. /*
  82. * If we do open the console, it has to be done separately for
  83. * input and output, with different magic file names.
  84. *
  85. * We need both read and write permission for both handles,
  86. * because read permission is needed to read the console mode
  87. * (in particular, to test if a file handle _is_ a console),
  88. * and write permission to change it.
  89. */
  90. conio->hin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
  91. 0, NULL, OPEN_EXISTING, 0, NULL);
  92. if (conio->hin != INVALID_HANDLE_VALUE)
  93. conio->need_close_hin = true;
  94. conio->hout = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
  95. 0, NULL, OPEN_EXISTING, 0, NULL);
  96. if (conio->hout != INVALID_HANDLE_VALUE)
  97. conio->need_close_hout = true;
  98. }
  99. /*
  100. * Fall back from that to using the standard handles. We use
  101. * standard error rather than standard output for our prompts,
  102. * because that has a better chance of separating them from
  103. */
  104. if (conio->hin == INVALID_HANDLE_VALUE)
  105. conio->hin = GetStdHandle(STD_INPUT_HANDLE);
  106. if (conio->hout == INVALID_HANDLE_VALUE)
  107. conio->hout = GetStdHandle(STD_OUTPUT_HANDLE);
  108. DWORD dummy;
  109. conio->hin_is_console = GetConsoleMode(conio->hin, &dummy);
  110. conio->hout_is_console = GetConsoleMode(conio->hout, &dummy);
  111. BinarySink_INIT(conio, console_write);
  112. return conio;
  113. }
  114. static void conio_free(ConsoleIO *conio)
  115. {
  116. if (conio->need_close_hin)
  117. CloseHandle(conio->hin);
  118. if (conio->need_close_hout)
  119. CloseHandle(conio->hout);
  120. sfree(conio);
  121. }
  122. static void console_write(BinarySink *bs, const void *data, size_t len)
  123. {
  124. ConsoleIO *conio = BinarySink_DOWNCAST(bs, ConsoleIO);
  125. if (conio->utf8) {
  126. /*
  127. * Convert the UTF-8 input into a wide string.
  128. */
  129. size_t wlen;
  130. wchar_t *wide = dup_mb_to_wc_c(CP_UTF8, 0, data, len, &wlen);
  131. if (conio->hout_is_console) {
  132. /*
  133. * To write UTF-8 to a console, use WriteConsoleW on the
  134. * wide string we've just made.
  135. */
  136. size_t pos = 0;
  137. DWORD nwritten;
  138. while (pos < wlen && WriteConsoleW(conio->hout, wide+pos, wlen-pos,
  139. &nwritten, NULL))
  140. pos += nwritten;
  141. } else {
  142. /*
  143. * To write a string encoded in UTF-8 to any other file
  144. * handle, the best we can do is to convert it into the
  145. * system code page. This will lose some characters, but
  146. * what else can you do?
  147. */
  148. size_t clen;
  149. char *sys_cp = dup_wc_to_mb_c(CP_ACP, 0, wide, wlen, "?", &clen);
  150. size_t pos = 0;
  151. DWORD nwritten;
  152. while (pos < clen && WriteFile(conio->hout, sys_cp+pos, clen-pos,
  153. &nwritten, NULL))
  154. pos += nwritten;
  155. burnstr(sys_cp);
  156. }
  157. burnwcs(wide);
  158. } else {
  159. /*
  160. * If we're in legacy non-UTF-8 mode, just send the bytes
  161. * we're given to the file handle without trying to be clever.
  162. */
  163. const char *cdata = (const char *)data;
  164. size_t pos = 0;
  165. DWORD nwritten;
  166. while (pos < len && WriteFile(conio->hout, cdata+pos, len-pos,
  167. &nwritten, NULL))
  168. pos += nwritten;
  169. }
  170. }
  171. static bool console_read_line_to_strbuf(ConsoleIO *conio, bool echo,
  172. strbuf *sb)
  173. {
  174. DWORD savemode;
  175. if (conio->hin_is_console) {
  176. GetConsoleMode(conio->hin, &savemode);
  177. DWORD newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
  178. if (!echo)
  179. newmode &= ~ENABLE_ECHO_INPUT;
  180. else
  181. newmode |= ENABLE_ECHO_INPUT;
  182. SetConsoleMode(conio->hin, newmode);
  183. }
  184. bool toret = false;
  185. while (true) {
  186. if (ptrlen_endswith(ptrlen_from_strbuf(sb),
  187. PTRLEN_LITERAL("\n"), NULL)) {
  188. toret = true;
  189. goto out;
  190. }
  191. if (conio->utf8) {
  192. wchar_t wbuf[4096];
  193. size_t wlen;
  194. if (conio->hin_is_console) {
  195. /*
  196. * To read UTF-8 from a console, read wide character data
  197. * via ReadConsoleW, and convert it to UTF-8.
  198. */
  199. DWORD nread;
  200. if (!ReadConsoleW(conio->hin, wbuf, lenof(wbuf), &nread, NULL))
  201. goto out;
  202. wlen = nread;
  203. } else {
  204. /*
  205. * To read UTF-8 from an ordinary file handle, read it
  206. * as normal bytes and then convert from CP_ACP to
  207. * UTF-8, in the reverse of what we did above for
  208. * output.
  209. */
  210. char buf[4096];
  211. DWORD nread;
  212. if (!ReadFile(conio->hin, buf, lenof(buf), &nread, NULL))
  213. goto out;
  214. wlen = mb_to_wc(CP_ACP, 0, buf, nread, wbuf, lenof(wbuf));
  215. smemclr(buf, sizeof(buf));
  216. }
  217. /* Allocate the maximum space in the strbuf that might be
  218. * needed for this data */
  219. size_t oldlen = sb->len, maxout = wlen * 4;
  220. void *outptr = strbuf_append(sb, maxout);
  221. size_t newlen = oldlen + wc_to_mb(CP_UTF8, 0, wbuf, wlen,
  222. outptr, maxout, NULL);
  223. strbuf_shrink_to(sb, newlen);
  224. smemclr(wbuf, sizeof(wbuf));
  225. } else {
  226. /*
  227. * If we're in legacy non-UTF-8 mode, just read bytes
  228. * directly from the file handle into the output strbuf.
  229. */
  230. char buf[4096];
  231. DWORD nread;
  232. if (!ReadFile(conio->hin, buf, lenof(buf), &nread, NULL))
  233. goto out;
  234. put_data(sb, buf, nread);
  235. smemclr(buf, sizeof(buf));
  236. }
  237. }
  238. out:
  239. if (!echo)
  240. put_datalit(conio, "\r\n");
  241. if (conio->hin_is_console)
  242. SetConsoleMode(conio->hin, savemode);
  243. return toret;
  244. }
  245. static char *console_read_line(ConsoleIO *conio, bool echo)
  246. {
  247. strbuf *sb = strbuf_new_nm();
  248. if (!console_read_line_to_strbuf(conio, echo, sb)) {
  249. strbuf_free(sb);
  250. return NULL;
  251. } else {
  252. return strbuf_to_str(sb);
  253. }
  254. }
  255. typedef enum {
  256. RESPONSE_ABANDON,
  257. RESPONSE_YES,
  258. RESPONSE_NO,
  259. RESPONSE_INFO,
  260. RESPONSE_UNRECOGNISED
  261. } ResponseType;
  262. static ResponseType parse_and_free_response(char *line)
  263. {
  264. if (!line)
  265. return RESPONSE_ABANDON;
  266. ResponseType toret;
  267. switch (line[0]) {
  268. /* In case of misplaced reflexes from another program,
  269. * recognise 'q' as 'abandon connection' as well as the
  270. * advertised 'just press Return' */
  271. case 'q':
  272. case 'Q':
  273. case '\n':
  274. case '\r':
  275. case '\0':
  276. toret = RESPONSE_ABANDON;
  277. break;
  278. case 'y':
  279. case 'Y':
  280. toret = RESPONSE_YES;
  281. break;
  282. case 'n':
  283. case 'N':
  284. toret = RESPONSE_NO;
  285. break;
  286. case 'i':
  287. case 'I':
  288. toret = RESPONSE_INFO;
  289. break;
  290. default:
  291. toret = RESPONSE_UNRECOGNISED;
  292. break;
  293. }
  294. burnstr(line);
  295. return toret;
  296. }
  297. /*
  298. * Helper function to print the message from a SeatDialogText. Returns
  299. * the final prompt to print on the input line, or NULL if a
  300. * batch-mode abort is needed. In the latter case it will have printed
  301. * the abort text already.
  302. */
  303. static const char *console_print_seatdialogtext(
  304. ConsoleIO *conio, SeatDialogText *text)
  305. {
  306. const char *prompt = NULL;
  307. for (SeatDialogTextItem *item = text->items,
  308. *end = item+text->nitems; item < end; item++) {
  309. switch (item->type) {
  310. case SDT_PARA:
  311. wordwrap(BinarySink_UPCAST(conio),
  312. ptrlen_from_asciz(item->text), 60);
  313. put_byte(conio, '\n');
  314. break;
  315. case SDT_DISPLAY:
  316. put_fmt(conio, " %s\n", item->text);
  317. break;
  318. case SDT_SCARY_HEADING:
  319. /* Can't change font size or weight in this context */
  320. put_fmt(conio, "%s\n", item->text);
  321. break;
  322. case SDT_BATCH_ABORT:
  323. if (console_batch_mode) {
  324. put_fmt(conio, "%s\n", item->text);
  325. return NULL;
  326. }
  327. break;
  328. case SDT_PROMPT:
  329. prompt = item->text;
  330. break;
  331. default:
  332. break;
  333. }
  334. }
  335. assert(prompt); /* something in the SeatDialogText should have set this */
  336. return prompt;
  337. }
  338. SeatPromptResult console_confirm_ssh_host_key(
  339. Seat *seat, const char *host, int port, const char *keytype,
  340. char *keystr, SeatDialogText *text, HelpCtx helpctx,
  341. void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
  342. {
  343. ConsoleIO *conio = conio_setup(false);
  344. SeatPromptResult result;
  345. const char *prompt = console_print_seatdialogtext(conio, text);
  346. if (!prompt) {
  347. result = SPR_SW_ABORT("Cannot confirm a host key in batch mode");
  348. goto out;
  349. }
  350. ResponseType response;
  351. while (true) {
  352. put_fmt(conio, "%s (y/n, Return cancels connection, i for more info) ",
  353. prompt);
  354. response = parse_and_free_response(console_read_line(conio, true));
  355. if (response == RESPONSE_INFO) {
  356. for (SeatDialogTextItem *item = text->items,
  357. *end = item+text->nitems; item < end; item++) {
  358. switch (item->type) {
  359. case SDT_MORE_INFO_KEY:
  360. put_dataz(conio, item->text);
  361. break;
  362. case SDT_MORE_INFO_VALUE_SHORT:
  363. put_fmt(conio, ": %s\n", item->text);
  364. break;
  365. case SDT_MORE_INFO_VALUE_BLOB:
  366. put_fmt(conio, ":\n%s\n", item->text);
  367. break;
  368. default:
  369. break;
  370. }
  371. }
  372. } else {
  373. break;
  374. }
  375. }
  376. if (response == RESPONSE_YES || response == RESPONSE_NO) {
  377. if (response == RESPONSE_YES)
  378. store_host_key(seat, host, port, keytype, keystr);
  379. result = SPR_OK;
  380. } else {
  381. put_dataz(conio, console_abandoned_msg);
  382. result = SPR_USER_ABORT;
  383. }
  384. out:
  385. conio_free(conio);
  386. return result;
  387. }
  388. SeatPromptResult console_confirm_weak_crypto_primitive(
  389. Seat *seat, SeatDialogText *text,
  390. void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
  391. {
  392. ConsoleIO *conio = conio_setup(false);
  393. SeatPromptResult result;
  394. const char *prompt = console_print_seatdialogtext(conio, text);
  395. if (!prompt) {
  396. result = SPR_SW_ABORT("Cannot confirm a weak crypto primitive "
  397. "in batch mode");
  398. goto out;
  399. }
  400. put_fmt(conio, "%s (y/n) ", prompt);
  401. ResponseType response = parse_and_free_response(
  402. console_read_line(conio, true));
  403. if (response == RESPONSE_YES) {
  404. result = SPR_OK;
  405. } else {
  406. put_dataz(conio, console_abandoned_msg);
  407. result = SPR_USER_ABORT;
  408. }
  409. out:
  410. conio_free(conio);
  411. return result;
  412. }
  413. SeatPromptResult console_confirm_weak_cached_hostkey(
  414. Seat *seat, SeatDialogText *text,
  415. void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
  416. {
  417. ConsoleIO *conio = conio_setup(false);
  418. SeatPromptResult result;
  419. const char *prompt = console_print_seatdialogtext(conio, text);
  420. if (!prompt)
  421. return SPR_SW_ABORT("Cannot confirm a weak cached host key "
  422. "in batch mode");
  423. put_fmt(conio, "%s (y/n) ", prompt);
  424. ResponseType response = parse_and_free_response(
  425. console_read_line(conio, true));
  426. if (response == RESPONSE_YES) {
  427. result = SPR_OK;
  428. } else {
  429. put_dataz(conio, console_abandoned_msg);
  430. result = SPR_USER_ABORT;
  431. }
  432. conio_free(conio);
  433. return result;
  434. }
  435. bool is_interactive(void)
  436. {
  437. ConsoleIO *conio = conio_setup(false);
  438. bool toret = conio->hin_is_console;
  439. conio_free(conio);
  440. return toret;
  441. }
  442. bool console_antispoof_prompt = true;
  443. void console_set_trust_status(Seat *seat, bool trusted)
  444. {
  445. /* Do nothing in response to a change of trust status, because
  446. * there's nothing we can do in a console environment. However,
  447. * the query function below will make a fiddly decision about
  448. * whether to tell the backend to enable fallback handling. */
  449. }
  450. bool console_can_set_trust_status(Seat *seat)
  451. {
  452. if (console_batch_mode) {
  453. /*
  454. * In batch mode, we don't need to worry about the server
  455. * mimicking our interactive authentication, because the user
  456. * already knows not to expect any.
  457. */
  458. return true;
  459. }
  460. return false;
  461. }
  462. bool console_has_mixed_input_stream(Seat *seat)
  463. {
  464. if (!is_interactive() || !console_antispoof_prompt) {
  465. /*
  466. * If standard input isn't connected to a terminal, then even
  467. * if the server did send a spoof authentication prompt, the
  468. * user couldn't respond to it via the terminal anyway.
  469. *
  470. * We also pretend this is true if the user has purposely
  471. * disabled the antispoof prompt.
  472. */
  473. return false;
  474. }
  475. return true;
  476. }
  477. /*
  478. * Ask whether to wipe a session log file before writing to it.
  479. * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
  480. */
  481. int console_askappend(LogPolicy *lp, Filename *filename,
  482. void (*callback)(void *ctx, int result), void *ctx)
  483. {
  484. static const char msgtemplate[] =
  485. "The session log file \"%.*s\" already exists.\n"
  486. "You can overwrite it with a new session log,\n"
  487. "append your session log to the end of it,\n"
  488. "or disable session logging for this session.\n"
  489. "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
  490. "or just press Return to disable logging.\n"
  491. "Wipe the log file? (y/n, Return cancels logging) ";
  492. static const char msgtemplate_batch[] =
  493. "The session log file \"%.*s\" already exists.\n"
  494. "Logging will not be enabled.\n";
  495. ConsoleIO *conio = conio_setup(true);
  496. int result;
  497. if (console_batch_mode) {
  498. put_fmt(conio, msgtemplate_batch, FILENAME_MAX, filename->utf8path);
  499. result = 0;
  500. goto out;
  501. }
  502. put_fmt(conio, msgtemplate, FILENAME_MAX, filename->utf8path);
  503. ResponseType response = parse_and_free_response(
  504. console_read_line(conio, true));
  505. if (response == RESPONSE_YES)
  506. result = 2;
  507. else if (response == RESPONSE_NO)
  508. result = 1;
  509. else
  510. result = 0;
  511. out:
  512. conio_free(conio);
  513. return result;
  514. }
  515. /*
  516. * Warn about the obsolescent key file format.
  517. *
  518. * Uniquely among these functions, this one does _not_ expect a
  519. * frontend handle. This means that if PuTTY is ported to a
  520. * platform which requires frontend handles, this function will be
  521. * an anomaly. Fortunately, the problem it addresses will not have
  522. * been present on that platform, so it can plausibly be
  523. * implemented as an empty function.
  524. */
  525. void old_keyfile_warning(void)
  526. {
  527. static const char message[] =
  528. "You are loading an SSH-2 private key which has an\n"
  529. "old version of the file format. This means your key\n"
  530. "file is not fully tamperproof. Future versions of\n"
  531. "PuTTY may stop supporting this private key format,\n"
  532. "so we recommend you convert your key to the new\n"
  533. "format.\n"
  534. "\n"
  535. "Once the key is loaded into PuTTYgen, you can perform\n"
  536. "this conversion simply by saving it again.\n";
  537. fputs(message, stderr);
  538. }
  539. /*
  540. * Display the fingerprints of the PGP Master Keys to the user.
  541. */
  542. void pgp_fingerprints(void)
  543. {
  544. fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
  545. "be used to establish a trust path from this executable to another\n"
  546. "one. See the manual for more information.\n"
  547. "(Note: these fingerprints have nothing to do with SSH!)\n"
  548. "\n"
  549. "PuTTY Master Key as of " PGP_MASTER_KEY_YEAR
  550. " (" PGP_MASTER_KEY_DETAILS "):\n"
  551. " " PGP_MASTER_KEY_FP "\n\n"
  552. "Previous Master Key (" PGP_PREV_MASTER_KEY_YEAR
  553. ", " PGP_PREV_MASTER_KEY_DETAILS "):\n"
  554. " " PGP_PREV_MASTER_KEY_FP "\n", stdout);
  555. }
  556. void console_logging_error(LogPolicy *lp, const char *string)
  557. {
  558. /* Ordinary Event Log entries are displayed in the same way as
  559. * logging errors, but only in verbose mode */
  560. fprintf(stderr, "%s\n", string);
  561. fflush(stderr);
  562. }
  563. void console_eventlog(LogPolicy *lp, const char *string)
  564. {
  565. /* Ordinary Event Log entries are displayed in the same way as
  566. * logging errors, but only in verbose mode */
  567. if (lp_verbose(lp))
  568. console_logging_error(lp, string);
  569. }
  570. StripCtrlChars *console_stripctrl_new(
  571. Seat *seat, BinarySink *bs_out, SeatInteractionContext sic)
  572. {
  573. return stripctrl_new(bs_out, false, 0);
  574. }
  575. SeatPromptResult console_get_userpass_input(prompts_t *p)
  576. {
  577. ConsoleIO *conio = conio_setup(p->utf8);
  578. SeatPromptResult result;
  579. size_t curr_prompt;
  580. /*
  581. * Zero all the results, in case we abort half-way through.
  582. */
  583. {
  584. int i;
  585. for (i = 0; i < (int)p->n_prompts; i++)
  586. prompt_set_result(p->prompts[i], "");
  587. }
  588. /*
  589. * The prompts_t might contain a message to be displayed but no
  590. * actual prompt. More usually, though, it will contain
  591. * questions that the user needs to answer, in which case we
  592. * need to ensure that we're able to get the answers.
  593. */
  594. if (p->n_prompts) {
  595. if (console_batch_mode) {
  596. result = SPR_SW_ABORT("Cannot answer interactive prompts "
  597. "in batch mode");
  598. goto out;
  599. }
  600. }
  601. /*
  602. * Preamble.
  603. */
  604. /* We only print the `name' caption if we have to... */
  605. if (p->name_reqd && p->name) {
  606. ptrlen plname = ptrlen_from_asciz(p->name);
  607. put_datapl(conio, plname);
  608. if (!ptrlen_endswith(plname, PTRLEN_LITERAL("\n"), NULL))
  609. put_datalit(conio, "\n");
  610. }
  611. /* ...but we always print any `instruction'. */
  612. if (p->instruction) {
  613. ptrlen plinst = ptrlen_from_asciz(p->instruction);
  614. put_datapl(conio, plinst);
  615. if (!ptrlen_endswith(plinst, PTRLEN_LITERAL("\n"), NULL))
  616. put_datalit(conio, "\n");
  617. }
  618. for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) {
  619. prompt_t *pr = p->prompts[curr_prompt];
  620. put_dataz(conio, pr->prompt);
  621. if (!console_read_line_to_strbuf(conio, pr->echo, pr->result)) {
  622. result = make_spr_sw_abort_winerror(
  623. "Error reading from console", GetLastError());
  624. goto out;
  625. } else if (!pr->result->len) {
  626. /* Regard EOF on the terminal as a deliberate user-abort */
  627. result = SPR_USER_ABORT;
  628. goto out;
  629. } else {
  630. if (strbuf_chomp(pr->result, '\n')) {
  631. strbuf_chomp(pr->result, '\r');
  632. }
  633. }
  634. }
  635. result = SPR_OK;
  636. out:
  637. conio_free(conio);
  638. return result;
  639. }