entry.c 9.68 KB
Newer Older
Enrico Tröger's avatar
Enrico Tröger committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
*
*   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>
Enrico Tröger's avatar
Enrico Tröger committed
20
#include <glib/gstdio.h>
Enrico Tröger's avatar
Enrico Tröger committed
21 22 23 24 25 26 27

#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
28

Enrico Tröger's avatar
Enrico Tröger committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

/*  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)
{
130 131
    const int length = mio_printf (TagFile.mio, "%s%s\t%s\t/%s/\n",
				   PSEUDO_TAG_PREFIX, tagName, fileName, pattern);
Enrico Tröger's avatar
Enrico Tröger committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    ++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,
162
			      MIO *const mio, MIOPos startOfLine)
Enrico Tröger's avatar
Enrico Tröger committed
163 164 165 166 167 168 169 170 171
{
    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')
	{
172
	    MIOPos nextLine;
Enrico Tröger's avatar
Enrico Tröger committed
173

174
	    if (mio_getpos (mio, &nextLine) == -1 || mio_setpos (mio, &startOfLine) == -1)
Enrico Tröger's avatar
Enrico Tröger committed
175 176 177
		error (WARNING, "Failed to update 'sorted' pseudo-tag");
	    else
	    {
178
		MIOPos flagLocation;
Enrico Tröger's avatar
Enrico Tröger committed
179 180 181
		int c, d;

		do
182
		    c = mio_getc (mio);
Enrico Tröger's avatar
Enrico Tröger committed
183
		while (c != '\t'  &&  c != '\n');
184 185
		mio_getpos (mio, &flagLocation);
		d = mio_getc (mio);
Enrico Tröger's avatar
Enrico Tröger committed
186 187 188
		if (c == '\t'  &&  (d == '0'  ||  d == '1')  &&
		    d != (int) Option.sorted)
		{
189 190
		    mio_setpos (mio, &flagLocation);
		    mio_putc (mio, Option.sorted ? '1' : '0');
Enrico Tröger's avatar
Enrico Tröger committed
191
		}
192
		mio_setpos (mio, &nextLine);
Enrico Tröger's avatar
Enrico Tröger committed
193 194 195 196 197 198 199 200
	    }
	}
    }
}

/*  Look through all line beginning with "!_TAG_FILE", and update those which
 *  require it.
 */
201
static long unsigned int updatePseudoTags (MIO *const mio)
Enrico Tröger's avatar
Enrico Tröger committed
202 203 204 205
{
    enum { maxClassLength = 20 };
    char class [maxClassLength + 1];
    unsigned long linesRead = 0;
206
    MIOPos startOfLine;
Enrico Tröger's avatar
Enrico Tröger committed
207 208 209 210 211 212 213
    size_t classLength;
    const char *line;

    sprintf (class, "%sTAG_FILE", PSEUDO_TAG_PREFIX);
    classLength = strlen (class);
    Assert (classLength < maxClassLength);

214 215
    mio_getpos (mio, &startOfLine);
    line = readLine (TagFile.vLine, mio);
Enrico Tröger's avatar
Enrico Tröger committed
216 217 218 219 220 221 222 223 224 225 226
    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)
227
		    updateSortedFlag (line, mio, startOfLine);
Enrico Tröger's avatar
Enrico Tröger committed
228
	    }
229
	    mio_getpos (mio, &startOfLine);
Enrico Tröger's avatar
Enrico Tröger committed
230
	}
231
	line = readLine (TagFile.vLine, mio);
Enrico Tröger's avatar
Enrico Tröger committed
232 233 234 235
    }
    while (line != NULL)			/* skip to end of file */
    {
	++linesRead;
236
	line = readLine (TagFile.vLine, mio);
Enrico Tröger's avatar
Enrico Tröger committed
237 238 239 240 241 242 243 244 245 246 247 248 249
    }
    return linesRead;
}

/*
 *  Tag file management
 */



static boolean isTagFile (const char *const filename)
{
    boolean ok = FALSE;			/* we assume not unless confirmed */
250
    MIO *const mio = mio_new_file_full (filename, "rb", g_fopen, fclose);
Enrico Tröger's avatar
Enrico Tröger committed
251

252
    if (mio == NULL  &&  errno == ENOENT)
Enrico Tröger's avatar
Enrico Tröger committed
253
	ok = TRUE;
254
    else if (mio != NULL)
Enrico Tröger's avatar
Enrico Tröger committed
255
    {
256
	const char *line = readLine (TagFile.vLine, mio);
Enrico Tröger's avatar
Enrico Tröger committed
257 258 259

	if (line == NULL)
	    ok = TRUE;
260
	mio_free (mio);
Enrico Tröger's avatar
Enrico Tröger committed
261 262 263 264
    }
    return ok;
}

265
extern void copyBytes (MIO* const fromMio, MIO* const toMio, const long size)
Enrico Tröger's avatar
Enrico Tröger committed
266 267 268 269 270 271 272 273 274
{
    enum { BufferSize = 1000 };
    long toRead, numRead;
    char* buffer = xMalloc (BufferSize, char);
    long remaining = size;
    do
    {
	toRead = (0 < remaining && remaining < BufferSize) ?
		    remaining : BufferSize;
275 276
	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)
Enrico Tröger's avatar
Enrico Tröger committed
277 278 279 280 281 282 283 284 285
	    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)
{
286 287
    MIO* const fromMio = mio_new_file_full (from, "rb", g_fopen, fclose);
    if (fromMio == NULL)
Enrico Tröger's avatar
Enrico Tröger committed
288 289 290
	error (FATAL | PERROR, "cannot open file to copy");
    else
    {
291 292
	MIO* const toMio = mio_new_file_full (to, "wb", g_fopen, fclose);
	if (toMio == NULL)
Enrico Tröger's avatar
Enrico Tröger committed
293 294 295
	    error (FATAL | PERROR, "cannot open copy destination");
	else
	{
296 297
	    copyBytes (fromMio, toMio, size);
	    mio_free (toMio);
Enrico Tröger's avatar
Enrico Tröger committed
298
	}
299
	mio_free (fromMio);
Enrico Tröger's avatar
Enrico Tröger committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313
    }
}

extern void openTagFile (void)
{
    setDefaultTagFileName ();
    TagsToStdout = isDestinationStdout ();

    if (TagFile.vLine == NULL)
	TagFile.vLine = vStringNew ();

    /*  Open the tags file.
     */
    if (TagsToStdout)
314 315 316 317 318 319
    {
	FILE *fp;

	fp = tempFile ("w", &TagFile.name);
	TagFile.mio = mio_new_fp (fp, fclose);
    }
Enrico Tröger's avatar
Enrico Tröger committed
320 321 322 323 324 325 326 327 328 329 330 331
    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);

332
	if (Option.append  &&  fileExists)
Enrico Tröger's avatar
Enrico Tröger committed
333
	{
334 335 336 337 338 339 340 341 342 343 344 345 346
	    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 ();
Enrico Tröger's avatar
Enrico Tröger committed
347
	}
348

349
	if (TagFile.mio == NULL)
Enrico Tröger's avatar
Enrico Tröger committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	{
	    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);
    }
}

403 404 405 406 407 408
extern void setTagArglistByName (const char *tag_name, const char *arglist)
{
    if (NULL != TagEntrySetArglistFunction)
	TagEntrySetArglistFunction(tag_name, arglist);
}

Enrico Tröger's avatar
Enrico Tröger committed
409 410 411 412 413 414 415 416 417 418 419 420 421
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: */