putty.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #ifndef PUTTY_PUTTY_H
  2. #define PUTTY_PUTTY_H
  3. #define PUTTY_REG_POS "Software\\SimonTatham\\PuTTY"
  4. #ifdef macintosh
  5. #define OPTIMISE_SCROLL
  6. #endif
  7. #ifdef macintosh
  8. #include <MacTypes.h>
  9. #include <Palettes.h>
  10. #include <Controls.h>
  11. #include <Windows.h>
  12. typedef UInt32 DWORD;
  13. #endif /* macintosh */
  14. #ifndef TRUE
  15. #define TRUE 1
  16. #endif
  17. #ifndef FALSE
  18. #define FALSE 0
  19. #endif
  20. #define ATTR_ACTCURS 0x80000000UL /* active cursor (block) */
  21. #define ATTR_PASCURS 0x40000000UL /* passive cursor (box) */
  22. #define ATTR_INVALID 0x20000000UL
  23. #define ATTR_WRAPPED 0x10000000UL
  24. #define ATTR_ASCII 0x00000000UL /* normal ASCII charset ESC ( B */
  25. #define ATTR_GBCHR 0x00100000UL /* UK variant charset ESC ( A */
  26. #define ATTR_LINEDRW 0x00200000UL /* line drawing charset ESC ( 0 */
  27. #define ATTR_BOLD 0x00000100UL
  28. #define ATTR_UNDER 0x00000200UL
  29. #define ATTR_REVERSE 0x00000400UL
  30. #define ATTR_FGMASK 0x0000F000UL
  31. #define ATTR_BGMASK 0x000F0000UL
  32. #define ATTR_FGSHIFT 12
  33. #define ATTR_BGSHIFT 16
  34. #define ATTR_DEFAULT 0x00098000UL
  35. #define ATTR_DEFFG 0x00008000UL
  36. #define ATTR_DEFBG 0x00090000UL
  37. #define ATTR_CUR_XOR 0x000BA000UL
  38. #define ERASE_CHAR (ATTR_DEFAULT | ' ')
  39. #define ATTR_MASK 0xFFFFFF00UL
  40. #define CHAR_MASK 0x000000FFUL
  41. #ifdef macintosh
  42. #define SEL_NL { 13 }
  43. #else
  44. #define SEL_NL { 13, 10 }
  45. #endif
  46. /*
  47. * Global variables. Most modules declare these `extern', but
  48. * window.c will do `#define PUTTY_DO_GLOBALS' before including this
  49. * module, and so will get them properly defined.
  50. */
  51. #ifdef PUTTY_DO_GLOBALS
  52. #define GLOBAL
  53. #else
  54. #define GLOBAL extern
  55. #endif
  56. #define INBUF_SIZE 2048
  57. #define INBUF_MASK (INBUF_SIZE-1)
  58. #define OUTBUF_SIZE 2048
  59. #define OUTBUF_MASK (OUTBUF_SIZE-1)
  60. #define WM_NETEVENT (WM_USER + 1)
  61. typedef enum {
  62. TS_AYT, TS_BRK, TS_SYNCH, TS_EC, TS_EL, TS_GA, TS_NOP, TS_ABORT,
  63. TS_AO, TS_IP, TS_SUSP, TS_EOR, TS_EOF
  64. } Telnet_Special;
  65. typedef enum {
  66. MB_NOTHING, MB_SELECT, MB_EXTEND, MB_PASTE
  67. } Mouse_Button;
  68. typedef enum {
  69. MA_NOTHING, MA_CLICK, MA_2CLK, MA_3CLK, MA_DRAG, MA_RELEASE
  70. } Mouse_Action;
  71. typedef enum {
  72. VT_XWINDOWS, VT_OEMANSI, VT_OEMONLY, VT_POORMAN
  73. } VT_Mode;
  74. typedef struct Session Session;
  75. typedef struct Socket Socket;
  76. /* Types of network event */
  77. typedef enum {
  78. NE_NULL, /* Nothing happened */
  79. NE_OPEN, /* Connection successfully opened */
  80. NE_NOHOST, /* DNS lookup failed for some reason */
  81. NE_REFUSED, /* Port unreachable */
  82. NE_NOOPEN, /* Connection failed to open for some other reason */
  83. NE_DATA, /* Incoming normal data */
  84. NE_URGENT, /* Incoming urgent data */
  85. NE_SENT, /* Used internally by Mac network stack */
  86. NE_CLOSING, /* Connection closed by remote host */
  87. NE_CLOSED, /* Connection close completed */
  88. NE_TIMEOUT, /* Remote host vanished */
  89. NE_ABORT, /* Remote host reset connection */
  90. NE_DIED, /* Connection has failed for some other reason */
  91. } Net_Event_Type;
  92. #ifdef macintosh
  93. typedef void *SOCKET;
  94. #define INVALID_SOCKET NULL
  95. #endif
  96. typedef struct {
  97. char *(*init) (Session *);
  98. int (*msg)(Session *, SOCKET, Net_Event_Type);
  99. void (*send) (Session *, char *buf, int len);
  100. void (*size) (Session *);
  101. void (*special) (Session *, Telnet_Special code);
  102. void (*shutdown) (Session *);
  103. } Backend;
  104. #ifdef macintosh
  105. typedef struct {
  106. int (*init)(void);
  107. SOCKET (*open)(Session *, char const *, int);
  108. int (*recv)(SOCKET, void *, int, int);
  109. int (*send)(SOCKET, void *, int, int);
  110. void (*poll)(void);
  111. void (*close)(SOCKET);
  112. void (*destroy)(SOCKET);
  113. void (*shutdown)(void);
  114. } Network_Stack;
  115. GLOBAL Network_Stack *net_stack;
  116. #define net_open(s, h, p) ((*net_stack->open)((s), (h), (p)))
  117. #define net_recv(s, b, l, f) ((*net_stack->recv)((s), (b), (l), (f)))
  118. #define net_send(s, b, l, f) ((*net_stack->send)((s), (b), (l), (f)))
  119. #define net_poll() ((*net_stack->poll)())
  120. #define net_close(s) ((*net_stack->close)(s))
  121. #define net_destroy(s) ((*net_stack->destroy)(s))
  122. #define net_shutdown() ((*net_stack->shutdown)())
  123. #endif
  124. typedef struct {
  125. /* Basic options */
  126. char host[512];
  127. int port;
  128. enum { PROT_TELNET, PROT_SSH } protocol;
  129. int close_on_exit;
  130. /* SSH options */
  131. int nopty;
  132. enum { CIPHER_3DES, CIPHER_BLOWFISH } cipher;
  133. /* Telnet options */
  134. char termtype[32];
  135. char termspeed[32];
  136. char environmt[1024]; /* VAR\tvalue\0VAR\tvalue\0\0 */
  137. char username[32];
  138. int rfc_environ;
  139. /* Keyboard options */
  140. int bksp_is_delete;
  141. int rxvt_homeend;
  142. int linux_funkeys;
  143. int app_cursor;
  144. int app_keypad;
  145. int meta_modifiers;
  146. /* Terminal options */
  147. int savelines;
  148. int dec_om;
  149. int wrap_mode;
  150. int lfhascr;
  151. int win_name_always;
  152. int width, height;
  153. char font[64];
  154. int fontisbold;
  155. int fontheight;
  156. VT_Mode vtmode;
  157. /* Colour options */
  158. int try_palette;
  159. int bold_colour;
  160. #ifdef macintosh
  161. PaletteHandle colours;
  162. #else /* not macintosh */
  163. unsigned char colours[22][3];
  164. #endif /* not macintosh */
  165. /* Selection options */
  166. int implicit_copy;
  167. #ifdef macintosh
  168. int mouse_is_xterm;
  169. #endif
  170. short wordness[256];
  171. } Config;
  172. typedef struct {
  173. /* Display buffers and pointers within them */
  174. unsigned long *text; /* buffer of text on terminal screen */
  175. unsigned long *scrtop; /* top of working screen */
  176. unsigned long *disptop; /* top of displayed screen */
  177. unsigned long *sbtop; /* top of scrollback */
  178. unsigned long *cpos; /* cursor position (convenience) */
  179. unsigned long *disptext; /* buffer of text on real screen */
  180. unsigned long *wanttext; /* buffer of text we want on screen */
  181. unsigned long *alttext; /* buffer of text on alt. screen */
  182. unsigned char *selspace; /* buffer for building selections in */
  183. /* Current state */
  184. unsigned long curr_attr;
  185. int curs_x, curs_y; /* cursor */
  186. int cset; /* 0 or 1: which char set is in GL */
  187. unsigned long cset_attr[2]; /* G0 and G1 char sets */
  188. /* Saved state */
  189. unsigned long save_attr;
  190. int save_x, save_y; /* saved cursor position */
  191. int save_cset, save_csattr; /* saved with cursor position */
  192. int marg_t, marg_b; /* scroll margins */
  193. /* Flags */
  194. int dec_om; /* DEC origin mode flag */
  195. int wrap, wrapnext; /* wrap flags */
  196. int insert; /* insert-mode flag */
  197. int rvideo; /* global reverse video flag */
  198. /*
  199. * Saved settings on the alternate screen.
  200. */
  201. int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
  202. int alt_t, alt_b;
  203. int alt_which;
  204. /* Escape sequence handler state */
  205. #define ARGS_MAX 32 /* max # of esc sequence arguments */
  206. int esc_args[ARGS_MAX];
  207. int esc_nargs;
  208. int esc_query;
  209. #define OSC_STR_MAX 2048
  210. int osc_strlen;
  211. char osc_string[OSC_STR_MAX+1];
  212. int osc_w;
  213. unsigned char *tabs;
  214. int nl_count;
  215. enum {
  216. TOPLEVEL, IGNORE_NEXT,
  217. SEEN_ESC, SEEN_CSI, SET_GL, SET_GR,
  218. SEEN_OSC, SEEN_OSC_P, SEEN_OSC_W, OSC_STRING, OSC_MAYBE_ST,
  219. SEEN_ESCHASH
  220. } termstate;
  221. enum {
  222. NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
  223. } selstate;
  224. enum {
  225. SM_CHAR, SM_WORD, SM_LINE
  226. } selmode;
  227. unsigned long *selstart, *selend, *selanchor;
  228. short wordness[256];
  229. } Term_State;
  230. typedef struct Session {
  231. /* Config that created this session */
  232. Config cfg;
  233. /* Terminal emulator internal state */
  234. Term_State ts;
  235. /* Display state */
  236. int rows, cols, savelines;
  237. int font_width, font_height;
  238. int has_focus;
  239. /* Buffers */
  240. unsigned char inbuf[INBUF_SIZE];
  241. int inbuf_head, inbuf_reap;
  242. unsigned char outbuf[OUTBUF_SIZE];
  243. int outbuf_head, outbuf_reap;
  244. /* Emulator state */
  245. int app_cursor_keys, app_keypad_keys;
  246. /* Backend */
  247. Backend *back;
  248. /* Conveniences */
  249. unsigned long attr_mask; /* Mask of attributes to display */
  250. #ifdef macintosh
  251. short fontnum;
  252. int font_ascent;
  253. int font_leading;
  254. int font_boldadjust;
  255. WindowPtr window;
  256. PaletteHandle palette;
  257. ControlHandle scrollbar;
  258. WCTabHandle wctab;
  259. #endif
  260. } Session;
  261. typedef struct Socket Socket;
  262. /*
  263. * Exports from display system
  264. */
  265. extern void request_resize(Session *, int, int);
  266. extern void do_text(Session *, int, int, char *, int, unsigned long);
  267. extern void set_title(Session *, char *);
  268. extern void set_icon(Session *, char *);
  269. extern void set_sbar(Session *, int, int, int);
  270. extern void pre_paint(Session *);
  271. extern void post_paint(Session *);
  272. extern void palette_set(Session *, int, int, int, int);
  273. extern void palette_reset(Session *);
  274. extern void write_clip (void *, int);
  275. extern void get_clip (void **, int *);
  276. extern void do_scroll(Session *, int, int, int);
  277. extern void fatalbox(const char *, ...);
  278. #ifdef macintosh
  279. #pragma noreturn (fatalbox)
  280. #endif
  281. extern void beep(Session *s);
  282. extern void lognegot(const char *);
  283. /*
  284. * Exports from the network system
  285. */
  286. #ifndef macintosh
  287. extern Socket *net_open(Session *, char *host, int port);
  288. extern char *net_realname(Socket *);
  289. extern int net_recv(Socket *, void *, int, int);
  290. extern int net_send(Socket *, void *, int, int);
  291. extern void net_close(Socket *); /* ask the remote end to close */
  292. extern void net_destroy(Socket *); /* Tidy up */
  293. #endif
  294. #define SEND_PUSH 0x01
  295. #define SEND_URG 0x02
  296. /*
  297. * Exports from noise.c.
  298. */
  299. void noise_get_heavy(void (*func) (void *, int));
  300. void noise_get_light(void (*func) (void *, int));
  301. void noise_ultralight(DWORD data);
  302. void random_save_seed(void);
  303. #ifndef macintosh
  304. /*
  305. * Exports from windlg.c.
  306. */
  307. int do_config (void);
  308. int do_reconfig (HWND);
  309. void do_defaults (char *);
  310. void lognegot (char *);
  311. void shownegot (HWND);
  312. void showabout (HWND);
  313. void verify_ssh_host_key(char *host, struct RSAKey *key);
  314. #endif
  315. /*
  316. * Exports from terminal.c.
  317. */
  318. extern void term_init(Session *);
  319. extern void term_size(Session *, int, int, int);
  320. extern void term_out(Session *);
  321. extern void term_paint(Session *, int, int, int, int);
  322. extern void term_scroll(Session *, int, int);
  323. extern void term_pwron(Session *);
  324. extern void term_clrsb(Session *);
  325. extern void term_mouse(Session *, Mouse_Button, Mouse_Action, int, int);
  326. extern void term_copy(Session *);
  327. extern void term_paste(Session *);
  328. extern int term_hasselection(Session *);
  329. extern void term_deselect (Session *);
  330. extern void term_update (Session *);
  331. extern void term_invalidate(Session *);
  332. /*
  333. * Exports from telnet.c.
  334. */
  335. extern Backend telnet_backend;
  336. /*
  337. * Exports from ssh.c.
  338. */
  339. extern Backend ssh_backend;
  340. /*
  341. * Exports from sshrand.c.
  342. */
  343. void random_add_noise(void *noise, int length);
  344. void random_init(void);
  345. int random_byte(void);
  346. void random_get_savedata(void **data, int *len);
  347. /*
  348. * Exports from misc.c.
  349. */
  350. /* #define MALLOC_LOG do this if you suspect putty of leaking memory */
  351. #ifdef MALLOC_LOG
  352. #define smalloc(z) (mlog(__FILE__,__LINE__), safemalloc(z))
  353. #define srealloc(y,z) (mlog(__FILE__,__LINE__), saferealloc(y,z))
  354. #define sfree(z) (mlog(__FILE__,__LINE__), safefree(z))
  355. void mlog(char *, int);
  356. #else
  357. #define smalloc safemalloc
  358. #define srealloc saferealloc
  359. #define sfree safefree
  360. #endif
  361. void *safemalloc(size_t);
  362. void *saferealloc(void *, size_t);
  363. void safefree(void *);
  364. /*
  365. * Exports from testback.c
  366. */
  367. extern Backend null_backend;
  368. extern Backend loop_backend;
  369. extern Backend hexdump_backend;
  370. /*
  371. * Exports from version.c.
  372. */
  373. extern char ver[];
  374. /*
  375. * A debug system.
  376. */
  377. #ifdef DEBUG
  378. #include <stdarg.h>
  379. #define debug(x) (dprintf x)
  380. static void dprintf(char *fmt, ...) {
  381. char buf[2048];
  382. DWORD dw;
  383. va_list ap;
  384. static int gotconsole = 0;
  385. if (!gotconsole) {
  386. AllocConsole();
  387. gotconsole = 1;
  388. }
  389. va_start(ap, fmt);
  390. vsprintf(buf, fmt, ap);
  391. WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &dw, NULL);
  392. va_end(ap);
  393. }
  394. #else
  395. #define debug(x)
  396. #endif
  397. #endif