--- parser3/src/targets/cgi/parser3.C 2002/06/26 09:00:03 1.186 +++ parser3/src/targets/cgi/parser3.C 2002/08/14 11:37:14 1.194 @@ -3,10 +3,10 @@ Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - - $Id: parser3.C,v 1.186 2002/06/26 09:00:03 paf Exp $ */ +static const char* IDENT_PARSER3_C="$Date: 2002/08/14 11:37:14 $"; + #include "pa_config_includes.h" #if _MSC_VER @@ -29,9 +29,11 @@ # include #endif +#include "fixopt.h" + //#define DEBUG_POOL_MALLOC //#define DEBUG_STRING_APPENDS_VS_EXPANDS -//#define DEBUG_MAILRECEIVE "test2.eml" +//#define DEBUG_MAILRECEIVE "test.eml" #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS extern ulong @@ -43,28 +45,6 @@ extern ulong // consts -#ifndef _PROFILE -extern const char *main_RCSIds[]; -#ifdef USE_SMTP -extern const char *smtp_RCSIds[]; -#endif -extern const char *gd_RCSIds[]; -extern const char *classes_RCSIds[]; -extern const char *types_RCSIds[]; -extern const char *parser3_RCSIds[]; -const char **RCSIds[]={ - main_RCSIds, -#ifdef USE_SMTP - smtp_RCSIds, -#endif - gd_RCSIds, - classes_RCSIds, - types_RCSIds, - parser3_RCSIds, - 0 -}; -#endif - #define REDIRECT_PREFIX "REDIRECT_" #define PARSER_CONFIG_ENV_NAME "CGI_PARSER_CONFIG" @@ -72,6 +52,9 @@ const char **RCSIds[]={ const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M static const char *argv0; +static const char *config_filespec_cstr=0; +static bool fail_on_config_read_problem=true; + static Pool *pool; // global pool [dont describe to doxygen: it confuses it with param names] static bool cgi; ///< we were started as CGI? static bool mail_received=false; ///< we were started with -m option? [asked to parse incoming message to $mail:received] @@ -79,18 +62,26 @@ static bool mail_received=false; ///< we // SAPI static void log(const char *fmt, va_list args) { - bool opened; + bool opened=false; FILE *f=0; - if(argv0) { - // beside by binary + if(config_filespec_cstr) { + char beside_config_path[MAX_STRING]; + strncpy(beside_config_path, config_filespec_cstr, MAX_STRING-1); beside_config_path[MAX_STRING-1]=0; + if(!( + rsplit(beside_config_path, '/') || + rsplit(beside_config_path, '\\'))) { // strip filename + // no path, just filename + beside_config_path[0]='.'; beside_config_path[1]=0; + } + char file_spec[MAX_STRING]; - strncpy(file_spec, argv0, MAX_STRING-1); file_spec[MAX_STRING-1]=0; // filespec of my binary - rsplit(file_spec, '/'); rsplit(file_spec, '\\');// strip filename - strcat(file_spec, "/parser3.log"); + snprintf(file_spec, MAX_STRING, + "%s/parser3.log", beside_config_path); f=fopen(file_spec, "at"); + opened=f!=0; } - opened=f!=0; + // fallback to stderr if(!opened) f=stderr; @@ -325,33 +316,35 @@ static void real_parser_handler( , true /* status_allowed */); - const char *config_filespec_cstr; char config_filespec_buf[MAX_STRING]; - const char *config_by_env=getenv(PARSER_CONFIG_ENV_NAME); - if(!config_by_env) - config_by_env=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME); - if(config_by_env) - config_filespec_cstr=config_by_env; - else { - // beside by binary - // @todo full path, not ./! - char beside_binary_path[MAX_STRING]; - strncpy(beside_binary_path, argv0, MAX_STRING-1); beside_binary_path[MAX_STRING-1]=0; // filespec of my binary - if(!( - rsplit(beside_binary_path, '/') || - rsplit(beside_binary_path, '\\'))) { // strip filename - // no path, just filename - beside_binary_path[0]='.'; beside_binary_path[1]=0; + if(!config_filespec_cstr) { + const char *config_by_env=getenv(PARSER_CONFIG_ENV_NAME); + if(!config_by_env) + config_by_env=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME); + if(config_by_env) + config_filespec_cstr=config_by_env; + else { + // beside by binary + char beside_binary_path[MAX_STRING]; + strncpy(beside_binary_path, argv0, MAX_STRING-1); beside_binary_path[MAX_STRING-1]=0; // filespec of my binary + if(!( + rsplit(beside_binary_path, '/') || + rsplit(beside_binary_path, '\\'))) { // strip filename + // no path, just filename + // @todo full path, not ./! + beside_binary_path[0]='.'; beside_binary_path[1]=0; + } + snprintf(config_filespec_buf, MAX_STRING, + "%s/%s", + beside_binary_path, AUTO_FILE_NAME); + config_filespec_cstr=config_filespec_buf; + fail_on_config_read_problem=false; } - snprintf(config_filespec_buf, MAX_STRING, - "%s/%s", - beside_binary_path, AUTO_FILE_NAME); - config_filespec_cstr=config_filespec_buf; } // process the request request.core( - config_filespec_cstr, false /*fail_on_read_problem*/, + config_filespec_cstr, fail_on_config_read_problem, header_only); // @@ -416,22 +409,29 @@ static void failed_new() { #endif static void usage(const char *program) { - fprintf(stderr, + printf( "Parser/%s Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)\n" "Author: Alexandr Petrosian (http://paf.design.ru)\n" "\n" "Usage: %s [options] file\n" "Options are:\n" #ifdef WITH_MAILRECEIVE - " -m Parse mail, put received letter to $mail:received\n" + " -m Parse mail, put received letter to $mail:received\n" #endif - " -h Display usage information (this message)\n" + " -f config_file Use this config file (/path/to/auto.p)\n" + " -h Display usage information (this message)\n" , PARSER_VERSION, program); exit(EINVAL); } -int main(int argc, char *argv[]) { +int main(int raw_argc, char *raw_argv[]) { + int argc; + char **argv; + fixopt( + raw_argc, raw_argv, + argc, argv); + Pool_storage global_pool_storage; Pool global_pool(&global_pool_storage); pool=&global_pool; @@ -464,7 +464,7 @@ int main(int argc, char *argv[]) { optind = 1; opterr = 0; int c; - while((c = getopt(argc, argv, "h" + while((c = getopt(argc, argv, "hf:" #ifdef WITH_MAILRECEIVE "m" #endif @@ -473,6 +473,9 @@ int main(int argc, char *argv[]) { case 'h': usage(argv[0]); break; + case 'f': + config_filespec_cstr=optarg; + break; #ifdef WITH_MAILRECEIVE case 'm': mail_received=true;