Skip to content

Commit

Permalink
Merge pull request universal-ctags#23 from masatake/github-issue-21
Browse files Browse the repository at this point in the history
Propagate errno from readTagLineRaw to tagsOpen instead of calling perror
  • Loading branch information
masatake authored Nov 3, 2020
2 parents 249dfdc + 8536bc8 commit 6c289b7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
33 changes: 26 additions & 7 deletions readtags.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ static void copyName (tagFile *const file)
file->name.buffer [length] = '\0';
}

static int readTagLineRaw (tagFile *const file)
static int readTagLineRaw (tagFile *const file, int *err)
{
int result = 1;
int reReadLine;
Expand All @@ -281,8 +281,13 @@ static int readTagLineRaw (tagFile *const file)
if (line == NULL)
{
/* read error */
if (err)
*err = 0;
if (! feof (file->fp))
perror ("readTagLine");
{
if (err)
*err = errno;
}
result = 0;
}
else if (*pLastChar != '\0' &&
Expand All @@ -309,16 +314,21 @@ static int readTagLineRaw (tagFile *const file)
return result;
}

static int readTagLine (tagFile *const file)
static int readTagLineFull (tagFile *const file, int *err)
{
int result;
do
{
result = readTagLineRaw (file);
result = readTagLineRaw (file, err);
} while (result && *file->name.buffer == '\0');
return result;
}

static int readTagLine (tagFile *const file)
{
return readTagLineFull (file, NULL);
}

static tagResult growFields (tagFile *const file)
{
tagResult result = TagFailure;
Expand Down Expand Up @@ -574,9 +584,10 @@ static int isPseudoTagLine (const char *buffer)
return (strncmp (buffer, PseudoTagPrefix, PseudoTagPrefixLength) == 0);
}

static void readPseudoTags (tagFile *const file, tagFileInfo *const info)
static int readPseudoTags (tagFile *const file, tagFileInfo *const info)
{
fpos_t startOfLine;
int err = 0;
const size_t prefixLength = strlen (PseudoTagPrefix);
if (info != NULL)
{
Expand All @@ -590,7 +601,7 @@ static void readPseudoTags (tagFile *const file, tagFileInfo *const info)
while (1)
{
fgetpos (file->fp, &startOfLine);
if (! readTagLine (file))
if (! readTagLineFull (file, &err))
break;
if (!isPseudoTagLine (file->line.buffer))
break;
Expand Down Expand Up @@ -625,6 +636,7 @@ static void readPseudoTags (tagFile *const file, tagFileInfo *const info)
}
}
fsetpos (file->fp, &startOfLine);
return err;
}

static int doesFilePointPseudoTag (tagFile *const file, void *unused)
Expand Down Expand Up @@ -684,7 +696,12 @@ static tagFile *initialize (const char *const filePath, tagFileInfo *const info)
goto file_error;
}
rewind (result->fp);
readPseudoTags (result, info);

if (info)
info->status.error_number = readPseudoTags (result, info);
if (info && info->status.error_number)
goto file_error;

if (info)
info->status.opened = 1;
result->initialized = 1;
Expand All @@ -698,6 +715,8 @@ static tagFile *initialize (const char *const filePath, tagFileInfo *const info)
free (result->line.buffer);
free (result->name.buffer);
free (result->fields.list);
if (result->fp)
fclose (result->fp);
free (result);
if (info)
info->status.opened = 0;
Expand Down
27 changes: 27 additions & 0 deletions tests/test-api-tagsOpen.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "readtags.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -156,6 +157,32 @@ main (void)
}
fprintf (stderr, "ok\n");

fprintf (stderr, "opening a / (EISDIR is expected)...");
t = tagsOpen ("/", &info);
if (t != NULL)
{
fprintf (stderr, "unexpected result (!NULL)\n");
return 1;
}
else if (info.status.opened)
{
fprintf (stderr, "unexpected result (opened != 0)\n");
return 1;
}
else if (info.status.error_number != EISDIR)
{
fprintf (stderr, "unexpected result (error_number != EISDIR)\n");
return 1;
}
fprintf (stderr, "ok\n");

fprintf (stderr, "closing the unopened tag file...");
if (tagsClose (t) == TagSuccess)
{
fprintf (stderr, "unexpected result\n");
return 1;
}
fprintf (stderr, "ok\n");

return 0;
}

0 comments on commit 6c289b7

Please sign in to comment.