prompts.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Functions for making, destroying, and manipulating prompts_t
  3. * structures.
  4. */
  5. #include "putty.h"
  6. prompts_t *new_prompts(void)
  7. {
  8. prompts_t *p = snew(prompts_t);
  9. p->prompts = NULL;
  10. p->n_prompts = p->prompts_size = 0;
  11. p->data = NULL;
  12. p->spr = SPR_INCOMPLETE;
  13. p->to_server = true; /* to be on the safe side */
  14. p->name = p->instruction = NULL;
  15. p->name_reqd = p->instr_reqd = false;
  16. p->callback = NULL;
  17. p->callback_ctx = NULL;
  18. p->ldisc_ptr_to_us = NULL;
  19. p->utf8 = false;
  20. return p;
  21. }
  22. void add_prompt(prompts_t *p, char *promptstr, bool echo)
  23. {
  24. prompt_t *pr = snew(prompt_t);
  25. pr->prompt = promptstr;
  26. pr->echo = echo;
  27. pr->result = strbuf_new_nm();
  28. sgrowarray(p->prompts, p->prompts_size, p->n_prompts);
  29. p->prompts[p->n_prompts++] = pr;
  30. }
  31. void prompt_set_result(prompt_t *pr, const char *newstr)
  32. {
  33. strbuf_clear(pr->result);
  34. put_dataz(pr->result, newstr);
  35. }
  36. const char *prompt_get_result_ref(prompt_t *pr)
  37. {
  38. return pr->result->s;
  39. }
  40. char *prompt_get_result(prompt_t *pr)
  41. {
  42. return dupstr(pr->result->s);
  43. }
  44. void free_prompts(prompts_t *p)
  45. {
  46. size_t i;
  47. /* If an Ldisc currently knows about us, tell it to forget us, so
  48. * it won't dereference a stale pointer later. */
  49. if (p->ldisc_ptr_to_us)
  50. *p->ldisc_ptr_to_us = NULL;
  51. for (i=0; i < p->n_prompts; i++) {
  52. prompt_t *pr = p->prompts[i];
  53. strbuf_free(pr->result);
  54. sfree(pr->prompt);
  55. sfree(pr);
  56. }
  57. sfree(p->prompts);
  58. sfree(p->name);
  59. sfree(p->instruction);
  60. sfree(p);
  61. }