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;
359 long long multiplier = 1000;
360 long long divisor = 1;
368 while (isspace(*
input)) {
373 scan_ll(
input, &msec, -1, &units);
386 while (isdigit(*units) || (*units ==
'.')) {
391 while (isspace(*units)) {
399 || (strncasecmp(units,
"s", 1) == 0)
400 || (strncasecmp(units,
"sec", 3) == 0)) {
404 }
else if ((strncasecmp(units,
"ms", 2) == 0)
405 || (strncasecmp(units,
"msec", 4) == 0)) {
409 }
else if ((strncasecmp(units,
"us", 2) == 0)
410 || (strncasecmp(units,
"usec", 4) == 0)) {
414 }
else if ((strncasecmp(units,
"m", 1) == 0)
415 || (strncasecmp(units,
"min", 3) == 0)) {
416 multiplier = 60 * 1000;
419 }
else if ((strncasecmp(units,
"h", 1) == 0)
420 || (strncasecmp(units,
"hr", 2) == 0)) {
421 multiplier = 60 * 60 * 1000;
430 if (msec > (LLONG_MAX / multiplier)) {
433 return (msec * multiplier) / divisor;
461 if (
input[0] ==
'P') {
464 if (period_s != NULL) {
474 crm_warn(
"Using 0 instead of invalid interval specification '%s'",
481 if (result_ms != NULL) {
482 *result_ms = (msec >= G_MAXUINT)? G_MAXUINT : (guint) msec;
490 gboolean ret = FALSE;
536 for (len = strlen(str) - 1; len >= 0 && str[len] ==
'\n'; len--) {
559 const char *p = prefix;
573 ends_with(
const char *s,
const char *match,
bool as_extension)
575 if (pcmk__str_empty(match)) {
577 }
else if (s == NULL) {
586 s = strrchr(s, match[0]);
587 return (s == NULL)? false : !strcmp(s, match);
590 mlen = strlen(match);
592 return ((slen >= mlen) && !strcmp(s + slen - mlen, match));
610 return ends_with(s, match,
false);
637 return ends_with(s, match,
true);
660 pcmk__str_hash(gconstpointer v)
662 const signed char *p;
665 for (p = v; *p !=
'\0'; p++)
666 h = (h << 5) - h + *p;
684 GDestroyNotify value_destroy_func)
686 return g_hash_table_new_full(pcmk__str_hash, g_str_equal,
687 key_destroy_func, value_destroy_func);
710 pcmk__strcase_equal(gconstpointer a, gconstpointer b)
712 return pcmk__str_eq((
const char *)a, (
const char *)b,
pcmk__str_casei);
716 pcmk__strcase_hash(gconstpointer v)
718 const signed char *p;
721 for (p = v; *p !=
'\0'; p++)
722 h = (h << 5) - h + g_ascii_tolower(*p);
740 GDestroyNotify value_destroy_func)
742 return g_hash_table_new_full(pcmk__strcase_hash, pcmk__strcase_equal,
743 key_destroy_func, value_destroy_func);
747 copy_str_table_entry(gpointer key, gpointer value, gpointer user_data)
749 if (key && value && user_data) {
751 (
const char *) key, (
const char *) value);
768 GHashTable *new_table = NULL;
772 g_hash_table_foreach(old_table, copy_str_table_entry, new_table);
795 const char *separator)
799 if (pcmk__str_empty(word)) {
805 *list = g_string_sized_new(init_size);
807 *list = g_string_new(NULL);
811 if ((*list)->len == 0) {
815 }
else if (separator == NULL) {
820 g_string_append(*list, separator);
821 g_string_append(*list, word);
838 char **
result,
unsigned int *result_len)
841 char *compressed = NULL;
842 char *uncompressed = strdup(
data);
843 #ifdef CLOCK_MONOTONIC 844 struct timespec after_t;
845 struct timespec before_t;
849 max = (length * 1.01) + 601;
852 #ifdef CLOCK_MONOTONIC 853 clock_gettime(CLOCK_MONOTONIC, &before_t);
859 rc = BZ2_bzBuffToBuffCompress(compressed, result_len, uncompressed, length,
866 crm_err(
"Compression of %d bytes failed: %s " CRM_XS " rc=%d",
872 #ifdef CLOCK_MONOTONIC 873 clock_gettime(CLOCK_MONOTONIC, &after_t);
875 crm_trace(
"Compressed %d bytes into %d (ratio %d:1) in %.0fms",
876 length, *result_len, length / (*result_len),
877 (after_t.tv_sec - before_t.tv_sec) * 1000 +
878 (after_t.tv_nsec - before_t.tv_nsec) / 1e6);
880 crm_trace(
"Compressed %d bytes into %d (ratio %d:1)",
881 length, *result_len, length / (*result_len));
895 va_start(ap, format);
896 len = vasprintf (&
string, format, ap);
905 char *remainder = NULL;
913 crm_trace(
"Attempting to decode: [%s]", srcstring);
923 if (*srcstring ==
'-') {
938 if (*remainder && *remainder ==
'-') {
939 if (*(remainder+1)) {
940 char *more_remainder = NULL;
946 }
else if (*more_remainder !=
'\0') {
950 }
else if (*remainder && *remainder !=
'-') {
983 for (
const GList *ele = lst; ele != NULL; ele = ele->next) {
984 if (pcmk__str_eq(s, ele->data,
flags)) {
993 str_any_of(
const char *s, va_list args, uint32_t
flags)
1000 const char *ele = va_arg(args,
const char *);
1004 }
else if (pcmk__str_eq(s, ele,
flags)) {
1082 while (*s1 && *s2) {
1083 if (isdigit(*s1) && isdigit(*s2)) {
1088 long num1 = strtol(s1, &end1, 10);
1089 long num2 = strtol(s2, &end2, 10);
1092 size_t len1 = end1 - s1;
1093 size_t len2 = end2 - s2;
1097 }
else if (num1 > num2) {
1099 }
else if (len1 < len2) {
1101 }
else if (len1 > len2) {
1108 int lower1 = tolower(*s1);
1109 int lower2 = tolower(*s2);
1111 if (lower1 < lower2) {
1113 }
else if (lower1 > lower2) {
1122 }
else if (*s1 && !*s2) {
1168 int reg_flags = REG_EXTENDED | REG_NOSUB;
1172 if (s1 == NULL || s2 == NULL) {
1177 reg_flags |= REG_ICASE;
1179 regcomp_rc = regcomp(&r_patt, s2, reg_flags);
1180 if (regcomp_rc != 0) {
1182 crm_err(
"Bad regex '%s' for update: %s", s2, strerror(regcomp_rc));
1184 rc = regexec(&r_patt, s1, 0, NULL, 0);
1203 if (s1 == NULL || s2 == NULL) {
1213 }
else if (s2 == NULL) {
1222 if (strcmp(s1,
"*") == 0 || strcmp(s2,
"*") == 0) {
1228 return strcasecmp(s1, s2);
1230 return strcmp(s1, s2);
1252 char *
result = strdup(str);
1255 crm_abort(file,
function, line,
"Out of memory", FALSE, TRUE);
1279 if ((str != NULL) && !pcmk__str_eq(*str, value,
pcmk__str_none)) {
1301 va_start(ap, buffer);
1304 const char *ele = va_arg(ap,
const char *);
1309 g_string_append(buffer, ele);
1325 }
else if (a == NULL || b == NULL) {
1328 }
else if (strcasecmp(a, b) == 0) {
1338 return g_strcmp0(a, b) == 0;
1341 }
else if (a == b) {
1344 }
else if (a == NULL || b == NULL) {
1348 }
else if (strcasecmp(a, b) == 0) {
1357 if (buffer != NULL) {
1358 snprintf(buffer, len,
"%d", an_int);
1366 return pcmk__str_hash(v);
1372 return pcmk__strcase_equal(a, b);
1378 return pcmk__strcase_hash(v);
1393 text = default_text;
1395 crm_err(
"No default conversion value supplied");
1411 if (errno != ERANGE) {
1412 crm_err(
"Conversion of %s was clipped: %lld", text,
result);
1417 }
else if (
result > INT_MAX) {
1419 if (errno != ERANGE) {
1420 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)
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)
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,...)
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)
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
#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)