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