Skip to content

Commit

Permalink
fixed #1057 indextool exit with error code in case multiple commands …
Browse files Browse the repository at this point in the history
…set at command line
  • Loading branch information
tomatolog committed Nov 18, 2019
1 parent faed322 commit 5331972
Showing 1 changed file with 69 additions and 53 deletions.
122 changes: 69 additions & 53 deletions src/indextool.cpp
Expand Up @@ -1041,6 +1041,40 @@ static void ShowHelp ()
); );
} }


enum class IndextoolCmd_e
{
NOTHING,
DUMPHEADER,
DUMPCONFIG,
DUMPDOCIDS,
DUMPHITLIST,
DUMPDICT,
CHECK,
STRIP,
MORPH,
BUILDIDF,
MERGEIDF,
CHECKCONFIG,
FOLD,
APPLYKLISTS
};

static IndextoolCmd_e g_eCommand = IndextoolCmd_e::NOTHING;
static const char * g_sCommands[] = {"", "dumpheader", "dumpconfig", "dumpdocids", "dumphitlist", "dumpdict",
"check", "htmlstrip", "morph", "buildidf", "mergeidf", "checkconfig", "fold", "apply-killlists" };


static void SetCmd ( IndextoolCmd_e eCmd )
{
if ( g_eCommand!=IndextoolCmd_e::NOTHING )
{
fprintf ( stdout, "ERROR: multiple commands not supported (%s, %s).\n", g_sCommands[(int)g_eCommand], g_sCommands[(int)eCmd] );
exit ( 1 );
}

g_eCommand = eCmd;
}

