char *eval_prompt(char *prompt) { sub_env(prompt); // Flag to indicate inside {...} block int in_macro = 0; // Counters to track lengths int prompt_len = strlen(prompt); // Buffers char macro[prompt_len]; *macro -= *macro; char *new_prompt = calloc(1, 128); *new_prompt -= *new_prompt; int i = 0; while (i < prompt_len) { if (prompt[i] == '\\' && prompt[i - 1] != '\\') { i++; continue; } if (prompt[i] == '{' && prompt[i - 1] != '\\') { in_macro = 1; } else if (prompt[i] == '}' && prompt[i - 1] != '\\') { if (in_macro) { in_macro = 0; eval_macro(macro, new_prompt); strcpy(macro, ""); } else { strncat(new_prompt, &prompt[i], 1); } } else { if (in_macro) { strncat(macro, &prompt[i], 1); } else { strncat(new_prompt, &prompt[i], 1); } } i++; } if (in_macro && strlen(macro)) { eval_macro(macro, new_prompt); } char *copy = calloc(1, strlen(new_prompt) + 1); strcpy(copy, new_prompt); free(new_prompt); return copy; }
void print_prompt(char *prompt) { // Flag to indicate inside @...@ block int in_macro = 0; // Counters to track lengths int prompt_len = strlen(prompt); int macro_len = 0; int new_prompt_len = 0; // Buffers char macro[prompt_len]; char new_prompt[prompt_len]; strcpy(macro, ""); strcpy(new_prompt, ""); // Loop counter int i = 0; while (i < prompt_len) { // Either start or end a macro if (prompt[i] == '@') { // If start, set flag if (!in_macro) in_macro = 1; // If end, ... else { // Unset flag in_macro = 0; // Evaluate the macro (substitute it in) eval_macro(macro, new_prompt); // Reset macro length and buffer strcpy(macro, ""); } // If not '@', either add to macro buffer... } else if (in_macro) { strncat(macro, &prompt[i], 1); // or just add current character to new prompt } else { strncat(new_prompt, &prompt[i], 1); } i++; } printf("%s", new_prompt); }
static Az_PRF eval_macro(const AzPmat *m_p, const AzPmat *m_y, /* 0,0,1,0,...0 */ double th) { AzDvect v_th(m_p->rowNum()); v_th.set(th); return eval_macro(m_p, m_y, &v_th); }