annotate src/misc.c @ 1259:3a0bb56adf8e

Fix a major bug in utf8_compare(): when case_sensitive is true, s1_t and s2_t were uninitialized, leading to unpredicable results.
author zas_
date Mon, 26 Jan 2009 08:30:30 +0000
parents 5fe3b8b3a612
children 8b89e3ff286b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1022
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
1 /*
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
2 * Geeqie
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
3 * Copyright (C) 2008 The Geeqie Team
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
4 *
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
5 * Authors: Vladimir Nadvornik / Laurent Monin
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
6 *
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
7 * This software is released under the GNU General Public License (GNU GPL).
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
8 * Please read the included file COPYING for more information.
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
9 * This software comes with no warranty of any kind, use at your own risk!
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
10 */
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
11
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
12 #include "main.h"
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
13 #include "misc.h"
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
14
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
15 gdouble get_zoom_increment(void)
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
16 {
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
17 return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
18 }
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
19
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
20 gchar *utf8_validate_or_convert(const gchar *text)
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
21 {
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
22 gint len;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
23
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
24 if (!text) return NULL;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
25
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
26 len = strlen(text);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
27 if (!g_utf8_validate(text, len, NULL))
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
28 return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
29
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
30 return g_strdup(text);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
31 }
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
32
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
33 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
34 {
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
35 gchar *s1_key, *s2_key;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
36 gchar *s1_t, *s2_t;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
37 gint ret;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
38
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
39 g_assert(g_utf8_validate(s1, -1, NULL));
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
40 g_assert(g_utf8_validate(s2, -1, NULL));
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
41
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
42 if (!case_sensitive)
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
43 {
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
44 s1_t = g_utf8_casefold(s1, -1);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
45 s2_t = g_utf8_casefold(s2, -1);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
46 }
1259
3a0bb56adf8e Fix a major bug in utf8_compare(): when case_sensitive is true, s1_t and s2_t were uninitialized, leading to unpredicable results.
zas_
parents: 1144
diff changeset
47 else
3a0bb56adf8e Fix a major bug in utf8_compare(): when case_sensitive is true, s1_t and s2_t were uninitialized, leading to unpredicable results.
zas_
parents: 1144
diff changeset
48 {
3a0bb56adf8e Fix a major bug in utf8_compare(): when case_sensitive is true, s1_t and s2_t were uninitialized, leading to unpredicable results.
zas_
parents: 1144
diff changeset
49 s1_t = (gchar *) s1;
3a0bb56adf8e Fix a major bug in utf8_compare(): when case_sensitive is true, s1_t and s2_t were uninitialized, leading to unpredicable results.
zas_
parents: 1144
diff changeset
50 s2_t = (gchar *) s2;
3a0bb56adf8e Fix a major bug in utf8_compare(): when case_sensitive is true, s1_t and s2_t were uninitialized, leading to unpredicable results.
zas_
parents: 1144
diff changeset
51 }
1022
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
52
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
53 s1_key = g_utf8_collate_key(s1_t, -1);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
54 s2_key = g_utf8_collate_key(s2_t, -1);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
55
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
56 ret = strcmp(s1_key, s2_key);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
57
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
58 g_free(s1_key);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
59 g_free(s2_key);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
60
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
61 if (!case_sensitive)
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
62 {
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
63 g_free(s1_t);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
64 g_free(s2_t);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
65 }
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
66
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
67 return ret;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
68 }
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
69
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
70 /* Borrowed from gtkfilesystemunix.c */
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
71 gchar *expand_tilde(const gchar *filename)
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
72 {
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
73 #ifndef G_OS_UNIX
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
74 return g_strdup(filename);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
75 #else
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
76 const gchar *notilde;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
77 const gchar *slash;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
78 const gchar *home;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
79
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
80 if (filename[0] != '~')
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
81 return g_strdup(filename);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
82
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
83 notilde = filename + 1;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
84 slash = strchr(notilde, G_DIR_SEPARATOR);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
85 if (slash == notilde || !*notilde)
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
86 {
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
87 home = g_get_home_dir();
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
88 if (!home)
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
89 return g_strdup(filename);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
90 }
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
91 else
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
92 {
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
93 gchar *username;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
94 struct passwd *passwd;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
95
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
96 if (slash)
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
97 username = g_strndup(notilde, slash - notilde);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
98 else
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
99 username = g_strdup(notilde);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
100
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
101 passwd = getpwnam(username);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
102 g_free(username);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
103
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
104 if (!passwd)
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
105 return g_strdup(filename);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
106
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
107 home = passwd->pw_dir;
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
108 }
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
109
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
110 if (slash)
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
111 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
112 else
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
113 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
114 #endif
9962b24b6b43 Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
diff changeset
115 }
1023
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
116
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
117 /*
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
118 returns text without quotes or NULL for empty or broken string
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
119 any text up to first '"' is skipped
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
120 tail is set to point at the char after the second '"'
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
121 or at the ending \0
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
122
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
123 */
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
124
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
125 gchar *quoted_value(const gchar *text, const gchar **tail)
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
126 {
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
127 const gchar *ptr;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
128 gint c = 0;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
129 gint l = strlen(text);
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
130 gchar *retval = NULL;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
131
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
132 if (tail) *tail = text;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
133
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
134 if (l == 0) return retval;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
135
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
136 while (c < l && text[c] != '"') c++;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
137 if (text[c] == '"')
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
138 {
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
139 gint e;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
140 c++;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
141 ptr = text + c;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
142 e = c;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
143 while (e < l)
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
144 {
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
145 if (text[e-1] != '\\' && text[e] == '"') break;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
146 e++;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
147 }
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
148 if (text[e] == '"')
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
149 {
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
150 if (e - c > 0)
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
151 {
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
152 gchar *substring = g_strndup(ptr, e - c);
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
153
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
154 if (substring)
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
155 {
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
156 retval = g_strcompress(substring);
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
157 g_free(substring);
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
158 }
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
159 }
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
160 }
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
161 if (tail) *tail = text + e + 1;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
162 }
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
163 else
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
164 /* for compatibility with older formats (<0.3.7)
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
165 * read a line without quotes too */
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
166 {
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
167 c = 0;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
168 while (c < l && text[c] != '\n' && !g_ascii_isspace(text[c])) c++;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
169 if (c != 0)
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
170 {
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
171 retval = g_strndup(text, c);
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
172 }
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
173 if (tail) *tail = text + c;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
174 }
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
175
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
176 return retval;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
177 }
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
178
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
179 gchar *escquote_value(const gchar *text)
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
180 {
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
181 gchar *e;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
182
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
183 if (!text) return g_strdup("\"\"");
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
184
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
185 e = g_strescape(text, "");
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
186 if (e)
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
187 {
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
188 gchar *retval = g_strdup_printf("\"%s\"", e);
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
189 g_free(e);
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
190 return retval;
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
191 }
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
192 return g_strdup("\"\"");
650c02c0c8ff Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents: 1022
diff changeset
193 }
1144
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
194
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
195 /* Run a command like system() but may output debug messages. */
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
196 int runcmd(gchar *cmd)
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
197 {
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
198 #if 1
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
199 return system(cmd);
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
200 return 0;
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
201 #else
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
202 /* For debugging purposes */
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
203 int retval = -1;
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
204 FILE *in;
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
205
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
206 DEBUG_1("Running command: %s", cmd);
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
207
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
208 in = popen(cmd, "r");
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
209 if (in)
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
210 {
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
211 int status;
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
212 const gchar *msg;
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
213 gchar buf[2048];
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
214
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
215 while (fgets(buf, sizeof(buf), in) != NULL )
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
216 {
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
217 DEBUG_1("Output: %s", buf);
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
218 }
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
219
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
220 status = pclose(in);
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
221
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
222 if (WIFEXITED(status))
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
223 {
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
224 msg = "Command terminated with exit code";
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
225 retval = WEXITSTATUS(status);
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
226 }
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
227 else if (WIFSIGNALED(status))
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
228 {
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
229 msg = "Command was killed by signal";
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
230 retval = WTERMSIG(status);
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
231 }
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
232 else
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
233 {
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
234 msg = "pclose() returned";
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
235 retval = status;
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
236 }
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
237
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
238 DEBUG_1("%s : %d\n", msg, retval);
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
239 }
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
240
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
241 return retval;
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
242 #endif
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
243 }
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
244
5fe3b8b3a612 Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
zas_
parents: 1055
diff changeset
245
1055
1646720364cf Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents: 1023
diff changeset
246 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */