00001
00027
00028
00029
00030
00031
00032
00033
00034 #include <sent/stddefs.h>
00035 #include <sent/tcpip.h>
00036 #include <stdarg.h>
00037
00038
00039 static void (*j_print_func)(char *) = NULL;
00040 static void (*j_printerr_func)(char *) = NULL;
00041 static void (*j_flush_func)(void) = NULL;
00042
00043 #define MAX_PRINTF_LEN 4096
00044 static char inbuf[MAX_PRINTF_LEN];
00045 #ifdef CHARACTER_CONVERSION
00046 static char outbuf[MAX_PRINTF_LEN];
00047 #endif
00048
00056 void
00057 set_print_func(void (*print)(char *), void (*printerr)(char *), void (*flush)(void))
00058 {
00059 j_print_func = print;
00060 j_printerr_func = printerr;
00061 j_flush_func = flush;
00062 return;
00063 }
00064
00075 int
00076 j_printf(char *fmt, ...)
00077 {
00078 va_list ap;
00079 int ret;
00080 char *buf;
00081
00082 va_start(ap,fmt);
00083 ret = vsnprintf(inbuf, MAX_PRINTF_LEN, fmt, ap);
00084 va_end(ap);
00085 #ifdef CHARACTER_CONVERSION
00086 buf = charconv(inbuf, outbuf, MAX_PRINTF_LEN);
00087 #else
00088 buf = inbuf;
00089 #endif
00090 if (j_print_func != NULL) {
00091 (*j_print_func)(buf);
00092 } else {
00093 ret = printf("%s", buf);
00094 }
00095
00096 return(ret);
00097 }
00098
00109 int
00110 j_printerr(char *fmt, ...)
00111 {
00112 va_list ap;
00113 int ret;
00114 char *buf;
00115
00116 va_start(ap,fmt);
00117 ret = vsnprintf(inbuf, MAX_PRINTF_LEN, fmt, ap);
00118 va_end(ap);
00119 #ifdef CHARACTER_CONVERSION
00120 buf = charconv(inbuf, outbuf, MAX_PRINTF_LEN);
00121 #else
00122 buf = inbuf;
00123 #endif
00124 if (j_printerr_func != NULL) {
00125 (*j_printerr_func)(buf);
00126 } else {
00127 ret = fprintf(stderr, "%s", buf);
00128 }
00129
00130 return(ret);
00131 }
00132
00140 int
00141 j_flushprint()
00142 {
00143 if (j_flush_func != NULL) {
00144 (*j_flush_func)();
00145 return 0;
00146 } else {
00147 return(fflush(stdout));
00148 }
00149 return(fflush(stdout));
00150 }
00151
00161 int
00162 module_send(int sd, char *fmt, ...)
00163 {
00164 va_list ap;
00165 int ret;
00166 char *buf;
00167
00168 va_start(ap,fmt);
00169 ret = vsnprintf(inbuf, MAX_PRINTF_LEN, fmt, ap);
00170 va_end(ap);
00171 if (ret > 0) {
00172
00173 #ifdef CHARACTER_CONVERSION
00174 buf = charconv(inbuf, outbuf, MAX_PRINTF_LEN);
00175 #else
00176 buf = inbuf;
00177 #endif
00178 if (
00179 #ifdef WINSOCK
00180 send(sd, buf, strlen(buf), 0)
00181 #else
00182 write(sd, buf, strlen(buf))
00183 #endif
00184 < 0) {
00185 perror("Error: module_send:");
00186 }
00187 }
00188 return(ret);
00189 }
00190
00191 static void (*error_func)() = NULL;
00192
00199 void
00200 j_error_register_exitfunc(void (*f)())
00201 {
00202 error_func = f;
00203 }
00204
00211 void
00212 j_error(char *fmt, ...)
00213 {
00214 va_list ap;
00215 int ret;
00216 va_start(ap,fmt);
00217 ret = vfprintf(stderr, fmt, ap);
00218 va_end(ap);
00219
00220 cleanup_socket();
00221 if (error_func != NULL) (*error_func)();
00222 else exit(1);
00223 return;
00224 }
00225
00226
00227 static void (*exit_func)() = NULL;
00228
00234 void
00235 j_exit_register_exitfunc(void (*f)())
00236 {
00237 exit_func = f;
00238 }
00239
00244 void
00245 j_exit()
00246 {
00247
00248 cleanup_socket();
00249 if (exit_func != NULL) (*exit_func)();
00250 else exit(0);
00251 }