4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * String manipulation functions.
33 * libcfs/libcfs/libcfs_string.c
35 * Author: Nathan Rutman <nathan.rutman@sun.com>
38 #include <linux/ctype.h>
39 #include <libcfs/libcfs.h>
40 #include <libcfs/libcfs_string.h>
43 /* convert a binary mask to a string of bit names */
44 int cfs_mask2str(char *str, int size, u64 mask, const char *(*bit2str)(int bit),
51 if (mask == 0) { /* "0" */
55 } else { /* space-separated tokens */
56 for (i = 0; i < 64; i++) {
57 if ((mask & BIT(i)) == 0)
61 if (!token) /* unused bit */
64 if (len > 0) { /* separator? */
89 EXPORT_SYMBOL(cfs_mask2str);
91 /* Convert a text string to a bitmask */
92 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
93 u64 *oldmask, u64 minmask, u64 allmask, u64 defmask)
96 u64 newmask = minmask, found = 0;
99 /* <str> must be a list of tokens separated by whitespace or comma,
100 * and optionally an operator ('+' or '-'). If an operator
101 * appears first in <str>, '*oldmask' is used as the starting point
102 * (relative), otherwise minmask is used (absolute). An operator
103 * applies to all following tokens up to the next operator.
109 while (isspace(*str) || *str == ',')
113 if (*str == '+' || *str == '-') {
116 /* only if first token is relative */
118 while (isspace(*str))
120 if (*str == 0) /* trailing op */
124 /* find token length */
125 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
126 str[len] != '+' && str[len] != '-' && str[len] != ',';
131 for (i = 0; i < 32; i++) {
132 debugstr = bit2str(i);
133 if (debugstr != NULL &&
134 strlen(debugstr) == len &&
135 strncasecmp(str, debugstr, len) == 0) {
144 if (!found && len == 3 &&
145 (strncasecmp(str, "ALL", len) == 0)) {
152 if (!found && strcasecmp(str, "DEFAULT") == 0) {
154 newmask = (newmask & ~defmask) | minmask;
162 CWARN("unknown mask '%.*s'.\n"
163 "mask usage: [+|-]<all|type> ...\n", len, str);
172 EXPORT_SYMBOL(cfs_str2mask);