int main ( int argc, char ** argv ) int main ( int argc, char ** argv )
{ {
if ( argc<=1 ) if ( argc<=1 )
Expand Down Expand Up @@ -1070,50 +1104,32 @@ int main ( int argc, char ** argv )
bool bRotate = false; bool bRotate = false;
bool bCheckIdDups = false; bool bCheckIdDups = false;


enum
{
CMD_NOTHING,
CMD_DUMPHEADER,
CMD_DUMPCONFIG,
CMD_DUMPDOCIDS,
CMD_DUMPHITLIST,
CMD_DUMPDICT,
CMD_CHECK,
CMD_STRIP,
CMD_MORPH,
CMD_BUILDIDF,
CMD_MERGEIDF,
CMD_CHECKCONFIG,
CMD_FOLD,
CMD_APPLYKLISTS
} eCommand = CMD_NOTHING;

int i; int i;
for ( i=1; i<argc; i++ ) for ( i=1; i<argc; i++ )
{ {
// handle argless options // handle argless options
if ( argv[i][0]!='-' ) break; if ( argv[i][0]!='-' ) break;
OPT ( "-q", "--quiet" ) { bQuiet = true; continue; } OPT ( "-q", "--quiet" ) { bQuiet = true; continue; }
OPT1 ( "--strip-path" ) { bStripPath = true; continue; } OPT1 ( "--strip-path" ) { bStripPath = true; continue; }
OPT1 ( "--checkconfig" ) { eCommand = CMD_CHECKCONFIG; continue; } OPT1 ( "--checkconfig" ) { SetCmd ( IndextoolCmd_e::CHECKCONFIG ); continue; }
OPT1 ( "--rotate" ) { bRotate = true; continue; } OPT1 ( "--rotate" ) { bRotate = true; continue; }
OPT1 ( "-v" ) { ShowVersion(); exit(0); } OPT1 ( "-v" ) { ShowVersion(); exit(0); }
OPT ( "-h", "--help" ) { ShowVersion(); ShowHelp(); exit(0); } OPT ( "-h", "--help" ) { ShowVersion(); ShowHelp(); exit(0); }
OPT1 ( "--apply-killlists" ){ eCommand = CMD_APPLYKLISTS; continue; } OPT1 ( "--apply-killlists" ){ SetCmd ( IndextoolCmd_e::APPLYKLISTS ); continue; }
OPT1 ( "--check-id-dups" ) { bCheckIdDups = true; continue; } OPT1 ( "--check-id-dups" ) { bCheckIdDups = true; continue; }


// handle options/commands with 1+ args // handle options/commands with 1+ args
if ( (i+1)>=argc ) break; if ( (i+1)>=argc ) break;
OPT ( "-c", "--config" ) sOptConfig = argv[++i]; OPT ( "-c", "--config" ) sOptConfig = argv[++i];
OPT1 ( "--dumpheader" ) { eCommand = CMD_DUMPHEADER; sDumpHeader = argv[++i]; } OPT1 ( "--dumpheader" ) { SetCmd ( IndextoolCmd_e::DUMPHEADER ); sDumpHeader = argv[++i]; }
OPT1 ( "--dumpconfig" ) { eCommand = CMD_DUMPCONFIG; sDumpHeader = argv[++i]; } OPT1 ( "--dumpconfig" ) { SetCmd ( IndextoolCmd_e::DUMPCONFIG ); sDumpHeader = argv[++i]; }
OPT1 ( "--dumpdocids" ) { eCommand = CMD_DUMPDOCIDS; sIndex = argv[++i]; } OPT1 ( "--dumpdocids" ) { SetCmd ( IndextoolCmd_e::DUMPDOCIDS ); sIndex = argv[++i]; }
OPT1 ( "--check" ) { eCommand = CMD_CHECK; sIndex = argv[++i]; } OPT1 ( "--check" ) { SetCmd ( IndextoolCmd_e::CHECK ); sIndex = argv[++i]; }
OPT1 ( "--htmlstrip" ) { eCommand = CMD_STRIP; sIndex = argv[++i]; } OPT1 ( "--htmlstrip" ) { SetCmd ( IndextoolCmd_e::STRIP ); sIndex = argv[++i]; }
OPT1 ( "--morph" ) { eCommand = CMD_MORPH; sIndex = argv[++i]; } OPT1 ( "--morph" ) { SetCmd ( IndextoolCmd_e::MORPH ); sIndex = argv[++i]; }
OPT1 ( "--dumpdict" ) OPT1 ( "--dumpdict" )
{ {
eCommand = CMD_DUMPDICT; SetCmd ( IndextoolCmd_e::DUMPDICT );
sDumpDict = argv[++i]; sDumpDict = argv[++i];
if ( (i+1)<argc && !strcmp ( argv[i+1], "--stats" ) ) if ( (i+1)<argc && !strcmp ( argv[i+1], "--stats" ) )
{ {
Expand All @@ -1123,7 +1139,7 @@ int main ( int argc, char ** argv )
} }
OPT1 ( "--fold" ) OPT1 ( "--fold" )
{ {
eCommand = CMD_FOLD; SetCmd ( IndextoolCmd_e::FOLD );
sIndex = argv[++i]; sIndex = argv[++i];
if ( (i+1)<argc && argv[i+1][0]!='-' ) if ( (i+1)<argc && argv[i+1][0]!='-' )
sFoldFile = argv[++i]; sFoldFile = argv[++i];
Expand All @@ -1138,7 +1154,7 @@ int main ( int argc, char ** argv )
} }
OPT1 ("--dumphitlist" ) OPT1 ("--dumphitlist" )
{ {
eCommand = CMD_DUMPHITLIST; SetCmd ( IndextoolCmd_e::DUMPHITLIST );
sIndex = argv[++i]; sIndex = argv[++i];


if ( !strcmp ( argv[i+1], "--wordid" ) ) if ( !strcmp ( argv[i+1], "--wordid" ) )
Expand All @@ -1153,7 +1169,7 @@ int main ( int argc, char ** argv )


} else if ( !strcmp ( argv[i], "--buildidf" ) || !strcmp ( argv[i], "--mergeidf" ) ) } else if ( !strcmp ( argv[i], "--buildidf" ) || !strcmp ( argv[i], "--mergeidf" ) )
{ {
eCommand = !strcmp ( argv[i], "--buildidf" ) ? CMD_BUILDIDF : CMD_MERGEIDF; SetCmd ( !strcmp ( argv[i], "--buildidf" ) ? IndextoolCmd_e::BUILDIDF : IndextoolCmd_e::MERGEIDF );
while ( ++i<argc ) while ( ++i<argc )
{ {
if ( !strcmp ( argv[i], "--out" ) ) if ( !strcmp ( argv[i], "--out" ) )
Expand Down Expand Up @@ -1207,13 +1223,13 @@ int main ( int argc, char ** argv )


while (true) while (true)
{ {
if ( eCommand==CMD_DUMPHEADER && sDumpHeader.Ends ( ".meta" ) ) if ( g_eCommand==IndextoolCmd_e::DUMPHEADER && sDumpHeader.Ends ( ".meta" ) )
{ {
InfoMeta ( sDumpHeader ); InfoMeta ( sDumpHeader );
return 0; return 0;
} }


if ( eCommand==CMD_DUMPDICT && !sDumpDict.Ends ( ".spi" ) ) if ( g_eCommand==IndextoolCmd_e::DUMPDICT && !sDumpDict.Ends ( ".spi" ) )
sIndex = sDumpDict; sIndex = sDumpDict;


break; break;
Expand All @@ -1223,7 +1239,7 @@ int main ( int argc, char ** argv )
// action! // action!
/////////// ///////////


if ( eCommand==CMD_CHECKCONFIG ) if ( g_eCommand==IndextoolCmd_e::CHECKCONFIG )
{ {
fprintf ( stdout, "config valid\nchecking index(es) ... " ); fprintf ( stdout, "config valid\nchecking index(es) ... " );


Expand Down Expand Up @@ -1269,7 +1285,7 @@ int main ( int argc, char ** argv )
} }
} }


if ( eCommand==CMD_APPLYKLISTS ) if ( g_eCommand==IndextoolCmd_e::APPLYKLISTS )
{ {
ApplyKilllists ( hConf ); ApplyKilllists ( hConf );
exit (0); exit (0);
Expand All @@ -1287,7 +1303,7 @@ int main ( int argc, char ** argv )
sphDie ( "index '%s': no such index in config\n", sIndex.cstr() ); sphDie ( "index '%s': no such index in config\n", sIndex.cstr() );


// only need config-level settings for --htmlstrip // only need config-level settings for --htmlstrip
if ( eCommand==CMD_STRIP ) if ( g_eCommand==IndextoolCmd_e::STRIP )
break; break;


CSphVariant * pType = hConf["index"][sIndex]("type"); CSphVariant * pType = hConf["index"][sIndex]("type");
Expand Down Expand Up @@ -1317,17 +1333,17 @@ int main ( int argc, char ** argv )
if ( !pIndex ) if ( !pIndex )
sphDie ( "index '%s': failed to create (%s)", sIndex.cstr(), sError.cstr() ); sphDie ( "index '%s': failed to create (%s)", sIndex.cstr(), sError.cstr() );


if ( eCommand==CMD_CHECK ) if ( g_eCommand==IndextoolCmd_e::CHECK )
pIndex->SetDebugCheck ( bCheckIdDups ); pIndex->SetDebugCheck ( bCheckIdDups );


CSphString sWarn; CSphString sWarn;
if ( !pIndex->Prealloc ( bStripPath ) ) if ( !pIndex->Prealloc ( bStripPath ) )
sphDie ( "index '%s': prealloc failed: %s\n", sIndex.cstr(), pIndex->GetLastError().cstr() ); sphDie ( "index '%s': prealloc failed: %s\n", sIndex.cstr(), pIndex->GetLastError().cstr() );


if ( eCommand==CMD_MORPH ) if ( g_eCommand==IndextoolCmd_e::MORPH )
break; break;


if ( eCommand!=CMD_CHECK ) if ( g_eCommand!=IndextoolCmd_e::CHECK )
pIndex->Preread(); pIndex->Preread();


if ( hConf["index"][sIndex]("hitless_words") ) if ( hConf["index"][sIndex]("hitless_words") )
Expand All @@ -1354,13 +1370,13 @@ int main ( int argc, char ** argv )
CSphString sNewIndex; CSphString sNewIndex;


// do the dew // do the dew
switch ( eCommand ) switch ( g_eCommand )
{ {
case CMD_NOTHING: case IndextoolCmd_e::NOTHING:
sphDie ( "nothing to do; specify a command (run indextool w/o switches for help)" ); sphDie ( "nothing to do; specify a command (run indextool w/o switches for help)" );


case CMD_DUMPHEADER: case IndextoolCmd_e::DUMPHEADER:
case CMD_DUMPCONFIG: case IndextoolCmd_e::DUMPCONFIG:
{ {
CSphString sIndexName = "(none)"; CSphString sIndexName = "(none)";
if ( hConf("index") && hConf["index"](sDumpHeader) ) if ( hConf("index") && hConf["index"](sDumpHeader) )
Expand All @@ -1376,21 +1392,21 @@ int main ( int argc, char ** argv )
fprintf ( stdout, "dumping header file '%s'...\n", sDumpHeader.cstr() ); fprintf ( stdout, "dumping header file '%s'...\n", sDumpHeader.cstr() );


pIndex = sphCreateIndexPhrase ( sIndexName.cstr(), "" ); pIndex = sphCreateIndexPhrase ( sIndexName.cstr(), "" );
pIndex->DebugDumpHeader ( stdout, sDumpHeader.cstr(), eCommand==CMD_DUMPCONFIG ); pIndex->DebugDumpHeader ( stdout, sDumpHeader.cstr(), g_eCommand==IndextoolCmd_e::DUMPCONFIG );
break; break;
} }


case CMD_DUMPDOCIDS: case IndextoolCmd_e::DUMPDOCIDS:
fprintf ( stdout, "dumping docids for index '%s'...\n", sIndex.cstr() ); fprintf ( stdout, "dumping docids for index '%s'...\n", sIndex.cstr() );
pIndex->DebugDumpDocids ( stdout ); pIndex->DebugDumpDocids ( stdout );
break; break;


case CMD_DUMPHITLIST: case IndextoolCmd_e::DUMPHITLIST:
fprintf ( stdout, "dumping hitlist for index '%s' keyword '%s'...\n", sIndex.cstr(), sKeyword.cstr() ); fprintf ( stdout, "dumping hitlist for index '%s' keyword '%s'...\n", sIndex.cstr(), sKeyword.cstr() );
pIndex->DebugDumpHitlist ( stdout, sKeyword.cstr(), bWordid ); pIndex->DebugDumpHitlist ( stdout, sKeyword.cstr(), bWordid );
break; break;


case CMD_DUMPDICT: case IndextoolCmd_e::DUMPDICT:
{ {
if ( sDumpDict.Ends ( ".spi" ) ) if ( sDumpDict.Ends ( ".spi" ) )
{ {
Expand All @@ -1416,7 +1432,7 @@ int main ( int argc, char ** argv )
break; break;
} }


case CMD_CHECK: case IndextoolCmd_e::CHECK:
fprintf ( stdout, "checking index '%s'...\n", sIndex.cstr() ); fprintf ( stdout, "checking index '%s'...\n", sIndex.cstr() );
iCheckErrno = pIndex->DebugCheck ( stdout ); iCheckErrno = pIndex->DebugCheck ( stdout );
if ( iCheckErrno ) if ( iCheckErrno )
Expand All @@ -1430,7 +1446,7 @@ int main ( int argc, char ** argv )
} }
return 0; return 0;


case CMD_STRIP: case IndextoolCmd_e::STRIP:
{ {
const CSphConfigSection & hIndex = hConf["index"][sIndex]; const CSphConfigSection & hIndex = hConf["index"][sIndex];
if ( hIndex.GetInt ( "html_strip" )==0 ) if ( hIndex.GetInt ( "html_strip" )==0 )
Expand All @@ -1439,21 +1455,21 @@ int main ( int argc, char ** argv )
} }
break; break;


case CMD_MORPH: case IndextoolCmd_e::MORPH:
ApplyMorphology ( pIndex ); ApplyMorphology ( pIndex );
break; break;


case CMD_BUILDIDF: case IndextoolCmd_e::BUILDIDF:
if ( !BuildIDF ( sOut, dFiles, sError, bSkipUnique ) ) if ( !BuildIDF ( sOut, dFiles, sError, bSkipUnique ) )
sphDie ( "ERROR: %s\n", sError.cstr() ); sphDie ( "ERROR: %s\n", sError.cstr() );
break; break;


case CMD_MERGEIDF: case IndextoolCmd_e::MERGEIDF:
if ( !MergeIDF ( sOut, dFiles, sError, bSkipUnique ) ) if ( !MergeIDF ( sOut, dFiles, sError, bSkipUnique ) )
sphDie ( "ERROR: %s\n", sError.cstr() ); sphDie ( "ERROR: %s\n", sError.cstr() );
break; break;


case CMD_FOLD: case IndextoolCmd_e::FOLD:
{ {
FILE * fp = stdin; FILE * fp = stdin;
if ( !sFoldFile.IsEmpty() ) if ( !sFoldFile.IsEmpty() )
Expand All @@ -1469,7 +1485,7 @@ int main ( int argc, char ** argv )
break; break;


default: default:
sphDie ( "INTERNAL ERROR: unhandled command (id=%d)", (int)eCommand ); sphDie ( "INTERNAL ERROR: unhandled command (id=%d)", (int)g_eCommand );
} }


return 0; return 0;
Expand Down

0 comments on commit 5331972

Please sign in to comment.