Pull out parsing of ^C style strings from the terminal answerback code to
[u/mdw/putty] / misc.h
1 #ifndef PUTTY_MISC_H
2 #define PUTTY_MISC_H
3
4 #include "puttymem.h"
5
6 #include <stdio.h> /* for FILE * */
7 #include <stdarg.h> /* for va_list */
8 #include <time.h> /* for struct tm */
9
10 #ifndef FALSE
11 #define FALSE 0
12 #endif
13 #ifndef TRUE
14 #define TRUE 1
15 #endif
16
17 typedef struct Filename Filename;
18 typedef struct FontSpec FontSpec;
19
20 unsigned long parse_blocksize(const char *bs);
21 char ctrlparse(char *s, char **next);
22
23 char *dupstr(const char *s);
24 char *dupcat(const char *s1, ...);
25 char *dupprintf(const char *fmt, ...);
26 char *dupvprintf(const char *fmt, va_list ap);
27
28 char *fgetline(FILE *fp);
29
30 void base64_encode_atom(unsigned char *data, int n, char *out);
31
32 struct bufchain_granule;
33 typedef struct bufchain_tag {
34 struct bufchain_granule *head, *tail;
35 int buffersize; /* current amount of buffered data */
36 } bufchain;
37
38 void bufchain_init(bufchain *ch);
39 void bufchain_clear(bufchain *ch);
40 int bufchain_size(bufchain *ch);
41 void bufchain_add(bufchain *ch, const void *data, int len);
42 void bufchain_prefix(bufchain *ch, void **data, int *len);
43 void bufchain_consume(bufchain *ch, int len);
44 void bufchain_fetch(bufchain *ch, void *data, int len);
45
46 struct tm ltime(void);
47
48 /*
49 * Debugging functions.
50 *
51 * Output goes to debug.log
52 *
53 * debug(()) (note the double brackets) is like printf().
54 *
55 * dmemdump() and dmemdumpl() both do memory dumps. The difference
56 * is that dmemdumpl() is more suited for when the memory address is
57 * important (say because you'll be recording pointer values later
58 * on). dmemdump() is more concise.
59 */
60
61 #ifdef DEBUG
62 void debug_printf(char *fmt, ...);
63 void debug_memdump(void *buf, int len, int L);
64 #define debug(x) (debug_printf x)
65 #define dmemdump(buf,len) debug_memdump (buf, len, 0);
66 #define dmemdumpl(buf,len) debug_memdump (buf, len, 1);
67 #else
68 #define debug(x)
69 #define dmemdump(buf,len)
70 #define dmemdumpl(buf,len)
71 #endif
72
73 #ifndef lenof
74 #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
75 #endif
76
77 #ifndef min
78 #define min(x,y) ( (x) < (y) ? (x) : (y) )
79 #endif
80 #ifndef max
81 #define max(x,y) ( (x) > (y) ? (x) : (y) )
82 #endif
83
84 #define GET_32BIT_LSB_FIRST(cp) \
85 (((unsigned long)(unsigned char)(cp)[0]) | \
86 ((unsigned long)(unsigned char)(cp)[1] << 8) | \
87 ((unsigned long)(unsigned char)(cp)[2] << 16) | \
88 ((unsigned long)(unsigned char)(cp)[3] << 24))
89
90 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
91 (cp)[0] = (unsigned char)(value), \
92 (cp)[1] = (unsigned char)((value) >> 8), \
93 (cp)[2] = (unsigned char)((value) >> 16), \
94 (cp)[3] = (unsigned char)((value) >> 24) )
95
96 #define GET_16BIT_LSB_FIRST(cp) \
97 (((unsigned long)(unsigned char)(cp)[0]) | \
98 ((unsigned long)(unsigned char)(cp)[1] << 8))
99
100 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
101 (cp)[0] = (unsigned char)(value), \
102 (cp)[1] = (unsigned char)((value) >> 8) )
103
104 #define GET_32BIT_MSB_FIRST(cp) \
105 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
106 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
107 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
108 ((unsigned long)(unsigned char)(cp)[3]))
109
110 #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
111
112 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
113 (cp)[0] = (unsigned char)((value) >> 24), \
114 (cp)[1] = (unsigned char)((value) >> 16), \
115 (cp)[2] = (unsigned char)((value) >> 8), \
116 (cp)[3] = (unsigned char)(value) )
117
118 #define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value)
119
120 #define GET_16BIT_MSB_FIRST(cp) \
121 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
122 ((unsigned long)(unsigned char)(cp)[1]))
123
124 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
125 (cp)[0] = (unsigned char)((value) >> 8), \
126 (cp)[1] = (unsigned char)(value) )
127
128 #endif