| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /*
- *
- */
- #include <assert.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/utsname.h>
- #include <faux/conv.h>
- #include <faux/str.h>
- #include <faux/list.h>
- #include <faux/sysdb.h>
- #include <klish/kcontext.h>
- #include <klish/ksession.h>
- #include <klish/kpath.h>
- int klish_nop(kcontext_t *context)
- {
- context = context; // Happy compiler
- return 0;
- }
- // Symbol for testing purposes
- int klish_tsym(kcontext_t *context)
- {
- const char *script = NULL;
- script = kcontext_script(context);
- if (faux_str_is_empty(script)) {
- kcontext_printf(context, "[<empty>]\n");
- kcontext_fprintf(context, stderr, "Empty item\n");
- return -1;
- }
- kcontext_printf(context, "[%s]\n", script);
- return 0;
- }
- // Print content of ACTION script
- int klish_printl(kcontext_t *context)
- {
- const char *script = NULL;
- script = kcontext_script(context);
- if (faux_str_is_empty(script))
- script = "";
- kcontext_printf(context, "%s\n", script);
- return 0;
- }
- // Print content of ACTION script. Without additional '/n'
- int klish_print(kcontext_t *context)
- {
- const char *script = NULL;
- script = kcontext_script(context);
- if (faux_str_is_empty(script))
- script = "";
- kcontext_printf(context, "%s", script);
- return 0;
- }
- // Template for easy prompt string generation
- int klish_prompt(kcontext_t *context)
- {
- const char *script = NULL;
- const char *start = NULL;
- const char *pos = NULL;
- char *prompt = NULL;
- bool_t is_macro = BOOL_FALSE;
- script = kcontext_script(context);
- if (faux_str_is_empty(script))
- return 0;
- pos = script;
- start = script;
- while (*pos != '\0') {
- if (is_macro) {
- switch (*pos) {
- // Percent symbol itself
- case '%': {
- faux_str_cat(&prompt, "%");
- break;
- }
- // Backslash symbol itself
- case '\\': {
- faux_str_cat(&prompt, "\\");
- break;
- }
- // Escape character
- case 'e': {
- faux_str_cat(&prompt, "\x1b");
- break;
- }
- // Hexadecimal byte
- case 'x': {
- char c;
- char hs[3] = {};
- if (*(pos + 1) == '\0')
- break;
- if (*(pos + 2) == '\0') {
- pos++;
- break;
- }
- hs[0] = *(pos + 1);
- hs[1] = *(pos + 2);
- pos += 2;
- if (!faux_conv_atouc(hs, &c, 16))
- break;
- faux_str_catn(&prompt, &c, 1);
- break;
- }
- // Hostname
- case 'h': {
- struct utsname buf;
- if (uname(&buf) == 0)
- faux_str_cat(&prompt, buf.nodename);
- break;
- }
- // Username
- case 'u': {
- const char *user = NULL;
- user = ksession_user(kcontext_session(context));
- if (user)
- faux_str_cat(&prompt, user);
- break;
- }
- }
- is_macro = BOOL_FALSE;
- start = pos + 1;
- } else if (*pos == '%' || *pos == '\\') {
- is_macro = BOOL_TRUE;
- if (pos > start)
- faux_str_catn(&prompt, start, pos - start);
- }
- pos++;
- }
- if (pos > start)
- faux_str_catn(&prompt, start, pos - start);
- kcontext_fwrite(context, stdout, prompt, strlen(prompt));
- faux_str_free(prompt);
- return 0;
- }
|