Skip to content

Commit

Permalink
better check for executable path name, thanks to David Betz
Browse files Browse the repository at this point in the history
  • Loading branch information
totalspectrum committed Jan 25, 2019
1 parent 3140106 commit 0c1ffd1
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Version 3.9.16
- Check environment variable FLEXCC_INCLUDE_PATH for included files
- Better relative path finding for include files thanks to David Betz

Version 3.9.15
- Fixed abort/catch in P2
Expand Down
9 changes: 1 addition & 8 deletions fastspin.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,14 @@ main(int argc, const char **argv)
int useEeprom = 0;
const char *listFile = NULL;

#if 0
printf("fastspin: arguments are:\n");
for (i = 0; i < argc; i++) {
printf("[%s]\n", argv[i]);
}
fflush(stdout);
#endif
gl_start_time = getCurTime();

/* Initialize the global preprocessor; we need to do this here
so that the -D command line option can define preprocessor
symbols. The rest of initialization happens after command line
options have been parsed
*/
InitPreprocessor();
InitPreprocessor(argv);

/* save our command line arguments and comments describing
how we were run
Expand Down
2 changes: 1 addition & 1 deletion frontends/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ int DoPropellerChecksum(const char *fname, size_t eepromSize);

// initialization functions
void Init();
void InitPreprocessor();
void InitPreprocessor(const char *argv[]);
void SetPreprocessorLanguage(int language);

// perform common sub-expression elimination on a function
Expand Down
79 changes: 78 additions & 1 deletion frontends/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1703,12 +1703,89 @@ struct constants p2_constants[] = {
{ "_set", SYM_CONSTANT, 0xF },
};

void InitPreprocessor()
#if defined(WIN32)
#include <windows.h>
#include <psapi.h>
#endif

#if defined(MACOSX)
#include <mach-o/dyld.h>
#endif

#if defined(LINUX)
#include <unistd.h>
#endif

#if defined(WIN32)
#define PATH_SEP ';'
#define DIR_SEP '\\'
#define DIR_SEP_STR "\\"
#else
#define PATH_SEP ':'
#define DIR_SEP '/'
#define DIR_SEP_STR "/"
#endif

int
getProgramPath(const char **argv, char *path, int size)
{
#if defined(WIN32)

#if defined(Q_OS_WIN32) || defined(MINGW)
/* get the full path to the executable */
if (!GetModuleFileNameA(NULL, path, size))
return -1;
#else
/* get the full path to the executable */
if (!GetModuleFileNameEx(GetCurrentProcess(), NULL, path, size))
return -1;
#endif /* Q_OS_WIN32 */

#elif defined(LINUX)
int r;
r = readlink("/proc/self/exe", path, size - 1);
if (r >= 0)
path[r] = 0;
else
return -1;
#elif defined(MACOSX)
uint32_t bufsize = size - 1;
int r = _NSGetExecutablePath(path, &bufsize);
if (r < 0)
return -1;
#else
/* fall back on argv[0]... probably not the best bet, since
shells might not put the full path in, but it's the most portable
*/
strcpy(path, argv[0]);
#endif

return 0;
}

char gl_prognamebuf[1024];

void InitPreprocessor(const char **argv)
{
const char *envpath;
char *progname;
pp_init(&gl_pp);
SetPreprocessorLanguage(LANG_SPIN);

// add a path relative to the executable
if (argv[0] != NULL) {
if (getProgramPath(argv, gl_prognamebuf, sizeof(gl_prognamebuf)) != 0) {
strcpy(gl_prognamebuf, argv[0]);
}
progname = strrchr(gl_prognamebuf, '/');
if (progname) {
progname++;
} else {
progname = gl_prognamebuf;
}
strcpy(progname, "../include");
pp_add_to_path(&gl_pp, progname);
}
// check for environment variables
envpath = getenv("FLEXCC_INCLUDE_PATH");
if (!envpath) {
Expand Down
6 changes: 6 additions & 0 deletions frontends/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include "optokens.h"
#include "util/flexbuf.h"

#if defined(__linux__)
#define LINUX 1
#elif defined(__MINGW32__) || defined(__MINGW64__)
#define MINGW 1
#endif

extern int lexgetc(LexStream *L);
extern void lexungetc(LexStream *L, int c);
extern void EstablishIndent(LexStream *L, int level);
Expand Down
2 changes: 1 addition & 1 deletion spin2cpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ main(int argc, const char **argv)
symbols. The rest of initialization happens after command line
options have been parsed
*/
InitPreprocessor();
InitPreprocessor(argv);

/* save our command line arguments and comments describing
how we were run
Expand Down

0 comments on commit 0c1ffd1

Please sign in to comment.