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