Use enum for file types

enums are nice for some type safety and readability. This one also
removes the distinction between file type mask / file type index and
some naming consistency.

Signed-off-by: Ran Benita <ran234@gmail.com>
master
Ran Benita 2012-06-29 17:05:33 +03:00
parent 8f257397af
commit 48b4d30aa3
17 changed files with 112 additions and 109 deletions

View File

@ -186,19 +186,21 @@ XkbcModMaskText(unsigned mask, bool cFormat)
}
const char *
XkbcConfigText(unsigned config)
XkbcFileTypeText(enum xkb_file_type type)
{
switch (config) {
case XkmKeymapFile:
switch (type) {
case FILE_TYPE_KEYMAP:
return "Keymap";
case XkmTypesIndex:
case FILE_TYPE_TYPES:
return "Types";
case XkmCompatMapIndex:
case FILE_TYPE_COMPAT:
return "CompatMap";
case XkmSymbolsIndex:
case FILE_TYPE_SYMBOLS:
return "Symbols";
case XkmKeyNamesIndex:
case FILE_TYPE_KEYCODES:
return "KeyNames";
case FILE_TYPE_RULES:
return "Rules";
default:
return "unknown";
}

View File

@ -39,7 +39,7 @@ extern const char *
XkbcModMaskText(unsigned mask, bool cFormat);
extern const char *
XkbcConfigText(unsigned config);
XkbcFileTypeText(enum xkb_file_type type);
extern const char *
XkbcActionTypeText(unsigned type);

View File

@ -89,25 +89,24 @@ THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "utils.h"
#include "darray.h"
/* From XKM.h */
#define XkmKeymapFile 22
#define XkmRulesFile 24
enum xkb_file_type {
/* The top level file which includes the other component files. */
FILE_TYPE_KEYMAP = (1 << 0),
#define XkmTypesIndex 0
#define XkmCompatMapIndex 1
#define XkmSymbolsIndex 2
#define XkmKeyNamesIndex 4
#define XkmGeometryIndex 5
/* Component files. */
FILE_TYPE_TYPES = (1 << 1),
FILE_TYPE_COMPAT = (1 << 2),
FILE_TYPE_SYMBOLS = (1 << 3),
FILE_TYPE_KEYCODES = (1 << 4),
FILE_TYPE_GEOMETRY = (1 << 5),
#define XkmTypesMask (1<<0)
#define XkmCompatMapMask (1<<1)
#define XkmSymbolsMask (1<<2)
#define XkmKeyNamesMask (1<<4)
#define XkmGeometryMask (1<<5)
/* This one doesn't mix with the others, but useful here as well. */
FILE_TYPE_RULES = (1 << 6),
};
#define XkmKeymapRequired (XkmCompatMapMask|XkmKeyNamesMask|XkmSymbolsMask|XkmTypesMask)
#define XkmKeymapOptional ((XkmTypesMask|XkmGeometryMask)&(~XkmKeymapRequired))
#define XkmKeymapLegal (XkmKeymapRequired|XkmKeymapOptional)
/* Files needed for a complete keymap. */
#define REQUIRED_FILE_TYPES (FILE_TYPE_TYPES | FILE_TYPE_COMPAT | FILE_TYPE_SYMBOLS | FILE_TYPE_KEYCODES)
#define LEGAL_FILE_TYPES (FILE_TYPE_GEOMETRY | REQUIRED_FILE_TYPES)
/**
* Legacy names for the components of an XKB keymap, also known as KcCGST.

View File

@ -404,7 +404,7 @@ HandleIncludeCompatMap(IncludeStmt *stmt, struct xkb_keymap *keymap,
included = *info;
memset(info, 0, sizeof(CompatInfo));
}
else if (ProcessIncludeFile(keymap->ctx, stmt, XkmCompatMapIndex, &rtrn,
else if (ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_COMPAT, &rtrn,
&newMerge))
{
InitCompatInfo(&included, keymap);
@ -445,7 +445,7 @@ HandleIncludeCompatMap(IncludeStmt *stmt, struct xkb_keymap *keymap,
MergeIncludedCompatMaps(&included, info, next->merge);
ClearCompatInfo(info, keymap);
}
else if (ProcessIncludeFile(keymap->ctx, next, XkmCompatMapIndex,
else if (ProcessIncludeFile(keymap->ctx, next, FILE_TYPE_COMPAT,
&rtrn, &op))
{
InitCompatInfo(&next_incl, keymap);

View File

@ -511,7 +511,7 @@ HandleIncludeKeycodes(IncludeStmt *stmt, struct xkb_keymap *keymap,
info->explicitMax = XKB_KEYCODE_MAX;
return (info->errorCount == 0);
} /* parse file, store returned info in the xkb struct */
else if (ProcessIncludeFile(keymap->ctx, stmt, XkmKeyNamesIndex, &rtrn,
else if (ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_KEYCODES, &rtrn,
&newMerge))
{
InitKeyNamesInfo(&included);
@ -544,7 +544,7 @@ HandleIncludeKeycodes(IncludeStmt *stmt, struct xkb_keymap *keymap,
MergeIncludedKeycodes(&included, keymap, info, next->merge);
ClearKeyNamesInfo(info);
}
else if (ProcessIncludeFile(keymap->ctx, next, XkmKeyNamesIndex,
else if (ProcessIncludeFile(keymap->ctx, next, FILE_TYPE_KEYCODES,
&rtrn, &op))
{
InitKeyNamesInfo(&next_incl);

View File

@ -30,14 +30,14 @@
/**
* Compile the given file and store the output in xkb.
* @param file A list of XkbFiles, each denoting one type (e.g.
* XkmKeyNamesIdx, etc.)
* FILE_TYPE_KEYCODES, etc.)
*/
struct xkb_keymap *
CompileKeymap(struct xkb_context *ctx, XkbFile *file)
{
unsigned have = 0;
bool ok;
unsigned mainType;
enum xkb_file_type mainType;
const char *mainName;
LEDInfo *unbound = NULL, *next;
struct xkb_keymap *keymap = XkbcAllocKeyboard(ctx);
@ -55,55 +55,52 @@ CompileKeymap(struct xkb_context *ctx, XkbFile *file)
mainName = file->name ? file->name : "(unnamed)";
/*
* Other aggregate file types are converted to XkmKeymapFile
* Other aggregate file types are converted to FILE_TYPE_KEYMAP
* in the parser.
*/
if (mainType != XkmKeymapFile) {
if (mainType != FILE_TYPE_KEYMAP) {
ERROR("Cannot compile a %s file alone into a keymap\n",
XkbcConfigText(mainType));
return false;
XkbcFileTypeText(mainType));
goto err;
}
/* Check for duplicate entries in the input file */
for (file = (XkbFile *) file->defs; file; file = (XkbFile *) file->common.next)
{
if ((have & (1 << file->type)) != 0)
{
if (have & file->type) {
ERROR("More than one %s section in a %s file\n",
XkbcConfigText(file->type), XkbcConfigText(mainType));
XkbcFileTypeText(file->type), XkbcFileTypeText(mainType));
ACTION("All sections after the first ignored\n");
continue;
}
else if ((1 << file->type) & (~XkmKeymapLegal))
{
else if (!(file->type & LEGAL_FILE_TYPES)) {
ERROR("Cannot define %s in a %s file\n",
XkbcConfigText(file->type), XkbcConfigText(mainType));
XkbcFileTypeText(file->type), XkbcFileTypeText(mainType));
continue;
}
switch (file->type)
{
case XkmKeyNamesIndex:
switch (file->type) {
case FILE_TYPE_KEYCODES:
sections.keycodes = file;
break;
case XkmTypesIndex:
case FILE_TYPE_TYPES:
sections.types = file;
break;
case XkmSymbolsIndex:
case FILE_TYPE_SYMBOLS:
sections.symbols = file;
break;
case XkmCompatMapIndex:
case FILE_TYPE_COMPAT:
sections.compat = file;
break;
case XkmGeometryIndex:
case FILE_TYPE_GEOMETRY:
continue;
default:
WSGO("Unknown file type %d\n", file->type);
ACTION("Ignored\n");
continue;
case XkmKeymapFile:
case FILE_TYPE_KEYMAP:
WSGO("Illegal %s configuration in a %s file\n",
XkbcConfigText(file->type), XkbcConfigText(mainType));
XkbcFileTypeText(file->type), XkbcFileTypeText(mainType));
ACTION("Ignored\n");
continue;
}
@ -113,22 +110,23 @@ CompileKeymap(struct xkb_context *ctx, XkbFile *file)
file->topName = strdup(mainName);
}
have |= (1 << file->type);
have |= file->type;
}
if (XkmKeymapRequired & (~have))
{
int i, bit;
unsigned missing;
missing = XkmKeymapRequired & (~have);
for (i = 0, bit = 1; missing != 0; i++, bit <<= 1)
{
if (missing & bit)
{
ERROR("Required section %s missing from keymap\n", XkbcConfigText(i));
if (REQUIRED_FILE_TYPES & (~have)) {
enum xkb_file_type bit;
enum xkb_file_type missing;
missing = REQUIRED_FILE_TYPES & (~have);
for (bit = 1; missing != 0; bit <<= 1) {
if (missing & bit) {
ERROR("Required section %s missing from keymap\n",
XkbcFileTypeText(bit));
missing &= ~bit;
}
}
goto err;
}

View File

@ -346,7 +346,7 @@ HandleIncludeKeyTypes(IncludeStmt *stmt, struct xkb_keymap *keymap,
included = *info;
memset(info, 0, sizeof(KeyTypesInfo));
}
else if (ProcessIncludeFile(keymap->ctx, stmt, XkmTypesIndex, &rtrn,
else if (ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_TYPES, &rtrn,
&newMerge))
{
InitKeyTypesInfo(&included, keymap, info);
@ -381,7 +381,7 @@ HandleIncludeKeyTypes(IncludeStmt *stmt, struct xkb_keymap *keymap,
MergeIncludedKeyTypes(&included, info, next->merge, keymap);
FreeKeyTypesInfo(info);
}
else if (ProcessIncludeFile(keymap->ctx, next, XkmTypesIndex,
else if (ProcessIncludeFile(keymap->ctx, next, FILE_TYPE_TYPES,
&rtrn, &op))
{
InitKeyTypesInfo(&next_incl, keymap, &included);

View File

@ -38,7 +38,7 @@
*
* @param ctx The ctx containing include paths
* @param stmt The include statement, specifying the file name to look for.
* @param file_type Type of file (XkmKeyNamesIdx, etc.)
* @param file_type Type of file (FILE_TYPE_KEYCODES, etc.)
* @param file_rtrn Returns the key map to be used.
* @param merge_rtrn Always returns stmt->merge.
*
@ -47,7 +47,7 @@
bool
ProcessIncludeFile(struct xkb_context *ctx,
IncludeStmt * stmt,
unsigned file_type,
enum xkb_file_type file_type,
XkbFile ** file_rtrn, unsigned *merge_rtrn)
{
FILE *file;
@ -92,7 +92,7 @@ ProcessIncludeFile(struct xkb_context *ctx,
if (!mapToUse)
{
ERROR("No %s named \"%s\" in the include file \"%s\"\n",
XkbcConfigText(file_type), stmt->map, stmt->file);
XkbcFileTypeText(file_type), stmt->map, stmt->file);
return false;
}
}
@ -105,7 +105,7 @@ ProcessIncludeFile(struct xkb_context *ctx,
if (mapToUse->type != file_type)
{
ERROR("Include file wrong type (expected %s, got %s)\n",
XkbcConfigText(file_type), XkbcConfigText(mapToUse->type));
XkbcFileTypeText(file_type), XkbcFileTypeText(mapToUse->type));
ACTION("Include file \"%s\" ignored\n", stmt->file);
return false;
}

View File

@ -113,6 +113,7 @@ extern int yylex(union YYSTYPE *val, struct YYLTYPE *loc, void *scanner);
int ival;
unsigned uval;
int64_t num;
enum xkb_file_type file_type;
char *str;
xkb_atom_t sval;
ParseCommon *any;
@ -134,7 +135,8 @@ extern int yylex(union YYSTYPE *val, struct YYLTYPE *loc, void *scanner);
%type <num> INTEGER FLOAT
%type <str> IDENT KEYNAME STRING
%type <ival> Number Integer Float SignedNumber
%type <uval> XkbCompositeType FileType MergeMode OptMergeMode
%type <uval> MergeMode OptMergeMode
%type <file_type> XkbCompositeType FileType
%type <uval> DoodadType Flag Flags OptFlags KeyCode
%type <str> KeyName MapName OptMapName KeySym
%type <sval> FieldSpec Ident Element String
@ -181,9 +183,9 @@ XkbCompositeMap : OptFlags XkbCompositeType OptMapName OBRACE
}
;
XkbCompositeType: XKB_KEYMAP { $$= XkmKeymapFile; }
| XKB_SEMANTICS { $$= XkmKeymapFile; }
| XKB_LAYOUT { $$= XkmKeymapFile; }
XkbCompositeType: XKB_KEYMAP { $$= FILE_TYPE_KEYMAP; }
| XKB_SEMANTICS { $$= FILE_TYPE_KEYMAP; }
| XKB_LAYOUT { $$= FILE_TYPE_KEYMAP; }
;
XkbMapConfigList : XkbMapConfigList XkbMapConfig
@ -201,7 +203,7 @@ XkbMapConfig : OptFlags FileType OptMapName OBRACE
DeclList
CBRACE SEMI
{
if ($2 == XkmGeometryIndex)
if ($2 == FILE_TYPE_GEOMETRY)
{
free($3);
FreeStmt($5);
@ -216,7 +218,7 @@ XkbMapConfig : OptFlags FileType OptMapName OBRACE
XkbConfig : OptFlags FileType OptMapName DeclList
{
if ($2 == XkmGeometryIndex)
if ($2 == FILE_TYPE_GEOMETRY)
{
free($3);
FreeStmt($4);
@ -230,11 +232,11 @@ XkbConfig : OptFlags FileType OptMapName DeclList
;
FileType : XKB_KEYCODES { $$= XkmKeyNamesIndex; }
| XKB_TYPES { $$= XkmTypesIndex; }
| XKB_COMPATMAP { $$= XkmCompatMapIndex; }
| XKB_SYMBOLS { $$= XkmSymbolsIndex; }
| XKB_GEOMETRY { $$= XkmGeometryIndex; }
FileType : XKB_KEYCODES { $$= FILE_TYPE_KEYCODES; }
| XKB_TYPES { $$= FILE_TYPE_TYPES; }
| XKB_COMPATMAP { $$= FILE_TYPE_COMPAT; }
| XKB_SYMBOLS { $$= FILE_TYPE_SYMBOLS; }
| XKB_GEOMETRY { $$= FILE_TYPE_GEOMETRY; }
;
OptFlags : Flags { $$= $1; }

View File

@ -606,7 +606,7 @@ EnsureSafeMapName(char *name)
}
XkbFile *
CreateXKBFile(struct xkb_context *ctx, int type, char *name,
CreateXKBFile(struct xkb_context *ctx, enum xkb_file_type type, char *name,
ParseCommon *defs, unsigned flags)
{
XkbFile *file;
@ -771,16 +771,18 @@ FreeXKBFile(XkbFile *file)
switch (file->type)
{
case XkmKeymapFile:
case FILE_TYPE_KEYMAP:
FreeXKBFile((XkbFile *)file->defs);
break;
case XkmTypesIndex:
case XkmCompatMapIndex:
case XkmSymbolsIndex:
case XkmKeyNamesIndex:
case XkmGeometryIndex:
case FILE_TYPE_TYPES:
case FILE_TYPE_COMPAT:
case FILE_TYPE_SYMBOLS:
case FILE_TYPE_KEYCODES:
case FILE_TYPE_GEOMETRY:
FreeStmt(file->defs);
break;
default:
break;
}
free(file->name);

View File

@ -120,7 +120,7 @@ extern void
CheckDefaultMap(XkbFile *maps, const char *fileName);
extern XkbFile *
CreateXKBFile(struct xkb_context *ctx, int type, char *name,
CreateXKBFile(struct xkb_context *ctx, enum xkb_file_type type, char *name,
ParseCommon *defs, unsigned flags);
extern bool

View File

@ -142,23 +142,23 @@ XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
* Return the xkb directory based on the type.
*/
const char *
XkbDirectoryForInclude(unsigned type)
XkbDirectoryForInclude(enum xkb_file_type type)
{
switch (type)
{
case XkmKeymapFile:
case FILE_TYPE_KEYMAP:
return "keymap";
case XkmKeyNamesIndex:
case FILE_TYPE_KEYCODES:
return "keycodes";
case XkmTypesIndex:
case FILE_TYPE_TYPES:
return "types";
case XkmSymbolsIndex:
case FILE_TYPE_SYMBOLS:
return "symbols";
case XkmCompatMapIndex:
case FILE_TYPE_COMPAT:
return "compat";
case XkmGeometryIndex:
case FILE_TYPE_GEOMETRY:
return "geometry";
case XkmRulesFile:
case FILE_TYPE_RULES:
return "rules";
default:
return "";
@ -171,16 +171,16 @@ XkbDirectoryForInclude(unsigned type)
* Search for the given file name in the include directories.
*
* @param ctx the XKB ctx containing the include paths
* @param type one of XkbTypesIndex, XkbCompatMapIndex, ..., or
* XkmKeymapFile
* @param pathReturn is set to the full path of the file if found.
* @param type one of FILE_TYPE_TYPES, FILE_TYPE_COMPAT, ..., or
* FILE_TYPE_KEYMAP or FILE_TYPE_RULES
* @param pathRtrn is set to the full path of the file if found.
*
* @return an FD to the file or NULL. If NULL is returned, the value of
* pathRtrn is undefined.
*/
FILE *
XkbFindFileInPath(struct xkb_context *ctx,
const char *name, unsigned type, char **pathRtrn)
const char *name, enum xkb_file_type type, char **pathRtrn)
{
size_t i;
int ret;

View File

@ -1059,7 +1059,7 @@ xkb_components_from_rules(struct xkb_context *ctx,
struct rules *rules;
struct xkb_component_names *kccgst = NULL;
file = XkbFindFileInPath(ctx, rmlvo->rules, XkmRulesFile, &path);
file = XkbFindFileInPath(ctx, rmlvo->rules, FILE_TYPE_RULES, &path);
if (!file) {
ERROR("could not find \"%s\" rules in XKB path\n", rmlvo->rules);
ERROR("%d include paths searched:\n",

View File

@ -798,7 +798,7 @@ HandleIncludeSymbols(IncludeStmt *stmt, struct xkb_keymap *keymap,
included = *info;
memset(info, 0, sizeof(SymbolsInfo));
}
else if (ProcessIncludeFile(keymap->ctx, stmt, XkmSymbolsIndex, &rtrn,
else if (ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_SYMBOLS, &rtrn,
&newMerge))
{
InitSymbolsInfo(&included, keymap);
@ -840,7 +840,7 @@ HandleIncludeSymbols(IncludeStmt *stmt, struct xkb_keymap *keymap,
MergeIncludedSymbols(&included, info, next->merge, keymap);
FreeSymbolsInfo(info);
}
else if (ProcessIncludeFile(keymap->ctx, next, XkmSymbolsIndex,
else if (ProcessIncludeFile(keymap->ctx, next, FILE_TYPE_SYMBOLS,
&rtrn, &op))
{
InitSymbolsInfo(&next_incl, keymap);

View File

@ -65,8 +65,8 @@ ReportBadField(const char *type, const char *field, const char *name);
extern bool
ProcessIncludeFile(struct xkb_context *ctx,
IncludeStmt *stmt, unsigned file_type, XkbFile **file_rtrn,
unsigned *merge_rtrn);
IncludeStmt *stmt, enum xkb_file_type file_type,
XkbFile **file_rtrn, unsigned *merge_rtrn);
extern bool
FindNamedKey(struct xkb_keymap *keymap, unsigned long name,

View File

@ -41,25 +41,25 @@ XkbKeymapFileFromComponents(struct xkb_context *ctx,
IncludeStmt *inc;
inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
keycodes = CreateXKBFile(ctx, XkmKeyNamesIndex, NULL,
keycodes = CreateXKBFile(ctx, FILE_TYPE_KEYCODES, NULL,
(ParseCommon *)inc, 0);
inc = IncludeCreate(ktcsg->types, MergeDefault);
types = CreateXKBFile(ctx, XkmTypesIndex, NULL,
types = CreateXKBFile(ctx, FILE_TYPE_TYPES, NULL,
(ParseCommon *)inc, 0);
AppendStmt(&keycodes->common, &types->common);
inc = IncludeCreate(ktcsg->compat, MergeDefault);
compat = CreateXKBFile(ctx, XkmCompatMapIndex, NULL,
compat = CreateXKBFile(ctx, FILE_TYPE_COMPAT, NULL,
(ParseCommon *)inc, 0);
AppendStmt(&keycodes->common, &compat->common);
inc = IncludeCreate(ktcsg->symbols, MergeDefault);
symbols = CreateXKBFile(ctx, XkmSymbolsIndex, NULL,
symbols = CreateXKBFile(ctx, FILE_TYPE_SYMBOLS, NULL,
(ParseCommon *)inc, 0);
AppendStmt(&keycodes->common, &symbols->common);
return CreateXKBFile(ctx, XkmKeymapFile,
return CreateXKBFile(ctx, FILE_TYPE_KEYMAP,
ktcsg->keymap ? ktcsg->keymap : strdup(""),
&keycodes->common, 0);
}
@ -141,7 +141,7 @@ compile_keymap(struct xkb_context *ctx, XkbFile *file)
if (!mapToUse)
goto err;
if (mapToUse->type != XkmKeymapFile) {
if (mapToUse->type != FILE_TYPE_KEYMAP) {
ERROR("file type %d not handled\n", mapToUse->type);
goto err;
}

View File

@ -237,7 +237,7 @@ typedef struct _IndicatorMapDef
typedef struct _XkbFile
{
ParseCommon common;
unsigned type;
enum xkb_file_type type;
char *topName;
char *name;
ParseCommon *defs;