error.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 _report_error(int type, char *info, char *details, int shouldexit)
  37. {
  38. /* Be sure headers are terminated. */
  39. fputs("\n", stdout);
  40. fprintf(stdout, "<SCRIPT LANGUAGE=\"JavaScript\">");
  41. output_alert(type, info, details, 0);
  42. if(shouldexit) {
  43. fprintf(stdout, "if(history.length>1) history.back();");
  44. }
  45. fprintf(stdout, "</SCRIPT>\n");
  46. if(shouldexit) {
  47. exit(0);
  48. }
  49. }
  50. /*
  51. * Format and output a call to the JavaScript alert() function.
  52. * The caller must ensure a JavaScript context.
  53. */
  54. NSAPI_PUBLIC void output_alert(int type, char *info, char *details, int wait)
  55. {
  56. char *wrapped=NULL;
  57. int err;
  58. if(type >= MAX_ERROR)
  59. type=DEFAULT_ERROR;
  60. wrapped=alert_word_wrap(details, WORD_WRAP_WIDTH, "\\n");
  61. if(!info) info="";
  62. fprintf(stdout, (wait) ? "confirm(\"" : "alert(\"");
  63. fprintf(stdout, "%s:%s\\n%s", error_headers[type], info, wrapped);
  64. if(type==FILE_ERROR || type==SYSTEM_ERROR) {
  65. err = get_error();
  66. if(err != 0){
  67. const char *err_str = verbose_error();
  68. fprintf(stdout,
  69. "\\n\\nThe system returned error number %d, "
  70. "which is %s.", err, err_str);
  71. FREE(err_str);
  72. }
  73. }
  74. fprintf(stdout, "\");");
  75. FREE(wrapped);
  76. }
  77. NSAPI_PUBLIC void report_error(int type, char *info, char *details)
  78. {
  79. _report_error(type, info, details, 1);
  80. }
  81. NSAPI_PUBLIC void report_warning(int type, char *info, char *details)
  82. {
  83. _report_error(type, info, details, 0);
  84. }