wmail: added command-line option to specify a different config-file.

This commit is contained in:
Jeremy Sowden 2019-06-12 21:37:21 +01:00 committed by Carlos R. Mafra
parent 7590c32f82
commit 6e48b4a431
4 changed files with 219 additions and 195 deletions

View file

@ -132,6 +132,11 @@ Description:
rc-file statement: Colors.NonShapedFrame = "<string>" rc-file statement: Colors.NonShapedFrame = "<string>"
default value : <unset, this color is translucent> default value : <unset, this color is translucent>
item : rc-file location
cmd-line option : -rc <path>
rc-file statement: <none>
default value : "~/.wmailrc"
libdockapp provides the following additional cmd-line options: libdockapp provides the following additional cmd-line options:
windowed-mode : -w windowed-mode : -w
@ -206,8 +211,7 @@ Install:
your X11 base library directory or let $LD_LIBRARY_PATH point to the lib). your X11 base library directory or let $LD_LIBRARY_PATH point to the lib).
If you would like to use rc-based configuration, look into the provided If you would like to use rc-based configuration, look into the provided
sample-file "wmailrc-sample" and use it as a base for your particular needs sample-file "wmailrc-sample" and use it as a base for your particular needs
by copying it to ~/.wmailrc. Note: the name of this rc-file is hard-wired and by copying it to ~/.wmailrc.
cannot be customized.
Run: Run:

View file

