123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <faux/str.h>
- #include <faux/list.h>
- #include <klish/khelper.h>
- #include <klish/kcommand.h>
- #include <klish/kview.h>
- struct kview_s {
- char *name;
- faux_list_t *commands;
- };
- // Simple attributes
- KGET_STR(kview, name);
- KSET_STR_ONCE(kview, name);
- static int kview_command_compare(const void *first, const void *second)
- {
- const kcommand_t *f = (const kcommand_t *)first;
- const kcommand_t *s = (const kcommand_t *)second;
- return strcmp(kcommand_name(f), kcommand_name(s));
- }
- static int kview_command_kcompare(const void *key, const void *list_item)
- {
- const char *f = (const char *)key;
- const kcommand_t *s = (const kcommand_t *)list_item;
- return strcmp(f, kcommand_name(s));
- }
- static kview_t *kview_new_empty(void)
- {
- kview_t *view = NULL;
- view = faux_zmalloc(sizeof(*view));
- assert(view);
- if (!view)
- return NULL;
- // Initialize
- view->name = NULL;
- view->commands = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
- kview_command_compare, kview_command_kcompare,
- (void (*)(void *))kcommand_free);
- assert(view->commands);
- return view;
- }
- kview_t *kview_new(const iview_t *info, kview_error_e *error)
- {
- kview_t *view = NULL;
- view = kview_new_empty();
- assert(view);
- if (!view)
- return NULL;
- if (!info)
- return view;
- if (!kview_parse(view, info, error)) {
- kview_free(view);
- return NULL;
- }
- return view;
- }
- void kview_free(kview_t *view)
- {
- if (!view)
- return;
- faux_str_free(view->name);
- faux_list_free(view->commands);
- faux_free(view);
- }
- bool_t kview_add_command(kview_t *view, kcommand_t *command)
- {
- assert(view);
- if (!view)
- return BOOL_FALSE;
- assert(command);
- if (!command)
- return BOOL_FALSE;
- if (!faux_list_add(view->commands, command))
- return BOOL_FALSE;
- return BOOL_TRUE;
- }
- bool_t kview_parse(kview_t *view, const iview_t *info, kview_error_e *error)
- {
- view = view;
- info = info;
- error = error;
- return BOOL_TRUE;
- }
|