sshcr.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Coroutine mechanics used in PuTTY's SSH code.
  3. */
  4. #ifndef PUTTY_SSHCR_H
  5. #define PUTTY_SSHCR_H
  6. /*
  7. * If these macros look impenetrable to you, you might find it helpful
  8. * to read
  9. *
  10. * https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
  11. *
  12. * which explains the theory behind these macros.
  13. *
  14. * In particular, if you are getting `case expression not constant'
  15. * errors when building with MS Visual Studio, this is because MS's
  16. * Edit and Continue debugging feature causes their compiler to
  17. * violate ANSI C. To disable Edit and Continue debugging:
  18. *
  19. * - right-click ssh.c in the FileView
  20. * - click Settings
  21. * - select the C/C++ tab and the General category
  22. * - under `Debug info:', select anything _other_ than `Program
  23. * Database for Edit and Continue'.
  24. */
  25. #define crBegin(v) { int *crLine = &v; switch(v) { case 0:;
  26. #define crBeginState crBegin(s->crLine)
  27. #define crStateP(t, v) \
  28. struct t *s; \
  29. if (!(v)) { s = (v) = snew(struct t); s->crLine = 0; } \
  30. s = (v);
  31. #define crState(t) crStateP(t, ssh->t)
  32. #define crFinish(z) } *crLine = 0; return (z); }
  33. #define crFinishV } *crLine = 0; return; }
  34. #define crFinishFree(z) } sfree(s); return (z); }
  35. #define crFinishFreeV } sfree(s); return; }
  36. #define crReturn(z) \
  37. do {\
  38. *crLine =__LINE__; return (z); case __LINE__:;\
  39. } while (0)
  40. #define crReturnV \
  41. do {\
  42. *crLine=__LINE__; return; case __LINE__:;\
  43. } while (0)
  44. #define crStop(z) do{ *crLine = 0; return (z); }while(0)
  45. #define crStopV do{ *crLine = 0; return; }while(0)
  46. /*
  47. * The crMaybeWaitUntil macros could have been more easily written in
  48. * terms of the simple crReturn above, by writing things like
  49. *
  50. * while (!condition) { crReturn(whatever); }
  51. *
  52. * (or do-while in the case of crWaitUntil). But it's better to do it
  53. * directly by writing _once_ to crLine before first testing the
  54. * condition, because this way it's robust against the condition check
  55. * potentially freeing the entire coroutine state structure as a side
  56. * effect (as long as it also evaluates false if it does that),
  57. * because we don't write into crLine between the condition evaluating
  58. * to false and the 'return' statement.
  59. */
  60. #define crMaybeWaitUntil(c) \
  61. do { \
  62. *crLine =__LINE__; \
  63. case __LINE__: if (!(c)) return 0; \
  64. } while (0)
  65. #define crMaybeWaitUntilV(c) \
  66. do { \
  67. *crLine =__LINE__; \
  68. case __LINE__: if (!(c)) return; \
  69. } while (0)
  70. #define crWaitUntil(c) \
  71. do { \
  72. *crLine =__LINE__; return; \
  73. case __LINE__: if (!(c)) return 0; \
  74. } while (0)
  75. #define crWaitUntilV(c) \
  76. do { \
  77. *crLine =__LINE__; return; \
  78. case __LINE__: if (!(c)) return; \
  79. } while (0)
  80. #endif /* PUTTY_SSHCR_H */