void print_str(t_pidlist *list) { if (list->prompt == NULL) { list->prompt = format_prompt(tstr_convert(list->str)); tstr_del(list->str); list->str = NULL; return ; } if (list->color == NULL) { list->color = format_color(tstr_convert(list->str)); tstr_del(list->str); list->str = NULL; return ; } list->msg = tstr_convert(list->str); tstr_del(list->str); list->str = NULL; ft_putstr(list->color); ft_putstr(list->prompt); ft_putendl(list->msg); ft_putstr(C_DEFAULT); free(list->msg); list->msg = NULL; }
char *format_prompt(t_context *context, const char *format) { t_list *tokens; char *formatted; tokens = tokenize_prompt(context, (char *)format); if (!tokens) return (format_prompt(context, DEFAULT_PROMPT)); formatted = parse_prompt_tokens(tokens); ft_lstdel(&tokens, free_token); return (formatted); }
static void _check_preload(QSP_ARG_DECL const char *prompt, int n, const char **choices) { const char *pline; if( ! IS_COMPLETING(THIS_QSP) ) return; if( ! intractive() ) return; if( *prompt == 0 ) return; // Need to format the prompt! pline = format_prompt(PROMPT_FORMAT, prompt); preload_history_list(pline,n,choices); }
int main(int argc, char* arg[]) { char fmt_PS1[4096]; /*Defaults for switches, may or may not change after get_options*/ logging = 0; restricted = 0; verbose = 0; read_rc = 1; history = 1; input = 0; get_options(argc,arg); init_env(); if(logging) open_log(); init_shell(); if(read_rc) parse_rc(); if(history) using_history(); if(!PS1){ PS1 = (char *) default_prompt; specified_PS1 = 0; } /* * Here's the main loop. * First we go ahead and get the current working directory * Next we see if the we're in a restricted shell, and change the * prompt accordingly. Then we get the string, and if all goes well * with that, write the history. Then we remove the \n terminator and * feed the string to the parser. After everything is parsed we pass the * string to check for builtins. If it was a built in, there's no need * to execute it, so we skip the execute function, otherwise we go ahead * and execute it. */ while(1){ if( (getcwd(env->cur_dir, sizeof(env->cur_dir)) == NULL)){ if(logging) write_to_log(SHELL_OOPS,"could not get current working directory"); perror("get current directory"); } memset(fmt_PS1, 0, 4096); format_prompt(fmt_PS1, 4096); input = readline(fmt_PS1); if(!input) exit_clean(1, "invalid input"); if(strlen(input) < 1) continue; if(history){ if(!history_filename) get_history_filename(); add_history(input); } parse(input, argv); if(built_ins(argv) == 1) continue; execute(argv); } }