prompt.cxx

Go to the documentation of this file.
00001 // @(#)root/editline:$Id: prompt.cxx 36303 2010-10-11 15:16:49Z axel $
00002 // Author: Mary-Louise Gill, 2009
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 /*      $NetBSD: prompt.c,v 1.8 2001/01/10 07:45:41 jdolecek Exp $      */
00013 
00014 /*-
00015  * Copyright (c) 1992, 1993
00016  *      The Regents of the University of California.  All rights reserved.
00017  *
00018  * This code is derived from software contributed to Berkeley by
00019  * Christos Zoulas of Cornell University.
00020  *
00021  * Redistribution and use in source and binary forms, with or without
00022  * modification, are permitted provided that the following conditions
00023  * are met:
00024  * 1. Redistributions of source code must retain the above copyright
00025  *    notice, this list of conditions and the following disclaimer.
00026  * 2. Redistributions in binary form must reproduce the above copyright
00027  *    notice, this list of conditions and the following disclaimer in the
00028  *    documentation and/or other materials provided with the distribution.
00029  * 3. Neither the name of the University nor the names of its contributors
00030  *    may be used to endorse or promote products derived from this software
00031  *    without specific prior written permission.
00032  *
00033  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00034  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00035  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00036  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00037  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00038  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00039  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00040  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00041  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00042  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00043  * SUCH DAMAGE.
00044  */
00045 
00046 #include "compat.h"
00047 
00048 /*
00049  * prompt.c: Prompt printing functions
00050  */
00051 #include "sys.h"
00052 #include <stdio.h>
00053 #include "el.h"
00054 
00055 ElColor_t prompt_color(6, -1);
00056 
00057 el_private const char* prompt_default(EditLine_t*);
00058 el_private const char* prompt_default_r(EditLine_t*);
00059 
00060 /* prompt_default():
00061  *      Just a default prompt, in case the user did not provide one
00062  */
00063 el_private const char*
00064 /*ARGSUSED*/
00065 prompt_default(EditLine_t* /*el*/) {
00066    static char a[3] = { '?', ' ', '\0' };
00067 
00068    return a;
00069 }
00070 
00071 
00072 /* prompt_default_r():
00073  *      Just a default rprompt, in case the user did not provide one
00074  */
00075 el_private const char*
00076 /*ARGSUSED*/
00077 prompt_default_r(EditLine_t* /*el*/) {
00078    static char a[1] = { '\0' };
00079 
00080    return a;
00081 }
00082 
00083 
00084 /* prompt_print():
00085  *      Print the prompt and update the prompt position.
00086  *      We use an array of integers in case we want to pass
00087  *      literal escape sequences in the prompt and we want a
00088  *      bit to flag them
00089  */
00090 el_protected void
00091 prompt_print(EditLine_t* el, int op) {
00092    ElPrompt_t* elp;
00093    const char* p;
00094 
00095    if (op == EL_PROMPT) {
00096       elp = &el->fPrompt;
00097    } else {
00098       elp = &el->fRPrompt;
00099    }
00100    p = (elp->fFunc)(el);
00101 
00102    if (*p && !tty_can_output()) {
00103       // don't print the prompt to not block
00104       return;
00105    }
00106 
00107 
00108    ElColor_t col(prompt_color);
00109 
00110    while (*p) {
00111       if (*p == '\033' && p[1] == '[') {
00112          // escape sequence?
00113          // we support up to 3 numbers separated by ';'
00114          int num[3] = {0};
00115          int i = 2;
00116          int n = 0;
00117          while(n < 3) {
00118             while (isdigit(p[i])) {
00119                num[n] *= 10;
00120                num[n] += p[i] - '0';
00121                ++i;
00122             };
00123             ++n;
00124             if (p[i] != ';') {
00125                // ';' is number separator
00126                break;
00127             }
00128          }
00129          if (p[i] == 'm') {
00130             // color / bold / ...
00131             const char* strColor = 0;
00132             if (n < 2) {
00133                if (num[0] == 0) {
00134                   strColor = "default";
00135                } else if (num[0] == 1) {
00136                   strColor = "bold default";
00137                } else if (num[0] == '4') {
00138                   strColor = "under default";
00139                } else if (num[0] == '5') {
00140                   strColor = "bold default";
00141                } else if (num[0] == '7') {
00142                   // reverse, not supported
00143                   // strColor = "reverse";
00144                }
00145             } else if (num[0] == '3') {
00146                const char* colors[] = {
00147                   "black", "red", "green", "yellow", "blue",
00148                   "magenta" , "cyan", "white", "default"
00149                };
00150                strColor = colors[num[1]];
00151             } else if (num[0] == '4') {
00152                // bg color, not supported
00153             }
00154 
00155             if (strColor) {
00156                col.fForeColor = term__atocolor(strColor);
00157             } else {
00158                col.fForeColor = -1;
00159             }
00160             p += i + 1; // skip escape
00161             continue;
00162          }
00163       }
00164       re_putc(el, *p++, 1, &col);
00165    }
00166 
00167    elp->fPos.fV = el->fRefresh.r_cursor.fV;
00168    elp->fPos.fH = el->fRefresh.r_cursor.fH;
00169 } // prompt_print
00170 
00171 
00172 /* prompt_init():
00173  *      Initialize the prompt stuff
00174  */
00175 el_protected int
00176 prompt_init(EditLine_t* el) {
00177    el->fPrompt.fFunc = prompt_default;
00178    el->fPrompt.fPos.fV = 0;
00179    el->fPrompt.fPos.fH = 0;
00180    el->fRPrompt.fFunc = prompt_default_r;
00181    el->fRPrompt.fPos.fV = 0;
00182    el->fRPrompt.fPos.fH = 0;
00183    return 0;
00184 }
00185 
00186 
00187 /* prompt_end():
00188  *      Clean up the prompt stuff
00189  */
00190 el_protected void
00191 /*ARGSUSED*/
00192 prompt_end(EditLine_t* /*el*/) {
00193 }
00194 
00195 
00196 /* prompt_set():
00197  *      Install a prompt printing function
00198  */
00199 el_protected int
00200 prompt_set(EditLine_t* el, ElPFunc_t prf, int op) {
00201    ElPrompt_t* p;
00202 
00203    if (op == EL_PROMPT) {
00204       p = &el->fPrompt;
00205    } else {
00206       p = &el->fRPrompt;
00207    }
00208 
00209    if (prf == NULL) {
00210       if (op == EL_PROMPT) {
00211          p->fFunc = prompt_default;
00212       } else {
00213          p->fFunc = prompt_default_r;
00214       }
00215    } else {
00216       p->fFunc = prf;
00217    }
00218    p->fPos.fV = 0;
00219    p->fPos.fH = 0;
00220    return 0;
00221 } // prompt_set
00222 
00223 
00224 /* prompt_get():
00225  *      Retrieve the prompt printing function
00226  */
00227 el_protected int
00228 prompt_get(EditLine_t* el, ElPFunc_t* prf, int op) {
00229    if (prf == NULL) {
00230       return -1;
00231    }
00232 
00233    if (op == EL_PROMPT) {
00234       *prf = el->fPrompt.fFunc;
00235    } else {
00236       *prf = el->fRPrompt.fFunc;
00237    }
00238    return 0;
00239 }
00240 
00241 
00242 /* prompt_setcolor():
00243  *      Set the prompt's dislpay color
00244  */
00245 el_protected void
00246 prompt_setcolor(int col) {
00247    prompt_color.fForeColor = col;
00248 }

Generated on Tue Jul 5 14:11:38 2011 for ROOT_528-00b_version by  doxygen 1.5.1