utils: add/replace string equality macros

It's more tidy and less error prone, since we use strcasecmp == 0 a lot.
We replace strcmp == 0 by streq, strcasecmp == 0 by istreq,
uStrCasePrefix by istreq_prefix and uDupString by strdup_safe.

Signed-off-by: Ran Benita <ran234@gmail.com>
master
Ran Benita 2012-07-24 19:54:14 +03:00
parent 4f843c817b
commit 89723b7cb7
22 changed files with 190 additions and 189 deletions

View File

@ -136,8 +136,7 @@ atom_text(struct atom_table *table, xkb_atom_t atom)
char *
atom_strdup(struct atom_table *table, xkb_atom_t atom)
{
const char *ret = atom_text(table, atom);
return ret ? strdup(ret) : NULL;
return strdup_safe(atom_text(table, atom));
}
/*

View File

@ -104,7 +104,7 @@ xkb_keysym_from_name(const char *s)
entry = &_XkeyTable[idx];
if ((entry[0] == sig1) && (entry[1] == sig2) &&
!strcmp(s, (const char *) entry + 6)) {
streq(s, (const char *) entry + 6)) {
val = (entry[2] << 24) | (entry[3] << 16) |
(entry[4] << 8) | entry[5];
if (!val)

View File

@ -111,25 +111,25 @@ xkb_map_mod_get_index(struct xkb_keymap *keymap, const char *name)
{
xkb_mod_index_t i;
if (strcasecmp(name, "Shift") == 0)
if (istreq(name, "Shift"))
return ShiftMapIndex;
if (strcasecmp(name, "Control") == 0)
if (istreq(name, "Control"))
return ControlMapIndex;
if (strcasecmp(name, "Caps Lock") == 0)
if (istreq(name, "Caps Lock"))
return LockMapIndex;
if (strcasecmp(name, "Mod1") == 0)
if (istreq(name, "Mod1"))
return Mod1MapIndex;
if (strcasecmp(name, "Mod2") == 0)
if (istreq(name, "Mod2"))
return Mod2MapIndex;
if (strcasecmp(name, "Mod3") == 0)
if (istreq(name, "Mod3"))
return Mod3MapIndex;
if (strcasecmp(name, "Mod4") == 0)
if (istreq(name, "Mod4"))
return Mod4MapIndex;
if (strcasecmp(name, "Mod5") == 0)
if (istreq(name, "Mod5"))
return Mod5MapIndex;
for (i = 0; i < XkbNumVirtualMods && keymap->vmod_names[i]; i++) {
if (strcasecmp(name, keymap->vmod_names[i]) == 0)
if (istreq(name, keymap->vmod_names[i]))
return i + Mod5MapIndex;
}
@ -173,10 +173,9 @@ xkb_map_group_get_index(struct xkb_keymap *keymap, const char *name)
xkb_group_index_t num_groups = xkb_map_num_groups(keymap);
xkb_group_index_t i;
for (i = 0; i < num_groups; i++) {
if (strcasecmp(keymap->group_names[i], name) == 0)
for (i = 0; i < num_groups; i++)
if (istreq(keymap->group_names[i], name))
return i;
}
return XKB_GROUP_INVALID;
}
@ -231,10 +230,9 @@ xkb_map_led_get_index(struct xkb_keymap *keymap, const char *name)
xkb_led_index_t num_leds = xkb_map_num_leds(keymap);
xkb_led_index_t i;
for (i = 0; i < num_leds; i++) {
if (strcasecmp(keymap->indicator_names[i], name) == 0)
for (i = 0; i < num_leds; i++)
if (istreq(keymap->indicator_names[i], name))
return i;
}
return XKB_LED_INVALID;
}

View File

@ -27,7 +27,8 @@
* software without specific, written prior permission.
\*/
/***====================================================================***/
#include <stdbool.h>
#include <string.h>
/*
* We sometimes malloc strings and then expose them as const char*'s. This
@ -36,8 +37,29 @@
*/
#define UNCONSTIFY(const_ptr) ((void *) (uintptr_t) (const_ptr))
#define uDupString(s) ((s) ? strdup(s) : NULL)
#define uStrCasePrefix(s1, s2) (strncasecmp((s1), (s2), strlen(s1)) == 0)
static inline bool
streq(const char *s1, const char *s2)
{
return strcmp(s1, s2) == 0;
}
static inline bool
istreq(const char *s1, const char *s2)
{
return strcasecmp(s1, s2) == 0;
}
static inline bool
istreq_prefix(const char *s1, const char *s2)
{
return strncasecmp(s1, s2, strlen(s1)) == 0;
}
static inline char *
strdup_safe(const char *s)
{
return s ? strdup(s) : NULL;
}
/* Compiler Attributes */

View File

