Tput.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Console;
  36. /**
  37. * Class \Hoa\Console\Tput.
  38. *
  39. * Query terminfo database.
  40. * Resources:
  41. * • http://man.cx/terminfo(5),
  42. * • http://pubs.opengroup.org/onlinepubs/7908799/xcurses/terminfo.html,
  43. *
  44. * @copyright Copyright © 2007-2017 Hoa community
  45. * @license New BSD License
  46. */
  47. class Tput
  48. {
  49. /**
  50. * Booleans.
  51. *
  52. * @var array
  53. */
  54. protected static $_booleans = [
  55. 'auto_left_margin',
  56. 'auto_right_margin',
  57. 'no_esc_ctlc',
  58. 'ceol_standout_glitch',
  59. 'eat_newline_glitch',
  60. 'erase_overstrike',
  61. 'generic_type',
  62. 'hard_copy',
  63. 'meta_key', // originally has_meta_key
  64. 'status_line', // originally has_status_line
  65. 'insert_null_glitch',
  66. 'memory_above',
  67. 'memory_below',
  68. 'move_insert_mode',
  69. 'move_standout_mode',
  70. 'over_strike',
  71. 'status_line_esc_ok',
  72. 'dest_tabs_magic_smso',
  73. 'tilde_glitch',
  74. 'transparent_underline',
  75. 'xon_xoff',
  76. 'needs_xon_xoff',
  77. 'prtr_silent',
  78. 'hard_cursor',
  79. 'non_rev_rmcup',
  80. 'no_pad_char',
  81. 'non_dest_scroll_region',
  82. 'can_change',
  83. 'back_color_erase',
  84. 'hue_lightness_saturation',
  85. 'col_addr_glitch',
  86. 'cr_cancels_micro_mode',
  87. 'print_wheel', // originally has_print_wheel
  88. 'row_addr_glitch',
  89. 'semi_auto_right_margin',
  90. 'cpi_changes_res',
  91. 'lpi_changes_res',
  92. // #ifdef __INTERNAL_CAPS_VISIBLE
  93. 'backspaces_with_bs',
  94. 'crt_no_scrolling',
  95. 'no_correctly_working_cr',
  96. 'gnu_meta_key', // originally gnu_has_meta_key
  97. 'linefeed_is_newline',
  98. 'hardware_tabs', // originally has_hardware_tabs
  99. 'return_does_clr_eol'
  100. ];
  101. /**
  102. * Numbers.
  103. *
  104. * @var array
  105. */
  106. protected static $_numbers = [
  107. 'columns',
  108. 'init_tabs',
  109. 'lines',
  110. 'lines_of_memory',
  111. 'magic_cookie_glitch',
  112. 'padding_baud_rate',
  113. 'virtual_terminal',
  114. 'width_status_line',
  115. 'num_labels',
  116. 'label_height',
  117. 'label_width',
  118. 'max_attributes',
  119. 'maximum_windows',
  120. 'max_colors',
  121. 'max_pairs',
  122. 'no_color_video',
  123. 'buffer_capacity',
  124. 'dot_vert_spacing',
  125. 'dot_horz_spacing',
  126. 'max_micro_address',
  127. 'max_micro_jump',
  128. 'micro_col_size',
  129. 'micro_line_size',
  130. 'number_of_pins',
  131. 'output_res_char',
  132. 'output_res_line',
  133. 'output_res_horz_inch',
  134. 'output_res_vert_inch',
  135. 'print_rate',
  136. 'wide_char_size',
  137. 'buttons',
  138. 'bit_image_entwining',
  139. 'bit_image_type',
  140. // #ifdef __INTERNAL_CAPS_VISIBLE
  141. 'magic_cookie_glitch_ul',
  142. 'carriage_return_delay',
  143. 'new_line_delay',
  144. 'backspace_delay',
  145. 'horizontal_tab_delay',
  146. 'number_of_function_keys'
  147. ];
  148. /**
  149. * Strings.
  150. *
  151. * @var array
  152. */
  153. protected static $_strings = [
  154. 'back_tab',
  155. 'bell',
  156. 'carriage_return',
  157. 'change_scroll_region',
  158. 'clear_all_tabs',
  159. 'clear_screen',
  160. 'clr_eol',
  161. 'clr_eos',
  162. 'column_address',
  163. 'command_character',
  164. 'cursor_address',
  165. 'cursor_down',
  166. 'cursor_home',
  167. 'cursor_invisible',
  168. 'cursor_left',
  169. 'cursor_mem_address',
  170. 'cursor_normal',
  171. 'cursor_right',
  172. 'cursor_to_ll',
  173. 'cursor_up',
  174. 'cursor_visible',
  175. 'delete_character',
  176. 'delete_line',
  177. 'dis_status_line',
  178. 'down_half_line',
  179. 'enter_alt_charset_mode',
  180. 'enter_blink_mode',
  181. 'enter_bold_mode',
  182. 'enter_ca_mode',
  183. 'enter_delete_mode',
  184. 'enter_dim_mode',
  185. 'enter_insert_mode',
  186. 'enter_secure_mode',
  187. 'enter_protected_mode',
  188. 'enter_reverse_mode',
  189. 'enter_standout_mode',
  190. 'enter_underline_mode',
  191. 'erase_chars',
  192. 'exit_alt_charset_mode',
  193. 'exit_attribute_mode',
  194. 'exit_ca_mode',
  195. 'exit_delete_mode',
  196. 'exit_insert_mode',
  197. 'exit_standout_mode',
  198. 'exit_underline_mode',
  199. 'flash_screen',
  200. 'form_feed',
  201. 'from_status_line',
  202. 'init_1string',
  203. 'init_2string',
  204. 'init_3string',
  205. 'init_file',
  206. 'insert_character',
  207. 'insert_line',
  208. 'insert_padding',
  209. 'key_backspace',
  210. 'key_catab',
  211. 'key_clear',
  212. 'key_ctab',
  213. 'key_dc',
  214. 'key_dl',
  215. 'key_down',
  216. 'key_eic',
  217. 'key_eol',
  218. 'key_eos',
  219. 'key_f0',
  220. 'key_f1',
  221. 'key_f10',
  222. 'key_f2',
  223. 'key_f3',
  224. 'key_f4',
  225. 'key_f5',
  226. 'key_f6',
  227. 'key_f7',
  228. 'key_f8',
  229. 'key_f9',
  230. 'key_home',
  231. 'key_ic',
  232. 'key_il',
  233. 'key_left',
  234. 'key_ll',
  235. 'key_npage',
  236. 'key_ppage',
  237. 'key_right',
  238. 'key_sf',
  239. 'key_sr',
  240. 'key_stab',
  241. 'key_up',
  242. 'keypad_local',
  243. 'keypad_xmit',
  244. 'lab_f0',
  245. 'lab_f1',
  246. 'lab_f10',
  247. 'lab_f2',
  248. 'lab_f3',
  249. 'lab_f4',
  250. 'lab_f5',
  251. 'lab_f6',
  252. 'lab_f7',
  253. 'lab_f8',
  254. 'lab_f9',
  255. 'meta_off',
  256. 'meta_on',
  257. 'newline',
  258. 'pad_char',
  259. 'parm_dch',
  260. 'parm_delete_line',
  261. 'parm_down_cursor',
  262. 'parm_ich',
  263. 'parm_index',
  264. 'parm_insert_line',
  265. 'parm_left_cursor',
  266. 'parm_right_cursor',
  267. 'parm_rindex',
  268. 'parm_up_cursor',
  269. 'pkey_key',
  270. 'pkey_local',
  271. 'pkey_xmit',
  272. 'print_screen',
  273. 'prtr_off',
  274. 'prtr_on',
  275. 'repeat_char',
  276. 'reset_1string',
  277. 'reset_2string',
  278. 'reset_3string',
  279. 'reset_file',
  280. 'restore_cursor',
  281. 'row_address',
  282. 'save_cursor',
  283. 'scroll_forward',
  284. 'scroll_reverse',
  285. 'set_attributes',
  286. 'set_tab',
  287. 'set_window',
  288. 'tab',
  289. 'to_status_line',
  290. 'underline_char',
  291. 'up_half_line',
  292. 'init_prog',
  293. 'key_a1',
  294. 'key_a3',
  295. 'key_b2',
  296. 'key_c1',
  297. 'key_c3',
  298. 'prtr_non',
  299. 'char_padding',
  300. 'acs_chars',
  301. 'plab_norm',
  302. 'key_btab',
  303. 'enter_xon_mode',
  304. 'exit_xon_mode',
  305. 'enter_am_mode',
  306. 'exit_am_mode',
  307. 'xon_character',
  308. 'xoff_character',
  309. 'ena_acs',
  310. 'label_on',
  311. 'label_off',
  312. 'key_beg',
  313. 'key_cancel',
  314. 'key_close',
  315. 'key_command',
  316. 'key_copy',
  317. 'key_create',
  318. 'key_end',
  319. 'key_enter',
  320. 'key_exit',
  321. 'key_find',
  322. 'key_help',
  323. 'key_mark',
  324. 'key_message',
  325. 'key_move',
  326. 'key_next',
  327. 'key_open',
  328. 'key_options',
  329. 'key_previous',
  330. 'key_print',
  331. 'key_redo',
  332. 'key_reference',
  333. 'key_refresh',
  334. 'key_replace',
  335. 'key_restart',
  336. 'key_resume',
  337. 'key_save',
  338. 'key_suspend',
  339. 'key_undo',
  340. 'key_sbeg',
  341. 'key_scancel',
  342. 'key_scommand',
  343. 'key_scopy',
  344. 'key_screate',
  345. 'key_sdc',
  346. 'key_sdl',
  347. 'key_select',
  348. 'key_send',
  349. 'key_seol',
  350. 'key_sexit',
  351. 'key_sfind',
  352. 'key_shelp',
  353. 'key_shome',
  354. 'key_sic',
  355. 'key_sleft',
  356. 'key_smessage',
  357. 'key_smove',
  358. 'key_snext',
  359. 'key_soptions',
  360. 'key_sprevious',
  361. 'key_sprint',
  362. 'key_sredo',
  363. 'key_sreplace',
  364. 'key_sright',
  365. 'key_srsume',
  366. 'key_ssave',
  367. 'key_ssuspend',
  368. 'key_sundo',
  369. 'req_for_input',
  370. 'key_f11',
  371. 'key_f12',
  372. 'key_f13',
  373. 'key_f14',
  374. 'key_f15',
  375. 'key_f16',
  376. 'key_f17',
  377. 'key_f18',
  378. 'key_f19',
  379. 'key_f20',
  380. 'key_f21',
  381. 'key_f22',
  382. 'key_f23',
  383. 'key_f24',
  384. 'key_f25',
  385. 'key_f26',
  386. 'key_f27',
  387. 'key_f28',
  388. 'key_f29',
  389. 'key_f30',
  390. 'key_f31',
  391. 'key_f32',
  392. 'key_f33',
  393. 'key_f34',
  394. 'key_f35',
  395. 'key_f36',
  396. 'key_f37',
  397. 'key_f38',
  398. 'key_f39',
  399. 'key_f40',
  400. 'key_f41',
  401. 'key_f42',
  402. 'key_f43',
  403. 'key_f44',
  404. 'key_f45',
  405. 'key_f46',
  406. 'key_f47',
  407. 'key_f48',
  408. 'key_f49',
  409. 'key_f50',
  410. 'key_f51',
  411. 'key_f52',
  412. 'key_f53',
  413. 'key_f54',
  414. 'key_f55',
  415. 'key_f56',
  416. 'key_f57',
  417. 'key_f58',
  418. 'key_f59',
  419. 'key_f60',
  420. 'key_f61',
  421. 'key_f62',
  422. 'key_f63',
  423. 'clr_bol',
  424. 'clear_margins',
  425. 'set_left_margin',
  426. 'set_right_margin',
  427. 'label_format',
  428. 'set_clock',
  429. 'display_clock',
  430. 'remove_clock',
  431. 'create_window',
  432. 'goto_window',
  433. 'hangup',
  434. 'dial_phone',
  435. 'quick_dial',
  436. 'tone',
  437. 'pulse',
  438. 'flash_hook',
  439. 'fixed_pause',
  440. 'wait_tone',
  441. 'user0',
  442. 'user1',
  443. 'user2',
  444. 'user3',
  445. 'user4',
  446. 'user5',
  447. 'user6',
  448. 'user7',
  449. 'user8',
  450. 'user9',
  451. 'orig_pair',
  452. 'orig_colors',
  453. 'initialize_color',
  454. 'initialize_pair',
  455. 'set_color_pair',
  456. 'set_foreground',
  457. 'set_background',
  458. 'change_char_pitch',
  459. 'change_line_pitch',
  460. 'change_res_horz',
  461. 'change_res_vert',
  462. 'define_char',
  463. 'enter_doublewide_mode',
  464. 'enter_draft_quality',
  465. 'enter_italics_mode',
  466. 'enter_leftward_mode',
  467. 'enter_micro_mode',
  468. 'enter_near_letter_quality',
  469. 'enter_normal_quality',
  470. 'enter_shadow_mode',
  471. 'enter_subscript_mode',
  472. 'enter_superscript_mode',
  473. 'enter_upward_mode',
  474. 'exit_doublewide_mode',
  475. 'exit_italics_mode',
  476. 'exit_leftward_mode',
  477. 'exit_micro_mode',
  478. 'exit_shadow_mode',
  479. 'exit_subscript_mode',
  480. 'exit_superscript_mode',
  481. 'exit_upward_mode',
  482. 'micro_column_address',
  483. 'micro_down',
  484. 'micro_left',
  485. 'micro_right',
  486. 'micro_row_address',
  487. 'micro_up',
  488. 'order_of_pins',
  489. 'parm_down_micro',
  490. 'parm_left_micro',
  491. 'parm_right_micro',
  492. 'parm_up_micro',
  493. 'select_char_set',
  494. 'set_bottom_margin',
  495. 'set_bottom_margin_parm',
  496. 'set_left_margin_parm',
  497. 'set_right_margin_parm',
  498. 'set_top_margin',
  499. 'set_top_margin_parm',
  500. 'start_bit_image',
  501. 'start_char_set_def',
  502. 'stop_bit_image',
  503. 'stop_char_set_def',
  504. 'subscript_characters',
  505. 'superscript_characters',
  506. 'these_cause_cr',
  507. 'zero_motion',
  508. 'char_set_names',
  509. 'key_mouse',
  510. 'mouse_info',
  511. 'req_mouse_pos',
  512. 'get_mouse',
  513. 'set_a_foreground',
  514. 'set_a_background',
  515. 'pkey_plab',
  516. 'device_type',
  517. 'code_set_init',
  518. 'set0_des_seq',
  519. 'set1_des_seq',
  520. 'set2_des_seq',
  521. 'set3_des_seq',
  522. 'set_lr_margin',
  523. 'set_tb_margin',
  524. 'bit_image_repeat',
  525. 'bit_image_newline',
  526. 'bit_image_carriage_return',
  527. 'color_names',
  528. 'define_bit_image_region',
  529. 'end_bit_image_region',
  530. 'set_color_band',
  531. 'set_page_length',
  532. 'display_pc_char',
  533. 'enter_pc_charset_mode',
  534. 'exit_pc_charset_mode',
  535. 'enter_scancode_mode',
  536. 'exit_scancode_mode',
  537. 'pc_term_options',
  538. 'scancode_escape',
  539. 'alt_scancode_esc',
  540. 'enter_horizontal_hl_mode',
  541. 'enter_left_hl_mode',
  542. 'enter_low_hl_mode',
  543. 'enter_right_hl_mode',
  544. 'enter_top_hl_mode',
  545. 'enter_vertical_hl_mode',
  546. 'set_a_attributes',
  547. 'set_pglen_inch',
  548. // #ifdef __INTERNAL_CAPS_VISIBLE
  549. 'termcap_init2',
  550. 'termcap_reset',
  551. 'linefeed_if_not_lf',
  552. 'backspace_if_not_bs',
  553. 'other_non_function_keys',
  554. 'arrow_key_map',
  555. 'acs_ulcorner',
  556. 'acs_llcorner',
  557. 'acs_urcorner',
  558. 'acs_lrcorner',
  559. 'acs_ltee',
  560. 'acs_rtee',
  561. 'acs_btee',
  562. 'acs_ttee',
  563. 'acs_hline',
  564. 'acs_vline',
  565. 'acs_plus',
  566. 'memory_lock',
  567. 'memory_unlock',
  568. 'box_chars_1'
  569. ];
  570. /**
  571. * Computed informations.
  572. *
  573. * @var array
  574. */
  575. protected $_informations = [];
  576. /**
  577. * Set stream and parse.
  578. *
  579. * @param string $terminfo Terminfo file.
  580. */
  581. public function __construct($terminfo = null)
  582. {
  583. if (null === $terminfo) {
  584. $terminfo = static::getTerminfo();
  585. }
  586. $this->parse($terminfo);
  587. return;
  588. }
  589. /**
  590. * Parse.
  591. *
  592. * @param string $terminfo Terminfo file.
  593. * @return array
  594. * @throws \Hoa\Console\Exception
  595. */
  596. protected function parse($terminfo)
  597. {
  598. if (!file_exists($terminfo)) {
  599. throw new Exception(
  600. 'Terminfo file %s does not exist.',
  601. 0,
  602. $terminfo
  603. );
  604. }
  605. $data = file_get_contents($terminfo);
  606. $length = strlen($data);
  607. $out = ['file' => $terminfo];
  608. $headers = [
  609. 'data_size' => $length,
  610. 'header_size' => 12,
  611. 'magic_number' => (ord($data[ 1]) << 8) | ord($data[ 0]),
  612. 'names_size' => (ord($data[ 3]) << 8) | ord($data[ 2]),
  613. 'bool_count' => (ord($data[ 5]) << 8) | ord($data[ 4]),
  614. 'number_count' => (ord($data[ 7]) << 8) | ord($data[ 6]),
  615. 'string_count' => (ord($data[ 9]) << 8) | ord($data[ 8]),
  616. 'string_table_size' => (ord($data[11]) << 8) | ord($data[10]),
  617. ];
  618. $out['headers'] = $headers;
  619. // Names.
  620. $i = $headers['header_size'];
  621. $nameAndDescription = explode('|', substr($data, $i, $headers['names_size'] - 1));
  622. $out['name'] = $nameAndDescription[0];
  623. $out['description'] = $nameAndDescription[1];
  624. // Booleans.
  625. $i += $headers['names_size'];
  626. $booleans = [];
  627. $booleanNames = &static::$_booleans;
  628. for (
  629. $e = 0, $max = $i + $headers['bool_count'];
  630. $i < $max;
  631. ++$e, ++$i
  632. ) {
  633. $booleans[$booleanNames[$e]] = 1 === ord($data[$i]);
  634. }
  635. $out['booleans'] = $booleans;
  636. // Numbers.
  637. if (1 === ($i % 2)) {
  638. ++$i;
  639. }
  640. $numbers = [];
  641. $numberNames = &static::$_numbers;
  642. for (
  643. $e = 0, $max = $i + $headers['number_count'] * 2;
  644. $i < $max;
  645. ++$e, $i += 2
  646. ) {
  647. $name = $numberNames[$e];
  648. $data_i0 = ord($data[$i ]);
  649. $data_i1 = ord($data[$i + 1]);
  650. if ($data_i1 === 255 && $data_i0 === 255) {
  651. $numbers[$name] = -1;
  652. } else {
  653. $numbers[$name] = ($data_i1 << 8) | $data_i0;
  654. }
  655. }
  656. $out['numbers'] = $numbers;
  657. // Strings.
  658. $strings = [];
  659. $stringNames = &static::$_strings;
  660. $ii = $i + $headers['string_count'] * 2;
  661. for (
  662. $e = 0, $max = $ii;
  663. $i < $max;
  664. ++$e, $i += 2
  665. ) {
  666. $name = $stringNames[$e];
  667. $data_i0 = ord($data[$i ]);
  668. $data_i1 = ord($data[$i + 1]);
  669. if ($data_i1 === 255 && $data_i0 === 255) {
  670. continue;
  671. }
  672. $a = ($data_i1 << 8) | $data_i0;
  673. $strings[$name] = $a;
  674. if (65534 === $a) {
  675. continue;
  676. }
  677. $b = $ii + $a;
  678. $c = $b;
  679. while ($c < $length && ord($data[$c])) {
  680. $c++;
  681. }
  682. $value = substr($data, $b, $c - $b);
  683. $strings[$name] = false !== $value ? $value : null;
  684. }
  685. $out['strings'] = $strings;
  686. return $this->_informations = $out;
  687. }
  688. /**
  689. * Get all informations.
  690. *
  691. * @return array
  692. */
  693. public function getInformations()
  694. {
  695. return $this->_informations;
  696. }
  697. /**
  698. * Get a boolean value.
  699. *
  700. * @param bool $boolean Boolean.
  701. * @return bool
  702. */
  703. public function has($boolean)
  704. {
  705. if (!isset($this->_informations['booleans'][$boolean])) {
  706. return false;
  707. }
  708. return $this->_informations['booleans'][$boolean];
  709. }
  710. /**
  711. * Get a number value.
  712. *
  713. * @param int $number Number.
  714. * @return int
  715. */
  716. public function count($number)
  717. {
  718. if (!isset($this->_informations['numbers'][$number])) {
  719. return 0;
  720. }
  721. return $this->_informations['numbers'][$number];
  722. }
  723. /**
  724. * Get a string value.
  725. *
  726. * @param string $string String.
  727. * @return int
  728. */
  729. public function get($string)
  730. {
  731. if (!isset($this->_informations['strings'][$string])) {
  732. return null;
  733. }
  734. return $this->_informations['strings'][$string];
  735. }
  736. /**
  737. * Get current term profile.
  738. *
  739. * @return string
  740. */
  741. public static function getTerm()
  742. {
  743. return
  744. isset($_SERVER['TERM']) && !empty($_SERVER['TERM'])
  745. ? $_SERVER['TERM']
  746. : (OS_WIN ? 'windows-ansi' : 'xterm');
  747. }
  748. /**
  749. * Get pathname to the current terminfo.
  750. *
  751. * @param string $term Term.
  752. * @return string
  753. */
  754. public static function getTerminfo($term = null)
  755. {
  756. $paths = [];
  757. if (isset($_SERVER['TERMINFO'])) {
  758. $paths[] = $_SERVER['TERMINFO'];
  759. }
  760. if (isset($_SERVER['HOME'])) {
  761. $paths[] = $_SERVER['HOME'] . DS . '.terminfo';
  762. }
  763. if (isset($_SERVER['TERMINFO_DIRS'])) {
  764. foreach (explode(':', $_SERVER['TERMINFO_DIRS']) as $path) {
  765. $paths[] = $path;
  766. }
  767. }
  768. $paths[] = '/usr/share/terminfo';
  769. $paths[] = '/usr/share/lib/terminfo';
  770. $paths[] = '/lib/terminfo';
  771. $paths[] = '/usr/lib/terminfo';
  772. $paths[] = '/usr/local/share/terminfo';
  773. $paths[] = '/usr/local/share/lib/terminfo';
  774. $paths[] = '/usr/local/lib/terminfo';
  775. $paths[] = '/usr/local/ncurses/lib/terminfo';
  776. $paths[] = 'hoa://Library/Console/Terminfo';
  777. $term = $term ?: static::getTerm();
  778. $fileHexa = dechex(ord($term[0])) . DS . $term;
  779. $fileAlpha = $term[0] . DS . $term;
  780. $pathname = null;
  781. foreach ($paths as $path) {
  782. if (file_exists($_ = $path . DS . $fileHexa) ||
  783. file_exists($_ = $path . DS . $fileAlpha)) {
  784. $pathname = $_;
  785. break;
  786. }
  787. }
  788. if (null === $pathname && 'xterm' !== $term) {
  789. return static::getTerminfo('xterm');
  790. }
  791. return $pathname;
  792. }
  793. }