Code cleanup around glib version check (2.28 minimum).
[claws.git] / src / common / utils.h
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2016 Hiroyuki Yamamoto and the Claws Mail team
4  *
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.
9  *
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.
14  *
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/>.
17  *
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
21  */
22
23 #ifndef __UTILS_H__
24 #define __UTILS_H__
25
26 #ifdef HAVE_CONFIG_H
27 #include "claws-features.h"
28 #endif
29
30 #ifdef HAVE_BACKTRACE
31 #include <execinfo.h>
32 #endif
33
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
49
50 /* The Hurd doesn't have this limit */
51 #ifndef PATH_MAX
52   #define PATH_MAX 4196
53 #endif
54
55 #ifdef G_OS_WIN32
56
57 #define fsync _commit
58
59 #define pipe(phandles)  _pipe (phandles, 4096, _O_BINARY)
60 #endif
61 /* Wrappers for C library function that take pathname arguments. */
62 #  include <glib/gstdio.h>
63
64 /* why is this sometimes undefined !? */
65 #ifndef G_MAXOFFSET
66 typedef gint64 goffset;
67 #define G_MINOFFSET     G_MININT64
68 #define G_MAXOFFSET     G_MAXINT64
69 #endif
70
71 #ifndef HAVE_U32_TYPEDEF
72   #undef u32        /* maybe there is a macro with this name */
73   typedef guint32 u32;
74   #define HAVE_U32_TYPEDEF
75 #endif
76
77 #ifndef BIG_ENDIAN_HOST
78   #if (G_BYTE_ORDER == G_BIG_ENDIAN)
79     #define BIG_ENDIAN_HOST 1
80   #endif
81 #endif
82
83 #define CHDIR_RETURN_IF_FAIL(dir) \
84 { \
85         if (change_dir(dir) < 0) return; \
86 }
87
88 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
89 { \
90         if (change_dir(dir) < 0) return val; \
91 }
92
93 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
94 { \
95         if (change_dir(dir) < 0) { \
96                 code \
97                 return val; \
98         } \
99 }
100
101 #define Xalloca(ptr, size, iffail) \
102 { \
103         if ((ptr = alloca(size)) == NULL) { \
104                 g_warning("can't allocate memory"); \
105                 iffail; \
106         } \
107 }
108
109 #define Xstrdup_a(ptr, str, iffail) \
110 { \
111         gchar *__tmp; \
112  \
113         if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
114                 g_warning("can't allocate memory"); \
115                 iffail; \
116         } else \
117                 strcpy(__tmp, str); \
118  \
119         ptr = __tmp; \
120 }
121
122 #define Xstrndup_a(ptr, str, len, iffail) \
123 { \
124         gchar *__tmp; \
125  \
126         if ((__tmp = alloca(len + 1)) == NULL) { \
127                 g_warning("can't allocate memory"); \
128                 iffail; \
129         } else { \
130                 memcpy(__tmp, str, len); \
131                 __tmp[len] = '\0'; \
132         } \
133  \
134         ptr = __tmp; \
135 }
136
137 #define Xstrcat_a(ptr, str1, str2, iffail) \
138 { \
139         gchar *__tmp; \
140         gint len1, len2; \
141  \
142         len1 = strlen(str1); \
143         len2 = strlen(str2); \
144         if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
145                 g_warning("can't allocate memory"); \
146                 iffail; \
147         } else { \
148                 memcpy(__tmp, str1, len1); \
149                 memcpy(__tmp + len1, str2, len2 + 1); \
150         } \
151  \
152         ptr = __tmp; \
153 }
154
155 #define AUTORELEASE_STR(str, iffail) \
156 { \
157         gchar *__str; \
158         Xstrdup_a(__str, str, iffail); \
159         g_free(str); \
160         str = __str; \
161 }
162
163 #define FILE_OP_ERROR(file, func) \
164 { \
165         g_printerr("%s: ", file); \
166         fflush(stderr); \
167         perror(func); \
168 }
169
170 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
171
172 /* from NetworkManager */
173 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
174 #define print_backtrace()                                               \
175 G_STMT_START                                                            \
176 {                                                                       \
177         void *_call_stack[512];                                         \
178         int  _call_stack_size;                                          \
179         char **_symbols;                                                \
180         _call_stack_size = backtrace (_call_stack,                      \
181                                       G_N_ELEMENTS (_call_stack));      \
182         _symbols = backtrace_symbols (_call_stack, _call_stack_size);   \
183         if (_symbols != NULL)                                           \
184         {                                                               \
185                 int _i;                                                 \
186                 _i = 0;                                                 \
187                 g_print ("traceback:\n");                               \
188                 while (_i < _call_stack_size)                           \
189                 {                                                       \
190                         g_print ("%d:\t%s\n", _i, _symbols[_i]);        \
191                         _i++;                                           \
192                 }                                                       \
193                 free (_symbols);                                        \
194         }                                                               \
195 }                                                                       \
196 G_STMT_END
197 #else
198 #define print_backtrace()                                               \
199 G_STMT_START                                                            \
200 {                                                                       \
201 }                                                                       \
202 G_STMT_END
203 #endif
204
205
206 #define cm_return_val_if_fail(expr,val) G_STMT_START {                  \
207         if (!(expr)) {                                                  \
208                 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
209                 print_backtrace();                                      \
210                 g_print("\n");                                          \
211                 return val;                                             \
212         }                                                               \
213 } G_STMT_END
214
215 #define cm_return_if_fail(expr) G_STMT_START {                          \
216         if (!(expr)) {                                                  \
217                 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
218                 print_backtrace();                                      \
219                 g_print("\n");                                          \
220                 return;                                                 \
221         }                                                               \
222 } G_STMT_END
223
224 #ifndef MIN
225         #define MIN(a, b) ((a) < (b) ? (a) : (b))
226 #endif
227 #ifndef MAX
228         #define MAX(a, b) ((a) > (b) ? (a) : (b))
229 #endif
230
231 #ifdef __cplusplus
232 extern "C" {
233 #endif
234
235 typedef gpointer (*GNodeMapFunc)        (gpointer nodedata, gpointer data);
236
237 /* debug functions */
238 void debug_set_mode             (gboolean mode);
239 gboolean debug_get_mode         (void);
240
241 #ifndef __CYGWIN__
242 #define debug_print \
243         debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
244         debug_print_real
245 #else
246   /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
247 #define debug_print \
248         debug_print_real("%s:%d:", __FILE__, __LINE__), \
249         debug_print_real
250 #endif
251
252 /* for macro expansion */
253 #define Str(x)  #x
254 #define Xstr(x) Str(x)
255
256
257 /* System related stuff.  */
258
259 gboolean superuser_p (void);
260
261 /* List utilities. */
262
263 GSList *slist_copy_deep         (GSList         *list,
264                                  GCopyFunc       func);
265
266 /* String utilities.  */
267
268 void list_free_strings_full             (GList          *list);
269 void slist_free_strings_full    (GSList         *list);
270
271 void hash_free_strings          (GHashTable     *table);
272
273 gint str_case_equal             (gconstpointer   v,
274                                  gconstpointer   v2);
275 guint str_case_hash             (gconstpointer   key);
276
277 void ptr_array_free_strings     (GPtrArray      *array);
278
279 /* number-string conversion */
280 gint to_number                  (const gchar *nstr);
281 gchar *itos_buf                 (gchar       *nstr,
282                                  gint         n);
283 gchar *itos                     (gint         n);
284 gchar *to_human_readable        (goffset      size);
285
286 /* alternative string functions */
287 gint strcmp2            (const gchar    *s1,
288                          const gchar    *s2);
289 gchar *strstr2          (const gchar    *s1,
290                          const gchar    *s2);
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 gint file_strip_crs     (const gchar    *file);
298 gchar *strcasestr       (const gchar    *haystack,
299                          const gchar    *needle);
300 gchar *strncasestr      (const gchar    *haystack,
301                          gint            haystack_len,
302                          const gchar    *needle);
303 gpointer my_memmem      (gconstpointer   haystack,
304                          size_t          haystacklen,
305                          gconstpointer   needle,
306                          size_t          needlelen);
307 gchar *strncpy2         (gchar          *dest,
308                          const gchar    *src,
309                          size_t          n);
310
311 gboolean is_next_nonascii       (const gchar *s);
312 gint get_next_word_len          (const gchar *s);
313
314 /* functions for string parsing */
315 gint subject_compare                    (const gchar    *s1,
316                                          const gchar    *s2);
317 gint subject_compare_for_sort           (const gchar    *s1,
318                                          const gchar    *s2);
319 void trim_subject                       (gchar          *str);
320 void eliminate_parenthesis              (gchar          *str,
321                                          gchar           op,
322                                          gchar           cl);
323 void extract_parenthesis                (gchar          *str,
324                                          gchar           op,
325                                          gchar           cl);
326
327 void extract_quote                      (gchar          *str,
328                                          gchar           quote_chr);
329 gchar *escape_internal_quotes           (gchar          *str,
330                                          gchar           quote_chr);
331 void eliminate_address_comment          (gchar          *str);
332 gchar *strchr_with_skip_quote           (const gchar    *str,
333                                          gint            quote_chr,
334                                          gint            c);
335 void extract_address                    (gchar          *str);
336 void extract_list_id_str                (gchar          *str);
337
338 GSList *address_list_append             (GSList         *addr_list,
339                                          const gchar    *str);
340 GSList *address_list_append_with_comments(GSList        *addr_list,
341                                          const gchar    *str);
342 GSList *references_list_prepend         (GSList         *msgid_list,
343                                          const gchar    *str);
344 GSList *references_list_append          (GSList         *msgid_list,
345                                          const gchar    *str);
346 GSList *newsgroup_list_append           (GSList         *group_list,
347                                          const gchar    *str);
348
349 GList *add_history                      (GList          *list,
350                                          const gchar    *str);
351
352 void remove_return                      (gchar          *str);
353 void remove_space                       (gchar          *str);
354 void unfold_line                        (gchar          *str);
355 void subst_char                         (gchar          *str,
356                                          gchar           orig,
357                                          gchar           subst);
358 void subst_chars                        (gchar          *str,   
359                                          gchar          *orig, 
360                                          gchar          subst);
361 void subst_for_filename                 (gchar          *str);
362 void subst_for_shellsafe_filename       (gchar          *str);
363 gboolean is_ascii_str                   (const gchar    *str);
364 gint get_quote_level                    (const gchar    *str,
365                                          const gchar    *quote_chars);
366 gint check_line_length                  (const gchar    *str,
367                                          gint            max_chars,
368                                          gint           *line);
369
370 gchar **strsplit_with_quote             (const gchar    *str,
371                                          const gchar    *delim,
372                                          gint            max_tokens);
373
374 gchar *get_abbrev_newsgroup_name        (const gchar    *group,
375                                          gint            len);
376 gchar *trim_string                      (const gchar    *str,
377                                          gint            len);
378
379 GList *uri_list_extract_filenames       (const gchar    *uri_list);
380 gboolean is_uri_string                  (const gchar    *str);
381 gchar *get_uri_path                     (const gchar    *uri);
382 gint get_uri_len                        (const gchar    *str);
383 void decode_uri                         (gchar          *decoded_uri,
384                                          const gchar    *encoded_uri);
385 void decode_uri_with_plus               (gchar          *decoded_uri, 
386                                          const gchar    *encoded_uri, 
387                                          gboolean        with_plus);
388 gint scan_mailto_url                    (const gchar    *mailto,
389                                          gchar         **from,
390                                          gchar         **to,
391                                          gchar         **cc,
392                                          gchar         **bcc,
393                                          gchar         **subject,
394                                          gchar         **body,
395                                          gchar         ***attach,
396                                          gchar         **inreplyto);
397
398 /* return static strings */
399 const gchar *get_home_dir               (void);
400 const gchar *get_rc_dir                 (void);
401 void  set_rc_dir                        (const gchar *dir);
402 gboolean rc_dir_is_alt                  (void);
403 const gchar *get_mail_base_dir          (void);
404 const gchar *get_news_cache_dir         (void);
405 const gchar *get_imap_cache_dir         (void);
406 const gchar *get_mime_tmp_dir           (void);
407 const gchar *get_template_dir           (void);
408 const gchar *get_plugin_dir             (void);
409 const gchar *get_tmp_dir                (void);
410 const gchar *get_locale_dir             (void);
411 gchar *get_tmp_file                     (void);
412 const gchar *get_domain_name            (void);
413 const gchar *get_desktop_file(void);
414 #ifdef G_OS_WIN32
415 const gchar *w32_get_themes_dir    (void);
416 const gchar *w32_get_cert_file          (void);
417 #endif
418 /* file / directory handling */
419 off_t get_file_size             (const gchar    *file);
420 time_t get_file_mtime           (const gchar *file);
421
422 gboolean file_exist             (const gchar    *file,
423                                  gboolean        allow_fifo);
424 gboolean is_relative_filename   (const gchar *file);
425 gboolean is_dir_exist           (const gchar    *dir);
426 gboolean is_file_entry_exist    (const gchar    *file);
427 gboolean dirent_is_regular_file (struct dirent  *d);
428
429 #define is_file_exist(file)             file_exist(file, FALSE)
430 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
431
432 gint change_dir                 (const gchar    *dir);
433 gint make_dir                   (const gchar    *dir);
434 gint make_dir_hier              (const gchar    *dir);
435 gint remove_all_files           (const gchar    *dir);
436 gint remove_numbered_files      (const gchar    *dir,
437                                  guint           first,
438                                  guint           last);
439 gint remove_numbered_files_not_in_list(const gchar *dir,
440                                        GSList *numberlist);
441 gint remove_all_numbered_files  (const gchar    *dir);
442 gint remove_dir_recursive       (const gchar    *dir);
443 gint append_file                (const gchar    *src,
444                                  const gchar    *dest,
445                                  gboolean        keep_backup);
446 gint rename_force               (const gchar    *oldpath,
447                                  const gchar    *newpath);
448 gint copy_file                  (const gchar    *src,
449                                  const gchar    *dest,
450                                  gboolean        keep_backup);
451 gint move_file                  (const gchar    *src,
452                                  const gchar    *dest,
453                                  gboolean        overwrite);
454 gint copy_dir                   (const gchar    *src,
455                                  const gchar    *dest);
456 gint copy_file_part_to_fp       (FILE           *fp,
457                                  off_t           offset,
458                                  size_t          length,
459                                  FILE           *dest_fp);
460 gint copy_file_part             (FILE           *fp,
461                                  off_t           offset,
462                                  size_t          length,
463                                  const gchar    *dest);
464
465 gchar *canonicalize_str         (const gchar    *str);
466 gint canonicalize_file          (const gchar    *src,
467                                  const gchar    *dest);
468 gint canonicalize_file_replace  (const gchar    *file);
469
470 gchar *normalize_newlines       (const gchar    *str);
471
472 gchar *get_outgoing_rfc2822_str (FILE           *fp);
473
474 gint change_file_mode_rw        (FILE           *fp,
475                                  const gchar    *file);
476 FILE *my_tmpfile                (void);
477 FILE *get_tmpfile_in_dir        (const gchar    *dir,
478                                  gchar         **filename);
479 FILE *str_open_as_stream        (const gchar    *str);
480 gint str_write_to_file          (const gchar    *str,
481                                  const gchar    *file);
482 gchar *file_read_to_str         (const gchar    *file);
483 gchar *file_read_stream_to_str  (FILE           *fp);
484 gchar *file_read_to_str_no_recode(const gchar *file);
485 gchar *file_read_stream_to_str_no_recode(FILE *fp);
486
487 char *fgets_crlf(char *buf, int size, FILE *stream);
488
489 /* process execution */
490 gint execute_command_line       (const gchar    *cmdline,
491                                  gboolean        async,
492                                  const gchar    *working_directory);
493 gchar *get_command_output       (const gchar    *cmdline);
494
495 /* open URI with external browser */
496 gint open_uri(const gchar *uri, const gchar *cmdline);
497 /* open file with text editor */
498 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
499
500 /* time functions */
501 time_t remote_tzoffset_sec      (const gchar    *zone);
502 time_t tzoffset_sec             (time_t         *now);
503 gchar *tzoffset                 (time_t         *now);
504 void get_rfc822_date            (gchar          *buf,
505                                  gint            len);
506 void get_rfc822_date_hide_tz    (gchar          *buf,
507                                  gint            len);
508
509 size_t fast_strftime            (gchar                  *buf, 
510                                  gint                    buflen, 
511                                  const gchar            *format, 
512                                  struct tm              *lt);
513
514 /* debugging */
515 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
516 const char * debug_srcname (const char *file);
517
518 /* subject threading */
519 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
520 void subject_table_insert(GHashTable *subject_table, gchar * subject,
521                           void * data);
522 void subject_table_remove(GHashTable *subject_table, gchar * subject);
523 void utils_free_regex(void);
524 gint subject_get_prefix_length (const gchar *subject);
525
526 /* quoting recognition */
527 const gchar * line_has_quote_char       (const gchar *str,
528                                          const gchar *quote_chars);
529
530 gint g_int_compare      (gconstpointer a, gconstpointer b);
531
532 gchar *generate_mime_boundary   (const gchar *prefix);
533
534 gint quote_cmd_argument(gchar * result, guint size,
535                         const gchar * path);
536 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
537
538 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
539 void get_hex_str(gchar *out, guchar ch);
540
541 /* auto pointer for containers that support GType system */
542
543 #define G_TYPE_AUTO_POINTER     g_auto_pointer_register()
544 typedef struct AutoPointer      GAuto;
545 GType g_auto_pointer_register           (void);
546 GAuto *g_auto_pointer_new               (gpointer pointer);
547 GAuto *g_auto_pointer_new_with_free     (gpointer p, 
548                                          GFreeFunc free);
549 gpointer g_auto_pointer_get_ptr         (GAuto *auto_ptr);
550 GAuto *g_auto_pointer_copy              (GAuto *auto_ptr);
551 void g_auto_pointer_free                (GAuto *auto_ptr);
552 void replace_returns                    (gchar *str);
553 gboolean get_uri_part   (const gchar *start,
554                          const gchar *scanpos,
555                          const gchar **bp,
556                          const gchar **ep,
557                          gboolean hdr);
558 gchar *make_uri_string  (const gchar *bp,
559                          const gchar *ep);
560 gboolean get_email_part (const gchar *start, 
561                          const gchar *scanpos,
562                          const gchar **bp, 
563                          const gchar **ep,
564                          gboolean hdr);
565 gchar *make_email_string(const gchar *bp,
566                          const gchar *ep);
567 gchar *make_http_string (const gchar *bp,
568                          const gchar *ep);
569
570 gchar *mailcap_get_command_for_type(const gchar *type, 
571                                     const gchar *file_to_open);
572 void mailcap_update_default        (const gchar *type,
573                                     const gchar *command);
574
575 gboolean file_is_email(const gchar *filename);
576 gboolean sc_g_list_bigger(GList *list, gint max);
577 gboolean sc_g_slist_bigger(GSList *list, gint max);
578
579 int claws_unlink(const gchar *filename);
580
581 GMutex *cm_mutex_new(void);
582 void cm_mutex_free(GMutex *mutex);
583
584 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
585
586 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len);
587
588 #if !GLIB_CHECK_VERSION(2, 30, 0)
589 gchar   *g_utf8_substring         (const gchar *p,
590                                    glong        start_pos,
591                                    glong        end_pos) G_GNUC_MALLOC;
592 #endif
593
594 gboolean get_random_bytes(void *buf, size_t count);
595
596 #ifdef __cplusplus
597 }
598 #endif
599
600 #endif /* __UTILS_H__ */