error.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. /*
  13. * error.c - Handle error recovery
  14. *
  15. * All blame to Mike McCool
  16. */
  17. #include "libadmin/libadmin.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <base/file.h>
  22. #define ERROR_HTML "error.html"
  23. /* Be sure to edit libadmin.h and add new #define types for these headers. */
  24. char *error_headers[MAX_ERROR] =
  25. {"File System Error",
  26. "Memory Error",
  27. "System Error",
  28. "Incorrect Usage",
  29. "Form Element Missing",
  30. "Registry Database Error",
  31. "Network Error",
  32. "Unexpected Failure",
  33. "Warning"};
  34. #define get_error() errno
  35. #define verbose_error() system_errmsg()
  36. void
  37. _report_error(int type, char *info, char *details, int shouldexit)
  38. {
  39. /* Be sure headers are terminated. */
  40. fputs("\n", stdout);
  41. fprintf(stdout, "<SCRIPT LANGUAGE=\"JavaScript\">");
  42. output_alert(type, info, details, 0);
  43. if (shouldexit) {
  44. fprintf(stdout, "if(history.length>1) history.back();");
  45. }
  46. fprintf(stdout, "</SCRIPT>\n");
  47. if (shouldexit) {
  48. exit(0);
  49. }
  50. }
  51. /*
  52. * Format and output a call to the JavaScript alert() function.
  53. * The caller must ensure a JavaScript context.
  54. */
  55. NSAPI_PUBLIC void
  56. output_alert(int type, char *info, char *details, int wait)
  57. {
  58. char *wrapped = NULL;
  59. int err;
  60. if (type >= MAX_ERROR)
  61. type = DEFAULT_ERROR;
  62. wrapped = alert_word_wrap(details, WORD_WRAP_WIDTH, "\\n");
  63. if (!info)
  64. info = "";
  65. fprintf(stdout, (wait) ? "confirm(\"" : "alert(\"");
  66. fprintf(stdout, "%s:%s\\n%s", error_headers[type], info, wrapped);
  67. if (type == FILE_ERROR || type == SYSTEM_ERROR) {
  68. err = get_error();
  69. if (err != 0) {
  70. const char *err_str = verbose_error();
  71. fprintf(stdout,
  72. "\\n\\nThe system returned error number %d, "
  73. "which is %s.",
  74. err, err_str);
  75. FREE(err_str);
  76. }
  77. }
  78. fprintf(stdout, "\");");
  79. FREE(wrapped);
  80. }
  81. NSAPI_PUBLIC void
  82. report_error(int type, char *info, char *details)
  83. {
  84. _report_error(type, info, details, 1);
  85. }
  86. NSAPI_PUBLIC void
  87. report_warning(int type, char *info, char *details)
  88. {
  89. _report_error(type, info, details, 0);
  90. }