fix bug 4824, 'No syntax highlighting after exiting external editor'
[claws.git] / src / common / utils.h
blobe107805e3bef48d8b0fd7678b3515872f11a64d2
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 The Claws Mail Team and Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * The code of the g_utf8_substring function below is owned by
19 * Matthias Clasen <matthiasc@src.gnome.org>/<mclasen@redhat.com>
20 * and is got from GLIB 2.30
23 #ifndef __UTILS_H__
24 #define __UTILS_H__
26 #ifdef HAVE_CONFIG_H
27 #include "claws-features.h"
28 #endif
30 #ifdef HAVE_BACKTRACE
31 #include <execinfo.h>
32 #endif
34 #include <glib.h>
35 #include <glib-object.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <time.h>
42 #if HAVE_ALLOCA_H
43 # include <alloca.h>
44 #endif
45 #if HAVE_WCHAR_H
46 # include <wchar.h>
47 #endif
49 /* The Hurd doesn't have these limits */
50 #ifndef PATH_MAX
51 #define PATH_MAX 4196
52 #endif
53 #ifndef HOST_NAME_MAX
54 #define HOST_NAME_MAX 256
55 #endif
57 /* Handling Base64 content in procmime and prefs_customheader */
58 #define B64_LINE_SIZE 57
60 #ifdef G_OS_WIN32
62 #define fsync _commit
64 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
65 #endif
66 /* Wrappers for C library function that take pathname arguments. */
67 # include <glib/gstdio.h>
69 /* why is this sometimes undefined !? */
70 #ifndef G_MAXOFFSET
71 typedef gint64 goffset;
72 #define G_MINOFFSET G_MININT64
73 #define G_MAXOFFSET G_MAXINT64
74 #endif
76 #ifndef BIG_ENDIAN_HOST
77 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
78 #define BIG_ENDIAN_HOST 1
79 #endif
80 #endif
82 #define CHDIR_RETURN_IF_FAIL(dir) \
83 { \
84 if (change_dir(dir) < 0) return; \
87 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
88 { \
89 if (change_dir(dir) < 0) return val; \
92 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
93 { \
94 if (change_dir(dir) < 0) { \
95 code \
96 return val; \
97 } \
100 #define MAX_ALLOCA_MEM_SIZE 102400
102 #define Xalloca(ptr, size, iffail) \
104 size_t __size = size; \
106 if (__size > MAX_ALLOCA_MEM_SIZE) { \
107 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
108 iffail; \
110 if ((ptr = alloca(__size)) == NULL) { \
111 g_warning("can't allocate memory"); \
112 iffail; \
116 #define Xstrdup_a(ptr, str, iffail) \
118 gchar *__tmp; \
119 size_t __size = strlen(str); \
121 if (__size > MAX_ALLOCA_MEM_SIZE) { \
122 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
123 iffail; \
125 if ((__tmp = alloca(__size + 1)) == NULL) { \
126 g_warning("can't allocate memory"); \
127 iffail; \
128 } else \
129 strcpy(__tmp, str); \
131 ptr = __tmp; \
134 #define Xstrndup_a(ptr, str, len, iffail) \
136 gchar *__tmp; \
137 size_t __size = len; \
139 if (__size > MAX_ALLOCA_MEM_SIZE) { \
140 g_warning("%" G_GSIZE_FORMAT "bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
141 iffail; \
143 if ((__tmp = alloca(__size + 1)) == NULL) { \
144 g_warning("can't allocate memory"); \
145 iffail; \
146 } else { \
147 memcpy(__tmp, str, __size); \
148 __tmp[__size] = '\0'; \
151 ptr = __tmp; \
154 #define Xstrcat_a(ptr, str1, str2, iffail) \
156 gchar *__tmp; \
157 size_t len1, len2; \
159 len1 = strlen(str1); \
160 len2 = strlen(str2); \
161 if (len1 + len2 > MAX_ALLOCA_MEM_SIZE) { \
162 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", len1 + len2, MAX_ALLOCA_MEM_SIZE); \
163 iffail; \
165 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
166 g_warning("can't allocate memory"); \
167 iffail; \
168 } else { \
169 memcpy(__tmp, str1, len1); \
170 memcpy(__tmp + len1, str2, len2 + 1); \
173 ptr = __tmp; \
176 #define AUTORELEASE_STR(str, iffail) \
178 gchar *__str; \
179 Xstrdup_a(__str, str, iffail); \
180 g_free(str); \
181 str = __str; \
184 #define FILE_OP_ERROR(file, func) \
186 g_printerr("%s: ", file); \
187 fflush(stderr); \
188 perror(func); \
191 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
193 /* from NetworkManager */
194 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
195 #define print_backtrace() \
196 G_STMT_START \
198 void *_call_stack[512]; \
199 int _call_stack_size; \
200 char **_symbols; \
201 _call_stack_size = backtrace (_call_stack, \
202 G_N_ELEMENTS (_call_stack)); \
203 _symbols = backtrace_symbols (_call_stack, _call_stack_size); \
204 if (_symbols != NULL) \
206 int _i; \
207 _i = 0; \
208 g_print ("traceback:\n"); \
209 while (_i < _call_stack_size) \
211 g_print ("%d:\t%s\n", _i, _symbols[_i]); \
212 _i++; \
214 free (_symbols); \
217 G_STMT_END
218 #else
219 #define print_backtrace() \
220 G_STMT_START \
223 G_STMT_END
224 #endif
227 #define cm_return_val_if_fail(expr,val) G_STMT_START { \
228 if (!(expr)) { \
229 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
230 print_backtrace(); \
231 g_print("\n"); \
232 return val; \
234 } G_STMT_END
236 #define cm_return_if_fail(expr) G_STMT_START { \
237 if (!(expr)) { \
238 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
239 print_backtrace(); \
240 g_print("\n"); \
241 return; \
243 } G_STMT_END
245 #ifndef MIN
246 #define MIN(a, b) ((a) < (b) ? (a) : (b))
247 #endif
248 #ifndef MAX
249 #define MAX(a, b) ((a) > (b) ? (a) : (b))
250 #endif
252 #ifdef __cplusplus
253 extern "C" {
254 #endif
256 typedef gpointer (*GNodeMapFunc) (gpointer nodedata, gpointer data);
258 /* debug functions */
259 void debug_set_mode (gboolean mode);
260 gboolean debug_get_mode (void);
262 #ifdef HAVE_VA_OPT
263 #define debug_print(format, ...) debug_print_real(__FILE__, __LINE__, format __VA_OPT__(,) __VA_ARGS__)
264 #else
265 #define debug_print \
266 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
267 debug_print_real
268 #endif
270 /* for macro expansion */
271 #define Str(x) #x
272 #define Xstr(x) Str(x)
274 /* String utilities. */
276 void list_free_strings_full (GList *list);
277 void slist_free_strings_full (GSList *list);
279 void hash_free_strings (GHashTable *table);
281 gint str_case_equal (gconstpointer v,
282 gconstpointer v2);
283 guint str_case_hash (gconstpointer key);
285 /* number-string conversion */
286 gint to_number (const gchar *nstr);
287 gchar *itos_buf (gchar *nstr,
288 gint n);
289 gchar *itos (gint n);
290 gchar *to_human_readable (goffset size);
292 /* alternative string functions */
293 gint path_cmp (const gchar *s1,
294 const gchar *s2);
295 gchar *strretchomp (gchar *str);
296 gchar *strtailchomp (gchar *str,
297 gchar tail_char);
298 gchar *strcrchomp (gchar *str);
299 gchar *strcrlftrunc (gchar *str);
300 #ifndef HAVE_STRCASESTR
301 gchar *strcasestr (const gchar *haystack,
302 const gchar *needle);
303 #endif /* HAVE_STRCASESTR */
304 gchar *strncasestr (const gchar *haystack,
305 gint haystack_len,
306 const gchar *needle);
307 gpointer my_memmem (gconstpointer haystack,
308 size_t haystacklen,
309 gconstpointer needle,
310 size_t needlelen);
311 gchar *strncpy2 (gchar *dest,
312 const gchar *src,
313 size_t n);
315 gboolean is_next_nonascii (const gchar *s);
316 gint get_next_word_len (const gchar *s);
318 /* functions for string parsing */
319 gint subject_compare (const gchar *s1,
320 const gchar *s2);
321 gint subject_compare_for_sort (const gchar *s1,
322 const gchar *s2);
323 void trim_subject (gchar *str);
324 void eliminate_parenthesis (gchar *str,
325 gchar op,
326 gchar cl);
327 void extract_parenthesis (gchar *str,
328 gchar op,
329 gchar cl);
331 void extract_quote (gchar *str,
332 gchar quote_chr);
333 gchar *escape_internal_quotes (gchar *str,
334 gchar quote_chr);
335 void eliminate_address_comment (gchar *str);
336 gchar *strchr_with_skip_quote (const gchar *str,
337 gint quote_chr,
338 gint c);
339 void extract_address (gchar *str);
340 void extract_list_id_str (gchar *str);
342 GSList *address_list_append (GSList *addr_list,
343 const gchar *str);
344 GSList *address_list_append_with_comments(GSList *addr_list,
345 const gchar *str);
346 GSList *references_list_prepend (GSList *msgid_list,
347 const gchar *str);
348 GSList *references_list_append (GSList *msgid_list,
349 const gchar *str);
350 GSList *newsgroup_list_append (GSList *group_list,
351 const gchar *str);
353 GList *add_history (GList *list,
354 const gchar *str);
356 void remove_return (gchar *str);
357 void remove_space (gchar *str);
358 void unfold_line (gchar *str);
359 void subst_char (gchar *str,
360 gchar orig,
361 gchar subst);
362 void subst_chars (gchar *str,
363 gchar *orig,
364 gchar subst);
365 void subst_for_filename (gchar *str);
366 void subst_for_shellsafe_filename (gchar *str);
367 gboolean is_ascii_str (const gchar *str);
368 gint get_quote_level (const gchar *str,
369 const gchar *quote_chars);
370 gint check_line_length (const gchar *str,
371 gint max_chars,
372 gint *line);
374 gchar **strsplit_with_quote (const gchar *str,
375 const gchar *delim,
376 gint max_tokens);
378 gchar *get_abbrev_newsgroup_name (const gchar *group,
379 gint len);
380 gchar *trim_string (const gchar *str,
381 gint len);
383 GList *uri_list_extract_filenames (const gchar *uri_list);
384 gboolean is_uri_string (const gchar *str);
385 gchar *get_uri_path (const gchar *uri);
386 gint get_uri_len (const gchar *str);
387 void decode_uri (gchar *decoded_uri,
388 const gchar *encoded_uri);
389 void decode_uri_with_plus (gchar *decoded_uri,
390 const gchar *encoded_uri,
391 gboolean with_plus);
392 gint scan_mailto_url (const gchar *mailto,
393 gchar **from,
394 gchar **to,
395 gchar **cc,
396 gchar **bcc,
397 gchar **subject,
398 gchar **body,
399 gchar ***attach,
400 gchar **inreplyto);
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 gboolean is_numeric_host_address (const gchar *hostaddress);
418 const gchar *get_desktop_file(void);
419 #ifdef G_OS_WIN32
420 const gchar *w32_get_themes_dir (void);
421 const gchar *w32_get_cert_file (void);
422 #endif
423 /* file / directory handling */
424 off_t get_file_size (const gchar *file);
425 time_t get_file_mtime (const gchar *file);
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 is_file_entry_regular(const gchar *file);
434 #define is_file_exist(file) file_exist(file, FALSE)
435 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
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 gchar *canonicalize_str (const gchar *str);
449 gchar *normalize_newlines (const gchar *str);
451 gchar *get_outgoing_rfc2822_str (FILE *fp);
453 char *fgets_crlf(char *buf, int size, FILE *stream);
455 /* process execution */
456 gint execute_command_line (const gchar *cmdline,
457 gboolean async,
458 const gchar *working_directory);
459 gchar *get_command_output (const gchar *cmdline);
460 FILE *get_command_output_stream (const gchar *cmdline);
462 /* open URI with external browser */
463 gint open_uri(const gchar *uri, const gchar *cmdline);
464 /* open file with text editor */
465 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
467 /* time functions */
468 time_t remote_tzoffset_sec (const gchar *zone);
469 time_t tzoffset_sec (time_t *now);
470 gchar *tzoffset (time_t *now);
471 void get_rfc822_date (gchar *buf,
472 gint len);
473 void get_rfc822_date_hide_tz (gchar *buf,
474 gint len);
476 size_t fast_strftime (gchar *buf,
477 gint buflen,
478 const gchar *format,
479 struct tm *lt);
481 /* debugging */
482 #ifdef HAVE_VA_OPT
483 void debug_print_real (const char *file, int line, const gchar *format, ...) G_GNUC_PRINTF(3, 4);
484 #else
485 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
486 #endif
487 const char * debug_srcname (const char *file);
489 /* subject threading */
490 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
491 void subject_table_insert(GHashTable *subject_table, gchar * subject,
492 void * data);
493 void subject_table_remove(GHashTable *subject_table, gchar * subject);
494 void utils_free_regex(void);
495 gint subject_get_prefix_length (const gchar *subject);
497 /* quoting recognition */
498 const gchar * line_has_quote_char (const gchar *str,
499 const gchar *quote_chars);
501 gint g_int_compare (gconstpointer a, gconstpointer b);
503 gchar *generate_mime_boundary (const gchar *prefix);
505 gint quote_cmd_argument(gchar * result, guint size,
506 const gchar * path);
507 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
509 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
510 void get_hex_str(gchar *out, guchar ch);
512 /* auto pointer for containers that support GType system */
514 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
515 typedef struct AutoPointer GAuto;
516 GType g_auto_pointer_register (void);
517 GAuto *g_auto_pointer_new (gpointer pointer);
518 GAuto *g_auto_pointer_new_with_free (gpointer p,
519 GFreeFunc free);
520 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
521 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
522 void g_auto_pointer_free (GAuto *auto_ptr);
523 gboolean get_uri_part (const gchar *start,
524 const gchar *scanpos,
525 const gchar **bp,
526 const gchar **ep,
527 gboolean hdr);
528 gchar *make_uri_string (const gchar *bp,
529 const gchar *ep);
530 gboolean get_email_part (const gchar *start,
531 const gchar *scanpos,
532 const gchar **bp,
533 const gchar **ep,
534 gboolean hdr);
535 gchar *make_email_string(const gchar *bp,
536 const gchar *ep);
537 gchar *make_http_string (const gchar *bp,
538 const gchar *ep);
540 gchar *mailcap_get_command_for_type(const gchar *type,
541 const gchar *file_to_open);
542 void mailcap_update_default (const gchar *type,
543 const gchar *command);
545 gboolean file_is_email(const gchar *filename);
546 gboolean sc_g_list_bigger(GList *list, gint max);
547 gboolean sc_g_slist_bigger(GSList *list, gint max);
549 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
551 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len);
553 gboolean get_random_bytes(void *buf, size_t count);
555 #ifdef __cplusplus
557 #endif
559 gboolean get_serverportfp_from_filename(const gchar *str, gchar **server, gchar **port, gchar **fp);
561 #ifdef G_OS_WIN32
562 gchar *win32_debug_log_path(void);
563 #endif
565 #endif /* __UTILS_H__ */