|
Packit Service |
50ad14 |
#include "config.h"
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
#include <string.h>
|
|
Packit Service |
50ad14 |
#include <stdlib.h>
|
|
Packit Service |
50ad14 |
#include <stdarg.h>
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
#include "keymap.h"
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
#include "kbd.h"
|
|
Packit Service |
50ad14 |
#include "nls.h"
|
|
Packit Service |
50ad14 |
#include "contextP.h"
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
void __attribute__((format(printf, 6, 7)))
|
|
Packit Service |
50ad14 |
lk_log(struct lk_ctx *ctx, int priority,
|
|
Packit Service |
50ad14 |
const char *file, int line, const char *fn,
|
|
Packit Service |
50ad14 |
const char *fmt, ...)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
va_list args;
|
|
Packit Service |
50ad14 |
if (ctx->log_fn == NULL)
|
|
Packit Service |
50ad14 |
return;
|
|
Packit Service |
50ad14 |
va_start(args, fmt);
|
|
Packit Service |
50ad14 |
ctx->log_fn(ctx->log_data, priority, file, line, fn, fmt, args);
|
|
Packit Service |
50ad14 |
va_end(args);
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
#ifndef DEBUG
|
|
Packit Service |
50ad14 |
#define log_unused __attribute__((unused))
|
|
Packit Service |
50ad14 |
#else
|
|
Packit Service |
50ad14 |
#define log_unused
|
|
Packit Service |
50ad14 |
#endif
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
static void __attribute__((format(printf, 6, 0)))
|
|
Packit Service |
50ad14 |
log_file(void *data,
|
|
Packit Service |
50ad14 |
int priority log_unused,
|
|
Packit Service |
50ad14 |
const char *file log_unused,
|
|
Packit Service |
50ad14 |
const int line log_unused,
|
|
Packit Service |
50ad14 |
const char *fn log_unused,
|
|
Packit Service |
50ad14 |
const char *format, va_list args)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
FILE *fp = data;
|
|
Packit Service |
50ad14 |
#ifdef DEBUG
|
|
Packit Service |
50ad14 |
char buf[16];
|
|
Packit Service |
50ad14 |
const char *priname;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
switch (priority) {
|
|
Packit Service |
50ad14 |
case LOG_EMERG:
|
|
Packit Service |
50ad14 |
priname = "EMERGENCY";
|
|
Packit Service |
50ad14 |
break;
|
|
Packit Service |
50ad14 |
case LOG_ALERT:
|
|
Packit Service |
50ad14 |
priname = "ALERT";
|
|
Packit Service |
50ad14 |
break;
|
|
Packit Service |
50ad14 |
case LOG_CRIT:
|
|
Packit Service |
50ad14 |
priname = "CRITICAL";
|
|
Packit Service |
50ad14 |
break;
|
|
Packit Service |
50ad14 |
case LOG_ERR:
|
|
Packit Service |
50ad14 |
priname = "ERROR";
|
|
Packit Service |
50ad14 |
break;
|
|
Packit Service |
50ad14 |
case LOG_WARNING:
|
|
Packit Service |
50ad14 |
priname = "WARNING";
|
|
Packit Service |
50ad14 |
break;
|
|
Packit Service |
50ad14 |
case LOG_NOTICE:
|
|
Packit Service |
50ad14 |
priname = "NOTICE";
|
|
Packit Service |
50ad14 |
break;
|
|
Packit Service |
50ad14 |
case LOG_INFO:
|
|
Packit Service |
50ad14 |
priname = "INFO";
|
|
Packit Service |
50ad14 |
break;
|
|
Packit Service |
50ad14 |
case LOG_DEBUG:
|
|
Packit Service |
50ad14 |
priname = "DEBUG";
|
|
Packit Service |
50ad14 |
break;
|
|
Packit Service |
50ad14 |
default:
|
|
Packit Service |
50ad14 |
snprintf(buf, sizeof(buf), "L:%d", priority);
|
|
Packit Service |
50ad14 |
priname = buf;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
fprintf(fp, "libkeymap: %s %s:%d %s: ", priname, file, line, fn);
|
|
Packit Service |
50ad14 |
#endif
|
|
Packit Service |
50ad14 |
vfprintf(fp, format, args);
|
|
Packit Service |
50ad14 |
fprintf(fp, "\n");
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
#undef log_unused
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_set_log_fn(struct lk_ctx *ctx,
|
|
Packit Service |
50ad14 |
void (*log_fn)(void *data, int priority,
|
|
Packit Service |
50ad14 |
const char *file, int line, const char *fn,
|
|
Packit Service |
50ad14 |
const char *format, va_list args),
|
|
Packit Service |
50ad14 |
const void *data)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (!ctx)
|
|
Packit Service |
50ad14 |
return -1;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
ctx->log_fn = log_fn;
|
|
Packit Service |
50ad14 |
ctx->log_data = (void *)data;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_get_log_priority(struct lk_ctx *ctx)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (!ctx)
|
|
Packit Service |
50ad14 |
return -1;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return ctx->log_priority;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_set_log_priority(struct lk_ctx *ctx, int priority)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (!ctx)
|
|
Packit Service |
50ad14 |
return -1;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
ctx->log_priority = priority;
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
lk_flags
|
|
Packit Service |
50ad14 |
lk_get_parser_flags(struct lk_ctx *ctx)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (!ctx)
|
|
Packit Service |
50ad14 |
return -1;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return ctx->flags;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_set_parser_flags(struct lk_ctx *ctx, lk_flags flags)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (!ctx)
|
|
Packit Service |
50ad14 |
return -1;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
ctx->flags = flags;
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
static int
|
|
Packit Service |
50ad14 |
init_array(struct lk_ctx *ctx, struct lk_array **arr, size_t size)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
int rc;
|
|
Packit Service |
50ad14 |
void *ptr;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
ptr = malloc(sizeof(struct lk_array));
|
|
Packit Service |
50ad14 |
if (!ptr) {
|
|
Packit Service |
50ad14 |
ERR(ctx, _("out of memory"));
|
|
Packit Service |
50ad14 |
return -1;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
rc = lk_array_init(ptr, size, 0);
|
|
Packit Service |
50ad14 |
if (rc < 0) {
|
|
Packit Service |
50ad14 |
ERR(ctx, _("unable to initialize array: %s"), strerror(rc));
|
|
Packit Service |
50ad14 |
return -1;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
*arr = ptr;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
struct lk_ctx *
|
|
Packit Service |
50ad14 |
lk_init(void)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
struct lk_ctx *ctx;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
ctx = malloc(sizeof(struct lk_ctx));
|
|
Packit Service |
50ad14 |
if (!ctx)
|
|
Packit Service |
50ad14 |
return NULL;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
memset(ctx, 0, sizeof(struct lk_ctx));
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
lk_set_log_fn(ctx, log_file, stderr);
|
|
Packit Service |
50ad14 |
lk_set_log_priority(ctx, LOG_ERR);
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (init_array(ctx, &ctx->keymap, sizeof(void *)) < 0 ||
|
|
Packit Service |
50ad14 |
init_array(ctx, &ctx->func_table, sizeof(void *)) < 0 ||
|
|
Packit Service |
50ad14 |
init_array(ctx, &ctx->accent_table, sizeof(void *)) < 0 ||
|
|
Packit Service |
50ad14 |
init_array(ctx, &ctx->key_constant, sizeof(char)) < 0 ||
|
|
Packit Service |
50ad14 |
init_array(ctx, &ctx->key_line, sizeof(int)) < 0) {
|
|
Packit Service |
50ad14 |
lk_free(ctx);
|
|
Packit Service |
50ad14 |
return NULL;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return ctx;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_free(struct lk_ctx *ctx)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
unsigned int i; //, j;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (!ctx)
|
|
Packit Service |
50ad14 |
return -1;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (ctx->keymap) {
|
|
Packit Service |
50ad14 |
for (i = 0; i < ctx->keymap->total; i++) {
|
|
Packit Service |
50ad14 |
struct lk_array *map;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
map = lk_array_get_ptr(ctx->keymap, i);
|
|
Packit Service |
50ad14 |
if (!map)
|
|
Packit Service |
50ad14 |
continue;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
lk_array_free(map);
|
|
Packit Service |
50ad14 |
free(map);
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
lk_array_free(ctx->keymap);
|
|
Packit Service |
50ad14 |
free(ctx->keymap);
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
ctx->keymap = NULL;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (ctx->func_table) {
|
|
Packit Service |
50ad14 |
for (i = 0; i < ctx->func_table->total; i++) {
|
|
Packit Service |
50ad14 |
char *ptr;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
ptr = lk_array_get_ptr(ctx->func_table, i);
|
|
Packit Service |
50ad14 |
if (!ptr)
|
|
Packit Service |
50ad14 |
continue;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
free(ptr);
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
lk_array_free(ctx->func_table);
|
|
Packit Service |
50ad14 |
free(ctx->func_table);
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
ctx->func_table = NULL;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (ctx->accent_table) {
|
|
Packit Service |
50ad14 |
for (i = 0; i < ctx->accent_table->total; i++) {
|
|
Packit Service |
50ad14 |
struct lk_array *ptr;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
ptr = lk_array_get_ptr(ctx->accent_table, i);
|
|
Packit Service |
50ad14 |
if (!ptr)
|
|
Packit Service |
50ad14 |
continue;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
free(ptr);
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
lk_array_free(ctx->accent_table);
|
|
Packit Service |
50ad14 |
free(ctx->accent_table);
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
ctx->accent_table = NULL;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (ctx->key_constant) {
|
|
Packit Service |
50ad14 |
lk_array_free(ctx->key_constant);
|
|
Packit Service |
50ad14 |
free(ctx->key_constant);
|
|
Packit Service |
50ad14 |
ctx->key_constant = NULL;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (ctx->key_line) {
|
|
Packit Service |
50ad14 |
lk_array_free(ctx->key_line);
|
|
Packit Service |
50ad14 |
free(ctx->key_line);
|
|
Packit Service |
50ad14 |
ctx->key_line = NULL;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|