Skip to content

Commit 7784732

Browse files
authored
Merge branch 'master' into terminal-funcs
2 parents 989d687 + 15c1ca8 commit 7784732

File tree

11 files changed

+85
-20
lines changed

11 files changed

+85
-20
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@ Debug
5252
miniscript-cpp.dir
5353
*.vcxproj*
5454
*.sln
55+
56+
# Tests fallout
57+
_*.txt

MiniScript-cpp/src/DateTimeUtils.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
#if _WIN32 || _WIN64
1515
struct tm *localtime_r( const time_t *timer, struct tm *buf ) {
16-
*buf = *_localtime64(timer);
16+
struct tm *newtime = _localtime64(timer);
17+
if (newtime == nullptr) return nullptr;
18+
*buf = *newtime;
1719
return buf;
1820
}
1921
#endif
@@ -31,7 +33,8 @@ static bool Match(const String s, size_t *posB, const String match) {
3133

3234
String FormatDate(time_t t, String formatSpec) {
3335
tm dateTime;
34-
localtime_r(&t, &dateTime);
36+
struct tm *newtime = localtime_r(&t, &dateTime);
37+
if (newtime == nullptr) return ""; // arg t too large
3538

3639
const int BUFSIZE = 128;
3740
char buffer[BUFSIZE];
@@ -304,6 +307,7 @@ time_t ParseDate(const String dateStr) {
304307
dateTime.tm_mon = now.tm_mon;
305308
dateTime.tm_mday = now.tm_mday;
306309
}
310+
dateTime.tm_isdst = -1;
307311
return mktime(&dateTime);
308312
}
309313

MiniScript-cpp/src/MiniScript/MiniscriptLexer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace MiniScript {
3737
case Token::Type::OpLesser: result = "OpLesser"; break;
3838
case Token::Type::OpLessEqual: result = "OpLessEqual"; break;
3939
case Token::Type::OpAssignPlus: result = "OpAssignPlus"; break;
40-
case Token::Type::OpAssignMinus: result = "OpAssignPMinus"; break;
40+
case Token::Type::OpAssignMinus: result = "OpAssignMinus"; break;
4141
case Token::Type::OpAssignTimes: result = "OpAssignTimes"; break;
4242
case Token::Type::OpAssignDivide: result = "OpAssignDivide"; break;
4343
case Token::Type::OpAssignMod: result = "OpAssignMod"; break;

MiniScript-cpp/src/MiniScript/MiniscriptTypes.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#include <iostream>
1717
#include <math.h>
18-
#include <bit>
1918

2019
namespace MiniScript {
2120

MiniScript-cpp/src/ShellExec.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@ String readFromFd(HANDLE fd, bool trimTrailingNewline=true) {
4141
buffer[bytesRead] = '\0';
4242
if (trimTrailingNewline and bytesRead < bufferSize-1 and bytesRead > 0 and buffer[bytesRead-1] == '\n') {
4343
// Efficiently trim \n or \r\n from the end of the buffer
44-
buffer[bytesRead-1] = '\0';
45-
if (bytesRead > 1 and buffer[bytesRead-2] == '\r') {
46-
buffer[bytesRead-2] = '\0';
47-
}
44+
bytesRead--;
45+
if (bytesRead > 0 and buffer[bytesRead-1] == '\r') bytesRead--;
4846
trimmed = true;
4947
}
5048

51-
String s(buffer, bytesRead+1);
49+
String s(buffer, bytesRead);
5250
output += s;
5351
}
5452

MiniScript-cpp/src/ShellIntrinsics.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
#else
5151
#include <fcntl.h>
5252
#include <unistd.h>
53-
#include <unistd.h>
5453
#include <dirent.h> // for readdir
5554
#include <libgen.h> // for basename and dirname
5655
#include <sys/stat.h> // for stat
@@ -61,7 +60,6 @@
6160
#include <sys/sendfile.h>
6261
#endif
6362
#define PATHSEP '/'
64-
#include <unistd.h>
6563
#include <sys/wait.h>
6664
#endif
6765

@@ -105,15 +103,16 @@ class RawDataHandleStorage : public RefCountedStorage {
105103
}
106104
virtual ~RawDataHandleStorage() { free(data); }
107105
void resize(size_t newSize) {
108-
if (data) {
106+
if (newSize == 0) {
107+
free(data);
108+
data = nullptr;
109+
dataSize = 0;
110+
} else {
109111
void *newData = realloc(data, newSize);
110112
if (newData) {
111113
data = newData;
112114
dataSize = newSize;
113115
}
114-
} else {
115-
data = malloc(newSize);
116-
if (data) dataSize = newSize;
117116
}
118117
}
119118

@@ -350,8 +349,12 @@ static IntrinsicResult intrinsic_basename(Context *context, IntrinsicResult part
350349
char extBuf[256];
351350
_splitpath_s(pathStr.c_str(), driveBuf, sizeof(driveBuf), nullptr, 0, nameBuf, sizeof(nameBuf), extBuf, sizeof(extBuf));
352351
String result = String(nameBuf) + String(extBuf);
353-
#else
352+
#elif defined(__APPLE__) || defined(__FreeBSD__)
354353
String result(basename((char*)pathStr.c_str()));
354+
#else
355+
char *duplicate = strdup((char*)pathStr.c_str());
356+
String result(basename(duplicate));
357+
free(duplicate);
355358
#endif
356359
return IntrinsicResult(result);
357360
}
@@ -740,7 +743,7 @@ static IntrinsicResult intrinsic_readLines(Context *context, IntrinsicResult par
740743
partialLine = "";
741744
}
742745
list.Add(line);
743-
if (buf[i] == '\n' && i+1 < bytesRead && buf[i+1] == '\r') i++;
746+
if (buf[i] == '\r' && i+1 < bytesRead && buf[i+1] == '\n') i++;
744747
if (i+1 < bytesRead && buf[i+1] == 0) i++;
745748
lineStart = i + 1;
746749
}
@@ -749,6 +752,7 @@ static IntrinsicResult intrinsic_readLines(Context *context, IntrinsicResult par
749752
partialLine = String(&buf[lineStart], bytesRead - lineStart);
750753
}
751754
}
755+
if (!partialLine.empty()) list.Add(partialLine);
752756
fclose(handle);
753757
return IntrinsicResult(list);
754758
}
@@ -1139,7 +1143,6 @@ static IntrinsicResult intrinsic_rawDataSetUtf8(Context *context, IntrinsicResul
11391143
return IntrinsicResult(nBytes);
11401144
}
11411145

1142-
11431146
static IntrinsicResult intrinsic_keyAvailable(Context *context, IntrinsicResult partialResult) {
11441147
return IntrinsicResult(KeyAvailable());
11451148
}
@@ -1330,7 +1333,6 @@ static IntrinsicResult intrinsic_exec(Context* context, IntrinsicResult partialR
13301333
}
13311334
#else
13321335

1333-
13341336
static IntrinsicResult intrinsic_exec(Context *context, IntrinsicResult partialResult) {
13351337
double now = context->vm->RunTime();
13361338
if (partialResult.Done()) {
@@ -1362,7 +1364,6 @@ static IntrinsicResult intrinsic_exec(Context *context, IntrinsicResult partialR
13621364
return IntrinsicResult(data, false);
13631365
}
13641366
}
1365-
#endif
13661367

13671368
static bool disallowAssignment(ValueDict& dict, Value key, Value value) {
13681369
return true;

MiniScript-cpp/src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ int main(int argc, const char * argv[]) {
317317
AddScriptPathVar(arg.c_str());
318318
int rc = DoScriptFile(interp, arg);
319319
if (!launchReplAfterScript) return rc;
320+
break;
320321
} else {
321322
PrintHeaderInfo();
322323
return ReturnErr(String("Unknown option: ") + arg);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import "qa"
2+
3+
testDateTimeDateStr = function
4+
qa.assertEqual _dateStr(0), "2000-01-01 00:00:00"
5+
qa.assertEqual _dateStr(1e20), ""
6+
end function
7+
8+
if refEquals(locals, globals) then testDateTimeDateStr

MiniScript-cpp/tests/testDaylight.ms

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import "qa"
2+
3+
testDaylight = function
4+
5+
// This test always only tests a half of functionality depending on whether the host system has "daylight savings" on or off.
6+
7+
d = "2000-01-01 11:00:00"
8+
qa.assertEqual _dateStr(d, "%H"), "11"
9+
10+
d = "2000-07-01 11:00:00"
11+
qa.assertEqual _dateStr(d, "%H"), "11"
12+
end function
13+
14+
if refEquals(locals, globals) then testDaylight

MiniScript-cpp/tests/testFileName.ms

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import "qa"
2+
3+
testDir = "tests/"
4+
5+
testFileName = function
6+
p = "a/b/"
7+
n = file.name(p)
8+
qa.assertEqual n, "b"
9+
qa.assertEqual p, "a/b/"
10+
end function
11+
12+
if refEquals(locals, globals) then testFileName

0 commit comments

Comments
 (0)