Permalink
Browse files

Change to make the lookahead array a std::stack instead of a static 4…

…k list(!)
  • Loading branch information...
1 parent 464f1f4 commit e152cfac34df6686836a702660b50859f29c3e8c @ridiculousfish ridiculousfish committed Nov 25, 2012
Showing with 65 additions and 47 deletions.
  1. +37 −20 input_common.cpp
  2. +23 −23 screen.cpp
  3. +5 −4 screen.h
View
@@ -14,6 +14,8 @@ Implementation file for the low level input library
#include <sys/types.h>
#include <unistd.h>
#include <wchar.h>
+#include <stack>
+#include <list>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
@@ -34,15 +36,30 @@ Implementation file for the low level input library
*/
#define WAIT_ON_ESCAPE 10
-/**
- Characters that have been read and returned by the sequence matching code
-*/
-static wint_t lookahead_arr[1024];
+/** Characters that have been read and returned by the sequence matching code */
+static std::stack<wint_t, std::list<wint_t> > lookahead_list;
-/**
- Number of entries in lookahead_arr
-*/
-static int lookahead_count = 0;
+static bool has_lookahead(void)
+{
+ return ! lookahead_list.empty();
+}
+
+static wint_t lookahead_pop(void)
+{
+ wint_t result = lookahead_list.top();
+ lookahead_list.pop();
+ return result;
+}
+
+static void lookahead_push(wint_t c)
+{
+ lookahead_list.push(c);
+}
+
+static wint_t lookahead_top(void)
+{
+ return lookahead_list.top();
+}
/** Callback function for handling interrupts on reading */
static int (*interrupt_handler)();
@@ -115,9 +132,9 @@ static wint_t readb()
{
return res;
}
- if (lookahead_count)
+ if (has_lookahead())
{
- return lookahead_arr[--lookahead_count];
+ return lookahead_pop();
}
}
@@ -144,18 +161,18 @@ static wint_t readb()
{
debug(3, L"Wake up on universal variable event");
env_universal_read_all();
- if (lookahead_count)
+ if (has_lookahead())
{
- return lookahead_arr[--lookahead_count];
+ return lookahead_pop();
}
}
if (ioport > 0 && FD_ISSET(ioport, &fdset))
{
iothread_service_completion();
- if (lookahead_count)
+ if (has_lookahead())
{
- return lookahead_arr[--lookahead_count];
+ return lookahead_pop();
}
}
@@ -179,7 +196,7 @@ static wint_t readb()
wchar_t input_common_readch(int timed)
{
- if (lookahead_count == 0)
+ if (! has_lookahead())
{
if (timed)
{
@@ -247,19 +264,19 @@ wchar_t input_common_readch(int timed)
{
if (!timed)
{
- while ((lookahead_count >= 0) && (lookahead_arr[lookahead_count-1] == WEOF))
- lookahead_count--;
- if (lookahead_count == 0)
+ while (has_lookahead() && lookahead_top() == WEOF)
+ lookahead_pop();
+ if (! has_lookahead())
return input_common_readch(0);
}
- return lookahead_arr[--lookahead_count];
+ return lookahead_pop();
}
}
void input_common_unreadch(wint_t ch)
{
- lookahead_arr[lookahead_count++] = ch;
+ lookahead_push(ch);
}
View
@@ -175,7 +175,7 @@ static size_t calc_prompt_width_and_lines(const wchar_t *prompt, size_t *out_pro
size_t res = 0;
size_t j, k;
*out_prompt_lines = 1;
-
+
for (j=0; prompt[j]; j++)
{
if (prompt[j] == L'\x1b')
@@ -186,7 +186,7 @@ static size_t calc_prompt_width_and_lines(const wchar_t *prompt, size_t *out_pro
size_t p;
int len=0;
bool found = false;
-
+
/*
Detect these terminfo color escapes with parameter
value 0..7, all of which don't move the cursor
@@ -199,7 +199,7 @@ static size_t calc_prompt_width_and_lines(const wchar_t *prompt, size_t *out_pro
set_background,
}
;
-
+
/*
Detect these semi-common terminfo escapes without any
parameter values, all of which don't move the cursor
@@ -228,12 +228,12 @@ static size_t calc_prompt_width_and_lines(const wchar_t *prompt, size_t *out_pro
enter_secure_mode
}
;
-
+
for (p=0; p < sizeof esc / sizeof *esc && !found; p++)
{
if (!esc[p])
continue;
-
+
for (k=0; k<8; k++)
{
len = try_sequence(tparm(esc[p],k), &prompt[j]);
@@ -245,7 +245,7 @@ static size_t calc_prompt_width_and_lines(const wchar_t *prompt, size_t *out_pro
}
}
}
-
+
/* PCA for term256 support, let's just detect the escape codes directly */
if (! found)
{
@@ -256,8 +256,8 @@ static size_t calc_prompt_width_and_lines(const wchar_t *prompt, size_t *out_pro
found = true;
}
}
-
-
+
+
for (p=0; p < (sizeof(esc2)/sizeof(char *)) && !found; p++)
{
if (!esc2[p])
@@ -269,14 +269,14 @@ static size_t calc_prompt_width_and_lines(const wchar_t *prompt, size_t *out_pro
*/
len = maxi(try_sequence(tparm(esc2[p]), &prompt[j]),
try_sequence(esc2[p], &prompt[j]));
-
+
if (len)
{
j += (len-1);
found = true;
}
}
-
+
if (!found)
{
if (prompt[j+1] == L'k')
@@ -307,7 +307,7 @@ static size_t calc_prompt_width_and_lines(const wchar_t *prompt, size_t *out_pro
}
}
}
-
+
}
else if (prompt[j] == L'\t')
{
@@ -1173,40 +1173,40 @@ void s_write(screen_t *s,
void s_reset(screen_t *s, screen_reset_mode_t mode)
{
CHECK(s,);
-
+
bool abandon_line = false, repaint_prompt = false, clear_to_eos = false;
switch (mode)
{
case screen_reset_current_line_contents:
break;
-
+
case screen_reset_current_line_and_prompt:
repaint_prompt = true;
break;
-
+
case screen_reset_abandon_line:
abandon_line = true;
repaint_prompt = true;
break;
-
+
case screen_reset_abandon_line_and_clear_to_end_of_screen:
abandon_line = true;
repaint_prompt = true;
clear_to_eos = true;
break;
}
-
+
/* If we're abandoning the line, we must also be repainting the prompt */
assert(! abandon_line || repaint_prompt);
-
+
/* If we are not abandoning the line, we need to remember how many lines we had output to, so we can clear the remaining lines in the next call to s_update. This prevents leaving junk underneath the cursor when resizing a window wider such that it reduces our desired line count. */
if (! abandon_line)
{
s->actual_lines_before_reset = maxi(s->actual_lines_before_reset, s->actual.line_count());
}
-
+
int prev_line = s->actual.cursor.y;
-
+
if (repaint_prompt)
{
/* If the prompt is multi-line, we need to move up to the prompt's initial line. We do this by lying to ourselves and claiming that we're really below what we consider "line 0" (which is the last line of the prompt). This will cause is to move up to try to get back to line 0, but really we're getting back to the initial line of the prompt. */
@@ -1217,21 +1217,21 @@ void s_reset(screen_t *s, screen_reset_mode_t mode)
/* Clear the prompt */
s->actual_left_prompt.clear();
}
-
+
s->actual.resize(0);
s->actual.cursor.x = 0;
s->actual.cursor.y = 0;
-
+
s->need_clear_lines = true;
s->need_clear_screen = s->need_clear_screen || clear_to_eos;
-
+
if (!abandon_line)
{
/* This should prevent reseting the cursor position during the next repaint. */
write_loop(1, "\r", 1);
s->actual.cursor.y = prev_line;
}
-
+
fstat(1, &s->prev_buff_1);
fstat(2, &s->prev_buff_2);
}
View
@@ -149,7 +149,7 @@ class screen_t
actual_lines_before_reset.
*/
bool need_clear_lines;
-
+
/** Whether there may be yet more content after the lines, and we issue a clr_eos if possible. */
bool need_clear_screen;
@@ -217,16 +217,17 @@ void s_write(screen_t *s,
void s_reset(screen_t *s, bool reset_cursor, bool reset_prompt = true);
-enum screen_reset_mode_t {
+enum screen_reset_mode_t
+{
/* Do not make a new line, do not repaint the prompt. */
screen_reset_current_line_contents,
/* Do not make a new line, do repaint the prompt. */
screen_reset_current_line_and_prompt,
-
+
/* Abandon the current line, go to the next one, repaint the prompt */
screen_reset_abandon_line,
-
+
/* Abandon the current line, go to the next one, clear the rest of the screen */
screen_reset_abandon_line_and_clear_to_end_of_screen
};

0 comments on commit e152cfa

Please sign in to comment.