/* * GPL HEADER START * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 only, * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is included * in the LICENSE file that accompanied this code). * * You should have received a copy of the GNU General Public License * version 2 along with this program; If not, see * http://www.gnu.org/licenses/gpl-2.0.html * * GPL HEADER END */ /* * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. * Use is subject to license terms. * * Copyright (c) 2012, 2017, Intel Corporation. */ /* * This file is part of Lustre, http://www.lustre.org/ * * String manipulation functions. * * libcfs/libcfs/libcfs_string.c * * Author: Nathan Rutman */ #include #include #include /* Convert a text string to a bitmask */ int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), int *oldmask, int minmask, int allmask, int defmask) { const char *debugstr; char op = 0; int newmask = minmask, i, len, found = 0; ENTRY; /* must be a list of tokens separated by whitespace or comma, * and optionally an operator ('+' or '-'). If an operator * appears first in , '*oldmask' is used as the starting point * (relative), otherwise minmask is used (absolute). An operator * applies to all following tokens up to the next operator. */ while (*str != 0) { while (isspace(*str) || *str == ',') str++; if (*str == 0) break; if (*str == '+' || *str == '-') { op = *str++; if (!found) /* only if first token is relative */ newmask = *oldmask; while (isspace(*str)) str++; if (*str == 0) /* trailing op */ return -EINVAL; } /* find token length */ for (len = 0; str[len] != 0 && !isspace(str[len]) && str[len] != '+' && str[len] != '-' && str[len] != ','; len++); /* match token */ found = 0; for (i = 0; i < 32; i++) { debugstr = bit2str(i); if (debugstr != NULL && strlen(debugstr) == len && strncasecmp(str, debugstr, len) == 0) { if (op == '-') newmask &= ~BIT(i); else newmask |= BIT(i); found = 1; break; } } if (!found && len == 3 && (strncasecmp(str, "ALL", len) == 0)) { if (op == '-') newmask = minmask; else newmask = allmask; found = 1; } if (!found && strcasecmp(str, "DEFAULT") == 0) { if (op == '-') newmask = (newmask & ~defmask) | minmask; else if (op == '+') newmask |= defmask; else newmask = defmask; found = 1; } if (!found) { CWARN("unknown mask '%.*s'.\n" "mask usage: [+|-] ...\n", len, str); return -EINVAL; } str += len; } *oldmask = newmask; return 0; } EXPORT_SYMBOL(cfs_str2mask);