From b73bd676156fc36fe1859661348511bdd55610d8 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sun, 13 May 2012 09:49:08 +0300 Subject: [PATCH] rules: only export a single function Really all we need from this file is a way to get xkb_component_names from an xkb_rule_names, which is now the only thing being exposed. This should allow for some much needed refactoring of this code. Since this is only used by xkbcomp.c and uses xkbcomp functions, also move rules.{c,h} under the xkbcomp dir. Signed-off-by: Ran Benita --- Makefile.am | 4 +- src/rules.h | 107 ---------------------------------- src/{ => xkbcomp}/rules.c | 143 ++++++++++++++++++++++++++++++++++++++++++---- src/xkbcomp/rules.h | 36 ++++++++++++ src/xkbcomp/xkbcomp.c | 82 ++++---------------------- 5 files changed, 180 insertions(+), 192 deletions(-) delete mode 100644 src/rules.h rename src/{ => xkbcomp}/rules.c (86%) create mode 100644 src/xkbcomp/rules.h diff --git a/Makefile.am b/Makefile.am index 0da3b79..cef9790 100644 --- a/Makefile.am +++ b/Makefile.am @@ -59,6 +59,8 @@ libxkbcommon_la_SOURCES = \ src/xkbcomp/parseutils.h \ src/xkbcomp/path.c \ src/xkbcomp/path.h \ + src/xkbcomp/rules.c \ + src/xkbcomp/rules.h \ src/xkbcomp/scanner.l \ src/xkbcomp/symbols.c \ src/xkbcomp/vmod.c \ @@ -74,8 +76,6 @@ libxkbcommon_la_SOURCES = \ src/keysym.c \ src/map.c \ src/misc.c \ - src/rules.c \ - src/rules.h \ src/state.c \ src/text.c \ src/text.h \ diff --git a/src/rules.h b/src/rules.h deleted file mode 100644 index ecc66aa..0000000 --- a/src/rules.h +++ /dev/null @@ -1,107 +0,0 @@ -/* -Copyright 2009 Dan Nicholson - -Permission is hereby granted, free of charge, to any person obtaining a -copy of this software and associated documentation files (the "Software"), -to deal in the Software without restriction, including without limitation -the rights to use, copy, modify, merge, publish, distribute, sublicense, -and/or sell copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -Except as contained in this notice, the names of the authors or their -institutions shall not be used in advertising or otherwise to promote the -sale, use or other dealings in this Software without prior written -authorization from the authors. -*/ - -#ifndef RULES_H -#define RULES_H - -#include - -#include "xkb-priv.h" - -/* From filecommon */ - -typedef struct _XkbRF_VarDefs { - const char * model; - const char * layout; - const char * variant; - const char * options; -} XkbRF_VarDefsRec,*XkbRF_VarDefsPtr; - -typedef struct _XkbRF_VarDesc { - char * name; - char * desc; -} XkbRF_VarDescRec, *XkbRF_VarDescPtr; - -typedef struct _XkbRF_DescribeVars { - size_t sz_desc; - size_t num_desc; - XkbRF_VarDescPtr desc; -} XkbRF_DescribeVarsRec,*XkbRF_DescribeVarsPtr; - -typedef struct _XkbRF_Rule { - int number; - int layout_num; - int variant_num; - char * model; - char * layout; - char * variant; - char * option; - /* yields */ - char * keycodes; - char * symbols; - char * types; - char * compat; - char * keymap; - unsigned flags; -} XkbRF_RuleRec,*XkbRF_RulePtr; - -typedef struct _XkbRF_Group { - int number; - char * name; - char * words; -} XkbRF_GroupRec, *XkbRF_GroupPtr; - -#define XkbRF_PendingMatch (1L<<1) -#define XkbRF_Option (1L<<2) -#define XkbRF_Append (1L<<3) -#define XkbRF_Normal (1L<<4) -#define XkbRF_Invalid (1L<<5) - -typedef struct _XkbRF_Rules { - XkbRF_DescribeVarsRec models; - XkbRF_DescribeVarsRec layouts; - XkbRF_DescribeVarsRec variants; - XkbRF_DescribeVarsRec options; - - size_t sz_rules; - size_t num_rules; - XkbRF_RulePtr rules; - size_t sz_groups; - size_t num_groups; - XkbRF_GroupPtr groups; -} XkbRF_RulesRec, *XkbRF_RulesPtr; - -extern bool -XkbcRF_GetComponents(XkbRF_RulesPtr rules, XkbRF_VarDefsPtr defs, - struct xkb_component_names * names); - -extern bool -XkbcRF_LoadRules(FILE *file, XkbRF_RulesPtr rules); - -extern void -XkbcRF_Free(XkbRF_RulesPtr rules); - -#endif /* RULES_H */ diff --git a/src/rules.c b/src/xkbcomp/rules.c similarity index 86% rename from src/rules.c rename to src/xkbcomp/rules.c index b0d4d2f..48a3693 100644 --- a/src/rules.c +++ b/src/xkbcomp/rules.c @@ -187,6 +187,13 @@ static const char * cname[MAX_WORDS] = { "keycodes", "symbols", "types", "compat", "geometry", "keymap" }; +typedef struct { + const char *model; + const char *layout; + const char *variant; + const char *options; +} XkbRF_VarDefsRec, *XkbRF_VarDefsPtr; + typedef struct _RemapSpec { int number; size_t num_remap; @@ -208,6 +215,60 @@ typedef struct { char * options; } XkbRF_MultiDefsRec, *XkbRF_MultiDefsPtr; +typedef struct _XkbRF_VarDesc { + char * name; + char * desc; +} XkbRF_VarDescRec, *XkbRF_VarDescPtr; + +typedef struct _XkbRF_DescribeVars { + size_t sz_desc; + size_t num_desc; + XkbRF_VarDescPtr desc; +} XkbRF_DescribeVarsRec,*XkbRF_DescribeVarsPtr; + +typedef struct _XkbRF_Group { + int number; + char * name; + char * words; +} XkbRF_GroupRec, *XkbRF_GroupPtr; + +#define XkbRF_PendingMatch (1L<<1) +#define XkbRF_Option (1L<<2) +#define XkbRF_Append (1L<<3) +#define XkbRF_Normal (1L<<4) +#define XkbRF_Invalid (1L<<5) + +typedef struct _XkbRF_Rule { + int number; + int layout_num; + int variant_num; + char * model; + char * layout; + char * variant; + char * option; + /* yields */ + char * keycodes; + char * symbols; + char * types; + char * compat; + char * keymap; + unsigned flags; +} XkbRF_RuleRec,*XkbRF_RulePtr; + +typedef struct XkbRF_Rules { + XkbRF_DescribeVarsRec models; + XkbRF_DescribeVarsRec layouts; + XkbRF_DescribeVarsRec variants; + XkbRF_DescribeVarsRec options; + + size_t sz_rules; + size_t num_rules; + XkbRF_RulePtr rules; + size_t sz_groups; + size_t num_groups; + XkbRF_GroupPtr groups; +} XkbRF_RulesRec, *XkbRF_RulesPtr; + #define NDX_BUFF_SIZE 4 /***====================================================================***/ @@ -491,7 +552,7 @@ squeeze_spaces(char *p1) } static bool -MakeMultiDefs(XkbRF_MultiDefsPtr mdefs, XkbRF_VarDefsPtr defs) +MakeMultiDefs(XkbRF_MultiDefsPtr mdefs, const XkbRF_VarDefsPtr defs) { memset(mdefs, 0, sizeof(XkbRF_MultiDefsRec)); mdefs->model = defs->model; @@ -827,10 +888,9 @@ XkbRF_SubstituteVars(char *name, XkbRF_MultiDefsPtr mdefs) /***====================================================================***/ -bool -XkbcRF_GetComponents( XkbRF_RulesPtr rules, - XkbRF_VarDefsPtr defs, - struct xkb_component_names * names) +static bool +XkbcRF_GetComponents(struct XkbRF_Rules *rules, const XkbRF_VarDefsPtr defs, + struct xkb_component_names *names) { XkbRF_MultiDefsRec mdefs; @@ -899,16 +959,19 @@ XkbcRF_AddGroup(XkbRF_RulesPtr rules) return &rules->groups[rules->num_groups++]; } -bool -XkbcRF_LoadRules(FILE *file, XkbRF_RulesPtr rules) +static XkbRF_RulesPtr +XkbcRF_LoadRules(FILE *file) { InputLine line; RemapSpec remap; XkbRF_RuleRec trule,*rule; XkbRF_GroupRec tgroup,*group; + XkbRF_RulesPtr rules; + + rules = calloc(1, sizeof(*rules)); + if (!rules) + return NULL; - if (!(rules && file)) - return false; memset(&remap, 0, sizeof(RemapSpec)); memset(&tgroup, 0, sizeof(XkbRF_GroupRec)); InitInputLine(&line); @@ -929,7 +992,7 @@ XkbRF_GroupRec tgroup,*group; line.num_line= 0; } FreeInputLine(&line); - return true; + return rules; } static void @@ -946,7 +1009,7 @@ XkbRF_ClearVarDescriptions(XkbRF_DescribeVarsPtr var) var->desc= NULL; } -void +static void XkbcRF_Free(XkbRF_RulesPtr rules) { int i; @@ -981,3 +1044,61 @@ XkbcRF_Free(XkbRF_RulesPtr rules) free(rules); } + +struct xkb_component_names * +xkb_components_from_rules(struct xkb_context *ctx, + const struct xkb_rule_names *rmlvo) +{ + int i; + FILE *rulesFile; + char *rulesPath; + XkbRF_RulesPtr loaded; + struct xkb_component_names *names = NULL; + XkbRF_VarDefsRec defs = { + .model = rmlvo->model, + .layout = rmlvo->layout, + .variant = rmlvo->variant, + .options = rmlvo->options, + }; + + rulesFile = XkbFindFileInPath(ctx, rmlvo->rules, XkmRulesFile, + &rulesPath); + if (!rulesFile) { + ERROR("could not find \"%s\" rules in XKB path\n", rmlvo->rules); + ERROR("%d include paths searched:\n", + xkb_context_num_include_paths(ctx)); + for (i = 0; i < xkb_context_num_include_paths(ctx); i++) + ERROR("\t%s\n", xkb_context_include_path_get(ctx, i)); + return NULL; + } + + loaded = XkbcRF_LoadRules(rulesFile); + if (!loaded) { + ERROR("failed to load XKB rules \"%s\"\n", rulesPath); + goto unwind_file; + } + + names = calloc(1, sizeof(*names)); + if (!names) { + ERROR("failed to allocate XKB components\n"); + goto unwind_file; + } + + if (!XkbcRF_GetComponents(loaded, &defs, names)) { + free(names->keymap); + free(names->keycodes); + free(names->types); + free(names->compat); + free(names->symbols); + free(names); + names = NULL; + ERROR("no components returned from XKB rules \"%s\"\n", rulesPath); + } + +unwind_file: + XkbcRF_Free(loaded); + if (rulesFile) + fclose(rulesFile); + free(rulesPath); + return names; +} diff --git a/src/xkbcomp/rules.h b/src/xkbcomp/rules.h new file mode 100644 index 0000000..d2d655a --- /dev/null +++ b/src/xkbcomp/rules.h @@ -0,0 +1,36 @@ +/* +Copyright 2009 Dan Nicholson + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the names of the authors or their +institutions shall not be used in advertising or otherwise to promote the +sale, use or other dealings in this Software without prior written +authorization from the authors. +*/ + +#ifndef RULES_H +#define RULES_H + +#include "xkbcomp-priv.h" + +struct xkb_component_names * +xkb_components_from_rules(struct xkb_context *ctx, + const struct xkb_rule_names *rmlvo); + +#endif /* RULES_H */ diff --git a/src/xkbcomp/xkbcomp.c b/src/xkbcomp/xkbcomp.c index 68e180f..5242c13 100644 --- a/src/xkbcomp/xkbcomp.c +++ b/src/xkbcomp/xkbcomp.c @@ -27,7 +27,6 @@ authorization from the authors. #include "xkbcomp-priv.h" #include "rules.h" #include "parseutils.h" -#include "path.h" /* Global warning level */ unsigned int warningLevel = 0; @@ -65,68 +64,12 @@ XkbKeymapFileFromComponents(struct xkb_context *ctx, &keycodes->common, 0); } -static struct xkb_component_names * -XkbComponentsFromRules(struct xkb_context *ctx, - const char *rules, - const XkbRF_VarDefsPtr defs) -{ - FILE *rulesFile = NULL; - char *rulesPath = NULL; - XkbRF_RulesPtr loaded = NULL; - struct xkb_component_names * names = NULL; - int i; - - rulesFile = XkbFindFileInPath(ctx, rules, XkmRulesFile, &rulesPath); - if (!rulesFile) { - ERROR("could not find \"%s\" rules in XKB path\n", rules); - ERROR("%d include paths searched:\n", - xkb_context_num_include_paths(ctx)); - for (i = 0; i < xkb_context_num_include_paths(ctx); i++) - ERROR("\t%s\n", xkb_context_include_path_get(ctx, i)); - return NULL; - } - - if (!(loaded = uTypedCalloc(1, XkbRF_RulesRec))) { - ERROR("failed to allocate XKB rules\n"); - goto unwind_file; - } - - if (!XkbcRF_LoadRules(rulesFile, loaded)) { - ERROR("failed to load XKB rules \"%s\"\n", rulesPath); - goto unwind_file; - } - - if (!(names = uTypedCalloc(1, struct xkb_component_names))) { - ERROR("failed to allocate XKB components\n"); - goto unwind_file; - } - - if (!XkbcRF_GetComponents(loaded, defs, names)) { - free(names->keymap); - free(names->keycodes); - free(names->types); - free(names->compat); - free(names->symbols); - free(names); - names = NULL; - ERROR("no components returned from XKB rules \"%s\"\n", rulesPath); - } - -unwind_file: - XkbcRF_Free(loaded); - if (rulesFile) - fclose(rulesFile); - free(rulesPath); - return names; -} - _X_EXPORT struct xkb_keymap * xkb_map_new_from_names(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo, enum xkb_map_compile_flags flags) { - XkbRF_VarDefsRec defs; - struct xkb_component_names *names; + struct xkb_component_names *kkctgs; struct xkb_keymap *keymap; if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) { @@ -134,26 +77,21 @@ xkb_map_new_from_names(struct xkb_context *ctx, return NULL; } - defs.model = rmlvo->model; - defs.layout = rmlvo->layout; - defs.variant = rmlvo->variant; - defs.options = rmlvo->options; - - names = XkbComponentsFromRules(ctx, rmlvo->rules, &defs); - if (!names) { + kkctgs = xkb_components_from_rules(ctx, rmlvo); + if (!kkctgs) { ERROR("failed to generate XKB components from rules \"%s\"\n", rmlvo->rules); return NULL; } - keymap = xkb_map_new_from_kccgst(ctx, names, 0); + keymap = xkb_map_new_from_kccgst(ctx, kkctgs, 0); - free(names->keymap); - free(names->keycodes); - free(names->types); - free(names->compat); - free(names->symbols); - free(names); + free(kkctgs->keymap); + free(kkctgs->keycodes); + free(kkctgs->types); + free(kkctgs->compat); + free(kkctgs->symbols); + free(kkctgs); return keymap; } -- 2.7.4