1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016 Intel Corporation 3 */ 4 5 #ifndef __PARSER_H 6 #define __PARSER_H 7 8 #include <ctype.h> 9 #include <sys/types.h> 10 #include <netinet/in.h> 11 #include <netinet/ip.h> 12 #include <string.h> 13 14 struct parse_status { 15 int status; 16 char parse_msg[256]; 17 }; 18 19 #define APP_CHECK(exp, st, fmt, ...) \ 20 do { \ 21 if (!(exp)) { \ 22 sprintf((st)->parse_msg, fmt "\n", \ 23 ## __VA_ARGS__); \ 24 (st)->status = -1; \ 25 } else \ 26 (st)->status = 0; \ 27 } while (0) 28 29 #define APP_CHECK_PRESENCE(val, str, status) \ 30 APP_CHECK(val == 0, status, \ 31 "item \"%s\" already present", str) 32 33 #define APP_CHECK_TOKEN_EQUAL(tokens, index, ref, status) \ 34 APP_CHECK(strcmp(tokens[index], ref) == 0, status, \ 35 "unrecognized input \"%s\": expect \"%s\"\n", \ 36 tokens[index], ref) 37 38 static inline int 39 is_str_num(const char *str) 40 { 41 uint32_t i; 42 43 for (i = 0; i < strlen(str); i++) 44 if (!isdigit(str[i])) 45 return -1; 46 47 return 0; 48 } 49 50 #define APP_CHECK_TOKEN_IS_NUM(tokens, index, status) \ 51 APP_CHECK(is_str_num(tokens[index]) == 0, status, \ 52 "input \"%s\" is not valid number string", tokens[index]) 53 54 55 #define INCREMENT_TOKEN_INDEX(index, max_num, status) \ 56 do { \ 57 APP_CHECK(index + 1 < max_num, status, "reaching the end of " \ 58 "the token array"); \ 59 index++; \ 60 } while (0) 61 62 int 63 parse_ipv4_addr(const char *token, struct in_addr *ipv4, uint32_t *mask); 64 65 int 66 parse_ipv6_addr(const char *token, struct in6_addr *ipv6, uint32_t *mask); 67 68 int 69 parse_range(const char *token, uint16_t *low, uint16_t *high); 70 71 void 72 sp4_sort_arr(void); 73 74 void 75 parse_sp4_tokens(char **tokens, uint32_t n_tokens, 76 struct parse_status *status); 77 78 void 79 sp6_sort_arr(void); 80 81 void 82 parse_sp6_tokens(char **tokens, uint32_t n_tokens, 83 struct parse_status *status); 84 85 void 86 sa_sort_arr(void); 87 88 void 89 parse_sa_tokens(char **tokens, uint32_t n_tokens, 90 struct parse_status *status); 91 92 void 93 parse_rt_tokens(char **tokens, uint32_t n_tokens, 94 struct parse_status *status); 95 96 int 97 parse_cfg_file(const char *cfg_filename); 98 99 #endif 100