Changeset 1236 in kBuild for trunk/src/kash/parser.c
- Timestamp:
- Oct 10, 2007 12:41:39 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/parser.c
r1233 r1236 62 62 # include "myhistedit.h" 63 63 #endif 64 #include "cd.h" 64 65 #include "shinstance.h" 65 66 … … 1621 1622 } 1622 1623 1624 STATIC const char * 1625 my_basename(const char *argv0, unsigned *lenp) 1626 { 1627 const char *tmp; 1628 1629 /* skip the path */ 1630 for (tmp = strpbrk(argv0, "\\/:"); tmp; tmp = strpbrk(argv0, "\\/:")) 1631 argv0 = tmp + 1; 1632 1633 if (lenp) { 1634 /* find the end, ignoring extenions */ 1635 tmp = strrchr(argv0, '.'); 1636 if (!tmp) 1637 tmp = strchr(argv0, '\0'); 1638 *lenp = (unsigned)(tmp - argv0); 1639 } 1640 return argv0; 1641 } 1642 1643 1623 1644 STATIC void 1624 1645 setprompt(shinstance *psh, int which) … … 1629 1650 if (!el) 1630 1651 #endif 1631 out2str(psh, getprompt(psh, NULL)); 1652 { 1653 /* deal with bash prompts */ 1654 const char *prompt = getprompt(psh, NULL); 1655 if (!strchr(prompt, '\\')) { 1656 out2str(psh, prompt); 1657 } else { 1658 while (*prompt) { 1659 if (*prompt != '\\') { 1660 out2c(psh, *prompt++); 1661 } else { 1662 prompt++; 1663 switch (*prompt++) 1664 { 1665 /* simple */ 1666 case '$': out2c(psh, sh_geteuid(psh) ? '$' : '#'); break; 1667 case '\\': out2c(psh, '\\'); break; 1668 case 'a': out2c(psh, '\a'); break; 1669 case 'e': out2c(psh, 033); break; 1670 case 'n': out2c(psh, '\n'); break; 1671 case 'r': out2c(psh, '\r'); break; 1672 1673 /* complicated */ 1674 case 's': { 1675 unsigned len; 1676 const char *arg0 = my_basename(psh->arg0, &len); 1677 outfmt(psh->out2, "%.*s", len, arg0); 1678 break; 1679 } 1680 case 'v': 1681 outfmt(psh->out2, "%d.%d", KBUILD_VERSION_MAJOR, 1682 KBUILD_VERSION_MINOR); 1683 break; 1684 case 'V': 1685 outfmt(psh->out2, "%d.%d.%d", KBUILD_VERSION_MAJOR, 1686 KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH); 1687 break; 1688 out2str(psh, getpwd(psh, 1) ? getpwd(psh, 1) : "?"); 1689 break; 1690 case 'w': 1691 case 'W': { 1692 const char *cwd = getpwd(psh, 1); 1693 const char *home = bltinlookup(psh, "HOME", 1); 1694 size_t home_len = home ? strlen(home) : 0; 1695 if (!cwd) cwd = "?"; 1696 if (!strncmp(cwd, home, home_len) 1697 && ( cwd[home_len] == '\0' 1698 || (cwd[home_len] == '/' && prompt[-1] == 'w'))) { 1699 out2c(psh, '~'); 1700 if (prompt[-1] == 'w' && cwd[home_len]) { 1701 out2str(psh, cwd + home_len); 1702 } 1703 } else if (prompt[-1] == 'w') { 1704 out2str(psh, cwd); 1705 } else { 1706 out2str(psh, my_basename(cwd, NULL)); 1707 } 1708 break; 1709 } 1710 case '0': 1711 case '1': 1712 case '2': 1713 case '3': { 1714 unsigned int ch = prompt[-1] - '0'; 1715 if (isdigit(*prompt)) { 1716 ch *= 8; 1717 ch += *prompt++ - '0'; 1718 } 1719 if (isdigit(*prompt)) { 1720 ch *= 8; 1721 ch += *prompt++ - '0'; 1722 } 1723 out2c(psh, ch); 1724 break; 1725 } 1726 1727 /* ignore */ 1728 break; 1729 case '!': 1730 case '#': 1731 case '@': 1732 case 'A': 1733 case 'h': 1734 case 'H': 1735 case 'j': 1736 case 'l': 1737 case 't': 1738 case 'T': 1739 case 'u': 1740 case '[': 1741 if (strchr(prompt, ']')) { 1742 prompt = strchr(prompt, ']') + 1; 1743 } 1744 break; 1745 case 'D': 1746 if (*prompt == '{' && strchr(prompt, '}')) { 1747 prompt = strchr(prompt, '}') + 1; 1748 } 1749 break; 1750 } 1751 1752 } 1753 } 1754 } 1755 } 1632 1756 } 1633 1757
Note:
See TracChangeset
for help on using the changeset viewer.