1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK **/
/*
* this is used for generating the (large) scripts during create_instance.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "portable.h"
#if defined( XP_WIN32 )
#include <io.h>
#endif
/* reads the file on inpath, and rewrites it on outpath.
* 'table' is a list of string-pairs (terminated by a pair of NULLs) that
* indicate substitution pairs. for example, the pair:
* "SERVER-ROOT", "/export/home/slapd-bastille"
* means to substitute any occurance of "{{SERVER-ROOT}}" in the file with
* "/export/home/slapd-bastille".
*
* returns 0 on success, -1 if it had trouble opening or reading/writing
* the two files.
*/
#define GS_BUFLEN 256
int generate_script(const char *inpath, const char *outpath, int mode,
const char *table[][2])
{
FILE *fin, *fout;
char buffer[GS_BUFLEN], save_buffer[GS_BUFLEN];
char *p, *q;
int i;
fin = fopen(inpath, "r");
if (fin == NULL) {
return -1;
}
fout = fopen(outpath, "w");
if (fout == NULL) {
fclose(fin);
return -1;
}
while (!feof(fin)) {
fgets(buffer, GS_BUFLEN, fin);
if (feof(fin)) {
break;
}
buffer[GS_BUFLEN-1] = 0;
if (buffer[strlen(buffer)-1] == '\n') {
buffer[strlen(buffer)-1] = 0;
}
if (buffer[strlen(buffer)-1] == '\r') {
buffer[strlen(buffer)-1] = 0;
}
p = buffer;
while ((p = strstr(p, "{{")) != NULL) {
q = strstr(p+2, "}}");
if (q == NULL) {
/* skip this one then */
p += 2;
continue;
}
/* key between {{ }} is now in [p+2, q-1] */
for (i = 0; table[i][0] != NULL; i++) {
if ((strlen(table[i][0]) == (q-(p+2))) &&
(strncasecmp(table[i][0], p+2, q-(p+2)) == 0)) {
/* match! ...but is there room for the subtitution? */
int extra = strlen(table[i][1]) - (q+2-p);
if (strlen(buffer) + extra > GS_BUFLEN-1) {
/* not enough room, scratch it */
continue;
}
strcpy(save_buffer, q+2);
strcpy(p, table[i][1]);
strcat(p, save_buffer);
q = p;
break; /* out of the for loop */
}
}
/* move on... */
p = q;
}
fprintf(fout, "%s\n", buffer);
}
#if defined( XP_UNIX )
fchmod(fileno(fout), mode);
#endif
fclose(fin);
fclose(fout);
#if defined( XP_WIN32 )
chmod(outpath, mode);
#endif
return 0;
}
|