[Nasm-commits] [nasm:path] Add __PATH__ support

nasm-bot for Keith Kanios keith at kanios.net
Thu Jun 4 19:56:36 PDT 2020


Commit-ID:  622a7e2f1dc7c53e5fdc73ba6a6bf59808ef701c
Gitweb:     http://repo.or.cz/w/nasm.git?a=commitdiff;h=622a7e2f1dc7c53e5fdc73ba6a6bf59808ef701c
Author:     Keith Kanios <keith at kanios.net>
AuthorDate: Sun, 5 Jun 2011 04:11:01 -0500
Committer:  Keith Kanios <keith at kanios.net>
CommitDate: Sun, 5 Jun 2011 04:11:01 -0500

Add __PATH__ support



---
 compiler.h   | 39 +++++++++++++++++++++++++++++++++++++++
 nasmlib.c    | 21 +++++++++++++++++++++
 nasmlib.h    |  2 ++
 preproc.c    | 20 ++++++++++++++++++--
 standard.mac |  3 ++-
 5 files changed, 82 insertions(+), 3 deletions(-)

diff --git a/compiler.h b/compiler.h
index 5805f5a9..117e8c5c 100644
--- a/compiler.h
+++ b/compiler.h
@@ -183,4 +183,43 @@ char *strsep(char **, const char *);
 # define no_return void
 #endif
 
+/*
+ * Detect DOS Host/Target
+ */
+#ifndef __DOS__
+# ifdef DOS
+#  define __DOS__
+# endif
+# ifdef MSDOS
+#  define __DOS__
+# endif
+# ifdef __MSDOS__
+#  define __DOS__
+# endif
+# ifdef _MSDOS
+#  define __DOS__
+# endif
+#endif
+
+/*
+ * Detect Windows Host/Target
+ */
+#ifndef __WINDOWS__
+# ifdef _WIN32
+#  define __WINDOWS__
+# endif
+# ifdef __WIN32__
+#  define __WINDOWS__
+# endif
+# ifdef _WIN64
+#  define __WINDOWS__
+# endif
+# ifdef _MSC_VER
+#  define __WINDOWS__
+# endif
+# ifdef __TOS_WIN__
+#  define __WINDOWS__
+# endif
+#endif
+
 #endif	/* NASM_COMPILER_H */
diff --git a/nasmlib.c b/nasmlib.c
index d70f6c24..04672efb 100644
--- a/nasmlib.c
+++ b/nasmlib.c
@@ -649,6 +649,27 @@ int src_get(int32_t *xline, char **xname)
     return 0;
 }
 
+int src_get_path(char **xname)
+{
+    const char *path = NULL;
+
+    if (!file_name) {
+        return -1;
+    }
+
+    path = strrchr(file_name, '/');
+#if defined(__DOS__) || defined(__WINDOWS__)
+    if (!path)
+        path = strrchr(file_name, '\\');
+#endif
+
+    if (path != NULL) {
+        *xname = nasm_strndup(file_name, (path - file_name + 1));
+    }
+
+    return 0;
+}
+
 char *nasm_strcat(const char *one, const char *two)
 {
     char *rslt;
diff --git a/nasmlib.h b/nasmlib.h
index 2c335e11..deca0907 100644
--- a/nasmlib.h
+++ b/nasmlib.h
@@ -386,6 +386,8 @@ int32_t src_get_linnum(void);
  */
 int src_get(int32_t *xline, char **xname);
 
+int src_get_path(char **xname);
+
 char *nasm_strcat(const char *one, const char *two);
 
 char *nasm_skip_spaces(const char *p);
diff --git a/preproc.c b/preproc.c
index 5fdeab26..c7a2285b 100644
--- a/preproc.c
+++ b/preproc.c
@@ -4403,13 +4403,29 @@ again:
                      * expansion back on the to-do stack.
                      */
                     if (!m->expansion) {
+                        if (!strcmp("__PATH__", m->name)) {
+                            char *path = NULL;
+                            src_get_path(&path);
+                            if (path != NULL) {
+                                tline->text = nasm_quote(path, strlen(path));
+                                nasm_free(path);
+                            } else {
+                                tline->text = nasm_quote("", 0);
+                            }
+                            tline->type = TOK_STRING;
+                            continue;
+                        }
                         if (!strcmp("__FILE__", m->name)) {
                             int32_t num = 0;
                             char *file = NULL;
                             src_get(&num, &file);
-                            tline->text = nasm_quote(file, strlen(file));
+                            if (file != NULL) {
+                                tline->text = nasm_quote(file, strlen(file));
+                                nasm_free(file);
+                            } else {
+                                tline->text = nasm_quote("", 0);
+                            }
                             tline->type = TOK_STRING;
-                            nasm_free(file);
                             continue;
                         }
                         if (!strcmp("__LINE__", m->name)) {
diff --git a/standard.mac b/standard.mac
index b2dff8d6..0c27fe2b 100644
--- a/standard.mac
+++ b/standard.mac
@@ -50,8 +50,9 @@
 ; here, not all of them are: the user-level form of a format-specific
 ; directive should be defined in the module for that directive.
 
-; These three need to be defined, though the actual definitions will
+; These need to be defined, though the actual definitions will
 ; be constantly updated during preprocessing.
+%define __PATH__
 %define __FILE__
 %define __LINE__
 %define __BITS__


More information about the Nasm-commits mailing list