Fix error creation and warning
[claws.git] / src / common / utils.h
blob43f68a2ac63819a8db0b0ac2471ac2b4284f81c5
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 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 <dirent.h>
42 #include <time.h>
43 #if HAVE_ALLOCA_H
44 # include <alloca.h>
45 #endif
46 #if HAVE_WCHAR_H
47 # include <wchar.h>
48 #endif
50 /* The Hurd doesn't have these limits */
51 #ifndef PATH_MAX
52 #define PATH_MAX 4196
53 #endif
54 #ifndef HOST_NAME_MAX
55 #define HOST_NAME_MAX 256
56 #endif
58 #ifdef G_OS_WIN32
60 #define fsync _commit
62 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
63 #endif
64 /* Wrappers for C library function that take pathname arguments. */
65 # include <glib/gstdio.h>
67 /* why is this sometimes undefined !? */
68 #ifndef G_MAXOFFSET
69 typedef gint64 goffset;
70 #define G_MINOFFSET G_MININT64
71 #define G_MAXOFFSET G_MAXINT64
72 #endif
74 #ifndef BIG_ENDIAN_HOST
75 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
76 #define BIG_ENDIAN_HOST 1
77 #endif
78 #endif
80 #define CHDIR_RETURN_IF_FAIL(dir) \
81 { \
82 if (change_dir(dir) < 0) return; \
85 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
86 { \
87 if (change_dir(dir) < 0) return val; \
90 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
91 { \
92 if (change_dir(dir) < 0) { \
93 code \
94 return val; \
95 } \
98 #define MAX_ALLOCA_MEM_SIZE 102400
100 #define Xalloca(ptr, size, iffail) \
102 size_t __size = size; \
104 if (__size > MAX_ALLOCA_MEM_SIZE) { \
105 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
106 iffail; \
108 if ((ptr = alloca(__size)) == NULL) { \
109 g_warning("can't allocate memory"); \
110 iffail; \
114 #define Xstrdup_a(ptr, str, iffail) \
116 gchar *__tmp; \
117 size_t __size = strlen(str); \
119 if (__size > MAX_ALLOCA_MEM_SIZE) { \
120 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
121 iffail; \
123 if ((__tmp = alloca(__size + 1)) == NULL) { \
124 g_warning("can't allocate memory"); \
125 iffail; \
126 } else \
127 strcpy(__tmp, str); \
129 ptr = __tmp; \
132 #define Xstrndup_a(ptr, str, len, iffail) \
134 gchar *__tmp; \
135 size_t __size = len; \
137 if (__size > MAX_ALLOCA_MEM_SIZE) { \
138 g_warning("%" G_GSIZE_FORMAT "bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
139 iffail; \
141 if ((__tmp = alloca(__size + 1)) == NULL) { \
142 g_warning("can't allocate memory"); \
143 iffail; \
144 } else { \
145 memcpy(__tmp, str, __size); \
146 __tmp[__size] = '\0'; \
149 ptr = __tmp; \
152 #define Xstrcat_a(ptr, str1, str2, iffail) \
154 gchar *__tmp; \
155 size_t len1, len2; \
157 len1 = strlen(str1); \
158 len2 = strlen(str2); \
159 if (len1 + len2 > MAX_ALLOCA_MEM_SIZE) { \
160 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", len1 + len2, MAX_ALLOCA_MEM_SIZE); \
161 iffail; \
163 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
164 g_warning("can't allocate memory"); \
165 iffail; \
166 } else { \
167 memcpy(__tmp, str1, len1); \
168 memcpy(__tmp + len1, str2, len2 + 1); \
171 ptr = __tmp; \
174 #define AUTORELEASE_STR(str, iffail) \
176 gchar *__str; \
177 Xstrdup_a(__str, str, iffail); \
178 g_free(str); \
179 str = __str; \
182 #define FILE_OP_ERROR(file, func) \
184 g_printerr("%s: ", file); \
185 fflush(stderr); \
186 perror(func); \
189 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
191 /* from NetworkManager */
192 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
193 #define print_backtrace() \
194 G_STMT_START \
196 void *_call_stack[512]; \
197 int _call_stack_size; \
198 char **_symbols; \
199 _call_stack_size = backtrace (_call_stack, \
200 G_N_ELEMENTS (_call_stack)); \
201 _symbols = backtrace_symbols (_call_stack, _call_stack_size); \
202 if (_symbols != NULL) \
204 int _i; \
205 _i = 0; \
206 g_print ("traceback:\n"); \
207 while (_i < _call_stack_size) \
209 g_print ("%d:\t%s\n", _i, _symbols[_i]); \
210 _i++; \
212 free (_symbols); \
215 G_STMT_END
216 #else
217 #define print_backtrace() \
218 G_STMT_START \
221 G_STMT_END
222 #endif
225 #define cm_return_val_if_fail(expr,val) G_STMT_START { \
226 if (!(expr)) { \
227 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
228 print_backtrace(); \
229 g_print("\n"); \
230 return val; \
232 } G_STMT_END
234 #define cm_return_if_fail(expr) G_STMT_START { \
235 if (!(expr)) { \
236 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
237 print_backtrace(); \
238 g_print("\n"); \
239 return; \
241 } G_STMT_END
243 #ifndef MIN
244 #define MIN(a, b) ((a) < (b) ? (a) : (b))
245 #endif
246 #ifndef MAX
247 #define MAX(a, b) ((a) > (b) ? (a) : (b))
248 #endif
250 #ifdef __cplusplus
251 extern "C" {
252 #endif
254 typedef gpointer (*GNodeMapFunc) (gpointer nodedata, gpointer data);
256 /* debug functions */
257 void debug_set_mode (gboolean mode);
258 gboolean debug_get_mode (void);
260 #ifdef HAVE_VA_OPT
261 #define debug_print(format, ...) debug_print_real(__FILE__, __LINE__, format __VA_OPT__(,) __VA_ARGS__)
262 #else
263 #define debug_print \
264 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
265 debug_print_real
266 #endif
268 /* for macro expansion */
269 #define Str(x) #x
270 #define Xstr(x) Str(x)
272 /* String utilities. */
274 void list_free_strings_full (GList *list);
275 void slist_free_strings_full (GSList *list);
277 void hash_free_strings (GHashTable *table);
279 gint str_case_equal (gconstpointer v,
280 gconstpointer v2);
281 guint str_case_hash (gconstpointer key);
283 /* number-string conversion */
284 gint to_number (const gchar *nstr);
285 gchar *itos_buf (gchar *nstr,
286 gint n);
287 gchar *itos (gint n);
288 gchar *to_human_readable (goffset size);
290 /* alternative string functions */
291 gint path_cmp (const gchar *s1,
292 const gchar *s2);
293 gchar *strretchomp (gchar *str);
294 gchar *strtailchomp (gchar *str,
295 gchar tail_char);
296 gchar *strcrchomp (gchar *str);
297 gchar *strcrlftrunc (gchar *str);
298 #ifndef HAVE_STRCASESTR
299 gchar *strcasestr (const gchar *haystack,
300 const gchar *needle);
301 #endif /* HAVE_STRCASESTR */
302 gchar *strncasestr (const gchar *haystack,
303 gint haystack_len,
304 const gchar *needle);
305 gpointer my_memmem (gconstpointer haystack,
306 size_t haystacklen,
307 gconstpointer needle,
308 size_t needlelen);
309 gchar *strncpy2 (gchar *dest,
310 const gchar *src,
311 size_t n);
313 gboolean is_next_nonascii (const gchar *s);
314 gint get_next_word_len (const gchar *s);
316 /* functions for string parsing */
317 gint subject_compare (const gchar *s1,
318 const gchar *s2);
319 gint subject_compare_for_sort (const gchar *s1,
320 const gchar *s2);
321 void trim_subject (gchar *str);
322 void eliminate_parenthesis (gchar *str,
323 gchar op,
324 gchar cl);
325 void extract_parenthesis (gchar *str,
326 gchar op,
327 gchar cl);
329 void extract_quote (gchar *str,
330 gchar quote_chr);
331 gchar *escape_internal_quotes (gchar *str,
332 gchar quote_chr);
333 void eliminate_address_comment (gchar *str);
334 gchar *strchr_with_skip_quote (const gchar *str,
335 gint quote_chr,
336 gint c);
337 void extract_address (gchar *str);
338 void extract_list_id_str (gchar *str);
340 GSList *address_list_append (GSList *addr_list,
341 const gchar *str);
342 GSList *address_list_append_with_comments(GSList *addr_list,
343 const gchar *str);
344 GSList *references_list_prepend (GSList *msgid_list,
345 const gchar *str);
346 GSList *references_list_append (GSList *msgid_list,
347 const gchar *str);
348 GSList *newsgroup_list_append (GSList *group_list,
349 const gchar *str);
351 GList *add_history (GList *list,
352 const gchar *str);
354 void remove_return (gchar *str);
355 void remove_space (gchar *str);
356 void unfold_line (gchar *str);
357 void subst_char (gchar *str,
358 gchar orig,
359 gchar subst);
360 void subst_chars (gchar *str,
361 gchar *orig,
362 gchar subst);
363 void subst_for_filename (gchar *str);
364 void subst_for_shellsafe_filename (gchar *str);
365 gboolean is_ascii_str (const gchar *str);
366 gint get_quote_level (const gchar *str,
367 const gchar *quote_chars);
368 gint check_line_length (const gchar *str,
369 gint max_chars,
370 gint *line);
372 gchar **strsplit_with_quote (const gchar *str,
373 const gchar *delim,
374 gint max_tokens);
376 gchar *get_abbrev_newsgroup_name (const gchar *group,
377 gint len);
378 gchar *trim_string (const gchar *str,
379 gint len);
381 GList *uri_list_extract_filenames (const gchar *uri_list);
382 gboolean is_uri_string (const gchar *str);
383 gchar *get_uri_path (const gchar *uri);
384 gint get_uri_len (const gchar *str);
385 void decode_uri (gchar *decoded_uri,
386 const gchar *encoded_uri);
387 void decode_uri_with_plus (gchar *decoded_uri,
388 const gchar *encoded_uri,
389 gboolean with_plus);
390 gint scan_mailto_url (const gchar *mailto,
391 gchar **from,
392 gchar **to,
393 gchar **cc,
394 gchar **bcc,
395 gchar **subject,
396 gchar **body,
397 gchar ***attach,
398 gchar **inreplyto);
400 /* return static strings */
401 const gchar *get_home_dir (void);
402 const gchar *get_rc_dir (void);
403 void set_rc_dir (const gchar *dir);
404 gboolean rc_dir_is_alt (void);
405 const gchar *get_mail_base_dir (void);
406 const gchar *get_news_cache_dir (void);
407 const gchar *get_imap_cache_dir (void);
408 const gchar *get_mime_tmp_dir (void);
409 const gchar *get_template_dir (void);
410 const gchar *get_plugin_dir (void);
411 const gchar *get_tmp_dir (void);
412 const gchar *get_locale_dir (void);
413 gchar *get_tmp_file (void);
414 const gchar *get_domain_name (void);
415 gboolean is_numeric_host_address (const gchar *hostaddress);
416 const gchar *get_desktop_file(void);
417 #ifdef G_OS_WIN32
418 const gchar *w32_get_themes_dir (void);
419 const gchar *w32_get_cert_file (void);
420 #endif
421 /* file / directory handling */
422 off_t get_file_size (const gchar *file);
423 time_t get_file_mtime (const gchar *file);
425 gboolean file_exist (const gchar *file,
426 gboolean allow_fifo);
427 gboolean is_relative_filename (const gchar *file);
428 gboolean is_dir_exist (const gchar *dir);
429 gboolean is_file_entry_exist (const gchar *file);
430 gboolean is_file_entry_regular(const gchar *file);
431 gboolean dirent_is_regular_file (struct dirent *d);
433 #define is_file_exist(file) file_exist(file, FALSE)
434 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
436 gint change_dir (const gchar *dir);
437 gint make_dir (const gchar *dir);
438 gint make_dir_hier (const gchar *dir);
439 gint remove_all_files (const gchar *dir);
440 gint remove_numbered_files (const gchar *dir,
441 guint first,
442 guint last);
443 gint remove_numbered_files_not_in_list(const gchar *dir,
444 GSList *numberlist);
445 gint remove_all_numbered_files (const gchar *dir);
446 gint remove_dir_recursive (const gchar *dir);
447 gchar *canonicalize_str (const gchar *str);
448 gchar *normalize_newlines (const gchar *str);
450 gchar *get_outgoing_rfc2822_str (FILE *fp);
452 char *fgets_crlf(char *buf, int size, FILE *stream);
454 /* process execution */
455 gint execute_command_line (const gchar *cmdline,
456 gboolean async,
457 const gchar *working_directory);
458 gchar *get_command_output (const gchar *cmdline);
459 FILE *get_command_output_stream (const gchar *cmdline);
461 /* open URI with external browser */
462 gint open_uri(const gchar *uri, const gchar *cmdline);
463 /* open file with text editor */
464 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
466 /* time functions */
467 time_t remote_tzoffset_sec (const gchar *zone);
468 time_t tzoffset_sec (time_t *now);
469 gchar *tzoffset (time_t *now);
470 void get_rfc822_date (gchar *buf,
471 gint len);
472 void get_rfc822_date_hide_tz (gchar *buf,
473 gint len);
475 size_t fast_strftime (gchar *buf,
476 gint buflen,
477 const gchar *format,
478 struct tm *lt);
480 /* debugging */
481 #ifdef HAVE_VA_OPT
482 void debug_print_real (const char *file, int line, const gchar *format, ...) G_GNUC_PRINTF(3, 4);
483 #else
484 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
485 #endif
486 const char * debug_srcname (const char *file);
488 /* subject threading */
489 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
490 void subject_table_insert(GHashTable *subject_table, gchar * subject,
491 void * data);
492 void subject_table_remove(GHashTable *subject_table, gchar * subject);
493 void utils_free_regex(void);
494 gint subject_get_prefix_length (const gchar *subject);
496 /* quoting recognition */
497 const gchar * line_has_quote_char (const gchar *str,
498 const gchar *quote_chars);
500 gint g_int_compare (gconstpointer a, gconstpointer b);
502 gchar *generate_mime_boundary (const gchar *prefix);
504 gint quote_cmd_argument(gchar * result, guint size,
505 const gchar * path);
506 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
508 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
509 void get_hex_str(gchar *out, guchar ch);
511 /* auto pointer for containers that support GType system */
513 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
514 typedef struct AutoPointer GAuto;
515 GType g_auto_pointer_register (void);
516 GAuto *g_auto_pointer_new (gpointer pointer);
517 GAuto *g_auto_pointer_new_with_free (gpointer p,
518 GFreeFunc free);
519 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
520 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
521 void g_auto_pointer_free (GAuto *auto_ptr);
522 gboolean get_uri_part (const gchar *start,
523 const gchar *scanpos,
524 const gchar **bp,
525 const gchar **ep,
526 gboolean hdr);
527 gchar *make_uri_string (const gchar *bp,
528 const gchar *ep);
529 gboolean get_email_part (const gchar *start,
530 const gchar *scanpos,
531 const gchar **bp,
532 const gchar **ep,
533 gboolean hdr);
534 gchar *make_email_string(const gchar *bp,
535 const gchar *ep);
536 gchar *make_http_string (const gchar *bp,
537 const gchar *ep);
539 gchar *mailcap_get_command_for_type(const gchar *type,
540 const gchar *file_to_open);
541 void mailcap_update_default (const gchar *type,
542 const gchar *command);
544 gboolean file_is_email(const gchar *filename);
545 gboolean sc_g_list_bigger(GList *list, gint max);
546 gboolean sc_g_slist_bigger(GSList *list, gint max);
548 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
550 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len);
552 gboolean get_random_bytes(void *buf, size_t count);
554 #ifdef __cplusplus
556 #endif
558 gboolean get_serverportfp_from_filename(const gchar *str, gchar **server, gchar **port, gchar **fp);
560 #ifdef G_OS_WIN32
561 gchar *win32_debug_log_path(void);
562 #endif
564 #endif /* __UTILS_H__ */