Imported Upstream version 2.0.90
[platform/upstream/kbd.git] / src / libkeymap / func.c
1 /* func.c
2  *
3  * This file is part of kbd project.
4  * Copyright (C) 2014  Alexey Gladkov <gladkov.alexey@gmail.com>
5  *
6  * This file is covered by the GNU General Public License,
7  * which should be included with kbd as the file COPYING.
8  */
9 #include "config.h"
10
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "keymap.h"
15
16 #include "libcommon.h"
17 #include "contextP.h"
18
19 int lk_func_exists(struct lk_ctx *ctx, int index)
20 {
21         return (lk_array_get_ptr(ctx->func_table, index) != NULL);
22 }
23
24 int lk_get_func(struct lk_ctx *ctx, struct kbsentry *kbs)
25 {
26         char *s;
27
28         s = lk_array_get_ptr(ctx->func_table, kbs->kb_func);
29         if (!s) {
30                 ERR(ctx, _("func %d not allocated"), kbs->kb_func);
31                 return -1;
32         }
33
34         strncpy((char *)kbs->kb_string, s, sizeof(kbs->kb_string));
35         kbs->kb_string[sizeof(kbs->kb_string) - 1] = 0;
36
37         return 0;
38 }
39
40 int lk_add_func(struct lk_ctx *ctx, struct kbsentry *kbs)
41 {
42         char *s;
43
44         s = lk_array_get_ptr(ctx->func_table, kbs->kb_func);
45         if (s)
46                 free(s);
47
48         s = strdup((char *)kbs->kb_string);
49
50         if (lk_array_set(ctx->func_table, kbs->kb_func, &s) < 0) {
51                 free(s);
52                 ERR(ctx, _("out of memory"));
53                 return -1;
54         }
55
56         return 0;
57 }
58
59 int lk_del_func(struct lk_ctx *ctx, int index)
60 {
61         if (lk_array_unset(ctx->func_table, index) < 0) {
62                 ERR(ctx, _("Unable to remove item from the list of functions"));
63                 return -1;
64         }
65
66         return 0;
67 }