2 * Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file string.c Memory allocation and string handling functions. */
12 #include <sys/utsname.h> /* uname() */
26 * Paraslash's version of realloc().
28 * \param p Pointer to the memory block, may be \p NULL.
29 * \param size The desired new size.
31 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
32 * i.e. there is no need to check the return value in the caller.
34 * \return A pointer to newly allocated memory which is suitably aligned for
35 * any kind of variable and may be different from \a p.
39 __must_check void *para_realloc(void *p, size_t size)
42 * No need to check for NULL pointers: If p is NULL, the call
43 * to realloc is equivalent to malloc(size)
46 if (!(p = realloc(p, size))) {
47 PARA_EMERG_LOG("realloc failed (size = %zu), aborting\n",
55 * Paraslash's version of malloc().
57 * \param size The desired new size.
59 * A wrapper for malloc(3) which exits on errors.
61 * \return A pointer to the allocated memory, which is suitably aligned for any
66 __must_check __malloc void *para_malloc(size_t size)
73 PARA_EMERG_LOG("malloc failed (size = %zu), aborting\n",
81 * Paraslash's version of calloc().
83 * \param size The desired new size.
85 * A wrapper for calloc(3) which exits on errors.
87 * \return A pointer to the allocated and zeroed-out memory, which is suitably
88 * aligned for any kind of variable.
92 __must_check __malloc void *para_calloc(size_t size)
94 void *ret = para_malloc(size);
101 * Paraslash's version of strdup().
103 * \param s The string to be duplicated.
105 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
106 * there is no need to check the return value in the caller.
108 * \return A pointer to the duplicated string. If \a s was the \p NULL pointer,
109 * an pointer to an empty string is returned.
113 __must_check __malloc char *para_strdup(const char *s)
117 if ((ret = strdup(s? s: "")))
119 PARA_EMERG_LOG("strdup failed, aborting\n");
124 * Print a formated message to a dynamically allocated string.
126 * \param result The formated string is returned here.
127 * \param fmt The format string.
128 * \param ap Initialized list of arguments.
130 * This function is similar to vasprintf(), a GNU extension which is not in C
131 * or POSIX. It allocates a string large enough to hold the output including
132 * the terminating null byte. The allocated string is returned via the first
133 * argument and must be freed by the caller. However, unlike vasprintf(), this
134 * function calls exit() if insufficient memory is available, while vasprintf()
135 * returns -1 in this case.
137 * \return Number of bytes written, not including the terminating \p NULL
140 * \sa printf(3), vsnprintf(3), va_start(3), vasprintf(3), \ref xasprintf().
142 __printf_2_0 unsigned xvasprintf(char **result, const char *fmt, va_list ap)
148 *result = para_malloc(size + 1);
150 ret = vsnprintf(*result, size, fmt, aq);
153 if (ret < size) /* OK */
156 *result = para_realloc(*result, size);
158 ret = vsnprintf(*result, size, fmt, aq);
160 assert(ret >= 0 && ret < size);
165 * Print to a dynamically allocated string, variable number of arguments.
167 * \param result See \ref xvasprintf().
168 * \param fmt Usual format string.
170 * \return The return value of the underlying call to \ref xvasprintf().
172 * \sa \ref xvasprintf() and the references mentioned there.
174 __printf_2_3 unsigned xasprintf(char **result, const char *fmt, ...)
180 ret = xvasprintf(result, fmt, ap);
186 * Allocate a sufficiently large string and print into it.
188 * \param fmt A usual format string.
190 * Produce output according to \p fmt. No artificial bound on the length of the
191 * resulting string is imposed.
193 * \return This function either returns a pointer to a string that must be
194 * freed by the caller or aborts without returning.
196 * \sa printf(3), xasprintf().
198 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
204 xvasprintf(&msg, fmt, ap);
210 * Free the content of a pointer and set it to \p NULL.
212 * This is equivalent to "free(*arg); *arg = NULL;".
214 * \param arg The pointer whose content should be freed.
216 void freep(void *arg)
218 void **ptr = (void **)arg;
224 * Paraslash's version of strcat().
226 * \param a String to be appended to.
227 * \param b String to append.
229 * Append \p b to \p a.
231 * \return If \a a is \p NULL, return a pointer to a copy of \a b, i.e.
232 * para_strcat(NULL, b) is equivalent to para_strdup(b). If \a b is \p NULL,
233 * return \a a without making a copy of \a a. Otherwise, construct the
234 * concatenation \a c, free \a a (but not \a b) and return \a c.
238 __must_check __malloc char *para_strcat(char *a, const char *b)
243 return para_strdup(b);
246 tmp = make_message("%s%s", a, b);
252 * Paraslash's version of dirname().
254 * \param name Pointer to the full path.
256 * Compute the directory component of \p name.
258 * \return If \a name is \p NULL or the empty string, return \p NULL.
259 * Otherwise, Make a copy of \a name and return its directory component. Caller
260 * is responsible to free the result.
262 __must_check __malloc char *para_dirname(const char *name)
268 ret = para_strdup(name);
269 p = strrchr(ret, '/');
278 * Paraslash's version of basename().
280 * \param name Pointer to the full path.
282 * Compute the filename component of \a name.
284 * \return \p NULL if (a) \a name is the empty string or \p NULL, or (b) name
285 * ends with a slash. Otherwise, a pointer within \a name is returned. Caller
286 * must not free the result.
288 __must_check char *para_basename(const char *name)
294 ret = strrchr(name, '/');
302 * Get the logname of the current user.
304 * \return A dynamically allocated string that must be freed by the caller. On
305 * errors, the string "unknown_user" is returned, i.e. this function never
310 __must_check __malloc char *para_logname(void)
312 struct passwd *pw = getpwuid(getuid());
313 return para_strdup(pw? pw->pw_name : "unknown_user");
317 * Get the home directory of the current user.
319 * \return A dynamically allocated string that must be freed by the caller. If
320 * the home directory could not be found, this function returns "/tmp".
322 __must_check __malloc char *para_homedir(void)
324 struct passwd *pw = getpwuid(getuid());
325 return para_strdup(pw? pw->pw_dir : "/tmp");
329 * Get the own hostname.
331 * \return A dynamically allocated string containing the hostname.
335 __malloc char *para_hostname(void)
340 return para_strdup(u.nodename);
344 * Call a custom function for each complete line.
346 * \param flags Any combination of flags defined in \ref for_each_line_flags.
347 * \param buf The buffer containing data separated by newlines.
348 * \param size The number of bytes in \a buf.
349 * \param line_handler The custom function.
350 * \param private_data Pointer passed to \a line_handler.
352 * For each complete line in \p buf, \p line_handler is called. The first
353 * argument to \p line_handler is (a copy of) the current line, and \p
354 * private_data is passed as the second argument. If the \p FELF_READ_ONLY
355 * flag is unset, a pointer into \a buf is passed to the line handler,
356 * otherwise a pointer to a copy of the current line is passed instead. This
357 * copy is freed immediately after the line handler returns.
359 * The function returns if \p line_handler returns a negative value or no more
360 * lines are in the buffer. The rest of the buffer (last chunk containing an
361 * incomplete line) is moved to the beginning of the buffer if FELF_READ_ONLY is
364 * \return On success this function returns the number of bytes not handled to
365 * \p line_handler. The only possible error is a negative return value from the
366 * line handler. In this case processing stops and the return value of the line
367 * handler is returned to indicate failure.
369 * \sa \ref for_each_line_flags.
371 int for_each_line(unsigned flags, char *buf, size_t size,
372 line_handler_t *line_handler, void *private_data)
374 char *start = buf, *end;
375 int ret, i, num_lines = 0;
377 // PARA_NOTICE_LOG("buf: %s\n", buf);
378 while (start < buf + size) {
382 next_cr = memchr(start, '\n', buf + size - start);
383 next_null = memchr(start, '\0', next_cr?
384 next_cr - start : buf + size - start);
385 if (!next_cr && !next_null)
392 if (!(flags & FELF_DISCARD_FIRST) || start != buf) {
393 if (flags & FELF_READ_ONLY) {
394 size_t s = end - start;
395 char *b = para_malloc(s + 1);
398 ret = line_handler(b, private_data);
402 ret = line_handler(start, private_data);
409 i = buf + size - start;
410 if (i && i != size && !(flags & FELF_READ_ONLY))
411 memmove(buf, start, i);
415 /** Return the hex characters of the lower 4 bits. */
416 #define hex(a) (hexchar[(a) & 15])
418 static void write_size_header(char *buf, int n)
420 static char hexchar[] = "0123456789abcdef";
422 buf[0] = hex(n >> 12);
423 buf[1] = hex(n >> 8);
424 buf[2] = hex(n >> 4);
430 * Read a four-byte hex-number and return its value.
432 * Each status item sent by para_server is prefixed with such a hex number in
433 * ASCII which describes the size of the status item.
435 * \param buf The buffer which must be at least four bytes long.
437 * \return The value of the hex number on success, \p -E_SIZE_PREFIX if the
438 * buffer did not contain only hex digits.
440 int read_size_header(const char *buf)
444 for (i = 0; i < 4; i++) {
445 unsigned char c = buf[i];
447 if (c >= '0' && c <= '9') {
451 if (c >= 'a' && c <= 'f') {
455 return -E_SIZE_PREFIX;
458 return -E_SIZE_PREFIX;
463 * Safely print into a buffer at a given offset.
465 * \param b Determines the buffer, its size, and the offset.
466 * \param fmt The format string.
468 * This function prints into the buffer given by \a b at the offset which is
469 * also given by \a b. If there is not enough space to hold the result, the
470 * buffer size is doubled until the underlying call to vsnprintf() succeeds
471 * or the size of the buffer exceeds the maximal size specified in \a b.
473 * In the latter case the unmodified \a buf and \a offset values as well as the
474 * private_data pointer of \a b are passed to the \a max_size_handler of \a b.
475 * If this function succeeds, i.e. returns a non-negative value, the offset of
476 * \a b is reset to zero and the given data is written to the beginning of the
477 * buffer. If \a max_size_handler() returns a negative value, this value is
478 * returned by \a para_printf().
480 * Upon return, the offset of \a b is adjusted accordingly so that subsequent
481 * calls to this function append data to what is already contained in the
484 * It's OK to call this function with \p b->buf being \p NULL. In this case, an
485 * initial buffer is allocated.
487 * \return The number of bytes printed into the buffer (not including the
488 * terminating \p NULL byte) on success, negative on errors. If there is no
489 * size-bound on \a b, i.e. if \p b->max_size is zero, this function never
492 * \sa make_message(), vsnprintf(3).
494 __printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...)
496 int ret, sz_off = (b->flags & PBF_SIZE_PREFIX)? 5 : 0;
499 b->buf = para_malloc(128);
504 char *p = b->buf + b->offset;
505 size_t size = b->size - b->offset;
510 ret = vsnprintf(p + sz_off, size - sz_off, fmt, ap);
512 if (ret > -1 && ret < size - sz_off) { /* success */
513 b->offset += ret + sz_off;
515 write_size_header(p, ret);
519 /* check if we may grow the buffer */
520 if (!b->max_size || 2 * b->size < b->max_size) { /* yes */
521 /* try again with more space */
523 b->buf = para_realloc(b->buf, b->size);
526 /* can't grow buffer */
527 if (!b->offset || !b->max_size_handler) /* message too large */
528 return -ERRNO_TO_PARA_ERROR(ENOSPC);
529 ret = b->max_size_handler(b->buf, b->offset, b->private_data);
536 /** \cond llong_minmax */
537 /* LLONG_MAX and LLONG_MIN might not be defined. */
539 #define LLONG_MAX 9223372036854775807LL
542 #define LLONG_MIN (-LLONG_MAX - 1LL)
544 /** \endcond llong_minmax */
547 * Convert a string to a 64-bit signed integer value.
549 * \param str The string to be converted.
550 * \param value Result pointer.
554 * \sa para_atoi32(), strtol(3), atoi(3).
556 int para_atoi64(const char *str, int64_t *value)
561 errno = 0; /* To distinguish success/failure after call */
562 tmp = strtoll(str, &endptr, 10);
563 if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
564 return -E_ATOI_OVERFLOW;
566 * If there were no digits at all, strtoll() stores the original value
570 return -E_ATOI_NO_DIGITS;
572 * The implementation may also set errno and return 0 in case no
573 * conversion was performed.
575 if (errno != 0 && tmp == 0)
576 return -E_ATOI_NO_DIGITS;
577 if (*endptr != '\0') /* Further characters after number */
578 return -E_ATOI_JUNK_AT_END;
584 * Convert a string to a 32-bit signed integer value.
586 * \param str The string to be converted.
587 * \param value Result pointer.
593 int para_atoi32(const char *str, int32_t *value)
597 const int32_t max = 2147483647;
599 ret = para_atoi64(str, &tmp);
602 if (tmp > max || tmp < -max - 1)
603 return -E_ATOI_OVERFLOW;
608 static inline int loglevel_equal(const char *arg, const char * const ll)
610 return !strncasecmp(arg, ll, strlen(ll));
614 * Compute the loglevel number from its name.
616 * \param txt The name of the loglevel (debug, info, ...).
618 * \return The numeric representation of the loglevel name.
620 int get_loglevel_by_name(const char *txt)
622 if (loglevel_equal(txt, "debug"))
624 if (loglevel_equal(txt, "info"))
626 if (loglevel_equal(txt, "notice"))
628 if (loglevel_equal(txt, "warning"))
630 if (loglevel_equal(txt, "error"))
632 if (loglevel_equal(txt, "crit"))
634 if (loglevel_equal(txt, "emerg"))
639 static int get_next_word(const char *buf, const char *delim, char **word)
641 enum line_state_flags {LSF_HAVE_WORD = 1, LSF_BACKSLASH = 2,
642 LSF_SINGLE_QUOTE = 4, LSF_DOUBLE_QUOTE = 8};
647 out = para_malloc(strlen(buf) + 1);
650 for (in = buf; *in; in++) {
655 if (state & LSF_BACKSLASH) /* \\ */
657 state |= LSF_BACKSLASH;
658 state |= LSF_HAVE_WORD;
662 if (state & LSF_BACKSLASH) { /* \n or \t */
663 *out++ = (*in == 'n')? '\n' : '\t';
664 state &= ~LSF_BACKSLASH;
669 if (state & LSF_BACKSLASH) /* \" */
671 if (state & LSF_SINGLE_QUOTE) /* '" */
673 if (state & LSF_DOUBLE_QUOTE) {
674 state &= ~LSF_DOUBLE_QUOTE;
677 state |= LSF_HAVE_WORD;
678 state |= LSF_DOUBLE_QUOTE;
681 if (state & LSF_BACKSLASH) /* \' */
683 if (state & LSF_DOUBLE_QUOTE) /* "' */
685 if (state & LSF_SINGLE_QUOTE) {
686 state &= ~LSF_SINGLE_QUOTE;
689 state |= LSF_HAVE_WORD;
690 state |= LSF_SINGLE_QUOTE;
693 for (p = delim; *p; p++) {
696 if (state & LSF_BACKSLASH)
698 if (state & LSF_SINGLE_QUOTE)
700 if (state & LSF_DOUBLE_QUOTE)
702 if (state & LSF_HAVE_WORD)
706 if (*p) /* ignore delimiter at the beginning */
709 state |= LSF_HAVE_WORD;
711 state &= ~LSF_BACKSLASH;
714 if (!(state & LSF_HAVE_WORD))
716 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
717 if (state & LSF_BACKSLASH) {
718 PARA_ERROR_LOG("trailing backslash\n");
721 if ((state & LSF_SINGLE_QUOTE) || (state & LSF_DOUBLE_QUOTE)) {
722 PARA_ERROR_LOG("unmatched quote character\n");
735 * Get the number of the word the cursor is on.
737 * \param buf The zero-terminated line buffer.
738 * \param delim Characters that separate words.
739 * \param point The cursor position.
741 * \return Zero-based word number.
743 int compute_word_num(const char *buf, const char *delim, int point)
749 for (p = buf, num_words = 0; ; p += ret, num_words++) {
750 ret = get_next_word(p, delim, &word);
754 if (p + ret >= buf + point)
761 * Free an array of words created by create_argv() or create_shifted_argv().
763 * \param argv A pointer previously obtained by \ref create_argv().
765 void free_argv(char **argv)
771 for (i = 0; argv[i]; i++)
776 static int create_argv_offset(int offset, const char *buf, const char *delim,
779 char *word, **argv = para_malloc((offset + 1) * sizeof(char *));
783 for (i = 0; i < offset; i++)
785 for (p = buf; p && *p; p += ret, i++) {
786 ret = get_next_word(p, delim, &word);
791 argv = para_realloc(argv, (i + 2) * sizeof(char*));
806 * Split a buffer into words.
808 * This parser honors single and double quotes, backslash-escaped characters
809 * and special characters like \p \\n. The result contains pointers to copies
810 * of the words contained in \a buf and has to be freed by using \ref
813 * \param buf The buffer to be split.
814 * \param delim Each character in this string is treated as a separator.
815 * \param result The array of words is returned here.
817 * \return Number of words in \a buf, negative on errors.
819 int create_argv(const char *buf, const char *delim, char ***result)
821 return create_argv_offset(0, buf, delim, result);
825 * Split a buffer into words, offset one.
827 * This is similar to \ref create_argv() but the returned array is one element
828 * larger, words start at index one and element zero is initialized to \p NULL.
829 * Callers must set element zero to a non-NULL value before calling free_argv()
830 * on the returned array to avoid a memory leak.
832 * \param buf See \ref create_argv().
833 * \param delim See \ref create_argv().
834 * \param result See \ref create_argv().
836 * \return Number of words plus one on success, negative on errors.
838 int create_shifted_argv(const char *buf, const char *delim, char ***result)
840 return create_argv_offset(1, buf, delim, result);
844 * Find out if the given string is contained in the arg vector.
846 * \param arg The string to look for.
847 * \param argv The array to search.
849 * \return The first index whose value equals \a arg, or \p -E_ARG_NOT_FOUND if
850 * arg was not found in \a argv.
852 int find_arg(const char *arg, char **argv)
857 return -E_ARG_NOT_FOUND;
858 for (i = 0; argv[i]; i++)
859 if (strcmp(arg, argv[i]) == 0)
861 return -E_ARG_NOT_FOUND;
865 * Compile a regular expression.
867 * This simple wrapper calls regcomp() and logs a message on errors.
869 * \param preg See regcomp(3).
870 * \param regex See regcomp(3).
871 * \param cflags See regcomp(3).
875 int para_regcomp(regex_t *preg, const char *regex, int cflags)
879 int ret = regcomp(preg, regex, cflags);
883 size = regerror(ret, preg, NULL, 0);
884 buf = para_malloc(size);
885 regerror(ret, preg, buf, size);
886 PARA_ERROR_LOG("%s\n", buf);
892 * strdup() for not necessarily zero-terminated strings.
894 * \param src The source buffer.
895 * \param len The number of bytes to be copied.
897 * \return A 0-terminated buffer of length \a len + 1.
899 * This is similar to strndup(), which is a GNU extension. However, one
900 * difference is that strndup() returns \p NULL if insufficient memory was
901 * available while this function aborts in this case.
903 * \sa strdup(), \ref para_strdup().
905 char *safe_strdup(const char *src, size_t len)
909 assert(len < (size_t)-1);
910 p = para_malloc(len + 1);
918 * Copy the value of a key=value pair.
920 * This checks whether the given buffer starts with "key=", ignoring case. If
921 * yes, a copy of the value is returned. The source buffer may not be
924 * \param src The source buffer.
925 * \param len The number of bytes of the tag.
926 * \param key Only copy if it is the value of this key.
928 * \return A zero-terminated buffer, or \p NULL if the key was
929 * not of the given type.
931 char *key_value_copy(const char *src, size_t len, const char *key)
933 int keylen = strlen(key);
937 if (strncasecmp(src, key, keylen))
939 if (src[keylen] != '=')
941 return safe_strdup(src + keylen + 1, len - keylen - 1);
944 static bool utf8_mode(void)
946 static bool initialized, have_utf8;
949 char *info = nl_langinfo(CODESET);
950 have_utf8 = (info && strcmp(info, "UTF-8") == 0);
952 PARA_INFO_LOG("%susing UTF-8 character encoding\n",
953 have_utf8? "" : "not ");
959 * glibc's wcswidth returns -1 if the string contains a tab character, which
960 * makes the function next to useless. The two functions below are taken from
964 #define IsWPrint(wc) (iswprint(wc) || wc >= 0xa0)
966 static int mutt_wcwidth(wchar_t wc, size_t pos)
970 if (wc == 0x09) /* tab */
971 return (pos | 7) + 1 - pos;
973 if (IsWPrint(wc) && n > 0)
982 static size_t mutt_wcswidth(const wchar_t *s, size_t n)
987 w += mutt_wcwidth(*s++, w);
992 * Skip a given number of cells at the beginning of a string.
994 * \param s The input string.
995 * \param cells_to_skip Desired number of cells that should be skipped.
996 * \param bytes_to_skip Result.
998 * This function computes how many input bytes must be skipped to advance a
999 * string by the given width. If the current character encoding is not UTF-8,
1000 * this is simply the given number of cells, i.e. \a cells_to_skip. Otherwise,
1001 * \a s is treated as a multibyte string and on successful return, \a s +
1002 * bytes_to_skip points to the start of a multibyte string such that the total
1003 * width of the multibyte characters that are skipped by advancing \a s that
1004 * many bytes equals at least \a cells_to_skip.
1008 int skip_cells(const char *s, size_t cells_to_skip, size_t *bytes_to_skip)
1012 size_t n, bytes_parsed, cells_skipped;
1015 if (cells_to_skip == 0)
1018 *bytes_to_skip = cells_to_skip;
1021 bytes_parsed = cells_skipped = 0;
1022 memset(&ps, 0, sizeof(ps));
1024 while (cells_to_skip > cells_skipped) {
1027 mbret = mbrtowc(&wc, s + bytes_parsed, n - bytes_parsed, &ps);
1029 if (mbret == (size_t)-1 || mbret == (size_t)-2)
1030 return -ERRNO_TO_PARA_ERROR(EILSEQ);
1031 bytes_parsed += mbret;
1032 cells_skipped += mutt_wcwidth(wc, cells_skipped);
1034 *bytes_to_skip = bytes_parsed;
1039 * Compute the width of an UTF-8 string.
1041 * \param s The string.
1042 * \param result The width of \a s is returned here.
1044 * If not in UTF8-mode. this function is just a wrapper for strlen(3).
1045 * Otherwise \a s is treated as an UTF-8 string and its display width is
1046 * computed. Note that this function may fail if the underlying call to
1047 * mbsrtowcs(3) fails, so the caller must check the return value.
1049 * \sa nl_langinfo(3), wcswidth(3).
1053 __must_check int strwidth(const char *s, size_t *result)
1055 const char *src = s;
1057 static wchar_t *dest;
1061 * Never call any log function here. This may result in an endless loop
1062 * as para_gui's para_log() calls this function.
1066 *result = strlen(s);
1069 memset(&state, 0, sizeof(state));
1071 num_wchars = mbsrtowcs(NULL, &src, 0, &state);
1072 if (num_wchars == (size_t)-1)
1073 return -ERRNO_TO_PARA_ERROR(errno);
1074 if (num_wchars == 0)
1076 dest = para_malloc(num_wchars * sizeof(*dest));
1078 memset(&state, 0, sizeof(state));
1079 num_wchars = mbsrtowcs(dest, &src, num_wchars, &state);
1080 assert(num_wchars > 0 && num_wchars != (size_t)-1);
1081 *result = mutt_wcswidth(dest, num_wchars);