tool_msgs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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)
  38. {
  39. size_t width = (79 - strlen(prefix));
  40. DEBUGASSERT(!strchr(fmt, '\n'));
  41. if(!config->silent) {
  42. size_t len;
  43. char *ptr;
  44. char *print_buffer;
  45. print_buffer = curlx_mvaprintf(fmt, ap);
  46. if(!print_buffer)
  47. return;
  48. len = strlen(print_buffer);
  49. ptr = print_buffer;
  50. while(len > 0) {
  51. fputs(prefix, tool_stderr);
  52. if(len > width) {
  53. size_t cut = width-1;
  54. while(!ISBLANK(ptr[cut]) && cut) {
  55. cut--;
  56. }
  57. if(0 == cut)
  58. /* not a single cutting position was found, just cut it at the
  59. max text width then! */
  60. cut = width-1;
  61. (void)fwrite(ptr, cut + 1, 1, tool_stderr);
  62. fputs("\n", tool_stderr);
  63. ptr += cut + 1; /* skip the space too */
  64. len -= cut + 1;
  65. }
  66. else {
  67. fputs(ptr, tool_stderr);
  68. fputs("\n", tool_stderr);
  69. len = 0;
  70. }
  71. }
  72. curl_free(print_buffer);
  73. }
  74. }
  75. /*
  76. * Emit 'note' formatted message on configured 'errors' stream, if verbose was
  77. * selected.
  78. */
  79. void notef(struct GlobalConfig *config, const char *fmt, ...)
  80. {
  81. va_list ap;
  82. va_start(ap, fmt);
  83. if(config->tracetype)
  84. voutf(config, NOTE_PREFIX, fmt, ap);
  85. va_end(ap);
  86. }
  87. /*
  88. * Emit warning formatted message on configured 'errors' stream unless
  89. * mute (--silent) was selected.
  90. */
  91. void warnf(struct GlobalConfig *config, const char *fmt, ...)
  92. {
  93. va_list ap;
  94. va_start(ap, fmt);
  95. voutf(config, WARN_PREFIX, fmt, ap);
  96. va_end(ap);
  97. }
  98. /*
  99. * Emit help formatted message on given stream. This is for errors with or
  100. * related to command line arguments.
  101. */
  102. void helpf(FILE *errors, const char *fmt, ...)
  103. {
  104. if(fmt) {
  105. va_list ap;
  106. va_start(ap, fmt);
  107. DEBUGASSERT(!strchr(fmt, '\n'));
  108. fputs("curl: ", errors); /* prefix it */
  109. vfprintf(errors, fmt, ap);
  110. va_end(ap);
  111. fputs("\n", errors); /* newline it */
  112. }
  113. fprintf(errors, "curl: try 'curl --help' "
  114. #ifdef USE_MANUAL
  115. "or 'curl --manual' "
  116. #endif
  117. "for more information\n");
  118. }
  119. /*
  120. * Emit error message on error stream if not muted. When errors are not tied
  121. * to command line arguments, use helpf() for such errors.
  122. */
  123. void errorf(struct GlobalConfig *config, const char *fmt, ...)
  124. {
  125. if(!config->silent || config->showerror) {
  126. va_list ap;
  127. va_start(ap, fmt);
  128. voutf(config, ERROR_PREFIX, fmt, ap);
  129. va_end(ap);
  130. }
  131. }