2005-11-22 12:26:26 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (c) 1996-2001, Darren Hiebert
|
|
|
|
*
|
|
|
|
* This source code is released for free distribution under the terms of the
|
|
|
|
* GNU General Public License.
|
|
|
|
*
|
|
|
|
* This module contains functions for creating tag entries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INCLUDE FILES
|
|
|
|
*/
|
|
|
|
#include "general.h" /* must always come first */
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h> /* to define isspace () */
|
|
|
|
#include <errno.h>
|
|
|
|
#include <glib.h>
|
2007-07-17 12:37:48 +00:00
|
|
|
#include <glib/gstdio.h>
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
#if defined (HAVE_SYS_TYPES_H)
|
|
|
|
# include <sys/types.h> /* to declare off_t on some hosts */
|
|
|
|
#endif
|
|
|
|
#if defined (HAVE_TYPES_H)
|
|
|
|
# include <types.h> /* to declare off_t on some hosts */
|
|
|
|
#endif
|
2008-06-05 16:52:39 +00:00
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
/* These header files provide for the functions necessary to do file
|
|
|
|
* truncation.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
# include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
# include <io.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ctags.h"
|
|
|
|
#include "entry.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "read.h"
|
|
|
|
#include "sort.h"
|
|
|
|
#include "strlist.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MACROS
|
|
|
|
*/
|
|
|
|
#define PSEUDO_TAG_PREFIX "!_"
|
|
|
|
|
|
|
|
#define includeExtensionFlags() (Option.tagFileFormat > 1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Portability defines
|
|
|
|
*/
|
|
|
|
#if !defined(HAVE_TRUNCATE) && !defined(HAVE_FTRUNCATE) && !defined(HAVE_CHSIZE)
|
|
|
|
# define USE_REPLACEMENT_TRUNCATE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Hack for rediculous practice of Microsoft Visual C++.
|
|
|
|
*/
|
|
|
|
#if defined (WIN32) && defined (_MSC_VER)
|
|
|
|
# define chsize _chsize
|
|
|
|
# define open _open
|
|
|
|
# define close _close
|
|
|
|
# define O_RDWR _O_RDWR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DATA DEFINITIONS
|
|
|
|
*/
|
|
|
|
|
|
|
|
tagFile TagFile = {
|
|
|
|
NULL, /* tag file name */
|
|
|
|
NULL, /* tag file directory (absolute) */
|
|
|
|
NULL, /* file pointer */
|
|
|
|
{ 0, 0 }, /* numTags */
|
|
|
|
{ 0, 0, 0 }, /* max */
|
|
|
|
{ NULL, NULL, 0 }, /* etags */
|
|
|
|
NULL /* vLine */
|
|
|
|
};
|
|
|
|
|
|
|
|
static boolean TagsToStdout = FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FUNCTION PROTOTYPES
|
|
|
|
*/
|
|
|
|
#ifdef NEED_PROTO_TRUNCATE
|
|
|
|
extern int truncate (const char *path, off_t length);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NEED_PROTO_FTRUNCATE
|
|
|
|
extern int ftruncate (int fd, off_t length);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FUNCTION DEFINITIONS
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern void freeTagFileResources (void)
|
|
|
|
{
|
|
|
|
eFree (TagFile.directory);
|
|
|
|
vStringDelete (TagFile.vLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern const char *tagFileName (void)
|
|
|
|
{
|
|
|
|
return TagFile.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pseudo tag support
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void rememberMaxLengths (const size_t nameLength, const size_t lineLength)
|
|
|
|
{
|
|
|
|
if (nameLength > TagFile.max.tag)
|
|
|
|
TagFile.max.tag = nameLength;
|
|
|
|
|
|
|
|
if (lineLength > TagFile.max.line)
|
|
|
|
TagFile.max.line = lineLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writePseudoTag (const char *const tagName,
|
|
|
|
const char *const fileName,
|
|
|
|
const char *const pattern)
|
|
|
|
{
|
2011-03-05 22:40:50 +00:00
|
|
|
const int length = mio_printf (TagFile.mio, "%s%s\t%s\t/%s/\n",
|
|
|
|
PSEUDO_TAG_PREFIX, tagName, fileName, pattern);
|
2005-11-22 12:26:26 +00:00
|
|
|
++TagFile.numTags.added;
|
|
|
|
rememberMaxLengths (strlen (tagName), (size_t) length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addPseudoTags (void)
|
|
|
|
{
|
|
|
|
if (! Option.xref)
|
|
|
|
{
|
|
|
|
char format [11];
|
|
|
|
const char *formatComment = "unknown format";
|
|
|
|
|
|
|
|
sprintf (format, "%u", Option.tagFileFormat);
|
|
|
|
|
|
|
|
if (Option.tagFileFormat == 1)
|
|
|
|
formatComment = "original ctags format";
|
|
|
|
else if (Option.tagFileFormat == 2)
|
|
|
|
formatComment =
|
|
|
|
"extended format; --format=1 will not append ;\" to lines";
|
|
|
|
|
|
|
|
writePseudoTag ("TAG_FILE_FORMAT", format, formatComment);
|
|
|
|
writePseudoTag ("TAG_FILE_SORTED", Option.sorted ? "1":"0",
|
|
|
|
"0=unsorted, 1=sorted");
|
|
|
|
writePseudoTag ("TAG_PROGRAM_AUTHOR", AUTHOR_NAME, AUTHOR_EMAIL);
|
|
|
|
writePseudoTag ("TAG_PROGRAM_NAME", PROGRAM_NAME, "");
|
|
|
|
writePseudoTag ("TAG_PROGRAM_URL", PROGRAM_URL, "official site");
|
|
|
|
writePseudoTag ("TAG_PROGRAM_VERSION", PROGRAM_VERSION, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void updateSortedFlag (const char *const line,
|
2011-03-05 22:40:50 +00:00
|
|
|
MIO *const mio, MIOPos startOfLine)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
const char *const tab = strchr (line, '\t');
|
|
|
|
|
|
|
|
if (tab != NULL)
|
|
|
|
{
|
|
|
|
const long boolOffset = tab - line + 1; /* where it should be */
|
|
|
|
|
|
|
|
if (line [boolOffset] == '0' || line [boolOffset] == '1')
|
|
|
|
{
|
2011-03-05 22:40:50 +00:00
|
|
|
MIOPos nextLine;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2011-03-05 22:40:50 +00:00
|
|
|
if (mio_getpos (mio, &nextLine) == -1 || mio_setpos (mio, &startOfLine) == -1)
|
2005-11-22 12:26:26 +00:00
|
|
|
error (WARNING, "Failed to update 'sorted' pseudo-tag");
|
|
|
|
else
|
|
|
|
{
|
2011-03-05 22:40:50 +00:00
|
|
|
MIOPos flagLocation;
|
2005-11-22 12:26:26 +00:00
|
|
|
int c, d;
|
|
|
|
|
|
|
|
do
|
2011-03-05 22:40:50 +00:00
|
|
|
c = mio_getc (mio);
|
2005-11-22 12:26:26 +00:00
|
|
|
while (c != '\t' && c != '\n');
|
2011-03-05 22:40:50 +00:00
|
|
|
mio_getpos (mio, &flagLocation);
|
|
|
|
d = mio_getc (mio);
|
2005-11-22 12:26:26 +00:00
|
|
|
if (c == '\t' && (d == '0' || d == '1') &&
|
|
|
|
d != (int) Option.sorted)
|
|
|
|
{
|
2011-03-05 22:40:50 +00:00
|
|
|
mio_setpos (mio, &flagLocation);
|
|
|
|
mio_putc (mio, Option.sorted ? '1' : '0');
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2011-03-05 22:40:50 +00:00
|
|
|
mio_setpos (mio, &nextLine);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look through all line beginning with "!_TAG_FILE", and update those which
|
|
|
|
* require it.
|
|
|
|
*/
|
2011-03-05 22:40:50 +00:00
|
|
|
static long unsigned int updatePseudoTags (MIO *const mio)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
enum { maxClassLength = 20 };
|
|
|
|
char class [maxClassLength + 1];
|
|
|
|
unsigned long linesRead = 0;
|
2011-03-05 22:40:50 +00:00
|
|
|
MIOPos startOfLine;
|
2005-11-22 12:26:26 +00:00
|
|
|
size_t classLength;
|
|
|
|
const char *line;
|
|
|
|
|
|
|
|
sprintf (class, "%sTAG_FILE", PSEUDO_TAG_PREFIX);
|
|
|
|
classLength = strlen (class);
|
|
|
|
Assert (classLength < maxClassLength);
|
|
|
|
|
2011-03-05 22:40:50 +00:00
|
|
|
mio_getpos (mio, &startOfLine);
|
|
|
|
line = readLine (TagFile.vLine, mio);
|
2005-11-22 12:26:26 +00:00
|
|
|
while (line != NULL && line [0] == class [0])
|
|
|
|
{
|
|
|
|
++linesRead;
|
|
|
|
if (strncmp (line, class, classLength) == 0)
|
|
|
|
{
|
|
|
|
char tab, classType [16];
|
|
|
|
|
|
|
|
if (sscanf (line + classLength, "%15s%c", classType, &tab) == 2 &&
|
|
|
|
tab == '\t')
|
|
|
|
{
|
|
|
|
if (strcmp (classType, "_SORTED") == 0)
|
2011-03-05 22:40:50 +00:00
|
|
|
updateSortedFlag (line, mio, startOfLine);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2011-03-05 22:40:50 +00:00
|
|
|
mio_getpos (mio, &startOfLine);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2011-03-05 22:40:50 +00:00
|
|
|
line = readLine (TagFile.vLine, mio);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
while (line != NULL) /* skip to end of file */
|
|
|
|
{
|
|
|
|
++linesRead;
|
2011-03-05 22:40:50 +00:00
|
|
|
line = readLine (TagFile.vLine, mio);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
return linesRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tag file management
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static boolean isTagFile (const char *const filename)
|
|
|
|
{
|
|
|
|
boolean ok = FALSE; /* we assume not unless confirmed */
|
2011-03-05 22:40:50 +00:00
|
|
|
MIO *const mio = mio_new_file_full (filename, "rb", g_fopen, fclose);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2011-03-05 22:40:50 +00:00
|
|
|
if (mio == NULL && errno == ENOENT)
|
2005-11-22 12:26:26 +00:00
|
|
|
ok = TRUE;
|
2011-03-05 22:40:50 +00:00
|
|
|
else if (mio != NULL)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2011-03-05 22:40:50 +00:00
|
|
|
const char *line = readLine (TagFile.vLine, mio);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
if (line == NULL)
|
|
|
|
ok = TRUE;
|
2011-03-05 22:40:50 +00:00
|
|
|
mio_free (mio);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2011-03-05 22:40:50 +00:00
|
|
|
extern void copyBytes (MIO* const fromMio, MIO* const toMio, const long size)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
enum { BufferSize = 1000 };
|
|
|
|
long toRead, numRead;
|
|
|
|
char* buffer = xMalloc (BufferSize, char);
|
|
|
|
long remaining = size;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
toRead = (0 < remaining && remaining < BufferSize) ?
|
|
|
|
remaining : BufferSize;
|
2011-03-05 22:40:50 +00:00
|
|
|
numRead = mio_read (fromMio, buffer, (size_t) 1, (size_t) toRead);
|
|
|
|
if (mio_write (toMio, buffer, (size_t)1, (size_t)numRead) < (size_t)numRead)
|
2005-11-22 12:26:26 +00:00
|
|
|
error (FATAL | PERROR, "cannot complete write");
|
|
|
|
if (remaining > 0)
|
|
|
|
remaining -= numRead;
|
|
|
|
} while (numRead == toRead && remaining != 0);
|
|
|
|
eFree (buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void copyFile (const char *const from, const char *const to, const long size)
|
|
|
|
{
|
2011-03-05 22:40:50 +00:00
|
|
|
MIO* const fromMio = mio_new_file_full (from, "rb", g_fopen, fclose);
|
|
|
|
if (fromMio == NULL)
|
2005-11-22 12:26:26 +00:00
|
|
|
error (FATAL | PERROR, "cannot open file to copy");
|
|
|
|
else
|
|
|
|
{
|
2011-03-05 22:40:50 +00:00
|
|
|
MIO* const toMio = mio_new_file_full (to, "wb", g_fopen, fclose);
|
|
|
|
if (toMio == NULL)
|
2005-11-22 12:26:26 +00:00
|
|
|
error (FATAL | PERROR, "cannot open copy destination");
|
|
|
|
else
|
|
|
|
{
|
2011-03-05 22:40:50 +00:00
|
|
|
copyBytes (fromMio, toMio, size);
|
|
|
|
mio_free (toMio);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2011-03-05 22:40:50 +00:00
|
|
|
mio_free (fromMio);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void openTagFile (void)
|
|
|
|
{
|
|
|
|
setDefaultTagFileName ();
|
|
|
|
TagsToStdout = isDestinationStdout ();
|
|
|
|
|
|
|
|
if (TagFile.vLine == NULL)
|
|
|
|
TagFile.vLine = vStringNew ();
|
|
|
|
|
|
|
|
/* Open the tags file.
|
|
|
|
*/
|
|
|
|
if (TagsToStdout)
|
2011-03-05 22:40:50 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = tempFile ("w", &TagFile.name);
|
|
|
|
TagFile.mio = mio_new_fp (fp, fclose);
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
boolean fileExists;
|
|
|
|
|
|
|
|
setDefaultTagFileName ();
|
|
|
|
TagFile.name = eStrdup (Option.tagFileName);
|
|
|
|
fileExists = doesFileExist (TagFile.name);
|
|
|
|
if (fileExists && ! isTagFile (TagFile.name))
|
|
|
|
error (FATAL,
|
|
|
|
"\"%s\" doesn't look like a tag file; I refuse to overwrite it.",
|
|
|
|
TagFile.name);
|
|
|
|
|
2011-03-05 22:40:50 +00:00
|
|
|
if (Option.append && fileExists)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2011-03-05 22:40:50 +00:00
|
|
|
TagFile.mio = mio_new_file_full (TagFile.name, "r+", g_fopen, fclose);
|
|
|
|
if (TagFile.mio != NULL)
|
|
|
|
{
|
|
|
|
TagFile.numTags.prev = updatePseudoTags (TagFile.mio);
|
|
|
|
mio_free (TagFile.mio);
|
|
|
|
TagFile.mio = mio_new_file_full (TagFile.name, "a+", g_fopen, fclose);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TagFile.mio = mio_new_file_full (TagFile.name, "w", g_fopen, fclose);
|
|
|
|
if (TagFile.mio != NULL)
|
|
|
|
addPseudoTags ();
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2006-06-24 14:42:56 +00:00
|
|
|
|
2011-03-05 22:40:50 +00:00
|
|
|
if (TagFile.mio == NULL)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
error (FATAL | PERROR, "cannot open tag file");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (TagsToStdout)
|
|
|
|
TagFile.directory = eStrdup (CurrentDirectory);
|
|
|
|
else
|
|
|
|
TagFile.directory = absoluteDirname (TagFile.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_REPLACEMENT_TRUNCATE
|
|
|
|
|
|
|
|
/* Replacement for missing library function.
|
|
|
|
*/
|
|
|
|
static int replacementTruncate (const char *const name, const long size)
|
|
|
|
{
|
|
|
|
char *tempName = NULL;
|
|
|
|
FILE *fp = tempFile ("w", &tempName);
|
|
|
|
fclose (fp);
|
|
|
|
copyFile (name, tempName, size);
|
|
|
|
copyFile (tempName, name, WHOLE_FILE);
|
|
|
|
remove (tempName);
|
|
|
|
eFree (tempName);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tag entry management
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
extern void makeTagEntry (const tagEntryInfo *const tag)
|
|
|
|
{
|
|
|
|
Assert (tag->name != NULL);
|
|
|
|
if (tag->name [0] == '\0')
|
|
|
|
error (WARNING, "ignoring null tag in %s", vStringValue (File.name));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int length = 0;
|
|
|
|
|
|
|
|
if (NULL != TagEntryFunction)
|
|
|
|
length = TagEntryFunction(tag);
|
|
|
|
|
|
|
|
++TagFile.numTags.added;
|
|
|
|
rememberMaxLengths (strlen (tag->name), (size_t) length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-18 21:42:38 +00:00
|
|
|
extern void setTagArglistByName (const char *tag_name, const char *arglist)
|
|
|
|
{
|
|
|
|
if (NULL != TagEntrySetArglistFunction)
|
|
|
|
TagEntrySetArglistFunction(tag_name, arglist);
|
|
|
|
}
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
extern void initTagEntry (tagEntryInfo *const e, const char *const name)
|
|
|
|
{
|
|
|
|
Assert (File.source.name != NULL);
|
|
|
|
memset (e, 0, sizeof (tagEntryInfo));
|
|
|
|
e->lineNumberEntry = (boolean) (Option.locate == EX_LINENUM);
|
|
|
|
e->lineNumber = getSourceLineNumber ();
|
|
|
|
e->language = getSourceLanguageName ();
|
|
|
|
e->filePosition = getInputFilePosition ();
|
|
|
|
e->sourceFileName = getSourceFileTagPath ();
|
|
|
|
e->name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vi:set tabstop=8 shiftwidth=4: */
|