From c2f98432913b19f4704624e4e11c3d840e55834f Mon Sep 17 00:00:00 2001 From: Alexey Khromov Date: Sat, 11 May 2024 19:28:03 +0300 Subject: [PATCH] Earlydbg as new -v option for main prog --- source/bforce/bforce.c | 17 ++++-- source/bforce/conf_read.c | 111 ++++++++++++++------------------------ source/bforce/daemon.c | 6 +-- source/bfutil/bfindex.c | 2 +- source/bfutil/bfstat.c | 2 +- source/bfutil/nlookup.c | 2 +- source/include/bforce.h | 1 + source/include/confread.h | 2 +- source/include/logger.h | 32 +++++------ 9 files changed, 78 insertions(+), 97 deletions(-) diff --git a/source/bforce/bforce.c b/source/bforce/bforce.c index bf711b6..6b16a30 100644 --- a/source/bforce/bforce.c +++ b/source/bforce/bforce.c @@ -102,6 +102,7 @@ static void usage(void) " -S connect string (for slave mode only)\n" " -p override modem port (must be defined in config)\n" " -u binkp|ifcico|telnet protocol to use over TCP/IP\n" + " -v display early debug info on stderr\n" " -h show this help message\n" "\n", conf_getconfname() @@ -257,7 +258,7 @@ int main(int argc, char *argv[], char *envp[]) // parsing - while( (ch=getopt(argc, argv, "hfI:p:n:l:a:u:oiC:S:dq")) != (int)-1 ) + while( (ch=getopt(argc, argv, "hfI:p:n:l:a:u:oivC:S:dq")) != (int)-1 ) { switch( ch ) { case 'h': @@ -308,6 +309,9 @@ int main(int argc, char *argv[], char *envp[]) //if( opts.runmode != MODE_ANSWER || opts.inetd ) { usage(); exit(BFERR_FATALERROR); } opts.inetd = 1; break; + case 'v': + opts.earlydbg = TRUE; + break; case 'C': if( opts.confname || !optarg ) { usage(); exit(BFERR_FATALERROR); } opts.confname = (char *)xstrcpy(optarg); @@ -435,15 +439,16 @@ int main(int argc, char *argv[], char *envp[]) /* Process primary config file */ if( opts.confname && *opts.confname ) - rc = conf_readconf(opts.confname, 0); + rc = conf_readconf(opts.confname, 0, opts.earlydbg); else - rc = conf_readconf(conf_getconfname(), 0); + rc = conf_readconf(conf_getconfname(), 0, opts.earlydbg); if( rc ) gotoexit(BFERR_FATALERROR); /* Process additional config file, ignore errors */ if( opts.incname && *opts.incname ) - (void)conf_readconf(opts.incname, 1); + if (conf_readconf(opts.incname, 1, opts.earlydbg)) + gotoexit(BFERR_FATALERROR); /* Reopen log file if it was defined in config */ if( log_reopen(log_getfilename(LOG_FILE_SESSION), NULL, NULL) ) @@ -459,6 +464,7 @@ int main(int argc, char *argv[], char *envp[]) switch( opts.runmode ) { case MODE_DAEMON: + DEB((D_STATEM, "Daemon mode")); log("Daemon mode"); rc = bforce_daemon(&opts); break; @@ -466,14 +472,17 @@ case MODE_CALL_DEFAULT: case MODE_CALL_IP: case MODE_CALL_MODEM: case MODE_CALL_STDIO: + DEB((D_STATEM, "Outgoing call")); log("Outgoing call"); rc = bforce_master(&opts); break; case MODE_ANSWER: + DEB((D_STATEM, "Start answer")); log("Start answer"); rc = bforce_slave(&opts); break; default: + DEB((D_STATEM, "Could not determine run mode")); log("Could not determine run mode"); } diff --git a/source/bforce/conf_read.c b/source/bforce/conf_read.c index 30ca3c0..0117af6 100644 --- a/source/bforce/conf_read.c +++ b/source/bforce/conf_read.c @@ -129,11 +129,11 @@ static void conf_parsestr(char *str, char **key, char **expr, char **value) return; } -int conf_readconf(const char *confname, int inclevel) +int conf_readconf(const char *confname, int inclevel, bool earlydbg) { - #ifdef DEBUG + if (earlydbg) fprintf(stderr,"BF-DEBUG: Parsing config file: %s %d\n", confname, inclevel); - #endif + FILE *fp = NULL; char tmp[BF_MAXCFGLINE + 1]; int rc, maxrc = 0; @@ -158,10 +158,9 @@ int conf_readconf(const char *confname, int inclevel) { if( i != (int)bforce_config[i].real_key ) { - #ifdef DEBUG + if (earlydbg) fprintf(stderr,"BF-DEBUG: invalid config table: found %d instead of %d\n", bforce_config[i].real_key, i); - #endif return -1; } } @@ -169,14 +168,12 @@ int conf_readconf(const char *confname, int inclevel) if( (fp = file_open(confname,"r")) == NULL ) { - #ifdef DEBUG + if (earlydbg) fprintf(stderr,"BF-DEBUG: can't open config file \"%s\"\n", confname); - #endif return(PROC_RC_IGNORE); } - #ifdef DEBUG + if (earlydbg) fprintf(stderr,"BF-DEBUG: start reading config \"%s\"\n", confname); - #endif while( fgets(tmp, sizeof(tmp), fp) ) { @@ -187,9 +184,9 @@ int conf_readconf(const char *confname, int inclevel) { if( isifexpr ) { - #ifdef DEBUG + if (earlydbg) fprintf(stderr,"BF-DEBUG: warning: automatically close expression at empty line %d\n", line); - #endif + if( ifexpr ) { free(ifexpr); ifexpr = NULL; } isifexpr = FALSE; } @@ -197,9 +194,9 @@ int conf_readconf(const char *confname, int inclevel) if( !isappend ) continue; - #ifdef DEBUG + if (earlydbg) fprintf(stderr,"BF-DEBUG: warning: appending empty or comment line %d\n", line); - #endif + isappend = FALSE; } @@ -242,10 +239,10 @@ int conf_readconf(const char *confname, int inclevel) p_value = NULL; conf_parsestr(fullstr ? fullstr : tmp, &p_key, &p_expr, &p_value); - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: conf_readconf: [%d] key \"%s\" expr \"%s\" value \"%s\"\n", line, p_key, p_expr, p_value); - #endif + if( p_value ) { @@ -269,24 +266,24 @@ int conf_readconf(const char *confname, int inclevel) { if( value == NULL || expr ) { - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: incorrect usage of `%s' directive\n", p_key); - #endif + rc = PROC_RC_IGNORE; } else if( inclevel < MAXINCLUDELEVEL ) { - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: conf_readconf: process inlude file \"%s\"\n", value); - #endif - rc = conf_readconf(value, inclevel + 1); + + rc = conf_readconf(value, inclevel + 1, earlydbg); if( rc ) rc = PROC_RC_IGNORE; } else { - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: conf_readconf: too deep include\n"); - #endif + rc = PROC_RC_IGNORE; } } @@ -294,9 +291,9 @@ int conf_readconf(const char *confname, int inclevel) { if( value || isifexpr ) { - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: incorrect usage of `%s' directive\n", p_key); - #endif + rc = PROC_RC_ABORT; } else @@ -312,9 +309,9 @@ int conf_readconf(const char *confname, int inclevel) { if( value || expr || !isifexpr ) { - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: incorrect usage of `%s' directive\n", p_key); - #endif + rc = PROC_RC_IGNORE; } else @@ -326,9 +323,9 @@ int conf_readconf(const char *confname, int inclevel) else { - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: unknown directive `%s'\n", p_key); - #endif + rc = PROC_RC_IGNORE; } } @@ -336,9 +333,9 @@ int conf_readconf(const char *confname, int inclevel) { if( isifexpr && expr ) { - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: can't use expressions inside $ifexpr block\n"); - #endif + rc = PROC_RC_IGNORE; } else if( isifexpr ) @@ -352,9 +349,9 @@ int conf_readconf(const char *confname, int inclevel) } else { - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: incorrect config string %s\n", fullstr); - #endif + rc = PROC_RC_IGNORE; } @@ -369,19 +366,19 @@ int conf_readconf(const char *confname, int inclevel) case PROC_RC_OK: break; case PROC_RC_WARN: - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: warning in config \"%s\" line %d\n", confname, line); - #endif + break; case PROC_RC_IGNORE: - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: ignore line %d in config \"%s\"\n", line, confname); - #endif + break; case PROC_RC_ABORT: - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: fatal error in config \"%s\" in line %d\n", confname, line); - #endif + break; default: ASSERT_MSG(); @@ -395,9 +392,9 @@ int conf_readconf(const char *confname, int inclevel) if( isifexpr ) { maxrc = 1; - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: unterminated directive `#ifexp'\n"); - #endif + } if( fullstr ) @@ -405,17 +402,17 @@ int conf_readconf(const char *confname, int inclevel) if( ifexpr ) free(ifexpr); - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: readconfig: exit with maxrc = %d\n", maxrc); - #endif + /* update subsystems */ if (inclevel==0) { // end of main config if( log_reopen(log_getfilename(LOG_FILE_SESSION), NULL, NULL) ) { - #ifdef DEBUG + if (earlydbg) fprintf(stderr, "BF-DEBUG: can't continue without logging\n"); - #endif + exit(-1); } #ifdef DEBUG @@ -427,29 +424,3 @@ int conf_readconf(const char *confname, int inclevel) return maxrc; } -#ifdef DEBUG -/*void debug_override(void) -{ - s_cval_entry *ptrl; - char abuf[BF_MAXADDRSTR+1]; - - DEB((D_CONFIG, "debug_override: BEGIN")); - - for( ptrl = bforce_config[cf_override].data; ptrl; ptrl = ptrl->next ) - { - DEB((D_CONFIG, "log_overridelist: address %s", - ftn_addrstr(abuf, ptrl->d.override.addr))); - DEB((D_CONFIG, "log_overridelist: \tIpaddr = \"%s\"", - ptrl->d.override.sIpaddr)); - DEB((D_CONFIG, "log_overridelist: \tPhone = \"%s\"", - ptrl->d.override.sPhone)); - DEB((D_CONFIG, "log_overridelist: \tWorktime = \"%s\"", - timevec_string(abuf, &ptrl->d.override.worktime, sizeof(abuf)))); - DEB((D_CONFIG, "log_overridelist: \tFlags = \"%s\"", - ptrl->d.override.sFlags)); - } - - DEB((D_CONFIG, "debug_override: END")); -}*/ -#endif /* DEBUG */ - diff --git a/source/bforce/daemon.c b/source/bforce/daemon.c index 0b0c5f0..9db1e44 100644 --- a/source/bforce/daemon.c +++ b/source/bforce/daemon.c @@ -938,15 +938,15 @@ int daemon_run(const char *confname, const char *incname, bool quit) /* Read primary config file */ if( confname && *confname ) - rc = conf_readconf(confname, 0); + rc = conf_readconf(confname, 0, false); else - rc = conf_readconf(conf_getconfname(), 0); + rc = conf_readconf(conf_getconfname(), 0, false); if( rc ) return(BFERR_FATALERROR); /* Read additional config file (manual include) */ - if( incname && *incname && conf_readconf(incname, 1) ) + if( incname && *incname && conf_readconf(incname, 1, false) ) log("cannot read additional config (ignore)"); dmstate = DM_Start; diff --git a/source/bfutil/bfindex.c b/source/bfutil/bfindex.c index 2e9e04a..0311397 100644 --- a/source/bfutil/bfindex.c +++ b/source/bfutil/bfindex.c @@ -209,7 +209,7 @@ int main(int argc, char *argv[]) } - if( conf_readconf(conf_getconfname(), 0) ) + if( conf_readconf(conf_getconfname(), 0, false) ) { exit(1); } diff --git a/source/bfutil/bfstat.c b/source/bfutil/bfstat.c index 095d2f7..dc7f1b4 100644 --- a/source/bfutil/bfstat.c +++ b/source/bfutil/bfstat.c @@ -301,7 +301,7 @@ int main(int argc, char *argv[]) /* Initialise current locale */ (void)setlocale(LC_ALL, ""); - if( conf_readconf(conf_getconfname(), 0) ) + if( conf_readconf(conf_getconfname(), 0, false) ) exit(1); memset(&ocb, '\0', sizeof(s_outbound_callback_data)); diff --git a/source/bfutil/nlookup.c b/source/bfutil/nlookup.c index d622d4f..46be17c 100644 --- a/source/bfutil/nlookup.c +++ b/source/bfutil/nlookup.c @@ -159,7 +159,7 @@ int main(int argc, char *argv[]) exit(BFERR_FATALERROR); } - if( conf_readconf(conf_getconfname(), 0) ) + if( conf_readconf(conf_getconfname(), 0, false) ) exit(BFERR_FATALERROR); if( rawstring ) diff --git a/source/include/bforce.h b/source/include/bforce.h index c364436..283be5b 100644 --- a/source/include/bforce.h +++ b/source/include/bforce.h @@ -151,6 +151,7 @@ typedef struct { bool daemon; /* Run as daemon? */ bool quit; /* Quit from daemon */ bool usestdio; /* Session on stdin and stdout */ + bool earlydbg; /* Early debug on stderr */ int inetd; /* Called from inetd? */ int force; /* Force call? */ int hiddline; /* Hidden line number (0,1..) */ diff --git a/source/include/confread.h b/source/include/confread.h index db88b9f..4c5e1c1 100644 --- a/source/include/confread.h +++ b/source/include/confread.h @@ -347,7 +347,7 @@ void deinit_translate(s_translate *dest); const char *conf_getconfname(void); int conf_postreadcheck(void); int conf_readpasswdlist(s_falist **pwdlist, char *fname); -int conf_readconf(const char *confname, int inclevel); +int conf_readconf(const char *confname, int inclevel, bool earlydbg); #ifdef DEBUG void log_overridelist(s_override *subst); void log_options(s_options *opt); diff --git a/source/include/logger.h b/source/include/logger.h index 8c86721..616078b 100644 --- a/source/include/logger.h +++ b/source/include/logger.h @@ -33,22 +33,22 @@ enum { LOG_FILE_DAEMON, LOG_FILE_SESSION, LOG_FILE_DEBUG, LOG_FILE_HISTORY }; #ifdef DEBUG -# define D_CONFIG 0x0000001L -# define D_OVERRIDE 0x0000002L -# define D_EVENT 0x0000004L -# define D_NODELIST 0x0000008L -# define D_OUTBOUND 0x0000010L -# define D_INFO 0x0000020L -# define D_HSHAKE 0x0000040L -# define D_TTYIO 0x0000080L -# define D_MODEM 0x0000100L -# define D_PROT 0x0000200L -# define D_FREQ 0x0000400L -# define D_STATEM 0x0000800L -# define D_DAEMON 0x0001000L -# define D_24554 0x0002000L -# define D_FREE 0x0004000L -# define D_FULL 0xfffffffL +#define D_CONFIG 0x0000001L +#define D_OVERRIDE 0x0000002L +#define D_EVENT 0x0000004L +#define D_NODELIST 0x0000008L +#define D_OUTBOUND 0x0000010L +#define D_INFO 0x0000020L +#define D_HSHAKE 0x0000040L +#define D_TTYIO 0x0000080L +#define D_MODEM 0x0000100L +#define D_PROT 0x0000200L +#define D_FREQ 0x0000400L +#define D_STATEM 0x0000800L +#define D_DAEMON 0x0001000L +#define D_24554 0x0002000L +#define D_FREE 0x0004000L +#define D_FULL 0xfffffffL #endif #ifdef DEBUG