1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 03:03:02 +01:00

refactor calling of toxic_string functions

This commit is contained in:
Jfreegman 2014-06-06 15:27:06 -04:00
parent 901ffbc7c0
commit bfa266c604
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
5 changed files with 134 additions and 146 deletions

View File

@ -561,7 +561,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (ltr) { if (ltr) {
/* prevents buffer overflows and strange behaviour when cursor goes past the window */ /* prevents buffer overflows and strange behaviour when cursor goes past the window */
if ( (ctx->len < MAX_STR_SIZE - 1) && (ctx->len < (x2 * (CHATBOX_HEIGHT - 1) - 1)) ) { if ( (ctx->len < MAX_STR_SIZE - 1) && (ctx->len < (x2 * (CHATBOX_HEIGHT - 1) - 1)) ) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key); add_char_to_buf(ctx, key);
if (x == x2 - 1) if (x == x2 - 1)
wmove(self->window, y + 1, 0); wmove(self->window, y + 1, 0);
@ -577,7 +577,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key */ if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key */
if (ctx->pos > 0) { if (ctx->pos > 0) {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1])); cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1]));
del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len); del_char_buf_bck(ctx);
if (x == 0) if (x == 0)
wmove(self->window, y - 1, x2 - cur_len); wmove(self->window, y - 1, x2 - cur_len);
@ -590,14 +590,14 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_DC) { /* DEL key: Remove character at pos */ else if (key == KEY_DC) { /* DEL key: Remove character at pos */
if (ctx->pos != ctx->len) if (ctx->pos != ctx->len)
del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len); del_char_buf_frnt(ctx);
else else
beep(); beep();
} }
else if (key == T_KEY_DISCARD) { /* CTRL-U: Delete entire line behind pos */ else if (key == T_KEY_DISCARD) { /* CTRL-U: Delete entire line behind pos */
if (ctx->pos > 0) { if (ctx->pos > 0) {
discard_buf(ctx->line, &ctx->pos, &ctx->len); discard_buf(ctx);
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} else { } else {
beep(); beep();
@ -606,7 +606,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == T_KEY_KILL) { /* CTRL-K: Delete entire line in front of pos */ else if (key == T_KEY_KILL) { /* CTRL-K: Delete entire line in front of pos */
if (ctx->pos != ctx->len) if (ctx->pos != ctx->len)
kill_buf(ctx->line, &ctx->pos, &ctx->len); kill_buf(ctx);
else else
beep(); beep();
} }
@ -654,21 +654,18 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
} }
else if (key == KEY_UP) { /* fetches previous item in history */ else if (key == KEY_UP) { /* fetches previous item in history */
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, fetch_hist_item(ctx, MOVE_UP);
&ctx->hst_pos, MOVE_UP);
mv_curs_end(self->window, ctx->len, y2, x2); mv_curs_end(self->window, ctx->len, y2, x2);
} }
else if (key == KEY_DOWN) { /* fetches next item in history */ else if (key == KEY_DOWN) { /* fetches next item in history */
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, fetch_hist_item(ctx, MOVE_DOWN);
&ctx->hst_pos, MOVE_DOWN);
mv_curs_end(self->window, ctx->len, y2, x2); mv_curs_end(self->window, ctx->len, y2, x2);
} }
else if (key == '\t') { /* TAB key: completes command */ else if (key == '\t') { /* TAB key: completes command */
if (ctx->len > 1 && ctx->line[0] == '/') { if (ctx->len > 1 && ctx->line[0] == '/') {
int diff = complete_line(ctx->line, &ctx->pos, &ctx->len, chat_cmd_list, AC_NUM_CHAT_COMMANDS, int diff = complete_line(ctx, chat_cmd_list, AC_NUM_CHAT_COMMANDS, MAX_CMDNAME_SIZE);
MAX_CMDNAME_SIZE);
if (diff != -1) { if (diff != -1) {
if (x + diff > x2 - 1) { if (x + diff > x2 - 1) {
@ -687,7 +684,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
/* RETURN key: Execute command or print line */ /* RETURN key: Execute command or print line */
else if (key == '\n') { else if (key == '\n') {
rm_trailing_spaces_buf(ctx->line, &ctx->pos, &ctx->len); rm_trailing_spaces_buf(ctx);
uint8_t line[MAX_STR_SIZE]; uint8_t line[MAX_STR_SIZE];
@ -698,7 +695,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
if (!string_is_empty(line)) if (!string_is_empty(line))
add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); add_line_to_hist(ctx);
if (line[0] == '/') { if (line[0] == '/') {
if (strcmp(line, "/close") == 0) { if (strcmp(line, "/close") == 0) {
@ -730,7 +727,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
} }
} }
reset_buf(ctx->line, &ctx->pos, &ctx->len); reset_buf(ctx);
} }
} }
@ -758,7 +755,7 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
uint8_t line[MAX_STR_SIZE]; uint8_t line[MAX_STR_SIZE];
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) { if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) {
reset_buf(ctx->line, &ctx->pos, &ctx->len); reset_buf(ctx);
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} else { } else {
mvwprintw(ctx->linewin, 1, 0, "%s", line); mvwprintw(ctx->linewin, 1, 0, "%s", line);

View File

@ -395,7 +395,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (ltr) { if (ltr) {
if ( (ctx->len < MAX_STR_SIZE - 1) && (ctx->len < (x2 * (CHATBOX_HEIGHT - 1) - 1)) ) { if ( (ctx->len < MAX_STR_SIZE - 1) && (ctx->len < (x2 * (CHATBOX_HEIGHT - 1) - 1)) ) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key); add_char_to_buf(ctx, key);
if (x == x2 - 1) if (x == x2 - 1)
wmove(self->window, y + 1, 0); wmove(self->window, y + 1, 0);
@ -408,7 +408,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */ if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */
if (ctx->pos > 0) { if (ctx->pos > 0) {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1])); cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1]));
del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len); del_char_buf_bck(ctx);
if (x == 0) if (x == 0)
wmove(self->window, y - 1, x2 - cur_len); wmove(self->window, y - 1, x2 - cur_len);
@ -421,14 +421,14 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_DC) { /* DEL key: Remove character at pos */ else if (key == KEY_DC) { /* DEL key: Remove character at pos */
if (ctx->pos != ctx->len) if (ctx->pos != ctx->len)
del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len); del_char_buf_frnt(ctx);
else else
beep(); beep();
} }
else if (key == T_KEY_DISCARD) { /* CTRL-U: Delete entire line behind pos */ else if (key == T_KEY_DISCARD) { /* CTRL-U: Delete entire line behind pos */
if (ctx->pos > 0) { if (ctx->pos > 0) {
discard_buf(ctx->line, &ctx->pos, &ctx->len); discard_buf(ctx);
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} else { } else {
beep(); beep();
@ -437,7 +437,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == T_KEY_KILL) { /* CTRL-K: Delete entire line in front of pos */ else if (key == T_KEY_KILL) { /* CTRL-K: Delete entire line in front of pos */
if (ctx->pos != ctx->len) if (ctx->pos != ctx->len)
kill_buf(ctx->line, &ctx->pos, &ctx->len); kill_buf(ctx);
else else
beep(); beep();
} }
@ -485,14 +485,12 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
} }
else if (key == KEY_UP) { /* fetches previous item in history */ else if (key == KEY_UP) { /* fetches previous item in history */
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, fetch_hist_item(ctx, MOVE_UP);
&ctx->hst_pos, MOVE_UP);
mv_curs_end(self->window, ctx->len, y2, x2); mv_curs_end(self->window, ctx->len, y2, x2);
} }
else if (key == KEY_DOWN) { /* fetches next item in history */ else if (key == KEY_DOWN) { /* fetches next item in history */
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, fetch_hist_item(ctx, MOVE_DOWN);
&ctx->hst_pos, MOVE_DOWN);
mv_curs_end(self->window, ctx->len, y2, x2); mv_curs_end(self->window, ctx->len, y2, x2);
} }
@ -501,11 +499,10 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
int diff; int diff;
if ((ctx->line[0] != '/') || (ctx->line[1] == 'm' && ctx->line[2] == 'e')) if ((ctx->line[0] != '/') || (ctx->line[1] == 'm' && ctx->line[2] == 'e'))
diff = complete_line(ctx->line, &ctx->pos, &ctx->len, groupchats[self->num].peer_names, diff = complete_line(ctx, groupchats[self->num].peer_names,
groupchats[self->num].num_peers, TOX_MAX_NAME_LENGTH); groupchats[self->num].num_peers, TOX_MAX_NAME_LENGTH);
else else
diff = complete_line(ctx->line, &ctx->pos, &ctx->len, glob_cmd_list, AC_NUM_GLOB_COMMANDS, diff = complete_line(ctx, glob_cmd_list, AC_NUM_GLOB_COMMANDS, MAX_CMDNAME_SIZE);
MAX_CMDNAME_SIZE);
if (diff != -1) { if (diff != -1) {
if (x + diff > x2 - 1) { if (x + diff > x2 - 1) {
@ -537,7 +534,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
/* RETURN key: Execute command or print line */ /* RETURN key: Execute command or print line */
else if (key == '\n') { else if (key == '\n') {
rm_trailing_spaces_buf(ctx->line, &ctx->pos, &ctx->len); rm_trailing_spaces_buf(ctx);
uint8_t line[MAX_STR_SIZE]; uint8_t line[MAX_STR_SIZE];
@ -549,7 +546,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (!string_is_empty(line)) if (!string_is_empty(line))
add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); add_line_to_hist(ctx);
if (line[0] == '/') { if (line[0] == '/') {
if (strcmp(line, "/close") == 0) { if (strcmp(line, "/close") == 0) {
@ -573,7 +570,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
} }
} }
reset_buf(ctx->line, &ctx->pos, &ctx->len); reset_buf(ctx);
} }
} }
} }
@ -599,7 +596,7 @@ static void groupchat_onDraw(ToxWindow *self, Tox *m)
uint8_t line[MAX_STR_SIZE]; uint8_t line[MAX_STR_SIZE];
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) { if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) {
reset_buf(ctx->line, &ctx->pos, &ctx->len); reset_buf(ctx);
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} else { } else {
mvwprintw(ctx->linewin, 1, 0, "%s", line); mvwprintw(ctx->linewin, 1, 0, "%s", line);

View File

@ -147,14 +147,14 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (ltr) { if (ltr) {
if (ctx->len < (MAX_STR_SIZE - 1)) { if (ctx->len < (MAX_STR_SIZE - 1)) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key); add_char_to_buf(ctx, key);
} }
} else { /* if (!ltr) */ } else { /* if (!ltr) */
/* BACKSPACE key: Remove one character from line */ /* BACKSPACE key: Remove one character from line */
if (key == 0x107 || key == 0x8 || key == 0x7f) { if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (ctx->pos > 0) { if (ctx->pos > 0) {
del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len); del_char_buf_bck(ctx);
wmove(ctx->history, y, x - 1); /* not necessary but fixes a display glitch */ wmove(ctx->history, y, x - 1); /* not necessary but fixes a display glitch */
} else { } else {
beep(); beep();
@ -163,7 +163,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_DC) { /* DEL key: Remove character at pos */ else if (key == KEY_DC) { /* DEL key: Remove character at pos */
if (ctx->pos != ctx->len) { if (ctx->pos != ctx->len) {
del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len); del_char_buf_frnt(ctx);
} else { } else {
beep(); beep();
} }
@ -173,7 +173,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (ctx->pos > 0) { if (ctx->pos > 0) {
wmove(ctx->history, ctx->orig_y, X_OFST); wmove(ctx->history, ctx->orig_y, X_OFST);
wclrtobot(ctx->history); wclrtobot(ctx->history);
discard_buf(ctx->line, &ctx->pos, &ctx->len); discard_buf(ctx);
} else { } else {
beep(); beep();
} }
@ -181,7 +181,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == T_KEY_KILL) { /* CTRL-K: Delete entire line in front of pos */ else if (key == T_KEY_KILL) { /* CTRL-K: Delete entire line in front of pos */
if (ctx->len != ctx->pos) if (ctx->len != ctx->pos)
kill_buf(ctx->line, &ctx->pos, &ctx->len); kill_buf(ctx);
else else
beep(); beep();
} }
@ -212,8 +212,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_UP) { /* fetches previous item in history */ else if (key == KEY_UP) { /* fetches previous item in history */
wmove(ctx->history, ctx->orig_y, X_OFST); wmove(ctx->history, ctx->orig_y, X_OFST);
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, fetch_hist_item(ctx, MOVE_UP);
&ctx->hst_pos, MOVE_UP);
/* adjust line y origin appropriately when window scrolls down */ /* adjust line y origin appropriately when window scrolls down */
if (ctx->at_bottom && ctx->len >= x2 - X_OFST) { if (ctx->at_bottom && ctx->len >= x2 - X_OFST) {
@ -233,14 +232,12 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_DOWN) { /* fetches next item in history */ else if (key == KEY_DOWN) { /* fetches next item in history */
wmove(ctx->history, ctx->orig_y, X_OFST); wmove(ctx->history, ctx->orig_y, X_OFST);
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, fetch_hist_item(ctx, MOVE_DOWN);
&ctx->hst_pos, MOVE_DOWN);
} }
else if (key == '\t') { /* TAB key: completes command */ else if (key == '\t') { /* TAB key: completes command */
if (ctx->len > 1 && ctx->line[0] == '/') { if (ctx->len > 1 && ctx->line[0] == '/') {
if (complete_line(ctx->line, &ctx->pos, &ctx->len, glob_cmd_list, AC_NUM_GLOB_COMMANDS, if (complete_line(ctx, glob_cmd_list, AC_NUM_GLOB_COMMANDS, MAX_CMDNAME_SIZE) == -1)
MAX_CMDNAME_SIZE) == -1)
beep(); beep();
} else { } else {
beep(); beep();
@ -249,7 +246,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
/* RETURN key: execute command */ /* RETURN key: execute command */
else if (key == '\n') { else if (key == '\n') {
rm_trailing_spaces_buf(ctx->line, &ctx->pos, &ctx->len); rm_trailing_spaces_buf(ctx);
wprintw(ctx->history, "\n"); wprintw(ctx->history, "\n");
uint8_t line[MAX_STR_SIZE] = {0}; uint8_t line[MAX_STR_SIZE] = {0};
@ -258,11 +255,11 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
memset(&line, 0, sizeof(line)); memset(&line, 0, sizeof(line));
if (!string_is_empty(line)) if (!string_is_empty(line))
add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); add_line_to_hist(ctx);
line_info_add(self, NULL, NULL, NULL, line, PROMPT, 0, 0); line_info_add(self, NULL, NULL, NULL, line, PROMPT, 0, 0);
execute(ctx->history, self, m, line, GLOBAL_COMMAND_MODE); execute(ctx->history, self, m, line, GLOBAL_COMMAND_MODE);
reset_buf(ctx->line, &ctx->pos, &ctx->len); reset_buf(ctx);
} }
} }
} }
@ -295,7 +292,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
uint8_t line[MAX_STR_SIZE]; uint8_t line[MAX_STR_SIZE];
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1)
reset_buf(ctx->line, &ctx->pos, &ctx->len); reset_buf(ctx);
else else
mvwprintw(ctx->history, ctx->orig_y, X_OFST, line); mvwprintw(ctx->history, ctx->orig_y, X_OFST, line);

