tool_msgs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "tool_setup.h"
  25. #define ENABLE_CURLX_PRINTF
  26. /* use our own printf() functions */
  27. #include "curlx.h"
  28. #include "tool_cfgable.h"
  29. #include "tool_msgs.h"
  30. #include "memdebug.h" /* keep this as LAST include */
  31. #define WARN_PREFIX "Warning: "
  32. #define NOTE_PREFIX "Note: "
  33. #define ERROR_PREFIX "curl: "
  34. static void voutf(struct GlobalConfig *config,
  35. const char *prefix,
  36. const char *fmt,
  37. va_list ap) CURL_PRINTF(3, 0);
  38. static void voutf(struct GlobalConfig *config,
  39. const char *prefix,
  40. const char *fmt,
  41. va_list ap)
  42. {
  43. size_t width = (79 - strlen(prefix));
  44. DEBUGASSERT(!strchr(fmt, '\n'));
  45. if(!config->silent) {
  46. size_t len;
  47. char *ptr;
  48. char *print_buffer;
  49. print_buffer = curlx_mvaprintf(fmt, ap);
  50. if(!print_buffer)
  51. return;
  52. len = strlen(print_buffer);
  53. ptr = print_buffer;
  54. while(len > 0) {
  55. fputs(prefix, tool_stderr);
  56. if(len > width) {
  57. size_t cut = width-1;
  58. while(!ISBLANK(ptr[cut]) && cut) {
  59. cut--;
  60. }
  61. if(0 == cut)
  62. /* not a single cutting position was found, just cut it at the
  63. max text width then! */
  64. cut = width-1;
  65. (void)fwrite(ptr, cut + 1, 1, tool_stderr);
  66. fputs("\n", tool_stderr);
  67. ptr += cut + 1; /* skip the space too */
  68. len -= cut + 1;
  69. }
  70. else {
  71. fputs(ptr, tool_stderr);
  72. fputs("\n", tool_stderr);
  73. len = 0;
  74. }
  75. }
  76. curl_free(print_buffer);
  77. }
  78. }
  79. /*
  80. * Emit 'note' formatted message on configured 'errors' stream, if verbose was
  81. * selected.
  82. */
  83. void notef(struct GlobalConfig *config, const char *fmt, ...)
  84. {
  85. va_list ap;
  86. va_start(ap, fmt);
  87. if(config->tracetype)
  88. voutf(config, NOTE_PREFIX, fmt, ap);
  89. va_end(ap);
  90. }
  91. /*
  92. * Emit warning formatted message on configured 'errors' stream unless
  93. * mute (--silent) was selected.
  94. */
  95. void warnf(struct GlobalConfig *config, const char *fmt, ...)
  96. {
  97. va_list ap;
  98. va_start(ap, fmt);
  99. voutf(config, WARN_PREFIX, fmt, ap);
  100. va_end(ap);
  101. }
  102. /*
  103. * Emit help formatted message on given stream. This is for errors with or
  104. * related to command line arguments.
  105. */
  106. void helpf(FILE *errors, const char *fmt, ...)
  107. {
  108. if(fmt) {
  109. va_list ap;
  110. va_start(ap, fmt);
  111. DEBUGASSERT(!strchr(fmt, '\n'));
  112. fputs("curl: ", errors); /* prefix it */
  113. vfprintf(errors, fmt, ap);
  114. va_end(ap);
  115. fputs("\n", errors); /* newline it */
  116. }
  117. fprintf(errors, "curl: try 'curl --help' "
  118. #ifdef USE_MANUAL
  119. "or 'curl --manual' "
  120. #endif
  121. "for more information\n");
  122. }
  123. /*
  124. * Emit error message on error stream if not muted. When errors are not tied
  125. * to command line arguments, use helpf() for such errors.
  126. */
  127. void errorf(struct GlobalConfig *config, const char *fmt, ...)
  128. {
  129. if(!config->silent || config->showerror) {
  130. va_list ap;
  131. va_start(ap, fmt);
  132. voutf(config, ERROR_PREFIX, fmt, ap);
  133. va_end(ap);
  134. }
  135. }