From 4ae314731d995cae8d92279594662f6afc39acdc Mon Sep 17 00:00:00 2001
From: Axel Kohlmeyer <akohlmey@gmail.com>
Date: Tue, 30 May 2017 07:42:10 -0400
Subject: [PATCH] must not use strtok() in library function as it is not
 re-entrant and may be used inside LAMMPS commands

---
 src/library.cpp | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/library.cpp b/src/library.cpp
index 892818ff9c..87cc5d99d8 100644
--- a/src/library.cpp
+++ b/src/library.cpp
@@ -277,12 +277,24 @@ void lammps_commands_string(void *ptr, char *str)
 
   BEGIN_CAPTURE
   {
-    char *ptr = strtok(copy,"\n");
-    if (ptr) concatenate_lines(ptr);
-    while (ptr) {
-      lmp->input->one(ptr);
-      ptr = strtok(NULL,"\n");
-      if (ptr) concatenate_lines(ptr);
+    char *ptr = copy;
+    for (int i=0; i < n-1; ++i) {
+
+      // handle continuation character as last character in line or string
+      if ((copy[i] == '&') && (copy[i+1] == '\n'))
+        copy[i+1] = copy[i] = ' ';
+      else if ((copy[i] == '&') && (copy[i+1] == '\0'))
+        copy[i] = ' ';
+
+      if ((copy[i] == '\r') || (copy[i] == '\t'))
+        copy[i] = ' ';
+
+      if (copy[i] == '\n') {
+        copy[i] = '\0';
+        lmp->input->one(ptr);
+        ptr = copy + i+1;
+      } else if (copy[i+1] == '\0')
+        lmp->input->one(ptr);
     }
   }
   END_CAPTURE
-- 
GitLab