View File

@ -31,177 +31,174 @@
#include "misc_tools.h" #include "misc_tools.h"
#include "toxic_strings.h" #include "toxic_strings.h"
/* Adds char to buffer at pos */ /* Adds char to line at pos */
void add_char_to_buf(wchar_t *buf, size_t *pos, size_t *len, wint_t ch) void add_char_to_buf(ChatContext *ctx, wint_t ch)
{ {
if (*pos < 0 || *len >= MAX_STR_SIZE) if (ctx->pos < 0 || ctx->len >= MAX_STR_SIZE)
return; return;
/* move all chars including null in front of pos one space forward and insert char in pos */ /* move all chars including null in front of pos one space forward and insert char in pos */
int i; int i;
for (i = *len; i >= *pos && i >= 0; --i) for (i = ctx->len; i >= ctx->pos && i >= 0; --i)
buf[i + 1] = buf[i]; ctx->line[i + 1] = ctx->line[i];
buf[(*pos)++] = ch; ctx->line[ctx->pos++] = ch;
++(*len); ++ctx->len;
} }
/* Deletes the character before pos */ /* Deletes the character before pos */
void del_char_buf_bck(wchar_t *buf, size_t *pos, size_t *len) void del_char_buf_bck(ChatContext *ctx)
{ {
if (*pos <= 0) if (ctx->pos <= 0)
return; return;
int i; int i;
/* similar to add_char_to_buf but deletes a char */ /* similar to add_char_to_buf but deletes a char */
for (i = *pos - 1; i <= *len; ++i) for (i = ctx->pos - 1; i <= ctx->len; ++i)
buf[i] = buf[i + 1]; ctx->line[i] = ctx->line[i + 1];
--(*pos); --ctx->pos;
--(*len); --ctx->len;
} }
/* Deletes the character at pos */ /* Deletes the character at pos */
void del_char_buf_frnt(wchar_t *buf, size_t *pos, size_t *len) void del_char_buf_frnt(ChatContext *ctx)
{ {
if (*pos < 0 || *pos >= *len) if (ctx->pos < 0 || ctx->pos >= ctx->len)
return; return;
int i; int i;
for (i = *pos; i < *len; ++i) for (i = ctx->pos; i < ctx->len; ++i)
buf[i] = buf[i + 1]; ctx->line[i] = ctx->line[i + 1];
--(*len); --ctx->len;
} }
/* Deletes the line from beginning to pos */ /* Deletes the line from beginning to pos */
void discard_buf(wchar_t *buf, size_t *pos, size_t *len) void discard_buf(ChatContext *ctx)
{ {
if (*pos <= 0) if (ctx->pos <= 0)
return; return;
int i; int i;
int c = 0; int c = 0;
for (i = *pos; i <= *len; ++i) for (i = ctx->pos; i <= ctx->len; ++i)
buf[c++] = buf[i]; ctx->line[c++] = ctx->line[i];
*pos = 0; ctx->pos = 0;
*len = c - 1; ctx->len = c - 1;
} }
/* Deletes the line from pos to len */ /* Deletes the line from pos to len */
void kill_buf(wchar_t *buf, size_t *pos, size_t *len) void kill_buf(ChatContext *ctx)
{ {
if (*len == *pos) if (ctx->len == ctx->pos)
return; return;
buf[*pos] = L'\0'; ctx->line[ctx->pos] = L'\0';
*len = *pos; ctx->len = ctx->pos;
} }
/* nulls buf and sets pos and len to 0 */ /* nulls line and sets pos and len to 0 */
void reset_buf(wchar_t *buf, size_t *pos, size_t *len) void reset_buf(ChatContext *ctx)
{ {
buf[0] = L'\0'; ctx->line[0] = L'\0';
*pos = 0; ctx->pos = 0;
*len = 0; ctx->len = 0;
} }
/* Removes trailing spaces from buf. */ /* Removes trailing spaces from line. */
void rm_trailing_spaces_buf(wchar_t *buf, size_t *pos, size_t *len) void rm_trailing_spaces_buf(ChatContext *ctx)
{ {
if (*len <= 0) if (ctx->len <= 0)
return; return;
if (buf[*len - 1] != ' ') if (ctx->line[ctx->len - 1] != ' ')
return; return;
int i; int i;
for (i = *len - 1; i >= 0; --i) { for (i = ctx->len - 1; i >= 0; --i) {
if (buf[i] != ' ') if (ctx->line[i] != ' ')
break; break;
} }
*len = i + 1; ctx->len = i + 1;
*pos = MIN(*pos, i + 1); ctx->pos = MIN(ctx->pos, i + 1);
buf[*len] = L'\0'; ctx->line[ctx->len] = L'\0';
} }
#define HIST_PURGE MAX_LINE_HIST / 4 #define HIST_PURGE MAX_LINE_HIST / 4
/* shifts hist items back and makes room for HIST_PURGE new entries */ /* shifts hist items back and makes room for HIST_PURGE new entries */
static void shift_hist_back(wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot) static void shift_hist_back(ChatContext *ctx)
{ {
int i; int i;
int n = MAX_LINE_HIST - HIST_PURGE; int n = MAX_LINE_HIST - HIST_PURGE;
for (i = 0; i < n; ++i) for (i = 0; i < n; ++i)
wmemcpy(hst[i], hst[i + HIST_PURGE], MAX_STR_SIZE); wmemcpy(ctx->ln_history[i], ctx->ln_history[i + HIST_PURGE], MAX_STR_SIZE);
*hst_tot = n; ctx->hst_tot = n;
} }
/* adds a line to the ln_history buffer at hst_pos and sets hst_pos to end of history. */ /* adds a line to the ln_history buffer at hst_pos and sets hst_pos to end of history. */
void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot, void add_line_to_hist(ChatContext *ctx)
int *hst_pos)
{ {
if (len > MAX_STR_SIZE) if (ctx->len > MAX_STR_SIZE)
return; return;
if (*hst_tot >= MAX_LINE_HIST) if (ctx->hst_tot >= MAX_LINE_HIST)
shift_hist_back(hst, hst_tot); shift_hist_back(ctx);
++(*hst_tot); ++ctx->hst_tot;
*hst_pos = *hst_tot; ctx->hst_pos = ctx->hst_tot;
wmemcpy(hst[*hst_tot - 1], buf, len + 1); wmemcpy(ctx->ln_history[ctx->hst_tot - 1], ctx->line, ctx->len + 1);
} }
/* copies history item at hst_pos to buf. Sets pos and len to the len of the history item. /* copies history item at hst_pos to line. Sets pos and len to the len of the history item.
hst_pos is decremented or incremented depending on key_dir. hst_pos is decremented or incremented depending on key_dir.
resets buffer if at end of history */ resets line if at end of history */
void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, wchar_t (*hst)[MAX_STR_SIZE], void fetch_hist_item(ChatContext *ctx, int key_dir)
int hst_tot, int *hst_pos, int key_dir)
{ {
if (key_dir == MOVE_UP) { if (key_dir == MOVE_UP) {
if (--(*hst_pos) < 0) { if (--ctx->hst_pos < 0) {
*hst_pos = 0; ctx->hst_pos = 0;
beep(); beep();
} }
} else { } else {
if (++(*hst_pos) >= hst_tot) { if (++ctx->hst_pos >= ctx->hst_tot) {
*hst_pos = hst_tot; ctx->hst_pos = ctx->hst_tot;
reset_buf(buf, pos, len); reset_buf(ctx);
return; return;
} }
} }
const wchar_t *hst_line = hst[*hst_pos]; const wchar_t *hst_line = ctx->ln_history[ctx->hst_pos];
size_t h_len = wcslen(hst_line); size_t h_len = wcslen(hst_line);
wmemcpy(buf, hst_line, h_len + 1); wmemcpy(ctx->line, hst_line, h_len + 1);
ctx->pos = h_len;
*pos = h_len; ctx->len = h_len;
*len = h_len;
} }
/* looks for the first instance in list that begins with the last entered word in buf according to pos, /* looks for the first instance in list that begins with the last entered word in line according to pos,
then fills buf with the complete word. e.g. "Hello jo" would complete the buffer then fills line with the complete word. e.g. "Hello jo" would complete the line
with "Hello john". with "Hello john".
list is a pointer to the list of strings being compared, n_items is the number of items list is a pointer to the list of strings being compared, n_items is the number of items
in the list, and size is the size of each item in the list. in the list, and size is the size of each item in the list.
Returns the difference between the old len and new len of buf on success, -1 if error */ Returns the difference between the old len and new len of line on success, -1 if error */
int complete_line(wchar_t *buf, size_t *pos, size_t *len, const void *list, int n_items, int size) int complete_line(ChatContext *ctx, const void *list, int n_items, int size)
{ {
if (*pos <= 0 || *len <= 0 || *len >= MAX_STR_SIZE) if (ctx->pos <= 0 || ctx->len <= 0 || ctx->len >= MAX_STR_SIZE)
return -1; return -1;
const uint8_t *L = (uint8_t *) list; const uint8_t *L = (uint8_t *) list;
@ -209,13 +206,13 @@ int complete_line(wchar_t *buf, size_t *pos, size_t *len, const void *list, int
uint8_t ubuf[MAX_STR_SIZE]; uint8_t ubuf[MAX_STR_SIZE];
/* work with multibyte string copy of buf for simplicity */ /* work with multibyte string copy of buf for simplicity */
if (wcs_to_mbs_buf(ubuf, buf, MAX_STR_SIZE) == -1) if (wcs_to_mbs_buf(ubuf, ctx->line, MAX_STR_SIZE) == -1)
return -1; return -1;
/* isolate substring from space behind pos to pos */ /* isolate substring from space behind pos to pos */
uint8_t tmp[MAX_STR_SIZE]; uint8_t tmp[MAX_STR_SIZE];
snprintf(tmp, sizeof(tmp), "%s", ubuf); snprintf(tmp, sizeof(tmp), "%s", ubuf);
tmp[*pos] = '\0'; tmp[ctx->pos] = '\0';
uint8_t *sub = strrchr(tmp, ' '); uint8_t *sub = strrchr(tmp, ' ');
int n_endchrs = 1; /* 1 = append space to end of match, 2 = append ": " */ int n_endchrs = 1; /* 1 = append space to end of match, 2 = append ": " */
@ -248,14 +245,14 @@ int complete_line(wchar_t *buf, size_t *pos, size_t *len, const void *list, int
/* put match in correct spot in buf and append endchars (space or ": ") */ /* put match in correct spot in buf and append endchars (space or ": ") */
const uint8_t *endchrs = n_endchrs == 1 ? " " : ": "; const uint8_t *endchrs = n_endchrs == 1 ? " " : ": ";
int m_len = strlen(match); int m_len = strlen(match);
int strt = *pos - s_len; int strt = ctx->pos - s_len;
int diff = m_len - s_len + n_endchrs; int diff = m_len - s_len + n_endchrs;
if (*len + diff > MAX_STR_SIZE) if (ctx->len + diff > MAX_STR_SIZE)
return -1; return -1;
uint8_t tmpend[MAX_STR_SIZE]; uint8_t tmpend[MAX_STR_SIZE];
strcpy(tmpend, &ubuf[*pos]); strcpy(tmpend, &ubuf[ctx->pos]);
strcpy(&ubuf[strt], match); strcpy(&ubuf[strt], match);
strcpy(&ubuf[strt + m_len], endchrs); strcpy(&ubuf[strt + m_len], endchrs);
strcpy(&ubuf[strt + m_len + n_endchrs], tmpend); strcpy(&ubuf[strt + m_len + n_endchrs], tmpend);
@ -266,10 +263,10 @@ int complete_line(wchar_t *buf, size_t *pos, size_t *len, const void *list, int
if (mbs_to_wcs_buf(newbuf, ubuf, MAX_STR_SIZE) == -1) if (mbs_to_wcs_buf(newbuf, ubuf, MAX_STR_SIZE) == -1)
return -1; return -1;
wcscpy(buf, newbuf); wcscpy(ctx->line, newbuf);
*len += (size_t) diff; ctx->len += (size_t) diff;
*pos += (size_t) diff; ctx->pos += (size_t) diff;
return diff; return diff;
} }

View File

@ -20,42 +20,42 @@
* *
*/ */
/* Adds char to buffer at pos */ /* Adds char to line at pos */
void add_char_to_buf(wchar_t *buf, size_t *pos, size_t *len, wint_t ch); void add_char_to_buf(ChatContext *ctx, wint_t ch);
/* Deletes the character before pos */ /* Deletes the character before pos */
void del_char_buf_bck(wchar_t *buf, size_t *pos, size_t *len); void del_char_buf_bck(ChatContext *ctx);
/* Deletes the character at pos */ /* Deletes the character at pos */
void del_char_buf_frnt(wchar_t *buf, size_t *pos, size_t *len); void del_char_buf_frnt(ChatContext *ctx);
/* Deletes the line from beginning to pos */ /* Deletes the line from beginning to pos */
void discard_buf(wchar_t *buf, size_t *pos, size_t *len); void discard_buf(ChatContext *ctx);
/* Deletes the line from pos to len */ /* Deletes the line from pos to len */
void kill_buf(wchar_t *buf, size_t *pos, size_t *len); void kill_buf(ChatContext *ctx);
/* nulls buf and sets pos and len to 0 */ /* nulls line and sets pos and len to 0 */
void reset_buf(wchar_t *buf, size_t *pos, size_t *len); void reset_buf(ChatContext *ctx);
/* Removes trailing spaces from buf. */ /* Removes trailing spaces from line. */
void rm_trailing_spaces_buf(wchar_t *buf, size_t *pos, size_t *len); void rm_trailing_spaces_buf(ChatContext *ctx);
/* looks for the first instance in list that begins with the last entered word in buf according to pos, /* looks for the first instance in list that begins with the last entered word in line according to pos,
then fills buf with the complete word. e.g. "Hello jo" would complete the buffer then fills line with the complete word. e.g. "Hello jo" would complete the line
with "Hello john". with "Hello john".
list is a pointer to the list of strings being compared, n_items is the number of items list is a pointer to the list of strings being compared, n_items is the number of items
in the list, and size is the size of each item in the list. in the list, and size is the size of each item in the list.
Returns the difference between the old len and new len of buf on success, -1 if error */ Returns the difference between the old len and new len of line on success, -1 if error */
int complete_line(wchar_t *buf, size_t *pos, size_t *len, const void *list, int n_items, int size); int complete_line(ChatContext *ctx, const void *list, int n_items, int size);
/* adds a line to the ln_history buffer at hst_pos and sets hst_pos to last history item. */ /* adds a line to the ln_history buffer at hst_pos and sets hst_pos to last history item. */
void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot, void add_line_to_hist(ChatContext *ctx);
int *hst_pos);
/* copies history item at hst_pos to buf. Sets pos and len to the len of the history item. /* copies history item at hst_pos to line. Sets pos and len to the len of the history item.
hst_pos is decremented or incremented depending on key_dir. */ hst_pos is decremented or incremented depending on key_dir.
void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, wchar_t (*hst)[MAX_STR_SIZE],
int hst_tot, int *hst_pos, int key_dir); resets line if at end of history */
void fetch_hist_item(ChatContext *ctx, int key_dir);