24 #include <sys/types.h> 42 scan_ll(
const char *text,
long long *
result,
long long default_value,
45 long long local_result = default_value;
46 char *local_end_text = NULL;
51 local_result = strtoll(text, &local_end_text, 10);
52 if (errno == ERANGE) {
54 crm_debug(
"Integer parsed from '%s' was clipped to %lld",
57 }
else if (local_end_text == text) {
59 local_result = default_value;
60 crm_debug(
"Could not parse integer from '%s' (using %lld instead): " 61 "No digits found", text, default_value);
63 }
else if (errno != 0) {
65 local_result = default_value;
66 crm_debug(
"Could not parse integer from '%s' (using %lld instead): " 70 if ((end_text == NULL) && !pcmk__str_empty(local_end_text)) {
71 crm_debug(
"Characters left over after parsing '%s': '%s'",
72 text, local_end_text);
76 if (end_text != NULL) {
77 *end_text = local_end_text;
98 long long local_result = default_value;
102 rc = scan_ll(text, &local_result, default_value, NULL);
104 local_result = default_value;
133 if (result_ll < (
long long) minimum) {
134 crm_warn(
"Clipped '%s' to minimum acceptable value %d", text, minimum);
135 result_ll = (
long long) minimum;
137 }
else if (result_ll > INT_MAX) {
138 crm_warn(
"Clipped '%s' to maximum integer %d", text, INT_MAX);
139 result_ll = (
long long) INT_MAX;
144 *
result = (int) result_ll;
168 }
else if ((text != NULL)
169 && ((port_ll < 0LL) || (port_ll > 65535LL))) {
170 crm_warn(
"Ignoring port specification '%s' " 171 "not in valid range (0-65535)", text);
176 *port = (int) port_ll;
205 char *local_end_text = NULL;
210 text = (text != NULL) ? text : default_text;
214 crm_debug(
"No text and no default conversion value supplied");
218 *
result = strtod(text, &local_end_text);
220 if (errno == ERANGE) {
230 const char *over_under;
232 if (QB_ABS(*
result) > DBL_MIN) {
237 over_under =
"under";
240 crm_debug(
"Floating-point value parsed from '%s' would %sflow " 241 "(using %g instead)", text, over_under, *
result);
243 }
else if (errno != 0) {
248 crm_debug(
"Could not parse floating-point value from '%s' (using " 252 }
else if (local_end_text == text) {
257 crm_debug(
"Could not parse floating-point value from '%s' (using " 258 "%.1f instead): No digits found", text,
261 }
else if (QB_ABS(*
result) <= DBL_MIN) {
274 for (
const char *p = text; p != local_end_text; p++) {
275 if (strchr(
"0.eE", *p) == NULL) {
277 crm_debug(
"Floating-point value parsed from '%s' would " 278 "underflow (using %g instead)", text, *
result);
284 crm_trace(
"Floating-point value parsed successfully from " 285 "'%s': %g", text, *
result);
288 if ((end_text == NULL) && !pcmk__str_empty(local_end_text)) {
289 crm_debug(
"Characters left over after parsing '%s': '%s'",
290 text, local_end_text);
294 if (end_text != NULL) {
295 *end_text = local_end_text;
320 CRM_CHECK((table != NULL) && (key != NULL),
return EINVAL);
326 value = g_hash_table_lookup(table, key);
333 crm_warn(
"Using default (%u) for %s because '%s' is not a " 334 "valid integer: %s", default_val, key, value,
pcmk_rc_str(rc));
338 if ((value_ll < 0) || (value_ll > G_MAXUINT)) {
339 crm_warn(
"Using default (%u) for %s because '%s' is not in valid range",
340 default_val, key, value);
345 *
result = (guint) value_ll;
364 long long multiplier = 1000;
365 long long divisor = 1;
374 while (isspace(*
input)) {
380 if ((rc == EOVERFLOW) && (msec > 0)) {
383 }
else if ((rc !=
pcmk_rc_ok) || (msec < 0)) {
384 crm_warn(
"'%s' is not a valid time duration: %s",
397 while (isdigit(*units) || (*units ==
'.')) {
402 while (isspace(*units)) {
410 || (strncasecmp(units,
"s", 1) == 0)
411 || (strncasecmp(units,
"sec", 3) == 0)) {
415 }
else if ((strncasecmp(units,
"ms", 2) == 0)
416 || (strncasecmp(units,
"msec", 4) == 0)) {
420 }
else if ((strncasecmp(units,
"us", 2) == 0)
421 || (strncasecmp(units,
"usec", 4) == 0)) {
425 }
else if ((strncasecmp(units,
"m", 1) == 0)
426 || (strncasecmp(units,
"min", 3) == 0)) {
427 multiplier = 60 * 1000;
430 }
else if ((strncasecmp(units,
"h", 1) == 0)
431 || (strncasecmp(units,
"hr", 2) == 0)) {
432 multiplier = 60 * 60 * 1000;
441 if (msec > (LLONG_MAX / multiplier)) {
444 return (msec * multiplier) / divisor;
472 if (
input[0] ==
'P') {
475 if (period_s != NULL) {
477 msec = QB_MIN(msec, G_MAXUINT / 1000) * 1000;
486 crm_warn(
"Using 0 instead of invalid interval specification '%s'",
493 if (result_ms != NULL) {
494 *result_ms = (msec >= G_MAXUINT)? G_MAXUINT : (guint) msec;
502 gboolean ret = FALSE;
548 for (len = strlen(str) - 1; len >= 0 && str[len] ==
'\n'; len--) {
571 const char *p = prefix;
585 ends_with(
const char *s,
const char *match,
bool as_extension)
587 if (pcmk__str_empty(match)) {
589 }
else if (s == NULL) {
598 s = strrchr(s, match[0]);
599 return (s == NULL)? false : !strcmp(s, match);
602 mlen = strlen(match);
604 return ((slen >= mlen) && !strcmp(s + slen - mlen, match));
622 return ends_with(s, match,
false);
649 return ends_with(s, match,
true);
672 pcmk__str_hash(gconstpointer v)
674 const signed char *p;
677 for (p = v; *p !=
'\0'; p++)
678 h = (h << 5) - h + *p;
696 GDestroyNotify value_destroy_func)
698 return g_hash_table_new_full(pcmk__str_hash, g_str_equal,
699 key_destroy_func, value_destroy_func);
722 pcmk__strcase_equal(gconstpointer a, gconstpointer b)
724 return pcmk__str_eq((
const char *)a, (
const char *)b,
pcmk__str_casei);
728 pcmk__strcase_hash(gconstpointer v)
730 const signed char *p;
733 for (p = v; *p !=
'\0'; p++)
734 h = (h << 5) - h + g_ascii_tolower(*p);
752 GDestroyNotify value_destroy_func)
754 return g_hash_table_new_full(pcmk__strcase_hash, pcmk__strcase_equal,
755 key_destroy_func, value_destroy_func);
759 copy_str_table_entry(gpointer key, gpointer value, gpointer user_data)
761 if (key && value && user_data) {
763 (
const char *) key, (
const char *) value);
780 GHashTable *new_table = NULL;
784 g_hash_table_foreach(old_table, copy_str_table_entry, new_table);
807 const char *separator)
811 if (pcmk__str_empty(word)) {
817 *list = g_string_sized_new(init_size);
819 *list = g_string_new(NULL);
823 if ((*list)->len == 0) {
827 }
else if (separator == NULL) {
832 g_string_append(*list, separator);
833 g_string_append(*list, word);
850 char **
result,
unsigned int *result_len)
853 char *compressed = NULL;
854 char *uncompressed = strdup(
data);
855 #ifdef CLOCK_MONOTONIC 856 struct timespec after_t;
857 struct timespec before_t;
861 max = (length * 1.01) + 601;
864 #ifdef CLOCK_MONOTONIC 865 clock_gettime(CLOCK_MONOTONIC, &before_t);
871 rc = BZ2_bzBuffToBuffCompress(compressed, result_len, uncompressed, length,
878 crm_err(
"Compression of %d bytes failed: %s " CRM_XS " rc=%d",
884 #ifdef CLOCK_MONOTONIC 885 clock_gettime(CLOCK_MONOTONIC, &after_t);
887 crm_trace(
"Compressed %d bytes into %d (ratio %d:1) in %.0fms",
888 length, *result_len, length / (*result_len),
889 (after_t.tv_sec - before_t.tv_sec) * 1000 +
890 (after_t.tv_nsec - before_t.tv_nsec) / 1e6);
892 crm_trace(
"Compressed %d bytes into %d (ratio %d:1)",
893 length, *result_len, length / (*result_len));
907 va_start(ap, format);
908 len = vasprintf(&
string, format, ap);
917 char *remainder = NULL;
925 crm_trace(
"Attempting to decode: [%s]", srcstring);
935 if (*srcstring ==
'-') {
950 if (*remainder && *remainder ==
'-') {
951 if (*(remainder+1)) {
952 char *more_remainder = NULL;
958 }
else if (*more_remainder !=
'\0') {
962 }
else if (*remainder && *remainder !=
'-') {
995 for (
const GList *ele = lst; ele != NULL; ele = ele->next) {
996 if (pcmk__str_eq(s, ele->data,
flags)) {
1005 str_any_of(
const char *s, va_list args, uint32_t
flags)
1012 const char *ele = va_arg(args,
const char *);
1016 }
else if (pcmk__str_eq(s, ele,
flags)) {
1094 while (*s1 && *s2) {
1095 if (isdigit(*s1) && isdigit(*s2)) {
1100 long num1 = strtol(s1, &end1, 10);
1101 long num2 = strtol(s2, &end2, 10);
1104 size_t len1 = end1 - s1;
1105 size_t len2 = end2 - s2;
1109 }
else if (num1 > num2) {
1111 }
else if (len1 < len2) {
1113 }
else if (len1 > len2) {
1120 int lower1 = tolower(*s1);
1121 int lower2 = tolower(*s2);
1123 if (lower1 < lower2) {
1125 }
else if (lower1 > lower2) {
1134 }
else if (*s1 && !*s2) {
1180 int reg_flags = REG_EXTENDED | REG_NOSUB;
1184 if (s1 == NULL || s2 == NULL) {
1189 reg_flags |= REG_ICASE;
1191 regcomp_rc = regcomp(&r_patt, s2, reg_flags);
1192 if (regcomp_rc != 0) {
1194 crm_err(
"Bad regex '%s' for update: %s", s2, strerror(regcomp_rc));
1196 rc = regexec(&r_patt, s1, 0, NULL, 0);
1215 if (s1 == NULL || s2 == NULL) {
1225 }
else if (s2 == NULL) {
1234 if (strcmp(s1,
"*") == 0 || strcmp(s2,
"*") == 0) {
1240 return strcasecmp(s1, s2);
1242 return strcmp(s1, s2);
1264 char *
result = strdup(str);
1267 crm_abort(file,
function, line,
"Out of memory", FALSE, TRUE);
1291 if ((str != NULL) && !pcmk__str_eq(*str, value,
pcmk__str_none)) {
1313 va_start(ap, buffer);
1316 const char *ele = va_arg(ap,
const char *);
1321 g_string_append(buffer, ele);
1337 }
else if (a == NULL || b == NULL) {
1340 }
else if (strcasecmp(a, b) == 0) {
1350 return g_strcmp0(a, b) == 0;
1353 }
else if (a == b) {
1356 }
else if (a == NULL || b == NULL) {
1360 }
else if (strcasecmp(a, b) == 0) {
1369 if (buffer != NULL) {
1370 snprintf(buffer, len,
"%d", an_int);
1378 return pcmk__str_hash(v);
1384 return pcmk__strcase_equal(a, b);
1390 return pcmk__strcase_hash(v);
1405 text = default_text;
1407 crm_err(
"No default conversion value supplied");
1423 if (errno != ERANGE) {
1424 crm_err(
"Conversion of %s was clipped: %lld", text,
result);
1429 }
else if (
result > INT_MAX) {
1431 if (errno != ERANGE) {
1432 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,...)
_Noreturn crm_exit_t crm_exit(crm_exit_t rc)
int pcmk_numeric_strcasecmp(const char *s1, const char *s2)
struct crm_time_s crm_time_t
guint g_str_hash_traditional(gconstpointer v)
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
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,...)
crm_time_t * crm_time_parse_duration(const char *duration_str)
Parse a time duration from an ISO 8601 duration specification.
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)
External (OS/environmental) problem.
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.
void pcmk__insert_dup(GHashTable *table, const char *name, const char *value)
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)
int pcmk_parse_interval_spec(const char *input, guint *result_ms)
Parse milliseconds from a Pacemaker interval specification.
long long crm_time_get_seconds(const crm_time_t *dt)
char * crm_itoa_stack(int an_int, char *buffer, size_t len)
#define pcmk__str_copy(str)
int crm_str_to_boolean(const char *s, int *ret)
#define pcmk__assert(expr)
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,...)
char * pcmk__str_copy_as(const char *file, const char *function, uint32_t line, const char *str)
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)
#define pcmk__assert_alloc(nmemb, size)
bool pcmk__ends_with(const char *s, const char *match)
bool pcmk__strcase_any_of(const char *s,...)
void crm_time_free(crm_time_t *dt)