summaryrefslogtreecommitdiffstats
path: root/ldap/admin/src/script-gen.c
diff options
context:
space:
mode:
authorcvsadm <cvsadm>2005-01-21 00:44:34 +0000
committercvsadm <cvsadm>2005-01-21 00:44:34 +0000
commitb2093e3016027d6b5cf06b3f91f30769bfc099e2 (patch)
treecf58939393a9032182c4fbc4441164a9456e82f8 /ldap/admin/src/script-gen.c
downloadds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.tar.gz
ds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.tar.xz
ds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.zip
Moving NSCP Directory Server from DirectoryBranch to TRUNK, initial drop. (foxworth)ldapserver7x
Diffstat (limited to 'ldap/admin/src/script-gen.c')
-rw-r--r--ldap/admin/src/script-gen.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/ldap/admin/src/script-gen.c b/ldap/admin/src/script-gen.c
new file mode 100644
index 00000000..e2caab4c
--- /dev/null
+++ b/ldap/admin/src/script-gen.c
@@ -0,0 +1,107 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * 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"
+
+
+/* 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;
+}