phpdbg_cmd.c 19 KB

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