tool_msgs.c 3.6 KB

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