Change xkb_map_new_from_fd to use FILE* i.e. xkb_map_new_from_file. The reason is that flex only works with FILE's, so we must use fdopen on the file descriptor; but to avoid a memory leak, we must also fclose() it, which, in turn, closes the file descriptor itself. Either way is not acceptable, so we can either: * dup() the fd and use fdopen on that, or * have the user call fdopen on his own, and accept a FILE* instead of an fd. The second one seems better, and is standard C, so why not. We must add stdio.h to xkbcommon.h though, which is regrettable, but not a big deal. Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
diff --git a/include/xkbcommon/xkbcommon.h b/include/xkbcommon/xkbcommon.h
index 2b1eb96..e2786fe 100644
--- a/include/xkbcommon/xkbcommon.h
+++ b/include/xkbcommon/xkbcommon.h
@@ -82,6 +82,7 @@ THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stddef.h>
#include <stdint.h>
+#include <stdio.h>
#include <xkbcommon/xkbcommon-names.h>
#include <xkbcommon/xkbcommon-keysyms.h>
@@ -260,11 +261,11 @@ enum xkb_keymap_format {
/**
* Creates an XKB keymap from a full text XKB keymap passed into the
- * file descriptor.
+ * file.
*/
struct xkb_keymap *
-xkb_map_new_from_fd(struct xkb_context *context,
- int fd, enum xkb_keymap_format format,
+xkb_map_new_from_file(struct xkb_context *context,
+ FILE *file, enum xkb_keymap_format format,
enum xkb_map_compile_flags flags);
/**
diff --git a/src/xkbcomp/xkbcomp.c b/src/xkbcomp/xkbcomp.c
index 8518a1d..e6f71c9 100644
--- a/src/xkbcomp/xkbcomp.c
+++ b/src/xkbcomp/xkbcomp.c
@@ -283,36 +283,29 @@ xkb_map_new_from_string(struct xkb_context *ctx,
}
_X_EXPORT struct xkb_keymap *
-xkb_map_new_from_fd(struct xkb_context *ctx,
- int fd,
- enum xkb_keymap_format format,
- enum xkb_map_compile_flags flags)
+xkb_map_new_from_file(struct xkb_context *ctx,
+ FILE *file,
+ enum xkb_keymap_format format,
+ enum xkb_map_compile_flags flags)
{
- XkbFile *file;
- FILE *fptr;
+ XkbFile *xkb_file;
if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
ERROR("unsupported keymap format %d\n", format);
return NULL;
}
- if (fd < 0) {
+ if (!file) {
ERROR("no file specified to generate XKB keymap\n");
- return NULL;
- }
-
- fptr = fdopen(fd, "r");
- if (!fptr) {
- ERROR("couldn't associate fd with file pointer\n");
return NULL;
}
- if (!XKBParseFile(ctx, fptr, "(unknown file)", &file)) {
+ if (!XKBParseFile(ctx, file, "(unknown file)", &xkb_file)) {
ERROR("failed to parse input xkb file\n");
return NULL;
}
- return compile_keymap(ctx, file);
+ return compile_keymap(ctx, xkb_file);
}
_X_EXPORT struct xkb_keymap *
diff --git a/test/filecomp.c b/test/filecomp.c
index 5465b2c..7f140f5 100644
--- a/test/filecomp.c
+++ b/test/filecomp.c
@@ -25,34 +25,31 @@ authorization from the authors.
*/
#include <assert.h>
-#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#include <fcntl.h>
#include <limits.h>
-#include <sys/types.h>
-#include <sys/stat.h>
#include "xkbcommon/xkbcommon.h"
static int
test_file(const char *path)
{
- int fd;
+ FILE *file;
struct xkb_context *context;
struct xkb_keymap *keymap;
- fd = open(path, O_RDONLY);
- assert(fd >= 0);
+ file = fopen(path, "r");
+ assert(file != NULL);
context = xkb_context_new(0);
assert(context);
fprintf(stderr, "\nCompiling path: %s\n", path);
- keymap = xkb_map_new_from_fd(context, fd, XKB_KEYMAP_FORMAT_TEXT_V1, 0);
- close(fd);
+ keymap = xkb_map_new_from_file(context, file,
+ XKB_KEYMAP_FORMAT_TEXT_V1, 0);
+ fclose(file);
if (!keymap) {
fprintf(stderr, "Failed to compile keymap\n");