@ -234,195 +234,179 @@ static void PostProcessConfiguration( void )
} }
} }
void ReadConfigFile( bool resetConfigStrings ) void ReadConfigFile( const char *configFile, bool resetConfigStrings )
{ {
char *usersHome;
// free all config strings and reset their pointers if required // free all config strings and reset their pointers if required
if( resetConfigStrings ) if( resetConfigStrings )
ResetConfigStrings(); ResetConfigStrings();
if(( usersHome = getenv( "HOME" )) != NULL ) FILE *f = fopen( configFile, "r" );
if( f != NULL )
{ {
char *fileName; char buf[1024];
if(( fileName = MakePathName( usersHome, WMAIL_RC_FILE )) != NULL ) int line = 1;
for( ; !feof( f ); ++line )
{ {
FILE *f = fopen( fileName, "rt" ); const char *id, *value;
size_t len;
if( f != NULL ) if( fgets( buf, sizeof buf, f ) == NULL )
break;
// first eliminate the trailing whitespaces
for( len = strlen( buf );
len > 0 && IsWhiteSpace(buf+(--len)); )
*(buf+len) = '\0';
if( !Tokenize( buf, &id, &value ))
continue;
if( PREFIX_MATCHES( id, "Window.Display", false ))
{ {
char buf[1024]; if( !( config.givenOptions & CL_DISPLAY ))
int line = 1; ReadString( value, line, &config.display );
continue;
for( ; !feof( f ); ++line )
{
const char *id, *value;
size_t len;
if( fgets( buf, sizeof buf, f ) == NULL )
break;
// first eliminate the trailing whitespaces
for( len = strlen( buf );
len > 0 && IsWhiteSpace(buf+(--len)); )
*(buf+len) = '\0';
if( !Tokenize( buf, &id, &value ))
continue;
if( PREFIX_MATCHES( id, "Window.Display", false ))
{
if( !( config.givenOptions & CL_DISPLAY ))
ReadString( value, line, &config.display );
continue;
}
if( PREFIX_MATCHES( id, "Window.NonShaped", false ))
{
if( !( config.givenOptions & CL_NOSHAPE ))
ReadBool( value, line, &config.noshape );
continue;
}
if( PREFIX_MATCHES( id, "Window.Button.Command", false ))
{
if( !( config.givenOptions & CL_RUNCMD ))
ReadString( value, line, &config.runCmd );
continue;
}
if( PREFIX_MATCHES( id, "Mail.MailBox", false ))
{
if( !( config.givenOptions & CL_MAILBOX ))
ReadString( value, line, &config.mailBox );
continue;
}
if( PREFIX_MATCHES( id, "Mail.ChecksumFile", false ))
{
/*
* No corresponding command-line option.
*/
ReadString( value, line, &config.checksumFileName );
continue;
}
if( PREFIX_MATCHES( id, "Mail.CheckIntervall", false ))
{
if( !( config.givenOptions & CL_CHECKINTERVAL ))
ReadInt( value, line, &config.checkInterval );
continue;
}
if( PREFIX_MATCHES( id, "Mail.ShowOnlyNew", false ))
{
if( !( config.givenOptions & CL_NEWMAILONLY ))
ReadBool( value, line, &config.newMailsOnly );
continue;
}
if( PREFIX_MATCHES( id, "Ticker.Mode", false ))
{
if( !( config.givenOptions & CL_TICKERMODE ))
ReadEnum( value, line, (int *)&config.tickerMode, tickerEnum );
continue;
}
if( PREFIX_MATCHES( id, "Ticker.Frames", false ))
{
if( !( config.givenOptions & CL_FPS ))
ReadInt( value, line, &config.fps );
continue;
}
if( PREFIX_MATCHES( id, "Colors.Symbols", false ))
{
if( !( config.givenOptions & CL_SYMBOLCOLOR ))
ReadString( value, line, &config.symbolColor );
continue;
}
if( PREFIX_MATCHES( id, "Colors.Font", false ))
{
if( !( config.givenOptions & CL_FONTCOLOR ))
ReadString( value, line, &config.fontColor );
continue;
}
if( PREFIX_MATCHES( id, "Colors.Backlight", false ))
{
if( !( config.givenOptions & CL_BACKCOLOR ))
ReadString( value, line, &config.backColor );
continue;
}
if( PREFIX_MATCHES( id, "Colors.OffLight", false ))
{
if( !( config.givenOptions & CL_OFFLIGHTCOLOR ))
ReadString( value, line, &config.offlightColor );
continue;
}
if( PREFIX_MATCHES( id, "Colors.NonShapedFrame", false ))
{
if( !( config.givenOptions & CL_NOSHAPE ))
ReadString( value, line, &config.backgroundColor );
continue;
}
if( PREFIX_MATCHES( id, "Ticker.X11Font", false ))
{
if( !( config.givenOptions & CL_USEX11FONT ))
ReadString( value, line, &config.useX11Font );
continue;
}
if( PREFIX_MATCHES( id, "Mail.SkipSender", false ))
{
/*
* No corresponding command-line option.
*/
char *skip;
if( ReadString( value, line, &skip ))
AddSenderToSkipList( skip );
continue;
}
if( PREFIX_MATCHES( id, "Mail.OnNew.Command", false ))
{
if( !( config.givenOptions & CL_CMDONMAIL ))
ReadString( value, line, &config.cmdOnMail );
continue;
}
if( PREFIX_MATCHES( id, "Mail.UseStatusField", false ))
{
if( !( config.givenOptions & CL_CONSIDERSTATUSFIELD ))
ReadBool( value, line, &config.considerStatusField );
continue;
}
if( PREFIX_MATCHES( id, "Mail.ReadStatus", false ))
{
if( !( config.givenOptions & CL_READSTATUS ))
ReadString( value, line, &config.readStatus );
continue;
}
WARNING( "cfg-file(%i): unrecognized: \"%s\"\n", line, buf );
}
fclose( f );
} else {
TRACE( "unable to open config-file \"%s\"\n", fileName );
} }
free( fileName ); if( PREFIX_MATCHES( id, "Window.NonShaped", false ))
} else { {
TRACE( "unable to allocate config-file\n" ); if( !( config.givenOptions & CL_NOSHAPE ))
ReadBool( value, line, &config.noshape );
continue;
}
if( PREFIX_MATCHES( id, "Window.Button.Command", false ))
{
if( !( config.givenOptions & CL_RUNCMD ))
ReadString( value, line, &config.runCmd );
continue;
}
if( PREFIX_MATCHES( id, "Mail.MailBox", false ))
{
if( !( config.givenOptions & CL_MAILBOX ))
ReadString( value, line, &config.mailBox );
continue;
}
if( PREFIX_MATCHES( id, "Mail.ChecksumFile", false ))
{
/*
* No corresponding command-line option.
*/
ReadString( value, line, &config.checksumFileName );
continue;
}
if( PREFIX_MATCHES( id, "Mail.CheckIntervall", false ))
{
if( !( config.givenOptions & CL_CHECKINTERVAL ))
ReadInt( value, line, &config.checkInterval );
continue;
}
if( PREFIX_MATCHES( id, "Mail.ShowOnlyNew", false ))
{
if( !( config.givenOptions & CL_NEWMAILONLY ))
ReadBool( value, line, &config.newMailsOnly );
continue;
}
if( PREFIX_MATCHES( id, "Ticker.Mode", false ))
{
if( !( config.givenOptions & CL_TICKERMODE ))
ReadEnum( value, line, (int *)&config.tickerMode, tickerEnum );
continue;
}
if( PREFIX_MATCHES( id, "Ticker.Frames", false ))
{
if( !( config.givenOptions & CL_FPS ))
ReadInt( value, line, &config.fps );
continue;
}
if( PREFIX_MATCHES( id, "Colors.Symbols", false ))
{
if( !( config.givenOptions & CL_SYMBOLCOLOR ))
ReadString( value, line, &config.symbolColor );
continue;
}
if( PREFIX_MATCHES( id, "Colors.Font", false ))
{
if( !( config.givenOptions & CL_FONTCOLOR ))
ReadString( value, line, &config.fontColor );
continue;
}
if( PREFIX_MATCHES( id, "Colors.Backlight", false ))
{
if( !( config.givenOptions & CL_BACKCOLOR ))
ReadString( value, line, &config.backColor );
continue;
}
if( PREFIX_MATCHES( id, "Colors.OffLight", false ))
{
if( !( config.givenOptions & CL_OFFLIGHTCOLOR ))
ReadString( value, line, &config.offlightColor );
continue;
}
if( PREFIX_MATCHES( id, "Colors.NonShapedFrame", false ))
{
if( !( config.givenOptions & CL_NOSHAPE ))
ReadString( value, line, &config.backgroundColor );
continue;
}
if( PREFIX_MATCHES( id, "Ticker.X11Font", false ))
{
if( !( config.givenOptions & CL_USEX11FONT ))
ReadString( value, line, &config.useX11Font );
continue;
}
if( PREFIX_MATCHES( id, "Mail.SkipSender", false ))
{
/*
* No corresponding command-line option.
*/
char *skip;
if( ReadString( value, line, &skip ))
AddSenderToSkipList( skip );
continue;
}
if( PREFIX_MATCHES( id, "Mail.OnNew.Command", false ))
{
if( !( config.givenOptions & CL_CMDONMAIL ))
ReadString( value, line, &config.cmdOnMail );
continue;
}
if( PREFIX_MATCHES( id, "Mail.UseStatusField", false ))
{
if( !( config.givenOptions & CL_CONSIDERSTATUSFIELD ))
ReadBool( value, line, &config.considerStatusField );
continue;
}
if( PREFIX_MATCHES( id, "Mail.ReadStatus", false ))
{
if( !( config.givenOptions & CL_READSTATUS ))
ReadString( value, line, &config.readStatus );
continue;
}
WARNING( "cfg-file(%i): unrecognized: \"%s\"\n", line, buf );
} }
fclose( f );
} else { } else {
TRACE( "no $HOME defined - config-file not read\n" ); TRACE( "unable to open config-file \"%s\"\n", configFile );
} }
PostProcessConfiguration(); PostProcessConfiguration();

View file

@ -67,7 +67,8 @@ enum {
CL_CMDONMAIL = 1<<15, CL_CMDONMAIL = 1<<15,
CL_CONSIDERSTATUSFIELD = 1<<16, CL_CONSIDERSTATUSFIELD = 1<<16,
CL_READSTATUS = 1<<17, CL_READSTATUS = 1<<17,
CL_USEX11FONT = 1<<18 CL_USEX11FONT = 1<<18,
CL_CONFIGFILE = 1<<19
}; };
typedef struct _config_t { typedef struct _config_t {
@ -100,7 +101,7 @@ typedef struct _config_t {
extern config_t config; extern config_t config;
// config manipulation functions // config manipulation functions
void ReadConfigFile( bool resetConfigStrings ); void ReadConfigFile( const char *configFile, bool resetConfigStrings );
void ResetConfigStrings( void ); void ResetConfigStrings( void );

View file

@ -115,6 +115,7 @@ typedef enum {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// data // data
static char *configFile;
static unsigned long lastTimeOut; static unsigned long lastTimeOut;
static sig_atomic_t caughtSig; static sig_atomic_t caughtSig;
static mail_state_t state; static mail_state_t state;
@ -158,7 +159,8 @@ enum
OPT_INDEX_EXECUTE, OPT_INDEX_EXECUTE,
OPT_INDEX_STATUS_FIELD, OPT_INDEX_STATUS_FIELD,
OPT_INDEX_READ_STATUS, OPT_INDEX_READ_STATUS,
OPT_INDEX_TICKER_FONT OPT_INDEX_TICKER_FONT,
OPT_INDEX_CONFIG_FILE,
}; };
static DAProgramOption options[] = { static DAProgramOption options[] = {
@ -276,6 +278,13 @@ static DAProgramOption options[] = {
.description = "use specified X11 font to draw the ticker", .description = "use specified X11 font to draw the ticker",
.type = DOString, .type = DOString,
.value = { .string = &config.useX11Font } .value = { .string = &config.useX11Font }
},
[OPT_INDEX_CONFIG_FILE] = {
.shortForm = "-rc",
.longForm = "--rcfile",
.description = "specify another rc-file ($HOME/.wmailrc is default)",
.type = DOString,
.value = { .string = &configFile }
} }
}; };
@ -324,7 +333,7 @@ static bool HasTickerWork( void );
int main( int argc, char **argv ) int main( int argc, char **argv )
{ {
char *usersHome; char *usersHome = getenv( "HOME" );
struct sigaction sa = { .sa_handler = ExitHandler }; struct sigaction sa = { .sa_handler = ExitHandler };
struct stat fileStat; struct stat fileStat;
XTextProperty windowName; XTextProperty windowName;
@ -383,21 +392,45 @@ int main( int argc, char **argv )
if( options[OPT_INDEX_TICKER_FONT].used ) if( options[OPT_INDEX_TICKER_FONT].used )
config.givenOptions |= CL_USEX11FONT; config.givenOptions |= CL_USEX11FONT;
// read the config file if( configFile == NULL)
ReadConfigFile( false ); {
if( usersHome == NULL)
{
WARNING( "HOME environment-variable is not set, looking for %s in current directory!\n",
WMAIL_RC_FILE );
configFile = strdup( WMAIL_RC_FILE );
}
else
configFile = MakePathName( usersHome, WMAIL_RC_FILE );
if( configFile == NULL )
{
WARNING( "Cannot allocate config file-name.\n");
exit( EXIT_FAILURE );
}
if( config.checksumFileName == NULL ) {
if(( usersHome = getenv( "HOME" )) == NULL ) {
WARNING( "HOME environment-variable is not set, placing %s in current directory!\n", WMAIL_CHECKSUM_FILE );
config.checksumFileName = strdup( WMAIL_CHECKSUM_FILE );
} else
config.checksumFileName = MakePathName( usersHome, WMAIL_CHECKSUM_FILE );
} }
if( config.checksumFileName == NULL ) TRACE( "%s: configFile = %s\n", __func__, configFile );
{
WARNING( "Cannot allocate checksum file-name.\n"); // read the config file
exit( EXIT_FAILURE ); ReadConfigFile( configFile, false );
if( config.checksumFileName == NULL ) {
if( usersHome == NULL )
{
WARNING( "HOME environment-variable is not set, placing %s in current directory!\n",
WMAIL_CHECKSUM_FILE );
config.checksumFileName = strdup( WMAIL_CHECKSUM_FILE );
}
else
config.checksumFileName = MakePathName( usersHome, WMAIL_CHECKSUM_FILE );
if( config.checksumFileName == NULL )
{
WARNING( "Cannot allocate checksum file-name.\n");
exit( EXIT_FAILURE );
}
} }
TRACE( "using checksum-file \"%s\"\n", config.checksumFileName ); TRACE( "using checksum-file \"%s\"\n", config.checksumFileName );
@ -672,6 +705,8 @@ static void TimedOut( void )
if( caughtSig ) { if( caughtSig ) {
ClearAllNames(); ClearAllNames();
ResetConfigStrings(); ResetConfigStrings();
if( !options[OPT_INDEX_CONFIG_FILE].used )
free( configFile );
exit( EXIT_SUCCESS ); exit( EXIT_SUCCESS );
} }
@ -1472,7 +1507,7 @@ static void UpdateConfiguration( void )
TRACE( "reading configuration file...\n" ); TRACE( "reading configuration file...\n" );
ReadConfigFile( true ); ReadConfigFile( configFile, true );
// if no path/name to an mbox or maildir inbox directory was given, // if no path/name to an mbox or maildir inbox directory was given,
// use the environment // use the environment