Whamcloud - gitweb
LU-19098 hsm: don't print progname twice with lhsmtool
[fs/lustre-release.git] / libcfs / include / libcfs / util / string.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 /*
4  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Copyright (c) 2012, 2017, Intel Corporation.
8  */
9
10 /*
11  * This file is part of Lustre, http://www.lustre.org/
12  *
13  * Generic string manipulation functions.
14  *
15  * Author: Nathan Rutman <nathan.rutman@sun.com>
16  */
17
18 #ifndef __LIBCFS_UTIL_STRING_H__
19 #define __LIBCFS_UTIL_STRING_H__
20
21 #include <stddef.h>
22 #include <stdarg.h>
23
24 #include <linux/types.h>
25 #include <linux/lnet/lnet-types.h>
26 #include <libcfs/util/list.h>
27
28 static inline
29 int vscnprintf(char *buf, size_t bufsz, const char *format, va_list args)
30 {
31         int ret;
32
33         if (!bufsz)
34                 return 0;
35
36         ret = vsnprintf(buf, bufsz, format, args);
37         return (bufsz > ret) ? ret : bufsz - 1;
38 }
39
40 /* __printf from linux kernel */
41 #ifndef __printf
42 #define __printf(a, b)          __attribute__((__format__(printf, a, b)))
43 #endif
44
45 __printf(3, 4)
46 static inline int scnprintf(char *buf, size_t bufsz, const char *format, ...)
47 {
48         int ret;
49         va_list args;
50
51         va_start(args, format);
52         ret = vscnprintf(buf, bufsz, format, args);
53         va_end(args);
54
55         return ret;
56 }
57
58 struct netstrfns {
59         __u32   nf_type;
60         char    *nf_name;
61         char    *nf_modname;
62         void    (*nf_addr2str)(__u32 addr, char *str, size_t size);
63         void    (*nf_addr2str_size)(const __be32 *addr, size_t asize, char *str,
64                                     size_t size);
65         int     (*nf_str2addr)(const char *str, int nob, __u32 *addr);
66         int     (*nf_str2addr_size)(const char *str, int nob,
67                                     __be32 *addr, size_t *asize);
68         int     (*nf_parse_addrlist)(char *str, int len,
69                                      struct list_head *list);
70         int     (*nf_print_addrlist)(char *buffer, int count,
71                                      struct list_head *list);
72         int     (*nf_match_addr)(__u32 addr, struct list_head *list);
73         int     (*nf_match_netmask)(const __be32 *addr, size_t asize,
74                                     const __be32 *netmask,
75                                     const __be32 *netaddr);
76         int     (*nf_min_max)(struct list_head *nidlist, __u32 *min_nid,
77                               __u32 *max_nid);
78         int     (*nf_expand_addrrange)(struct list_head *addrranges,
79                                        __u32 *addrs, int max_addrs);
80 };
81
82 /**
83  * Structure to represent NULL-less strings.
84  */
85 struct cfs_lstr {
86         char            *ls_str;
87         int             ls_len;
88 };
89
90 /*
91  * Structure to represent \<range_expr\> token of the syntax.
92  */
93 struct cfs_range_expr {
94         /*
95          * Link to cfs_expr_list::el_exprs.
96          */
97         struct list_head        re_link;
98         __u32                   re_lo;
99         __u32                   re_hi;
100         __u32                   re_stride;
101 };
102
103 struct cfs_expr_list {
104         struct list_head        el_link;
105         struct list_head        el_exprs;
106 };
107
108 int cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp);
109 int cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res);
110 int cfs_str2num_check(char *str, int nob, unsigned *num,
111                       unsigned min, unsigned max);
112 int cfs_expr2str(struct list_head *list, char *str, size_t size);
113 int cfs_expr_list_print(char *buffer, int count,
114                         struct cfs_expr_list *expr_list);
115 int cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
116                         struct cfs_expr_list **elpp);
117 void cfs_expr_list_free(struct cfs_expr_list *expr_list);
118 void cfs_expr_list_free_list(struct list_head *list);
119 int cfs_ip_addr_parse(char *str, int len, struct list_head *list);
120 int cfs_ip_addr_range_gen(__u32 *ip_list, int count,
121                           struct list_head *ip_addr_expr);
122 int cfs_ip_addr_match(__u32 addr, struct list_head *list);
123 int libcfs_ip_in_netmask(const __be32 *addr, size_t asize,
124                          const __be32 *netmask,
125                          const __be32 *netaddr);
126 int cfs_expand_nidlist(struct list_head *nidlist, lnet_nid_t *lnet_nidlist,
127                        int max_nids);
128 int cfs_parse_nid_parts(char *str, struct list_head *addr,
129                         struct list_head *net_num, __u32 *net_type);
130 int cfs_abs_path(const char *request_path, char **resolved_path);
131 char *strim(char *s);
132
133 #endif