2002-05-13 19:37:36 +00:00
|
|
|
/*
|
|
|
|
#FILENAME#
|
|
|
|
|
|
|
|
#DESCRIPTION#
|
|
|
|
|
|
|
|
Copyright (C) 2001 #AUTHOR#
|
|
|
|
|
|
|
|
Author: #AUTHOR#
|
|
|
|
Date: #DATE#
|
|
|
|
|
|
|
|
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
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-01-15 15:31:36 +00:00
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2002-05-13 19:37:36 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
# include <io.h>
|
|
|
|
#endif
|
2003-03-12 22:31:44 +00:00
|
|
|
#include <string.h>
|
2002-05-16 23:44:53 +00:00
|
|
|
#include <getopt.h>
|
2002-05-13 19:37:36 +00:00
|
|
|
#include <sys/types.h>
|
2004-01-20 05:57:39 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
# include <fcntl.h>
|
|
|
|
#else
|
|
|
|
# include <sys/fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2002-08-09 04:06:17 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2002-05-13 19:37:36 +00:00
|
|
|
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/hash.h"
|
|
|
|
#include "QF/pr_comp.h"
|
|
|
|
#include "QF/progs.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakeio.h"
|
2002-05-13 19:37:36 +00:00
|
|
|
#include "QF/sys.h"
|
2002-06-12 03:01:30 +00:00
|
|
|
#include "QF/va.h"
|
2002-05-13 19:37:36 +00:00
|
|
|
#include "QF/zone.h"
|
|
|
|
|
2004-01-29 07:43:05 +00:00
|
|
|
#include "obj_file.h"
|
2003-01-06 18:28:13 +00:00
|
|
|
#include "qfprogs.h"
|
2002-05-16 23:44:53 +00:00
|
|
|
|
2003-04-23 20:10:51 +00:00
|
|
|
int sorted = 0;
|
|
|
|
int verbosity = 0;
|
|
|
|
|
2002-05-16 23:44:53 +00:00
|
|
|
static const struct option long_options[] = {
|
|
|
|
{"disassemble", no_argument, 0, 'd'},
|
|
|
|
{"globals", no_argument, 0, 'g'},
|
|
|
|
{"strings", no_argument, 0, 's'},
|
2002-06-09 16:28:19 +00:00
|
|
|
{"fields", no_argument, 0, 'f'},
|
|
|
|
{"functions", no_argument, 0, 'F'},
|
2002-09-07 06:47:16 +00:00
|
|
|
{"lines", no_argument, 0, 'l'},
|
2002-08-20 14:31:47 +00:00
|
|
|
{"modules", no_argument, 0, 'M'},
|
2003-03-12 22:31:44 +00:00
|
|
|
{"path", required_argument, 0, 'P'},
|
2002-10-16 17:31:15 +00:00
|
|
|
{"verbose", no_argument, 0, 'v'},
|
2003-04-23 20:10:51 +00:00
|
|
|
{"numeric", no_argument, 0, 'n'},
|
2002-05-16 23:44:53 +00:00
|
|
|
{NULL, 0, NULL, 0},
|
|
|
|
};
|
2002-05-13 19:37:36 +00:00
|
|
|
|
|
|
|
static edict_t *edicts;
|
|
|
|
static int num_edicts;
|
|
|
|
static int reserved_edicts = 1;
|
|
|
|
static progs_t pr;
|
|
|
|
static void *membase;
|
|
|
|
static int memsize = 1024*1024;
|
|
|
|
|
2004-01-29 07:43:05 +00:00
|
|
|
static qfo_t *qfo;
|
|
|
|
static dprograms_t progs;
|
|
|
|
|
2003-03-12 22:31:44 +00:00
|
|
|
static const char *source_path = "";
|
2002-10-16 17:31:15 +00:00
|
|
|
|
2002-05-13 19:37:36 +00:00
|
|
|
static hashtab_t *func_tab;
|
|
|
|
|
2002-08-27 07:16:28 +00:00
|
|
|
static QFile *
|
2002-06-11 17:24:37 +00:00
|
|
|
open_file (const char *path, int *len)
|
|
|
|
{
|
2002-09-27 04:27:19 +00:00
|
|
|
QFile *file = Qopen (path, "rbz");
|
2002-06-11 17:24:37 +00:00
|
|
|
|
2003-01-29 20:32:44 +00:00
|
|
|
if (!file)
|
2002-06-11 17:24:37 +00:00
|
|
|
return 0;
|
2002-09-27 04:27:19 +00:00
|
|
|
*len = Qfilesize (file);
|
|
|
|
return file;
|
2002-06-11 17:24:37 +00:00
|
|
|
}
|
|
|
|
|
2003-03-12 22:31:44 +00:00
|
|
|
static void
|
|
|
|
file_error (progs_t *pr, const char *name)
|
|
|
|
{
|
|
|
|
perror (name);
|
|
|
|
}
|
|
|
|
|
2002-06-11 17:24:37 +00:00
|
|
|
static void *
|
|
|
|
load_file (progs_t *pr, const char *name)
|
|
|
|
{
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *file;
|
2002-06-11 17:24:37 +00:00
|
|
|
int size;
|
|
|
|
void *sym;
|
|
|
|
|
|
|
|
file = open_file (name, &size);
|
|
|
|
if (!file) {
|
2002-06-12 03:01:30 +00:00
|
|
|
file = open_file (va ("%s.gz", name), &size);
|
2003-03-12 22:31:44 +00:00
|
|
|
if (!file)
|
2002-06-12 03:01:30 +00:00
|
|
|
return 0;
|
2002-06-11 17:24:37 +00:00
|
|
|
}
|
|
|
|
sym = malloc (size);
|
|
|
|
Qread (file, sym, size);
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2002-05-13 19:37:36 +00:00
|
|
|
static void *
|
|
|
|
allocate_progs_mem (progs_t *pr, int size)
|
|
|
|
{
|
|
|
|
return malloc (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_progs_mem (progs_t *pr, void *mem)
|
|
|
|
{
|
|
|
|
free (mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
func_hash (void *func, void *unused)
|
|
|
|
{
|
|
|
|
return ((dfunction_t *) func)->first_statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
func_compare (void *f1, void *f2, void *unused)
|
|
|
|
{
|
|
|
|
return ((dfunction_t *) f1)->first_statement
|
|
|
|
== ((dfunction_t *) f2)->first_statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
dfunction_t *
|
|
|
|
func_find (int st_ofs)
|
|
|
|
{
|
|
|
|
dfunction_t f;
|
|
|
|
|
|
|
|
f.first_statement = st_ofs;
|
|
|
|
return Hash_FindElement (func_tab, &f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_qf (void)
|
|
|
|
{
|
|
|
|
Cvar_Init_Hash ();
|
|
|
|
Cmd_Init_Hash ();
|
|
|
|
Cvar_Init ();
|
|
|
|
Sys_Init_Cvars ();
|
|
|
|
Cmd_Init ();
|
|
|
|
|
|
|
|
membase = malloc (memsize);
|
|
|
|
Memory_Init (membase, memsize);
|
|
|
|
|
2002-10-16 17:31:15 +00:00
|
|
|
Cvar_Get ("pr_debug", va ("%d", verbosity), 0, 0, "");
|
2003-03-12 22:31:44 +00:00
|
|
|
Cvar_Get ("pr_source_path", source_path, 0, 0, "");
|
2002-05-13 19:37:36 +00:00
|
|
|
PR_Init_Cvars ();
|
|
|
|
PR_Init ();
|
|
|
|
|
|
|
|
pr.edicts = &edicts;
|
|
|
|
pr.num_edicts = &num_edicts;
|
|
|
|
pr.reserved_edicts = &reserved_edicts;
|
2003-03-12 22:31:44 +00:00
|
|
|
pr.file_error = file_error;
|
2002-06-11 17:24:37 +00:00
|
|
|
pr.load_file = load_file;
|
2002-05-13 19:37:36 +00:00
|
|
|
pr.allocate_progs_mem = allocate_progs_mem;
|
|
|
|
pr.free_progs_mem = free_progs_mem;
|
|
|
|
|
|
|
|
func_tab = Hash_NewTable (1021, 0, 0, 0);
|
|
|
|
Hash_SetHashCompare (func_tab, func_hash, func_compare);
|
|
|
|
}
|
|
|
|
|
2004-01-29 07:43:05 +00:00
|
|
|
static void
|
|
|
|
convert_qfo (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pr.progs = &progs;
|
|
|
|
progs.version = PROG_VERSION;
|
|
|
|
|
|
|
|
pr.pr_statements = qfo->code;
|
|
|
|
progs.numstatements = qfo->code_size;
|
|
|
|
|
|
|
|
pr.pr_strings = qfo->strings;
|
|
|
|
progs.numstrings = qfo->strings_size;
|
|
|
|
pr.pr_stringsize = qfo->strings_size;
|
|
|
|
|
|
|
|
pr.pr_globals = qfo->data;
|
|
|
|
progs.numglobals = qfo->data_size;
|
|
|
|
|
|
|
|
progs.numglobaldefs = 0;
|
|
|
|
progs.numfielddefs = 0;
|
|
|
|
progs.entityfields = 0;
|
|
|
|
pr.pr_globaldefs = calloc (qfo->num_defs, sizeof (ddef_t));
|
|
|
|
pr.pr_fielddefs = calloc (qfo->num_defs, sizeof (ddef_t));
|
|
|
|
for (i = 0; i < qfo->num_defs; i++) {
|
|
|
|
qfo_def_t *def = qfo->defs + i;
|
|
|
|
ddef_t ddef;
|
|
|
|
|
|
|
|
if ((def->flags & QFOD_LOCAL) || !def->name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ddef.type = def->basic_type;
|
|
|
|
ddef.ofs = def->ofs;
|
|
|
|
ddef.s_name = def->name;
|
|
|
|
if (!(def->flags & QFOD_NOSAVE)
|
|
|
|
&& !(def->flags & QFOD_CONSTANT)
|
|
|
|
&& (def->flags & QFOD_GLOBAL)
|
|
|
|
&& def->basic_type != ev_func
|
|
|
|
&& def->basic_type != ev_field)
|
|
|
|
ddef.type |= DEF_SAVEGLOBAL;
|
|
|
|
pr.pr_globaldefs[progs.numglobaldefs++] = ddef;
|
|
|
|
if (ddef.type == ev_field) {
|
|
|
|
const char *type = qfo->types + def->full_type;
|
|
|
|
if (type[0] != 'F') {
|
|
|
|
ddef.type = ev_void;
|
|
|
|
} else {
|
|
|
|
switch (type[1]) {
|
|
|
|
default:
|
|
|
|
case 'v':
|
|
|
|
ddef.type = ev_void;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
ddef.type = ev_string;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
ddef.type = ev_float;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
ddef.type = ev_vector;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
ddef.type = ev_entity;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
ddef.type = ev_field;
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
ddef.type = ev_func;
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
ddef.type = ev_sel;
|
|
|
|
break;
|
|
|
|
case '@': // id
|
|
|
|
case '#': // class
|
|
|
|
case '^':
|
|
|
|
ddef.type = ev_pointer;
|
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
ddef.type = ev_quaternion;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
ddef.type = ev_integer;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
ddef.type = ev_uinteger;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
ddef.type = ev_short;
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
ddef.type = ev_struct;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
ddef.type = ev_array;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ddef.ofs = G_INT (&pr, ddef.ofs);
|
|
|
|
pr.pr_fielddefs[progs.numfielddefs++] = ddef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
progs.numfunctions = qfo->num_funcs;
|
|
|
|
pr.pr_functions = calloc (qfo->num_funcs, sizeof (dfunction_t));
|
|
|
|
for (i = 0; i < qfo->num_funcs; i++) {
|
|
|
|
qfo_func_t *func = qfo->funcs + i;
|
|
|
|
dfunction_t df;
|
|
|
|
|
|
|
|
df.first_statement = func->builtin ? -func->builtin : func->code;
|
|
|
|
df.parm_start = 0;
|
|
|
|
df.locals = func->locals_size;
|
|
|
|
df.profile = 0;
|
|
|
|
df.s_name = func->name;
|
|
|
|
df.s_file = func->file;
|
|
|
|
df.numparms = func->num_parms;
|
|
|
|
memcpy (df.parm_size, func->parm_size, sizeof (df.parm_size));
|
|
|
|
|
|
|
|
pr.pr_functions[i] = df;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2002-05-13 19:37:36 +00:00
|
|
|
load_progs (const char *name)
|
|
|
|
{
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *file;
|
2002-05-13 19:37:36 +00:00
|
|
|
int i, size;
|
2004-01-29 07:43:05 +00:00
|
|
|
char buff[5];
|
|
|
|
|
|
|
|
Hash_FlushTable (func_tab);
|
2002-05-13 19:37:36 +00:00
|
|
|
|
|
|
|
file = open_file (name, &size);
|
|
|
|
if (!file) {
|
|
|
|
perror (name);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-01-29 07:43:05 +00:00
|
|
|
Qread (file, buff, 4);
|
|
|
|
buff[4] = 0;
|
|
|
|
Qseek (file, 0, SEEK_SET);
|
|
|
|
if (!strcmp (buff, QFO)) {
|
|
|
|
qfo = qfo_read (file);
|
|
|
|
Qclose (file);
|
|
|
|
|
|
|
|
if (!qfo)
|
|
|
|
return 0;
|
2003-01-29 20:32:44 +00:00
|
|
|
|
2004-01-29 07:43:05 +00:00
|
|
|
convert_qfo ();
|
|
|
|
} else {
|
|
|
|
pr.progs_name = name;
|
|
|
|
PR_LoadProgsFile (&pr, file, size, 1, 0);
|
|
|
|
Qclose (file);
|
|
|
|
|
|
|
|
if (!pr.progs)
|
|
|
|
return 0;
|
2003-01-29 20:32:44 +00:00
|
|
|
|
2004-01-29 07:43:05 +00:00
|
|
|
PR_LoadStrings (&pr);
|
2002-05-13 19:37:36 +00:00
|
|
|
|
2004-01-29 07:43:05 +00:00
|
|
|
PR_LoadDebug (&pr);
|
|
|
|
}
|
2002-05-13 19:37:36 +00:00
|
|
|
for (i = 0; i < pr.progs->numfunctions; i++) {
|
2004-01-29 07:43:05 +00:00
|
|
|
// don't bother with builtins
|
|
|
|
if (pr.pr_functions[i].first_statement > 0)
|
2002-05-13 19:37:36 +00:00
|
|
|
Hash_AddElement (func_tab, &pr.pr_functions[i]);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2002-05-16 23:44:53 +00:00
|
|
|
int c;
|
|
|
|
void (*func)(progs_t *pr) = dump_globals;
|
|
|
|
|
2003-03-12 22:31:44 +00:00
|
|
|
while ((c = getopt_long (argc, argv,
|
2003-04-23 20:10:51 +00:00
|
|
|
"dgsfFlMP:vn", long_options, 0)) != EOF) {
|
2002-05-16 23:44:53 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
|
|
|
func = disassemble_progs;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
func = dump_globals;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
func = dump_strings;
|
|
|
|
break;
|
2002-06-09 16:28:19 +00:00
|
|
|
case 'f':
|
|
|
|
func = dump_fields;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
func = dump_functions;
|
|
|
|
break;
|
2002-09-07 06:47:16 +00:00
|
|
|
case 'l':
|
|
|
|
func = dump_lines;
|
|
|
|
break;
|
2002-08-20 14:31:47 +00:00
|
|
|
case 'M':
|
|
|
|
func = dump_modules;
|
|
|
|
break;
|
2003-03-12 22:31:44 +00:00
|
|
|
case 'P':
|
|
|
|
source_path = strdup (optarg);
|
2003-03-13 17:31:01 +00:00
|
|
|
break;
|
2002-10-16 17:31:15 +00:00
|
|
|
case 'v':
|
|
|
|
verbosity++;
|
|
|
|
break;
|
2003-04-23 20:10:51 +00:00
|
|
|
case 'n':
|
|
|
|
sorted = 1;
|
|
|
|
break;
|
2002-05-16 23:44:53 +00:00
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2002-10-16 17:31:15 +00:00
|
|
|
init_qf ();
|
2002-05-16 23:44:53 +00:00
|
|
|
while (optind < argc) {
|
2003-01-29 20:32:44 +00:00
|
|
|
if (!load_progs (argv[optind++]))
|
|
|
|
return 1;
|
2002-05-16 23:44:53 +00:00
|
|
|
func (&pr);
|
2002-05-13 19:37:36 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|