1 /*************************************************************************
2  *  TinyFugue - programmable mud client
3  *  Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2002, 2003, 2004, 2005, 2006-2007 Ken Keys
4  *
5  *  TinyFugue (aka "tf") is protected under the terms of the GNU
6  *  General Public License.  See the file "COPYING" for details.
7  ************************************************************************/
8 
9 #ifndef UTIL_H
10 #define UTIL_H
11 
12 struct feature {
13     const char *name;
14     const int *flag;
15 };
16 
17 #undef CTRL
18 /* convert to or from ctrl character */
19 #define CTRL(c)  (ucase(c) ^ '@')
20 
21 #if WIDECHAR
22 /* Make these no-op for now. */
23 #define mapchar(c)    (c)
24 #define unmapchar(c)  (c)
25 #define localize(c)   (c)
26 #else
27 /* map char to or from "safe" character set */
28 #define mapchar(c)    ((c) ? (c) & 0xFF : 0x80)
29 #define unmapchar(c)  ((char)(((c) == (char)0x80) ? 0x0 : (c)))
30 
31 /* Map character into set allowed by locale */
32 #define localize(c)  ((is_print(c) || is_cntrl(c)) ? (c) : (c) & 0x7F)
33 #endif
34 
35 /* Note STRNDUP works only if src[len] == '\0', ie. len == strlen(src) */
36 #define STRNDUP(src, len) \
37     (strcpy(xmalloc(NULL, (len) + 1, __FILE__, __LINE__), (src)))
38 #define STRDUP(src)  STRNDUP((src), strlen(src))
39 
40 
41 #define IS_QUOTE	0x01
42 #define IS_STATMETA	0x02
43 #define IS_STATEND	0x04
44 #define IS_KEYSTART	0x08
45 #define IS_UNARY	0x10
46 #define IS_MULT		0x20
47 #define IS_ADDITIVE	0x40
48 #define IS_ASSIGNPFX	0x80
49 
50 extern const struct timeval tvzero;
51 extern struct timeval mail_update;
52 extern int mail_count;
53 extern struct mail_info_s *maillist;
54 extern char tf_ctype[];
55 extern Stringp featurestr;
56 extern struct feature features[];
57 
58 extern const int feature_256colors;
59 extern const int feature_core;
60 extern const int feature_float;
61 extern const int feature_ftime;
62 extern const int feature_history;
63 extern const int feature_IPv6;
64 extern const int feature_locale;
65 extern const int feature_MCCPv1;
66 extern const int feature_MCCPv2;
67 extern const int feature_process;
68 extern const int feature_SOCKS;
69 extern const int feature_ssl;
70 extern const int feature_subsecond;
71 extern const int feature_TZ;
72 
73 #define is_quote(c)	(tf_ctype[(unsigned char)c] & IS_QUOTE)
74 #define is_statmeta(c)	(tf_ctype[(unsigned char)c] & IS_STATMETA)
75 #define is_statend(c)	(tf_ctype[(unsigned char)c] & IS_STATEND)
76 #define is_keystart(c)	(tf_ctype[(unsigned char)c] & IS_KEYSTART)
77 #define is_unary(c)	(tf_ctype[(unsigned char)c] & IS_UNARY)
78 #define is_mult(c)	(tf_ctype[(unsigned char)c] & IS_MULT)
79 #define is_additive(c)	(tf_ctype[(unsigned char)c] & IS_ADDITIVE)
80 #define is_assignpfx(c)	(tf_ctype[(unsigned char)c] & IS_ASSIGNPFX)
81 
82 #define tvcmp(a, b) \
83    (((a)->tv_sec != (b)->tv_sec) ? \
84        ((a)->tv_sec - (b)->tv_sec) : \
85        ((a)->tv_usec - (b)->tv_usec))
86 
87 #if HAVE_GETTIMEOFDAY
88 # define gettime(p)	(gettimeofday(p, NULL))
89 #else
90 # define gettime(p)	((p)->tv_usec = 0, time(&(p)->tv_sec))
91 #endif
92 
93 #define strtochr(s, ep)   ((char)(strtol((s), (char**)ep, 0) % 0x100))
94 #define strtoint(s, ep)   ((int)strtol((s), (char**)ep, 10))
95 #define strtolong(s, ep)  (strtol((s), (char**)ep, 10))
96 extern int    enum2int(const char *str, long val, conString *vec, const char *msg);
97 extern void   init_util1(void);
98 extern void   init_util2(void);
99 extern const conString* print_to_ascii(String *buf, const char *str);
100 extern const conString* ascii_to_print(const char *str);
101 extern char  *cstrchr(const char *s, int c);
102 extern char  *estrchr(const char *s, int c, int e);
103 extern int    numarg(const char **str);
104 extern int    nullstrcmp(const char *s, const char *t);
105 extern int    nullcstrcmp(const char *s, const char *t);
106 extern int    cstrncmp(const char *s, const char *t, size_t n);
107 extern char  *stringarg(char **str, const char **end);
108 extern int    stringliteral(struct String *dest, const char **str);
109 extern char  *stripstr(char *s);
110 extern void   startopt(const conString *args, const char *opts);
111 extern char   nextopt(const char **arg, ValueUnion *u, int *type, int *offp);
112 #if HAVE_TZSET
113 extern int    ch_timezone(Var *var);
114 #else
115 # define ch_timezone NULL
116 #endif
117 extern int    ch_locale(Var *var);
118 extern int    ch_mailfile(Var *var);
119 extern int    ch_maildelay(Var *var);
120 extern void   check_mail(void);
121 
122 extern type_t string_arithmetic_type(const char *str, int typeset);
123 extern Value *parsenumber(const char *str, const char **caller_endp,
124 		int typeset, Value *val);
125 extern long   parsetime(const char *str, char **endp, int *istime);
126 extern void   abstime(struct timeval *tv);
127 extern void   append_usec(String *buf, long usec, int truncflag);
128 extern void   tftime(String *buf, const conString *fmt,
129 		const struct timeval *tv);
130 extern void   tvsub(struct timeval *a, const struct timeval *b,
131 		const struct timeval *c);
132 extern void   tvadd(struct timeval *a, const struct timeval *b,
133 		const struct timeval *c);
134 extern void   die(const char *why, int err) NORET;
135 #if USE_DMALLOC
136 extern void   free_util(void);
137 #endif
138 
139 #endif /* UTIL_H */
140