char * expand_argument (const char *str, const char *end) { char *tmp, *alloc = NULL; char *r; if (str == end) return xstrdup(""); if (!end || *end == '\0') return allocated_variable_expand (str); if (end - str + 1 > 1000) tmp = alloc = xmalloc (end - str + 1); else tmp = alloca (end - str + 1); memcpy (tmp, str, end - str); tmp[end - str] = '\0'; r = allocated_variable_expand (tmp); if (alloc) free (alloc); return r; }
char * expand_argument (const char *str, const char *end) { #ifndef CONFIG_WITH_VALUE_LENGTH char *tmp, *alloc = NULL; char *r; #endif if (str == end) return xstrdup(""); #ifndef CONFIG_WITH_VALUE_LENGTH if (!end || *end == '\0') return allocated_variable_expand (str); if (end - str + 1 > 1000) tmp = alloc = xmalloc (end - str + 1); else tmp = alloca (end - str + 1); memcpy (tmp, str, end - str); tmp[end - str] = '\0'; r = allocated_variable_expand (tmp); if (alloc) free (alloc); return r; #else /* CONFIG_WITH_VALUE_LENGTH */ if (!end) return allocated_variable_expand_2 (str, ~0U, NULL); return allocated_variable_expand_2 (str, end - str, NULL); #endif /* CONFIG_WITH_VALUE_LENGTH */ }
char * recursively_expand_for_file (struct variable *v, struct file *file) { char *value; const struct floc *this_var; const struct floc **saved_varp; struct variable_set_list *save = 0; int set_reading = 0; /* Don't install a new location if this location is empty. This can happen for command-line variables, builtin variables, etc. */ saved_varp = expanding_var; if (v->fileinfo.filenm) { this_var = &v->fileinfo; expanding_var = &this_var; } /* If we have no other file-reading context, use the variable's context. */ if (!reading_file) { set_reading = 1; reading_file = &v->fileinfo; } if (v->expanding) { if (!v->exp_count) /* Expanding V causes infinite recursion. Lose. */ fatal (*expanding_var, _("Recursive variable `%s' references itself (eventually)"), v->name); --v->exp_count; } if (file) { save = current_variable_set_list; current_variable_set_list = file->variables; } v->expanding = 1; if (v->append) value = allocated_variable_append (v); else value = allocated_variable_expand (v->value); v->expanding = 0; if (set_reading) reading_file = 0; if (file) current_variable_set_list = save; expanding_var = saved_varp; return value; }
/* Perform the GNU make expansion function. */ static SCM guile_expand_wrapper (SCM obj) { char *str = cvt_scm_to_str (obj); SCM ret; char *res; DB (DB_BASIC, (_("guile: Expanding '%s'\n"), str)); res = allocated_variable_expand (str); ret = scm_from_locale_string (res); free (str); free (res); return ret; }
/** * Appends text to a textfile, creating the textfile if necessary. */ int kmk_builtin_append(int argc, char **argv, char **envp) { int i; int fFirst; int iFile; FILE *pFile; int fNewline = 0; int fNoTrailingNewline = 0; int fTruncate = 0; int fDefine = 0; int fVariables = 0; int fCommands = 0; g_progname = argv[0]; /* * Parse options. */ i = 1; while (i < argc && argv[i][0] == '-' && argv[i][1] != '\0' /* '-' is a file */ && strchr("-cdnNtv", argv[i][1]) /* valid option char */ ) { char *psz = &argv[i][1]; if (*psz != '-') { do { switch (*psz) { case 'c': if (fVariables) { errx(1, "Option '-c' clashes with '-v'."); return usage(stderr); } #ifndef kmk_builtin_append fCommands = 1; break; #else errx(1, "Option '-c' isn't supported in external mode."); return usage(stderr); #endif case 'd': if (fVariables) { errx(1, "Option '-d' must come before '-v'!"); return usage(stderr); } fDefine = 1; break; case 'n': fNewline = 1; break; case 'N': fNoTrailingNewline = 1; break; case 't': fTruncate = 1; break; case 'v': if (fCommands) { errx(1, "Option '-v' clashes with '-c'."); return usage(stderr); } #ifndef kmk_builtin_append fVariables = 1; break; #else errx(1, "Option '-v' isn't supported in external mode."); return usage(stderr); #endif default: errx(1, "Invalid option '%c'! (%s)", *psz, argv[i]); return usage(stderr); } } while (*++psz); } else if (!strcmp(psz, "-help")) { usage(stdout); return 0; } else if (!strcmp(psz, "-version")) return kbuild_version(argv[0]); else break; i++; } if (i + fDefine >= argc) { if (i <= argc) errx(1, "missing filename!"); else errx(1, "missing define name!"); return usage(stderr); } /* * Open the output file. */ iFile = i; pFile = fopen(argv[i], fTruncate ? "w" : "a"); if (!pFile) return err(1, "failed to open '%s'", argv[i]); /* * Start define? */ if (fDefine) { i++; fprintf(pFile, "define %s\n", argv[i]); } /* * Append the argument strings to the file */ fFirst = 1; for (i++; i < argc; i++) { const char *psz = argv[i]; size_t cch = strlen(psz); if (!fFirst) fputc(fNewline ? '\n' : ' ', pFile); #ifndef kmk_builtin_append if (fCommands) { char *pszOldBuf; unsigned cchOldBuf; char *pchEnd; install_variable_buffer(&pszOldBuf, &cchOldBuf); pchEnd = func_commands(variable_buffer, &argv[i], "commands"); fwrite(variable_buffer, 1, pchEnd - variable_buffer, pFile); restore_variable_buffer(pszOldBuf, cchOldBuf); } else if (fVariables) { struct variable *pVar = lookup_variable(psz, cch); if (!pVar) continue; if ( !pVar->recursive || IS_VARIABLE_RECURSIVE_WITHOUT_DOLLAR(pVar)) fwrite(pVar->value, 1, pVar->value_length, pFile); else { char *pszExpanded = allocated_variable_expand(pVar->value); fwrite(pszExpanded, 1, strlen(pszExpanded), pFile); free(pszExpanded); } } else #endif fwrite(psz, 1, cch, pFile); fFirst = 0; } /* * End the define? */ if (fDefine) { if (fFirst) fwrite("\nendef", 1, sizeof("\nendef") - 1, pFile); else fwrite("endef", 1, sizeof("endef") - 1, pFile); } /* * Add the final newline (unless supressed) and close the file. */ if ( ( !fNoTrailingNewline && fputc('\n', pFile) == EOF) || ferror(pFile)) { fclose(pFile); return errx(1, "error writing to '%s'!", argv[iFile]); } if (fclose(pFile)) return err(1, "failed to fclose '%s'!", argv[iFile]); return 0; }
/* Expand a string and return an allocated buffer. Caller must call gmk_free() with this buffer. */ char * gmk_expand (const char *ref) { return allocated_variable_expand (ref); }