1
0

error.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * error.c - Handle error recovery
  43. *
  44. * All blame to Mike McCool
  45. */
  46. #include "libadmin/libadmin.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #ifdef XP_WIN32
  51. #include <windows.h>
  52. #include "base/nterr.h"
  53. #endif
  54. #include <base/file.h>
  55. #define ERROR_HTML "error.html"
  56. /* Be sure to edit libadmin.h and add new #define types for these headers. */
  57. char *error_headers[MAX_ERROR] =
  58. {"File System Error",
  59. "Memory Error",
  60. "System Error",
  61. "Incorrect Usage",
  62. "Form Element Missing",
  63. "Registry Database Error",
  64. "Network Error",
  65. "Unexpected Failure",
  66. "Warning"};
  67. #ifdef XP_UNIX
  68. #define get_error() errno
  69. #define verbose_error() system_errmsg()
  70. #else /* XP_WIN32 */
  71. int get_error()
  72. {
  73. int error = GetLastError();
  74. return(error ? error: WSAGetLastError());
  75. }
  76. char *verbose_error()
  77. {
  78. /* Initialize error hash tables */
  79. HashNtErrors();
  80. return alert_word_wrap(system_errmsg(), WORD_WRAP_WIDTH, "\\n");
  81. }
  82. #endif /* XP_WIN32 */
  83. void _report_error(int type, char *info, char *details, int shouldexit)
  84. {
  85. /* Be sure headers are terminated. */
  86. fputs("\n", stdout);
  87. fprintf(stdout, "<SCRIPT LANGUAGE=\"JavaScript\">");
  88. output_alert(type, info, details, 0);
  89. if(shouldexit) {
  90. fprintf(stdout, "if(history.length>1) history.back();");
  91. }
  92. fprintf(stdout, "</SCRIPT>\n");
  93. if(shouldexit) {
  94. #ifdef XP_WIN32
  95. WSACleanup();
  96. #endif
  97. exit(0);
  98. }
  99. }
  100. /*
  101. * Format and output a call to the JavaScript alert() function.
  102. * The caller must ensure a JavaScript context.
  103. */
  104. NSAPI_PUBLIC void output_alert(int type, char *info, char *details, int wait)
  105. {
  106. char *wrapped=NULL;
  107. int err;
  108. if(type >= MAX_ERROR)
  109. type=DEFAULT_ERROR;
  110. wrapped=alert_word_wrap(details, WORD_WRAP_WIDTH, "\\n");
  111. if(!info) info="";
  112. fprintf(stdout, (wait) ? "confirm(\"" : "alert(\"");
  113. fprintf(stdout, "%s:%s\\n%s", error_headers[type], info, wrapped);
  114. if(type==FILE_ERROR || type==SYSTEM_ERROR) {
  115. err = get_error();
  116. if(err != 0)
  117. fprintf(stdout,
  118. "\\n\\nThe system returned error number %d, "
  119. "which is %s.", err, verbose_error());
  120. }
  121. fprintf(stdout, "\");");
  122. FREE(wrapped);
  123. }
  124. NSAPI_PUBLIC void report_error(int type, char *info, char *details)
  125. {
  126. _report_error(type, info, details, 1);
  127. }
  128. NSAPI_PUBLIC void report_warning(int type, char *info, char *details)
  129. {
  130. _report_error(type, info, details, 0);
  131. }