2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2012-08-21 03:03:52 +02:00
|
|
|
gba.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-08-21 03:03:52 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-08-21 03:03:52 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-08-21 03:03:52 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBA_C
|
|
|
|
|
|
|
|
#include "config.h"
|
2013-05-25 15:23:39 +02:00
|
|
|
#include "trunk_version.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gb_str.h"
|
|
|
|
#include "gb_file.h"
|
|
|
|
#include "gb_array.h"
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
|
|
|
|
#include "gbc_archive.h"
|
|
|
|
|
2009-05-15 19:57:29 +02:00
|
|
|
#if HAVE_GETOPT_LONG
|
2007-12-30 17:41:49 +01:00
|
|
|
static struct option Long_options[] =
|
|
|
|
{
|
2012-08-21 03:03:52 +02:00
|
|
|
{ "version", 0, NULL, 'V' },
|
|
|
|
{ "help", 0, NULL, 'h' },
|
|
|
|
{ "swap", 0, NULL, 's' },
|
|
|
|
{ "verbose", 0, NULL, 'v' },
|
|
|
|
{ "output", 1, NULL, 'o' },
|
|
|
|
{ "extract", 1, NULL, 'x' },
|
|
|
|
{ 0 }
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char **path_list;
|
|
|
|
static int path_current;
|
|
|
|
|
2012-10-10 16:58:14 +02:00
|
|
|
static const char *allowed_hidden_files[] = { ".gambas", ".info", ".list", ".lang", ".action", ".connection", ".component", ".public", NULL };
|
2010-10-31 23:05:29 +01:00
|
|
|
//static const char *remove_ext_root[] = { "module", "class", "form", "gambas", NULL };
|
2007-12-30 17:41:49 +01:00
|
|
|
static const char *remove_ext_lang[] = { "pot", "po", NULL };
|
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
static bool _extract = FALSE;
|
|
|
|
static char *_extract_archive;
|
|
|
|
static char *_extract_file = NULL;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static void get_arguments(int argc, char **argv)
|
|
|
|
{
|
2012-08-21 03:03:52 +02:00
|
|
|
int opt;
|
|
|
|
#if HAVE_GETOPT_LONG
|
|
|
|
int index = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
#if HAVE_GETOPT_LONG
|
|
|
|
opt = getopt_long(argc, argv, "vVLhso:x:", Long_options, &index);
|
|
|
|
#else
|
|
|
|
opt = getopt(argc, argv, "vVLhso:x:");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (opt < 0) break;
|
|
|
|
|
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 'V':
|
2013-05-23 01:03:48 +02:00
|
|
|
#ifdef TRUNK_VERSION
|
2017-08-15 11:35:11 +02:00
|
|
|
#ifdef TRUNK_VERSION_GIT
|
|
|
|
printf(VERSION " " TRUNK_VERSION "\n");
|
|
|
|
#else /* from svn */
|
2013-05-23 01:03:48 +02:00
|
|
|
printf(VERSION " r" TRUNK_VERSION "\n");
|
2017-08-15 11:35:11 +02:00
|
|
|
#endif
|
|
|
|
#else /* no TRUNK_VERSION */
|
2012-08-21 03:03:52 +02:00
|
|
|
printf(VERSION "\n");
|
2013-05-23 01:03:48 +02:00
|
|
|
#endif
|
2012-08-21 03:03:52 +02:00
|
|
|
exit(0);
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
ARCH_verbose = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
ARCH_swap = TRUE;
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
case 'o':
|
2012-08-21 03:03:52 +02:00
|
|
|
ARCH_define_output(optarg);
|
2010-04-25 12:46:57 +02:00
|
|
|
_extract = FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
2012-08-21 03:03:52 +02:00
|
|
|
_extract_archive = optarg;
|
2010-04-25 12:46:57 +02:00
|
|
|
_extract = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
break;
|
2010-04-25 12:46:57 +02:00
|
|
|
|
|
|
|
case 'L':
|
|
|
|
printf(
|
2012-08-21 03:03:52 +02:00
|
|
|
"\nGAMBAS Archiver version " VERSION " " __DATE__ " " __TIME__ "\n"
|
2010-04-25 12:46:57 +02:00
|
|
|
COPYRIGHT
|
|
|
|
);
|
|
|
|
exit(0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-08-21 03:03:52 +02:00
|
|
|
case 'h': case '?':
|
|
|
|
printf(
|
|
|
|
"\nCreate a standalone one-file executable from a Gambas project.\n"
|
|
|
|
"Or extract a specific file from a Gambas executable (-x option).\n"
|
|
|
|
"\nUsage: gba" GAMBAS_VERSION_STRING " [options] [<project directory>]\n"
|
|
|
|
" gba" GAMBAS_VERSION_STRING " -x <archive path> <file>\n\n"
|
|
|
|
"Options:"
|
|
|
|
#if HAVE_GETOPT_LONG
|
|
|
|
"\n"
|
|
|
|
" -o --output=ARCHIVE archive path [<project directory>/<project name>.gambas]\n"
|
|
|
|
" -x --extract=ARCHIVE archive path\n"
|
|
|
|
" -v --verbose verbose output\n"
|
|
|
|
" -s --swap swap endianness\n"
|
|
|
|
" -V --version display version\n"
|
|
|
|
" -L --license display license\n"
|
|
|
|
" -h --help display this help\n"
|
|
|
|
#else
|
|
|
|
" (no long options on this system)\n"
|
|
|
|
" -o=ARCHIVE archive path [<project directory>/<project name>.gambas]\n"
|
|
|
|
" -x=ARCHIVE archive path\n"
|
|
|
|
" -v verbose output\n"
|
|
|
|
" -s swap endianness\n"
|
|
|
|
" -V display version\n"
|
|
|
|
" -L display license\n"
|
|
|
|
" -h display this help\n"
|
|
|
|
#endif
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
default:
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < (argc - 1))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "gba: too many arguments.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
if (_extract)
|
|
|
|
{
|
|
|
|
if (optind == argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "gba: not enough arguments.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
_extract_file = argv[optind];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (optind == argc)
|
|
|
|
ARCH_define_project(NULL);
|
|
|
|
else
|
|
|
|
ARCH_define_project(argv[optind]);
|
|
|
|
|
|
|
|
if (!FILE_exist(ARCH_project))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "gba: project file not found: %s\n", ARCH_project);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void path_add(const char *path)
|
|
|
|
{
|
2012-08-21 03:03:52 +02:00
|
|
|
*((char **)ARRAY_add(&path_list)) = STR_copy(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void path_init(const char *first)
|
|
|
|
{
|
2012-08-21 03:03:52 +02:00
|
|
|
ARRAY_create(&path_list);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-08-21 03:03:52 +02:00
|
|
|
if (*first)
|
|
|
|
FILE_chdir(first);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-08-21 03:03:52 +02:00
|
|
|
path_add(FILE_get_current_dir());
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-08-21 03:03:52 +02:00
|
|
|
path_current = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void path_exit(void)
|
|
|
|
{
|
2012-08-21 03:03:52 +02:00
|
|
|
ARRAY_delete(&path_list);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
static int path_count(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-08-21 03:03:52 +02:00
|
|
|
return ARRAY_count(path_list);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-08-21 03:03:52 +02:00
|
|
|
const char *path;
|
2017-11-19 02:29:59 +01:00
|
|
|
int nfile;
|
|
|
|
struct dirent **filelist;
|
2012-08-21 03:03:52 +02:00
|
|
|
struct dirent *dirent;
|
|
|
|
char *file_name;
|
|
|
|
const char *file;
|
|
|
|
struct stat info;
|
|
|
|
const char *ext;
|
|
|
|
int len;
|
|
|
|
const char **p;
|
|
|
|
int len_prefix;
|
|
|
|
const char **remove_ext;
|
2010-04-25 12:46:57 +02:00
|
|
|
ARCH *arch;
|
|
|
|
ARCH_FIND find;
|
2017-11-19 02:29:59 +01:00
|
|
|
int i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-08-21 03:03:52 +02:00
|
|
|
get_arguments(argc, argv);
|
|
|
|
COMMON_init();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-08-21 03:03:52 +02:00
|
|
|
TRY
|
|
|
|
{
|
2010-04-25 12:46:57 +02:00
|
|
|
if (_extract) // Extract a file from an archive
|
|
|
|
{
|
|
|
|
arch = ARCH_open(_extract_archive);
|
|
|
|
|
|
|
|
if (ARCH_find(arch, _extract_file, 0, &find))
|
|
|
|
fprintf(stderr, "gba: file not found in archive\n");
|
|
|
|
else
|
|
|
|
fwrite(&arch->addr[find.pos], sizeof(char), find.len, stdout);
|
|
|
|
|
|
|
|
ARCH_close(arch);
|
|
|
|
}
|
|
|
|
else // Create an archive
|
|
|
|
{
|
|
|
|
ARCH_init();
|
|
|
|
|
|
|
|
file = FILE_get_dir(ARCH_project);
|
|
|
|
len_prefix = strlen(file);
|
|
|
|
path_init(file);
|
|
|
|
|
2010-07-12 01:13:11 +02:00
|
|
|
/* .startup and .project file always first ! */
|
|
|
|
|
|
|
|
path = FILE_cat(FILE_get_dir(ARCH_project), ".startup", NULL);
|
|
|
|
if (FILE_exist(path)) ARCH_add_file(path);
|
|
|
|
|
|
|
|
path = FILE_cat(FILE_get_dir(ARCH_project), ".project", NULL);
|
|
|
|
if (FILE_exist(path)) ARCH_add_file(path);
|
2010-04-25 12:46:57 +02:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (path_current >= path_count())
|
|
|
|
break;
|
|
|
|
|
|
|
|
path = path_list[path_current++];
|
2017-11-19 02:29:59 +01:00
|
|
|
|
|
|
|
if (chdir(path) != 0)
|
2010-04-25 12:46:57 +02:00
|
|
|
{
|
2017-11-19 02:29:59 +01:00
|
|
|
fprintf(stderr, "gba: warning: cannot change to directory: %s\n", path);
|
2010-04-25 12:46:57 +02:00
|
|
|
goto _NEXT_PATH;
|
|
|
|
}
|
|
|
|
|
2017-11-19 02:29:59 +01:00
|
|
|
filelist = NULL;
|
|
|
|
nfile = scandir(path, &filelist, NULL, alphasort);
|
|
|
|
|
|
|
|
if (nfile < 0)
|
2010-04-25 12:46:57 +02:00
|
|
|
{
|
2017-11-19 02:29:59 +01:00
|
|
|
fprintf(stderr, "gba: warning: cannot scan directory: %s\n", path);
|
2010-04-25 12:46:57 +02:00
|
|
|
goto _NEXT_PATH;
|
|
|
|
}
|
2017-11-19 02:29:59 +01:00
|
|
|
|
|
|
|
for (i = 0; i < nfile; i++)
|
2010-04-25 12:46:57 +02:00
|
|
|
{
|
2017-11-19 02:29:59 +01:00
|
|
|
dirent = filelist[i];
|
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
file_name = dirent->d_name;
|
|
|
|
len = strlen(file_name);
|
|
|
|
|
|
|
|
if (*file_name == '.')
|
|
|
|
{
|
|
|
|
for (p = allowed_hidden_files; *p; p++)
|
|
|
|
{
|
|
|
|
if (strcmp(file_name, *p) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*p == NULL)
|
|
|
|
continue;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
if (file_name[len - 1] == '~')
|
|
|
|
continue;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
//if (strcmp(file_name, ARCH_project_name) == 0)
|
|
|
|
// continue;
|
2011-04-30 14:53:09 +02:00
|
|
|
|
|
|
|
if ((len == 4) && (strncmp(file_name, "core", 4) == 0))
|
|
|
|
continue;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
if ((len > 5) && (strncmp(file_name, "core.", 5) == 0))
|
|
|
|
continue;
|
|
|
|
|
2010-06-13 13:04:57 +02:00
|
|
|
if ((len > 7) && (strncmp(file_name, "vgcore.", 5) == 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((len > 10) && (strncmp(file_name, "callgrind.", 5) == 0))
|
|
|
|
continue;
|
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
file = FILE_cat(path, file_name, NULL);
|
2010-10-31 23:05:29 +01:00
|
|
|
|
|
|
|
// Do not put the archive file inside itself.
|
|
|
|
if (!strcmp(file, ARCH_output))
|
|
|
|
continue;
|
2010-04-25 12:46:57 +02:00
|
|
|
|
|
|
|
if (stat(file_name, &info))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "gba: warning: cannot stat file: %s\n", file);
|
|
|
|
continue;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
if (S_ISDIR(info.st_mode))
|
|
|
|
{
|
|
|
|
if (strcmp(file_name, "CVS") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
path_add(file);
|
|
|
|
ARCH_add_file(file);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
2010-04-25 12:46:57 +02:00
|
|
|
{
|
|
|
|
ext = FILE_get_ext(file_name);
|
|
|
|
|
|
|
|
//printf("path = %s\n", &path[len_prefix]);
|
|
|
|
|
2010-10-31 23:05:29 +01:00
|
|
|
//if (path[len_prefix] == 0)
|
|
|
|
// remove_ext = remove_ext_root;
|
|
|
|
if (strcmp(&path[len_prefix], "/.lang") == 0)
|
2010-04-25 12:46:57 +02:00
|
|
|
remove_ext = remove_ext_lang;
|
|
|
|
else
|
|
|
|
remove_ext = 0;
|
|
|
|
|
|
|
|
if (remove_ext)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-04-25 12:46:57 +02:00
|
|
|
for (p = remove_ext; *p; p++)
|
|
|
|
{
|
|
|
|
if (strcasecmp(ext, *p) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*p != NULL)
|
|
|
|
continue;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2010-10-31 23:05:29 +01:00
|
|
|
|
|
|
|
if (strcmp(ext, "gambas") == 0)
|
|
|
|
continue;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
ARCH_add_file(file);
|
|
|
|
}
|
2017-11-19 23:18:23 +01:00
|
|
|
|
|
|
|
free(dirent);
|
2010-04-25 12:46:57 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
_NEXT_PATH:
|
2017-11-19 02:29:59 +01:00
|
|
|
if (filelist != NULL)
|
|
|
|
free(filelist);
|
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE((char **)&path);
|
2010-04-25 12:46:57 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
path_exit();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
ARCH_exit();
|
|
|
|
/*MEM_check();*/
|
|
|
|
}
|
2012-08-21 03:03:52 +02:00
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
fflush(NULL);
|
|
|
|
fprintf(stderr, "gba: ERROR: ");
|
|
|
|
ERROR_print();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
|
|
|
return 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|