7b0ca09c6c0b5f84c7ddab93eb92912e83245a44
[paraslash.git] / para.h
1 /*
2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file para.h global paraslash definitions */
8
9 #include "config.h"
10
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <sys/wait.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <time.h> /* time(), localtime() */
17 #include <unistd.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdarg.h>
21 #include <ctype.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <sys/socket.h>
25 #include <sys/un.h> /* needed by create_pf_socket */
26 #include <string.h>
27 #include "gcc-compat.h"
28
29 /** used in various contexts */
30 #define MAXLINE 255
31
32 /** compute the minimum of \a a and \a b */
33 #define PARA_MIN(a,b) ((a) < (b) ? (a) : (b))
34 /** compute the maximum of \a a and \a b */
35 #define PARA_MAX(a,b) ((a) > (b) ? (a) : (b))
36 /** compute the absolute value of \a a */
37 #define PARA_ABS(a) ((a) > 0 ? (a) : -(a))
38
39 /** debug loglevel, gets really noisy */
40 #define DEBUG 1
41 /** still noisy, but won't fill your disk */
42 #define INFO 2
43 /** normal, but significant event */
44 #define NOTICE 3
45 /** unexpected event that can be handled */
46 #define WARNING 4
47 /** unhandled error condition */
48 #define ERROR 5
49 /** system might be unreliable */
50 #define CRIT 6
51 /** last message before exit */
52 #define EMERG 7
53
54 /** log messages with lower proirity than that will not be compiled in*/
55 #define COMPILE_TIME_LOGLEVEL 0
56
57 #if DEBUG > COMPILE_TIME_LOGLEVEL
58 #define PARA_DEBUG_LOG(f,...) para_log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
59 #else
60 #define PARA_DEBUG_LOG(...) do {;} while (0)
61 #endif
62
63 #if INFO > COMPILE_TIME_LOGLEVEL
64 #define PARA_INFO_LOG(f,...) para_log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
65 #else
66 #define PARA_INFO_LOG(...) do {;} while (0)
67 #endif
68
69 #if NOTICE > COMPILE_TIME_LOGLEVEL
70 #define PARA_NOTICE_LOG(f,...) para_log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
71 #else
72 #define PARA_NOTICE_LOG(...) do {;} while (0)
73 #endif
74
75 #if WARNING > COMPILE_TIME_LOGLEVEL
76 #define PARA_WARNING_LOG(f,...) para_log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
77 #else
78 #define PARA_WARNING_LOG(...) do {;} while (0)
79 #endif
80
81 #if ERROR > COMPILE_TIME_LOGLEVEL
82 #define PARA_ERROR_LOG(f,...) para_log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
83 #else
84 #define PARA_ERROR_LOG(...) do {;} while (0)
85 #endif
86
87 #if CRIT > COMPILE_TIME_LOGLEVEL
88 #define PARA_CRIT_LOG(f,...) para_log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
89 #else
90 #define PARA_CRIT_LOG(...) do {;} while (0)
91 #endif
92
93 #if EMERG > COMPILE_TIME_LOGLEVEL
94 #define PARA_EMERG_LOG(f,...) para_log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
95 #else
96 #define PARA_EMERG_LOG(...)
97 #endif
98
99 /**
100 * define a standard log function that always writes to stderr
101 *
102 * \param loglevel_barrier If the loglevel of the current message
103 * is less than that, the message is going to be ignored.
104 *
105 */
106 #define INIT_STDERR_LOGGING(loglevel_barrier) \
107 __printf_2_3 void para_log(int ll, const char* fmt,...) \
108 { \
109 va_list argp; \
110 if (ll < loglevel_barrier) \
111 return; \
112 va_start(argp, fmt); \
113 vfprintf(stderr, fmt, argp); \
114 va_end(argp); \
115 }
116
117 /** version text used by various commands if -V switch was given */
118 #define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION "\n" \
119 "Copyright (C) 2007 Andre Noll\n" \
120 "This is free software with ABSOLUTELY NO WARRANTY." \
121 " See COPYING for details.\n" \
122 "Written by Andre Noll.\n" \
123 "Report bugs to <maan@systemlinux.org>.\n"
124
125 /** print out \p VERSION_TEXT and exit if version flag was given */
126 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
127 if (_args_info_struct.version_given) { \
128 printf("%s", VERSION_TEXT(_prefix)); \
129 exit(EXIT_SUCCESS); \
130 }
131 /** sent by para_server for commands that expect a data file */
132 #define AWAITING_DATA_MSG "\nAwaiting Data."
133 /** sent by para_server if authentication was successful */
134 #define PROCEED_MSG "\nProceed.\n"
135 /** length of the \p PROCEED_MSG string */
136 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
137 /** sent by para_client to indicate the end of the command line */
138 #define EOC_MSG "\nEnd of Command."
139 /** sent by para_client, followed by the decrypted challenge number */
140 #define CHALLENGE_RESPONSE_MSG "challenge_response:"
141
142 /* gui_common */
143 int para_open_audiod_pipe(char *);
144 int read_audiod_pipe(int, void (*)(char *));
145
146 /* exec */
147 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
148
149 /* time */
150 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
151 long unsigned tv2ms(const struct timeval*);
152 void d2tv(double, struct timeval*);
153 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
154 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
155 void tv_divide(const unsigned long divisor, const struct timeval *tv,
156 struct timeval *result);
157 int tv_convex_combination(const long a, const struct timeval *tv1,
158 const long b, const struct timeval *tv2,
159 struct timeval *result);
160 void ms2tv(const long unsigned n, struct timeval *tv);
161
162 /* stat */
163 enum {
164 SI_STATUS_BAR, SI_STATUS, SI_NUM_PLAYED,
165 SI_MTIME, SI_LENGTH_MIN, SI_LENGTH_SEC,
166 SI_FILE_SIZE, SI_STATUS_FLAGS, SI_FORMAT,
167 SI_SCORE, SI_AUDIO_INFO1, SI_AUDIO_INFO2,
168 SI_AUDIO_INFO3, SI_DBINFO1, SI_DBINFO2,
169 SI_DBINFO3, SI_DECODER_FLAGS, SI_AUDIOD_STATUS,
170 SI_PLAY_TIME, SI_UPTIME, SI_OFFSET,
171 SI_LENGTH, SI_STREAM_START, SI_CURRENT_TIME,
172 SI_AUDIOD_UPTIME, SI_SELECTOR, NUM_STAT_ITEMS
173 };
174
175 int stat_item_valid(const char *item);
176 int stat_line_valid(const char *);
177 void stat_client_write(const char *msg, int itemnum);
178 int stat_client_add(int fd, long unsigned mask);
179 size_t for_each_line(char *buf, size_t n, void (*line_handler)(char *));
180 #define FOR_EACH_STAT_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
181
182 __printf_2_3 void para_log(int, const char*, ...);
183
184 /* taken from printf man page */
185 #define PARA_VSPRINTF(fmt, p) \
186 { \
187 int n; \
188 size_t size = 100; \
189 p = para_malloc(size); \
190 while (1) { \
191 va_list ap; \
192 /* Try to print in the allocated space. */ \
193 va_start(ap, fmt); \
194 n = vsnprintf(p, size, fmt, ap); \
195 va_end(ap); \
196 /* If that worked, return the string. */ \
197 if (n > -1 && n < size) \
198 break; \
199 /* Else try again with more space. */ \
200 if (n > -1) /* glibc 2.1 */ \
201 size = n + 1; /* precisely what is needed */ \
202 else /* glibc 2.0 */ \
203 size *= 2; /* twice the old size */ \
204 p = para_realloc(p, size); \
205 } \
206 }