/* $NetBSD: prompt.c,v 1.27 2017/06/27 23:25:13 christos Exp $ */12/*-3* Copyright (c) 1992, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Christos Zoulas of Cornell University.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include "config.h"35#if !defined(lint) && !defined(SCCSID)36#if 037static char sccsid[] = "@(#)prompt.c 8.1 (Berkeley) 6/4/93";38#else39__RCSID("$NetBSD: prompt.c,v 1.27 2017/06/27 23:25:13 christos Exp $");40#endif41#endif /* not lint && not SCCSID */4243/*44* prompt.c: Prompt printing functions45*/46#include <stdio.h>47#include "el.h"4849static wchar_t *prompt_default(EditLine *);50static wchar_t *prompt_default_r(EditLine *);5152/* prompt_default():53* Just a default prompt, in case the user did not provide one54*/55static wchar_t *56/*ARGSUSED*/57prompt_default(EditLine *el __attribute__((__unused__)))58{59static wchar_t a[3] = L"? ";6061return a;62}636465/* prompt_default_r():66* Just a default rprompt, in case the user did not provide one67*/68static wchar_t *69/*ARGSUSED*/70prompt_default_r(EditLine *el __attribute__((__unused__)))71{72static wchar_t a[1] = L"";7374return a;75}767778/* prompt_print():79* Print the prompt and update the prompt position.80*/81libedit_private void82prompt_print(EditLine *el, int op)83{84el_prompt_t *elp;85wchar_t *p;8687if (op == EL_PROMPT)88elp = &el->el_prompt;89else90elp = &el->el_rprompt;9192if (elp->p_wide)93p = (*elp->p_func)(el);94else95p = ct_decode_string((char *)(void *)(*elp->p_func)(el),96&el->el_scratch);9798for (; *p; p++) {99if (elp->p_ignore == *p) {100wchar_t *litstart = ++p;101while (*p && *p != elp->p_ignore)102p++;103if (!*p || !p[1]) {104// XXX: We lose the last literal105break;106}107re_putliteral(el, litstart, p++);108continue;109}110re_putc(el, *p, 1);111}112113elp->p_pos.v = el->el_refresh.r_cursor.v;114elp->p_pos.h = el->el_refresh.r_cursor.h;115}116117118/* prompt_init():119* Initialize the prompt stuff120*/121libedit_private int122prompt_init(EditLine *el)123{124125el->el_prompt.p_func = prompt_default;126el->el_prompt.p_pos.v = 0;127el->el_prompt.p_pos.h = 0;128el->el_prompt.p_ignore = '\0';129el->el_rprompt.p_func = prompt_default_r;130el->el_rprompt.p_pos.v = 0;131el->el_rprompt.p_pos.h = 0;132el->el_rprompt.p_ignore = '\0';133return 0;134}135136137/* prompt_end():138* Clean up the prompt stuff139*/140libedit_private void141/*ARGSUSED*/142prompt_end(EditLine *el __attribute__((__unused__)))143{144}145146147/* prompt_set():148* Install a prompt printing function149*/150libedit_private int151prompt_set(EditLine *el, el_pfunc_t prf, wchar_t c, int op, int wide)152{153el_prompt_t *p;154155if (op == EL_PROMPT || op == EL_PROMPT_ESC)156p = &el->el_prompt;157else158p = &el->el_rprompt;159160if (prf == NULL) {161if (op == EL_PROMPT || op == EL_PROMPT_ESC)162p->p_func = prompt_default;163else164p->p_func = prompt_default_r;165} else {166p->p_func = prf;167}168169p->p_ignore = c;170171p->p_pos.v = 0;172p->p_pos.h = 0;173p->p_wide = wide;174175return 0;176}177178179/* prompt_get():180* Retrieve the prompt printing function181*/182libedit_private int183prompt_get(EditLine *el, el_pfunc_t *prf, wchar_t *c, int op)184{185el_prompt_t *p;186187if (prf == NULL)188return -1;189190if (op == EL_PROMPT)191p = &el->el_prompt;192else193p = &el->el_rprompt;194195if (prf)196*prf = p->p_func;197if (c)198*c = p->p_ignore;199200return 0;201}202203204