Skip to content

Commit

Permalink
Implement version command in C.
Browse files Browse the repository at this point in the history
  • Loading branch information
dwsteele committed Nov 29, 2017
1 parent bd74711 commit 915ae56
Show file tree
Hide file tree
Showing 22 changed files with 1,288 additions and 53 deletions.
5 changes: 4 additions & 1 deletion build/lib/pgBackRestBuild/Config/Build.pm
Expand Up @@ -16,11 +16,11 @@ use Storable qw(dclone);

use pgBackRest::Common::Log;
use pgBackRest::Common::String;
use pgBackRestBuild::Config::Data;
use pgBackRest::Version;

use pgBackRestBuild::Build::Common;
use pgBackRestBuild::Config::BuildDefine;
use pgBackRestBuild::Config::Data;

####################################################################################################################################
# Constants
Expand Down Expand Up @@ -123,6 +123,9 @@ sub buildConfig
" )\n";
}

# Add "none" command that is used to initialize the current command before anything is parsed
push(@{$rhEnum->{&BLD_LIST}}, buildConfigCommandEnum('none'));

$strBuildSource .=
")\n";

Expand Down
2 changes: 1 addition & 1 deletion build/lib/pgBackRestBuild/Config/BuildDefine.pm
Expand Up @@ -16,10 +16,10 @@ use Storable qw(dclone);

use pgBackRest::Common::Log;
use pgBackRest::Common::String;
use pgBackRestBuild::Config::Data;
use pgBackRest::Version;

use pgBackRestBuild::Build::Common;
use pgBackRestBuild::Config::Data;

####################################################################################################################################
# Constants
Expand Down
132 changes: 132 additions & 0 deletions build/lib/pgBackRestBuild/Config/BuildParse.pm
@@ -0,0 +1,132 @@
####################################################################################################################################
# Auto-Generate Option Definition for Parsing with getopt_long().
####################################################################################################################################
package pgBackRestBuild::Config::BuildParse;

use strict;
use warnings FATAL => qw(all);
use Carp qw(confess);
use English '-no_match_vars';

use Cwd qw(abs_path);
use Exporter qw(import);
our @EXPORT = qw();
use File::Basename qw(dirname);
use Storable qw(dclone);

use pgBackRest::Common::Log;
use pgBackRest::Common::String;
use pgBackRest::Version;

use pgBackRestBuild::Build::Common;
use pgBackRestBuild::Config::Build;
use pgBackRestBuild::Config::Data;

####################################################################################################################################
# Constants
####################################################################################################################################
use constant BLDLCL_FILE_DEFINE => 'parse';

use constant BLDLCL_DATA_OPTION => '01-dataOption';

####################################################################################################################################
# Definitions for constants and data to build
####################################################################################################################################
my $strSummary = 'Option Parse Definition';

my $rhBuild =
{
&BLD_FILE =>
{
&BLDLCL_FILE_DEFINE =>
{
&BLD_SUMMARY => $strSummary,

&BLD_DATA =>
{
&BLDLCL_DATA_OPTION =>
{
&BLD_SUMMARY => 'Option parse data',
},
},
},
},
};

####################################################################################################################################
# Build configuration constants and data
####################################################################################################################################
sub buildConfigParse
{
# Build option parse list
#-------------------------------------------------------------------------------------------------------------------------------
my $rhConfigDefine = cfgDefine();

my $strBuildSource =
"static const struct option optionList[] =\n" .
"{\n";

foreach my $strOption (sort(keys(%{$rhConfigDefine})))
{
my $rhOption = $rhConfigDefine->{$strOption};
my $strOptionEnum = buildConfigOptionEnum($strOption);
my $strOptionArg = ($rhOption->{&CFGDEF_TYPE} ne CFGDEF_TYPE_BOOLEAN ? " .has_arg = required_argument,\n" : '');
my $strOptionPrefix = $rhConfigDefine->{$strOption}{&CFGDEF_PREFIX};

for (my $iOptionIdx = 0; $iOptionIdx <= $rhOption->{&CFGDEF_INDEX_TOTAL}; $iOptionIdx++)
{
# Don't and option indexes if it is not indexeds
next if ($iOptionIdx == 1 && $rhOption->{&CFGDEF_INDEX_TOTAL} == 1);

# Generate option name
my $strOptionName = $iOptionIdx > 0 ?
"${strOptionPrefix}${iOptionIdx}-" . substr($strOption, length($strOptionPrefix) + 1) : $strOption;

# Generate option value used for parsing (offset is added so options don't conflict with getopt_long return values)
my $strOptionVal = 'PARSE_OPTION_OFFSET + ' . $strOptionEnum . ($iOptionIdx > 1 ? " + " . ($iOptionIdx - 1) : '');

# Add option
$strBuildSource .=
" {\n" .
" .name = \"${strOptionName}\",\n" .
$strOptionArg .
" .val = ${strOptionVal},\n" .
" },\n";

# Add negation when defined
if ($rhOption->{&CFGDEF_NEGATE})
{
$strBuildSource .=
" {\n" .
" .name = \"no-${strOptionName}\",\n" .
" .val = ${strOptionVal},\n" .
" },\n";
}
}
}

# Perl options passed to the C binary should be ignored (unless calling Perl which is done elsewhere)
$strBuildSource .=
" // Perl option is ignored by normal config parsing\n" .
" {\n" .
" .name = \"perl-option\",\n" .
" .has_arg = required_argument,\n" .
" .val = 0,\n" .
" },\n";

# The option list needs to be terminated or getopt_long will just keep on reading
$strBuildSource .=
" // Terminate option list\n" .
" {\n" .
" .name = NULL\n" .
" }\n" .
"};\n";

$rhBuild->{&BLD_FILE}{&BLDLCL_FILE_DEFINE}{&BLD_DATA}{&BLDLCL_DATA_OPTION}{&BLD_SOURCE} = $strBuildSource;

return $rhBuild;
}

