tool_msgs.c 3.6 KB

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