mailmap: add mail alias
[transsip.git] / src / xutils.h
blob61c6b171ae7fd6d42e44941f09ddd04a28d1657f
1 /*
2 * transsip - the telephony toolkit
3 * By Daniel Borkmann <daniel@transsip.org>
4 * Copyright 2011, 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>
5 * Subject to the GPL, version 2.
6 */
8 #ifndef XUTILS_H
9 #define XUTILS_H
11 #include <termios.h>
12 #include <string.h>
13 #include <signal.h>
14 #include <stdint.h>
15 #include <sys/ioctl.h>
16 #include <sys/types.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <netdb.h>
21 #include "built_in.h"
23 #define FILE_SETTINGS ".transsip/settings"
24 #define FILE_CONTACTS ".transsip/contacts"
25 #define FILE_PRIVKEY ".transsip/priv.key"
26 #define FILE_PUBKEY ".transsip/pub.key"
27 #define FILE_USERNAME ".transsip/username"
29 #define FILE_ETCDIR "/etc/transsip"
31 #define FILE_BUSY "busytone.s16"
32 #define FILE_RING "ring.s16"
33 #define FILE_DIAL "dialtone.s16"
35 #define USERSIZ 64
36 #define ADDRSIZ 256
37 #define PORTSIZ 16
39 struct pipepair {
40 int i, o;
43 struct cli_pkt {
44 __extension__ uint16_t ring:1,
45 take:1,
46 fin:1,
47 hold:1,
48 unhold:1,
49 res:11;
50 char user[USERSIZ];
51 char address[ADDRSIZ];
52 char port[PORTSIZ];
53 } __attribute__((packed));
55 extern int open_or_die(const char *file, int flags);
56 extern int open_or_die_m(const char *file, int flags, mode_t mode);
57 extern ssize_t read_or_die(int fd, void *buf, size_t count);
58 extern ssize_t write_or_die(int fd, const void *buf, size_t count);
59 extern size_t strlcpy(char *dest, const char *src, size_t size);
60 extern int slprintf(char *dst, size_t size, const char *fmt, ...);
61 extern char **strntoargv(char *str, size_t len, int *argc);
63 #define __reset "0"
64 #define __bold "1"
65 #define __black "30"
66 #define __red "31"
67 #define __green "32"
68 #define __yellow "33"
69 #define __blue "34"
70 #define __magenta "35"
71 #define __cyan "36"
72 #define __white "37"
73 #define __on_black "40"
74 #define __on_red "41"
75 #define __on_green "42"
76 #define __on_yellow "43"
77 #define __on_blue "44"
78 #define __on_magenta "45"
79 #define __on_cyan "46"
80 #define __on_white "47"
82 #define colorize_start(fore) "\033[" __##fore "m"
83 #define colorize_start_full(fore, back) "\033[" __##fore ";" __on_##back "m"
84 #define colorize_end() "\033[" __reset "m"
86 #define colorize_str(fore, text) \
87 colorize_start(fore) text colorize_end()
88 #define colorize_full_str(fore, back, text) \
89 colorize_start_full(fore, back) text colorize_end()
91 static inline void register_signal(int signal, void (*handler)(int))
93 sigset_t block_mask;
94 struct sigaction saction;
95 sigfillset(&block_mask);
96 saction.sa_handler = handler;
97 saction.sa_mask = block_mask;
98 saction.sa_flags = SA_RESTART;
99 sigaction(signal, &saction, NULL);
102 static inline int set_timeout(struct timeval *timeval, unsigned int msec)
104 if (msec == 0)
105 return -EINVAL;
107 timeval->tv_sec = 0;
108 timeval->tv_usec = 0;
110 if (msec < 1000) {
111 timeval->tv_usec = msec * 1000;
112 return 0;
115 timeval->tv_sec = (long) (msec / 1000);
116 timeval->tv_usec = (long) ((msec - (timeval->tv_sec * 1000)) * 1000);
118 return 0;
121 #endif /* XUTILS_H */