Refactor setting filter and use safe strncpy

This commit is contained in:
Christian Göttsche 2021-01-05 14:45:12 +01:00 committed by BenBE
parent a118928dee
commit 958112c5a3
3 changed files with 10 additions and 6 deletions

View File

@ -29,6 +29,13 @@ void IncSet_reset(IncSet* this, IncType type) {
IncMode_reset(&this->modes[type]); IncMode_reset(&this->modes[type]);
} }
void IncSet_setFilter(IncSet* this, const char* filter) {
IncMode* mode = &this->modes[INC_FILTER];
size_t len = String_safeStrncpy(mode->buffer, filter, sizeof(mode->buffer));
mode->index = len;
this->filtering = true;
}
static const char* const searchFunctions[] = {"Next ", "Prev ", "Cancel ", " Search: ", NULL}; static const char* const searchFunctions[] = {"Next ", "Prev ", "Cancel ", " Search: ", NULL};
static const char* const searchKeys[] = {"F3", "S-F3", "Esc", " "}; static const char* const searchKeys[] = {"F3", "S-F3", "Esc", " "};
static const int searchEvents[] = {KEY_F(3), KEY_F(15), 27, ERR}; static const int searchEvents[] = {KEY_F(3), KEY_F(15), 27, ERR};

View File

@ -40,6 +40,8 @@ static inline const char* IncSet_filter(const IncSet* this) {
return this->filtering ? this->modes[INC_FILTER].buffer : NULL; return this->filtering ? this->modes[INC_FILTER].buffer : NULL;
} }
void IncSet_setFilter(IncSet* this, const char* filter);
typedef const char* (*IncMode_GetPanelValue)(Panel*, int); typedef const char* (*IncMode_GetPanelValue)(Panel*, int);
void IncSet_reset(IncSet* this, IncType type); void IncSet_reset(IncSet* this, IncType type);

7
htop.c
View File

@ -248,13 +248,8 @@ static void setCommFilter(State* state, char** commFilter) {
MainPanel* panel = (MainPanel*)state->panel; MainPanel* panel = (MainPanel*)state->panel;
ProcessList* pl = state->pl; ProcessList* pl = state->pl;
IncSet* inc = panel->inc; IncSet* inc = panel->inc;
size_t maxlen = sizeof(inc->modes[INC_FILTER].buffer) - 1;
char* buffer = inc->modes[INC_FILTER].buffer;
strncpy(buffer, *commFilter, maxlen); IncSet_setFilter(inc, *commFilter);
buffer[maxlen] = 0;
inc->modes[INC_FILTER].index = strlen(buffer);
inc->filtering = true;
pl->incFilter = IncSet_filter(inc); pl->incFilter = IncSet_filter(inc);
free(*commFilter); free(*commFilter);