Beispiel #1
0
static void run_collect(COLLECT_CALLBACK_INFO *info, const char *input)
{
    char dir[1024];
    int dirlen = 0;

    /* get the directory */
    path_directory(input, dir, sizeof(dir));
    dirlen = strlen(dir);
    info->path_len = dirlen+1;

    /* set the start string */
    if(dirlen)
        info->start_str = input + dirlen + 1;
    else
        info->start_str = input;

    for(info->start_len = 0; info->start_str[info->start_len]; info->start_len++)
    {
        if(info->start_str[info->start_len] == '*')
            break;
    }

    /* set the end string */
    if(info->start_str[info->start_len])
        info->end_str = info->start_str + info->start_len + 1;
    else
        info->end_str = info->start_str + info->start_len;
    info->end_len = strlen(info->end_str);

    /* search the path */
    list_directory(dir, collect_callback, info);
}
Beispiel #2
0
void init_filepathstatic(/*out*/filepath_static_t* fpath, const struct directory_t* workdir, const char* filename)
{
   int err;

   if (  workdir
         && (  !filename
               || filename[0] != '/')) {
      wbuffer_t path = wbuffer_INIT_STATIC(sizeof(fpath->workdir)-2, (uint8_t*)fpath->workdir);
      err = path_directory(workdir, &path);
      (void) PROCESS_testerrortimer(&s_filepathstatic_errtimer, &err);
      if (err) {
         memcpy(fpath->workdir, "???ERR/", sizeof("???ERR/")/*include trailing \0*/);
      } else {
         size_t size = size_wbuffer(&path)-1/*\0 bytes*/;
         if (fpath->workdir[size-1] != '/') {
            fpath->workdir[size] = '/';
            ++size;
         }
         fpath->workdir[size] = 0;
      }
   } else {
      fpath->workdir[0] = 0;
   }

   fpath->filename = filename ? filename : "";
}
Beispiel #3
0
/* like file_createdir, but automatically creates all top-level directories needed
	If you feed it "output/somefiles/output.o" it will create "output/somefiles"
*/	
int file_createpath(const char *output_name)
{
	char buffer[MAX_PATH_LENGTH];
	int i;
	char t;
	
	/* fish out the directory */
	if(path_directory(output_name, buffer, sizeof(buffer)) != 0)
	{
		fprintf(stderr, "path error: %s\n", buffer);
		return -1;
	}
	
	/* no directory in path */
	if(buffer[0] == 0)
		return 0;
	
	/* check if we need to do a deep walk */
	if(file_createdir(buffer) == 0)
		return 0;
	
	/* create dir by doing a deep walk */
	i = 0;
	while(1)
	{
		if((buffer[i] == '/') || (buffer[i] == 0))
		{
			/* insert null terminator */
			t = buffer[i];
			buffer[i] = 0;
			
			if(file_createdir(buffer) != 0)
			{
				fprintf(stderr, "path error2: %s\n", buffer);
				return -1;
			}
			
			/* restore the path */
			buffer[i] = t;
		}
		
		if(buffer[i] == 0)
			break;
		
		i++;
	}
	
	/* return success */
	return 0;
}
Beispiel #4
0
static int test_filepathstatic(void)
{
   filepath_static_t fpath;
   const char*       F;
   cstring_t         workpath   = cstring_INIT;
   directory_t*      workdir    = 0;
   char              fullpath[sys_path_MAXSIZE];

   // prepare
   TEST(0 == new_directory(&workdir, "", 0));
   TEST(0 == path_directory(workdir, &(wbuffer_t) wbuffer_INIT_CSTRING(&workpath)));
   adaptsize_cstring(&workpath);

   // TEST init_filepathstatic: workdir == 0 && filename == 0
   memset(&fpath, 255, sizeof(fpath));
   init_filepathstatic(&fpath, 0, 0);
   TEST(0 == fpath.workdir[0]);
   TEST(0 != fpath.filename);
   TEST(0 == fpath.filename[0]);

   // TEST init_filepathstatic: workdir == 0
   memset(&fpath, 255, sizeof(fpath));
   F = "test-filename";
   init_filepathstatic(&fpath, 0, F);
   TEST(0 == fpath.workdir[0]);
   TEST(F == fpath.filename);

   // TEST init_filepathstatic: filename == 0
   memset(&fpath, 255, sizeof(fpath));
   init_filepathstatic(&fpath, workdir, 0);
   TEST('/' == fpath.workdir[size_cstring(&workpath)]);
   TEST(0 == fpath.workdir[size_cstring(&workpath)+1]);
   TEST(0 == memcmp(fpath.workdir, str_cstring(&workpath), size_cstring(&workpath)));
   TEST(0 != fpath.filename);
   TEST(0 == fpath.filename[0]);

   // TEST init_filepathstatic: filename[0] != '/'
   memset(&fpath, 255, sizeof(fpath));
   F = "test-filename";
   init_filepathstatic(&fpath, workdir, F);
   TEST('/' == fpath.workdir[size_cstring(&workpath)]);
   TEST(0 == fpath.workdir[size_cstring(&workpath)+1]);
   TEST(0 == memcmp(fpath.workdir, str_cstring(&workpath), size_cstring(&workpath)));
   TEST(F == fpath.filename);

   // TEST init_filepathstatic: absolute filename (filename[0] == '/')
   memset(&fpath, 255, sizeof(fpath));
   F = "/tmp/test-filename";
   init_filepathstatic(&fpath, workdir, F);
   TEST(0 == fpath.workdir[0]);
   TEST(F == fpath.filename);

   // TEST init_filepathstatic: ERROR
   init_testerrortimer(&s_filepathstatic_errtimer, 1, ENOENT);
   F = "test-filename";
   init_filepathstatic(&fpath, workdir, F);
   TEST(0 == strcmp("???ERR/", fpath.workdir));
   TEST(F == fpath.filename);

   // TEST STRPARAM_filepathstatic
   F = "test-filename";
   init_filepathstatic(&fpath, workdir, F);
   memset(fullpath, 255, sizeof(fullpath));
   snprintf(fullpath, sizeof(fullpath), "%s%s", STRPARAM_filepathstatic(&fpath));
   TEST(0 == strncmp(fullpath, str_cstring(&workpath), size_cstring(&workpath)));
   TEST('/' == fullpath[size_cstring(&workpath)]);
   TEST(0 == strcmp(fullpath + size_cstring(&workpath) + 1, F));

   // unprepare
   TEST(0 == free_cstring(&workpath));
   TEST(0 == delete_directory(&workdir));

   return 0;
ONERR:
   free_cstring(&workpath);
   delete_directory(&workdir);
   return EINVAL;
}
Beispiel #5
0
Datei: main.c Projekt: malind/bam
static int bam_setup(struct CONTEXT *context, const char *scriptfile, const char **targets, int num_targets)
{
	/* */	
	if(session.verbose)
		printf("%s: setup started\n", session.name);
	
	/* set filename */
	context->filename = scriptfile;
	
	/* set global timestamp to the script file */
	context->globaltimestamp = file_timestamp(scriptfile);

	/* */
	context->forced = option_force;
	
	/* fetch script directory */
	{
		char cwd[MAX_PATH_LENGTH];
		char path[MAX_PATH_LENGTH];

		if(!getcwd(cwd, sizeof(cwd)))
		{
			printf("%s: error: couldn't get current working directory\n", session.name);
			return -1;
		}
		
		if(path_directory(context->filename, path, sizeof(path)))
		{
			printf("%s: error: path too long '%s'\n", session.name, path);
			return -1;
		}
		
		if(path_join(cwd, -1, path, -1, context->script_directory, sizeof(context->script_directory)))
		{
			printf("%s: error: path too long when joining '%s' and '%s'\n", session.name, cwd, path);
			return -1;
		}
	}
	
	/* register all functions */
	event_begin(0, "lua setup", NULL);
	if(register_lua_globals(context->lua, context->script_directory, context->filename) != 0)
	{
		printf("%s: error: registering of lua functions failed\n", session.name);
		return -1;
	}
	event_end(0, "lua setup", NULL);

	/* load script */	
	if(session.verbose)
		printf("%s: reading script from '%s'\n", session.name, scriptfile);

	event_begin(0, "script load", NULL);
	/* push error function to stack and load the script */
	lua_getglobal(context->lua, "errorfunc");
	switch(luaL_loadfile(context->lua, scriptfile))
	{
		case 0: break;
		case LUA_ERRSYNTAX:
			lf_errorfunc(context->lua);
			return -1;
		case LUA_ERRMEM:
			printf("%s: memory allocation error\n", session.name);
			return -1;
		case LUA_ERRFILE:
			printf("%s: error opening '%s'\n", session.name, scriptfile);
			return -1;
		default:
			printf("%s: unknown error\n", session.name);
			return -1;
	}
	event_end(0, "script load", NULL);

	/* start the background stat thread */
	node_graph_start_statthread(context->graph);

	/* call the code chunk */
	event_begin(0, "script run", NULL);
	if(lua_pcall(context->lua, 0, LUA_MULTRET, -2) != 0)
	{
		node_graph_end_statthread(context->graph);
		printf("%s: script error (-t for more detail)\n", session.name);
		return -1;
	}
	event_end(0, "script run", NULL);

	/* stop the background stat thread */
	event_begin(0, "stat", NULL);
	node_graph_end_statthread(context->graph);
	event_end(0, "stat", NULL);
	
	/* run deferred functions */
	event_begin(0, "deferred cpp dependencies", NULL);
	if(run_deferred_functions(context, context->firstdeferred_cpp) != 0)
		return -1;
	event_end(0, "deferred cpp dependencies", NULL);
		
	event_begin(0, "deferred search dependencies", NULL);
	if(run_deferred_functions(context, context->firstdeferred_search) != 0)
		return -1;
	event_end(0, "deferred search dependencies", NULL);

	/* */	
	if(session.verbose)
		printf("%s: making build target\n", session.name);
	
	/* make build target */
	{
		struct NODE *node;
		int all_target = 0;
		int i;

		if(node_create(&context->target, context->graph, "_bam_buildtarget", NULL, TIMESTAMP_PSEUDO))
			return -1;

		if(num_targets)
		{
			/* search for all target */
			for(i = 0; i < num_targets; i++)
			{
				if(strcmp(targets[i], "all") == 0)
				{
					all_target = 1;
					break;
				}
			}
		}
		
		/* default too all if we have no targets or default target */
		if(num_targets == 0 && !context->defaulttarget)
			all_target = 1;
		
		if(all_target)
		{
			/* build the all target */
			for(node = context->graph->first; node; node = node->next)
			{
				if(node->firstparent == NULL && node != context->target)
				{
					if(!node_add_dependency (context->target, node))
						return -1;
				}
			}
		}
		else
		{
			if(num_targets)
			{
				for(i = 0; i < num_targets; i++)
				{
					struct NODE *node = node_find(context->graph, targets[i]);
					if(!node)
					{
						printf("%s: target '%s' not found\n", session.name, targets[i]);
						return -1;
					}
					
					if(option_dependent)
					{
						/* TODO: this should perhaps do a reverse walk up in the tree to
							find all dependent node with commandline */
						struct NODELINK *parent;
						for(parent = node->firstparent; parent; parent = parent->next)
						{
							if(!node_add_dependency (context->target, parent->node))
								return -1;
						}
								
					}
					else
					{
						if(!node_add_dependency (context->target, node))
							return -1;
					}
				}
			}
			else
			{
				if(!node_add_dependency (context->target, context->defaulttarget))
					return -1;
			}

		}
	}

	/* zero out the global timestamp if we don't want to use it */
	if(option_no_scripttimestamp)
		context->globaltimestamp = 0;

	/* */	
	if(session.verbose)
		printf("%s: setup done\n", session.name);
	
	/* return success */
	return 0;
}