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