trinidad_init_services 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +5 -0
- data/Gemfile +3 -0
- data/History.txt +6 -0
- data/README.md +95 -0
- data/Rakefile +9 -58
- data/bin/trinidad_init_service +3 -4
- data/init.d/trinidad.erb +33 -12
- data/jsvc-unix-src/CHANGES.txt +62 -0
- data/jsvc-unix-src/INSTALL.txt +81 -0
- data/jsvc-unix-src/Makedefs.in +32 -0
- data/jsvc-unix-src/Makefile.in +42 -0
- data/jsvc-unix-src/configure +4417 -0
- data/jsvc-unix-src/configure.in +141 -0
- data/jsvc-unix-src/man/README +20 -0
- data/jsvc-unix-src/man/fetch.sh +36 -0
- data/jsvc-unix-src/man/jsvc.1.xml +214 -0
- data/jsvc-unix-src/native/.indent.pro +7 -0
- data/jsvc-unix-src/native/Makefile.in +46 -0
- data/jsvc-unix-src/native/arguments.c +476 -0
- data/jsvc-unix-src/native/arguments.h +94 -0
- data/jsvc-unix-src/native/debug.c +87 -0
- data/jsvc-unix-src/native/debug.h +65 -0
- data/jsvc-unix-src/native/dso-dlfcn.c +62 -0
- data/jsvc-unix-src/native/dso-dyld.c +153 -0
- data/jsvc-unix-src/native/dso.h +38 -0
- data/jsvc-unix-src/native/help.c +106 -0
- data/jsvc-unix-src/native/help.h +24 -0
- data/jsvc-unix-src/native/home.c +265 -0
- data/jsvc-unix-src/native/home.h +47 -0
- data/jsvc-unix-src/native/java.c +608 -0
- data/jsvc-unix-src/native/java.h +35 -0
- data/jsvc-unix-src/native/jsvc-unix.c +1267 -0
- data/jsvc-unix-src/native/jsvc.h +55 -0
- data/jsvc-unix-src/native/location.c +151 -0
- data/jsvc-unix-src/native/location.h +29 -0
- data/jsvc-unix-src/native/locks.c +52 -0
- data/jsvc-unix-src/native/locks.h +40 -0
- data/jsvc-unix-src/native/replace.c +121 -0
- data/jsvc-unix-src/native/replace.h +39 -0
- data/jsvc-unix-src/native/signals.c +105 -0
- data/jsvc-unix-src/native/signals.h +34 -0
- data/jsvc-unix-src/native/version.h +63 -0
- data/jsvc-unix-src/support/apfunctions.m4 +110 -0
- data/jsvc-unix-src/support/apjava.m4 +94 -0
- data/jsvc-unix-src/support/apsupport.m4 +155 -0
- data/jsvc-unix-src/support/buildconf.sh +33 -0
- data/jsvc-unix-src/support/config.guess +1371 -0
- data/jsvc-unix-src/support/config.sub +1760 -0
- data/jsvc-unix-src/support/install.sh +128 -0
- data/jsvc-unix-src/support/mkdist.sh +104 -0
- data/lib/trinidad/daemon.rb +31 -0
- data/lib/trinidad_init_services.rb +2 -30
- data/lib/trinidad_init_services/configuration.rb +91 -14
- data/lib/trinidad_init_services/version.rb +5 -0
- data/spec/spec_helper.rb +5 -6
- data/spec/trinidad_daemon_spec.rb +0 -1
- data/spec/trinidad_init_services/configuration_spec.rb +34 -1
- data/trinidad_init_services.gemspec +14 -51
- metadata +146 -87
- data/README +0 -63
- data/trinidad-libs/jsvc_linux +0 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
2
|
+
* contributor license agreements. See the NOTICE file distributed with
|
3
|
+
* this work for additional information regarding copyright ownership.
|
4
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
5
|
+
* (the "License"); you may not use this file except in compliance with
|
6
|
+
* the License. You may obtain a copy of the License at
|
7
|
+
*
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
*
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
* See the License for the specific language governing permissions and
|
14
|
+
* limitations under the License.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/* @version $Id: jsvc.h 921756 2010-03-11 09:38:58Z mturk $ */
|
18
|
+
#ifndef __JSVC_H__
|
19
|
+
#define __JSVC_H__
|
20
|
+
|
21
|
+
#include <stdio.h>
|
22
|
+
#include <stdlib.h>
|
23
|
+
#include <stdarg.h>
|
24
|
+
#include <string.h>
|
25
|
+
#include <sys/types.h>
|
26
|
+
#include <sys/stat.h>
|
27
|
+
|
28
|
+
/* Definitions for booleans */
|
29
|
+
#ifdef OS_DARWIN
|
30
|
+
#include <stdbool.h>
|
31
|
+
#else
|
32
|
+
typedef enum {
|
33
|
+
false,
|
34
|
+
true
|
35
|
+
} bool;
|
36
|
+
#endif
|
37
|
+
|
38
|
+
#include "version.h"
|
39
|
+
#include "debug.h"
|
40
|
+
#include "arguments.h"
|
41
|
+
#include "home.h"
|
42
|
+
#include "location.h"
|
43
|
+
#include "replace.h"
|
44
|
+
#include "dso.h"
|
45
|
+
#include "java.h"
|
46
|
+
#include "help.h"
|
47
|
+
#include "signals.h"
|
48
|
+
#include "locks.h"
|
49
|
+
|
50
|
+
int main(int argc, char *argv[]);
|
51
|
+
void main_reload(void);
|
52
|
+
void main_shutdown(void);
|
53
|
+
|
54
|
+
#endif /* ifndef __JSVC_H__ */
|
55
|
+
|
@@ -0,0 +1,151 @@
|
|
1
|
+
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
2
|
+
* contributor license agreements. See the NOTICE file distributed with
|
3
|
+
* this work for additional information regarding copyright ownership.
|
4
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
5
|
+
* (the "License"); you may not use this file except in compliance with
|
6
|
+
* the License. You may obtain a copy of the License at
|
7
|
+
*
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
*
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
* See the License for the specific language governing permissions and
|
14
|
+
* limitations under the License.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/* @version $Id: location.c 1199195 2011-11-08 11:30:20Z mturk $ */
|
18
|
+
#include "jsvc.h"
|
19
|
+
|
20
|
+
/* Locations of various JVM files. We have to deal with all this madness since
|
21
|
+
* we're not distributed togheter (yet!) with an official VM distribution. All
|
22
|
+
* this CRAP needs improvement, and based on the observation of default
|
23
|
+
* distributions of VMs and OSes. If it doesn't work for you, please report
|
24
|
+
* your VM layout (ls -laR) and system details (build/config.guess) so that we
|
25
|
+
* can improve the search algorithms.
|
26
|
+
*/
|
27
|
+
|
28
|
+
/* If JAVA_HOME is not defined we search this list of paths (OS-dependant)
|
29
|
+
* to find the default location of the JVM.
|
30
|
+
*/
|
31
|
+
char *location_home[] = {
|
32
|
+
#if defined(OS_DARWIN)
|
33
|
+
"/System/Library/Frameworks/JavaVM.framework/Home",
|
34
|
+
"/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/",
|
35
|
+
#elif defined(OS_LINUX) || defined(OS_SOLARIS) || defined(OS_BSD)
|
36
|
+
"/usr/java",
|
37
|
+
"/usr/local/java",
|
38
|
+
"/etc/alternatives/java_sdk",
|
39
|
+
"/etc/alternatives/jre",
|
40
|
+
#elif defined(OS_CYGWIN)
|
41
|
+
"/cygdrive/c/WINNT/system32/java",
|
42
|
+
#elif defined(OS_SYSV)
|
43
|
+
"/opt/java",
|
44
|
+
"/opt/java/jdk13",
|
45
|
+
#elif defined(OS_TRU64)
|
46
|
+
"/usr/opt/java142",
|
47
|
+
"/usr/opt/java13",
|
48
|
+
#elif defined(OS_HPUX)
|
49
|
+
"/opt/java6",
|
50
|
+
"/opt/java1.5",
|
51
|
+
"/opt/java1.4",
|
52
|
+
"/opt/java1.3",
|
53
|
+
#endif
|
54
|
+
NULL,
|
55
|
+
};
|
56
|
+
|
57
|
+
/* The jvm.cfg file defines the VMs available for invocation. So far, on all
|
58
|
+
* all systems I've seen it's in $JAVA_HOME/lib. If this file is not found,
|
59
|
+
* then the "default" VMs (from location_jvm_default) is searched, otherwise,
|
60
|
+
* we're going to look thru the "configured" VMs (from lod_cfgvm) lying
|
61
|
+
* somewhere around JAVA_HOME. (Only two, I'm happy)
|
62
|
+
*/
|
63
|
+
char *location_jvm_cfg[] = {
|
64
|
+
"$JAVA_HOME/jre/lib/jvm.cfg", /* JDK */
|
65
|
+
"$JAVA_HOME/lib/jvm.cfg", /* JRE */
|
66
|
+
"$JAVA_HOME/jre/lib/" CPU "/jvm.cfg", /* JDK */
|
67
|
+
"$JAVA_HOME/lib/" CPU "/jvm.cfg", /* JRE */
|
68
|
+
NULL,
|
69
|
+
};
|
70
|
+
|
71
|
+
/* This is the list of "defaults" VM (searched when jvm.cfg is not found, as
|
72
|
+
* in the case of most JDKs 1.2.2
|
73
|
+
*/
|
74
|
+
char *location_jvm_default[] = {
|
75
|
+
#if defined(OS_DARWIN)
|
76
|
+
"$JAVA_HOME/../Libraries/libjvm.dylib",
|
77
|
+
#elif defined(OS_CYGWIN)
|
78
|
+
"$JAVA_HOME/jre/bin/classic/jvm.dll", /* Sun JDK 1.3 */
|
79
|
+
"$JAVA_HOME/jre/bin/client/jvm.dll", /* Sun JDK 1.4 */
|
80
|
+
#elif defined(OS_LINUX) || defined(OS_SOLARIS) || defined(OS_BSD) || defined(OS_SYSV) || defined(OS_FREEBSD) || defined(OS_TRU64)
|
81
|
+
"$JAVA_HOME/jre/lib/" CPU "/classic/libjvm.so", /* Sun JDK 1.2 */
|
82
|
+
"$JAVA_HOME/jre/lib/" CPU "/server/libjvm.so", /* Sun JDK 1.4 */
|
83
|
+
"$JAVA_HOME/jre/lib/" CPU "/client/libjvm.so", /* Sun JDK 1.3 */
|
84
|
+
"$JAVA_HOME/jre/lib/" CPU "/libjvm.so", /* Sun JDK */
|
85
|
+
"$JAVA_HOME/lib/" CPU "/classic/libjvm.so", /* Sun JRE 1.2 */
|
86
|
+
"$JAVA_HOME/lib/" CPU "/server/libjvm.so", /* Sun JRE 1.4 */
|
87
|
+
"$JAVA_HOME/lib/" CPU "/client/libjvm.so", /* Sun JRE 1.3 */
|
88
|
+
"$JAVA_HOME/lib/" CPU "/libjvm.so", /* Sun JRE */
|
89
|
+
"$JAVA_HOME/jre/bin/" CPU "/classic/libjvm.so", /* IBM JDK 1.3 */
|
90
|
+
"$JAVA_HOME/jre/bin/" CPU "/libjvm.so", /* IBM JDK */
|
91
|
+
"$JAVA_HOME/bin/" CPU "/classic/libjvm.so", /* IBM JRE 1.3 */
|
92
|
+
"$JAVA_HOME/bin/" CPU "/libjvm.so", /* IBM JRE */
|
93
|
+
/* Those are "weirdos: if we got here, we're probably in troubles and
|
94
|
+
* we're not going to find anything, but hope never dies...
|
95
|
+
*/
|
96
|
+
"$JAVA_HOME/jre/lib/" CPU "/classic/green_threads/libjvm.so",
|
97
|
+
#if defined(OSD_POSIX)
|
98
|
+
"$JAVA_HOME/lib/s390/client/green_threads/libjvm.so",
|
99
|
+
"$JAVA_HOME/lib/sparc/client/green_threads/libjvm.so",
|
100
|
+
#endif
|
101
|
+
"$JAVA_HOME/jre/lib/classic/libjvm.so",
|
102
|
+
"$JAVA_HOME/jre/lib/client/libjvm.so",
|
103
|
+
"$JAVA_HOME/jre/lib/libjvm.so",
|
104
|
+
"$JAVA_HOME/lib/classic/libjvm.so",
|
105
|
+
"$JAVA_HOME/lib/client/libjvm.so",
|
106
|
+
"$JAVA_HOME/lib/libjvm.so",
|
107
|
+
"$JAVA_HOME/jre/bin/classic/libjvm.so",
|
108
|
+
"$JAVA_HOME/jre/bin/client/libjvm.so",
|
109
|
+
"$JAVA_HOME/jre/bin/libjvm.so",
|
110
|
+
"$JAVA_HOME/bin/classic/libjvm.so",
|
111
|
+
"$JAVA_HOME/bin/client/libjvm.so",
|
112
|
+
"$JAVA_HOME/bin/libjvm.so",
|
113
|
+
"$JAVA_HOME/jre/lib/" CPU "/fast64/libjvm.so",
|
114
|
+
"$JAVA_HOME/jre/lib/" CPU "/fast32/libjvm.so",
|
115
|
+
"$JAVA_HOME/lib/" CPU "/fast64/libjvm.so",
|
116
|
+
"$JAVA_HOME/lib/" CPU "/fast32/libjvm.so",
|
117
|
+
#elif defined(OS_HPUX)
|
118
|
+
"$JAVA_HOME/jre/lib/" CPU "/server/libjvm." SO_EXT,
|
119
|
+
"$JAVA_HOME/jre/lib/" CPU "/client/libjvm." SO_EXT,
|
120
|
+
"$JAVA_HOME/jre/lib/" CPU "/hotspot/libjvm." SO_EXT,
|
121
|
+
"$JAVA_HOME/jre/lib/" CPU "/classic/libjvm." SO_EXT,
|
122
|
+
#endif
|
123
|
+
"/usr/lib/libgcj.so.7", /* gcc java libraries */
|
124
|
+
"/usr/lib/libgcj.so.6",
|
125
|
+
NULL,
|
126
|
+
};
|
127
|
+
|
128
|
+
/* This is the list of "configured" VM (searched when jvm.cfg is found, as
|
129
|
+
* in the case of most JDKs 1.3 (not IBM, for example), way easier than
|
130
|
+
* before, and lovely, indeed...
|
131
|
+
*/
|
132
|
+
char *location_jvm_configured[] = {
|
133
|
+
#if defined(OS_DARWIN)
|
134
|
+
"$JAVA_HOME/../Libraries/lib$VM_NAME.dylib",
|
135
|
+
#elif defined(OS_CYGWIN)
|
136
|
+
"$JAVA_HOME/jre/bin/$VM_NAME/jvm.dll", /* Sun JDK 1.3 */
|
137
|
+
#elif defined(OS_LINUX) || defined(OS_SOLARIS) || defined(OS_BSD) || defined(OS_FREEBSD) || defined(OS_TRU64)
|
138
|
+
"$JAVA_HOME/jre/lib/" CPU "/$VM_NAME/libjvm.so", /* Sun JDK 1.3 */
|
139
|
+
"$JAVA_HOME/lib/" CPU "/$VM_NAME/libjvm.so", /* Sun JRE 1.3 */
|
140
|
+
#elif defined(OS_HPUX)
|
141
|
+
"$JAVA_HOME/jre/lib/" CPU "/$VM_NAME/libjvm." SO_EXT,
|
142
|
+
"$JAVA_HOME/lib/" CPU "/$VM_NAME/libjvm." SO_EXT,
|
143
|
+
#elif defined(OS_SYSV)
|
144
|
+
"$JAVA_HOME/jre/lib/" CPU "/$VM_NAME/dce_threads/libjvm.so",
|
145
|
+
"$JAVA_HOME/jre/lib/" CPU "/$VM_NAME/green_threads/libjvm.so",
|
146
|
+
"$JAVA_HOME/lib/" CPU "/$VM_NAME/dce_threads/libjvm.so",
|
147
|
+
"$JAVA_HOME/lib/" CPU "/$VM_NAME/green_threads/libjvm.so",
|
148
|
+
#endif
|
149
|
+
NULL,
|
150
|
+
};
|
151
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
2
|
+
* contributor license agreements. See the NOTICE file distributed with
|
3
|
+
* this work for additional information regarding copyright ownership.
|
4
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
5
|
+
* (the "License"); you may not use this file except in compliance with
|
6
|
+
* the License. You may obtain a copy of the License at
|
7
|
+
*
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
*
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
* See the License for the specific language governing permissions and
|
14
|
+
* limitations under the License.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/* @version $Id: location.h 921765 2010-03-11 10:03:32Z mturk $ */
|
18
|
+
#ifndef __JSVC_LOCATION_H__
|
19
|
+
#define __JSVC_LOCATION_H__
|
20
|
+
|
21
|
+
#include "jsvc.h"
|
22
|
+
|
23
|
+
extern char *location_home[];
|
24
|
+
extern char *location_jvm_cfg[];
|
25
|
+
extern char *location_jvm_default[];
|
26
|
+
extern char *location_jvm_configured[];
|
27
|
+
|
28
|
+
#endif /* __JSVC_LOCATION_H__ */
|
29
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
2
|
+
* contributor license agreements. See the NOTICE file distributed with
|
3
|
+
* this work for additional information regarding copyright ownership.
|
4
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
5
|
+
* (the "License"); you may not use this file except in compliance with
|
6
|
+
* the License. You may obtain a copy of the License at
|
7
|
+
*
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
*
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
* See the License for the specific language governing permissions and
|
14
|
+
* limitations under the License.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/* @version $Id: java.c 909069 2010-02-11 16:43:36Z mturk $ */
|
18
|
+
|
19
|
+
/*
|
20
|
+
* as Cygwin does not support lockf, jsvc uses fcntl to emulate it.
|
21
|
+
*/
|
22
|
+
#ifdef OS_CYGWIN
|
23
|
+
#include "jsvc.h"
|
24
|
+
#include <sys/fcntl.h>
|
25
|
+
|
26
|
+
/*
|
27
|
+
* File locking routine
|
28
|
+
*/
|
29
|
+
int lockf(int fildes, int function, off_t size)
|
30
|
+
{
|
31
|
+
struct flock buf;
|
32
|
+
|
33
|
+
switch (function) {
|
34
|
+
case F_LOCK:
|
35
|
+
buf.l_type = F_WRLCK;
|
36
|
+
break;
|
37
|
+
case F_ULOCK:
|
38
|
+
buf.l_type = F_UNLCK;
|
39
|
+
break;
|
40
|
+
default:
|
41
|
+
return -1;
|
42
|
+
}
|
43
|
+
buf.l_whence = 0;
|
44
|
+
buf.l_start = 0;
|
45
|
+
buf.l_len = size;
|
46
|
+
|
47
|
+
return fcntl(fildes, F_SETLK, &buf);
|
48
|
+
}
|
49
|
+
#else
|
50
|
+
const char __unused_locks_c[] = __FILE__;
|
51
|
+
#endif
|
52
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
2
|
+
* contributor license agreements. See the NOTICE file distributed with
|
3
|
+
* this work for additional information regarding copyright ownership.
|
4
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
5
|
+
* (the "License"); you may not use this file except in compliance with
|
6
|
+
* the License. You may obtain a copy of the License at
|
7
|
+
*
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
*
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
* See the License for the specific language governing permissions and
|
14
|
+
* limitations under the License.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/* @version $Id$ */
|
18
|
+
#ifndef __JSVC_LOCKS_H__
|
19
|
+
#define __JSVC_LOCKS_H__
|
20
|
+
|
21
|
+
/*
|
22
|
+
* as Cygwin does not support locks, jsvc use NT API to emulate them.
|
23
|
+
*/
|
24
|
+
#ifdef OS_CYGWIN
|
25
|
+
|
26
|
+
#define F_ULOCK 0 /* Unlock a previously locked region */
|
27
|
+
#define F_LOCK 1 /* Lock a region for exclusive use */
|
28
|
+
|
29
|
+
/*
|
30
|
+
* allow a file to be locked
|
31
|
+
* @param fildes an open file descriptor
|
32
|
+
* @param function a control value that specifies the action to be taken
|
33
|
+
* @param size number of bytes to lock
|
34
|
+
* @return Zero on success, a value less than 0 if an error was encountered
|
35
|
+
*/
|
36
|
+
int lockf(int fildes, int function, off_t size);
|
37
|
+
|
38
|
+
#endif
|
39
|
+
#endif /* __JSVC_LOCKS_H__ */
|
40
|
+
|
@@ -0,0 +1,121 @@
|
|
1
|
+
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
2
|
+
* contributor license agreements. See the NOTICE file distributed with
|
3
|
+
* this work for additional information regarding copyright ownership.
|
4
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
5
|
+
* (the "License"); you may not use this file except in compliance with
|
6
|
+
* the License. You may obtain a copy of the License at
|
7
|
+
*
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
*
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
* See the License for the specific language governing permissions and
|
14
|
+
* limitations under the License.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/* @version $Id: replace.c 921765 2010-03-11 10:03:32Z mturk $ */
|
18
|
+
#include "jsvc.h"
|
19
|
+
|
20
|
+
/* Replace all occurrences of a string in another */
|
21
|
+
int replace(char *new, int len, char *old, char *mch, char *rpl)
|
22
|
+
{
|
23
|
+
char *tmp;
|
24
|
+
int count;
|
25
|
+
int shift;
|
26
|
+
int nlen;
|
27
|
+
int olen;
|
28
|
+
int mlen;
|
29
|
+
int rlen;
|
30
|
+
int x;
|
31
|
+
|
32
|
+
/* The new buffer is NULL, fail */
|
33
|
+
if (new == NULL)
|
34
|
+
return -1;
|
35
|
+
/* The length of the buffer is less than zero, fail */
|
36
|
+
if (len < 0)
|
37
|
+
return -2;
|
38
|
+
/* The old buffer is NULL, fail */
|
39
|
+
if (old == NULL)
|
40
|
+
return -3;
|
41
|
+
|
42
|
+
/* The string to be matched is NULL or empty, simply copy */
|
43
|
+
if ((mch == NULL) || (strlen(mch) == 0)) {
|
44
|
+
olen = strlen(old);
|
45
|
+
if (len <= olen)
|
46
|
+
return (olen + 1);
|
47
|
+
strcpy(new, old);
|
48
|
+
return 0;
|
49
|
+
}
|
50
|
+
|
51
|
+
/* The string to be replaced is NULL, assume it's an empty string */
|
52
|
+
if (rpl == NULL)
|
53
|
+
rpl = "";
|
54
|
+
|
55
|
+
/* Evaluate some lengths */
|
56
|
+
olen = strlen(old);
|
57
|
+
mlen = strlen(mch);
|
58
|
+
rlen = strlen(rpl);
|
59
|
+
|
60
|
+
/* Calculate how many times the mch string appears in old */
|
61
|
+
tmp = old;
|
62
|
+
count = 0;
|
63
|
+
while ((tmp = strstr(tmp, mch)) != NULL) {
|
64
|
+
count++;
|
65
|
+
tmp += mlen;
|
66
|
+
}
|
67
|
+
|
68
|
+
/* We have no matches, simply copy */
|
69
|
+
if (count == 0) {
|
70
|
+
olen = strlen(old);
|
71
|
+
if (len <= olen)
|
72
|
+
return (olen + 1);
|
73
|
+
strcpy(new, old);
|
74
|
+
return 0;
|
75
|
+
}
|
76
|
+
|
77
|
+
/* Calculate how big the buffer must be to hold the translation
|
78
|
+
* and of how many bytes we need to shift the data
|
79
|
+
*/
|
80
|
+
shift = rlen - mlen;
|
81
|
+
nlen = olen + (shift * count);
|
82
|
+
/* printf("Count=%d Shift= %d OLen=%d NLen=%d\n",count,shift,olen,nlen); */
|
83
|
+
|
84
|
+
/* Check if we have enough size in the buffer */
|
85
|
+
if (nlen >= len)
|
86
|
+
return (nlen + 1);
|
87
|
+
|
88
|
+
/* Copy over the old buffer in the new one (save memory) */
|
89
|
+
strcpy(new, old);
|
90
|
+
|
91
|
+
/* Start replacing */
|
92
|
+
tmp = new;
|
93
|
+
while ((tmp = strstr(tmp, mch)) != NULL) {
|
94
|
+
/* If shift is > 0 we need to move data from right to left */
|
95
|
+
if (shift > 0) {
|
96
|
+
for (x = (strlen(tmp) + shift); x > shift; x--) {
|
97
|
+
/*
|
98
|
+
printf("src %c(%d) dst %c(%d)\n",
|
99
|
+
tmp[x-shift],tmp[x-shift],tmp[x],tmp[x]);
|
100
|
+
*/
|
101
|
+
tmp[x] = tmp[x - shift];
|
102
|
+
}
|
103
|
+
/* If shift is < 0 we need to move data from left to right */
|
104
|
+
}
|
105
|
+
else if (shift < 0) {
|
106
|
+
for (x = mlen; x < strlen(tmp) - shift; x++) {
|
107
|
+
/*
|
108
|
+
printf("src %c(%d) dst %c(%d)\n",
|
109
|
+
tmp[x],tmp[x],tmp[x+shift],tmp[x+shift]);
|
110
|
+
*/
|
111
|
+
tmp[x + shift] = tmp[x];
|
112
|
+
}
|
113
|
+
}
|
114
|
+
/* If shift is = 0 we don't have to shift data */
|
115
|
+
strncpy(tmp, rpl, rlen);
|
116
|
+
tmp += rlen;
|
117
|
+
/* printf("\"%s\"\n",tmp); */
|
118
|
+
}
|
119
|
+
return 0;
|
120
|
+
}
|
121
|
+
|