25 #include <sys/types.h> 43 scan_ll(
const char *text,
long long *
result,
long long default_value,
46 long long local_result = default_value;
47 char *local_end_text = NULL;
52 local_result = strtoll(text, &local_end_text, 10);
53 if (errno == ERANGE) {
55 crm_warn(
"Integer parsed from '%s' was clipped to %lld",
58 }
else if (errno != 0) {
60 local_result = default_value;
61 crm_warn(
"Could not parse integer from '%s' (using %lld instead): " 64 }
else if (local_end_text == text) {
66 local_result = default_value;
67 crm_warn(
"Could not parse integer from '%s' (using %lld instead): " 68 "No digits found", text, default_value);
71 if ((end_text == NULL) && !pcmk__str_empty(local_end_text)) {
72 crm_warn(
"Characters left over after parsing '%s': '%s'",
73 text, local_end_text);
77 if (end_text != NULL) {
78 *end_text = local_end_text;
99 long long local_result = default_value;
103 rc = scan_ll(text, &local_result, default_value, NULL);
105 local_result = default_value;
134 if (result_ll < (
long long) minimum) {
135 crm_warn(
"Clipped '%s' to minimum acceptable value %d", text, minimum);
136 result_ll = (
long long) minimum;
138 }
else if (result_ll > INT_MAX) {
139 crm_warn(
"Clipped '%s' to maximum integer %d", text, INT_MAX);
140 result_ll = (
long long) INT_MAX;
145 *
result = (int) result_ll;
167 && ((port_ll < 0LL) || (port_ll > 65535LL))) {
168 crm_warn(
"Ignoring port specification '%s' " 169 "not in valid range (0-65535)", text);
174 *port = (int) port_ll;
203 char *local_end_text = NULL;
208 text = (text != NULL) ? text : default_text;
212 crm_debug(
"No text and no default conversion value supplied");
216 *
result = strtod(text, &local_end_text);
218 if (errno == ERANGE) {
228 const char *over_under;
230 if (QB_ABS(*
result) > DBL_MIN) {
235 over_under =
"under";
238 crm_debug(
"Floating-point value parsed from '%s' would %sflow " 239 "(using %g instead)", text, over_under, *
result);
241 }
else if (errno != 0) {
246 crm_debug(
"Could not parse floating-point value from '%s' (using " 250 }
else if (local_end_text == text) {
255 crm_debug(
"Could not parse floating-point value from '%s' (using " 256 "%.1f instead): No digits found", text,
259 }
else if (QB_ABS(*
result) <= DBL_MIN) {
272 for (
const char *p = text; p != local_end_text; p++) {
273 if (strchr(
"0.eE", *p) == NULL) {
275 crm_debug(
"Floating-point value parsed from '%s' would " 276 "underflow (using %g instead)", text, *
result);
282 crm_trace(
"Floating-point value parsed successfully from " 283 "'%s': %g", text, *
result);
286 if ((end_text == NULL) && !pcmk__str_empty(local_end_text)) {
287 crm_debug(
"Characters left over after parsing '%s': '%s'",
288 text, local_end_text);
292 if (end_text != NULL) {
293 *end_text = local_end_text;
318 CRM_CHECK((table != NULL) && (key != NULL),
return EINVAL);
324 value = g_hash_table_lookup(table, key);
334 if ((value_ll < 0) || (value_ll > G_MAXUINT)) {
335 crm_warn(
"Could not parse non-negative integer from %s", value);
340 *
result = (guint) value_ll;
346 # define NUMCHARS "0123456789." 350 # define WHITESPACE " \t\n\r\f" 366 const char *num_start = NULL;
368 long long multiplier = 1000;
369 long long divisor = 1;
372 char *end_text = NULL;
379 num_len = strspn(num_start,
NUMCHARS);
383 units = num_start + num_len;
386 if (!strncasecmp(units,
"ms", 2) || !strncasecmp(units,
"msec", 4)) {
389 }
else if (!strncasecmp(units,
"us", 2) || !strncasecmp(units,
"usec", 4)) {
392 }
else if (!strncasecmp(units,
"s", 1) || !strncasecmp(units,
"sec", 3)) {
395 }
else if (!strncasecmp(units,
"m", 1) || !strncasecmp(units,
"min", 3)) {
396 multiplier = 60 * 1000;
398 }
else if (!strncasecmp(units,
"h", 1) || !strncasecmp(units,
"hr", 2)) {
399 multiplier = 60 * 60 * 1000;
401 }
else if ((*units !=
'\0') && (*units !=
'\n') && (*units !=
'\r')) {
406 if (msec > (LLONG_MAX / multiplier)) {
418 gboolean ret = FALSE;
429 }
else if (strcasecmp(s,
"true") == 0
430 || strcasecmp(s,
"on") == 0
431 || strcasecmp(s,
"yes") == 0 || strcasecmp(s,
"y") == 0 || strcasecmp(s,
"1") == 0) {
435 }
else if (strcasecmp(s,
"false") == 0
436 || strcasecmp(s,
"off") == 0
437 || strcasecmp(s,
"no") == 0 || strcasecmp(s,
"n") == 0 || strcasecmp(s,
"0") == 0) {
461 for (len = strlen(str) - 1; len >= 0 && str[len] ==
'\n'; len--) {
484 const char *p = prefix;
498 ends_with(
const char *s,
const char *match,
bool as_extension)
500 if (pcmk__str_empty(match)) {
502 }
else if (s == NULL) {
511 s = strrchr(s, match[0]);
512 return (s == NULL)? false : !strcmp(s, match);
515 mlen = strlen(match);
517 return ((slen >= mlen) && !strcmp(s + slen - mlen, match));
535 return ends_with(s, match,
false);
562 return ends_with(s, match,
true);
585 pcmk__str_hash(gconstpointer v)
587 const signed char *p;
590 for (p = v; *p !=
'\0'; p++)
591 h = (h << 5) - h + *p;
609 GDestroyNotify value_destroy_func)
611 return g_hash_table_new_full(pcmk__str_hash, g_str_equal,
612 key_destroy_func, value_destroy_func);
617 pcmk__strcase_equal(gconstpointer a, gconstpointer b)
619 return pcmk__str_eq((
const char *)a, (
const char *)b,
pcmk__str_casei);
623 pcmk__strcase_hash(gconstpointer v)
625 const signed char *p;
628 for (p = v; *p !=
'\0'; p++)
629 h = (h << 5) - h + g_ascii_tolower(*p);
647 GDestroyNotify value_destroy_func)
649 return g_hash_table_new_full(pcmk__strcase_hash, pcmk__strcase_equal,
650 key_destroy_func, value_destroy_func);
654 copy_str_table_entry(gpointer key, gpointer value, gpointer user_data)
656 if (key && value && user_data) {
657 g_hash_table_insert((GHashTable*)user_data, strdup(key), strdup(value));
674 GHashTable *new_table = NULL;
678 g_hash_table_foreach(old_table, copy_str_table_entry, new_table);
701 const char *separator)
705 if (pcmk__str_empty(word)) {
711 *list = g_string_sized_new(init_size);
713 *list = g_string_new(NULL);
717 if ((*list)->len == 0) {
721 }
else if (separator == NULL) {
726 g_string_append(*list, separator);
727 g_string_append(*list, word);
744 char **
result,
unsigned int *result_len)
747 char *compressed = NULL;
748 char *uncompressed = strdup(
data);
749 #ifdef CLOCK_MONOTONIC 750 struct timespec after_t;
751 struct timespec before_t;
755 max = (length * 1.01) + 601;
758 #ifdef CLOCK_MONOTONIC 759 clock_gettime(CLOCK_MONOTONIC, &before_t);
762 compressed = calloc((
size_t) max,
sizeof(
char));
766 rc = BZ2_bzBuffToBuffCompress(compressed, result_len, uncompressed, length,
773 crm_err(
"Compression of %d bytes failed: %s " CRM_XS " rc=%d",
779 #ifdef CLOCK_MONOTONIC 780 clock_gettime(CLOCK_MONOTONIC, &after_t);
782 crm_trace(
"Compressed %d bytes into %d (ratio %d:1) in %.0fms",
783 length, *result_len, length / (*result_len),
784 (after_t.tv_sec - before_t.tv_sec) * 1000 +
785 (after_t.tv_nsec - before_t.tv_nsec) / 1e6);
787 crm_trace(
"Compressed %d bytes into %d (ratio %d:1)",
788 length, *result_len, length / (*result_len));
802 va_start(ap, format);
803 len = vasprintf (&
string, format, ap);
812 char *remainder = NULL;
820 crm_trace(
"Attempting to decode: [%s]", srcstring);
830 if (*srcstring ==
'-') {
845 if (*remainder && *remainder ==
'-') {
846 if (*(remainder+1)) {
847 char *more_remainder = NULL;
853 }
else if (*more_remainder !=
'\0') {
857 }
else if (*remainder && *remainder !=
'-') {
890 for (
const GList *ele = lst; ele != NULL; ele = ele->next) {
891 if (pcmk__str_eq(s, ele->data,
flags)) {
900 str_any_of(
const char *s, va_list args, uint32_t
flags)
907 const char *ele = va_arg(args,
const char *);
911 }
else if (pcmk__str_eq(s, ele,
flags)) {
992 const char *ele = va_arg(ap,
const char *);
996 }
else if (strchr(ele, ch) != NULL) {
1027 while (*s1 && *s2) {
1028 if (isdigit(*s1) && isdigit(*s2)) {
1033 long num1 = strtol(s1, &end1, 10);
1034 long num2 = strtol(s2, &end2, 10);
1037 size_t len1 = end1 - s1;
1038 size_t len2 = end2 - s2;
1042 }
else if (num1 > num2) {
1044 }
else if (len1 < len2) {
1046 }
else if (len1 > len2) {
1053 int lower1 = tolower(*s1);
1054 int lower2 = tolower(*s2);
1056 if (lower1 < lower2) {
1058 }
else if (lower1 > lower2) {
1067 }
else if (*s1 && !*s2) {
1113 int reg_flags = REG_EXTENDED | REG_NOSUB;
1117 if (s1 == NULL || s2 == NULL) {
1122 reg_flags |= REG_ICASE;
1124 regcomp_rc = regcomp(&r_patt, s2, reg_flags);
1125 if (regcomp_rc != 0) {
1127 crm_err(
"Bad regex '%s' for update: %s", s2, strerror(regcomp_rc));
1129 rc = regexec(&r_patt, s1, 0, NULL, 0);
1148 if (s1 == NULL || s2 == NULL) {
1158 }
else if (s2 == NULL) {
1167 if (strcmp(s1,
"*") == 0 || strcmp(s2,
"*") == 0) {
1173 return strcasecmp(s1, s2);
1175 return strcmp(s1, s2);
1195 if ((str != NULL) && !pcmk__str_eq(*str, value,
pcmk__str_none)) {
1197 if (value == NULL) {
1200 *str = strdup(value);
1222 va_start(ap, buffer);
1225 const char *ele = va_arg(ap,
const char *);
1230 g_string_append(buffer, ele);
1246 }
else if (a == NULL || b == NULL) {
1249 }
else if (strcasecmp(a, b) == 0) {
1259 return g_strcmp0(a, b) == 0;
1262 }
else if (a == b) {
1265 }
else if (a == NULL || b == NULL) {
1269 }
else if (strcasecmp(a, b) == 0) {
1278 if (buffer != NULL) {
1279 snprintf(buffer, len,
"%d", an_int);
1287 return pcmk__str_hash(v);
1293 return pcmk__strcase_equal(a, b);
1299 return pcmk__strcase_hash(v);
1314 text = default_text;
1316 crm_err(
"No default conversion value supplied");
1332 if (errno != ERANGE) {
1333 crm_err(
"Conversion of %s was clipped: %lld", text,
result);
1338 }
else if (
result > INT_MAX) {
1340 if (errno != ERANGE) {
1341 crm_err(
"Conversion of %s was clipped: %lld", text,
result);
#define CRM_CHECK(expr, failure_action)
bool pcmk__starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
char * crm_strdup_printf(char const *format,...)
int pcmk_numeric_strcasecmp(const char *s1, const char *s2)
guint g_str_hash_traditional(gconstpointer v)
GHashTable * pcmk__strkey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
#define PCMK__PARSE_DBL_DEFAULT
int pcmk__scan_port(const char *text, int *port)
int pcmk__scan_ll(const char *text, long long *result, long long default_value)
int pcmk__numeric_strcasecmp(const char *s1, const char *s2)
bool pcmk__str_any_of(const char *s,...)
int pcmk__scan_double(const char *text, double *result, const char *default_text, char **end_text)
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
void pcmk__str_update(char **str, const char *value)
#define PCMK__PARSE_INT_DEFAULT
#define crm_warn(fmt, args...)
int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val, guint *result)
#define crm_debug(fmt, args...)
void pcmk__add_separated_word(GString **list, size_t init_size, const char *word, const char *separator)
int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end)
int pcmk__compress(const char *data, unsigned int length, unsigned int max, char **result, unsigned int *result_len)
#define crm_trace(fmt, args...)
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
long long crm_get_msec(const char *input)
Parse a time+units string and return milliseconds equivalent.
int pcmk__scan_min_int(const char *text, int *result, int minimum)
GHashTable * pcmk__str_table_dup(GHashTable *old_table)
char * crm_itoa_stack(int an_int, char *buffer, size_t len)
Function and executable result codes.
int crm_str_to_boolean(const char *s, int *ret)
char * crm_strip_trailing_newline(char *str)
int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags)
GHashTable * pcmk__strikey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
gboolean crm_is_true(const char *s)
bool pcmk__ends_with_ext(const char *s, const char *match)
int pcmk__bzlib2rc(int bz2)
Map a bz2 return code to the most similar Pacemaker return code.
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
int crm_parse_int(const char *text, const char *default_text)
long long crm_parse_ll(const char *text, const char *default_text)
pcmk__action_result_t result
#define crm_err(fmt, args...)
Deprecated Pacemaker utilities.
gboolean crm_strcase_equal(gconstpointer a, gconstpointer b)
void pcmk__g_strcat(GString *buffer,...)
gboolean pcmk__str_in_list(const gchar *s, const GList *lst, uint32_t flags)
gboolean safe_str_neq(const char *a, const char *b)
guint crm_strcase_hash(gconstpointer v)
GHashTable * crm_str_table_dup(GHashTable *old_table)
char * pcmk__trim(char *str)
bool pcmk__char_in_any_str(int ch,...)
bool pcmk__ends_with(const char *s, const char *match)
bool pcmk__strcase_any_of(const char *s,...)