annotate mcabber/mcabber/utils.c @ 2248:377b6a52b25f

Prepare for loudmouth with sha256 support (>=1.5.3)
author franky
date Sat, 13 Feb 2016 11:05:07 +0100
parents 170597f5365b
children 5a107c907e71
rev   line source
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
1 /*
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
2 * utils.c -- Various utility functions
393
f8f3c7493457 Whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 378
diff changeset
3 *
2134
fc7a758ebbde Remove deprecated options comments, update headers
Mikael Berthe <mikael@lilotux.net>
parents: 2133
diff changeset
4 * Copyright (C) 2005-2014 Mikael Berthe <mikael@lilotux.net>
1599
dcd5d4c75199 Update/Add headers
Mikael Berthe <mikael@lilotux.net>
parents: 1598
diff changeset
5 * Some of the ut_* functions are derived from Cabber debug/log code.
1361
0562106d20c1 Update some copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 1292
diff changeset
6 * from_iso8601() comes from the Pidgin (libpurple) project.
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
7 *
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
8 * This program is free software; you can redistribute it and/or modify
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
9 * it under the terms of the GNU General Public License as published by
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or (at
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
11 * your option) any later version.
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
12 *
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
13 * This program is distributed in the hope that it will be useful, but
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
16 * General Public License for more details.
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
17 *
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
18 * You should have received a copy of the GNU General Public License
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
19 * along with this program; if not, write to the Free Software
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
21 * USA
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
22 */
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
23
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
24 #include <config.h>
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
25
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
26 #include <stdio.h>
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
27 #include <stdlib.h>
77
32f54ad6d729 [/trunk] Changeset 91 by mikael
mikael
parents: 76
diff changeset
28 #include <string.h>
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
29 #include <stdarg.h>
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
30 #include <errno.h>
802
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
31
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
32 #ifdef HAVE_LIBIDN
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
33 #include <idna.h>
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
34 #include <stringprep.h>
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
35 static char idnprep[1024];
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
36 #endif
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
37
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
38 #include <glib.h>
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
39 #include <glib/gprintf.h>
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
40
802
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
41 /* For Cygwin (thanks go to Yitzchak Scott-Thoennes) */
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
42 #ifdef __CYGWIN__
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
43 # define timezonevar
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
44 extern long timezone;
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
45 #endif
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
46 #include <time.h>
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
47 #include <unistd.h>
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
48 #include <sys/types.h>
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
49 #include <sys/stat.h>
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
50 #include <ctype.h>
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
51
977
5b01de4ac5e1 Cosmetic changes
Alexis Hildebrandt <afh [at] 2drop [dot] net>
parents: 952
diff changeset
52 #include "utils.h"
378
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
53 #include "logprint.h"
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
54 #include "settings.h"
1897
efd7c4c34ff2 Display mcabber version when starting a trace log
Mikael Berthe <mikael@lilotux.net>
parents: 1859
diff changeset
55 #include "main.h"
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
56
76
a8f8492abd44 [/trunk] Changeset 90 by mikael
mikael
parents: 24
diff changeset
57 static int DebugEnabled;
77
32f54ad6d729 [/trunk] Changeset 91 by mikael
mikael
parents: 76
diff changeset
58 static char *FName;
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
59
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
60 // jidtodisp(jid)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
61 // Strips the resource part from the jid
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
62 // The caller should g_free the result after use.
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
63 char *jidtodisp(const char *fjid)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
64 {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
65 char *ptr;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
66 char *alias;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
67
1910
3695266e16c7 Fix error presence packet handling.
Mikael Berthe <mikael@lilotux.net>
parents: 1897
diff changeset
68 if (!fjid) {
3695266e16c7 Fix error presence packet handling.
Mikael Berthe <mikael@lilotux.net>
parents: 1897
diff changeset
69 scr_LogPrint(LPRINT_LOGNORM, "** jidtodisp: NULL JID, "
3695266e16c7 Fix error presence packet handling.
Mikael Berthe <mikael@lilotux.net>
parents: 1897
diff changeset
70 "this is probably a bug, please report!");
3695266e16c7 Fix error presence packet handling.
Mikael Berthe <mikael@lilotux.net>
parents: 1897
diff changeset
71 return NULL;
3695266e16c7 Fix error presence packet handling.
Mikael Berthe <mikael@lilotux.net>
parents: 1897
diff changeset
72 }
3695266e16c7 Fix error presence packet handling.
Mikael Berthe <mikael@lilotux.net>
parents: 1897
diff changeset
73
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
74 alias = g_strdup(fjid);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
75
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
76 if ((ptr = strchr(alias, JID_RESOURCE_SEPARATOR)) != NULL) {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
77 *ptr = 0;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
78 }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
79 return alias;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
80 }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
81
2111
9023a6f2bf6c Misc. style updates
Mikael Berthe <mikael@lilotux.net>
parents: 2103
diff changeset
82 // The caller must free the string after use.
1611
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
83 char *jid_get_username(const char *fjid)
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
84 {
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
85 char *ptr;
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
86 char *username;
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
87
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
88 username = g_strdup(fjid);
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
89 if ((ptr = strchr(username, JID_DOMAIN_SEPARATOR)) != NULL) {
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
90 *ptr = 0;
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
91 }
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
92 return username;
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
93 }
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1599
diff changeset
94
2111
9023a6f2bf6c Misc. style updates
Mikael Berthe <mikael@lilotux.net>
parents: 2103
diff changeset
95 // The caller must free the string after use.
2103
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
96 char *get_servername(const char *username, const char *servername)
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
97 {
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
98 char *ptr;
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
99 char *server;
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
100
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
101 if ((ptr = strchr(username, JID_DOMAIN_SEPARATOR)) != NULL) {
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
102 server = g_strdup(ptr+1);
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
103 return server;
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
104 }
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
105
2111
9023a6f2bf6c Misc. style updates
Mikael Berthe <mikael@lilotux.net>
parents: 2103
diff changeset
106 return g_strdup(servername);
2103
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
107 }
1210a22726d3 We can determine if the server supports carbons. By means of XEP-0030: Service
Roeland Jago Douma <roeland@famdouma.nl>
parents: 2050
diff changeset
108
2111
9023a6f2bf6c Misc. style updates
Mikael Berthe <mikael@lilotux.net>
parents: 2103
diff changeset
109 // The caller must free the string after use.
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
110 char *compose_jid(const char *username, const char *servername,
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
111 const char *resource)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
112 {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
113 char *fjid;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
114
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
115 if (!strchr(username, JID_DOMAIN_SEPARATOR)) {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
116 fjid = g_strdup_printf("%s%c%s%c%s", username,
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
117 JID_DOMAIN_SEPARATOR, servername,
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
118 JID_RESOURCE_SEPARATOR, resource);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
119 } else {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
120 fjid = g_strdup_printf("%s%c%s", username,
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
121 JID_RESOURCE_SEPARATOR, resource);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
122 }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
123 return fjid;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
124 }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
125
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
126 gboolean jid_equal(const char *jid1, const char *jid2)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
127 {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
128 char *a,*b;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
129 int ret;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
130 if (!jid1 && !jid2)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
131 return TRUE;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
132 if (!jid1 || !jid2)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
133 return FALSE;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
134
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
135 a = jidtodisp(jid1);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
136 b = jidtodisp(jid2);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
137 ret = strcasecmp(a, b);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
138 g_free(a);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
139 g_free(b);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
140 return (ret == 0) ? TRUE : FALSE;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
141 }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
142
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
143 // expand_filename(filename)
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
144 // Expand "~/" with the $HOME env. variable in a file name.
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
145 // The caller must free the string after use.
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
146 char *expand_filename(const char *fname)
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
147 {
1222
75aee46d3aee Fix a problem with the ssl_ca{file,path} variables expansion
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
148 if (!fname)
75aee46d3aee Fix a problem with the ssl_ca{file,path} variables expansion
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
149 return NULL;
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
150 if (!strncmp(fname, "~/", 2)) {
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
151 char *homedir = getenv("HOME");
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
152 if (homedir)
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
153 return g_strdup_printf("%s%s", homedir, fname+1);
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
154 }
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
155 return g_strdup(fname);
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
156 }
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
157
2248
377b6a52b25f Prepare for loudmouth with sha256 support (>=1.5.3)
franky
parents: 2203
diff changeset
158 #ifndef LOUDMOUTH_USES_SHA256
2203
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
159 // fingerprint_to_hex(fprstr, hex, fpr_len)
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
160 // Convert the binary fingerprint fprstr (which is fpr_len bytes long)
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
161 // to a NULL-terminated hexadecimal string hex.
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
162 // The destination array hex should have been preallocated by the caller,
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
163 // and should be big enough (i.e. >= 3*fpr_len bytes).
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
164 void fingerprint_to_hex(const char *fprstr, char *hex, size_t fpr_len)
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
165 {
2203
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
166 unsigned int i;
2198
1591518a33b9 Minor change of the prototype for fingerprint_to_hex()
Mikael Berthe <mikael@lilotux.net>
parents: 2197
diff changeset
167 const unsigned char *fpr = (const unsigned char *)fprstr;
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
168 char *p;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
169
2197
189abf03ef24 Fix fingerprint management
Mikael Berthe <mikael@lilotux.net>
parents: 2168
diff changeset
170 hex[0] = 0;
2203
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
171 if (!fpr || fpr_len < 16) return;
2197
189abf03ef24 Fix fingerprint management
Mikael Berthe <mikael@lilotux.net>
parents: 2168
diff changeset
172
2203
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
173 for (p = hex, i = 0; i < fpr_len - 1; i++, p+=3)
2197
189abf03ef24 Fix fingerprint management
Mikael Berthe <mikael@lilotux.net>
parents: 2168
diff changeset
174 g_snprintf(p, 4, "%02X:", fpr[i]);
189abf03ef24 Fix fingerprint management
Mikael Berthe <mikael@lilotux.net>
parents: 2168
diff changeset
175 g_snprintf(p, 3, "%02X", fpr[i]);
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
176 }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
177
2203
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
178 // hex_to_fingerprint(hex, fpr, fpr_len)
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
179 // Convert the hexadecimal fingerprint hex to a byte array fpr[].
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
180 // The fpr array should have been preallocated with a size >= fpr_len.
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
181 gboolean hex_to_fingerprint(const char *hex, char *fpr, size_t fpr_len)
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
182 {
2203
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
183 unsigned int i;
2197
189abf03ef24 Fix fingerprint management
Mikael Berthe <mikael@lilotux.net>
parents: 2168
diff changeset
184 const char *p;
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
185
2203
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
186 if (fpr_len < 16) return FALSE;
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
187
2197
189abf03ef24 Fix fingerprint management
Mikael Berthe <mikael@lilotux.net>
parents: 2168
diff changeset
188 fpr[0] = 0;
2203
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
189
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
190 if (strlen(hex) != fpr_len*3 - 1)
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
191 return FALSE;
2203
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
192
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
193 for (i = 0, p = hex; i < fpr_len && *p && *(p+1); i++, p += 3) {
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
194 // Check we have two hex digits followed by a colon (or end of string)
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
195 if (!isxdigit(*p) || !isxdigit(*(p+1)))
2197
189abf03ef24 Fix fingerprint management
Mikael Berthe <mikael@lilotux.net>
parents: 2168
diff changeset
196 return FALSE;
2203
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
197 if (*(p+2) && (*(p+2) != ':'))
170597f5365b Use more generic routines to convert fingerprints to/from hexadecimal
Mikael Berthe <mikael@lilotux.net>
parents: 2198
diff changeset
198 return FALSE;
2197
189abf03ef24 Fix fingerprint management
Mikael Berthe <mikael@lilotux.net>
parents: 2168
diff changeset
199 fpr[i] = (char)g_ascii_strtoull(p, NULL, 16);
189abf03ef24 Fix fingerprint management
Mikael Berthe <mikael@lilotux.net>
parents: 2168
diff changeset
200 }
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
201 return TRUE;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
202 }
2248
377b6a52b25f Prepare for loudmouth with sha256 support (>=1.5.3)
franky
parents: 2203
diff changeset
203 #endif
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1568
diff changeset
204
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
205 static gboolean tracelog_create(void)
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
206 {
77
32f54ad6d729 [/trunk] Changeset 91 by mikael
mikael
parents: 76
diff changeset
207 FILE *fp;
428
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
208 struct stat buf;
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
209 int err;
1897
efd7c4c34ff2 Display mcabber version when starting a trace log
Mikael Berthe <mikael@lilotux.net>
parents: 1859
diff changeset
210 char *v;
77
32f54ad6d729 [/trunk] Changeset 91 by mikael
mikael
parents: 76
diff changeset
211
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
212 fp = fopen(FName, "a");
378
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
213 if (!fp) {
2135
361603828d9e Set the trace log file permissions again when we write to it
Mikael Berthe <mikael@lilotux.net>
parents: 2134
diff changeset
214 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file: %s",
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
215 strerror(errno));
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
216 return FALSE;
378
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
217 }
428
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
218
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
219 err = fstat(fileno(fp), &buf);
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
220 if (err || buf.st_uid != geteuid()) {
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
221 fclose(fp);
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
222 if (err)
2135
361603828d9e Set the trace log file permissions again when we write to it
Mikael Berthe <mikael@lilotux.net>
parents: 2134
diff changeset
223 scr_LogPrint(LPRINT_NORMAL, "ERROR: cannot stat the tracelog file: %s",
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
224 strerror(errno));
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
225 else
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
226 scr_LogPrint(LPRINT_NORMAL, "ERROR: tracelog file does not belong to you!");
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
227 return FALSE;
428
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
228 }
2135
361603828d9e Set the trace log file permissions again when we write to it
Mikael Berthe <mikael@lilotux.net>
parents: 2134
diff changeset
229
361603828d9e Set the trace log file permissions again when we write to it
Mikael Berthe <mikael@lilotux.net>
parents: 2134
diff changeset
230 if (fchmod(fileno(fp), S_IRUSR|S_IWUSR)) {
361603828d9e Set the trace log file permissions again when we write to it
Mikael Berthe <mikael@lilotux.net>
parents: 2134
diff changeset
231 scr_LogPrint(LPRINT_NORMAL, "WARNING: Cannot set tracelog file permissions: %s",
361603828d9e Set the trace log file permissions again when we write to it
Mikael Berthe <mikael@lilotux.net>
parents: 2134
diff changeset
232 strerror(errno));
361603828d9e Set the trace log file permissions again when we write to it
Mikael Berthe <mikael@lilotux.net>
parents: 2134
diff changeset
233 }
428
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
234
1897
efd7c4c34ff2 Display mcabber version when starting a trace log
Mikael Berthe <mikael@lilotux.net>
parents: 1859
diff changeset
235 v = mcabber_version();
efd7c4c34ff2 Display mcabber version when starting a trace log
Mikael Berthe <mikael@lilotux.net>
parents: 1859
diff changeset
236 fprintf(fp, "New trace log started. MCabber version %s\n"
efd7c4c34ff2 Display mcabber version when starting a trace log
Mikael Berthe <mikael@lilotux.net>
parents: 1859
diff changeset
237 "----------------------\n", v);
efd7c4c34ff2 Display mcabber version when starting a trace log
Mikael Berthe <mikael@lilotux.net>
parents: 1859
diff changeset
238 g_free(v);
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
239 fclose(fp);
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
240 return TRUE;
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
241 }
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
242
2111
9023a6f2bf6c Misc. style updates
Mikael Berthe <mikael@lilotux.net>
parents: 2103
diff changeset
243 // The caller must free the string after use.
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
244 static gchar *tracelog_level_guard(const gchar *key, const gchar *new_value)
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
245 {
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
246 int new_level = 0;
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
247 if (new_value)
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
248 new_level = atoi(new_value);
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
249 if (DebugEnabled < 1 && new_level > 0 && FName && !tracelog_create())
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
250 DebugEnabled = 0;
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
251 else
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
252 DebugEnabled = new_level;
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
253 return g_strdup(new_value);
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
254 }
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
255
2111
9023a6f2bf6c Misc. style updates
Mikael Berthe <mikael@lilotux.net>
parents: 2103
diff changeset
256 // The caller must free the string after use.
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
257 static gchar *tracelog_file_guard(const gchar *key, const gchar *new_value)
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
258 {
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
259 gchar *new_fname = NULL;
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
260
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
261 if (new_value)
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
262 new_fname = expand_filename(new_value);
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
263
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
264 if (g_strcmp0(FName, new_fname)) {
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
265 g_free(FName);
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
266 FName = new_fname;
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
267 if (DebugEnabled > 0 && !tracelog_create()) {
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
268 g_free(FName);
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
269 FName = NULL;
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
270 }
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
271 } else
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
272 g_free(new_fname);
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
273
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
274 return g_strdup(new_value);
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
275 }
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
276
1780
e4378fbab5d7 Major API cleanup - bump API to 4
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
277 // ut_init_debug()
2111
9023a6f2bf6c Misc. style updates
Mikael Berthe <mikael@lilotux.net>
parents: 2103
diff changeset
278 // Install option guards before initial config file parsing.
1780
e4378fbab5d7 Major API cleanup - bump API to 4
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
279 void ut_init_debug(void)
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
280 {
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
281 DebugEnabled = 0;
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
282 FName = NULL;
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
283 settings_set_guard("tracelog_level", tracelog_level_guard);
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
284 settings_set_guard("tracelog_file", tracelog_file_guard);
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
285 }
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
286
1780
e4378fbab5d7 Major API cleanup - bump API to 4
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
287 void ut_write_log(unsigned int flag, const char *data)
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
288 {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
289 if (!DebugEnabled || !FName) return;
393
f8f3c7493457 Whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 378
diff changeset
290
1617
9ca672ee884f Fix previous commit :)
Mikael Berthe <mikael@lilotux.net>
parents: 1611
diff changeset
291 if (((DebugEnabled >= 2) && (flag & (LPRINT_LOG|LPRINT_DEBUG))) ||
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
292 ((DebugEnabled == 1) && (flag & LPRINT_LOG))) {
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
293 FILE *fp = fopen(FName, "a+");
378
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
294 if (!fp) {
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
295 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file: %s.",
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
296 strerror(errno));
378
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
297 return;
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
298 }
2135
361603828d9e Set the trace log file permissions again when we write to it
Mikael Berthe <mikael@lilotux.net>
parents: 2134
diff changeset
299
361603828d9e Set the trace log file permissions again when we write to it
Mikael Berthe <mikael@lilotux.net>
parents: 2134
diff changeset
300 // Check file permissions again (it could be a new file)
361603828d9e Set the trace log file permissions again when we write to it
Mikael Berthe <mikael@lilotux.net>
parents: 2134
diff changeset
301 fchmod(fileno(fp), S_IRUSR|S_IWUSR);
361603828d9e Set the trace log file permissions again when we write to it
Mikael Berthe <mikael@lilotux.net>
parents: 2134
diff changeset
302
1531
9f92c0edde1c Display a warning when writing to the tracelog file fails
Mikael Berthe <mikael@lilotux.net>
parents: 1460
diff changeset
303 if (fputs(data, fp) == EOF)
1674
f02e7076ccec Use option guards for tracelog
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
304 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot write to tracelog file.");
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
305 fclose(fp);
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
306 }
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
307 }
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
308
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
309 // checkset_perm(name, setmode)
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
310 // Check the permissions of the "name" file/dir
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
311 // If setmode is true, correct the permissions if they are wrong
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
312 // Return values: -1 == bad file/dir, 0 == success, 1 == cannot correct
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
313 int checkset_perm(const char *name, unsigned int setmode)
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
314 {
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
315 int fd;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
316 struct stat buf;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
317
816
a6628f0aabc1 Skip file permission checks on Cygwin
Mikael Berthe <mikael@lilotux.net>
parents: 803
diff changeset
318 #ifdef __CYGWIN__
1460
af54d33facd0 Fix two comments
Mikael Berthe <mikael@lilotux.net>
parents: 1459
diff changeset
319 // Permission checking isn't efficient on Cygwin
816
a6628f0aabc1 Skip file permission checks on Cygwin
Mikael Berthe <mikael@lilotux.net>
parents: 803
diff changeset
320 return 0;
a6628f0aabc1 Skip file permission checks on Cygwin
Mikael Berthe <mikael@lilotux.net>
parents: 803
diff changeset
321 #endif
a6628f0aabc1 Skip file permission checks on Cygwin
Mikael Berthe <mikael@lilotux.net>
parents: 803
diff changeset
322
1459
d76eff4ed71d Use stat() to check permissions in checkset_perm() (Dave Holland)
Mikael Berthe <mikael@lilotux.net>
parents: 1458
diff changeset
323 fd = stat(name, &buf);
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
324 if (fd == -1) return -1;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
325
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
326 if (buf.st_uid != geteuid()) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
327 scr_LogPrint(LPRINT_LOGNORM, "Wrong file owner [%s]", name);
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
328 return 1;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
329 }
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
330
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
331 if (buf.st_mode & (S_IRGRP | S_IWGRP | S_IXGRP) ||
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
332 buf.st_mode & (S_IROTH | S_IWOTH | S_IXOTH)) {
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
333 if (setmode) {
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
334 mode_t newmode = 0;
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
335 scr_LogPrint(LPRINT_LOGNORM, "Bad permissions [%s]", name);
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
336 if (S_ISDIR(buf.st_mode))
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
337 newmode |= S_IXUSR;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
338 newmode |= S_IRUSR | S_IWUSR;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
339 if (chmod(name, newmode)) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
340 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Failed to correct permissions!");
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
341 return 1;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
342 }
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
343 scr_LogPrint(LPRINT_LOGNORM, "Permissions have been corrected");
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
344 } else {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
345 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Bad permissions [%s]", name);
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
346 return 1;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
347 }
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
348 }
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
349
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
350 return 0;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
351 }
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
352
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
353 const char *ut_get_tmpdir(void)
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
354 {
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
355 static const char *tmpdir;
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
356 const char *tmpvars[] = { "MCABBERTMPDIR", "TMP", "TMPDIR", "TEMP" };
735
2f027806cd48 Some code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 727
diff changeset
357 unsigned int i;
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
358
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
359 if (tmpdir)
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
360 return tmpdir;
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
361
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
362 for (i = 0; i < (sizeof(tmpvars) / sizeof(const char *)); i++) {
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
363 tmpdir = getenv(tmpvars[i]);
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
364 if (tmpdir && tmpdir[0] && tmpdir[0] == '/' && tmpdir[1]) {
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
365 // Looks ok.
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
366 return tmpdir;
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
367 }
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
368 }
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
369
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
370 // Default temporary directory
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
371 tmpdir = "/tmp";
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
372 return tmpdir;
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
373 }
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
374
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
375 // to_iso8601(dststr, timestamp)
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
376 // Convert timestamp to iso8601 format, and store it in dststr.
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
377 // NOTE: dststr should be at last 19 chars long.
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
378 // Return should be 0
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
379 int to_iso8601(char *dststr, time_t timestamp)
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
380 {
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
381 struct tm *tm_time;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
382 int ret;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
383
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
384 tm_time = gmtime(&timestamp);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
385
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
386 ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ",
1458
17e95be6c39b time_t fix from the MirBSD port (Thorsten Glaser)
Mikael Berthe <mikael@lilotux.net>
parents: 1452
diff changeset
387 (int)(1900+tm_time->tm_year), tm_time->tm_mon+1, tm_time->tm_mday,
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
388 tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
389
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
390 return ((ret == -1) ? -1 : 0);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
391 }
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
392
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
393 // from_iso8601(timestamp, utc)
1361
0562106d20c1 Update some copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 1292
diff changeset
394 // This function came from the Pidgin project, gaim_str_to_time().
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
395 // (Actually date may not be pure iso-8601)
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
396 // Thanks, guys!
802
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
397 // ** Modified by somian 10 Apr 2006 with advice from ysth.
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
398 time_t from_iso8601(const char *timestamp, int utc)
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
399 {
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
400 struct tm t;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
401 time_t retval = 0;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
402 char buf[32];
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
403 char *c;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
404 int tzoff = 0;
802
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
405 int hms_succ = 0;
1458
17e95be6c39b time_t fix from the MirBSD port (Thorsten Glaser)
Mikael Berthe <mikael@lilotux.net>
parents: 1452
diff changeset
406 int tmpyear;
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
407
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
408 time(&retval);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
409 localtime_r(&retval, &t);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
410
463
339e85418b49 from_iso8601(): set time to 00:00:00 if not specified
Mikael Berthe <mikael@lilotux.net>
parents: 456
diff changeset
411 /* Reset time to midnight (00:00:00) */
339e85418b49 from_iso8601(): set time to 00:00:00 if not specified
Mikael Berthe <mikael@lilotux.net>
parents: 456
diff changeset
412 t.tm_hour = t.tm_min = t.tm_sec = 0;
339e85418b49 from_iso8601(): set time to 00:00:00 if not specified
Mikael Berthe <mikael@lilotux.net>
parents: 456
diff changeset
413
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
414 snprintf(buf, sizeof(buf), "%s", timestamp);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
415 c = buf;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
416
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
417 /* 4 digit year */
1458
17e95be6c39b time_t fix from the MirBSD port (Thorsten Glaser)
Mikael Berthe <mikael@lilotux.net>
parents: 1452
diff changeset
418 if (!sscanf(c, "%04d", &tmpyear)) return 0;
17e95be6c39b time_t fix from the MirBSD port (Thorsten Glaser)
Mikael Berthe <mikael@lilotux.net>
parents: 1452
diff changeset
419 t.tm_year = tmpyear;
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
420 c+=4;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
421 if (*c == '-')
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
422 c++;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
423
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
424 t.tm_year -= 1900;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
425
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
426 /* 2 digit month */
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
427 if (!sscanf(c, "%02d", &t.tm_mon)) return 0;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
428 c+=2;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
429 if (*c == '-')
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
430 c++;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
431
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
432 t.tm_mon -= 1;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
433
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
434 /* 2 digit day */
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
435 if (!sscanf(c, "%02d", &t.tm_mday)) return 0;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
436 c+=2;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
437 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
438 c++; /* skip the "T" */
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
439
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
440 /* 2 digit hour */
802
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
441 if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3)
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
442 {
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
443 hms_succ = 1;
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
444 c += 8;
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
445 }
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
446 else if (sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3)
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
447 {
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
448 hms_succ = 1;
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
449 c += 6;
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
450 }
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
451
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
452 if (hms_succ) {
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
453 int tzhrs, tzmins;
802
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
454
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
455 if (*c == '.') /* dealing with precision we don't care about */
2168
87244845fd9c Handle fractional seconds with more (or less) than 3 digits in timestamps
Mikael Berthe <mikael@lilotux.net>
parents: 2135
diff changeset
456 while (isdigit(*++c))
87244845fd9c Handle fractional seconds with more (or less) than 3 digits in timestamps
Mikael Berthe <mikael@lilotux.net>
parents: 2135
diff changeset
457 ;
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
458
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
459 if ((*c == '+' || *c == '-') &&
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
460 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) {
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
461 tzoff = tzhrs*60*60 + tzmins*60;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
462 if (*c == '+')
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
463 tzoff *= -1;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
464 }
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
465
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
466 if (tzoff || utc) {
802
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
467 #ifdef HAVE_TM_GMTOFF
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
468 tzoff += t.tm_gmtoff;
802
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
469 #else
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
470 # ifdef HAVE_TIMEZONE
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
471 tzset(); /* making sure */
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
472 tzoff -= timezone;
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
473 # endif
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
474 #endif
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
475 }
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
476 }
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
477 }
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
478
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
479 t.tm_isdst = -1;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
480
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
481 retval = mktime(&t);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
482
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
483 retval += tzoff;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
484
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
485 return retval;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
486 }
419
2f9852610cf4 Small code review
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
487
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
488 /**
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
489 * Derived from libjabber/jid.c, because the libjabber version is not
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
490 * really convenient for our usage.
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
491 *
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
492 * Check if the full JID is valid
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
493 * Return 0 if it is valid, non zero otherwise
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
494 */
1292
382ec54b584e Muc nick coloring functionality
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1241
diff changeset
495 int check_jid_syntax(const char *fjid)
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
496 {
1292
382ec54b584e Muc nick coloring functionality
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1241
diff changeset
497 const char *str;
382ec54b584e Muc nick coloring functionality
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1241
diff changeset
498 const char *domain, *resource;
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
499 int domlen;
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
500 #ifdef HAVE_LIBIDN
1641
c3c7d6d0348f Remove (probably) unnecessary check
Mikael Berthe <mikael@lilotux.net>
parents: 1640
diff changeset
501 char *idnpp;
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
502 int r;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
503 #endif
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
504
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 979
diff changeset
505 if (!fjid) return 1;
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
506
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 979
diff changeset
507 domain = strchr(fjid, JID_DOMAIN_SEPARATOR);
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
508
456
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
509 /* the username is optional */
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
510 if (!domain) {
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 979
diff changeset
511 domain = fjid;
456
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
512 } else {
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
513 /* node identifiers may not be longer than 1023 bytes */
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 979
diff changeset
514 if ((domain == fjid) || (domain-fjid > 1023))
456
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
515 return 1;
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
516 domain++;
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
517
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
518 #ifdef HAVE_LIBIDN
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
519 idnpp = idnprep;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
520 str = fjid;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
521 while (*str != JID_DOMAIN_SEPARATOR)
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
522 *idnpp++ = *str++;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
523 *idnpp = 0;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
524
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
525 r = stringprep(idnprep, 1023, 0, stringprep_xmpp_nodeprep);
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
526 if (r != STRINGPREP_OK || !idnprep[0])
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
527 return 1;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
528 /* the username looks okay */
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
529 #else
456
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
530 /* check for low and invalid ascii characters in the username */
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 979
diff changeset
531 for (str = fjid; *str != JID_DOMAIN_SEPARATOR; str++) {
977
5b01de4ac5e1 Cosmetic changes
Alexis Hildebrandt <afh [at] 2drop [dot] net>
parents: 952
diff changeset
532 if (*str <= ' ' || *str == ':' || *str == JID_DOMAIN_SEPARATOR ||
456
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
533 *str == '<' || *str == '>' || *str == '\'' ||
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
534 *str == '"' || *str == '&') {
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
535 return 1;
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
536 }
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
537 }
456
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
538 /* the username is okay as far as we can tell without LIBIDN */
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
539 #endif
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
540 }
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
541
977
5b01de4ac5e1 Cosmetic changes
Alexis Hildebrandt <afh [at] 2drop [dot] net>
parents: 952
diff changeset
542 resource = strchr(domain, JID_RESOURCE_SEPARATOR);
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
543
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
544 /* the resource is optional */
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
545 if (resource) {
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
546 domlen = resource - domain;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
547 resource++;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
548 /* resources may not be longer than 1023 bytes */
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
549 if ((*resource == '\0') || strlen(resource) > 1023)
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
550 return 1;
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
551 #ifdef HAVE_LIBIDN
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
552 strncpy(idnprep, resource, sizeof(idnprep));
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
553 r = stringprep(idnprep, 1023, 0, stringprep_xmpp_resourceprep);
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
554 if (r != STRINGPREP_OK || !idnprep[0])
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
555 return 1;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
556 #endif
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
557 } else {
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
558 domlen = strlen(domain);
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
559 }
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
560
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
561 /* there must be a domain identifier */
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
562 if (domlen == 0) return 1;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
563
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
564 /* and it must not be longer than 1023 bytes */
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
565 if (domlen > 1023) return 1;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
566
1859
a20701a2840c "." is not a valid Jabber domain name
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
567 /* /.+/ is not a valid domain name pattern */
a20701a2840c "." is not a valid Jabber domain name
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
568 for (str = domain; *str && *str != JID_RESOURCE_SEPARATOR; str++)
a20701a2840c "." is not a valid Jabber domain name
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
569 if (*str != '.') break;
a20701a2840c "." is not a valid Jabber domain name
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
570 if (!*str || *str == JID_RESOURCE_SEPARATOR)
a20701a2840c "." is not a valid Jabber domain name
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
571 return 1; /* domain contains only dots */
a20701a2840c "." is not a valid Jabber domain name
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
572
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
573 #ifdef HAVE_LIBIDN
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
574 idnpp = idnprep;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
575 str = domain;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
576 while (*str != '\0' && *str != JID_RESOURCE_SEPARATOR)
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
577 *idnpp++ = *str++;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
578 *idnpp = 0;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
579
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
580 r = stringprep_nameprep(idnprep, 1023);
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
581 if (r != STRINGPREP_OK || !idnprep[0])
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
582 return 1;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
583
1641
c3c7d6d0348f Remove (probably) unnecessary check
Mikael Berthe <mikael@lilotux.net>
parents: 1640
diff changeset
584 if (idna_to_ascii_8z(idnprep, &idnpp, IDNA_USE_STD3_ASCII_RULES) !=
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
585 IDNA_SUCCESS)
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
586 return 1;
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
587 else
1641
c3c7d6d0348f Remove (probably) unnecessary check
Mikael Berthe <mikael@lilotux.net>
parents: 1640
diff changeset
588 free(idnpp);
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
589 #else
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
590 /* make sure the hostname is valid characters */
977
5b01de4ac5e1 Cosmetic changes
Alexis Hildebrandt <afh [at] 2drop [dot] net>
parents: 952
diff changeset
591 for (str = domain; *str != '\0' && *str != JID_RESOURCE_SEPARATOR; str++) {
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
592 if (!(isalnum(*str) || *str == '.' || *str == '-' || *str == '_'))
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
593 return 1;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
594 }
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
595 #endif
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
596
1639
d9913c1b35e7 Add support for libidn
Mikael Berthe <mikael@lilotux.net>
parents: 1617
diff changeset
597 /* it's okay as far as we can tell */
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
598 return 0;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
599 }
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
600
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
601
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
602 inline void mc_strtolower(char *str)
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
603 {
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
604 if (!str) return;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
605 for ( ; *str; str++)
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
606 *str = tolower(*str);
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
607 }
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
608
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
609 // strip_arg_special_chars(string)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
610 // Remove quotes and backslashes before an escaped quote
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
611 // Only quotes need a backslash
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
612 // Ex.: ["a b"] -> [a b]; [a\"b] -> [a"b]
554
2424bbf0a6db Some more work on do_room()
Mikael Berthe <mikael@lilotux.net>
parents: 553
diff changeset
613 void strip_arg_special_chars(char *s)
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
614 {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
615 int instring = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
616 int escape = FALSE;
2133
69d00a118c0c Fix ANSI C-incompatible change
Mikael Berthe <mikael@lilotux.net>
parents: 2119
diff changeset
617 char *p, *t;
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
618
979
ea939ff047d8 Improve /buffer search_*
Mikael Berthe <mikael@lilotux.net>
parents: 977
diff changeset
619 if (!s) return;
ea939ff047d8 Improve /buffer search_*
Mikael Berthe <mikael@lilotux.net>
parents: 977
diff changeset
620
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
621 for (p = s; *p; p++) {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
622 if (*p == '"') {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
623 if (!escape) {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
624 instring = !instring;
2119
3b26f3a58cb9 Fix command line issue reported by Minos
Mikael Berthe <mikael@lilotux.net>
parents: 2111
diff changeset
625 //memmove(p, p+1, strlen(p));
2133
69d00a118c0c Fix ANSI C-incompatible change
Mikael Berthe <mikael@lilotux.net>
parents: 2119
diff changeset
626 for (t=p; *t; t++)
2119
3b26f3a58cb9 Fix command line issue reported by Minos
Mikael Berthe <mikael@lilotux.net>
parents: 2111
diff changeset
627 *t = *(t+1);
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
628 p--;
1452
83e275dca409 Fix escaping bug in strip_arg_special_chars()
Mikael Berthe <mikael@lilotux.net>
parents: 1440
diff changeset
629 } else
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
630 escape = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
631 } else if (*p == '\\') {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
632 if (!escape) {
2119
3b26f3a58cb9 Fix command line issue reported by Minos
Mikael Berthe <mikael@lilotux.net>
parents: 2111
diff changeset
633 //memmove(p, p+1, strlen(p));
2133
69d00a118c0c Fix ANSI C-incompatible change
Mikael Berthe <mikael@lilotux.net>
parents: 2119
diff changeset
634 for (t=p; *t; t++)
2119
3b26f3a58cb9 Fix command line issue reported by Minos
Mikael Berthe <mikael@lilotux.net>
parents: 2111
diff changeset
635 *t = *(t+1);
1452
83e275dca409 Fix escaping bug in strip_arg_special_chars()
Mikael Berthe <mikael@lilotux.net>
parents: 1440
diff changeset
636 p--;
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
637 }
1452
83e275dca409 Fix escaping bug in strip_arg_special_chars()
Mikael Berthe <mikael@lilotux.net>
parents: 1440
diff changeset
638 escape = !escape;
83e275dca409 Fix escaping bug in strip_arg_special_chars()
Mikael Berthe <mikael@lilotux.net>
parents: 1440
diff changeset
639 } else
83e275dca409 Fix escaping bug in strip_arg_special_chars()
Mikael Berthe <mikael@lilotux.net>
parents: 1440
diff changeset
640 escape = FALSE;
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
641 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
642 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
643
554
2424bbf0a6db Some more work on do_room()
Mikael Berthe <mikael@lilotux.net>
parents: 553
diff changeset
644 // split_arg(arg, n, preservelast)
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
645 // Split the string arg into a maximum of n pieces, taking care of
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
646 // double quotes.
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
647 // Return a null-terminated array of strings. This array should be freed
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
648 // by the caller after use, for example with free_arg_lst().
559
4eb579399613 Fix a small bug in split_arg()
Mikael Berthe <mikael@lilotux.net>
parents: 556
diff changeset
649 // If dontstriplast is true, the Nth argument isn't stripped (i.e. no
4eb579399613 Fix a small bug in split_arg()
Mikael Berthe <mikael@lilotux.net>
parents: 556
diff changeset
650 // processing of quote chars)
554
2424bbf0a6db Some more work on do_room()
Mikael Berthe <mikael@lilotux.net>
parents: 553
diff changeset
651 char **split_arg(const char *arg, unsigned int n, int dontstriplast)
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
652 {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
653 char **arglst;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
654 const char *p, *start, *end;
735
2f027806cd48 Some code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 727
diff changeset
655 unsigned int i = 0;
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
656 int instring = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
657 int escape = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
658
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
659 arglst = g_new0(char*, n+1);
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
660
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
661 if (!arg || !n) return arglst;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
662
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
663 // Skip leading space
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
664 for (start = arg; *start && *start == ' '; start++) ;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
665 // End of string pointer
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
666 for (end = start; *end; end++) ;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
667 // Skip trailing space
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
668 while (end > start+1 && *(end-1) == ' ')
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
669 end--;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
670
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
671 for (p = start; p < end; p++) {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
672 if (*p == '"' && !escape)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
673 instring = !instring;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
674 if (*p == '\\' && !escape)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
675 escape = TRUE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
676 else if (escape)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
677 escape = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
678 if (*p == ' ' && !instring && i+1 < n) {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
679 // end of parameter
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
680 *(arglst+i) = g_strndup(start, p-start);
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
681 strip_arg_special_chars(*(arglst+i));
556
c4fee1a2c478 Fix bug in split_arg()
Mikael Berthe <mikael@lilotux.net>
parents: 554
diff changeset
682 for (start = p+1; *start && *start == ' '; start++) ;
1367
cd0d63024923 Fix up split_arg()
Mikael Berthe <mikael@lilotux.net>
parents: 1361
diff changeset
683 p = start-1;
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
684 i++;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
685 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
686 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
687
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
688 if (start < end) {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
689 *(arglst+i) = g_strndup(start, end-start);
559
4eb579399613 Fix a small bug in split_arg()
Mikael Berthe <mikael@lilotux.net>
parents: 556
diff changeset
690 if (!dontstriplast || i+1 < n)
554
2424bbf0a6db Some more work on do_room()
Mikael Berthe <mikael@lilotux.net>
parents: 553
diff changeset
691 strip_arg_special_chars(*(arglst+i));
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
692 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
693
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
694 return arglst;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
695 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
696
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
697 // free_arg_lst(arglst)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
698 // Free an array allocated by split_arg()
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
699 void free_arg_lst(char **arglst)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
700 {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
701 char **arg_elt;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
702
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
703 for (arg_elt = arglst; *arg_elt; arg_elt++)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
704 g_free(*arg_elt);
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
705 g_free(arglst);
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
706 }
576
8b3db0b555a1 Add Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 559
diff changeset
707
655
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
708 // replace_nl_with_dots(bufstr)
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
709 // Replace '\n' with "(...)" (or with a NUL if the string is too short)
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
710 void replace_nl_with_dots(char *bufstr)
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
711 {
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
712 char *p = strchr(bufstr, '\n');
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
713 if (p) {
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
714 if (strlen(p) >= 5)
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
715 strcpy(p, "(...)");
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
716 else
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
717 *p = 0;
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
718 }
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
719 }
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
720
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
721 // ut_expand_tabs(text)
1122
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
722 // Expand tabs and filter out some bad chars in string text.
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
723 // If there is no tab and no bad chars in the string, a pointer to text
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
724 // is returned (be careful _not_ to free the pointer in this case).
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
725 // If there are some tabs or bad chars, a new string with expanded chars
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
726 // and no bad chars is returned; this is up to the caller to free this
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
727 // string after use.
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
728 char *ut_expand_tabs(const char *text)
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
729 {
1568
e89787ee40f7 Fix tab expansion when using say_to -f
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1531
diff changeset
730 char *xtext, *linestart;
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
731 char *p, *q;
1122
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
732 guint n = 0, bc = 0;
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
733
2049
0ba755e5dc48 ut_expand_tabs: Check the argument is not null
Mikael Berthe <mikael@lilotux.net>
parents: 1910
diff changeset
734 if (!text)
0ba755e5dc48 ut_expand_tabs: Check the argument is not null
Mikael Berthe <mikael@lilotux.net>
parents: 1910
diff changeset
735 return NULL;
0ba755e5dc48 ut_expand_tabs: Check the argument is not null
Mikael Berthe <mikael@lilotux.net>
parents: 1910
diff changeset
736
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
737 xtext = (char*)text;
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
738 for (p=xtext; *p; p++)
1122
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
739 if (*p == '\t')
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
740 n++;
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
741 else if (*p == '\x0d')
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
742 bc++;
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
743 // XXX Are there other special chars we should filter out?
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
744
1122
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
745 if (!n && !bc)
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
746 return (char*)text;
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
747
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
748 xtext = g_new(char, strlen(text) + 1 + 8*n);
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
749 p = (char*)text;
1568
e89787ee40f7 Fix tab expansion when using say_to -f
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1531
diff changeset
750 q = linestart = xtext;
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
751 do {
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
752 if (*p == '\t') {
1568
e89787ee40f7 Fix tab expansion when using say_to -f
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1531
diff changeset
753 do { *q++ = ' '; } while ((q-linestart)%8);
1122
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
754 } else if (*p != '\x0d') {
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
755 *q++ = *p;
1568
e89787ee40f7 Fix tab expansion when using say_to -f
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1531
diff changeset
756 if (*p =='\n')
e89787ee40f7 Fix tab expansion when using say_to -f
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1531
diff changeset
757 linestart = q;
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
758 }
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
759 } while (*p++);
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
760
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
761 return xtext;
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
762 }
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
763
2050
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
764 // ut_unescape_tabs_cr(text)
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
765 // Expand CR or TAB character sequences (\n, \t) in string text.
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
766 // If there is no CR/TAB in text, then the original pointer is returned
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
767 // (be careful _not_ to free the pointer in this case).
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
768 // If there are some unescaped sequences, a new string with those chars
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
769 // replaced with real newline/tab characters is allocated; in this case
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
770 // this is up to the caller to free this string after use.
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
771 char *ut_unescape_tabs_cr(const char *text)
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
772 {
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
773 char *xtext, *linestart;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
774 char *p, *q;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
775
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
776 if (!text)
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
777 return NULL;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
778
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
779 p = g_strstr_len(text, -1, "\\n");
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
780 if (!p) {
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
781 p = g_strstr_len(text, -1, "\\t");
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
782 if (!p)
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
783 return (char*)text;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
784 }
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
785
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
786 xtext = g_new(char, strlen(text) + 1);
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
787 p = (char*)text;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
788 q = linestart = xtext;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
789 do {
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
790 if (*p == '\\') {
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
791 if (*(p+1) == '\\' && (*(p+2) == 'n' || *(p+2) == 't')) {
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
792 // This is an escaped CR sequence
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
793 *q++ = '\\';
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
794 *q++ = 'n';
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
795 p += 2;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
796 continue;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
797 }
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
798 if (*(p+1) == 'n' || *(p+1) == 't') {
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
799 // This is a CR sequence
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
800 p++;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
801 *q++ = (*p == 'n' ? '\n' : '\t');
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
802 continue;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
803 }
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
804 }
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
805 *q++ = *p;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
806 } while (*p++);
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
807
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
808 return xtext;
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
809 }
998feaa54ef0 Add ut_unescape_tabs_cr()
Mikael Berthe <mikael@lilotux.net>
parents: 2049
diff changeset
810
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
811
803
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
812 /* Cygwin's newlib does not have strcasestr() */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
813 /* The author of the code before the endif is
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
814 * Jeffrey Stedfast <fejj@ximian.com>
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
815 * and this code is reusable in compliance with the GPL v2. -- somian */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
816
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
817 #if !defined(HAVE_STRCASESTR)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
818
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
819 # define lowercase(c) (isupper ((int) (c)) ? tolower ((int) (c)) : (int) (c))
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
820 # define bm_index(c, icase) ((icase) ? lowercase (c) : (int) (c))
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
821 # define bm_equal(c1, c2, icase) ((icase) ? lowercase (c1) == lowercase (c2) : (c1) == (c2))
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
822
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
823 /* FIXME: this is just a guess... should really do some performace tests to get an accurate measure */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
824 # define bm_optimal(hlen, nlen) (((hlen) ? (hlen) > 20 : 1) && (nlen) > 10 ? 1 : 0)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
825
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
826 static unsigned char *
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
827 __boyer_moore (const unsigned char *haystack, size_t haystacklen,
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
828 const unsigned char *needle, size_t needlelen, int icase)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
829 {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
830 register unsigned char *hc_ptr, *nc_ptr;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
831 unsigned char *he_ptr, *ne_ptr, *h_ptr;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
832 size_t skiptable[256], n;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
833 register int i;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
834
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
835 #ifdef BOYER_MOORE_CHECKS
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
836 /* we don't need to do these checks since memmem/strstr/etc do it already */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
837 /* if the haystack is shorter than the needle then we can't possibly match */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
838 if (haystacklen < needlelen)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
839 return NULL;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
840
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
841 /* instant match if the pattern buffer is 0-length */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
842 if (needlelen == 0)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
843 return (unsigned char *) haystack;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
844 #endif /* BOYER_MOORE_CHECKS */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
845
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
846 /* set a pointer at the end of each string */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
847 ne_ptr = (unsigned char *) needle + needlelen - 1;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
848 he_ptr = (unsigned char *) haystack + haystacklen - 1;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
849
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
850 /* create our skip table */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
851 for (i = 0; i < 256; i++)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
852 skiptable[i] = needlelen;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
853 for (nc_ptr = (unsigned char *) needle; nc_ptr < ne_ptr; nc_ptr++)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
854 skiptable[bm_index (*nc_ptr, icase)] = (size_t) (ne_ptr - nc_ptr);
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
855
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
856 h_ptr = (unsigned char *) haystack;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
857 while (haystacklen >= needlelen) {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
858 hc_ptr = h_ptr + needlelen - 1; /* set the haystack compare pointer */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
859 nc_ptr = ne_ptr; /* set the needle compare pointer */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
860
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
861 /* work our way backwards till they don't match */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
862 for (i = 0; nc_ptr > (unsigned char *) needle; nc_ptr--, hc_ptr--, i++)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
863 if (!bm_equal (*nc_ptr, *hc_ptr, icase))
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
864 break;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
865
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
866 if (!bm_equal (*nc_ptr, *hc_ptr, icase)) {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
867 n = skiptable[bm_index (*hc_ptr, icase)];
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
868 if (n == needlelen && i)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
869 if (bm_equal (*ne_ptr, ((unsigned char *) needle)[0], icase))
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
870 n--;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
871 h_ptr += n;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
872 haystacklen -= n;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
873 } else
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
874 return (unsigned char *) h_ptr;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
875 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
876
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
877 return NULL;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
878 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
879
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
880 /*
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
881 * strcasestr:
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
882 * @haystack: string to search
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
883 * @needle: substring to search for
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
884 *
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
885 * Finds the first occurence of the substring @needle within the
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
886 * string @haystack ignoring case.
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
887 *
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
888 * Returns a pointer to the beginning of the substring match within
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
889 * @haystack, or NULL if the substring is not found.
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
890 **/
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
891 char *
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
892 strcasestr (const char *haystack, const char *needle)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
893 {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
894 register unsigned char *h, *n, *hc, *nc;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
895 size_t needlelen;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
896
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
897 needlelen = strlen (needle);
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
898
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
899 if (needlelen == 0) {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
900 return (char *) haystack;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
901 } else if (bm_optimal (0, needlelen)) {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
902 return (char *) __boyer_moore ((const unsigned char *) haystack,
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
903 strlen (haystack),
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
904 (const unsigned char *) needle,
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
905 needlelen, 1);
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
906 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
907
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
908 h = (unsigned char *) haystack;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
909 n = (unsigned char *) needle;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
910
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
911 while (*(h + needlelen - 1)) {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
912 if (lowercase (*h) == lowercase (*n)) {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
913 for (hc = h + 1, nc = n + 1; *hc && *nc; hc++, nc++)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
914 if (lowercase (*hc) != lowercase (*nc))
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
915 break;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
916
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
917 if (!*nc)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
918 return (char *) h;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
919 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
920 h++;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
921 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
922 return NULL;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
923 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
924 #endif /* !HAVE_STRCASESTR */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
925
1203
c96fef31ff96 Expand startswith()
Mikael Berthe <mikael@lilotux.net>
parents: 1192
diff changeset
926 // startswith(str, word, ignore_case)
1192
7b8765c10abb New command: /source
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
927 // Returns TRUE if string str starts with word.
1203
c96fef31ff96 Expand startswith()
Mikael Berthe <mikael@lilotux.net>
parents: 1192
diff changeset
928 int startswith(const char *str, const char *word, guint ignore_case)
1192
7b8765c10abb New command: /source
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
929 {
1203
c96fef31ff96 Expand startswith()
Mikael Berthe <mikael@lilotux.net>
parents: 1192
diff changeset
930 if (ignore_case && !strncasecmp(str, word, strlen(word)))
c96fef31ff96 Expand startswith()
Mikael Berthe <mikael@lilotux.net>
parents: 1192
diff changeset
931 return TRUE;
c96fef31ff96 Expand startswith()
Mikael Berthe <mikael@lilotux.net>
parents: 1192
diff changeset
932 else if (!ignore_case && !strncmp(str, word, strlen(word)))
1192
7b8765c10abb New command: /source
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
933 return TRUE;
7b8765c10abb New command: /source
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
934 return FALSE;
7b8765c10abb New command: /source
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
935 }
7b8765c10abb New command: /source
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
936
1811
e6d355e50d7a Update Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 1780
diff changeset
937 /* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2: For Vim users... */