Skip to content

Commit aefac5e

Browse files
committed
readtags,refactor: take an action after parsing command line
The original code called findTag or listTags directly in the process of command line parsing. This change introduces new data structure named actionSpec. During parsing command line, readtags builds an actionSpec. After parsing readtags takes an action based on the actionSpec. Signed-off-by: Masatake YAMATO <[email protected]>
1 parent f173ae6 commit aefac5e

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

extra-cmds/readtags-cmd.c

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ struct inputSpec {
4646
char *tempFileName;
4747
};
4848

49+
struct actionSpec {
50+
enum {
51+
ACTION_NONE,
52+
ACTION_FIND = 1 << 0,
53+
ACTION_LIST = 1 << 1,
54+
ACTION_LIST_PTAGS = 1 << 2,
55+
} action;
56+
const char *name; /* for ACTION_FIND */
57+
};
58+
4959
static const char *ProgramName;
5060
static int debugMode;
5161
#ifdef READTAGS_DSL
@@ -696,7 +706,6 @@ static void printVersion(void)
696706

697707
extern int main (int argc, char **argv)
698708
{
699-
int actionSupplied = 0;
700709
int i;
701710
int ignore_prefix = 0;
702711

@@ -706,6 +715,10 @@ extern int main (int argc, char **argv)
706715
.tagFileName = "tags",
707716
.tempFileName = NULL,
708717
};
718+
struct actionSpec actionSpec = {
719+
.action = ACTION_NONE,
720+
.name = NULL,
721+
};
709722

710723
memset (&printOpts, 0, sizeof (printOpts));
711724
memset (&readOpts, 0, sizeof (readOpts));
@@ -732,10 +745,8 @@ extern int main (int argc, char **argv)
732745
const char *const arg = argv [i];
733746
if (ignore_prefix || arg [0] != '-')
734747
{
735-
if (canon)
736-
canon->ptags = 0;
737-
findTag (&inputSpec, arg, &readOpts, &printOpts, canon);
738-
actionSupplied = 1;
748+
actionSpec.action |= ACTION_FIND;
749+
actionSpec.name = arg;
739750
}
740751
else if (arg [0] == '-' && arg [1] == '\0')
741752
ignore_prefix = 1;
@@ -746,13 +757,7 @@ extern int main (int argc, char **argv)
746757
debugMode++;
747758
else if (strcmp (optname, "list-pseudo-tags") == 0
748759
|| strcmp (optname, "with-pseudo-tags") == 0)
749-
{
750-
if (canon)
751-
canon->ptags = 1;
752-
listTags (&inputSpec, 1, &printOpts, canon);
753-
if (optname[0] == 'l')
754-
actionSupplied = 1;
755-
}
760+
actionSpec.action |= ACTION_LIST_PTAGS;
756761
else if (strcmp (optname, "help") == 0)
757762
printUsage (stdout, 0);
758763
#ifdef READTAGS_DSL
@@ -794,12 +799,7 @@ extern int main (int argc, char **argv)
794799
else if (strcmp (optname, "prefix-match") == 0)
795800
readOpts.matchOpts |= TAG_PARTIALMATCH;
796801
else if (strcmp (optname, "list") == 0)
797-
{
798-
if (canon)
799-
canon->ptags = 0;
800-
listTags (&inputSpec, 0, &printOpts, canon);
801-
actionSupplied = 1;
802-
}
802+
actionSpec.action |= ACTION_LIST;
803803
else if (strcmp (optname, "line-number") == 0)
804804
printOpts.lineNumber = 1;
805805
else if (strcmp (optname, "tag-file") == 0)
@@ -900,11 +900,7 @@ extern int main (int argc, char **argv)
900900
case 'd': debugMode++; break;
901901
case 'D':
902902
case 'P':
903-
if (canon)
904-
canon->ptags = 1;
905-
listTags (&inputSpec, 1, &printOpts, canon);
906-
if (arg [j] == 'D')
907-
actionSupplied = 1;
903+
actionSpec.action |= ACTION_LIST_PTAGS;
908904
break;
909905
case 'h': printUsage (stdout, 0); break;
910906
#ifdef READTAGS_DSL
@@ -930,10 +926,7 @@ extern int main (int argc, char **argv)
930926
case 'i': readOpts.matchOpts |= TAG_IGNORECASE; break;
931927
case 'p': readOpts.matchOpts |= TAG_PARTIALMATCH; break;
932928
case 'l':
933-
if (canon)
934-
canon->ptags = 0;
935-
listTags (&inputSpec, 0, &printOpts, canon);
936-
actionSupplied = 1;
929+
actionSpec.action |= ACTION_LIST;
937930
break;
938931
case 'n': printOpts.lineNumber = 1; break;
939932
case 't':
@@ -995,13 +988,39 @@ extern int main (int argc, char **argv)
995988
}
996989
}
997990
}
998-
if (! actionSupplied)
991+
992+
993+
if (actionSpec.action == ACTION_NONE)
999994
{
1000995
fprintf (stderr,
1001996
"%s: no action specified: specify one of NAME, -l or -D\n",
1002997
ProgramName);
1003998
exit (1);
1004999
}
1000+
1001+
if ((actionSpec.action & ACTION_FIND) && (actionSpec.action & ACTION_LIST))
1002+
{
1003+
fprintf (stderr,
1004+
"%s: choose either an action: finding a tag or listing all\n",
1005+
ProgramName);
1006+
exit (1);
1007+
}
1008+
1009+
if (actionSpec.action & ACTION_LIST_PTAGS)
1010+
{
1011+
if (canon)
1012+
canon->ptags = 1;
1013+
listTags (&inputSpec, 1, &printOpts, canon);
1014+
if (canon)
1015+
canon->ptags = 0;
1016+
}
1017+
1018+
if (actionSpec.action & ACTION_FIND)
1019+
findTag (&inputSpec, actionSpec.name, &readOpts, &printOpts, canon);
1020+
else if (actionSpec.action & ACTION_LIST)
1021+
listTags (&inputSpec, 0, &printOpts, canon);
1022+
1023+
10051024
#ifdef READTAGS_DSL
10061025
if (Qualifier)
10071026
q_destroy (Qualifier);

0 commit comments

Comments
 (0)