@ -124,7 +124,7 @@ stringToValue(const LookupEntry tab[], const char *string,
return false;
for (entry = tab; entry->name != NULL; entry++) {
if (strcasecmp(entry->name, string) == 0) {
if (istreq(entry->name, string)) {
*value_rtrn = entry->result;
return true;
}
@ -247,8 +247,8 @@ CheckModifierField(struct xkb_keymap *keymap, unsigned action, ExprDef *value,
if (value->op == ExprIdent) {
const char *valStr;
valStr = xkb_atom_text(keymap->ctx, value->value.str);
if (valStr && ((strcasecmp(valStr, "usemodmapmods") == 0) ||
(strcasecmp(valStr, "modmapmods") == 0))) {
if (valStr && (istreq(valStr, "usemodmapmods") ||
istreq(valStr, "modmapmods"))) {
*mods_rtrn = 0;
*flags_inout |= XkbSA_UseModMapMods;
@ -1168,7 +1168,7 @@ SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field,
return false;
}
if (strcasecmp(elem, "action") == 0)
if (istreq(elem, "action"))
new->action = XkbSA_NoAction;
else {
if (!stringToAction(elem, &new->action)) {

View File

@ -380,15 +380,15 @@ ResolveStateAndPredicate(ExprDef * expr,
if (expr->op == ExprActionDecl) {
const char *pred_txt = xkb_atom_text(info->keymap->ctx,
expr->value.action.name);
if (strcasecmp(pred_txt, "noneof") == 0)
if (istreq(pred_txt, "noneof"))
*pred_rtrn = XkbSI_NoneOf;
else if (strcasecmp(pred_txt, "anyofornone") == 0)
else if (istreq(pred_txt, "anyofornone"))
*pred_rtrn = XkbSI_AnyOfOrNone;
else if (strcasecmp(pred_txt, "anyof") == 0)
else if (istreq(pred_txt, "anyof"))
*pred_rtrn = XkbSI_AnyOf;
else if (strcasecmp(pred_txt, "allof") == 0)
else if (istreq(pred_txt, "allof"))
*pred_rtrn = XkbSI_AllOf;
else if (strcasecmp(pred_txt, "exactly") == 0)
else if (istreq(pred_txt, "exactly"))
*pred_rtrn = XkbSI_Exactly;
else {
log_err(info->keymap->ctx,
@ -400,7 +400,7 @@ ResolveStateAndPredicate(ExprDef * expr,
else if (expr->op == ExprIdent) {
const char *pred_txt = xkb_atom_text(info->keymap->ctx,
expr->value.str);
if ((pred_txt) && (strcasecmp(pred_txt, "any") == 0)) {
if (pred_txt && istreq(pred_txt, "any")) {
*pred_rtrn = XkbSI_AnyOf;
*mods_rtrn = 0xff;
return true;
@ -644,7 +644,7 @@ SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
ExprResult tmp;
struct xkb_keymap *keymap = info->keymap;
if (strcasecmp(field, "action") == 0) {
if (istreq(field, "action")) {
if (arrayNdx != NULL)
return ReportSINotArray(info, si, field);
ok = HandleActionDef(value, keymap, &si->interp.act.any,
@ -652,8 +652,8 @@ SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
if (ok)
si->defined |= _SI_Action;
}
else if ((strcasecmp(field, "virtualmodifier") == 0) ||
(strcasecmp(field, "virtualmod") == 0)) {
else if (istreq(field, "virtualmodifier") ||
istreq(field, "virtualmod")) {
if (arrayNdx != NULL)
return ReportSINotArray(info, si, field);
ok = ResolveVirtualModifier(value, keymap, &tmp, &info->vmods);
@ -664,7 +664,7 @@ SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
else
return ReportSIBadType(info, si, field, "virtual modifier");
}
else if (strcasecmp(field, "repeat") == 0) {
else if (istreq(field, "repeat")) {
if (arrayNdx != NULL)
return ReportSINotArray(info, si, field);
ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
@ -678,7 +678,7 @@ SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
else
return ReportSIBadType(info, si, field, "boolean");
}
else if (strcasecmp(field, "locking") == 0) {
else if (istreq(field, "locking")) {
if (arrayNdx != NULL)
return ReportSINotArray(info, si, field);
ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
@ -692,8 +692,8 @@ SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
else
return ReportSIBadType(info, si, field, "boolean");
}
else if ((strcasecmp(field, "usemodmap") == 0) ||
(strcasecmp(field, "usemodmapmods") == 0)) {
else if (istreq(field, "usemodmap") ||
istreq(field, "usemodmapmods")) {
if (arrayNdx != NULL)
return ReportSINotArray(info, si, field);
ok = ExprResolveEnum(keymap->ctx, value, &tmp, useModMapValues);
@ -756,7 +756,7 @@ SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
bool ok = true;
struct xkb_keymap *keymap = info->keymap;
if (strcasecmp(field, "modifiers") == 0 || strcasecmp(field, "mods") == 0) {
if (istreq(field, "modifiers") || istreq(field, "mods")) {
if (arrayNdx != NULL)
return ReportIndicatorNotArray(info, led, field);
@ -767,7 +767,7 @@ SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
led->vmods = (rtrn.uval >> 8) & 0xff;
led->defined |= _LED_Mods;
}
else if (strcasecmp(field, "groups") == 0) {
else if (istreq(field, "groups")) {
if (arrayNdx != NULL)
return ReportIndicatorNotArray(info, led, field);
@ -777,8 +777,7 @@ SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
led->groups = rtrn.uval;
led->defined |= _LED_Groups;
}
else if (strcasecmp(field, "controls") == 0 ||
strcasecmp(field, "ctrls") == 0) {
else if (istreq(field, "controls") || istreq(field, "ctrls")) {
if (arrayNdx != NULL)
return ReportIndicatorNotArray(info, led, field);
@ -789,7 +788,7 @@ SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
led->ctrls = rtrn.uval;
led->defined |= _LED_Ctrls;
}
else if (strcasecmp(field, "allowexplicit") == 0) {
else if (istreq(field, "allowexplicit")) {
if (arrayNdx != NULL)
return ReportIndicatorNotArray(info, led, field);
@ -802,8 +801,8 @@ SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
led->flags |= XkbIM_NoExplicit;
led->defined |= _LED_Explicit;
}
else if (strcasecmp(field, "whichmodstate") == 0 ||
strcasecmp(field, "whichmodifierstate") == 0) {
else if (istreq(field, "whichmodstate") ||
istreq(field, "whichmodifierstate")) {
if (arrayNdx != NULL)
return ReportIndicatorNotArray(info, led, field);
@ -813,7 +812,7 @@ SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
led->which_mods = rtrn.uval;
}
else if (strcasecmp(field, "whichgroupstate") == 0) {
else if (istreq(field, "whichgroupstate")) {
if (arrayNdx != NULL)
return ReportIndicatorNotArray(info, led, field);
@ -823,12 +822,12 @@ SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
led->which_groups = rtrn.uval;
}
else if (strcasecmp(field, "driveskbd") == 0 ||
strcasecmp(field, "driveskeyboard") == 0 ||
strcasecmp(field, "leddriveskbd") == 0 ||
strcasecmp(field, "leddriveskeyboard") == 0 ||
strcasecmp(field, "indicatordriveskbd") == 0 ||
strcasecmp(field, "indicatordriveskeyboard") == 0) {
else if (istreq(field, "driveskbd") ||
istreq(field, "driveskeyboard") ||
istreq(field, "leddriveskbd") ||
istreq(field, "leddriveskeyboard") ||
istreq(field, "indicatordriveskbd") ||
istreq(field, "indicatordriveskeyboard")) {
if (arrayNdx != NULL)
return ReportIndicatorNotArray(info, led, field);
@ -841,7 +840,7 @@ SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
led->flags &= ~XkbIM_LEDDrivesKB;
led->defined |= _LED_DrivesKbd;
}
else if (strcasecmp(field, "index") == 0) {
else if (istreq(field, "index")) {
if (arrayNdx != NULL)
return ReportIndicatorNotArray(info, led, field);
@ -881,9 +880,9 @@ HandleInterpVar(CompatInfo *info, VarDef *stmt)
if (ExprResolveLhs(info->keymap, stmt->name, &elem, &field, &ndx) == 0)
ret = 0; /* internal error, already reported */
else if (elem.str && (strcasecmp(elem.str, "interpret") == 0))
else if (elem.str && istreq(elem.str, "interpret"))
ret = SetInterpField(info, &info->dflt, field.str, ndx, stmt->value);
else if (elem.str && (strcasecmp(elem.str, "indicator") == 0))
else if (elem.str && istreq(elem.str, "indicator"))
ret = SetIndicatorMapField(info, &info->ledDflt, field.str, ndx,
stmt->value);
else
@ -1033,7 +1032,7 @@ HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
if (merge == MERGE_DEFAULT)
merge = MERGE_AUGMENT;
free(info->name);
info->name = uDupString(file->name);
info->name = strdup_safe(file->name);
stmt = file->defs;
while (stmt)
{
@ -1111,8 +1110,8 @@ BindIndicators(CompatInfo *info, struct list *unbound_leds)
if (led->indicator == _LED_NotBound) {
for (i = 0; i < XkbNumIndicators; i++) {
if (keymap->indicator_names[i] &&
strcmp(keymap->indicator_names[i],
xkb_atom_text(keymap->ctx, led->name)) == 0) {
streq(keymap->indicator_names[i],
xkb_atom_text(keymap->ctx, led->name))) {
led->indicator = i + 1;
break;
}
@ -1147,8 +1146,8 @@ BindIndicators(CompatInfo *info, struct list *unbound_leds)
continue;
}
if (strcmp(keymap->indicator_names[led->indicator - 1],
xkb_atom_text(keymap->ctx, led->name)) != 0) {
if (!streq(keymap->indicator_names[led->indicator - 1],
xkb_atom_text(keymap->ctx, led->name))) {
const char *old = keymap->indicator_names[led->indicator - 1];
log_err(info->keymap->ctx,
"Multiple names bound to indicator %d; "

View File

@ -162,7 +162,7 @@ SimpleLookup(struct xkb_context *ctx, const void *priv,
str = xkb_atom_text(ctx, field);
for (entry = priv; (entry != NULL) && (entry->name != NULL); entry++) {
if (strcasecmp(str, entry->name) == 0) {
if (istreq(str, entry->name)) {
val_rtrn->uval = entry->result;
return true;
}
@ -203,9 +203,9 @@ LookupModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
str = xkb_atom_text(ctx, field);
if (str == NULL)
return false;
if (strcasecmp(str, "all") == 0)
if (istreq(str, "all"))
val_rtrn->uval = 0xff;
else if (strcasecmp(str, "none") == 0)
else if (istreq(str, "none"))
val_rtrn->uval = 0;
else if (LookupModIndex(ctx, priv, field, type, val_rtrn))
val_rtrn->uval = (1 << val_rtrn->uval);
@ -219,7 +219,7 @@ ExprResolveBoolean(struct xkb_context *ctx, ExprDef *expr,
ExprResult *val_rtrn)
{
int ok = 0;
const char *bogus = NULL;
const char *ident;
switch (expr->op) {
case ExprValue:
@ -233,22 +233,22 @@ ExprResolveBoolean(struct xkb_context *ctx, ExprDef *expr,
return true;
case ExprIdent:
bogus = xkb_atom_text(ctx, expr->value.str);
if (bogus) {
if ((strcasecmp(bogus, "true") == 0) ||
(strcasecmp(bogus, "yes") == 0) ||
(strcasecmp(bogus, "on") == 0)) {
ident = xkb_atom_text(ctx, expr->value.str);
if (ident) {
if (istreq(ident, "true") ||
istreq(ident, "yes") ||
istreq(ident, "on")) {
val_rtrn->uval = 1;
return true;
}
else if ((strcasecmp(bogus, "false") == 0) ||
(strcasecmp(bogus, "no") == 0) ||
(strcasecmp(bogus, "off") == 0)) {
else if (istreq(ident, "false") ||
istreq(ident, "no") ||
istreq(ident, "off")) {
val_rtrn->uval = 0;
return true;
}
}
log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
log_err(ctx, "Identifier \"%s\" of type boolean is unknown\n",
xkb_atom_text(ctx, expr->value.str));
return false;

View File

@ -463,7 +463,7 @@ HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
KeyNamesInfo included, next_incl;
/* XXX: What's that? */
if (stmt->file && strcmp(stmt->file, "computed") == 0) {
if (stmt->file && streq(stmt->file, "computed")) {
info->keymap->flags |= AutoKeyNames;
info->explicitMin = 0;
info->explicitMax = XKB_KEYCODE_MAX;
@ -623,9 +623,9 @@ HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt)
goto err_out;
}
if (strcasecmp(field.str, "minimum") == 0)
if (istreq(field.str, "minimum"))
which = MIN_KEYCODE_DEF;
else if (strcasecmp(field.str, "maximum") == 0)
else if (istreq(field.str, "maximum"))
which = MAX_KEYCODE_DEF;
else {
log_err(info->keymap->ctx,
@ -745,7 +745,7 @@ HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
ParseCommon *stmt;
free(info->name);
info->name = uDupString(file->name);
info->name = strdup_safe(file->name);
stmt = file->defs;
while (stmt)
{

View File

@ -701,7 +701,7 @@ SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
{
ExprResult tmp;
if (strcasecmp(field, "modifiers") == 0) {
if (istreq(field, "modifiers")) {
unsigned mods, vmods;
if (arrayNdx != NULL)
log_warn(info->keymap->ctx,
@ -730,16 +730,15 @@ SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
type->defined |= _KT_Mask;
return true;
}
else if (strcasecmp(field, "map") == 0) {
else if (istreq(field, "map")) {
type->defined |= _KT_Map;
return SetMapEntry(info, type, arrayNdx, value);
}
else if (strcasecmp(field, "preserve") == 0) {
else if (istreq(field, "preserve")) {
type->defined |= _KT_Preserve;
return SetPreserve(info, type, arrayNdx, value);
}
else if ((strcasecmp(field, "levelname") == 0) ||
(strcasecmp(field, "level_name") == 0)) {
else if (istreq(field, "levelname") || istreq(field, "level_name")) {
type->defined |= _KT_LevelNames;
return SetLevelName(info, type, arrayNdx, value);
}
@ -759,7 +758,7 @@ HandleKeyTypeVar(KeyTypesInfo *info, VarDef *stmt)
if (!ExprResolveLhs(info->keymap, stmt->name, &elem, &field, &arrayNdx))
return false; /* internal error, already reported */
if (elem.str && (strcasecmp(elem.str, "type") == 0))
if (elem.str && istreq(elem.str, "type"))
return SetKeyTypeField(info, &info->dflt, field.str, arrayNdx,
stmt->value);
if (elem.str != NULL) {
@ -871,7 +870,7 @@ HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
ParseCommon *stmt;
free(info->name);
info->name = uDupString(file->name);
info->name = strdup_safe(file->name);
stmt = file->defs;
while (stmt)
{

View File

@ -70,7 +70,7 @@ ProcessIncludeFile(struct xkb_context *ctx,
{
next = (XkbFile *) mapToUse->common.next;
mapToUse->common.next = NULL;
if (strcmp(mapToUse->name, stmt->map) == 0 &&
if (streq(mapToUse->name, stmt->map) &&
mapToUse->type == file_type) {
FreeXKBFile(next);
break;

View File

@ -356,26 +356,27 @@ AppendMultiKeysymList(ExprDef * list, ExprDef * append)
return list;
}
int
bool
LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn)
{
xkb_keysym_t sym;
if ((!str) || (strcasecmp(str, "any") == 0) ||
(strcasecmp(str, "nosymbol") == 0)) {
if (!str || istreq(str, "any") || istreq(str, "nosymbol")) {
*sym_rtrn = XKB_KEY_NoSymbol;
return 1;
}
else if ((strcasecmp(str, "none") == 0) ||
(strcasecmp(str, "voidsymbol") == 0)) {
if (istreq(str, "none") || istreq(str, "voidsymbol")) {
*sym_rtrn = XKB_KEY_VoidSymbol;
return 1;
}
sym = xkb_keysym_from_name(str);
if (sym != XKB_KEY_NoSymbol) {
*sym_rtrn = sym;
return 1;
}
return 0;
}
@ -392,7 +393,7 @@ IncludeCreate(struct xkb_context *ctx, char *str, enum merge_mode merge)
incl = first = NULL;
file = map = NULL;
tmp = str;
stmt = uDupString(str);
stmt = strdup_safe(str);
while (tmp && *tmp)
{
if (!XkbParseIncludeMap(&tmp, &file, &map, &nextop, &extra_data))
@ -502,7 +503,7 @@ CreateXKBFile(struct xkb_context *ctx, enum xkb_file_type type, char *name,
EnsureSafeMapName(name);
file->type = type;
file->topName = name ? strdup(name) : NULL;
file->topName = strdup_safe(name);
file->name = name;
file->defs = defs;
file->id = xkb_context_take_file_id(ctx);

View File

@ -109,7 +109,7 @@ AppendMultiKeysymList(ExprDef *list, ExprDef *append);
extern ExprDef *
AppendKeysymList(ExprDef *list, char *sym);
extern int
bool
LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn);
extern IncludeStmt *

View File

@ -79,7 +79,7 @@ XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
tmp = strchr(str, ':');
if (tmp != NULL) {
*tmp++ = '\0';
*extra_data = uDupString(tmp);
*extra_data = strdup(tmp);
}
else {
*extra_data = NULL;
@ -87,7 +87,7 @@ XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
tmp = strchr(str, '(');
if (tmp == NULL) {
*file_rtrn = uDupString(str);
*file_rtrn = strdup(str);
*map_rtrn = NULL;
}
else if (str[0] == '(') {
@ -96,7 +96,7 @@ XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
}
else {
*tmp++ = '\0';
*file_rtrn = uDupString(str);
*file_rtrn = strdup(str);
str = tmp;
tmp = strchr(str, ')');
if ((tmp == NULL) || (tmp[1] != '\0')) {
@ -105,7 +105,7 @@ XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
return false;
}
*tmp++ = '\0';
*map_rtrn = uDupString(str);
*map_rtrn = strdup(str);
}
if (*nextop_rtrn == '\0')

View File

@ -284,7 +284,7 @@ match_mapping_line(struct xkb_context *ctx, darray_char *line,
found = false;
str = NULL;
if (strcmp(tok, "=") == 0)
if (streq(tok, "="))
continue;
for (i = 0; i < MAX_WORDS; i++) {
@ -426,7 +426,7 @@ match_rule_line(struct xkb_context *ctx, darray_char *line,
nread++) {
str = NULL;
if (strcmp(tok, "=") == 0) {
if (streq(tok, "=")) {
nread--;
continue;
}
@ -459,15 +459,15 @@ match_rule_line(struct xkb_context *ctx, darray_char *line,
else
rule->flags |= RULE_FLAG_NORMAL;
rule->model = uDupString(names[MODEL]);
rule->layout = uDupString(names[LAYOUT]);
rule->variant = uDupString(names[VARIANT]);
rule->option = uDupString(names[OPTION]);
rule->model = strdup_safe(names[MODEL]);
rule->layout = strdup_safe(names[LAYOUT]);
rule->variant = strdup_safe(names[VARIANT]);
rule->option = strdup_safe(names[OPTION]);
rule->keycodes = uDupString(names[KEYCODES]);
rule->symbols = uDupString(names[SYMBOLS]);
rule->types = uDupString(names[TYPES]);
rule->compat = uDupString(names[COMPAT]);
rule->keycodes = strdup_safe(names[KEYCODES]);
rule->symbols = strdup_safe(names[SYMBOLS]);
rule->types = strdup_safe(names[TYPES]);
rule->compat = strdup_safe(names[COMPAT]);
rule->layout_num = rule->variant_num = 0;
for (i = 0; i < nread; i++) {
@ -673,7 +673,7 @@ match_group_member(struct rules *rules, const char *group_name,
struct group *iter, *group = NULL;
darray_foreach(iter, rules->groups) {
if (strcmp(iter->name, group_name) == 0) {
if (streq(iter->name, group_name)) {
group = iter;
break;
}
@ -684,7 +684,7 @@ match_group_member(struct rules *rules, const char *group_name,
word = group->words;
for (i = 0; i < group->number; i++, word += strlen(word) + 1)
if (strcmp(word, name) == 0)
if (streq(word, name))
return true;
return false;
@ -717,14 +717,14 @@ apply_rule_if_matches(struct rules *rules, struct rule *rule,
if (mdefs->model == NULL)
return 0;
if (strcmp(rule->model, "*") == 0) {
if (streq(rule->model, "*")) {
pending = true;
}
else if (rule->model[0] == '$') {
if (!match_group_member(rules, rule->model, mdefs->model))
return 0;
}
else if (strcmp(rule->model, mdefs->model) != 0) {
else if (!streq(rule->model, mdefs->model)) {
return 0;
}
}
@ -741,7 +741,7 @@ apply_rule_if_matches(struct rules *rules, struct rule *rule,
if (mdefs->layout[rule->layout_num] == NULL)
return 0;
if (strcmp(rule->layout, "*") == 0) {
if (streq(rule->layout, "*")) {
pending = true;
}
else if (rule->layout[0] == '$') {
@ -749,8 +749,7 @@ apply_rule_if_matches(struct rules *rules, struct rule *rule,
mdefs->layout[rule->layout_num]))
return 0;
}
else if (strcmp(rule->layout,
mdefs->layout[rule->layout_num]) != 0) {
else if (!streq(rule->layout, mdefs->layout[rule->layout_num])) {
return 0;
}
}
@ -759,7 +758,7 @@ apply_rule_if_matches(struct rules *rules, struct rule *rule,
if (mdefs->variant[rule->variant_num] == NULL)
return 0;
if (strcmp(rule->variant, "*") == 0) {
if (streq(rule->variant, "*")) {
pending = true;
}
else if (rule->variant[0] == '$') {
@ -767,8 +766,7 @@ apply_rule_if_matches(struct rules *rules, struct rule *rule,
mdefs->variant[rule->variant_num]))
return 0;
}
else if (strcmp(rule->variant,
mdefs->variant[rule->variant_num]) != 0) {
else if (!streq(rule->variant, mdefs->variant[rule->variant_num])) {
return 0;
}
}

View File

@ -1026,7 +1026,7 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
ExprResult tmp;
struct xkb_context *ctx = info->keymap->ctx;
if (strcasecmp(field, "type") == 0) {
if (istreq(field, "type")) {
ExprResult ndx;
if (!ExprResolveString(ctx, value, &tmp))
log_lvl(info->keymap->ctx, 1,
@ -1048,13 +1048,13 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
keyi->typesDefined |= (1 << (ndx.uval - 1));
}
}
else if (strcasecmp(field, "symbols") == 0)
else if (istreq(field, "symbols"))
return AddSymbolsToKey(info, keyi, arrayNdx, value);
else if (strcasecmp(field, "actions") == 0)
else if (istreq(field, "actions"))
return AddActionsToKey(info, keyi, arrayNdx, value);
else if ((strcasecmp(field, "vmods") == 0) ||
(strcasecmp(field, "virtualmods") == 0) ||
(strcasecmp(field, "virtualmodifiers") == 0)) {
else if (istreq(field, "vmods") ||
istreq(field, "virtualmods") ||
istreq(field, "virtualmodifiers")) {
ok = ExprResolveVModMask(info->keymap, value, &tmp);
if (ok) {
keyi->vmodmap = (tmp.uval >> 8);
@ -1067,33 +1067,33 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
exprOpText(value->op), longText(keyi->name));
}
}
else if ((strcasecmp(field, "locking") == 0) ||
(strcasecmp(field, "lock") == 0) ||
(strcasecmp(field, "locks") == 0)) {
else if (istreq(field, "locking") ||
istreq(field, "lock") ||
istreq(field, "locks")) {
ok = ExprResolveEnum(ctx, value, &tmp, lockingEntries);
if (ok)
keyi->behavior.type = tmp.uval;
keyi->defined |= _Key_Behavior;
}
else if ((strcasecmp(field, "radiogroup") == 0) ||
(strcasecmp(field, "permanentradiogroup") == 0) ||
(strcasecmp(field, "allownone") == 0)) {
else if (istreq(field, "radiogroup") ||
istreq(field, "permanentradiogroup") ||
istreq(field, "allownone")) {
log_err(info->keymap->ctx,
"Radio groups not supported; "
"Ignoring radio group specification for key %s\n",
longText(keyi->name));
return false;
}
else if (uStrCasePrefix("overlay", field) ||
uStrCasePrefix("permanentoverlay", field)) {
else if (istreq_prefix("overlay", field) ||
istreq_prefix("permanentoverlay", field)) {
log_err(info->keymap->ctx,
"Overlays not supported; "
"Ignoring overlay specification for key %s\n",
longText(keyi->name));
}
else if ((strcasecmp(field, "repeating") == 0) ||
(strcasecmp(field, "repeats") == 0) ||
(strcasecmp(field, "repeat") == 0)) {
else if (istreq(field, "repeating") ||
istreq(field, "repeats") ||
istreq(field, "repeat")) {
ok = ExprResolveEnum(ctx, value, &tmp, repeatEntries);
if (!ok) {
log_err(info->keymap->ctx,
@ -1105,10 +1105,9 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
keyi->repeat = tmp.uval;
keyi->defined |= _Key_Repeat;
}
else if ((strcasecmp(field, "groupswrap") == 0) ||
(strcasecmp(field, "wrapgroups") == 0)) {
ok = ExprResolveBoolean(ctx, value, &tmp);
if (!ok) {
else if (istreq(field, "groupswrap") ||
istreq(field, "wrapgroups")) {
if (!ExprResolveBoolean(ctx, value, &tmp)) {
log_err(info->keymap->ctx,
"Illegal groupsWrap setting for %s; "
"Non-boolean value ignored\n",
@ -1121,10 +1120,9 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
keyi->out_of_range_group_action = XkbClampIntoRange;
keyi->defined |= _Key_GroupInfo;
}
else if ((strcasecmp(field, "groupsclamp") == 0) ||
(strcasecmp(field, "clampgroups") == 0)) {
ok = ExprResolveBoolean(ctx, value, &tmp);
if (!ok) {
else if (istreq(field, "groupsclamp") ||
istreq(field, "clampgroups")) {
if (!ExprResolveBoolean(ctx, value, &tmp)) {
log_err(info->keymap->ctx,
"Illegal groupsClamp setting for %s; "
"Non-boolean value ignored\n",
@ -1137,8 +1135,8 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
keyi->out_of_range_group_action = XkbWrapIntoRange;
keyi->defined |= _Key_GroupInfo;
}
else if ((strcasecmp(field, "groupsredirect") == 0) ||
(strcasecmp(field, "redirectgroups") == 0)) {
else if (istreq(field, "groupsredirect") ||
istreq(field, "redirectgroups")) {
if (!ExprResolveGroup(ctx, value, &tmp)) {
log_err(info->keymap->ctx,
"Illegal group index for redirect of key %s; "
@ -1202,38 +1200,33 @@ HandleSymbolsVar(SymbolsInfo *info, VarDef *stmt)
if (ExprResolveLhs(info->keymap, stmt->name, &elem, &field,
&arrayNdx) == 0)
return 0; /* internal error, already reported */
if (elem.str && (strcasecmp(elem.str, "key") == 0)) {
if (elem.str && istreq(elem.str, "key")) {
ret = SetSymbolsField(info, &info->dflt, field.str, arrayNdx,
stmt->value);
}
else if ((elem.str == NULL) && ((strcasecmp(field.str, "name") == 0) ||
(strcasecmp(field.str, "groupname") ==
0))) {
else if (!elem.str && (istreq(field.str, "name") ||
istreq(field.str, "groupname"))) {
ret = SetGroupName(info, arrayNdx, stmt->value);
}
else if ((elem.str == NULL)
&& ((strcasecmp(field.str, "groupswrap") == 0) ||
(strcasecmp(field.str, "wrapgroups") == 0))) {
else if (!elem.str && (istreq(field.str, "groupswrap") ||
istreq(field.str, "wrapgroups"))) {
log_err(info->keymap->ctx,
"Global \"groupswrap\" not supported; Ignored\n");
ret = true;
}
else if ((elem.str == NULL)
&& ((strcasecmp(field.str, "groupsclamp") == 0) ||
(strcasecmp(field.str, "clampgroups") == 0))) {
else if (!elem.str && (istreq(field.str, "groupsclamp") ||
istreq(field.str, "clampgroups"))) {
log_err(info->keymap->ctx,
"Global \"groupsclamp\" not supported; Ignored\n");
ret = true;
}
else if ((elem.str == NULL)
&& ((strcasecmp(field.str, "groupsredirect") == 0) ||
(strcasecmp(field.str, "redirectgroups") == 0))) {
else if (!elem.str && (istreq(field.str, "groupsredirect") ||
istreq(field.str, "redirectgroups"))) {
log_err(info->keymap->ctx,
"Global \"groupsredirect\" not supported; Ignored\n");
ret = true;
}
else if ((elem.str == NULL) &&
(strcasecmp(field.str, "allownone") == 0)) {
else if (!elem.str && istreq(field.str, "allownone")) {
log_err(info->keymap->ctx,
"Radio groups not supported; "
"Ignoring \"allownone\" specification\n");
@ -1393,7 +1386,7 @@ HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
ParseCommon *stmt;
free(info->name);
info->name = uDupString(file->name);
info->name = strdup_safe(file->name);
stmt = file->defs;
while (stmt)
{
@ -1511,7 +1504,7 @@ FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
if (keymap) {
darray_foreach(type, keymap->types) {
if (strcmp(type->name, name) == 0) {
if (streq(type->name, name)) {
*type_rtrn = n;
return true;
}

View File

@ -83,11 +83,10 @@ HandleVModDef(VModDef *stmt, struct xkb_keymap *keymap,
if (!keymap->vmod_names[i])
continue;
if (strcmp(keymap->vmod_names[i],
xkb_atom_text(keymap->ctx, stmt->name)) != 0)
if (!streq(keymap->vmod_names[i],
xkb_atom_text(keymap->ctx, stmt->name)))
continue;
info->available |= bit;
if (!stmt->value)
@ -171,12 +170,12 @@ LookupVModIndex(const struct xkb_keymap *keymap, xkb_atom_t field,
* The order of modifiers is the same as in the virtual_modifiers line in
* the xkb_types section.
*/
for (i = 0; i < XkbNumVirtualMods; i++)
if (keymap->vmod_names[i] &&
strcmp(keymap->vmod_names[i], name) == 0) {
for (i = 0; i < XkbNumVirtualMods; i++) {
if (keymap->vmod_names[i] && streq(keymap->vmod_names[i], name)) {
val_rtrn->uval = i;
return true;
}
}
return false;
}
@ -230,7 +229,7 @@ ResolveVirtualModifier(ExprDef *def, struct xkb_keymap *keymap,
for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
if ((info->available & bit) && keymap->vmod_names[i] &&
strcmp(keymap->vmod_names[i], name) == 0) {
streq(keymap->vmod_names[i], name)) {
val_rtrn->uval = i;
return true;
}

View File

@ -56,7 +56,7 @@ main(int argc, char *argv[])
expected = test_read_file("keymaps/dump.data");
assert(expected);
if (strcmp(as_string, expected) != 0) {
if (!streq(as_string, expected)) {
fprintf(stderr, "dumped map differs from expected!\n\n");
fprintf(stderr, "length: got %zu, expected %zu\n",
strlen(as_string), strlen(expected));

View File

@ -106,14 +106,14 @@ main(void)
printf("%s", log_string.item);
assert(strcmp(log_string.item,
"warning: first warning: 87\n"
"error: first error: 115415\n"
"warning: first verbose 5\n"
"warning: second warning: 87\n"
"debug: second debug: hello world\n"
"info: second info\n"
"error: second error: 115415\n") == 0);
assert(streq(log_string.item,
"warning: first warning: 87\n"
"error: first error: 115415\n"
"warning: first verbose 5\n"
"warning: second warning: 87\n"
"debug: second debug: hello world\n"
"info: second info\n"
"error: second error: 115415\n"));
xkb_context_unref(ctx);
darray_free(log_string);

View File

@ -50,14 +50,6 @@ struct test_data {
bool should_fail;
};
static inline bool
streq(const char *s1, const char *s2)
{
if (s1 == NULL || s2 == NULL)
return s1 == s2;
return strcmp(s1, s2) == 0;
}
static bool
test_rules(struct xkb_context *ctx, struct test_data *data)
{

View File

@ -105,7 +105,7 @@ int main(int argc, char *argv[])
assert(!test_rmlvo(ctx, "base", "pc105", "", "", ""));
assert(!test_rmlvo(ctx, "badrules", "", "us", "", ""));
if (argc > 1 && strcmp(argv[1], "bench") == 0)
if (argc > 1 && streq(argv[1], "bench"))
benchmark(ctx);
xkb_context_unref(ctx);

View File

@ -24,6 +24,7 @@
*/
#include "xkbcommon/xkbcommon.h"
#include "utils.h"
const char *
test_get_path(const char *path_rel);

View File

@ -3,7 +3,7 @@
#include <stdio.h>
#include <string.h>
#include "xkbcommon/xkbcommon.h"
#include "test.h"
static int
test_string(const char *string, xkb_keysym_t expected)
@ -28,7 +28,7 @@ test_keysym(xkb_keysym_t keysym, const char *expected)
fprintf(stderr, "Expected keysym %#x -> %s\n", keysym, expected);
fprintf(stderr, "Received keysym %#x -> %s\n\n", keysym, s);
return strcmp(s, expected) == 0;
return streq(s, expected);
}
static int
@ -44,7 +44,7 @@ test_utf8(xkb_keysym_t keysym, const char *expected)
fprintf(stderr, "Expected keysym %#x -> %s\n", keysym, expected);
fprintf(stderr, "Received keysym %#x -> %s\n\n", keysym, s);
return strcmp(s, expected) == 0;
return streq(s, expected);
}
int