Skip to content

Commit

Permalink
main: make -f option work in json and xref output
Browse files Browse the repository at this point in the history
Close #1546.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Sep 10, 2017
1 parent f1396a3 commit 4b7a154
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 22 deletions.
12 changes: 12 additions & 0 deletions Tmain/json-output-to-file.d/input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>

static int foo (void)
{
return 1;
}

int
main(void)
{
return foo();
}
17 changes: 17 additions & 0 deletions Tmain/json-output-to-file.d/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright: 2017 Masatake YAMATO
# License: GPL-2

CTAGS=$1

. ../utils.sh

O=/tmp/ctags-tmain-$$.json

is_feature_available "${CTAGS}" json

$CTAGS --extras=-p --output-format=json -f ${O} input.c > /dev/null && cat ${O}
s=$?

rm -f ${O}

exit $s
2 changes: 2 additions & 0 deletions Tmain/json-output-to-file.d/stdout-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"_type": "tag", "name": "foo", "path": "input.c", "pattern": "/^static int foo (void)$/", "file": true, "typeref": "int", "kind": "function"}
{"_type": "tag", "name": "main", "path": "input.c", "pattern": "/^main(void)$/", "typeref": "int", "kind": "function"}
22 changes: 22 additions & 0 deletions Tmain/json-ptag-in-list-extras.d/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright: 2017 Masatake YAMATO
# License: GPL-2

CTAGS=$1


. ../utils.sh

is_feature_available ${CTAGS} json

O=/tmp/ctags-tmain-$$

echo '#' json regular file
"${CTAGS}" --quiet --options=NONE --output-format=json -o $O --list-extras | grep pseudo

echo '#' json -
"${CTAGS}" --quiet --options=NONE --output-format=json -o - --list-extras | grep pseudo

echo '#' json NOTHING
"${CTAGS}" --quiet --options=NONE --output-format=json --list-extras | grep pseudo

rm -f $O
Empty file.
6 changes: 6 additions & 0 deletions Tmain/json-ptag-in-list-extras.d/stdout-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# json regular file
p pseudo yes NONE no Include pseudo tags
# json -
p pseudo no NONE no Include pseudo tags
# json NOTHING
p pseudo no NONE no Include pseudo tags
12 changes: 12 additions & 0 deletions Tmain/xref-output-to-file.d/input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>

static int foo (void)
{
return 1;
}

int
main(void)
{
return foo();
}
14 changes: 14 additions & 0 deletions Tmain/xref-output-to-file.d/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright: 2017 Masatake YAMATO
# License: GPL-2

CTAGS=$1

. ../utils.sh

O=/tmp/ctags-tmain-$$.xref
$CTAGS --output-format=xref -f ${O} input.c > /dev/null && cat ${O}
s=$?

rm -f ${O}

exit $s
2 changes: 2 additions & 0 deletions Tmain/xref-output-to-file.d/stdout-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo function 3 input.c static int foo (void)
main function 9 input.c main(void)
26 changes: 26 additions & 0 deletions Tmain/xref-ptag-in-list-extras.d/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright: 2017 Masatake YAMATO
# License: GPL-2

CTAGS=$1

O=/tmp/ctags-tmain-$$

echo '#' tags regular file
"${CTAGS}" --options=NONE --output-format=u-ctags -o $O --list-extras | grep pseudo

echo '#' tags -
"${CTAGS}" --options=NONE --output-format=u-ctags -o - --list-extras | grep pseudo

echo '#' tags NOTHING
"${CTAGS}" --options=NONE --output-format=u-ctags --list-extras | grep pseudo

echo '#' xref regular file
"${CTAGS}" --options=NONE --output-format=xref -o $O --list-extras | grep pseudo

echo '#' xref -
"${CTAGS}" --options=NONE --output-format=xref -o - --list-extras | grep pseudo

echo '#' xref NOTHING
"${CTAGS}" --options=NONE --output-format=xref --list-extras | grep pseudo

rm -f $O
12 changes: 12 additions & 0 deletions Tmain/xref-ptag-in-list-extras.d/stdout-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# tags regular file
p pseudo yes NONE no Include pseudo tags
# tags -
p pseudo no NONE no Include pseudo tags
# tags NOTHING
p pseudo yes NONE no Include pseudo tags
# xref regular file
p pseudo no NONE yes Include pseudo tags
# xref -
p pseudo no NONE yes Include pseudo tags
# xref NOTHING
p pseudo no NONE yes Include pseudo tags
5 changes: 4 additions & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ extern bool isDestinationStdout (void)
{
bool toStdout = false;

if (outputFormatUsedStdoutByDefault() || Option.filter || Option.interactive ||
if (Option.filter || Option.interactive ||
(Option.tagFileName != NULL && (strcmp (Option.tagFileName, "-") == 0
|| strcmp (Option.tagFileName, "/dev/stdout") == 0
)))
toStdout = true;
else if (Option.tagFileName == NULL && NULL == outputDefaultFileName ())
toStdout = true;

return toStdout;
}

Expand Down
22 changes: 13 additions & 9 deletions main/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@
#define CTAGS_ENVIRONMENT "CTAGS"
#define ETAGS_ENVIRONMENT "ETAGS"

#define CTAGS_FILE "tags"
#define ETAGS_FILE "TAGS"

#ifndef ETAGS
# define ETAGS "etags" /* name which causes default use of to -e */
#endif
Expand Down Expand Up @@ -696,13 +693,20 @@ extern void freeList (stringList** const pList)
}