push @EXPORT, qw(buildConfigParse);

1;
4 changes: 4 additions & 0 deletions doc/xml/release.xml
Expand Up @@ -23,6 +23,10 @@
</release-improvement-list>

<release-development-list>
<release-item>
<p>Implement <cmd>version</cmd> command in C.</p>
</release-item>

<release-item>
<p>Add <code>memGrowRaw()</code> to memory context module.</p>
</release-item>
Expand Down
30 changes: 24 additions & 6 deletions src/Makefile
Expand Up @@ -2,12 +2,30 @@ CC=gcc
CFLAGS=-I. -Wfatal-errors -Wall -Wextra -Wwrite-strings -Wno-clobbered -std=c99 -funroll-loops -ftree-vectorize
DESTDIR=

pgbackrest: main.o \
common/error.o common/errorType.o common/memContext.o common/type/list.o common/type/string.o common/type/stringList.o \
perl/exec.o
$(CC) $(CFLAGS) -o pgbackrest main.o \
common/error.o common/errorType.o common/memContext.o common/type/list.o common/type/string.o common/type/stringList.o \
perl/exec.o
pgbackrest: \
common/error.o \
common/errorType.o \
common/memContext.o \
common/type/list.o \
common/type/string.o \
common/type/stringList.o \
config/config.o \
config/define.o \
config/parse.o \
perl/exec.o \
main.o
$(CC) $(CFLAGS) -o pgbackrest \
common/error.o \
common/errorType.o \
common/memContext.o \
common/type/list.o \
common/type/string.o \
common/type/stringList.o \
config/config.o \
config/define.o \
config/parse.o \
perl/exec.o \
main.o

install: pgbackrest
sudo install -d $(DESTDIR)/usr/bin
Expand Down
2 changes: 2 additions & 0 deletions src/common/errorType.c
Expand Up @@ -31,6 +31,8 @@ Define errors
ERROR_DEFINE(ERROR_CODE_MIN, AssertError, RuntimeError);

ERROR_DEFINE(ERROR_CODE_MIN + 04, FormatError, RuntimeError);
ERROR_DEFINE(ERROR_CODE_MIN + 23, CommandInvalidError, FormatError);
ERROR_DEFINE(ERROR_CODE_MIN + 31, OptionInvalidError, FormatError);
ERROR_DEFINE(ERROR_CODE_MIN + 69, MemoryError, RuntimeError);
ERROR_DEFINE(ERROR_CODE_MIN + 70, CipherError, FormatError);

Expand Down
2 changes: 2 additions & 0 deletions src/common/errorType.h
Expand Up @@ -17,6 +17,8 @@ typedef struct ErrorType ErrorType;
ERROR_DECLARE(AssertError);

ERROR_DECLARE(FormatError);
ERROR_DECLARE(CommandInvalidError);
ERROR_DECLARE(OptionInvalidError);
ERROR_DECLARE(MemoryError);
ERROR_DECLARE(CipherError);

Expand Down
1 change: 1 addition & 0 deletions src/config/config.auto.h
Expand Up @@ -26,6 +26,7 @@ typedef enum
cfgCmdStart,
cfgCmdStop,
cfgCmdVersion,
cfgCmdNone,
} ConfigCommand;

/***********************************************************************************************************************************
Expand Down
25 changes: 25 additions & 0 deletions src/config/config.c
Expand Up @@ -52,6 +52,31 @@ Include the automatically generated configuration data
***********************************************************************************************************************************/
#include "config.auto.c"

/***********************************************************************************************************************************
Store the current command
This is generally set by the command parser but can also be set by during execute to change commands, i.e. backup -> expire.
***********************************************************************************************************************************/
ConfigCommand command = cfgCmdNone;

/***********************************************************************************************************************************
Get the current command
***********************************************************************************************************************************/
ConfigCommand
cfgCommand()
{
return command;
}

/***********************************************************************************************************************************
Set the current command
***********************************************************************************************************************************/
void
cfgCommandSet(ConfigCommand commandParam)
{
command = commandParam;
}

/***********************************************************************************************************************************
Ensure that command id is valid
***********************************************************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions src/config/config.h
Expand Up @@ -12,9 +12,11 @@ Command and Option Configuration
/***********************************************************************************************************************************
Functions
***********************************************************************************************************************************/
ConfigCommand cfgCommand();
int cfgCommandId(const char *commandName);
const char *cfgCommandName(ConfigCommand commandId);
ConfigDefineCommand cfgCommandDefIdFromId(ConfigCommand commandId);
void cfgCommandSet(ConfigCommand commandParam);
unsigned int cfgCommandTotal();

int cfgOptionId(const char *optionName);
Expand Down

0 comments on commit 915ae56

Please sign in to comment.