CLI.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. /*
  3. +----------------------------------------------------------------------+
  4. | PHP Version 5 |
  5. +----------------------------------------------------------------------+
  6. | Copyright (c) 1997-2004 The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.0 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_0.txt. |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | [email protected] so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. | Author: Stig Sæther Bakken <[email protected]> |
  17. +----------------------------------------------------------------------+
  18. $Id: CLI.php,v 1.41 2004/02/17 05:49:16 ssb Exp $
  19. */
  20. require_once "PEAR.php";
  21. class PEAR_Frontend_CLI extends PEAR
  22. {
  23. // {{{ properties
  24. /**
  25. * What type of user interface this frontend is for.
  26. * @var string
  27. * @access public
  28. */
  29. var $type = 'CLI';
  30. var $lp = ''; // line prefix
  31. var $params = array();
  32. var $term = array(
  33. 'bold' => '',
  34. 'normal' => '',
  35. );
  36. // }}}
  37. // {{{ constructor
  38. function PEAR_Frontend_CLI()
  39. {
  40. parent::PEAR();
  41. $term = getenv('TERM'); //(cox) $_ENV is empty for me in 4.1.1
  42. if (function_exists('posix_isatty') && !posix_isatty(1)) {
  43. // output is being redirected to a file or through a pipe
  44. } elseif ($term) {
  45. // XXX can use ncurses extension here, if available
  46. if (preg_match('/^(xterm|vt220|linux)/', $term)) {
  47. $this->term['bold'] = sprintf("%c%c%c%c", 27, 91, 49, 109);
  48. $this->term['normal']=sprintf("%c%c%c", 27, 91, 109);
  49. } elseif (preg_match('/^vt100/', $term)) {
  50. $this->term['bold'] = sprintf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0);
  51. $this->term['normal']=sprintf("%c%c%c%c%c", 27, 91, 109, 0, 0);
  52. }
  53. } elseif (OS_WINDOWS) {
  54. // XXX add ANSI codes here
  55. }
  56. }
  57. // }}}
  58. // {{{ displayLine(text)
  59. function displayLine($text)
  60. {
  61. trigger_error("PEAR_Frontend_CLI::displayLine deprecated", E_USER_ERROR);
  62. }
  63. function _displayLine($text)
  64. {
  65. print "$this->lp$text\n";
  66. }
  67. // }}}
  68. // {{{ display(text)
  69. function display($text)
  70. {
  71. trigger_error("PEAR_Frontend_CLI::display deprecated", E_USER_ERROR);
  72. }
  73. function _display($text)
  74. {
  75. print $text;
  76. }
  77. // }}}
  78. // {{{ displayError(eobj)
  79. /**
  80. * @param object PEAR_Error object
  81. */
  82. function displayError($eobj)
  83. {
  84. return $this->_displayLine($eobj->getMessage());
  85. }
  86. // }}}
  87. // {{{ displayFatalError(eobj)
  88. /**
  89. * @param object PEAR_Error object
  90. */
  91. function displayFatalError($eobj)
  92. {
  93. $this->displayError($eobj);
  94. exit(1);
  95. }
  96. // }}}
  97. // {{{ displayHeading(title)
  98. function displayHeading($title)
  99. {
  100. trigger_error("PEAR_Frontend_CLI::displayHeading deprecated", E_USER_ERROR);
  101. }
  102. function _displayHeading($title)
  103. {
  104. print $this->lp.$this->bold($title)."\n";
  105. print $this->lp.str_repeat("=", strlen($title))."\n";
  106. }
  107. // }}}
  108. // {{{ userDialog(prompt, [type], [default])
  109. function userDialog($command, $prompts, $types = array(), $defaults = array())
  110. {
  111. $result = array();
  112. if (is_array($prompts)) {
  113. $fp = fopen("php://stdin", "r");
  114. foreach ($prompts as $key => $prompt) {
  115. $type = $types[$key];
  116. $default = @$defaults[$key];
  117. if ($type == 'password') {
  118. system('stty -echo');
  119. }
  120. print "$this->lp$prompt ";
  121. if ($default) {
  122. print "[$default] ";
  123. }
  124. print ": ";
  125. $line = fgets($fp, 2048);
  126. if ($type == 'password') {
  127. system('stty echo');
  128. print "\n";
  129. }
  130. if ($default && trim($line) == "") {
  131. $result[$key] = $default;
  132. } else {
  133. $result[$key] = $line;
  134. }
  135. }
  136. fclose($fp);
  137. }
  138. return $result;
  139. }
  140. // }}}
  141. // {{{ userConfirm(prompt, [default])
  142. function userConfirm($prompt, $default = 'yes')
  143. {
  144. trigger_error("PEAR_Frontend_CLI::userConfirm not yet converted", E_USER_ERROR);
  145. static $positives = array('y', 'yes', 'on', '1');
  146. static $negatives = array('n', 'no', 'off', '0');
  147. print "$this->lp$prompt [$default] : ";
  148. $fp = fopen("php://stdin", "r");
  149. $line = fgets($fp, 2048);
  150. fclose($fp);
  151. $answer = strtolower(trim($line));
  152. if (empty($answer)) {
  153. $answer = $default;
  154. }
  155. if (in_array($answer, $positives)) {
  156. return true;
  157. }
  158. if (in_array($answer, $negatives)) {
  159. return false;
  160. }
  161. if (in_array($default, $positives)) {
  162. return true;
  163. }
  164. return false;
  165. }
  166. // }}}
  167. // {{{ startTable([params])
  168. function startTable($params = array())
  169. {
  170. trigger_error("PEAR_Frontend_CLI::startTable deprecated", E_USER_ERROR);
  171. }
  172. function _startTable($params = array())
  173. {
  174. $params['table_data'] = array();
  175. $params['widest'] = array(); // indexed by column
  176. $params['highest'] = array(); // indexed by row
  177. $params['ncols'] = 0;
  178. $this->params = $params;
  179. }
  180. // }}}
  181. // {{{ tableRow(columns, [rowparams], [colparams])
  182. function tableRow($columns, $rowparams = array(), $colparams = array())
  183. {
  184. trigger_error("PEAR_Frontend_CLI::tableRow deprecated", E_USER_ERROR);
  185. }
  186. function _tableRow($columns, $rowparams = array(), $colparams = array())
  187. {
  188. $highest = 1;
  189. for ($i = 0; $i < sizeof($columns); $i++) {
  190. $col = &$columns[$i];
  191. if (isset($colparams[$i]) && !empty($colparams[$i]['wrap'])) {
  192. $col = wordwrap($col, $colparams[$i]['wrap'], "\n", 0);
  193. }
  194. if (strpos($col, "\n") !== false) {
  195. $multiline = explode("\n", $col);
  196. $w = 0;
  197. foreach ($multiline as $n => $line) {
  198. if (strlen($line) > $w) {
  199. $w = strlen($line);
  200. }
  201. }
  202. $lines = sizeof($multiline);
  203. } else {
  204. $w = strlen($col);
  205. }
  206. if ($w > @$this->params['widest'][$i]) {
  207. $this->params['widest'][$i] = $w;
  208. }
  209. $tmp = count_chars($columns[$i], 1);
  210. // handle unix, mac and windows formats
  211. $lines = (isset($tmp[10]) ? $tmp[10] : @$tmp[13]) + 1;
  212. if ($lines > $highest) {
  213. $highest = $lines;
  214. }
  215. }
  216. if (sizeof($columns) > $this->params['ncols']) {
  217. $this->params['ncols'] = sizeof($columns);
  218. }
  219. $new_row = array(
  220. 'data' => $columns,
  221. 'height' => $highest,
  222. 'rowparams' => $rowparams,
  223. 'colparams' => $colparams,
  224. );
  225. $this->params['table_data'][] = $new_row;
  226. }
  227. // }}}
  228. // {{{ endTable()
  229. function endTable()
  230. {
  231. trigger_error("PEAR_Frontend_CLI::endTable deprecated", E_USER_ERROR);
  232. }
  233. function _endTable()
  234. {
  235. extract($this->params);
  236. if (!empty($caption)) {
  237. $this->_displayHeading($caption);
  238. }
  239. if (count($table_data) == 0) {
  240. return;
  241. }
  242. if (!isset($width)) {
  243. $width = $widest;
  244. } else {
  245. for ($i = 0; $i < $ncols; $i++) {
  246. if (!isset($width[$i])) {
  247. $width[$i] = $widest[$i];
  248. }
  249. }
  250. }
  251. $border = false;
  252. if (empty($border)) {
  253. $cellstart = '';
  254. $cellend = ' ';
  255. $rowend = '';
  256. $padrowend = false;
  257. $borderline = '';
  258. } else {
  259. $cellstart = '| ';
  260. $cellend = ' ';
  261. $rowend = '|';
  262. $padrowend = true;
  263. $borderline = '+';
  264. foreach ($width as $w) {
  265. $borderline .= str_repeat('-', $w + strlen($cellstart) + strlen($cellend) - 1);
  266. $borderline .= '+';
  267. }
  268. }
  269. if ($borderline) {
  270. $this->_displayLine($borderline);
  271. }
  272. for ($i = 0; $i < sizeof($table_data); $i++) {
  273. extract($table_data[$i]);
  274. if (!is_array($rowparams)) {
  275. $rowparams = array();
  276. }
  277. if (!is_array($colparams)) {
  278. $colparams = array();
  279. }
  280. $rowlines = array();
  281. if ($height > 1) {
  282. for ($c = 0; $c < sizeof($data); $c++) {
  283. $rowlines[$c] = preg_split('/(\r?\n|\r)/', $data[$c]);
  284. if (sizeof($rowlines[$c]) < $height) {
  285. $rowlines[$c] = array_pad($rowlines[$c], $height, '');
  286. }
  287. }
  288. } else {
  289. for ($c = 0; $c < sizeof($data); $c++) {
  290. $rowlines[$c] = array($data[$c]);
  291. }
  292. }
  293. for ($r = 0; $r < $height; $r++) {
  294. $rowtext = '';
  295. for ($c = 0; $c < sizeof($data); $c++) {
  296. if (isset($colparams[$c])) {
  297. $attribs = array_merge($rowparams, $colparams);
  298. } else {
  299. $attribs = $rowparams;
  300. }
  301. $w = isset($width[$c]) ? $width[$c] : 0;
  302. //$cell = $data[$c];
  303. $cell = $rowlines[$c][$r];
  304. $l = strlen($cell);
  305. if ($l > $w) {
  306. $cell = substr($cell, 0, $w);
  307. }
  308. if (isset($attribs['bold'])) {
  309. $cell = $this->bold($cell);
  310. }
  311. if ($l < $w) {
  312. // not using str_pad here because we may
  313. // add bold escape characters to $cell
  314. $cell .= str_repeat(' ', $w - $l);
  315. }
  316. $rowtext .= $cellstart . $cell . $cellend;
  317. }
  318. if (!$border) {
  319. $rowtext = rtrim($rowtext);
  320. }
  321. $rowtext .= $rowend;
  322. $this->_displayLine($rowtext);
  323. }
  324. }
  325. if ($borderline) {
  326. $this->_displayLine($borderline);
  327. }
  328. }
  329. // }}}
  330. // {{{ outputData()
  331. function outputData($data, $command = '_default')
  332. {
  333. switch ($command) {
  334. case 'install':
  335. case 'upgrade':
  336. case 'upgrade-all':
  337. if (isset($data['release_warnings'])) {
  338. $this->_displayLine('');
  339. $this->_startTable(array(
  340. 'border' => false,
  341. 'caption' => 'Release Warnings'
  342. ));
  343. $this->_tableRow(array($data['release_warnings']), null, array(1 => array('wrap' => 55)));
  344. $this->_endTable();
  345. $this->_displayLine('');
  346. }
  347. $this->_displayLine($data['data']);
  348. break;
  349. case 'search':
  350. $this->_startTable($data);
  351. if (isset($data['headline']) && is_array($data['headline'])) {
  352. $this->_tableRow($data['headline'], array('bold' => true), array(1 => array('wrap' => 55)));
  353. }
  354. foreach($data['data'] as $category) {
  355. foreach($category as $pkg) {
  356. $this->_tableRow($pkg, null, array(1 => array('wrap' => 55)));
  357. }
  358. };
  359. $this->_endTable();
  360. break;
  361. case 'list-all':
  362. $this->_startTable($data);
  363. if (isset($data['headline']) && is_array($data['headline'])) {
  364. $this->_tableRow($data['headline'], array('bold' => true), array(1 => array('wrap' => 55)));
  365. }
  366. foreach($data['data'] as $category) {
  367. foreach($category as $pkg) {
  368. unset($pkg[3]);
  369. unset($pkg[4]);
  370. $this->_tableRow($pkg, null, array(1 => array('wrap' => 55)));
  371. }
  372. };
  373. $this->_endTable();
  374. break;
  375. case 'config-show':
  376. $data['border'] = false;
  377. $opts = array(0 => array('wrap' => 30),
  378. 1 => array('wrap' => 20),
  379. 2 => array('wrap' => 35));
  380. $this->_startTable($data);
  381. if (isset($data['headline']) && is_array($data['headline'])) {
  382. $this->_tableRow($data['headline'],
  383. array('bold' => true),
  384. $opts);
  385. }
  386. foreach($data['data'] as $group) {
  387. foreach($group as $value) {
  388. if ($value[2] == '') {
  389. $value[2] = "<not set>";
  390. }
  391. $this->_tableRow($value, null, $opts);
  392. }
  393. }
  394. $this->_endTable();
  395. break;
  396. case 'remote-info':
  397. $data = array(
  398. 'caption' => 'Package details:',
  399. 'border' => false,
  400. 'data' => array(
  401. array("Latest", $data['stable']),
  402. array("Installed", $data['installed']),
  403. array("Package", $data['name']),
  404. array("License", $data['license']),
  405. array("Category", $data['category']),
  406. array("Summary", $data['summary']),
  407. array("Description", $data['description']),
  408. ),
  409. );
  410. default: {
  411. if (is_array($data)) {
  412. $this->_startTable($data);
  413. $count = count($data['data'][0]);
  414. if ($count == 2) {
  415. $opts = array(0 => array('wrap' => 25),
  416. 1 => array('wrap' => 48)
  417. );
  418. } elseif ($count == 3) {
  419. $opts = array(0 => array('wrap' => 30),
  420. 1 => array('wrap' => 20),
  421. 2 => array('wrap' => 35)
  422. );
  423. } else {
  424. $opts = null;
  425. }
  426. if (isset($data['headline']) && is_array($data['headline'])) {
  427. $this->_tableRow($data['headline'],
  428. array('bold' => true),
  429. $opts);
  430. }
  431. foreach($data['data'] as $row) {
  432. $this->_tableRow($row, null, $opts);
  433. }
  434. $this->_endTable();
  435. } else {
  436. $this->_displayLine($data);
  437. }
  438. }
  439. }
  440. }
  441. // }}}
  442. // {{{ log(text)
  443. function log($text, $append_crlf = true)
  444. {
  445. if ($append_crlf) {
  446. return $this->_displayLine($text);
  447. }
  448. return $this->_display($text);
  449. }
  450. // }}}
  451. // {{{ bold($text)
  452. function bold($text)
  453. {
  454. if (empty($this->term['bold'])) {
  455. return strtoupper($text);
  456. }
  457. return $this->term['bold'] . $text . $this->term['normal'];
  458. }
  459. // }}}
  460. }
  461. ?>