Skip to content

Commit 39a3075

Browse files
committed
COMMON: fixes for RUN/EXEC
1 parent d1f8ceb commit 39a3075

File tree

6 files changed

+50
-57
lines changed

6 files changed

+50
-57
lines changed

configure.ac

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ function buildConsole() {
336336
dnl preconfigured values for unix console build
337337
TARGET="Building Unix console version."
338338
AC_DEFINE(_UnixOS, 1, [Building under Unix like systems.])
339-
AC_DEFINE(USE_TERM_IO, 0, [Uses terminal-io functions.])
340339
AC_DEFINE(DEV_EVENTS_OSD, 0, [dev_events() implemented using osd_events().])
341340
PACKAGE_LIBS="${PACKAGE_LIBS} -lm -ldl -lpthread"
342341
BUILD_SUBDIRS="src/common src/platform/unix"

src/common/device.c

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,6 @@
1818
#define usleep(s) Sleep((DWORD)((s+500)/1000))
1919
#endif
2020

21-
/**
22-
* break signal
23-
*/
24-
#if defined(_UnixOS) || defined(_DOS)
25-
void termination_handler(int signum) {
26-
static int ctrlc_count;
27-
28-
prog_error = -2;
29-
ctrlc_count++;
30-
if (ctrlc_count == 1) {
31-
dev_printf("\n\n\033[0m\033[7m\a * %s %d * \033[0m\n", WORD_BREAK_AT, prog_line);
32-
} else if (ctrlc_count == 3) {
33-
dev_restore();
34-
exit(1);
35-
}
36-
}
37-
#endif
38-
3921
/**
4022
* initialize all drivers
4123
*/
@@ -398,26 +380,31 @@ void dev_printf(const char *fmt, ...) {
398380
* prints to the output device as per dev_printf
399381
*/
400382
void log_printf(const char *format, ...) {
401-
char buf[4096], *p = buf;
402383
va_list args;
403-
404384
va_start(args, format);
405-
p += vsnprintf(p, sizeof buf - 1, format, args);
385+
unsigned size = vsnprintf(NULL, 0, format, args);
406386
va_end(args);
407387

408-
while (p > buf && isspace(p[-1])) {
409-
*--p = '\0';
410-
}
388+
if (size) {
389+
char *buf = malloc(size + 3);
390+
va_start(args, format);
391+
vsnprintf(buf, size + 1, format, args);
392+
va_end(args);
411393

412-
*p++ = '\r';
413-
*p++ = '\n';
414-
*p = '\0';
394+
buf[size] = '\0';
395+
int i = size - 1;
396+
while (i >= 0 && isspace(buf[i])) {
397+
buf[i--] = '\0';
398+
}
399+
strcat(buf, "\r\n");
415400

416401
#if defined(IMPL_LOG_WRITE)
417-
lwrite(buf);
402+
lwrite(buf);
418403
#else
419-
dev_print(buf);
404+
dev_print(buf);
420405
#endif
406+
free(buf);
407+
}
421408
}
422409

423410
#if defined(BUILD_CONSOLE)

src/common/scan.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,11 +4010,11 @@ void comp_preproc_unit_path(char *p) {
40104010
*up++ = *p++;
40114011
}
40124012
*up = '\0';
4013-
#if defined(setenv)
4014-
setenv(LCN_UNIT_PATH, upath, 1);
4015-
#else
4013+
#ifdef __MINGW32__
40164014
sprintf(comp_bc_temp, "UNITPATH=%s", upath);
40174015
putenv(strdup(comp_bc_temp));
4016+
#else
4017+
setenv(LCN_UNIT_PATH, upath, 1);
40184018
#endif
40194019
}
40204020
}

src/common/system.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
extern char **environ;
2727

2828
#define BUFSIZE 1024
29-
#define POPEN_BUFFSIZE 255
3029

3130
#if defined(_Win32)
3231

