#include <sys/cdefs.h>
__RCSID("$NetBSD: type_enum.c,v 1.13 2021/04/13 13:13:04 christos Exp $");
#include <ctype.h>
#include <stdlib.h>
#include <strings.h>
#include "form.h"
#include "internals.h"
static int
trim_blanks(char *field);
typedef struct
{
char **choices;
unsigned num_choices;
bool ignore_case;
bool exact;
} enum_args;
static int
trim_blanks(char *field)
{
int i;
i = (int) strlen(field);
if (i > 0)
i--;
else
return 0;
while ((i > 0) && isblank((unsigned char)field[i]))
i--;
return i;
}
static char *
create_enum_args(va_list *args)
{
enum_args *new;
char **choices;
new = malloc(sizeof(*new));
if (new == NULL)
return NULL;
new->choices = va_arg(*args, char **);
new->ignore_case = (va_arg(*args, int)) ? TRUE : FALSE;
new->exact = (va_arg(*args, int)) ? TRUE : FALSE;
_formi_dbg_printf("%s: ignore_case %d, no_blanks %d\n", __func__,
new->ignore_case, new->exact);
choices = new->choices;
new->num_choices = 0;
while (*choices != NULL) {
_formi_dbg_printf("%s: choice[%u] = \'%s\'\n", __func__,
new->num_choices, new->choices[new->num_choices]);
new->num_choices++;
choices++;
}
_formi_dbg_printf("%s: have %u choices\n", __func__,
new->num_choices);
return (void *) new;
}
static char *
copy_enum_args(char *args)
{
enum_args *new;
new = malloc(sizeof(*new));
if (new != NULL)
memcpy(new, args, sizeof(*new));
return (void *) new;
}
static void
free_enum_args(char *args)
{
if (args != NULL)
free(args);
}
static bool
match_enum(char **choices, unsigned num_choices, bool ignore_case,
bool exact, char *this, unsigned *match_num)
{
unsigned i, start, end, enum_start, blen, elen, enum_end;
bool cur_match;
start = _formi_skip_blanks(this, 0);
end = trim_blanks(this);
if (end >= start)
blen = (unsigned) (strlen(&this[start])
- strlen(&this[end]) + 1);
else
blen = 0;
_formi_dbg_printf("%s: start %u, blen %u\n", __func__, start, blen);
for (i = 0; i < num_choices; i++) {
enum_start = _formi_skip_blanks(choices[i], 0);
enum_end = trim_blanks(choices[i]);
if (enum_end >= enum_start)
elen = (unsigned) (strlen(&choices[i][enum_start])
- strlen(&choices[i][enum_end]) + 1);
else
elen = 0;
_formi_dbg_printf("%s: checking choice \'%s\'\n", __func__,
choices[i]);
_formi_dbg_printf("%s: enum_start %u, elen %u\n", __func__,
enum_start, elen);
if ((exact == TRUE) && (blen != elen))
continue;
if ((exact != TRUE) && (blen > elen))
continue;
if (ignore_case)
cur_match = (strncasecmp(&choices[i][enum_start],
&this[start],
(size_t)blen) == 0) ?
TRUE : FALSE;
else
cur_match = (strncmp(&choices[i][enum_start],
&this[start],
(size_t) blen) == 0) ?
TRUE : FALSE;
_formi_dbg_printf("%s: curmatch is %s\n", __func__,
(cur_match == TRUE)? "TRUE" : "FALSE");
if (cur_match == TRUE) {
*match_num = i;
return TRUE;
}
}
_formi_dbg_printf("%s: no match found\n", __func__);
return FALSE;
}
static int
enum_check_field(FIELD *field, char *args)
{
enum_args *ta;
unsigned match_num;
if (args == NULL)
return FALSE;
ta = (enum_args *) (void *) field->args;
if (match_enum(ta->choices, ta->num_choices, ta->ignore_case,
ta->exact, args, &match_num) == TRUE) {
_formi_dbg_printf("%s: We matched, match_num %u\n", __func__,
match_num);
_formi_dbg_printf("%s: buffer is \'%s\'\n", __func__,
ta->choices[match_num]);
set_field_buffer(field, 0, ta->choices[match_num]);
return TRUE;
}
return FALSE;
}
static int
next_enum(FIELD *field, char *args)
{
enum_args *ta;
unsigned cur_choice;
if (args == NULL)
return FALSE;
ta = (enum_args *) (void *) field->args;
_formi_dbg_printf("%s: attempt to match \'%s\'\n", __func__, args);
if (match_enum(ta->choices, ta->num_choices, ta->ignore_case,
ta->exact, args, &cur_choice) == FALSE) {
_formi_dbg_printf("%s: match failed\n", __func__);
return FALSE;
}
_formi_dbg_printf("%s: cur_choice is %u\n", __func__, cur_choice);
cur_choice++;
if (cur_choice >= ta->num_choices)
cur_choice = 0;
_formi_dbg_printf("%s: cur_choice is %u on exit\n", __func__,
cur_choice);
set_field_buffer(field, 0, ta->choices[cur_choice]);
return TRUE;
}
static int
prev_enum(FIELD *field, char *args)
{
enum_args *ta;
unsigned cur_choice;
if (args == NULL)
return FALSE;
ta = (enum_args *) (void *) field->args;
_formi_dbg_printf("%s: attempt to match \'%s\'\n", __func__, args);
if (match_enum(ta->choices, ta->num_choices, ta->ignore_case,
ta->exact, args, &cur_choice) == FALSE) {
_formi_dbg_printf("%s: match failed\n", __func__);
return FALSE;
}
_formi_dbg_printf("%s: cur_choice is %u\n", __func__, cur_choice);
if (cur_choice == 0)
cur_choice = ta->num_choices - 1;
else
cur_choice--;
_formi_dbg_printf("%s: cur_choice is %u on exit\n",
__func__, cur_choice);
set_field_buffer(field, 0, ta->choices[cur_choice]);
return TRUE;
}
static FIELDTYPE builtin_enum = {
_TYPE_HAS_ARGS | _TYPE_IS_BUILTIN,
0,
NULL,
create_enum_args,
copy_enum_args,
free_enum_args,
enum_check_field,
NULL,
next_enum,
prev_enum
};
FIELDTYPE *TYPE_ENUM = &builtin_enum;