interactive.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <sys/wait.h>
  9. #include <faux/faux.h>
  10. #include <faux/str.h>
  11. #include <faux/eloop.h>
  12. #include <klish/ktp.h>
  13. #include <klish/ktp_session.h>
  14. #include <tinyrl/tinyrl.h>
  15. #include "private.h"
  16. // Context for main loop
  17. typedef struct ctx_s {
  18. ktp_session_t *ktp;
  19. tinyrl_t *tinyrl;
  20. struct options *opts;
  21. char *hotkeys[VT100_HOTKEY_MAP_LEN];
  22. tri_t pager_working;
  23. FILE *pager_pipe;
  24. } ctx_t;
  25. bool_t auth_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  26. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  27. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  28. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  29. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  30. void *associated_data, void *user_data);
  31. static bool_t ktp_sync_auth(ktp_session_t *ktp, int *retcode,
  32. faux_error_t *error);
  33. static void reset_hotkey_table(ctx_t *ctx);
  34. static bool_t interactive_stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  35. void *user_data);
  36. // Keys
  37. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key);
  38. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key);
  39. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key);
  40. static bool_t tinyrl_key_hotkey(tinyrl_t *tinyrl, unsigned char key);
  41. int klish_interactive_shell(ktp_session_t *ktp, struct options *opts)
  42. {
  43. ctx_t ctx = {};
  44. faux_eloop_t *eloop = NULL;
  45. tinyrl_t *tinyrl = NULL;
  46. int stdin_flags = 0;
  47. char *hist_path = NULL;
  48. int auth_rc = -1;
  49. assert(ktp);
  50. if (!ktp)
  51. return -1;
  52. // Set stdin to O_NONBLOCK mode
  53. stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
  54. fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
  55. hist_path = faux_expand_tilde("~/.klish_history");
  56. tinyrl = tinyrl_new(stdin, stdout, hist_path, 100);
  57. faux_str_free(hist_path);
  58. tinyrl_set_prompt(tinyrl, "$ ");
  59. tinyrl_set_udata(tinyrl, &ctx);
  60. tinyrl_set_hotkey_fn(tinyrl, tinyrl_key_hotkey);
  61. tinyrl_bind_key(tinyrl, '\n', tinyrl_key_enter);
  62. tinyrl_bind_key(tinyrl, '\r', tinyrl_key_enter);
  63. tinyrl_bind_key(tinyrl, '\t', tinyrl_key_tab);
  64. tinyrl_bind_key(tinyrl, '?', tinyrl_key_help);
  65. ctx.ktp = ktp;
  66. ctx.tinyrl = tinyrl;
  67. ctx.opts = opts;
  68. faux_bzero(ctx.hotkeys, sizeof(ctx.hotkeys));
  69. ctx.pager_working = TRI_UNDEFINED;
  70. ctx.pager_pipe = NULL;
  71. // Now AUTH command is used only for starting hand-shake and getting
  72. // prompt from the server. Generally it must be necessary for
  73. // non-interactive session too but for now is not implemented.
  74. ktp_session_set_cb(ktp, KTP_SESSION_CB_AUTH_ACK, auth_ack_cb, &ctx);
  75. if (!ktp_sync_auth(ktp, &auth_rc, ktp_session_error(ktp)))
  76. goto cleanup;
  77. if (auth_rc < 0)
  78. goto cleanup;
  79. // Replace common stdout callback by interactive-specific one.
  80. // Place it after auth to make auth use standard stdout callback.
  81. ktp_session_set_cb(ktp, KTP_SESSION_CB_STDOUT, interactive_stdout_cb, &ctx);
  82. // Don't stop interactive loop on each answer
  83. ktp_session_set_stop_on_answer(ktp, BOOL_FALSE);
  84. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK, cmd_ack_cb, &ctx);
  85. ktp_session_set_cb(ktp, KTP_SESSION_CB_COMPLETION_ACK, completion_ack_cb, &ctx);
  86. ktp_session_set_cb(ktp, KTP_SESSION_CB_HELP_ACK, help_ack_cb, &ctx);
  87. tinyrl_redisplay(tinyrl);
  88. eloop = ktp_session_eloop(ktp);
  89. faux_eloop_add_fd(eloop, STDIN_FILENO, POLLIN, stdin_cb, &ctx);
  90. faux_eloop_loop(eloop);
  91. cleanup:
  92. // Cleanup
  93. reset_hotkey_table(&ctx);
  94. if (tinyrl_busy(tinyrl))
  95. faux_error_free(ktp_session_error(ktp));
  96. tinyrl_free(tinyrl);
  97. // Restore stdin mode
  98. fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
  99. return 0;
  100. }
  101. static bool_t process_prompt_param(tinyrl_t *tinyrl, const faux_msg_t *msg)
  102. {
  103. char *prompt = NULL;
  104. if (!tinyrl)
  105. return BOOL_FALSE;
  106. if (!msg)
  107. return BOOL_FALSE;
  108. prompt = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PROMPT);
  109. if (prompt) {
  110. tinyrl_set_prompt(tinyrl, prompt);
  111. faux_str_free(prompt);
  112. }
  113. return BOOL_TRUE;
  114. }
  115. static void reset_hotkey_table(ctx_t *ctx)
  116. {
  117. size_t i = 0;
  118. assert(ctx);
  119. for (i = 0; i < VT100_HOTKEY_MAP_LEN; i++)
  120. faux_str_free(ctx->hotkeys[i]);
  121. faux_bzero(ctx->hotkeys, sizeof(ctx->hotkeys));
  122. }
  123. static bool_t process_hotkey_param(ctx_t *ctx, const faux_msg_t *msg)
  124. {
  125. faux_list_node_t *iter = NULL;
  126. uint32_t param_len = 0;
  127. char *param_data = NULL;
  128. uint16_t param_type = 0;
  129. if (!ctx)
  130. return BOOL_FALSE;
  131. if (!msg)
  132. return BOOL_FALSE;
  133. if (!faux_msg_get_param_by_type(msg, KTP_PARAM_HOTKEY,
  134. (void **)&param_data, &param_len))
  135. return BOOL_TRUE;
  136. // If there is HOTKEY parameter then reinitialize whole hotkey table
  137. reset_hotkey_table(ctx);
  138. iter = faux_msg_init_param_iter(msg);
  139. while (faux_msg_get_param_each(
  140. &iter, &param_type, (void **)&param_data, &param_len)) {
  141. char *cmd = NULL;
  142. ssize_t code = -1;
  143. size_t key_len = 0;
  144. if (param_len < 3) // <key>'\0'<cmd>
  145. continue;
  146. if (KTP_PARAM_HOTKEY != param_type)
  147. continue;
  148. key_len = strlen(param_data); // Length of <key>
  149. if (key_len < 1)
  150. continue;
  151. code = vt100_hotkey_decode(param_data);
  152. if ((code < 0) || (code > VT100_HOTKEY_MAP_LEN))
  153. continue;
  154. cmd = faux_str_dupn(param_data + key_len + 1,
  155. param_len - key_len - 1);
  156. if (!cmd)
  157. continue;
  158. ctx->hotkeys[code] = cmd;
  159. }
  160. return BOOL_TRUE;
  161. }
  162. bool_t auth_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  163. {
  164. ctx_t *ctx = (ctx_t *)udata;
  165. int rc = -1;
  166. faux_error_t *error = NULL;
  167. process_prompt_param(ctx->tinyrl, msg);
  168. process_hotkey_param(ctx, msg);
  169. if (!ktp_session_retcode(ktp, &rc))
  170. rc = -1;
  171. error = ktp_session_error(ktp);
  172. if ((rc < 0) && (faux_error_len(error) > 0)) {
  173. faux_error_node_t *err_iter = faux_error_iter(error);
  174. const char *err = NULL;
  175. while ((err = faux_error_each(&err_iter)))
  176. fprintf(stderr, "Error: %s\n", err);
  177. }
  178. faux_error_free(error);
  179. // Happy compiler
  180. msg = msg;
  181. return BOOL_TRUE;
  182. }
  183. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  184. {
  185. ctx_t *ctx = (ctx_t *)udata;
  186. int rc = -1;
  187. faux_error_t *error = NULL;
  188. process_prompt_param(ctx->tinyrl, msg);
  189. process_hotkey_param(ctx, msg);
  190. if (!ktp_session_retcode(ktp, &rc))
  191. rc = -1;
  192. error = ktp_session_error(ktp);
  193. if ((rc < 0) && (faux_error_len(error) > 0)) {
  194. faux_error_node_t *err_iter = faux_error_iter(error);
  195. const char *err = NULL;
  196. while ((err = faux_error_each(&err_iter)))
  197. fprintf(stderr, "Error: %s\n", err);
  198. }
  199. faux_error_free(error);
  200. // Wait for pager
  201. if (ctx->pager_working == TRI_TRUE) {
  202. pclose(ctx->pager_pipe);
  203. ctx->pager_working = TRI_UNDEFINED;
  204. ctx->pager_pipe = NULL;
  205. }
  206. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  207. if (!ktp_session_done(ktp))
  208. tinyrl_redisplay(ctx->tinyrl);
  209. // Happy compiler
  210. msg = msg;
  211. return BOOL_TRUE;
  212. }
  213. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  214. void *associated_data, void *udata)
  215. {
  216. ctx_t *ctx = (ctx_t *)udata;
  217. ktp_session_state_e state = KTP_SESSION_STATE_ERROR;
  218. if (!ctx)
  219. return BOOL_FALSE;
  220. state = ktp_session_state(ctx->ktp);
  221. // Standard klish command line
  222. if (state == KTP_SESSION_STATE_IDLE) {
  223. tinyrl_read(ctx->tinyrl);
  224. return BOOL_TRUE;
  225. }
  226. // Interactive command
  227. if ((state == KTP_SESSION_STATE_WAIT_FOR_CMD) &&
  228. KTP_STATUS_IS_INTERACTIVE(ktp_session_cmd_features(ctx->ktp))) {
  229. int fd = fileno(tinyrl_istream(ctx->tinyrl));
  230. char buf[1024] = {};
  231. ssize_t bytes_readed = 0;
  232. while ((bytes_readed = read(fd, buf, sizeof(buf))) > 0) {
  233. ktp_session_stdin(ctx->ktp, buf, bytes_readed);
  234. if (bytes_readed != sizeof(buf))
  235. break;
  236. }
  237. }
  238. // Happy compiler
  239. eloop = eloop;
  240. type = type;
  241. associated_data = associated_data;
  242. return BOOL_TRUE;
  243. }
  244. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  245. {
  246. const char *line = NULL;
  247. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  248. faux_error_t *error = faux_error_new();
  249. tinyrl_line_to_hist(tinyrl);
  250. tinyrl_multi_crlf(tinyrl);
  251. tinyrl_reset_line_state(tinyrl);
  252. line = tinyrl_line(tinyrl);
  253. // Don't do anything on empty line
  254. if (faux_str_is_empty(line)) {
  255. faux_error_free(error);
  256. return BOOL_TRUE;
  257. }
  258. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  259. tinyrl_reset_line(tinyrl);
  260. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  261. key = key; // Happy compiler
  262. return BOOL_TRUE;
  263. }
  264. static bool_t tinyrl_key_hotkey(tinyrl_t *tinyrl, unsigned char key)
  265. {
  266. const char *line = NULL;
  267. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  268. faux_error_t *error = NULL;
  269. if (key >= VT100_HOTKEY_MAP_LEN)
  270. return BOOL_TRUE;
  271. line = ctx->hotkeys[key];
  272. if (faux_str_is_empty(line))
  273. return BOOL_TRUE;
  274. error = faux_error_new();
  275. tinyrl_multi_crlf(tinyrl);
  276. tinyrl_reset_line_state(tinyrl);
  277. tinyrl_reset_line(tinyrl);
  278. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  279. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  280. return BOOL_TRUE;
  281. }
  282. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  283. {
  284. const char *line = NULL;
  285. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  286. line = tinyrl_line(tinyrl);
  287. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  288. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  289. key = key; // Happy compiler
  290. return BOOL_TRUE;
  291. }
  292. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
  293. {
  294. const char *line = NULL;
  295. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  296. line = tinyrl_line(tinyrl);
  297. ktp_session_help(ctx->ktp, line);
  298. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  299. key = key; // Happy compiler
  300. return BOOL_TRUE;
  301. }
  302. static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
  303. const char *prefix, size_t max)
  304. {
  305. size_t width = tinyrl_width(tinyrl);
  306. size_t cols = 0;
  307. faux_list_node_t *iter = NULL;
  308. faux_list_node_t *node = NULL;
  309. size_t prefix_len = 0;
  310. size_t cols_filled = 0;
  311. if (prefix)
  312. prefix_len = strlen(prefix);
  313. // Find out column and rows number
  314. if (max < width)
  315. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  316. else
  317. cols = 1;
  318. iter = faux_list_head(completions);
  319. while ((node = faux_list_each_node(&iter))) {
  320. char *compl = (char *)faux_list_data(node);
  321. tinyrl_printf(tinyrl, "%*s%s",
  322. (prefix_len + max + 1 - strlen(compl)),
  323. prefix ? prefix : "",
  324. compl);
  325. cols_filled++;
  326. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  327. cols_filled = 0;
  328. tinyrl_crlf(tinyrl);
  329. }
  330. }
  331. }
  332. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  333. {
  334. ctx_t *ctx = (ctx_t *)udata;
  335. faux_list_node_t *iter = NULL;
  336. uint32_t param_len = 0;
  337. char *param_data = NULL;
  338. uint16_t param_type = 0;
  339. char *prefix = NULL;
  340. faux_list_t *completions = NULL;
  341. size_t completions_num = 0;
  342. size_t max_compl_len = 0;
  343. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  344. process_prompt_param(ctx->tinyrl, msg);
  345. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  346. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  347. NULL, NULL, (void (*)(void *))faux_str_free);
  348. iter = faux_msg_init_param_iter(msg);
  349. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  350. char *compl = NULL;
  351. if (KTP_PARAM_LINE != param_type)
  352. continue;
  353. compl = faux_str_dupn(param_data, param_len);
  354. faux_list_add(completions, compl);
  355. if (param_len > max_compl_len)
  356. max_compl_len = param_len;
  357. }
  358. completions_num = faux_list_len(completions);
  359. // Single possible completion
  360. if (1 == completions_num) {
  361. char *compl = (char *)faux_list_data(faux_list_head(completions));
  362. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  363. // Add space after completion
  364. tinyrl_line_insert(ctx->tinyrl, " ", 1);
  365. tinyrl_redisplay(ctx->tinyrl);
  366. // Multi possible completions
  367. } else if (completions_num > 1) {
  368. faux_list_node_t *eq_iter = NULL;
  369. size_t eq_part = 0;
  370. char *str = NULL;
  371. char *compl = NULL;
  372. // Try to find equal part for all possible completions
  373. eq_iter = faux_list_head(completions);
  374. str = (char *)faux_list_data(eq_iter);
  375. eq_part = strlen(str);
  376. eq_iter = faux_list_next_node(eq_iter);
  377. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  378. size_t cur_eq = 0;
  379. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  380. if (cur_eq < eq_part)
  381. eq_part = cur_eq;
  382. }
  383. // The equal part was found
  384. if (eq_part > 0) {
  385. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  386. tinyrl_redisplay(ctx->tinyrl);
  387. // There is no equal part for all completions
  388. } else {
  389. tinyrl_multi_crlf(ctx->tinyrl);
  390. tinyrl_reset_line_state(ctx->tinyrl);
  391. display_completions(ctx->tinyrl, completions,
  392. prefix, max_compl_len);
  393. tinyrl_redisplay(ctx->tinyrl);
  394. }
  395. }
  396. faux_list_free(completions);
  397. faux_str_free(prefix);
  398. // Happy compiler
  399. ktp = ktp;
  400. msg = msg;
  401. return BOOL_TRUE;
  402. }
  403. static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
  404. size_t max)
  405. {
  406. faux_list_node_t *iter = NULL;
  407. faux_list_node_t *node = NULL;
  408. iter = faux_list_head(help_list);
  409. while ((node = faux_list_each_node(&iter))) {
  410. help_t *help = (help_t *)faux_list_data(node);
  411. tinyrl_printf(tinyrl, " %s%*s%s\n",
  412. help->prefix,
  413. (max + 2 - strlen(help->prefix)),
  414. " ",
  415. help->line);
  416. }
  417. }
  418. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  419. {
  420. ctx_t *ctx = (ctx_t *)udata;
  421. faux_list_t *help_list = NULL;
  422. faux_list_node_t *iter = NULL;
  423. uint32_t param_len = 0;
  424. char *param_data = NULL;
  425. uint16_t param_type = 0;
  426. size_t max_prefix_len = 0;
  427. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  428. process_prompt_param(ctx->tinyrl, msg);
  429. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_NONUNIQUE,
  430. help_compare, help_kcompare, help_free);
  431. // Wait for PREFIX - LINE pairs
  432. iter = faux_msg_init_param_iter(msg);
  433. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data,
  434. &param_len)) {
  435. char *prefix_str = NULL;
  436. char *line_str = NULL;
  437. help_t *help = NULL;
  438. size_t prefix_len = 0;
  439. // Get PREFIX
  440. if (KTP_PARAM_PREFIX != param_type)
  441. continue;
  442. prefix_str = faux_str_dupn(param_data, param_len);
  443. prefix_len = param_len;
  444. // Get LINE
  445. if (!faux_msg_get_param_each(&iter, &param_type,
  446. (void **)&param_data, &param_len) ||
  447. (KTP_PARAM_LINE != param_type)) {
  448. faux_str_free(prefix_str);
  449. break;
  450. }
  451. line_str = faux_str_dupn(param_data, param_len);
  452. help = help_new(prefix_str, line_str);
  453. faux_list_add(help_list, help);
  454. if (prefix_len > max_prefix_len)
  455. max_prefix_len = prefix_len;
  456. }
  457. if (faux_list_len(help_list) > 0) {
  458. tinyrl_multi_crlf(ctx->tinyrl);
  459. tinyrl_reset_line_state(ctx->tinyrl);
  460. display_help(ctx->tinyrl, help_list, max_prefix_len);
  461. tinyrl_redisplay(ctx->tinyrl);
  462. }
  463. faux_list_free(help_list);
  464. ktp = ktp; // happy compiler
  465. return BOOL_TRUE;
  466. }
  467. static bool_t ktp_sync_auth(ktp_session_t *ktp, int *retcode,
  468. faux_error_t *error)
  469. {
  470. if (!ktp_session_auth(ktp, error))
  471. return BOOL_FALSE;
  472. faux_eloop_loop(ktp_session_eloop(ktp));
  473. return ktp_session_retcode(ktp, retcode);
  474. }
  475. static bool_t interactive_stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  476. void *udata)
  477. {
  478. ctx_t *ctx = (ctx_t *)udata;
  479. assert(ctx);
  480. // Start pager if necessary
  481. if (
  482. ctx->opts->pager_enabled && // Pager enabled within config file
  483. (ctx->pager_working == TRI_UNDEFINED) && // Pager is not working
  484. !(ktp_session_cmd_features(ktp) & KTP_STATUS_INTERACTIVE) // Non interactive command
  485. ) {
  486. ctx->pager_pipe = popen(ctx->opts->pager, "we");
  487. if (!ctx->pager_pipe)
  488. ctx->pager_working = BOOL_FALSE; // Indicates can't start
  489. else
  490. ctx->pager_working = BOOL_TRUE;
  491. }
  492. // Write to pager's pipe if pager is really working
  493. if (ctx->pager_working == TRI_TRUE) {
  494. if (fwrite(line, 1, len, ctx->pager_pipe) == 0) {
  495. // If we can't write to pager pipe then try stdout.
  496. if (write(STDOUT_FILENO, line, len) < 0)
  497. return BOOL_FALSE;
  498. return BOOL_TRUE; // Don't break the loop
  499. }
  500. fflush(ctx->pager_pipe);
  501. } else {
  502. if (write(STDOUT_FILENO, line, len) < 0)
  503. return BOOL_FALSE;
  504. }
  505. return BOOL_TRUE;
  506. }