extern void setDefaultTagFileName (void)

{
if (Option.tagFileName != NULL)
; /* accept given name */
else if (Option.etags)
Option.tagFileName = stringCopy (ETAGS_FILE);
else
Option.tagFileName = stringCopy (CTAGS_FILE);
if (Option.filter || Option.interactive)
return;

if (Option.tagFileName == NULL)
{
const char *tmp = outputDefaultFileName ();

if (tmp == NULL)
tmp = "-";

Option.tagFileName = stringCopy (tmp);
}
}

extern bool filesRequired (void)
Expand Down
7 changes: 5 additions & 2 deletions main/writer-ctags.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "writer.h"


#define CTAGS_FILE "tags"


static int writeCtagsEntry (tagWriter *writer CTAGS_ATTR_UNUSED,
MIO * mio, const tagEntryInfo *const tag);
static int writeCtagsPtagEntry (tagWriter *writer CTAGS_ATTR_UNUSED,
Expand All @@ -37,7 +40,7 @@ tagWriter uCtagsWriter = {
.preWriteEntry = NULL,
.postWriteEntry = NULL,
.buildFqTagCache = buildCtagsFqTagCache,
.useStdoutByDefault = false,
.defaultFileName = CTAGS_FILE,
};

static void *beginECtagsFile (tagWriter *writer CTAGS_ATTR_UNUSED, MIO * mio CTAGS_ATTR_UNUSED)
Expand All @@ -61,7 +64,7 @@ tagWriter eCtagsWriter = {
.preWriteEntry = beginECtagsFile,
.postWriteEntry = endECTagsFile,
.buildFqTagCache = buildCtagsFqTagCache,
.useStdoutByDefault = false,
.defaultFileName = CTAGS_FILE,
};

static const char* escapeFieldValue (tagWriter *writer, const tagEntryInfo * tag, fieldType ftype)
Expand Down
4 changes: 4 additions & 0 deletions main/writer-etags.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "writer.h"


#define ETAGS_FILE "TAGS"


static int writeEtagsEntry (tagWriter *writer, MIO * mio, const tagEntryInfo *const tag);
static void *beginEtagsFile (tagWriter *writer, MIO * mio);
static bool endEtagsFile (tagWriter *writer, MIO * mio, const char* filename);
Expand All @@ -29,6 +32,7 @@ tagWriter etagsWriter = {
.writePtagEntry = NULL,
.preWriteEntry = beginEtagsFile,
.postWriteEntry = endEtagsFile,
.defaultFileName = ETAGS_FILE,
};

struct sEtags {
Expand Down
4 changes: 2 additions & 2 deletions main/writer-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ tagWriter jsonWriter = {
.preWriteEntry = NULL,
.postWriteEntry = NULL,
.buildFqTagCache = buildJsonFqTagCache,
.useStdoutByDefault = true,
.defaultFileName = NULL,
};


Expand Down Expand Up @@ -232,7 +232,7 @@ tagWriter jsonWriter = {
.writePtagEntry = NULL,
.preWriteEntry = NULL,
.postWriteEntry = NULL,
.useStdoutByDefault = true,
.defaultFileName = "-",
};

extern bool ptagMakeJsonOutputVersion (ptagDesc *desc, void *data CTAGS_ATTR_UNUSED)
Expand Down
2 changes: 1 addition & 1 deletion main/writer-xref.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tagWriter xrefWriter = {
.preWriteEntry = NULL,
.postWriteEntry = NULL,
.buildFqTagCache = buildXrefFqTagCache,
.useStdoutByDefault = true,
.defaultFileName = NULL,
};

static int writeXrefEntry (tagWriter *writer CTAGS_ATTR_UNUSED,
Expand Down
10 changes: 5 additions & 5 deletions main/writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ extern void setTagWriter (writerType wtype)
writer->type = wtype;
}

extern bool outputFormatUsedStdoutByDefault (void)
{
return writer->useStdoutByDefault;
}

extern void writerSetup (MIO *mio)
{
if (writer->preWriteEntry)
Expand Down Expand Up @@ -99,6 +94,11 @@ extern bool ptagMakeCtagsOutputMode (ptagDesc *desc, void *data CTAGS_ATTR_UNUSE
NULL);
}

extern const char *outputDefaultFileName (void)
{
return writer->defaultFileName;
}

extern bool writerCanPrintPtag (void)
{
return (writer->writePtagEntry)? true: false;
Expand Down
4 changes: 2 additions & 2 deletions main/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct sTagWriter {
In such case the callee may do truncate output file. */
bool (* postWriteEntry) (tagWriter *writer, MIO * mio, const char* filename);
void (* buildFqTagCache) (tagWriter *writer, tagEntryInfo *const tag);
bool useStdoutByDefault;
const char *defaultFileName;

/* The value returned from preWriteEntry is stored `private' field.
The value must be released in postWriteEntry. */
Expand All @@ -64,7 +64,7 @@ int writerWritePtag (MIO * mio,

extern void writerBuildFqTagCache (tagEntryInfo *const tag);

extern bool outputFormatUsedStdoutByDefault (void);
extern const char *outputDefaultFileName (void);

extern void truncateTagLineAfterTag (char *const line, const char *const token,
const bool discardNewline);
Expand Down

0 comments on commit 4b7a154

Please sign in to comment.