2 * tc.prompt.c: Prompt printing stuff
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * kfk 21oct1983 -- add @ (time) and / ($cwd) in prompt.
38 * PWP 4/27/87 -- rearange for tcsh.
39 * mrdch@com.tau.edu.il 6/26/89 - added ~, T and .# - rearanged to switch()
40 * instead of if/elseif
41 * Luke Mewburn, <lukem@cs.rmit.edu.au>
42 * 6-Sep-91 changed date format
43 * 16-Feb-94 rewrote directory prompt code, added $ellipsis
44 * 29-Dec-96 added rprompt support
47 static const char *month_list[12];
48 static const char *day_list[7];
56 setlocale(LC_TIME, "");
58 for (i = 0; i < 12; i++)
59 xfree((ptr_t) month_list[i]);
60 month_list[0] = strsave(_time_info->abbrev_month[0]);
61 month_list[1] = strsave(_time_info->abbrev_month[1]);
62 month_list[2] = strsave(_time_info->abbrev_month[2]);
63 month_list[3] = strsave(_time_info->abbrev_month[3]);
64 month_list[4] = strsave(_time_info->abbrev_month[4]);
65 month_list[5] = strsave(_time_info->abbrev_month[5]);
66 month_list[6] = strsave(_time_info->abbrev_month[6]);
67 month_list[7] = strsave(_time_info->abbrev_month[7]);
68 month_list[8] = strsave(_time_info->abbrev_month[8]);
69 month_list[9] = strsave(_time_info->abbrev_month[9]);
70 month_list[10] = strsave(_time_info->abbrev_month[10]);
71 month_list[11] = strsave(_time_info->abbrev_month[11]);
73 for (i = 0; i < 7; i++)
74 xfree((ptr_t) day_list[i]);
75 day_list[0] = strsave(_time_info->abbrev_wkday[0]);
76 day_list[1] = strsave(_time_info->abbrev_wkday[1]);
77 day_list[2] = strsave(_time_info->abbrev_wkday[2]);
78 day_list[3] = strsave(_time_info->abbrev_wkday[3]);
79 day_list[4] = strsave(_time_info->abbrev_wkday[4]);
80 day_list[5] = strsave(_time_info->abbrev_wkday[5]);
81 day_list[6] = strsave(_time_info->abbrev_wkday[6]);
83 month_list[0] = "Jan";
84 month_list[1] = "Feb";
85 month_list[2] = "Mar";
86 month_list[3] = "Apr";
87 month_list[4] = "May";
88 month_list[5] = "Jun";
89 month_list[6] = "Jul";
90 month_list[7] = "Aug";
91 month_list[8] = "Sep";
92 month_list[9] = "Oct";
93 month_list[10] = "Nov";
94 month_list[11] = "Dec";
107 printprompt(int promptno, const char *str)
109 static const Char *ocp = NULL;
110 static const char *ostr = NULL;
111 time_t lclock = time(NULL);
117 cp = varval(STRprompt);
120 cp = varval(STRprompt2);
123 cp = varval(STRprompt3);
131 cp = varval(STRprompt);
142 Prompt = tprintf(FMT_PROMPT, cp, str, lclock, NULL);
144 for (cp = Prompt; *cp ; )
145 (void) putwraw(*cp++);
152 if (promptno == 0) { /* determine rprompt if using main prompt */
153 cp = varval(STRrprompt);
154 RPrompt = tprintf(FMT_PROMPT, cp, NULL, lclock, NULL);
155 /* if not editing, put rprompt after prompt */
156 if (!editing && RPrompt[0] != '\0') {
157 for (cp = RPrompt; *cp ; )
158 (void) putwraw(*cp++);
167 tprintf_append_mbs(struct Strbuf *buf, const char *mbs, Char attributes)
172 mbs += one_mbtowc(&wc, mbs, MB_LEN_MAX);
173 Strbuf_append1(buf, wc | attributes);
178 tprintf(int what, const Char *fmt, const char *str, time_t tim, ptr_t info)
180 struct Strbuf buf = Strbuf_INIT;
183 static int print_prompt_did_ding = 0;
187 const Char *cp = fmt;
189 struct tm *t = localtime(&tim);
192 static Char *olduser = NULL;
196 cleanup_push(&buf, Strbuf_cleanup);
198 if ((*cp == '%') && ! (cp[1] == '\0')) {
202 if (what == FMT_HISTORY) {
203 cz = fmthist('R', info);
204 tprintf_append_mbs(&buf, cz, attributes);
208 tprintf_append_mbs(&buf, str, attributes);
213 /* Check for being member of the Administrators group */
215 gid_t grps[NGROUPS_MAX];
218 gcnt = getgroups(NGROUPS_MAX, grps);
219 # define DOMAIN_GROUP_RID_ADMINS 544
220 for (grp = 0; grp < gcnt; ++grp)
221 if (grps[grp] == DOMAIN_GROUP_RID_ADMINS)
223 Scp = (grp < gcnt) ? PRCHROOT : PRCH;
226 Scp = (uid == 0 || euid == 0) ? PRCHROOT : PRCH;
229 Strbuf_append1(&buf, attributes | Scp);
235 cz = fmthist('h', info);
238 cz = xasprintf("%d", *(int *)info);
241 cz = xasprintf("%d", eventno + 1);
244 tprintf_append_mbs(&buf, cz, attributes);
247 case 'T': /* 24 hour format */
249 case 't': /* 12 hour am/pm format */
250 case 'p': /* With seconds */
256 /* addition by Hans J. Albertsson */
257 /* and another adapted from Justin Bur */
258 if (adrof(STRampm) || (*cp != 'T' && *cp != 'P')) {
266 } /* else do a 24 hour clock */
268 /* "DING!" stuff by Hans also */
269 if (t->tm_min || print_prompt_did_ding ||
270 what != FMT_PROMPT || adrof(STRnoding)) {
272 print_prompt_did_ding = 0;
274 * Pad hour to 2 characters if padhour is set,
275 * by ADAM David Alan Martin
277 p = Itoa(hr, adrof(STRpadhour) ? 2 : 0, attributes);
278 Strbuf_append(&buf, p);
280 Strbuf_append1(&buf, attributes | ':');
281 p = Itoa(t->tm_min, 2, attributes);
282 Strbuf_append(&buf, p);
284 if (*cp == 'p' || *cp == 'P') {
285 Strbuf_append1(&buf, attributes | ':');
286 p = Itoa(t->tm_sec, 2, attributes);
287 Strbuf_append(&buf, p);
290 if (adrof(STRampm) || (*cp != 'T' && *cp != 'P')) {
291 Strbuf_append1(&buf, attributes | ampm);
292 Strbuf_append1(&buf, attributes | 'm');
295 else { /* we need to ding */
298 for (i = 0; STRDING[i] != 0; i++)
299 Strbuf_append1(&buf, attributes | STRDING[i]);
300 print_prompt_did_ding = 1;
308 cz = who_info(info, 'M');
310 #endif /* HAVENOUTMP */
313 * Bug pointed out by Laurent Dami <dami@cui.unige.ch>: don't
314 * derefrence that NULL (if HOST is not set)...
317 tprintf_append_mbs(&buf, cz, attributes);
326 scz = cz = who_info(info, 'm');
328 #endif /* HAVENOUTMP */
332 while (*cz != 0 && (what == FMT_WHO || *cz != '.')) {
335 cz += one_mbtowc(&wc, cz, MB_LEN_MAX);
336 Strbuf_append1(&buf, wc | attributes);
343 /* lukem: new directory prompt code */
350 if (Scp == 'c') /* store format type (c == .) */
352 if ((z = varval(STRcwd)) == STRNULL)
353 break; /* no cwd, so don't do anything */
355 /* show ~ whenever possible - a la dirs */
356 if (Scp == '~' || Scp == '.' ) {
357 static Char *olddir = NULL;
359 if (tlength == 0 || olddir != z) {
360 olddir = z; /* have we changed dir? */
361 olduser = getusername(&olddir);
368 /* option to determine fixed # of dirs from path */
369 if (Scp == '.' || Scp == 'C') {
374 Strbuf_append1(&buf, attributes | *z++);
375 Strbuf_append1(&buf, attributes | *z++);
377 if (*z == '/' && z[1] == '/') {
378 Strbuf_append1(&buf, attributes | *z++);
379 Strbuf_append1(&buf, attributes | *z++);
381 Strbuf_append1(&buf, attributes | *z++);
384 #endif /* WINNT_NATIVE */
386 while (*z) /* calc # of /'s */
392 * for format type c, prompt will be following...
395 * //machine/share => //machine/share
396 * //machine/share/folder => //machine:folder
398 if (oldz[0] == '/' && oldz[1] == '/' && updirs > 1)
399 Strbuf_append1(&buf, attributes | ':');
400 #endif /* WINNT_NATIVE */
401 if ((Scp == 'C' && *q != '/'))
404 if (cp[1] == '0') { /* print <x> or ... */
408 if (cp[1] >= '1' && cp[1] <= '9') { /* calc # to skip */
417 while ((z > q) && (*z != '/'))
422 if (*z == '/' && z != q)
427 if ((olduser) && ((Scp == '~') ||
428 (Scp == '.' && (pdirs || (!pdirs && updirs <= 0))) )) {
429 Strbuf_append1(&buf, attributes | '~');
430 for (q = olduser; *q; q++)
431 Strbuf_append1(&buf, attributes | *q);
434 /* RWM - tell you how many dirs we've ignored */
435 /* and add '/' at front of this */
436 if (updirs > 0 && pdirs) {
437 if (adrof(STRellipsis)) {
438 Strbuf_append1(&buf, attributes | '.');
439 Strbuf_append1(&buf, attributes | '.');
440 Strbuf_append1(&buf, attributes | '.');
442 Strbuf_append1(&buf, attributes | '/');
443 Strbuf_append1(&buf, attributes | '<');
445 Strbuf_append1(&buf, attributes | '9');
446 Strbuf_append1(&buf, attributes | '+');
448 Strbuf_append1(&buf, attributes | ('0' + updirs));
449 Strbuf_append1(&buf, attributes | '>');
454 Strbuf_append1(&buf, attributes | *z++);
456 /* lukem: end of new directory prompt code */
460 if (what == FMT_WHO) {
461 cz = who_info(info, 'n');
462 tprintf_append_mbs(&buf, cz, attributes);
466 #endif /* HAVENOUTMP */
468 if ((z = varval(STRuser)) != STRNULL)
470 Strbuf_append1(&buf, attributes | *z++);
474 if ((z = varval(STReuser)) != STRNULL)
476 Strbuf_append1(&buf, attributes | *z++);
480 if (what == FMT_WHO) {
481 cz = who_info(info, 'l');
482 tprintf_append_mbs(&buf, cz, attributes);
486 #endif /* HAVENOUTMP */
488 if ((z = varval(STRtty)) != STRNULL)
490 Strbuf_append1(&buf, attributes | *z++);
494 tprintf_append_mbs(&buf, day_list[t->tm_wday], attributes);
497 p = Itoa(t->tm_mday, 2, attributes);
498 Strbuf_append(&buf, p);
502 tprintf_append_mbs(&buf, month_list[t->tm_mon], attributes);
505 p = Itoa(t->tm_mon + 1, 2, attributes);
506 Strbuf_append(&buf, p);
510 p = Itoa(t->tm_year % 100, 2, attributes);
511 Strbuf_append(&buf, p);
515 p = Itoa(t->tm_year + 1900, 4, attributes);
516 Strbuf_append(&buf, p);
519 case 'S': /* start standout */
520 attributes |= STANDOUT;
522 case 'B': /* start bold */
525 case 'U': /* start underline */
528 case 's': /* end standout */
529 attributes &= ~STANDOUT;
531 case 'b': /* end bold */
534 case 'u': /* end underline */
535 attributes &= ~UNDER;
546 for (pp = proclist.p_next; pp; pp = pp->p_next)
550 p = Itoa(njobs, 1, attributes);
551 Strbuf_append(&buf, p);
556 if ((z = varval(STRstatus)) != STRNULL)
558 Strbuf_append1(&buf, attributes | *z++);
561 expdollar(&buf, &cp, attributes);
562 /* cp should point the last char of current % sequence */
566 Strbuf_append1(&buf, attributes | '%');
568 case '{': /* literal characters start */
571 * No literal capability, so skip all chars in the literal
574 while (*cp != '\0' && (cp[-1] != '%' || *cp != '}'))
576 #endif /* LITERAL == 0 */
577 attributes |= LITERAL;
579 case '}': /* literal characters end */
580 attributes &= ~LITERAL;
584 if (*cp == 'a' && what == FMT_WHO) {
585 cz = who_info(info, 'a');
586 tprintf_append_mbs(&buf, cz, attributes);
590 #endif /* HAVENOUTMP */
592 Strbuf_append1(&buf, attributes | '%');
593 Strbuf_append1(&buf, attributes | *cp);
598 else if (*cp == '\\' || *cp == '^')
599 Strbuf_append1(&buf, attributes | parseescape(&cp));
600 else if (*cp == HIST) { /* EGS: handle '!'s in prompts */
601 if (what == FMT_HISTORY)
602 cz = fmthist('h', info);
604 cz = xasprintf("%d", eventno + 1);
605 tprintf_append_mbs(&buf, cz, attributes);
609 Strbuf_append1(&buf, attributes | *cp); /* normal character */
611 cleanup_ignore(&buf);
613 return Strbuf_finish(&buf);
617 expdollar(struct Strbuf *buf, const Char **srcp, Char attr)
620 const Char *src = *srcp;
625 /* found a variable, expand it */
626 var = xmalloc((Strlen(src) + 1) * sizeof (*var));
628 var[i] = *++src & TRIM;
629 if (i == 0 && var[i] == '{') {
631 var[i] = *++src & TRIM;
633 if (!alnum(var[i]) && var[i] != '_') {
639 if (curly && (*src & TRIM) == '}')
644 for (i = 0; vp->vec[i] != NULL; i++) {
645 for (val = vp->vec[i]; *val; val++)
646 if (*val != '\n' && *val != '\r')
647 Strbuf_append1(buf, *val | attr);
649 Strbuf_append1(buf, ' ' | attr);
653 val = (!vp) ? tgetenv(var) : NULL;
656 if (*val != '\n' && *val != '\r')
657 Strbuf_append1(buf, *val | attr);