phpdbg_cmd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Felipe Pena <felipe@php.net> |
  16. | Authors: Joe Watkins <joe.watkins@live.co.uk> |
  17. | Authors: Bob Weinand <bwoebi@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "phpdbg.h"
  21. #include "phpdbg_cmd.h"
  22. #include "phpdbg_utils.h"
  23. #include "phpdbg_set.h"
  24. #include "phpdbg_prompt.h"
  25. ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
  26. static inline const char *phpdbg_command_name(const phpdbg_command_t *command, char *buffer) {
  27. size_t pos = 0;
  28. if (command->parent) {
  29. memcpy(&buffer[pos], command->parent->name, command->parent->name_len);
  30. pos += command->parent->name_len;
  31. memcpy(&buffer[pos], " ", sizeof(" ")-1);
  32. pos += (sizeof(" ")-1);
  33. }
  34. memcpy(&buffer[pos], command->name, command->name_len);
  35. pos += command->name_len;
  36. buffer[pos] = 0;
  37. return buffer;
  38. }
  39. PHPDBG_API const char *phpdbg_get_param_type(const phpdbg_param_t *param TSRMLS_DC) /* {{{ */
  40. {
  41. switch (param->type) {
  42. case STACK_PARAM:
  43. return "stack";
  44. case EMPTY_PARAM:
  45. return "empty";
  46. case ADDR_PARAM:
  47. return "address";
  48. case NUMERIC_PARAM:
  49. return "numeric";
  50. case METHOD_PARAM:
  51. return "method";
  52. case NUMERIC_FUNCTION_PARAM:
  53. return "function opline";
  54. case NUMERIC_METHOD_PARAM:
  55. return "method opline";
  56. case FILE_PARAM:
  57. return "file or file opline";
  58. case STR_PARAM:
  59. return "string";
  60. default: /* this is bad */
  61. return "unknown";
  62. }
  63. }
  64. PHPDBG_API void phpdbg_clear_param(phpdbg_param_t *param TSRMLS_DC) /* {{{ */
  65. {
  66. if (param) {
  67. switch (param->type) {
  68. case FILE_PARAM:
  69. efree(param->file.name);
  70. break;
  71. case METHOD_PARAM:
  72. efree(param->method.class);
  73. efree(param->method.name);
  74. break;
  75. case STR_PARAM:
  76. efree(param->str);
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. } /* }}} */
  83. PHPDBG_API char* phpdbg_param_tostring(const phpdbg_param_t *param, char **pointer TSRMLS_DC) /* {{{ */
  84. {
  85. switch (param->type) {
  86. case STR_PARAM:
  87. asprintf(pointer,
  88. "%s", param->str);
  89. break;
  90. case ADDR_PARAM:
  91. asprintf(pointer,
  92. "%#lx", param->addr);
  93. break;
  94. case NUMERIC_PARAM:
  95. asprintf(pointer,
  96. "%li",
  97. param->num);
  98. break;
  99. case METHOD_PARAM:
  100. asprintf(pointer,
  101. "%s::%s",
  102. param->method.class,
  103. param->method.name);
  104. break;
  105. case FILE_PARAM:
  106. if (param->num) {
  107. asprintf(pointer,
  108. "%s:%lu#%lu",
  109. param->file.name,
  110. param->file.line,
  111. param->num);
  112. } else {
  113. asprintf(pointer,
  114. "%s:%lu",
  115. param->file.name,
  116. param->file.line);
  117. }
  118. break;
  119. case NUMERIC_FUNCTION_PARAM:
  120. asprintf(pointer,
  121. "%s#%lu", param->str, param->num);
  122. break;
  123. case NUMERIC_METHOD_PARAM:
  124. asprintf(pointer,
  125. "%s::%s#%lu",
  126. param->method.class,
  127. param->method.name,
  128. param->num);
  129. break;
  130. default:
  131. asprintf(pointer,
  132. "%s", "unknown");
  133. }
  134. return *pointer;
  135. } /* }}} */
  136. PHPDBG_API void phpdbg_copy_param(const phpdbg_param_t* src, phpdbg_param_t* dest TSRMLS_DC) /* {{{ */
  137. {
  138. switch ((dest->type = src->type)) {
  139. case STACK_PARAM:
  140. /* nope */
  141. break;
  142. case STR_PARAM:
  143. dest->str = estrndup(src->str, src->len);
  144. dest->len = src->len;
  145. break;
  146. case OP_PARAM:
  147. dest->str = estrndup(src->str, src->len);
  148. dest->len = src->len;
  149. break;
  150. case ADDR_PARAM:
  151. dest->addr = src->addr;
  152. break;
  153. case NUMERIC_PARAM:
  154. dest->num = src->num;
  155. break;
  156. case METHOD_PARAM:
  157. dest->method.class = estrdup(src->method.class);
  158. dest->method.name = estrdup(src->method.name);
  159. break;
  160. case NUMERIC_FILE_PARAM:
  161. case FILE_PARAM:
  162. dest->file.name = estrdup(src->file.name);
  163. dest->file.line = src->file.line;
  164. if (src->num)
  165. dest->num = src->num;
  166. break;
  167. case NUMERIC_FUNCTION_PARAM:
  168. dest->str = estrndup(src->str, src->len);
  169. dest->num = src->num;
  170. dest->len = src->len;
  171. break;
  172. case NUMERIC_METHOD_PARAM:
  173. dest->method.class = estrdup(src->method.class);
  174. dest->method.name = estrdup(src->method.name);
  175. dest->num = src->num;
  176. break;
  177. case EMPTY_PARAM: { /* do nothing */ } break;
  178. default: {
  179. /* not yet */
  180. }
  181. }
  182. } /* }}} */
  183. PHPDBG_API zend_ulong phpdbg_hash_param(const phpdbg_param_t *param TSRMLS_DC) /* {{{ */
  184. {
  185. zend_ulong hash = param->type;
  186. switch (param->type) {
  187. case STACK_PARAM:
  188. /* nope */
  189. break;
  190. case STR_PARAM:
  191. hash += zend_inline_hash_func(param->str, param->len);
  192. break;
  193. case METHOD_PARAM:
  194. hash += zend_inline_hash_func(param->method.class, strlen(param->method.class));
  195. hash += zend_inline_hash_func(param->method.name, strlen(param->method.name));
  196. break;
  197. case FILE_PARAM:
  198. hash += zend_inline_hash_func(param->file.name, strlen(param->file.name));
  199. hash += param->file.line;
  200. if (param->num)
  201. hash += param->num;
  202. break;
  203. case ADDR_PARAM:
  204. hash += param->addr;
  205. break;
  206. case NUMERIC_PARAM:
  207. hash += param->num;
  208. break;
  209. case NUMERIC_FUNCTION_PARAM:
  210. hash += zend_inline_hash_func(param->str, param->len);
  211. hash += param->num;
  212. break;
  213. case NUMERIC_METHOD_PARAM:
  214. hash += zend_inline_hash_func(param->method.class, strlen(param->method.class));
  215. hash += zend_inline_hash_func(param->method.name, strlen(param->method.name));
  216. if (param->num)
  217. hash+= param->num;
  218. break;
  219. case EMPTY_PARAM: { /* do nothing */ } break;
  220. default: {
  221. /* not yet */
  222. }
  223. }
  224. return hash;
  225. } /* }}} */
  226. PHPDBG_API zend_bool phpdbg_match_param(const phpdbg_param_t *l, const phpdbg_param_t *r TSRMLS_DC) /* {{{ */
  227. {
  228. if (l && r) {
  229. if (l->type == r->type) {
  230. switch (l->type) {
  231. case STACK_PARAM:
  232. /* nope, or yep */
  233. return 1;
  234. break;
  235. case NUMERIC_FUNCTION_PARAM:
  236. if (l->num != r->num) {
  237. break;
  238. }
  239. /* break intentionally omitted */
  240. case STR_PARAM:
  241. return (l->len == r->len) &&
  242. (memcmp(l->str, r->str, l->len) == SUCCESS);
  243. case NUMERIC_PARAM:
  244. return (l->num == r->num);
  245. case ADDR_PARAM:
  246. return (l->addr == r->addr);
  247. case FILE_PARAM: {
  248. if (l->file.line == r->file.line) {
  249. size_t lengths[2] = {
  250. strlen(l->file.name), strlen(r->file.name)};
  251. if (lengths[0] == lengths[1]) {
  252. if ((!l->num && !r->num) || (l->num == r->num)) {
  253. return (memcmp(
  254. l->file.name, r->file.name, lengths[0]) == SUCCESS);
  255. }
  256. }
  257. }
  258. } break;
  259. case NUMERIC_METHOD_PARAM:
  260. if (l->num != r->num) {
  261. break;
  262. }
  263. /* break intentionally omitted */
  264. case METHOD_PARAM: {
  265. size_t lengths[2] = {
  266. strlen(l->method.class), strlen(r->method.class)};
  267. if (lengths[0] == lengths[1]) {
  268. if (memcmp(l->method.class, r->method.class, lengths[0]) == SUCCESS) {
  269. lengths[0] = strlen(l->method.name);
  270. lengths[1] = strlen(r->method.name);
  271. if (lengths[0] == lengths[1]) {
  272. return (memcmp(
  273. l->method.name, r->method.name, lengths[0]) == SUCCESS);
  274. }
  275. }
  276. }
  277. } break;
  278. case EMPTY_PARAM:
  279. return 1;
  280. default: {
  281. /* not yet */
  282. }
  283. }
  284. }
  285. }
  286. return 0;
  287. } /* }}} */
  288. /* {{{ */
  289. PHPDBG_API void phpdbg_param_debug(const phpdbg_param_t *param, const char *msg) {
  290. if (param && param->type) {
  291. switch (param->type) {
  292. case STR_PARAM:
  293. fprintf(stderr, "%s STR_PARAM(%s=%lu)\n", msg, param->str, param->len);
  294. break;
  295. case ADDR_PARAM:
  296. fprintf(stderr, "%s ADDR_PARAM(%lu)\n", msg, param->addr);
  297. break;
  298. case NUMERIC_FILE_PARAM:
  299. fprintf(stderr, "%s NUMERIC_FILE_PARAM(%s:#%lu)\n", msg, param->file.name, param->file.line);
  300. break;
  301. case FILE_PARAM:
  302. fprintf(stderr, "%s FILE_PARAM(%s:%lu)\n", msg, param->file.name, param->file.line);
  303. break;
  304. case METHOD_PARAM:
  305. fprintf(stderr, "%s METHOD_PARAM(%s::%s)\n", msg, param->method.class, param->method.name);
  306. break;
  307. case NUMERIC_METHOD_PARAM:
  308. fprintf(stderr, "%s NUMERIC_METHOD_PARAM(%s::%s)\n", msg, param->method.class, param->method.name);
  309. break;
  310. case NUMERIC_FUNCTION_PARAM:
  311. fprintf(stderr, "%s NUMERIC_FUNCTION_PARAM(%s::%ld)\n", msg, param->str, param->num);
  312. break;
  313. case NUMERIC_PARAM:
  314. fprintf(stderr, "%s NUMERIC_PARAM(%ld)\n", msg, param->num);
  315. break;
  316. case COND_PARAM:
  317. fprintf(stderr, "%s COND_PARAM(%s=%lu)\n", msg, param->str, param->len);
  318. break;
  319. case OP_PARAM:
  320. fprintf(stderr, "%s OP_PARAM(%s=%lu)\n", msg, param->str, param->len);
  321. break;
  322. default: {
  323. /* not yet */
  324. }
  325. }
  326. }
  327. } /* }}} */
  328. /* {{{ */
  329. PHPDBG_API void phpdbg_stack_free(phpdbg_param_t *stack) {
  330. if (stack && stack->next) {
  331. phpdbg_param_t *remove = stack->next;
  332. while (remove) {
  333. phpdbg_param_t *next = NULL;
  334. if (remove->next)
  335. next = remove->next;
  336. switch (remove->type) {
  337. case NUMERIC_METHOD_PARAM:
  338. case METHOD_PARAM:
  339. if (remove->method.class)
  340. free(remove->method.class);
  341. if (remove->method.name)
  342. free(remove->method.name);
  343. break;
  344. case NUMERIC_FUNCTION_PARAM:
  345. case STR_PARAM:
  346. case OP_PARAM:
  347. if (remove->str)
  348. free(remove->str);
  349. break;
  350. case NUMERIC_FILE_PARAM:
  351. case FILE_PARAM:
  352. if (remove->file.name)
  353. free(remove->file.name);
  354. break;
  355. default: {
  356. /* nothing */
  357. }
  358. }
  359. free(remove);
  360. remove = NULL;
  361. if (next)
  362. remove = next;
  363. else break;
  364. }
  365. }
  366. stack->next = NULL;
  367. } /* }}} */
  368. /* {{{ */
  369. PHPDBG_API void phpdbg_stack_push(phpdbg_param_t *stack, phpdbg_param_t *param) {
  370. phpdbg_param_t *next = calloc(1, sizeof(phpdbg_param_t));
  371. if (!next)
  372. return;
  373. *(next) = *(param);
  374. next->next = NULL;
  375. if (stack->top == NULL) {
  376. stack->top = next;
  377. next->top = NULL;
  378. stack->next = next;
  379. } else {
  380. stack->top->next = next;
  381. next->top = stack->top;
  382. stack->top = next;
  383. }
  384. stack->len++;
  385. } /* }}} */
  386. PHPDBG_API int phpdbg_stack_verify(const phpdbg_command_t *command, phpdbg_param_t **stack, char **why TSRMLS_DC) {
  387. if (command) {
  388. char buffer[128] = {0,};
  389. const phpdbg_param_t *top = (stack != NULL) ? *stack : NULL;
  390. const char *arg = command->args;
  391. size_t least = 0L,
  392. received = 0L,
  393. current = 0L;
  394. zend_bool optional = 0;
  395. /* check for arg spec */
  396. if (!(arg) || !(*arg)) {
  397. if (!top) {
  398. return SUCCESS;
  399. }
  400. asprintf(why,
  401. "The command \"%s\" expected no arguments",
  402. phpdbg_command_name(command, buffer));
  403. return FAILURE;
  404. }
  405. least = 0L;
  406. /* count least amount of arguments */
  407. while (arg && *arg) {
  408. if (arg[0] == '|') {
  409. break;
  410. }
  411. least++;
  412. arg++;
  413. }
  414. arg = command->args;
  415. #define verify_arg(e, a, t) if (!(a)) { \
  416. if (!optional) { \
  417. asprintf(why, \
  418. "The command \"%s\" expected %s and got nothing at parameter %lu", \
  419. phpdbg_command_name(command, buffer), \
  420. (e), \
  421. current); \
  422. return FAILURE;\
  423. } \
  424. } else if ((a)->type != (t)) { \
  425. asprintf(why, \
  426. "The command \"%s\" expected %s and got %s at parameter %lu", \
  427. phpdbg_command_name(command, buffer), \
  428. (e),\
  429. phpdbg_get_param_type((a) TSRMLS_CC), \
  430. current); \
  431. return FAILURE; \
  432. }
  433. while (arg && *arg) {
  434. current++;
  435. switch (*arg) {
  436. case '|': {
  437. current--;
  438. optional = 1;
  439. arg++;
  440. } continue;
  441. case 'i': verify_arg("raw input", top, STR_PARAM); break;
  442. case 's': verify_arg("string", top, STR_PARAM); break;
  443. case 'n': verify_arg("number", top, NUMERIC_PARAM); break;
  444. case 'm': verify_arg("method", top, METHOD_PARAM); break;
  445. case 'a': verify_arg("address", top, ADDR_PARAM); break;
  446. case 'f': verify_arg("file:line", top, FILE_PARAM); break;
  447. case 'c': verify_arg("condition", top, COND_PARAM); break;
  448. case 'o': verify_arg("opcode", top, OP_PARAM); break;
  449. case 'b': verify_arg("boolean", top, NUMERIC_PARAM); break;
  450. case '*': { /* do nothing */ } break;
  451. }
  452. if (top ) {
  453. top = top->next;
  454. } else break;
  455. received++;
  456. arg++;
  457. }
  458. #undef verify_arg
  459. if ((received < least)) {
  460. asprintf(why,
  461. "The command \"%s\" expected at least %lu arguments (%s) and received %lu",
  462. phpdbg_command_name(command, buffer),
  463. least,
  464. command->args,
  465. received);
  466. return FAILURE;
  467. }
  468. }
  469. return SUCCESS;
  470. }
  471. /* {{{ */
  472. PHPDBG_API const phpdbg_command_t* phpdbg_stack_resolve(const phpdbg_command_t *commands, const phpdbg_command_t *parent, phpdbg_param_t **top, char **why) {
  473. const phpdbg_command_t *command = commands;
  474. phpdbg_param_t *name = *top;
  475. const phpdbg_command_t *matched[3] = {NULL, NULL, NULL};
  476. ulong matches = 0L;
  477. while (command && command->name && command->handler) {
  478. if ((name->len == 1) || (command->name_len >= name->len)) {
  479. /* match single letter alias */
  480. if (command->alias && (name->len == 1)) {
  481. if (command->alias == (*name->str)) {
  482. matched[matches] = command;
  483. matches++;
  484. }
  485. } else {
  486. /* match full, case insensitive, command name */
  487. if (strncasecmp(command->name, name->str, name->len) == SUCCESS) {
  488. if (matches < 3) {
  489. /* only allow abbreviating commands that can be aliased */
  490. if (((name->len != command->name_len) && command->alias) ||
  491. (name->len == command->name_len)) {
  492. matched[matches] = command;
  493. matches++;
  494. }
  495. /* exact match */
  496. if (name->len == command->name_len)
  497. break;
  498. } else break;
  499. }
  500. }
  501. }
  502. command++;
  503. }
  504. switch (matches) {
  505. case 0: {
  506. if (parent) {
  507. asprintf(
  508. why,
  509. "The command \"%s %s\" could not be found",
  510. parent->name, name->str);
  511. } else asprintf(
  512. why,
  513. "The command \"%s\" could not be found",
  514. name->str);
  515. } return parent;
  516. case 1: {
  517. (*top) = (*top)->next;
  518. command = matched[0];
  519. } break;
  520. default: {
  521. char *list = NULL;
  522. zend_uint it = 0;
  523. size_t pos = 0;
  524. while (it < matches) {
  525. if (!list) {
  526. list = malloc(
  527. matched[it]->name_len + 1 +
  528. ((it+1) < matches ? sizeof(", ")-1 : 0));
  529. } else {
  530. list = realloc(list,
  531. (pos + matched[it]->name_len) + 1 +
  532. ((it+1) < matches ? sizeof(", ")-1 : 0));
  533. }
  534. memcpy(&list[pos], matched[it]->name, matched[it]->name_len);
  535. pos += matched[it]->name_len;
  536. if ((it+1) < matches) {
  537. memcpy(&list[pos], ", ", sizeof(", ")-1);
  538. pos += (sizeof(", ") - 1);
  539. }
  540. list[pos] = 0;
  541. it++;
  542. }
  543. asprintf(
  544. why,
  545. "The command \"%s\" is ambigious, matching %lu commands (%s)",
  546. name->str, matches, list);
  547. free(list);
  548. } return NULL;
  549. }
  550. if (command->subs && (*top) && ((*top)->type == STR_PARAM)) {
  551. return phpdbg_stack_resolve(command->subs, command, top, why);
  552. } else {
  553. return command;
  554. }
  555. return NULL;
  556. } /* }}} */
  557. /* {{{ */
  558. PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack, char **why TSRMLS_DC) {
  559. phpdbg_param_t *top = NULL;
  560. const phpdbg_command_t *handler = NULL;
  561. if (stack->type != STACK_PARAM) {
  562. asprintf(
  563. why, "The passed argument was not a stack !!");
  564. return FAILURE;
  565. }
  566. if (!stack->len) {
  567. asprintf(
  568. why, "The stack contains nothing !!");
  569. return FAILURE;
  570. }
  571. top = (phpdbg_param_t*) stack->next;
  572. switch (top->type) {
  573. case EVAL_PARAM:
  574. return PHPDBG_COMMAND_HANDLER(ev)(top TSRMLS_CC);
  575. case RUN_PARAM:
  576. return PHPDBG_COMMAND_HANDLER(run)(top TSRMLS_CC);
  577. case SHELL_PARAM:
  578. return PHPDBG_COMMAND_HANDLER(sh)(top TSRMLS_CC);
  579. case STR_PARAM: {
  580. handler = phpdbg_stack_resolve(
  581. phpdbg_prompt_commands, NULL, &top, why);
  582. if (handler) {
  583. if (phpdbg_stack_verify(handler, &top, why TSRMLS_CC) == SUCCESS) {
  584. return handler->handler(top TSRMLS_CC);
  585. }
  586. }
  587. } return FAILURE;
  588. default:
  589. asprintf(
  590. why, "The first parameter makes no sense !!");
  591. return FAILURE;
  592. }
  593. return SUCCESS;
  594. } /* }}} */
  595. PHPDBG_API char* phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
  596. {
  597. char *cmd = NULL;
  598. #if !defined(HAVE_LIBREADLINE) && !defined(HAVE_LIBEDIT)
  599. char buf[PHPDBG_MAX_CMD];
  600. #endif
  601. char *buffer = NULL;
  602. if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
  603. if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE) &&
  604. (buffered == NULL)) {
  605. fflush(PHPDBG_G(io)[PHPDBG_STDOUT]);
  606. }
  607. if (buffered == NULL) {
  608. disconnect:
  609. if (0) {
  610. PHPDBG_G(flags) |= (PHPDBG_IS_QUITTING|PHPDBG_IS_DISCONNECTED);
  611. zend_bailout();
  612. return NULL;
  613. }
  614. #if !defined(HAVE_LIBREADLINE) && !defined(HAVE_LIBEDIT)
  615. if (!(PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {
  616. if (!phpdbg_write("%s", phpdbg_get_prompt(TSRMLS_C))) {
  617. goto disconnect;
  618. }
  619. }
  620. /* note: EOF is ignored */
  621. readline:
  622. if (!fgets(buf, PHPDBG_MAX_CMD, PHPDBG_G(io)[PHPDBG_STDIN])) {
  623. /* the user has gone away */
  624. if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {
  625. goto disconnect;
  626. } else goto readline;
  627. }
  628. cmd = buf;
  629. #else
  630. /* note: EOF makes readline write prompt again in local console mode */
  631. readline:
  632. if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {
  633. char buf[PHPDBG_MAX_CMD];
  634. if (fgets(buf, PHPDBG_MAX_CMD, PHPDBG_G(io)[PHPDBG_STDIN])) {
  635. cmd = buf;
  636. } else goto disconnect;
  637. } else cmd = readline(phpdbg_get_prompt(TSRMLS_C));
  638. if (!cmd) {
  639. goto readline;
  640. }
  641. if (!(PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {
  642. add_history(cmd);
  643. }
  644. #endif
  645. } else cmd = buffered;
  646. buffer = estrdup(cmd);
  647. #if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDIT)
  648. if (!buffered && cmd &&
  649. !(PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {
  650. free(cmd);
  651. }
  652. #endif
  653. }
  654. if (buffer && isspace(*buffer)) {
  655. char *trimmed = buffer;
  656. while (isspace(*trimmed))
  657. trimmed++;
  658. trimmed = estrdup(trimmed);
  659. efree(buffer);
  660. buffer = trimmed;
  661. }
  662. if (buffer && strlen(buffer)) {
  663. if (PHPDBG_G(buffer)) {
  664. efree(PHPDBG_G(buffer));
  665. }
  666. PHPDBG_G(buffer) = estrdup(buffer);
  667. } else {
  668. if (PHPDBG_G(buffer)) {
  669. buffer = estrdup(PHPDBG_G(buffer));
  670. }
  671. }
  672. return buffer;
  673. } /* }}} */
  674. PHPDBG_API void phpdbg_destroy_input(char **input TSRMLS_DC) /*{{{ */
  675. {
  676. efree(*input);
  677. } /* }}} */