05d292b2 (kx 2023-03-24 03:51:10 +0300 1)
05d292b2 (kx 2023-03-24 03:51:10 +0300 2) #ifdef HAVE_CONFIG_H
05d292b2 (kx 2023-03-24 03:51:10 +0300 3) #include <config.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 4) #endif
05d292b2 (kx 2023-03-24 03:51:10 +0300 5)
05d292b2 (kx 2023-03-24 03:51:10 +0300 6) #include <stdlib.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 7) #include <stdio.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 8) #include <sys/sysinfo.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 9) #include <sys/types.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 10) #include <stdint.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 11) #include <dirent.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 12) #include <sys/stat.h> /* chmod(2) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 13) #include <sys/file.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 14) #include <sys/mman.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 15) #include <fcntl.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 16) #include <limits.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 17) #include <string.h> /* strdup(3) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 18) #include <libgen.h> /* basename(3) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 19) #include <ctype.h> /* tolower(3) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 20) #include <errno.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 21) #include <time.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 22) #include <sys/time.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 23) #include <pwd.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 24) #include <grp.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 25) #include <stdarg.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 26) #include <unistd.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 27)
05d292b2 (kx 2023-03-24 03:51:10 +0300 28) #include <defs.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 29) #include <fatal.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 30) #include <http.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 31) #include <html.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 32) #include <strbuf.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 33)
05d292b2 (kx 2023-03-24 03:51:10 +0300 34)
05d292b2 (kx 2023-03-24 03:51:10 +0300 35) #define HTML_ERRMSG_SIZE 4096
05d292b2 (kx 2023-03-24 03:51:10 +0300 36)
05d292b2 (kx 2023-03-24 03:51:10 +0300 37) void html_error( const char *fmt, ... )
05d292b2 (kx 2023-03-24 03:51:10 +0300 38) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 39) va_list arg_ptr;
05d292b2 (kx 2023-03-24 03:51:10 +0300 40) char buf[HTML_ERRMSG_SIZE];
05d292b2 (kx 2023-03-24 03:51:10 +0300 41) char msg[HTML_ERRMSG_SIZE];
05d292b2 (kx 2023-03-24 03:51:10 +0300 42) char *format = "%s: %s\n";
05d292b2 (kx 2023-03-24 03:51:10 +0300 43)
05d292b2 (kx 2023-03-24 03:51:10 +0300 44) va_start( arg_ptr, fmt );
05d292b2 (kx 2023-03-24 03:51:10 +0300 45)
05d292b2 (kx 2023-03-24 03:51:10 +0300 46) vsnprintf( msg, HTML_ERRMSG_SIZE, (const void *)fmt, arg_ptr );
05d292b2 (kx 2023-03-24 03:51:10 +0300 47)
05d292b2 (kx 2023-03-24 03:51:10 +0300 48) va_end( arg_ptr ); /* Reset variable arguments. */
05d292b2 (kx 2023-03-24 03:51:10 +0300 49)
05d292b2 (kx 2023-03-24 03:51:10 +0300 50) snprintf( buf, HTML_ERRMSG_SIZE, format, "http", msg );
05d292b2 (kx 2023-03-24 03:51:10 +0300 51)
05d292b2 (kx 2023-03-24 03:51:10 +0300 52) (void)write( STDERR_FILENO, buf, strlen( buf ) );
05d292b2 (kx 2023-03-24 03:51:10 +0300 53)
05d292b2 (kx 2023-03-24 03:51:10 +0300 54) exit( 1 );
05d292b2 (kx 2023-03-24 03:51:10 +0300 55) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 56)
05d292b2 (kx 2023-03-24 03:51:10 +0300 57) html_errfunc html_fatal = html_error;
05d292b2 (kx 2023-03-24 03:51:10 +0300 58)
05d292b2 (kx 2023-03-24 03:51:10 +0300 59)
05d292b2 (kx 2023-03-24 03:51:10 +0300 60) void html_raw( const char *data, size_t size )
05d292b2 (kx 2023-03-24 03:51:10 +0300 61) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 62) if( write( STDOUT_FILENO, data, size ) != size )
05d292b2 (kx 2023-03-24 03:51:10 +0300 63) html_fatal( "write error on html output" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 64) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 65)
05d292b2 (kx 2023-03-24 03:51:10 +0300 66) void html( const char *txt )
05d292b2 (kx 2023-03-24 03:51:10 +0300 67) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 68) html_raw( txt, strlen(txt) );
05d292b2 (kx 2023-03-24 03:51:10 +0300 69) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 70)
05d292b2 (kx 2023-03-24 03:51:10 +0300 71) void htmlf( const char *format, ... )
05d292b2 (kx 2023-03-24 03:51:10 +0300 72) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 73) va_list args;
05d292b2 (kx 2023-03-24 03:51:10 +0300 74) struct strbuf buf = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300 75)
05d292b2 (kx 2023-03-24 03:51:10 +0300 76) va_start( args, format );
05d292b2 (kx 2023-03-24 03:51:10 +0300 77) strbuf_vaddf( &buf, format, args );
05d292b2 (kx 2023-03-24 03:51:10 +0300 78) va_end( args );
05d292b2 (kx 2023-03-24 03:51:10 +0300 79) html( buf.buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 80) strbuf_release( &buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 81) }