annotate mcabber/src/utils.c @ 1292:382ec54b584e

Muc nick coloring functionality
author Michal 'vorner' Vaner <vorner@ucw.cz>
date Tue, 28 Aug 2007 10:11:39 +0200
parents 3a4018f18bdf
children 0562106d20c1
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 *
1205
2de8f8ba1f34 Update copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 1203
diff changeset
4 * Copyright (C) 2005-2007 Mikael Berthe <mikael@lilotux.net>
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
5 * ut_* functions are derived from Cabber debug/log code.
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
6 * from_iso8601() comes from the Gaim project.
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
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
24 #include <stdio.h>
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
25 #include <stdlib.h>
77
32f54ad6d729 [/trunk] Changeset 91 by mikael
mikael
parents: 76
diff changeset
26 #include <string.h>
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
27 #include <stdarg.h>
802
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
28
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
29 /* 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
30 #ifdef __CYGWIN__
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
31 # define timezonevar
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
32 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
33 #endif
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
34 #include <time.h>
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
35 #include <unistd.h>
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
36 #include <sys/types.h>
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
37 #include <sys/stat.h>
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
38 #include <ctype.h>
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
39 #include <glib.h>
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
40
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
41 #include <config.h>
977
5b01de4ac5e1 Cosmetic changes
Alexis Hildebrandt <afh [at] 2drop [dot] net>
parents: 952
diff changeset
42 #include "utils.h"
378
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
43 #include "logprint.h"
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
44
76
a8f8492abd44 [/trunk] Changeset 90 by mikael
mikael
parents: 24
diff changeset
45 static int DebugEnabled;
77
32f54ad6d729 [/trunk] Changeset 91 by mikael
mikael
parents: 76
diff changeset
46 static char *FName;
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
47
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
48 // expand_filename(filename)
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
49 // 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
50 // The caller must free the string after use.
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
51 char *expand_filename(const char *fname)
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
52 {
1222
75aee46d3aee Fix a problem with the ssl_ca{file,path} variables expansion
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
53 if (!fname)
75aee46d3aee Fix a problem with the ssl_ca{file,path} variables expansion
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
54 return NULL;
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
55 if (!strncmp(fname, "~/", 2)) {
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
56 char *homedir = getenv("HOME");
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
57 if (homedir)
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
58 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
59 }
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
60 return g_strdup(fname);
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
61 }
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
62
952
527d6f234924 Small code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 911
diff changeset
63 void ut_InitDebug(int level, const char *filename)
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
64 {
77
32f54ad6d729 [/trunk] Changeset 91 by mikael
mikael
parents: 76
diff changeset
65 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
66 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
67 int err;
77
32f54ad6d729 [/trunk] Changeset 91 by mikael
mikael
parents: 76
diff changeset
68
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
69 if (level < 1) {
138
250f872c722f [/trunk] Changeset 150 by mikael
mikael
parents: 81
diff changeset
70 DebugEnabled = 0;
250f872c722f [/trunk] Changeset 150 by mikael
mikael
parents: 81
diff changeset
71 FName = NULL;
250f872c722f [/trunk] Changeset 150 by mikael
mikael
parents: 81
diff changeset
72 return;
250f872c722f [/trunk] Changeset 150 by mikael
mikael
parents: 81
diff changeset
73 }
250f872c722f [/trunk] Changeset 150 by mikael
mikael
parents: 81
diff changeset
74
250f872c722f [/trunk] Changeset 150 by mikael
mikael
parents: 81
diff changeset
75 if (filename)
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
76 FName = expand_filename(filename);
77
32f54ad6d729 [/trunk] Changeset 91 by mikael
mikael
parents: 76
diff changeset
77 else {
138
250f872c722f [/trunk] Changeset 150 by mikael
mikael
parents: 81
diff changeset
78 FName = getenv("HOME");
250f872c722f [/trunk] Changeset 150 by mikael
mikael
parents: 81
diff changeset
79 if (!FName)
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
80 FName = g_strdup("/tmp/mcabberlog");
138
250f872c722f [/trunk] Changeset 150 by mikael
mikael
parents: 81
diff changeset
81 else {
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1122
diff changeset
82 FName = g_strdup_printf("%s/mcabberlog", FName);
138
250f872c722f [/trunk] Changeset 150 by mikael
mikael
parents: 81
diff changeset
83 }
77
32f54ad6d729 [/trunk] Changeset 91 by mikael
mikael
parents: 76
diff changeset
84 }
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
85
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
86 DebugEnabled = level;
76
a8f8492abd44 [/trunk] Changeset 90 by mikael
mikael
parents: 24
diff changeset
87
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
88 fp = fopen(FName, "a");
378
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
89 if (!fp) {
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
90 fprintf(stderr, "ERROR: Cannot open tracelog file\n");
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
91 return;
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
92 }
428
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
93
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
94 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
95 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
96 fclose(fp);
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
97 DebugEnabled = 0;
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
98 FName = NULL;
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
99 if (err) {
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
100 fprintf(stderr, "ERROR: cannot stat the tracelog file!\n");
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
101 } else {
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
102 fprintf(stderr, "ERROR: tracelog file does not belong to you!\n");
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
103 }
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
104 return;
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
105 }
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
106 fchmod(fileno(fp), S_IRUSR|S_IWUSR);
4470868f90e5 Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents: 419
diff changeset
107
1241
3a4018f18bdf Cosmetics
Mikael Berthe <mikael@lilotux.net>
parents: 1222
diff changeset
108 fprintf(fp, "New trace log started.\n----------------------\n");
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
109 fclose(fp);
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
110 }
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
111
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
112 void ut_WriteLog(unsigned int flag, const char *data)
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
113 {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
114 if (!DebugEnabled || !FName) return;
393
f8f3c7493457 Whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 378
diff changeset
115
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
116 if (((DebugEnabled == 2) && (flag & (LPRINT_LOG|LPRINT_DEBUG))) ||
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
117 ((DebugEnabled == 1) && (flag & LPRINT_LOG))) {
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
118 FILE *fp = fopen(FName, "a+");
378
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
119 if (!fp) {
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
120 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file");
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
121 return;
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
122 }
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
123 fputs(data, fp);
24
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
124 fclose(fp);
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
125 }
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
126 }
e88b15cbf2de [/trunk] Changeset 40 by mikael
mikael
parents:
diff changeset
127
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
128 // checkset_perm(name, setmode)
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
129 // Check the permissions of the "name" file/dir
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
130 // 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
131 // 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
132 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
133 {
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
134 int fd;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
135 struct stat buf;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
136
816
a6628f0aabc1 Skip file permission checks on Cygwin
Mikael Berthe <mikael@lilotux.net>
parents: 803
diff changeset
137 #ifdef __CYGWIN__
a6628f0aabc1 Skip file permission checks on Cygwin
Mikael Berthe <mikael@lilotux.net>
parents: 803
diff changeset
138 // Permission checking isn't efficent on Cygwin
a6628f0aabc1 Skip file permission checks on Cygwin
Mikael Berthe <mikael@lilotux.net>
parents: 803
diff changeset
139 return 0;
a6628f0aabc1 Skip file permission checks on Cygwin
Mikael Berthe <mikael@lilotux.net>
parents: 803
diff changeset
140 #endif
a6628f0aabc1 Skip file permission checks on Cygwin
Mikael Berthe <mikael@lilotux.net>
parents: 803
diff changeset
141
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
142 fd = lstat(name, &buf);
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
143 if (fd == -1) return -1;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
144
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
145 if (buf.st_uid != geteuid()) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
146 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
147 return 1;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
148 }
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
149
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
150 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
151 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
152 if (setmode) {
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
153 mode_t newmode = 0;
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
154 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
155 if (S_ISDIR(buf.st_mode))
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
156 newmode |= S_IXUSR;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
157 newmode |= S_IRUSR | S_IWUSR;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
158 if (chmod(name, newmode)) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
159 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
160 return 1;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
161 }
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
162 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
163 } else {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
164 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
165 return 1;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
166 }
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
167 }
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
168
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
169 return 0;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
170 }
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
171
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
172 const char *ut_get_tmpdir(void)
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
173 {
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
174 static const char *tmpdir;
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
175 const char *tmpvars[] = { "MCABBERTMPDIR", "TMP", "TMPDIR", "TEMP" };
735
2f027806cd48 Some code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 727
diff changeset
176 unsigned int i;
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
177
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
178 if (tmpdir)
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
179 return tmpdir;
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
180
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
181 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
182 tmpdir = getenv(tmpvars[i]);
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
183 if (tmpdir && tmpdir[0] && tmpdir[0] == '/' && tmpdir[1]) {
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
184 // Looks ok.
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
185 return tmpdir;
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
186 }
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
187 }
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
188
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
189 // Default temporary directory
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
190 tmpdir = "/tmp";
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
191 return tmpdir;
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
192 }
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 463
diff changeset
193
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
194 // to_iso8601(dststr, timestamp)
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
195 // Convert timestamp to iso8601 format, and store it in dststr.
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
196 // NOTE: dststr should be at last 19 chars long.
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
197 // Return should be 0
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
198 int to_iso8601(char *dststr, time_t timestamp)
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
199 {
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
200 struct tm *tm_time;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
201 int ret;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
202
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
203 tm_time = gmtime(&timestamp);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
204
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
205 ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ",
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
206 1900+tm_time->tm_year, tm_time->tm_mon+1, tm_time->tm_mday,
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
207 tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
208
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
209 return ((ret == -1) ? -1 : 0);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
210 }
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
211
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
212 // from_iso8601(timestamp, utc)
802
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
213 // This function came from the Gaim project, gaim_str_to_time().
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
214 // (Actually date may not be pure iso-8601)
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
215 // 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
216 // ** Modified by somian 10 Apr 2006 with advice from ysth.
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
217 time_t from_iso8601(const char *timestamp, int utc)
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
218 {
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
219 struct tm t;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
220 time_t retval = 0;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
221 char buf[32];
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
222 char *c;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
223 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
224 int hms_succ = 0;
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
225
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
226 time(&retval);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
227 localtime_r(&retval, &t);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
228
463
339e85418b49 from_iso8601(): set time to 00:00:00 if not specified
Mikael Berthe <mikael@lilotux.net>
parents: 456
diff changeset
229 /* 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
230 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
231
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
232 snprintf(buf, sizeof(buf), "%s", timestamp);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
233 c = buf;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
234
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
235 /* 4 digit year */
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
236 if (!sscanf(c, "%04d", &t.tm_year)) return 0;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
237 c+=4;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
238 if (*c == '-')
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
239 c++;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
240
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
241 t.tm_year -= 1900;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
242
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
243 /* 2 digit month */
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
244 if (!sscanf(c, "%02d", &t.tm_mon)) return 0;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
245 c+=2;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
246 if (*c == '-')
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
247 c++;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
248
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
249 t.tm_mon -= 1;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
250
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
251 /* 2 digit day */
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
252 if (!sscanf(c, "%02d", &t.tm_mday)) return 0;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
253 c+=2;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
254 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
255 c++; /* skip the "T" */
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
256
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
257 /* 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
258 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
259 {
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
260 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
261 c += 8;
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
262 }
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
263 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
264 {
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
265 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
266 c += 6;
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
267 }
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
268
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
269 if (hms_succ) {
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
270 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
271
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
272 if (*c == '.') /* dealing with precision we don't care about */
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
273 c += 4;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
274
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
275 if ((*c == '+' || *c == '-') &&
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
276 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) {
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
277 tzoff = tzhrs*60*60 + tzmins*60;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
278 if (*c == '+')
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
279 tzoff *= -1;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
280 }
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
281
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
282 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
283 #ifdef HAVE_TM_GMTOFF
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
284 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
285 #else
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
286 # 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
287 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
288 tzoff -= timezone;
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
289 # endif
dd860da62002 Enable use on systems that do not have tm_gmtoff including Cygwin
Soren Andersen <somian@pobox.com>
parents: 735
diff changeset
290 #endif
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
291 }
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
292 }
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
293 }
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
294
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
295 t.tm_isdst = -1;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
296
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
297 retval = mktime(&t);
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
298
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
299 retval += tzoff;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
300
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
301 return retval;
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 138
diff changeset
302 }
419
2f9852610cf4 Small code review
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
303
2f9852610cf4 Small code review
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
304 // Should only be used for delays < 1s
2f9852610cf4 Small code review
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
305 inline void safe_usleep(unsigned int usec)
2f9852610cf4 Small code review
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
306 {
2f9852610cf4 Small code review
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
307 struct timespec req;
2f9852610cf4 Small code review
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
308 req.tv_sec = 0;
2f9852610cf4 Small code review
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
309 req.tv_nsec = (long)usec * 1000L;
2f9852610cf4 Small code review
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
310 nanosleep(&req, NULL);
2f9852610cf4 Small code review
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
311 }
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
312
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
313 /**
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
314 * 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
315 * really convenient for our usage.
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
316 *
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
317 * Check if the full JID is valid
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
318 * Return 0 if it is valid, non zero otherwise
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
319 */
1292
382ec54b584e Muc nick coloring functionality
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1241
diff changeset
320 int check_jid_syntax(const char *fjid)
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
321 {
1292
382ec54b584e Muc nick coloring functionality
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1241
diff changeset
322 const char *str;
382ec54b584e Muc nick coloring functionality
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1241
diff changeset
323 const char *domain, *resource;
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
324 int domlen;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
325
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 979
diff changeset
326 if (!fjid) return 1;
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
327
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 979
diff changeset
328 domain = strchr(fjid, JID_DOMAIN_SEPARATOR);
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
329
456
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
330 /* the username is optional */
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
331 if (!domain) {
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 979
diff changeset
332 domain = fjid;
456
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
333 } else {
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
334 /* node identifiers may not be longer than 1023 bytes */
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 979
diff changeset
335 if ((domain == fjid) || (domain-fjid > 1023))
456
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
336 return 1;
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
337 domain++;
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
338
456
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
339 /* check for low and invalid ascii characters in the username */
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 979
diff changeset
340 for (str = fjid; *str != JID_DOMAIN_SEPARATOR; str++) {
977
5b01de4ac5e1 Cosmetic changes
Alexis Hildebrandt <afh [at] 2drop [dot] net>
parents: 952
diff changeset
341 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
342 *str == '<' || *str == '>' || *str == '\'' ||
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
343 *str == '"' || *str == '&') {
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
344 return 1;
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
345 }
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
346 }
456
471c9ccde028 Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents: 452
diff changeset
347 /* the username is okay as far as we can tell without LIBIDN */
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
348 }
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
349
977
5b01de4ac5e1 Cosmetic changes
Alexis Hildebrandt <afh [at] 2drop [dot] net>
parents: 952
diff changeset
350 resource = strchr(domain, JID_RESOURCE_SEPARATOR);
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
351
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
352 /* the resource is optional */
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
353 if (resource) {
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
354 domlen = resource - domain;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
355 resource++;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
356 /* resources may not be longer than 1023 bytes */
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
357 if ((*resource == '\0') || strlen(resource) > 1023)
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
358 return 1;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
359 } else {
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
360 domlen = strlen(domain);
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
361 }
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
362
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
363 /* there must be a domain identifier */
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
364 if (domlen == 0) return 1;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
365
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
366 /* and it must not be longer than 1023 bytes */
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
367 if (domlen > 1023) return 1;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
368
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
369 /* make sure the hostname is valid characters */
977
5b01de4ac5e1 Cosmetic changes
Alexis Hildebrandt <afh [at] 2drop [dot] net>
parents: 952
diff changeset
370 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
371 if (!(isalnum(*str) || *str == '.' || *str == '-' || *str == '_'))
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
372 return 1;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
373 }
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
374
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
375 /* it's okay as far as we can tell without LIBIDN */
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
376 return 0;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
377 }
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
378
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
379
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
380 inline void mc_strtolower(char *str)
452
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
381 {
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
382 if (!str) return;
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
383 for ( ; *str; str++)
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
384 *str = tolower(*str);
dfd9c62b3a39 Jabber Id syntax checks
Mikael Berthe <mikael@lilotux.net>
parents: 428
diff changeset
385 }
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
386
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
387 // strip_arg_special_chars(string)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
388 // Remove quotes and backslashes before an escaped quote
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
389 // Only quotes need a backslash
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
390 // 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
391 void strip_arg_special_chars(char *s)
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
392 {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
393 int instring = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
394 int escape = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
395 char *p;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
396
979
ea939ff047d8 Improve /buffer search_*
Mikael Berthe <mikael@lilotux.net>
parents: 977
diff changeset
397 if (!s) return;
ea939ff047d8 Improve /buffer search_*
Mikael Berthe <mikael@lilotux.net>
parents: 977
diff changeset
398
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
399 for (p = s; *p; p++) {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
400 if (*p == '"') {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
401 if (!escape) {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
402 instring = !instring;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
403 strcpy(p, p+1);
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
404 p--;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
405 } else {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
406 escape = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
407 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
408 } else if (*p == '\\') {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
409 if (!escape) {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
410 if (*(p+1) == '"') {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
411 strcpy(p, p+1);
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
412 p--;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
413 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
414 escape = TRUE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
415 } else {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
416 escape = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
417 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
418 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
419 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
420 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
421
554
2424bbf0a6db Some more work on do_room()
Mikael Berthe <mikael@lilotux.net>
parents: 553
diff changeset
422 // split_arg(arg, n, preservelast)
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
423 // 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
424 // double quotes.
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
425 // 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
426 // 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
427 // 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
428 // processing of quote chars)
554
2424bbf0a6db Some more work on do_room()
Mikael Berthe <mikael@lilotux.net>
parents: 553
diff changeset
429 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
430 {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
431 char **arglst;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
432 const char *p, *start, *end;
735
2f027806cd48 Some code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 727
diff changeset
433 unsigned int i = 0;
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
434 int instring = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
435 int escape = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
436
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
437 arglst = g_new0(char*, n+1);
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
438
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
439 if (!arg || !n) return arglst;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
440
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
441 // Skip leading space
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
442 for (start = arg; *start && *start == ' '; start++) ;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
443 // End of string pointer
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
444 for (end = start; *end; end++) ;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
445 // Skip trailing space
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
446 while (end > start+1 && *(end-1) == ' ')
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
447 end--;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
448
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
449 for (p = start; p < end; p++) {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
450 if (*p == '"' && !escape)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
451 instring = !instring;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
452 if (*p == '\\' && !escape)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
453 escape = TRUE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
454 else if (escape)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
455 escape = FALSE;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
456 if (*p == ' ' && !instring && i+1 < n) {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
457 // end of parameter
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
458 *(arglst+i) = g_strndup(start, p-start);
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
459 strip_arg_special_chars(*(arglst+i));
556
c4fee1a2c478 Fix bug in split_arg()
Mikael Berthe <mikael@lilotux.net>
parents: 554
diff changeset
460 for (start = p+1; *start && *start == ' '; start++) ;
911
fe57dd753a56 Fix a segfault
Mikael Berthe <mikael@lilotux.net>
parents: 816
diff changeset
461 p = start;
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
462 i++;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
463 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
464 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
465
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
466 if (start < end) {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
467 *(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
468 if (!dontstriplast || i+1 < n)
554
2424bbf0a6db Some more work on do_room()
Mikael Berthe <mikael@lilotux.net>
parents: 553
diff changeset
469 strip_arg_special_chars(*(arglst+i));
551
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
470 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
471
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
472 return arglst;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
473 }
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
474
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
475 // free_arg_lst(arglst)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
476 // Free an array allocated by split_arg()
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
477 void free_arg_lst(char **arglst)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
478 {
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
479 char **arg_elt;
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
480
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
481 for (arg_elt = arglst; *arg_elt; arg_elt++)
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
482 g_free(*arg_elt);
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
483 g_free(arglst);
c71699efa5cc Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
484 }
576
8b3db0b555a1 Add Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 559
diff changeset
485
655
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
486 // 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
487 // 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
488 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
489 {
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
490 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
491 if (p) {
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
492 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
493 strcpy(p, "(...)");
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
494 else
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
495 *p = 0;
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
496 }
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
497 }
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
498
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
499 // ut_expand_tabs(text)
1122
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
500 // 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
501 // 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
502 // 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
503 // 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
504 // 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
505 // string after use.
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
506 char *ut_expand_tabs(const char *text)
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
507 {
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
508 char *xtext;
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
509 char *p, *q;
1122
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
510 guint n = 0, bc = 0;
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
511
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
512 xtext = (char*)text;
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
513 for (p=xtext; *p; p++)
1122
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
514 if (*p == '\t')
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
515 n++;
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
516 else if (*p == '\x0d')
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
517 bc++;
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
518 // 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
519
1122
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
520 if (!n && !bc)
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
521 return (char*)text;
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
522
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
523 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
524 p = (char*)text;
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
525 q = xtext;
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
526 do {
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
527 if (*p == '\t') {
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
528 do { *q++ = ' '; } while ((q-xtext)%8);
1122
648fe6f715a6 Filter out ^M (0x0d) characters in incoming messages
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
529 } else if (*p != '\x0d') {
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
530 *q++ = *p;
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
531 }
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
532 } while (*p++);
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
533
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
534 return xtext;
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
535 }
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
536
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
537
803
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
538 /* Cygwin's newlib does not have strcasestr() */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
539 /* 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
540 * Jeffrey Stedfast <fejj@ximian.com>
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
541 * 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
542
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
543 #if !defined(HAVE_STRCASESTR)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
544
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
545 # 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
546 # 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
547 # 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
548
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
549 /* 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
550 # 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
551
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
552 static unsigned char *
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
553 __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
554 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
555 {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
556 register unsigned char *hc_ptr, *nc_ptr;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
557 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
558 size_t skiptable[256], n;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
559 register int i;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
560
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
561 #ifdef BOYER_MOORE_CHECKS
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
562 /* 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
563 /* 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
564 if (haystacklen < needlelen)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
565 return NULL;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
566
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
567 /* 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
568 if (needlelen == 0)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
569 return (unsigned char *) haystack;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
570 #endif /* BOYER_MOORE_CHECKS */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
571
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
572 /* 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
573 ne_ptr = (unsigned char *) needle + needlelen - 1;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
574 he_ptr = (unsigned char *) haystack + haystacklen - 1;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
575
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
576 /* create our skip table */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
577 for (i = 0; i < 256; i++)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
578 skiptable[i] = needlelen;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
579 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
580 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
581
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
582 h_ptr = (unsigned char *) haystack;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
583 while (haystacklen >= needlelen) {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
584 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
585 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
586
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
587 /* 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
588 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
589 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
590 break;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
591
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
592 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
593 n = skiptable[bm_index (*hc_ptr, icase)];
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
594 if (n == needlelen && i)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
595 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
596 n--;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
597 h_ptr += n;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
598 haystacklen -= n;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
599 } else
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
600 return (unsigned char *) h_ptr;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
601 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
602
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
603 return NULL;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
604 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
605
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
606 /*
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
607 * strcasestr:
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
608 * @haystack: string to search
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
609 * @needle: substring to search for
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
610 *
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
611 * 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
612 * string @haystack ignoring case.
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
613 *
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
614 * 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
615 * @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
616 **/
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
617 char *
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
618 strcasestr (const char *haystack, const char *needle)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
619 {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
620 register unsigned char *h, *n, *hc, *nc;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
621 size_t needlelen;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
622
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
623 needlelen = strlen (needle);
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
624
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
625 if (needlelen == 0) {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
626 return (char *) haystack;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
627 } else if (bm_optimal (0, needlelen)) {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
628 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
629 strlen (haystack),
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
630 (const unsigned char *) needle,
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
631 needlelen, 1);
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
632 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
633
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
634 h = (unsigned char *) haystack;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
635 n = (unsigned char *) needle;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
636
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
637 while (*(h + needlelen - 1)) {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
638 if (lowercase (*h) == lowercase (*n)) {
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
639 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
640 if (lowercase (*hc) != lowercase (*nc))
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
641 break;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
642
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
643 if (!*nc)
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
644 return (char *) h;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
645 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
646 h++;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
647 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
648 return NULL;
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
649 }
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
650 #endif /* !HAVE_STRCASESTR */
8f8d8f8157a2 Take care of the strcasestr lacking (Cygwin)
Soren Andersen <somian@pobox.com>
parents: 802
diff changeset
651
1203
c96fef31ff96 Expand startswith()
Mikael Berthe <mikael@lilotux.net>
parents: 1192
diff changeset
652 // startswith(str, word, ignore_case)
1192
7b8765c10abb New command: /source
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
653 // Returns TRUE if string str starts with word.
1203
c96fef31ff96 Expand startswith()
Mikael Berthe <mikael@lilotux.net>
parents: 1192
diff changeset
654 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
655 {
1203
c96fef31ff96 Expand startswith()
Mikael Berthe <mikael@lilotux.net>
parents: 1192
diff changeset
656 if (ignore_case && !strncasecmp(str, word, strlen(word)))
c96fef31ff96 Expand startswith()
Mikael Berthe <mikael@lilotux.net>
parents: 1192
diff changeset
657 return TRUE;
c96fef31ff96 Expand startswith()
Mikael Berthe <mikael@lilotux.net>
parents: 1192
diff changeset
658 else if (!ignore_case && !strncmp(str, word, strlen(word)))
1192
7b8765c10abb New command: /source
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
659 return TRUE;
7b8765c10abb New command: /source
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
660 return FALSE;
7b8765c10abb New command: /source
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
661 }
7b8765c10abb New command: /source
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
662
580
fed6d1e4d7a9 Fix modelines
Mikael Berthe <mikael@lilotux.net>
parents: 576
diff changeset
663 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */