vpfmt.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2003 Gunnar Ritter
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute
  10. * it freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. *
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. *
  20. * 3. This notice may not be removed or altered from any source distribution.
  21. */
  22. /* Sccsid @(#)vpfmt.c 1.2 (gritter) 9/21/03 */
  23. #include <stdio.h>
  24. #include <stdarg.h>
  25. #include "pfmt.h"
  26. extern char *pfmt_label__;
  27. /*
  28. * Strip catalog and msgnum from s, but only if they actually appear.
  29. */
  30. static const char *
  31. begin(const char *s, long flags)
  32. {
  33. const char *sp;
  34. if (flags & MM_NOGET)
  35. return s;
  36. sp = s;
  37. if (*sp && *sp != ':') {
  38. sp++;
  39. while (*sp && *sp != '/' && *sp != ':' && sp - s < 14)
  40. sp++;
  41. }
  42. if (*sp++ != ':')
  43. return s;
  44. while (*sp >= '0' && *sp <= '9')
  45. sp++;
  46. if (*sp++ != ':' || *sp == '\0')
  47. return s;
  48. return sp;
  49. }
  50. int
  51. vpfmt(FILE *stream, long flags, const char *fmt, va_list ap)
  52. {
  53. int n = 0;
  54. const char *severity = NULL;
  55. char sevbuf[25];
  56. if ((flags&MM_NOSTD) == 0) {
  57. if (flags & MM_ACTION)
  58. severity = "TO FIX";
  59. else switch (flags & 0377) {
  60. case MM_HALT:
  61. severity = "HALT";
  62. break;
  63. case MM_WARNING:
  64. severity = "WARNING";
  65. break;
  66. case MM_INFO:
  67. severity = "INFO";
  68. break;
  69. case MM_ERROR:
  70. severity = "ERROR";
  71. break;
  72. default:
  73. snprintf(sevbuf, sizeof sevbuf, "SEV=%ld", flags&0377);
  74. severity = sevbuf;
  75. }
  76. if (pfmt_label__)
  77. n = fprintf(stream, "%s: ", pfmt_label__);
  78. if (severity)
  79. n += fprintf(stream, "%s: ", severity);
  80. }
  81. n += vfprintf(stream, begin(fmt, flags), ap);
  82. return n;
  83. }