1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 130 131 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
/*
*
* Copyright (c) 2001-2002, Biswapesh Chattopadhyay
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
*/
#include "general.h"
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#ifdef HAVE_FNMATCH_H
# include <fnmatch.h>
#endif
#include <glib/gstdio.h>
#include "options.h"
#define LIBCTAGS_DEFINED
#include "tm_tag.h"
#include "tm_workspace.h"
#include "tm_source_file.h"
#include "tm_file_entry.h"
#include "tm_project.h"
#define TM_FILE_NAME ".tm_project.cache"
static const char *s_sources[] = { "*.c", "*.pc" /* C/Pro*C files */
, "*.C", "*.cpp", "*.cc", "*.cxx", "*.c++" /* C++ files */
, "*.h", "*.hh", "*.hpp", "*.H", "*.h++", "*.i" /* Header files */
, "*.oaf", "*.gob", "*.idl" /* CORBA/Bonobo files */
, "*.l", "*.y" /* Lex/Yacc files */
#if 0
, "*.ui", "*.moc" /* KDE/QT Files */
, "*.glade" /* UI files */
#endif
, "*.java", "*.pl", "*.pm", "*.py", "*.sh" /* Other languages */
, NULL /* Must terminate with NULL */
};
static const char *s_ignore[] = { "CVS", "intl", "po", NULL };
static GList *glist_from_array(const char **arr)
{
GList *l = NULL;
int i;
for (i =0; arr[i]; ++ i)
l = g_list_prepend(l, (gpointer) arr[i]);
return g_list_reverse(l);
}
guint project_class_id = 0;
gboolean tm_project_init(TMProject *project, const char *dir
, const char **sources, const char **ignore, gboolean force)
{
struct stat s;
char *path;
g_return_val_if_fail((project && dir), FALSE);
#ifdef TM_DEBUG
g_message("Initializing project %s", dir);
#endif
if (0 == project_class_id)
{
project_class_id = tm_work_object_register(tm_project_free, tm_project_update
, tm_project_find_file);
}
if ((0 != g_stat(dir, &s)) || (!S_ISDIR(s.st_mode)))
{
g_warning("%s: Not a valid directory", dir);
return FALSE;
}
project->dir = tm_get_real_path(dir);
if (sources)
project->sources = sources;
else
project->sources = s_sources;
if (ignore)
project->ignore = ignore;
else
project->ignore = s_ignore;
project->file_list = NULL;
path = g_strdup_printf("%s/%s", project->dir, TM_FILE_NAME);
if ((0 != g_stat(path, &s)) || (0 == s.st_size))
force = TRUE;
if (FALSE == tm_work_object_init(&(project->work_object),
project_class_id, path, force))
{
g_warning("Unable to init project file %s", path);
g_free(project->dir);
g_free(path);
return FALSE;
}
if (! tm_workspace_add_object(TM_WORK_OBJECT(project)))
{
g_warning("Unable to init project file %s", path);
g_free(project->dir);
g_free(path);
return FALSE;
}
g_free(path);
tm_project_open(project, force);
if (!project->file_list || (0 == project->file_list->len))
tm_project_autoscan(project);
#ifdef TM_DEBUG
tm_workspace_dump();
#endif
return TRUE;
}
TMWorkObject *tm_project_new(const char *dir, const char **sources
, const char **ignore, gboolean force)
{
TMProject *project = g_new(TMProject, 1);
if (FALSE == tm_project_init(project, dir, sources, ignore, force))
{
g_free(project);
return NULL;
}
return (TMWorkObject *) project;
}
void tm_project_destroy(TMProject *project)
{
g_return_if_fail (project != NULL);
#ifdef TM_DEBUG
g_message("Destroying project: %s", project->work_object.file_name);
#endif
if (project->file_list)
{
guint i;
for (i = 0; i < project->file_list->len; ++i)
tm_source_file_free(project->file_list->pdata[i]);
g_ptr_array_free(project->file_list, TRUE);
}
tm_workspace_remove_object(TM_WORK_OBJECT(project), FALSE, TRUE);
g_free(project->dir);
tm_work_object_destroy(&(project->work_object));
}
void tm_project_free(gpointer project)
{
if (NULL != project)
{
tm_project_destroy(TM_PROJECT(project));
g_free(project);
}
}
gboolean tm_project_add_file(TMProject *project, const char *file_name
,gboolean update)
{
TMWorkObject *source_file;
const TMWorkObject *workspace = TM_WORK_OBJECT(tm_get_workspace());
char *path;
gboolean exists = FALSE;
g_return_val_if_fail((project && file_name), FALSE);
path = tm_get_real_path(file_name);
#ifdef TM_DEBUG
g_message("Adding %s to project", path);
#endif
/* Check if the file is already loaded in the workspace */
source_file = tm_workspace_find_object(TM_WORK_OBJECT(workspace), path, FALSE);
if (NULL != source_file)
{
if ((workspace == source_file->parent) || (NULL == source_file->parent))
{
#ifdef TM_DEBUG
g_message("%s moved from workspace to project", path);
#endif
tm_workspace_remove_object(source_file, FALSE, TRUE);
}
else if (TM_WORK_OBJECT(project) == source_file->parent)
{
#ifdef TM_DEBUG
g_message("%s already exists in project", path);
#endif
exists = TRUE;
}
else
{
g_warning("Source file %s is shared among projects - will be duplicated!", path);
source_file = NULL;
}
}
if (NULL == source_file)
{
if (NULL == (source_file = tm_source_file_new(file_name, TRUE, NULL)))
{
g_warning("Unable to create source file for file %s", file_name);
g_free(path);
return FALSE;
}
}
source_file->parent = TM_WORK_OBJECT(project);
if (NULL == project->file_list)
project->file_list = g_ptr_array_new();
if (!exists)
g_ptr_array_add(project->file_list, source_file);
TM_SOURCE_FILE(source_file)->inactive = FALSE;
if (update)
tm_project_update(TM_WORK_OBJECT(project), TRUE, FALSE, TRUE);
g_free(path);
return TRUE;
}
TMWorkObject *tm_project_find_file(TMWorkObject *work_object
, const char *file_name, gboolean name_only)
{
TMProject *project;
g_return_val_if_fail(work_object && file_name, NULL);
if (!IS_TM_PROJECT(work_object))
{
g_warning("Non project pointer passed to tm_project_find_file(%s)", file_name);
return NULL;
}
project = TM_PROJECT(work_object);
if ((NULL == project->file_list) || (0 == project->file_list->len))
return NULL;
else
{
guint i;
char *name, *name1;
if (name_only)
{
name = strrchr(file_name, '/');
if (name)
name = g_strdup(name + 1);
else
name = g_strdup(file_name);
}
else
name = tm_get_real_path(file_name);
for (i=0; i < project->file_list->len; ++i)
{
if (name_only)
name1 = TM_WORK_OBJECT(project->file_list->pdata[i])->short_name;
else
name1 = TM_WORK_OBJECT(project->file_list->pdata[i])->file_name;
if (0 == strcmp(name, name1))
{
g_free(name);
return TM_WORK_OBJECT(project->file_list->pdata[i]);
}
}
g_free(name);
}
return NULL;
}
gboolean tm_project_remove_object(TMProject *project, TMWorkObject *w)
{
guint i;
g_return_val_if_fail((project && w), FALSE);
if (!project->file_list)
return FALSE;
for (i=0; i < project->file_list->len; ++i)
{
if (w == project->file_list->pdata[i])
{
tm_work_object_free(w);
g_ptr_array_remove_index(project->file_list, i);
tm_project_update(TM_WORK_OBJECT(project), TRUE, FALSE, TRUE);
return TRUE;
}
}
return FALSE;
}
void tm_project_recreate_tags_array(TMProject *project)
{
guint i, j;
TMWorkObject *source_file;
g_return_if_fail(project);
#ifdef TM_DEBUG
g_message("Recreating tags for project: %s", project->work_object.file_name);
#endif
if (NULL != project->work_object.tags_array)
g_ptr_array_set_size(project->work_object.tags_array, 0);
else
project->work_object.tags_array = g_ptr_array_new();
if (!project->file_list)
return;
for (i=0; i < project->file_list->len; ++i)
{
source_file = TM_WORK_OBJECT(project->file_list->pdata[i]);
if ((NULL != source_file) && !(TM_SOURCE_FILE(source_file)->inactive) &&
(NULL != source_file->tags_array) && (source_file->tags_array->len > 0))
{
for (j = 0; j < source_file->tags_array->len; ++j)
{
g_ptr_array_add(project->work_object.tags_array,
source_file->tags_array->pdata[j]);
}
}
}
tm_tags_sort(project->work_object.tags_array, NULL, FALSE);
}
gboolean tm_project_update(TMWorkObject *work_object, gboolean force
, gboolean recurse, gboolean update_parent)
{
TMProject *project;
guint i;
gboolean update_tags = force;
if (!work_object || !IS_TM_PROJECT(work_object))
{
g_warning("Non project pointer passed to project update");
return FALSE;
}
#ifdef TM_DEBUG
g_message("Updating project: %s", work_object->file_name);
#endif
project = TM_PROJECT(work_object);
if ((NULL != project->file_list) && (project->file_list->len > 0))
{
if (recurse)
{
for (i=0; i < project->file_list->len; ++i)
{
if (TRUE == tm_source_file_update(TM_WORK_OBJECT(
project->file_list->pdata[i]), FALSE, FALSE, FALSE))
update_tags = TRUE;
}
}
if (update_tags || (TM_WORK_OBJECT (project)->tags_array == NULL))
{
#ifdef TM_DEBUG
g_message ("Tags updated, recreating tags array");
#endif
tm_project_recreate_tags_array(project);
}
}
/* work_object->analyze_time = time(NULL); */
if ((work_object->parent) && (update_parent))
tm_workspace_update(work_object->parent, TRUE, FALSE, FALSE);
return update_tags;
}
#define IGNORE_FILE ".tm_ignore"
static void tm_project_set_ignorelist(TMProject *project)
{
struct stat s;
char *ignore_file = g_strconcat(project->dir, "/", IGNORE_FILE, NULL);
if (0 == g_stat(ignore_file, &s))
{
if (NULL != Option.ignore)
stringListClear(Option.ignore);
addIgnoreListFromFile(ignore_file);
}
g_free(ignore_file);
}
gboolean tm_project_open(TMProject *project, gboolean force)
{
FILE *fp;
TMSourceFile *source_file = NULL;
TMTag *tag;
if (!project || !IS_TM_PROJECT(TM_WORK_OBJECT(project)))
return FALSE;
#ifdef TM_DEBUG
g_message("Opening project %s", project->work_object.file_name);
#endif
tm_project_set_ignorelist(project);
if (NULL == (fp = g_fopen(project->work_object.file_name, "r")))
return FALSE;
while (NULL != (tag = tm_tag_new_from_file(source_file, fp, 0, FALSE)))
{
if (tm_tag_file_t == tag->type)
{
if (!(source_file = TM_SOURCE_FILE(
tm_source_file_new(tag->name, FALSE, NULL))))
{
#ifdef TM_DEBUG
g_warning("Unable to create source file %s", tag->name);
#endif
if (!force)
{
tm_tag_unref(tag);
fclose(fp);
return FALSE;
}
else
source_file = NULL;
}
else
{
source_file->work_object.parent = TM_WORK_OBJECT(project);
source_file->lang = tag->atts.file.lang;
source_file->inactive = tag->atts.file.inactive;
if (!project->file_list)
project->file_list = g_ptr_array_new();
g_ptr_array_add(project->file_list, source_file);
}
tm_tag_unref(tag);
}
else
{
if ((NULL == source_file) || (source_file->inactive)) /* Dangling tag */
{
#ifdef TM_DEBUG
g_warning("Dangling tag %s", tag->name);
#endif
tm_tag_unref(tag);
if (!force)
{
fclose(fp);
return FALSE;
}
}
else
{
if (NULL == source_file->work_object.tags_array)
source_file->work_object.tags_array = g_ptr_array_new();
g_ptr_array_add(source_file->work_object.tags_array, tag);
#ifdef TM_DEBUG
g_message ("Added tag %s", tag->name);
#endif
}
}
}
fclose(fp);
tm_project_update((TMWorkObject *) project, FALSE, TRUE, TRUE);
return TRUE;
}
gboolean tm_project_save(TMProject *project)
{
guint i;
FILE *fp;
if (!project)
return FALSE;
if (NULL == (fp = g_fopen(project->work_object.file_name, "w")))
{
g_warning("Unable to save project %s", project->work_object.file_name);
return FALSE;
}
if (project->file_list)
{
for (i=0; i < project->file_list->len; ++i)
{
if (FALSE == tm_source_file_write(TM_WORK_OBJECT(project->file_list->pdata[i])
, fp, tm_tag_attr_max_t))
{
fclose(fp);
return FALSE;
}
}
}
fclose(fp);
return TRUE;
}
static void tm_project_add_file_recursive(TMFileEntry *entry
, gpointer user_data, guint __unused__ level)
{
TMProject *project;
if (!user_data || !entry || (tm_file_dir_t == entry->type))
return;
project = TM_PROJECT(user_data);
tm_project_add_file(project, entry->path, FALSE);
}
gboolean tm_project_autoscan(TMProject *project)
{
TMFileEntry *root_dir;
GList *file_match;
GList *dir_unmatch;
file_match = glist_from_array(project->sources);
dir_unmatch = glist_from_array(project->ignore);
if (!project || !IS_TM_PROJECT(TM_WORK_OBJECT(project))
|| (!project->dir))
return FALSE;
if (!(root_dir = tm_file_entry_new(project->dir, NULL, TRUE
, file_match, NULL, NULL, dir_unmatch, TRUE, TRUE)))
{
g_warning("Unable to create file entry");
return FALSE;
}
g_list_free(file_match);
g_list_free(dir_unmatch);
tm_file_entry_foreach(root_dir, tm_project_add_file_recursive
, project, 0, FALSE);
tm_file_entry_free(root_dir);
tm_project_update(TM_WORK_OBJECT(project), TRUE, FALSE, TRUE);
return TRUE;
}
gboolean tm_project_sync(TMProject *project, GList *files)
{
GList *tmp;
guint i;
if (project->file_list)
{
for (i = 0; i < project->file_list->len; ++i)
tm_source_file_free(project->file_list->pdata[i]);
g_ptr_array_free(project->file_list, TRUE);
project->file_list = NULL;
if (project->work_object.tags_array)
{
g_ptr_array_free(project->work_object.tags_array, TRUE);
project->work_object.tags_array = NULL;
}
}
for (tmp = files; tmp; tmp = g_list_next(tmp))
{
tm_project_add_file(project, (const char *) tmp->data, FALSE);
}
tm_project_update(TM_WORK_OBJECT(project), TRUE, FALSE, TRUE);
return TRUE;
}
void tm_project_dump(const TMProject *p)
{
if (p)
{
tm_work_object_dump(TM_WORK_OBJECT(p));
if (p->file_list)
{
guint i;
for (i=0; i < p->file_list->len; ++i)
{
fprintf(stderr, "->\t");
tm_work_object_dump(TM_WORK_OBJECT(p->file_list->pdata[i]));
}
}
fprintf(stderr, "-------------------------\n");
}
}
gboolean tm_project_is_source_file(TMProject *project, const char *file_name)
{
const char **pr_extn;
if (!(project && IS_TM_PROJECT(TM_WORK_OBJECT(project))
&& file_name && project->sources))
return FALSE;
for (pr_extn = project->sources; pr_extn && *pr_extn; ++ pr_extn)
{
if (0 == fnmatch(*pr_extn, file_name, 0))
return TRUE;
}
return FALSE;
}
|