fix bug 4773, 'remove obsolescent AC_C_CONST'
[claws.git] / src / common / utils.h
blobab07def2e49fd9d244beebe5416a199c43fb06dc
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 The Claws Mail Team and Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * The code of the g_utf8_substring function below is owned by
19 * Matthias Clasen <matthiasc@src.gnome.org>/<mclasen@redhat.com>
20 * and is got from GLIB 2.30
23 #ifndef __UTILS_H__
24 #define __UTILS_H__
26 #ifdef HAVE_CONFIG_H
27 #include "claws-features.h"
28 #endif
30 #ifdef HAVE_BACKTRACE
31 #include <execinfo.h>
32 #endif
34 #include <glib.h>
35 #include <glib-object.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <time.h>
42 #if HAVE_ALLOCA_H
43 # include <alloca.h>
44 #endif
45 #if HAVE_WCHAR_H
46 # include <wchar.h>
47 #endif
49 /* The Hurd doesn't have these limits */
50 #ifndef PATH_MAX
51 #define PATH_MAX 4196
52 #endif
53 #ifndef HOST_NAME_MAX
54 #define HOST_NAME_MAX 256
55 #endif
57 #ifdef G_OS_WIN32
59 #define fsync _commit
61 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
62 #endif
63 /* Wrappers for C library function that take pathname arguments. */
64 # include <glib/gstdio.h>
66 /* why is this sometimes undefined !? */
67 #ifndef G_MAXOFFSET
68 typedef gint64 goffset;
69 #define G_MINOFFSET G_MININT64
70 #define G_MAXOFFSET G_MAXINT64
71 #endif
73 #ifndef BIG_ENDIAN_HOST
74 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
75 #define BIG_ENDIAN_HOST 1
76 #endif
77 #endif
79 #define CHDIR_RETURN_IF_FAIL(dir) \
80 { \
81 if (change_dir(dir) < 0) return; \
84 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
85 { \
86 if (change_dir(dir) < 0) return val; \
89 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
90 { \
91 if (change_dir(dir) < 0) { \
92 code \
93 return val; \
94 } \
97 #define MAX_ALLOCA_MEM_SIZE 102400
99 #define Xalloca(ptr, size, iffail) \
101 size_t __size = size; \
103 if (__size > MAX_ALLOCA_MEM_SIZE) { \
104 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
105 iffail; \
107 if ((ptr = alloca(__size)) == NULL) { \
108 g_warning("can't allocate memory"); \
109 iffail; \
113 #define Xstrdup_a(ptr, str, iffail) \
115 gchar *__tmp; \
116 size_t __size = strlen(str); \
118 if (__size > MAX_ALLOCA_MEM_SIZE) { \
119 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
120 iffail; \
122 if ((__tmp = alloca(__size + 1)) == NULL) { \
123 g_warning("can't allocate memory"); \
124 iffail; \
125 } else \
126 strcpy(__tmp, str); \
128 ptr = __tmp; \
131 #define Xstrndup_a(ptr, str, len, iffail) \
133 gchar *__tmp; \
134 size_t __size = len; \
136 if (__size > MAX_ALLOCA_MEM_SIZE) { \
137 g_warning("%" G_GSIZE_FORMAT "bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
138 iffail; \
140 if ((__tmp = alloca(__size + 1)) == NULL) { \
141 g_warning("can't allocate memory"); \
142 iffail; \
143 } else { \
144 memcpy(__tmp, str, __size); \
145 __tmp[__size] = '\0'; \
148 ptr = __tmp; \
151 #define Xstrcat_a(ptr, str1, str2, iffail) \
153 gchar *__tmp; \
154 size_t len1, len2; \
156 len1 = strlen(str1); \
157 len2 = strlen(str2); \
158 if (len1 + len2 > MAX_ALLOCA_MEM_SIZE) { \
159 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", len1 + len2, MAX_ALLOCA_MEM_SIZE); \
160 iffail; \
162 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
163 g_warning("can't allocate memory"); \
164 iffail; \
165 } else { \
166 memcpy(__tmp, str1, len1); \
167 memcpy(__tmp + len1, str2, len2 + 1); \
170 ptr = __tmp; \
173 #define AUTORELEASE_STR(str, iffail) \
175 gchar *__str; \
176 Xstrdup_a(__str, str, iffail); \
177 g_free(str); \
178 str = __str; \
181 #define FILE_OP_ERROR(file, func) \
183 g_printerr("%s: ", file); \
184 fflush(stderr); \
185 perror(func); \
188 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
190 /* from NetworkManager */
191 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
192 #define print_backtrace() \
193 G_STMT_START \
195 void *_call_stack[512]; \
196 int _call_stack_size; \
197 char **_symbols; \
198 _call_stack_size = backtrace (_call_stack, \
199 G_N_ELEMENTS (_call_stack)); \
200 _symbols = backtrace_symbols (_call_stack, _call_stack_size); \
201 if (_symbols != NULL) \
203 int _i; \
204 _i = 0; \
205 g_print ("traceback:\n"); \
206 while (_i < _call_stack_size) \
208 g_print ("%d:\t%s\n", _i, _symbols[_i]); \
209 _i++; \
211 free (_symbols); \
214 G_STMT_END
215 #else
216 #define print_backtrace() \
217 G_STMT_START \
220 G_STMT_END
221 #endif
224 #define cm_return_val_if_fail(expr,val) G_STMT_START { \
225 if (!(expr)) { \
226 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
227 print_backtrace(); \
228 g_print("\n"); \
229 return val; \
231 } G_STMT_END
233 #define cm_return_if_fail(expr) G_STMT_START { \
234 if (!(expr)) { \
235 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
236 print_backtrace(); \
237 g_print("\n"); \
238 return; \
240 } G_STMT_END
242 #ifndef MIN
243 #define MIN(a, b) ((a) < (b) ? (a) : (b))
244 #endif
245 #ifndef MAX
246 #define MAX(a, b) ((a) > (b) ? (a) : (b))
247 #endif
249 #ifdef __cplusplus
250 extern "C" {
251 #endif
253 typedef gpointer (*GNodeMapFunc) (gpointer nodedata, gpointer data);
255 /* debug functions */
256 void debug_set_mode (gboolean mode);
257 gboolean debug_get_mode (void);
259 #ifdef HAVE_VA_OPT
260 #define debug_print(format, ...) debug_print_real(__FILE__, __LINE__, format __VA_OPT__(,) __VA_ARGS__)
261 #else
262 #define debug_print \
263 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
264 debug_print_real
265 #endif
267 /* for macro expansion */
268 #define Str(x) #x
269 #define Xstr(x) Str(x)
271 /* String utilities. */
273 void list_free_strings_full (GList *list);
274 void slist_free_strings_full (GSList *list);
276 void hash_free_strings (GHashTable *table);
278 gint str_case_equal (gconstpointer v,
279 gconstpointer v2);
280 guint str_case_hash (gconstpointer key);
282 /* number-string conversion */
283 gint to_number (const gchar *nstr);
284 gchar *itos_buf (gchar *nstr,
285 gint n);
286 gchar *itos (gint n);
287 gchar *to_human_readable (goffset size);
289 /* alternative string functions */
290 gint path_cmp (const gchar *s1,
291 const gchar *s2);
292 gchar *strretchomp (gchar *str);
293 gchar *strtailchomp (gchar *str,
294 gchar tail_char);
295 gchar *strcrchomp (gchar *str);
296 gchar *strcrlftrunc (gchar *str);
297 #ifndef HAVE_STRCASESTR
298 gchar *strcasestr (const gchar *haystack,
299 const gchar *needle);
300 #endif /* HAVE_STRCASESTR */
301 gchar *strncasestr (const gchar *haystack,
302 gint haystack_len,
303 const gchar *needle);
304 gpointer my_memmem (gconstpointer haystack,
305 size_t haystacklen,
306 gconstpointer needle,
307 size_t needlelen);
308 gchar *strncpy2 (gchar *dest,
309 const gchar *src,
310 size_t n);
312 gboolean is_next_nonascii (const gchar *s);
313 gint get_next_word_len (const gchar *s);
315 /* functions for string parsing */
316 gint subject_compare (const gchar *s1,
317 const gchar *s2);
318 gint subject_compare_for_sort (const gchar *s1,
319 const gchar *s2);
320 void trim_subject (gchar *str);
321 void eliminate_parenthesis (gchar *str,
322 gchar op,
323 gchar cl);
324 void extract_parenthesis (gchar *str,
325 gchar op,
326 gchar cl);
328 void extract_quote (gchar *str,
329 gchar quote_chr);
330 gchar *escape_internal_quotes (gchar *str,
331 gchar quote_chr);
332 void eliminate_address_comment (gchar *str);
333 gchar *strchr_with_skip_quote (const gchar *str,
334 gint quote_chr,
335 gint c);
336 void extract_address (gchar *str);
337 void extract_list_id_str (gchar *str);
339 GSList *address_list_append (GSList *addr_list,
340 const gchar *str);
341 GSList *address_list_append_with_comments(GSList *addr_list,
342 const gchar *str);
343 GSList *references_list_prepend (GSList *msgid_list,
344 const gchar *str);
345 GSList *references_list_append (GSList *msgid_list,
346 const gchar *str);
347 GSList *newsgroup_list_append (GSList *group_list,
348 const gchar *str);
350 GList *add_history (GList *list,
351 const gchar *str);
353 void remove_return (gchar *str);
354 void remove_space (gchar *str);
355 void unfold_line (gchar *str);
356 void subst_char (gchar *str,
357 gchar orig,
358 gchar subst);
359 void subst_chars (gchar *str,
360 gchar *orig,
361 gchar subst);
362 void subst_for_filename (gchar *str);
363 void subst_for_shellsafe_filename (gchar *str);
364 gboolean is_ascii_str (const gchar *str);
365 gint get_quote_level (const gchar *str,
366 const gchar *quote_chars);
367 gint check_line_length (const gchar *str,
368 gint max_chars,
369 gint *line);
371 gchar **strsplit_with_quote (const gchar *str,
372 const gchar *delim,
373 gint max_tokens);
375 gchar *get_abbrev_newsgroup_name (const gchar *group,
376 gint len);
377 gchar *trim_string (const gchar *str,
378 gint len);
380 GList *uri_list_extract_filenames (const gchar *uri_list);
381 gboolean is_uri_string (const gchar *str);
382 gchar *get_uri_path (const gchar *uri);
383 gint get_uri_len (const gchar *str);
384 void decode_uri (gchar *decoded_uri,
385 const gchar *encoded_uri);
386 void decode_uri_with_plus (gchar *decoded_uri,
387 const gchar *encoded_uri,
388 gboolean with_plus);
389 gint scan_mailto_url (const gchar *mailto,
390 gchar **from,
391 gchar **to,
392 gchar **cc,
393 gchar **bcc,
394 gchar **subject,
395 gchar **body,
396 gchar ***attach,
397 gchar **inreplyto);
399 /* return static strings */
400 const gchar *get_home_dir (void);
401 const gchar *get_rc_dir (void);
402 void set_rc_dir (const gchar *dir);
403 gboolean rc_dir_is_alt (void);
404 const gchar *get_mail_base_dir (void);
405 const gchar *get_news_cache_dir (void);
406 const gchar *get_imap_cache_dir (void);
407 const gchar *get_mime_tmp_dir (void);
408 const gchar *get_template_dir (void);
409 const gchar *get_plugin_dir (void);
410 const gchar *get_tmp_dir (void);
411 const gchar *get_locale_dir (void);
412 gchar *get_tmp_file (void);
413 const gchar *get_domain_name (void);
414 gboolean is_numeric_host_address (const gchar *hostaddress);
415 const gchar *get_desktop_file(void);
416 #ifdef G_OS_WIN32
417 const gchar *w32_get_themes_dir (void);
418 const gchar *w32_get_cert_file (void);
419 #endif
420 /* file / directory handling */
421 off_t get_file_size (const gchar *file);
422 time_t get_file_mtime (const gchar *file);
424 gboolean file_exist (const gchar *file,
425 gboolean allow_fifo);
426 gboolean is_relative_filename (const gchar *file);
427 gboolean is_dir_exist (const gchar *dir);
428 gboolean is_file_entry_exist (const gchar *file);
429 gboolean is_file_entry_regular(const gchar *file);
431 #define is_file_exist(file) file_exist(file, FALSE)
432 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
434 gint change_dir (const gchar *dir);
435 gint make_dir (const gchar *dir);
436 gint make_dir_hier (const gchar *dir);
437 gint remove_all_files (const gchar *dir);
438 gint remove_numbered_files (const gchar *dir,
439 guint first,
440 guint last);
441 gint remove_numbered_files_not_in_list(const gchar *dir,
442 GSList *numberlist);
443 gint remove_all_numbered_files (const gchar *dir);
444 gint remove_dir_recursive (const gchar *dir);
445 gchar *canonicalize_str (const gchar *str);
446 gchar *normalize_newlines (const gchar *str);
448 gchar *get_outgoing_rfc2822_str (FILE *fp);
450 char *fgets_crlf(char *buf, int size, FILE *stream);
452 /* process execution */
453 gint execute_command_line (const gchar *cmdline,
454 gboolean async,
455 const gchar *working_directory);
456 gchar *get_command_output (const gchar *cmdline);
457 FILE *get_command_output_stream (const gchar *cmdline);
459 /* open URI with external browser */
460 gint open_uri(const gchar *uri, const gchar *cmdline);
461 /* open file with text editor */
462 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
464 /* time functions */
465 time_t remote_tzoffset_sec (const gchar *zone);
466 time_t tzoffset_sec (time_t *now);
467 gchar *tzoffset (time_t *now);
468 void get_rfc822_date (gchar *buf,
469 gint len);
470 void get_rfc822_date_hide_tz (gchar *buf,
471 gint len);
473 size_t fast_strftime (gchar *buf,
474 gint buflen,
475 const gchar *format,
476 struct tm *lt);
478 /* debugging */
479 #ifdef HAVE_VA_OPT
480 void debug_print_real (const char *file, int line, const gchar *format, ...) G_GNUC_PRINTF(3, 4);
481 #else
482 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
483 #endif
484 const char * debug_srcname (const char *file);
486 /* subject threading */
487 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
488 void subject_table_insert(GHashTable *subject_table, gchar * subject,
489 void * data);
490 void subject_table_remove(GHashTable *subject_table, gchar * subject);
491 void utils_free_regex(void);
492 gint subject_get_prefix_length (const gchar *subject);
494 /* quoting recognition */
495 const gchar * line_has_quote_char (const gchar *str,
496 const gchar *quote_chars);
498 gint g_int_compare (gconstpointer a, gconstpointer b);
500 gchar *generate_mime_boundary (const gchar *prefix);
502 gint quote_cmd_argument(gchar * result, guint size,
503 const gchar * path);
504 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
506 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
507 void get_hex_str(gchar *out, guchar ch);
509 /* auto pointer for containers that support GType system */
511 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
512 typedef struct AutoPointer GAuto;
513 GType g_auto_pointer_register (void);
514 GAuto *g_auto_pointer_new (gpointer pointer);
515 GAuto *g_auto_pointer_new_with_free (gpointer p,
516 GFreeFunc free);
517 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
518 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
519 void g_auto_pointer_free (GAuto *auto_ptr);
520 gboolean get_uri_part (const gchar *start,
521 const gchar *scanpos,
522 const gchar **bp,
523 const gchar **ep,
524 gboolean hdr);
525 gchar *make_uri_string (const gchar *bp,
526 const gchar *ep);
527 gboolean get_email_part (const gchar *start,
528 const gchar *scanpos,
529 const gchar **bp,
530 const gchar **ep,
531 gboolean hdr);
532 gchar *make_email_string(const gchar *bp,
533 const gchar *ep);
534 gchar *make_http_string (const gchar *bp,
535 const gchar *ep);
537 gchar *mailcap_get_command_for_type(const gchar *type,
538 const gchar *file_to_open);
539 void mailcap_update_default (const gchar *type,
540 const gchar *command);
542 gboolean file_is_email(const gchar *filename);
543 gboolean sc_g_list_bigger(GList *list, gint max);
544 gboolean sc_g_slist_bigger(GSList *list, gint max);
546 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
548 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len);
550 gboolean get_random_bytes(void *buf, size_t count);
552 #ifdef __cplusplus
554 #endif
556 gboolean get_serverportfp_from_filename(const gchar *str, gchar **server, gchar **port, gchar **fp);
558 #ifdef G_OS_WIN32
559 gchar *win32_debug_log_path(void);
560 #endif
562 #endif /* __UTILS_H__ */