tool_msgs.c 4.0 KB

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