sshcr.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * - click Settings
  20. * - select the C/C++ tab and the General category
  21. * - under `Debug info:', select anything _other_ than `Program
  22. * Database for Edit and Continue'.
  23. */
  24. #define crBegin(v) do { int *crLine = &v; switch(v) { case 0:
  25. #define crBeginState crBegin(s->crLine)
  26. #define crStateP(t, v) \
  27. struct t *s; \
  28. if (!(v)) { s = (v) = snew(struct t); s->crLine = 0; } \
  29. s = (v);
  30. #define crState(t) crStateP(t, ssh->t)
  31. #define crFinish(z) } *crLine = 0; return (z); } while (0)
  32. #define crFinishV } *crLine = 0; return; } while (0)
  33. #define crFinishFreed(z) } return (z); } while (0)
  34. #define crFinishFreedV } return; } while (0)
  35. #define crFinishFree(z) } sfree(s); return (z); } while (0)
  36. #define crFinishFreeV } sfree(s); return; } while (0)
  37. #define crReturn(z) \
  38. do {\
  39. *crLine = __LINE__; return (z); case __LINE__:;\
  40. } while (0)
  41. #define crReturnV \
  42. do {\
  43. *crLine=__LINE__; return; case __LINE__:;\
  44. } while (0)
  45. #define crStop(z) do{ *crLine = 0; return (z); }while(0)
  46. #define crStopV do{ *crLine = 0; return; }while(0)
  47. /*
  48. * The crMaybeWaitUntil macros could have been more easily written in
  49. * terms of the simple crReturn above, by writing things like
  50. *
  51. * while (!condition) { crReturn(whatever); }
  52. *
  53. * (or do-while in the case of crWaitUntil). But it's better to do it
  54. * directly by writing _once_ to crLine before first testing the
  55. * condition, because this way it's robust against the condition check
  56. * potentially freeing the entire coroutine state structure as a side
  57. * effect (as long as it also evaluates false if it does that),
  58. * because we don't write into crLine between the condition evaluating
  59. * to false and the 'return' statement.
  60. */
  61. #define crMaybeWaitUntil(c) \
  62. do { \
  63. *crLine = __LINE__; \
  64. case __LINE__: if (!(c)) return 0; \
  65. } while (0)
  66. #define crMaybeWaitUntilV(c) \
  67. do { \
  68. *crLine = __LINE__; \
  69. case __LINE__: if (!(c)) return; \
  70. } while (0)
  71. #define crWaitUntil(c) \
  72. do { \
  73. *crLine = __LINE__; return; \
  74. case __LINE__: if (!(c)) return 0; \
  75. } while (0)
  76. #define crWaitUntilV(c) \
  77. do { \
  78. *crLine = __LINE__; return; \
  79. case __LINE__: if (!(c)) return; \
  80. } while (0)
  81. #endif /* PUTTY_SSHCR_H */