tool_msgs.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 http://curl.haxx.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 WARN_TEXTWIDTH (79 - (int)strlen(WARN_PREFIX))
  31. /*
  32. * Emit warning formatted message on configured 'errors' stream unless
  33. * mute (--silent) was selected.
  34. */
  35. void warnf(struct Configurable *config, const char *fmt, ...)
  36. {
  37. if(!config->mute) {
  38. va_list ap;
  39. int len;
  40. char *ptr;
  41. char print_buffer[256];
  42. va_start(ap, fmt);
  43. len = vsnprintf(print_buffer, sizeof(print_buffer), fmt, ap);
  44. va_end(ap);
  45. ptr = print_buffer;
  46. while(len > 0) {
  47. fputs(WARN_PREFIX, config->errors);
  48. if(len > (int)WARN_TEXTWIDTH) {
  49. int cut = WARN_TEXTWIDTH-1;
  50. while(!ISSPACE(ptr[cut]) && cut) {
  51. cut--;
  52. }
  53. if(0 == cut)
  54. /* not a single cutting position was found, just cut it at the
  55. max text width then! */
  56. cut = WARN_TEXTWIDTH-1;
  57. (void)fwrite(ptr, cut + 1, 1, config->errors);
  58. fputs("\n", config->errors);
  59. ptr += cut+1; /* skip the space too */
  60. len -= cut;
  61. }
  62. else {
  63. fputs(ptr, config->errors);
  64. len = 0;
  65. }
  66. }
  67. }
  68. }
  69. /*
  70. * Emit help formatted message on given stream.
  71. */
  72. void helpf(FILE *errors, const char *fmt, ...)
  73. {
  74. va_list ap;
  75. if(fmt) {
  76. va_start(ap, fmt);
  77. fputs("curl: ", errors); /* prefix it */
  78. vfprintf(errors, fmt, ap);
  79. va_end(ap);
  80. }
  81. fprintf(errors, "curl: try 'curl --help' "
  82. #ifdef USE_MANUAL
  83. "or 'curl --manual' "
  84. #endif
  85. "for more information\n");
  86. }