|
|
| version 1.362, 2024/12/02 02:50:40 | version 1.366, 2024/12/11 18:35:31 |
|---|---|
| Line 52 SAPI_Info_CGI sapi_cgi; | Line 52 SAPI_Info_CGI sapi_cgi; |
| static SAPI_Info *sapi_info = &sapi_cgi; | static SAPI_Info *sapi_info = &sapi_cgi; |
| static THREAD_LOCAL SAPI_Info *sapi_info_4log = NULL; // global for correct send error in die() | static THREAD_LOCAL SAPI_Info *sapi_info_4log = NULL; // global for correct send error in die() |
| const char* parser3_mode = "cgi"; // $status:mode | |
| static const char* filespec_to_process = 0; // [file] | static const char* filespec_to_process = 0; // [file] |
| static const char* httpd_host_port = 0; // -p option | static const char* httpd_host_port = 0; // -p option |
| static const char* config_filespec = 0; // -f option or from env or next to the executable if exists | static const char* config_filespec = 0; // -f option or from env or next to the executable if exists |
| Line 73 template <typename T> static T *dir_pos( | Line 74 template <typename T> static T *dir_pos( |
| return result; | return result; |
| } | } |
| // SAPI | const char *parser3_log_filespec() { // $status:log-filename |
| const char* slog=log_filespec; | |
| static void pa_log(const char* fmt, va_list args) { | |
| FILE *f=0; | |
| if(log_filespec) | |
| f=fopen(log_filespec, "at"); | |
| if(!f) { | if(!slog) |
| const char* log_by_env=getenv(PARSER_LOG_ENV_NAME); | slog=getenv(PARSER_LOG_ENV_NAME); |
| if(!log_by_env) | if(!slog) |
| log_by_env=getenv(REDIRECT_PREFIX PARSER_LOG_ENV_NAME); | slog=getenv(REDIRECT_PREFIX PARSER_LOG_ENV_NAME); |
| if(log_by_env) | if(!slog) { |
| f=fopen(log_by_env, "at"); | static char log_spec[MAX_STRING + 12 /* '/parser3.log' */]; |
| } | |
| if(!f && filespec_4log) { | |
| char log_spec[MAX_STRING + 12 /* '/parser3.log' */]; | |
| pa_strncpy(log_spec, filespec_4log, MAX_STRING); | pa_strncpy(log_spec, filespec_4log, MAX_STRING); |
| if(char* log_dir_pos=dir_pos(log_spec)){ | if(char* log_dir_pos=dir_pos(log_spec)){ |
| Line 99 static void pa_log(const char* fmt, va_l | Line 91 static void pa_log(const char* fmt, va_l |
| // no path, just filename | // no path, just filename |
| strcpy(log_spec, "./parser3.log"); | strcpy(log_spec, "./parser3.log"); |
| } | } |
| slog=log_spec; | |
| f=fopen(log_spec, "at"); | |
| } | } |
| return slog; | |
| } | |
| // SAPI | |
| static void pa_log(const char* fmt, va_list args) { | |
| // use no memory [so that we could log out-of-memory error] | |
| const char* slog=parser3_log_filespec(); | |
| FILE *f=strcmp(slog,"-") ? fopen(slog, "at") : stderr; | |
| // fallback to stderr | // fallback to stderr |
| if(!f) | if(!f) |
| f=stderr; | f=stderr; |
| // use no memory [so that we could log out-of-memory error] | |
| setbuf(f, 0); // stderr stream is unbuffered by default, but still... | setbuf(f, 0); // stderr stream is unbuffered by default, but still... |
| // prefix | // prefix |
| Line 115 static void pa_log(const char* fmt, va_l | Line 115 static void pa_log(const char* fmt, va_l |
| if(size_t len=strlen(stamp)) // saw once stamp being ="" | if(size_t len=strlen(stamp)) // saw once stamp being ="" |
| fprintf(f, "[%.*s] [%u] ", (int)len-1, stamp, (unsigned int)pa_get_thread_id() ); | fprintf(f, "[%.*s] [%u] ", (int)len-1, stamp, (unsigned int)pa_get_thread_id() ); |
| } | } |
| // message | |
| // message | |
| char buf[MAX_LOG_STRING]; | char buf[MAX_LOG_STRING]; |
| size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args); | size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args); |
| size=remove_crlf(buf, buf+size); | size=remove_crlf(buf, buf+size); |
| Line 608 static void call_real_parser_handler__su | Line 608 static void call_real_parser_handler__su |
| #define REAL_PARSER_HANDLER real_parser_handler | #define REAL_PARSER_HANDLER real_parser_handler |
| #endif | #endif |
| static void usage(const char* program) { | static void usage(const char* message=NULL) { |
| if(message){ | |
| fprintf(stderr, message, parser3_filespec); | |
| } | |
| printf( | printf( |
| "Parser/%s\n" | "Parser/%s\n" |
| "Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)\n" | "Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)\n" |
| Line 620 static void usage(const char* program) { | Line 624 static void usage(const char* program) { |
| " -m Parse mail, put received letter to $mail:received\n" | " -m Parse mail, put received letter to $mail:received\n" |
| #endif | #endif |
| " -f config_file Use this config file (/path/to/auto.p)\n" | " -f config_file Use this config file (/path/to/auto.p)\n" |
| " -l log_file Use this log file (/path/to/parser3.log)\n" | |
| " -p [host:]port Start web server on this port\n" | " -p [host:]port Start web server on this port\n" |
| " -h Display usage information (this message)\n", | " -h Display usage information (this message)\n", |
| PARSER_VERSION, | PARSER_VERSION, |
| program); | parser3_filespec ? parser3_filespec : "parser3" ); |
| exit(EINVAL); | exit(EINVAL); |
| } | } |
| #define ARG_REQUIRED \ | |
| if(c[1] || !*(++carg)){ \ | |
| fprintf(stderr, "%s: option '%c' requires an argument\n", parser3_filespec, *c); \ | |
| usage(); \ | |
| } | |
| int main(int argc, char *argv[]) { | int main(int argc, char *argv[]) { |
| #ifdef PA_DEBUG_CGI_ENTRY_EXIT | #ifdef PA_DEBUG_CGI_ENTRY_EXIT |
| pa_log("main: entry"); | pa_log("main: entry"); |
| #endif | #endif |
| parser3_filespec = argc && argv[0] ? MAYBE_BACK_SLASHES_TO_SLASHES(argv[0]) : "parser3"; | if(!argc || !argv[0]) |
| usage(); | |
| parser3_filespec = MAYBE_BACK_SLASHES_TO_SLASHES(argv[0]); | |
| umask(2); | umask(2); |
| // were we started as CGI? | // were we started as CGI? |
| bool cgi=(getenv("SERVER_SOFTWARE") || getenv("SERVER_NAME") || getenv("GATEWAY_INTERFACE") || getenv("REQUEST_METHOD")) && !getenv("PARSER_VERSION"); | bool cgi=(getenv("SERVER_SOFTWARE") || getenv("SERVER_NAME") || getenv("GATEWAY_INTERFACE") || getenv("REQUEST_METHOD")) && !getenv("PARSER_VERSION"); |
| if(!cgi) | if(!cgi){ |
| sapi_info = &sapi_console; | sapi_info = &sapi_console; |
| parser3_mode = "console"; | |
| } | |
| #ifdef SIGPIPE | #ifdef SIGPIPE |
| signal(SIGPIPE, SIGPIPE_handler); | signal(SIGPIPE, SIGPIPE_handler); |
| #endif | #endif |
| char* raw_filespec_to_process = NULL; | char* raw_filespec_to_process = NULL; |
| if(cgi) { | if(cgi) { |
| raw_filespec_to_process=getenv("PATH_TRANSLATED"); | raw_filespec_to_process=getenv("PATH_TRANSLATED"); |
| argv_extra=argv + 1; | argv_extra=argv + 1; |
| } else { | } else { |
| int optind=1; | char** carg = argv + 1; |
| while(optind < argc){ | for(;*carg; carg++){ |
| char* carg = argv[optind]; | if((*carg)[0] != '-') |
| if(carg[0] != '-') | |
| break; | break; |
| for(size_t k = 1; k < strlen(carg); k++){ | for(char* c=(*carg)+1; *c; c++){ |
| char c = carg[k]; | switch (*c) { |
| switch (c) { | |
| case 'h': | case 'h': |
| usage(parser3_filespec); | usage(); |
| break; | break; |
| case 'f': | case 'f': |
| if(optind < argc - 1){ | ARG_REQUIRED; |
| optind++; | config_filespec=*carg; |
| config_filespec=argv[optind]; | |
| } | |
| break; | break; |
| case 'l': | case 'l': |
| if(optind < argc - 1){ | ARG_REQUIRED; |
| optind++; | log_filespec=*carg; |
| log_filespec=argv[optind]; | |
| } | |
| break; | break; |
| case 'p': | case 'p': |
| if(optind < argc - 1){ | ARG_REQUIRED; |
| optind++; | httpd_host_port=*carg; |
| httpd_host_port=argv[optind]; | parser3_mode="httpd"; |
| } | |
| break; | break; |
| #ifdef WITH_MAILRECEIVE | #ifdef WITH_MAILRECEIVE |
| case 'm': | case 'm': |
| mail_received=true; | mail_received=true; |
| parser3_mode="mail"; | |
| break; | break; |
| #endif | #endif |
| default: | default: |
| fprintf(stderr, "%s: invalid option '%c'\n", parser3_filespec, c); | fprintf(stderr, "%s: invalid option '%c'\n", parser3_filespec, *c); |
| usage(parser3_filespec); | usage(); |
| break; | break; |
| } | } |
| } | } |
| optind++; | |
| } | } |
| if (optind > argc - 1) { | if (*carg) { |
| if(!httpd_host_port) { | raw_filespec_to_process=*carg; |
| fprintf(stderr, "%s: file not specified\n", parser3_filespec); | |
| usage(parser3_filespec); | |
| } | |
| } else { | } else { |
| raw_filespec_to_process=argv[optind]; | if(!httpd_host_port) |
| usage("%s: file not specified\n"); | |
| } | } |
| if (httpd_host_port && mail_received) { | if (httpd_host_port && mail_received) |
| fprintf(stderr, "%s: -p and -m options should not be used together\n", parser3_filespec); | usage("%s: -p and -m options should not be used together\n"); |
| usage(parser3_filespec); | |
| } | |
| argv_extra=argv + optind; | argv_extra=carg; |
| } | } |
| #ifdef _MSC_VER | #ifdef _MSC_VER |