void set_prompt(int to, Source *s) { cur_prompt = (uint8_t)to; switch (to) { /* command */ case PS1: /* * Substitute ! and !! here, before substitutions are done * so ! in expanded variables are not expanded. * NOTE: this is not what AT&T ksh does (it does it after * substitutions, POSIX doesn't say which is to be done. */ { struct shf *shf; char * volatile ps1; Area *saved_atemp; int saved_lineno; ps1 = str_val(global("PS1")); shf = shf_sopen(NULL, strlen(ps1) * 2, SHF_WR | SHF_DYNAMIC, NULL); while (*ps1) if (*ps1 != '!' || *++ps1 == '!') shf_putchar(*ps1++, shf); else shf_fprintf(shf, "%lu", s ? (unsigned long)s->line + 1 : 0UL); ps1 = shf_sclose(shf); saved_lineno = current_lineno; if (s) current_lineno = s->line + 1; saved_atemp = ATEMP; newenv(E_ERRH); if (kshsetjmp(e->jbuf)) { prompt = safe_prompt; /* * Don't print an error - assume it has already * been printed. Reason is we may have forked * to run a command and the child may be * unwinding its stack through this code as it * exits. */ } else { char *cp = substitute(ps1, 0); strdupx(prompt, cp, saved_atemp); } current_lineno = saved_lineno; quitenv(NULL); } break; /* command continuation */ case PS2: prompt = str_val(global("PS2")); break; } }
char * shf_smprintf(const char *fmt, ...) { struct shf shf; va_list args; shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf); va_start(args, fmt); shf_vfprintf(&shf, fmt, args); va_end(args); return shf_sclose(&shf); /* null terminates */ }
char * snptreef(char *s, int n, const char *fmt, ...) { va_list va; struct shf shf; shf_sopen(s, n, SHF_WR | (s ? 0 : SHF_DYNAMIC), &shf); va_start(va, fmt); vfptreef(&shf, 0, fmt, va); va_end(va); return shf_sclose(&shf); /* null terminates */ }
int shf_snprintf(char *buf, int bsize, const char *fmt, ...) { struct shf shf; va_list args; int n; if (!buf || bsize <= 0) internal_errorf(1, "shf_snprintf: buf %lx, bsize %d", (long) buf, bsize); shf_sopen(buf, bsize, SHF_WR, &shf); va_start(args, fmt); n = shf_vfprintf(&shf, fmt, args); va_end(args); shf_sclose(&shf); /* null terminates */ return n; }