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