/** 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 #include #include #include #include #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; }