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