--- shapetools-1.4pl6.orig/src/shape/macro.c
+++ shapetools-1.4pl6/src/shape/macro.c
@@ -6,7 +6,7 @@
*
* $Header: macro.c[8.0] Mon Jun 6 15:09:08 1994 axel@cs.tu-berlin.de frozen $
*/
-/* Thanks to Steve Emerson (steve@unidata.ucar.edu) for the
+/* Thanks to Steve Emerson (steve@unidata.ucar.edu) for the
reimplementation of expandmacro() */
#ifndef lint
@@ -40,7 +40,7 @@
register int j = 0;
int this_var;
struct vardef *this_vardef;
- char *p, *p1, *p2, *p3;
+ char *p = NULL, *p1, *p2, *p3;
char macro[2048];
static char retval[1024];
static char *BLUMENKOHL="bLuMeNkOhL";
@@ -48,7 +48,7 @@
retval[0] = '\0';
for (this_var = 0; this_var < variants_active; this_var++) {
-
+
this_vardef = &variantDefs[activeVariants[this_var]];
if (this_vardef->name) {
if ((!strcmp(name,"vpath")) && (this_vardef->vpath)) {
@@ -62,7 +62,7 @@
}
continue;
}
-
+
if ((!strcmp(name, "vflags")) && (this_vardef->vflags)) {
if (retval[0] == '\0') {
strcat(retval, this_vardef->vflags);
@@ -83,25 +83,25 @@
p1 = strchr(macro, '=');
p2 = strchr(macro, ' ');
p3 = strchr(macro, '\t');
-
+
if (p2 == NIL)
p2 = p1 + 1;
-
+
if (p3 == NIL)
p3 = p1 + 1;
-
+
if (( p1 < p2) && (p1 < p3))
p = p1;
if (( p2 < p1) && (p2 < p3))
p = p2;
if (( p3 < p2) && (p3 < p1))
p = p3;
-
+
*p = '\0';
p1++;
if (!strcmp(name, macro)) {
-
+
while((*p1 == ' ') || (*p1 == '\t') || (*p1 == '='))
p1++;
if(retval[0] == '\0') {
@@ -130,12 +130,12 @@
register int i;
p = string;
i = 0;
-
+
if (result_str)
result_str = check_realloc (result_str, (unsigned)(strlen (string)+1));
else
result_str = check_malloc ((unsigned)(strlen (string)+1));
-
+
while (p && *p && isspace(*p))
p++;
while (*p) {
@@ -172,7 +172,7 @@
int result_size = 0, result_bytes = 0, nbytes = 0;
y = check_strdup (str);
- if (strchr (str, '$'))
+ if (strchr (str, '$'))
x = check_strdup (expandmacro (y));
else
x = check_strdup (y);
@@ -181,7 +181,7 @@
i = 0;
y = check_realloc (y, strlen (x) + 1);
-
+
while (x[i]) {
if((x[i] == '$') && (x[i+1] == '$'))
i++;
@@ -208,7 +208,7 @@
} while (!feof (fd));
*end_result = '\0';
-
+
pclose (fd);
free (x);
free (y);
@@ -290,12 +290,12 @@
char *original;
{
/*
- * write the string, pointed to by original backwards into a
- * buffer that is allocated on the heap. A pointer to the
+ * write the string, pointed to by original backwards into a
+ * buffer that is allocated on the heap. A pointer to the
* buffer is returned. It's the responsibility of the caller
* to free the associated memory.
- * If original is empty or a NULL pointer, a NULL pointer is
- * returned.
+ * If original is empty or a NULL pointer, a NULL pointer is
+ * returned.
*/
register int i, j;
@@ -314,37 +314,40 @@
return lanigiro;
}
-
+
LOCAL char *substitute_string (subject, old, new)
char *subject, *old, *new; {
/*
* replace all occurrences of substring "old" in each token in
- * "subject" by string "new". Return a pointer to a statically
- * allocated string that is the result of this operation.
+ * "subject" by string "new". Return a pointer to a dynamically
+ * allocated string that is the result of this operation.
*/
- int subject_len, result_len, old_len, new_len;
+ int subject_len, result_len, old_len, new_len, growth;
char *result, *dlo, *wen, *tecjbus, *tluser;
register char *p, *s;
register int i, j;
- if (subject == NULL)
- return "";
+ if (subject == NULL) {
+ result = check_malloc(1);
+ *result = '\0';
+ return result;
+ }
else
subject_len = strlen (subject);
old_len = old ? strlen (old) : 0;
new_len = new ? strlen (new) : 0;
+ growth = new_len - old_len;
result_len = subject_len + 1;
if ((old_len == 0) || (old_len > subject_len)) {
- strcpy (result, subject);
- return result;
+ return check_strdup(subject);
}
- /*
+ /*
* mirror all participating strings. The corresponding
* pointer variables have the names of their originals backwards,
* i.e. old -> dlo etc.
@@ -368,19 +371,20 @@
i = 0;
while (*s && (*(p+i) == *s)) { s++; i++; }
- if (j+new_len >= result_len) {
- if ((tluser = (char *)realloc (tluser, j+new_len+subject_len) )
- == (char *)NULL)
- errexit (10, "realloc");
- result_len = j+new_len+subject_len;
- }
-
if (*s) {
tluser[j++] = *p++;
}
else {
char *oldp;
+ /* Match found. Make room for substitution. */
+ if (growth > 0) {
+ if ((tluser = (char *)realloc (tluser, result_len + growth))
+ == (char *)NULL)
+ errexit (10, "realloc");
+ result_len += growth;
+ }
+
p += old_len;
oldp = p; /* save position. the skipped text must be appended.. */
@@ -407,15 +411,15 @@
}
-LOCAL Bool chk_strsub (subject, old, new)
+LOCAL Bool chk_strsub (subject, old, new)
char *subject, **old, **new; {
/*
* Check if the string "subject" is of the form "str1=str2" where
* str1 must be non-empty. If "subject" does have this form,
- * allocate space sufficient to hold "str1" and "str2"
+ * allocate space sufficient to hold "str1" and "str2"
* respectively. Copy str1 and str2 to these memory areas and
* assign "old" to "str1" and "new" to "str2". Return TRUE.
- * If "subject doesn't match this form, leave the outbound
+ * If "subject doesn't match this form, leave the outbound
* parameters "old" and "new" untouched. Return FALSE.
*/
@@ -498,7 +502,7 @@
while (*lptr) lptr++;
if (lptr > value) lptr--;
while (*lptr && isspace (*lptr)) lptr--;
- if (*lptr)
+ if (*lptr)
*(lptr+1) = '\0';
if (!strcmp(name,"RECDEPTH")) {
@@ -637,7 +641,7 @@
*/
char *start = p + 2;
char klauf = p[1];
- char klazu = p[1] == LEFT_PAREN
+ char klazu = p[1] == LEFT_PAREN
? RIGHT_PAREN
: RIGHT_BRACE;
@@ -650,13 +654,12 @@
if (sbcpy(name, start, p-start) == 0)
errexit(10, "sbcpy");
- if (strcmp(sbstr(name), "SHAPE") == 0 ||
+ if (strcmp(sbstr(name), "SHAPE") == 0 ||
strcmp(sbstr(name), "MAKE") == 0) {
shape_command = TRUE;
}
if (*p == ':') {
- register char *closer;
int level = 1;
base = p + 1;
while (*p && level) {
@@ -694,22 +697,22 @@
if (curvar[0] != 0) {
char *get_variant_macro();
variant_macro = get_variant_macro(sbstr(name));
-
+
if (strcmp(variant_macro, "bLuMeNkOhL") != 0) {
char *mist = expandmacro(variant_macro);
-
+
if (string_substitution) {
char *cooked_string;
-
- cooked_string =
+
+ cooked_string =
substitute_string (mist, old, new);
- if (sbcat (mac_buf, cooked_string,
+ if (sbcat (mac_buf, cooked_string,
strlen (cooked_string)) == 0)
errexit (10, "sbcat");
if (cooked_string) free (cooked_string);
}
else {
- if (sbcat (mac_buf, mist,
+ if (sbcat (mac_buf, mist,
strlen (mist)) == 0)
errexit (10, "sbcat");
}
@@ -717,24 +720,24 @@
}
if (variant_macro == 0 || !strcmp(variant_macro,"bLuMeNkOhL")){
char *value, *expandedValue;
-
+
if (!visitedHash (sbstr(name))) {
if ((value = getHash (sbstr(name)))) {
expandedValue = expandmacro (value);
clearHashVisited (sbstr(name));
-
+
if (string_substitution) {
char *cooked_string;
- cooked_string =
+ cooked_string =
substitute_string (expandedValue, old, new);
- if (sbcat (mac_buf, cooked_string,
+ if (sbcat (mac_buf, cooked_string,
strlen (cooked_string)) == 0)
errexit (10, "sbcat");
if (cooked_string) free (cooked_string);
}
else {
- if (sbcat(mac_buf, expandedValue,
+ if (sbcat(mac_buf, expandedValue,
strlen(expandedValue)) == 0)
errexit(10, "sbcat");
}