Check incoming pointer
[platform/upstream/kbd.git] / src / libkeymap / common.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4
5 #include "kbd.h"
6 #include "keymap.h"
7
8 static void attr_fmt45
9 lkmessage(const char *file attr_unused, int line attr_unused, const char *fn attr_unused,
10           const char *fmt, ...)
11 {
12         va_list ap;
13         va_start(ap, fmt);
14 #ifdef DEBUG
15         fprintf(stdout, "%s(%d): %s: ", file, line, fn);
16 #endif
17         vfprintf(stdout, fmt, ap);
18         fprintf(stdout, "\n");
19         va_end(ap);
20 }
21
22 static void attr_fmt45
23 lkerror(const char *file attr_unused, int line attr_unused, const char *fn attr_unused,
24         const char *fmt, ...)
25 {
26         va_list ap;
27         va_start(ap, fmt);
28         vfprintf(stderr, fmt, ap);
29         fprintf(stderr, "\n");
30         va_end(ap);
31 }
32
33 int
34 lk_init(struct keymap *kmap)
35 {
36         if (!kmap)
37                 return -1;
38
39         memset(kmap, 0, sizeof(struct keymap));
40
41         kmap->verbose     = LOG_NORMAL;
42         kmap->log_message = lkmessage;
43         kmap->log_error   = lkerror;
44
45         return 0;
46 }
47
48
49 void
50 lk_free(struct keymap *kmap)
51 {
52         int i;
53
54         if (!kmap)
55                 return;
56
57         for (i = 0; i < MAX_NR_KEYMAPS; i++) {
58                 if (kmap->keymap_was_set[i] != NULL)
59                         free(kmap->keymap_was_set[i]);
60                 if (kmap->key_map[i] != NULL)
61                         free(kmap->key_map[i]);
62         }
63
64         for (i = 0; i < MAX_NR_FUNC; i++) {
65                 if (kmap->func_table[i] != NULL)
66                         free(kmap->func_table[i]);
67         }
68 }