@@ -35,9 +34,9 @@ extern char **environ;
3534
*
3635
* returns a newly allocated string with the result or NULL
3736
*
38-
* warning: if the cmd is a GUI process, the pw_shell will hang
37+
* warning: if the cmd is a GUI process, the shell will hang
3938
*/
40-
char *pw_shell(const char *cmd) {
39+
char *shell(const char *cmd) {
4140
HANDLE h_inppip, h_outpip, h_errpip, h_pid;
4241
char buf[BUFSIZE + 1], cv_buf[BUFSIZE + 1];
4342
char *result = NULL;
@@ -52,7 +51,9 @@ char *pw_shell(const char *cmd) {
5251
sa.nLength = sizeof(sa);
5352
sa.bInheritHandle = TRUE;
5453

54+
log_printf("shell: %s\n", cmd);
5555
if (!CreatePipe(&h_inppip, &h_outpip, &sa, BUFSIZE)) {
56+
log_printf("CreatePipe failed");
5657
return NULL;
5758
}
5859

@@ -66,7 +67,6 @@ char *pw_shell(const char *cmd) {
6667
si.cb = sizeof(si);
6768
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
6869
si.wShowWindow = SW_HIDE;
69-
7070
si.hStdOutput = h_outpip;
7171
si.hStdError = h_errpip;
7272

@@ -92,9 +92,10 @@ char *pw_shell(const char *cmd) {
9292
strcat(result, cv_buf);
9393
}
9494
CloseHandle(pi.hProcess);
95+
log_printf("shell completed %d bytes\n", strlen(result));
9596
}
9697
else {
97-
// could not run it
98+
log_printf("Failed to launch %s\n", cmd);
9899
result = NULL;
99100
}
100101

@@ -112,7 +113,7 @@ char *pw_shell(const char *cmd) {
112113
int dev_run(const char *src, var_t *r, int wait) {
113114
int result = 1;
114115
if (r != NULL) {
115-
char *buf = pw_shell(src);
116+
char *buf = shell(src);
116117
if (buf != NULL) {
117118
r->type = V_STR;
118119
r->v.p.ptr = buf;
@@ -121,7 +122,7 @@ int dev_run(const char *src, var_t *r, int wait) {
121122
result = 0;
122123
}
123124
} else if (wait) {
124-
char *out = pw_shell(src);
125+
char *out = shell(src);
125126
if (out != NULL) {
126127
free(out);
127128
} else {
@@ -139,25 +140,24 @@ int dev_run(const char *src, var_t *r, int wait) {
139140
int result = 1;
140141
if (r != NULL) {
141142
r->type = V_STR;
142-
r->v.p.size = POPEN_BUFFSIZE + 1;
143+
r->v.p.size = BUFSIZE + 1;
143144
r->v.p.ptr = malloc(r->v.p.size);
144-
*r->v.p.ptr = '\0';
145+
r->v.p.ptr[0] = '\0';
145146

146147
int bytes = 0;
147148
int total = 0;
148-
char buf[256];
149-
149+
char buf[BUFSIZE + 1];
150150
FILE *fin = popen(src, "r");
151151
if (fin) {
152152
while (!feof(fin)) {
153-
bytes = fread(buf, 1, POPEN_BUFFSIZE, fin);
154-
total += bytes;
153+
bytes = fread(buf, 1, BUFSIZE, fin);
155154
buf[bytes] = '\0';
156-
strcat(r->v.p.ptr, buf);
157-
if (total + POPEN_BUFFSIZE + 1 >= r->v.p.size) {
158-
r->v.p.size += POPEN_BUFFSIZE + 1;
155+
total += bytes;
156+
if (total >= r->v.p.size) {
157+
r->v.p.size += BUFSIZE + 1;
159158
r->v.p.ptr = realloc(r->v.p.ptr, r->v.p.size);
160159
}
160+
strcat(r->v.p.ptr, buf);
161161
}
162162
pclose(fin);
163163
} else {

src/languages/messages.en.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@
191191
#define ERR_GPF "\n\aOUT OF ADDRESS SPACE\n"
192192
#define ERR_CRITICAL_MISSING_PROC "Unsupported built-in procedure call %ld, please report this bug"
193193
#define ERR_CHAIN_FILE "CHAIN: File '%s' does not exist"
194-
#define ERR_RUN_FILE "RUN/EXEC\"%s\" Failed"
194+
#define ERR_RUN_FILE "RUN/EXEC \"%s\" Failed"
195195
#define ERR_RUNFUNC_FILE "RUN(\"%s\"): Failed"
196196
#define ERR_PARCOUNT_SP "Parameter count error @%d=%X"
197197
#define ERR_DATE "Invalid DATE: '%s'. Expected DD/MM/YYYY"

src/ui/system.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,19 +1034,26 @@ void System::setRestart() {
10341034
}
10351035

10361036
void System::systemPrint(const char *format, ...) {
1037-
char buf[4096], *p = buf;
10381037
va_list args;
10391038

10401039
va_start(args, format);
1041-
p += vsnprintf(p, sizeof(buf) - 1, format, args);
1040+
unsigned size = vsnprintf(NULL, 0, format, args);
10421041
va_end(args);
1043-
*p = '\0';
10441042

1045-
deviceLog("%s", buf);
1043+
if (size) {
1044+
char *buf = (char *)malloc(size + 1);
1045+
va_start(args, format);
1046+
vsnprintf(buf, size + 1, format, args);
1047+
va_end(args);
1048+
buf[size] = '\0';
10461049

1047-
int prevScreen = _output->selectBackScreen(CONSOLE_SCREEN);
1048-
_output->print(buf);
1049-
_output->selectBackScreen(prevScreen);
1050+
deviceLog("%s", buf);
1051+
1052+
int prevScreen = _output->selectBackScreen(CONSOLE_SCREEN);
1053+
_output->print(buf);
1054+
_output->selectBackScreen(prevScreen);
1055+
free(buf);
1056+
}
10501057
}
10511058

10521059
//

0 commit comments

Comments
 (0)