/* Invite */
/* Copyright (C) 2008 Emmanuel Varoquaux
This file is part of XOS.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see . */
#include "prompt.h"
#include "xmalloc.h"
#include
#include
char *local_prompt_prefix; /* partie de l'invite avant le dernier saut de ligne */
char *local_prompt; /* derniere ligne logique de l'invite */
int prompt_visible_length; /* nombre de caracteres visibles dans local_prompt */
/* Tous les caracteres situes entre '\001' et '\002' sont consideres comme
* invisibles. */
static int expand(const char *s, size_t n, char *buf)
{
int visible_length;
char *p;
int visible;
char c;
visible_length = 0;
p = buf;
visible = 1;
while (n--) {
c = *s++;
if (visible) {
if (c == '\001') {
visible = 0;
continue;
}
}
else
if (c == '\002') {
visible = 1;
continue;
}
*p++ = c;
if (visible)
visible_length++;
}
*p = '\0';
return visible_length;
}
static void expand_prompt(const char *prompt)
{
char *p;
int len;
if ((p = strrchr(prompt, '\n'))) {
p++;
len = p - prompt;
local_prompt_prefix = xmalloc(len + 1);
expand(prompt, len, local_prompt_prefix);
prompt = p;
}
else
local_prompt_prefix = NULL;
len = strlen(prompt);
local_prompt = xmalloc(len + 1);
prompt_visible_length = expand(prompt, len, local_prompt);
}
void set_prompt(const char *prompt)
{
if (prompt)
expand_prompt(prompt);
else {
local_prompt_prefix = NULL;
local_prompt = NULL;
prompt_visible_length = 0;
}
}
void free_prompt()
{
if (local_prompt_prefix)
free(local_prompt_prefix);
if (local_prompt)
free(local_prompt);
}