Skip to content

Commit d1f8ceb

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

File tree

12 files changed

+102
-122
lines changed

12 files changed

+102
-122
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2016-04-24 (0.12.6)
2+
Fixes for RUN/EXEC
3+
14
2016-04-20 (0.12.6)
25
Fixed memory handling issues with UNITs
36
Fixed memory issue related to SUB/FUNC pointers
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Hello world!
2+
I read: [Hello]+[ world!]
3+
NL=[Hello, world!]
4+
NL=[One more text line]

src/common/blib_func.c

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,49 +1451,8 @@ void cmd_strN(long funcCode, var_t *r) {
14511451
// Win32: use & at the end of the command to run-it in background
14521452
//
14531453
par_getstr(&arg1);
1454-
1455-
if (!prog_error) {
1456-
#if defined(_Win32)
1457-
char *buf;
1458-
#else
1459-
int bytes = 0, total = 0;
1460-
char buf[256];
1461-
#endif
1462-
1463-
#if defined(_Win32)
1464-
if ((buf = pw_shell(arg1.v.p.ptr)) != NULL) {
1465-
r->type = V_STR;
1466-
r->v.p.ptr = buf;
1467-
r->v.p.size = strlen(buf) + 1;
1468-
}
1469-
#else
1470-
/**
1471-
* default popen/pclose
1472-
*/
1473-
r->type = V_STR;
1474-
r->v.p.ptr = malloc(256);
1475-
*r->v.p.ptr = '\0';
1476-
r->v.p.size = 256;
1477-
1478-
FILE *fin = popen(arg1.v.p.ptr, "r");
1479-
if (fin) {
1480-
while (!feof(fin)) {
1481-
bytes = fread(buf, 1, 255, fin);
1482-
total += bytes;
1483-
buf[bytes] = '\0';
1484-
strcat(r->v.p.ptr, buf);
1485-
if (total + 256 >= r->v.p.size) {
1486-
r->v.p.size += 256;
1487-
r->v.p.ptr = realloc(r->v.p.ptr, r->v.p.size);
1488-
}
1489-
}
1490-
pclose(fin);
1491-
}
1492-
#endif // bcb or not
1493-
else {
1494-
v_zerostr(r);
1495-
rt_raise(ERR_RUNFUNC_FILE, arg1.v.p.ptr);
1496-
}
1454+
if (!prog_error && !dev_run(arg1.v.p.ptr, r, 1)) {
1455+
rt_raise(ERR_RUNFUNC_FILE, arg1.v.p.ptr);
14971456
}
14981457
break;
14991458

src/common/brun.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,6 @@ void cmd_chain(void) {
469469
*/
470470
void cmd_run(int retf) {
471471
var_t var;
472-
473472
v_init(&var);
474473
eval(&var);
475474
IF_ERR_RETURN;
@@ -482,7 +481,7 @@ void cmd_run(int retf) {
482481
v_free(&var);
483482
}
484483

485-
if (!dev_run(fileName, retf)) {
484+
if (!dev_run(fileName, NULL, retf)) {
486485
err_run_err(fileName);
487486
}
488487
}

src/common/device.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@
2727
#define _device_h
2828

2929
#include "common/sys.h"
30-
#if !defined(SCAN_MODULE)
3130
#include "common/var.h"
32-
#endif
33-
34-
#if USE_TERM_IO
35-
#include <termios.h>
36-
#endif
3731

3832
#if defined(__cplusplus)
3933
extern "C" {
@@ -221,14 +215,11 @@ void dev_clrkb(void);
221215
* run process
222216
*
223217
* @param prog is the command-line
224-
* @param retflg non-zero for return (system()); otherwise dev_run() never returns (exec())
218+
* @param v when non NULL, wait and return the process result
219+
* @param wait bool whether to detach or wait for the process result
225220
* @return non-zero on success
226221
*/
227-
int dev_run(const char *prog, int retflg);
228-
229-
#if defined(_Win32)
230-
char *pw_shell(const char *cmd);
231-
#endif
222+
int dev_run(const char *src, var_t *v, int wait);
232223

233224
/**
234225
* @ingroup dev
@@ -266,10 +257,9 @@ void dev_delay(dword ms);
266257
* Mouse & lightpen!
267258
*
268259
* Since 1988 the mouse was an new device for PC's, there is no mouse support on QB.
269-
* We shall use the PEN(x) to support the mouse, and we must maintain the Palm compatibility.
270260
*
271261
* <pre>
272-
PalmOS PEN, Lightpen & Mouse API
262+
PEN, Lightpen & Mouse API
273263
================================
274264
PEN(0) -> true (non zero) if there is a new pen or mouse event
275265
PEN(1) -> PEN: last pen-down x; MOUSE: last mouse button down x
@@ -304,8 +294,6 @@ int dev_getpen(int code);
304294
*
305295
* enable/disable pen/mouse driver.
306296
*
307-
* @note That is neccessary on some systems like PalmOS because the pointing device it may be used by the OS.
308-
*
309297
* @note That is the PEN ON/OFF command
310298
*
311299
* @param enable non-zero to enable pen/mouse driver.

src/common/osd.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* @defgroup lgraf Low-level graphics/sound driver
1212
*/
1313

14-
#if !defined(_palm_dev_h)
15-
#define _palm_dev_h
14+
#if !defined(_osd_h)
15+
#define _osd_h
1616

1717
#if defined(__cplusplus)
1818
extern "C" {
@@ -112,8 +112,6 @@ int osd_textheight(const char *str);
112112
*
113113
* enable/disable pen/mouse driver.
114114
*
115-
* That is neccessary on some systems like PalmOS because the pointing device it may be used by the OS.
116-
*
117115
* @note PEN ON/OFF command
118116
*
119117
* @param enable non-zero to enable pen/mouse driver.
@@ -126,10 +124,9 @@ void osd_setpenmode(int enable);
126124
* Mouse & lightpen!
127125
*
128126
* Since 1988 the mouse was an new device for PC's, there is no mouse support on QB.
129-
* We shall use the PEN(x) to support the mouse, and we must maintain the Palm compatibility.
130127
*
131128
* <pre>
132-
PalmOS PEN, Lightpen & Mouse API
129+
PEN, Lightpen & Mouse API
133130
================================
134131
PEN(0) -> true (non zero) if there is a new pen or mouse event
135132
PEN(1) -> PEN: last pen-down x; MOUSE: last mouse button down x

src/common/pmem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is part of SmallBASIC
22
//
3-
// Memory manager unix/palm
3+
// Memory manager unix
44
//
55
// This program is distributed under the terms of the GPL v2.0 or later
66
// Download the GNU Public License (GPL) from www.gnu.org

src/common/scan.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,7 +4010,12 @@ void comp_preproc_unit_path(char *p) {
40104010
*up++ = *p++;
40114011
}
40124012
*up = '\0';
4013+
#if defined(setenv)
40134014
setenv(LCN_UNIT_PATH, upath, 1);
4015+
#else
4016+
sprintf(comp_bc_temp, "UNITPATH=%s", upath);
4017+
putenv(strdup(comp_bc_temp));
4018+
#endif
40144019
}
40154020
}
40164021
}

src/common/system.c

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

2828
#define BUFSIZE 1024
29+
#define POPEN_BUFFSIZE 255
2930

3031
#if defined(_Win32)
3132

@@ -60,8 +61,7 @@ char *pw_shell(const char *cmd) {
6061
DuplicateHandle(h_pid, h_inppip, h_pid, &h_inppip, 0, FALSE,
6162
DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
6263
DuplicateHandle(h_pid, h_outpip, h_pid, &h_errpip, 0, TRUE, DUPLICATE_SAME_ACCESS);
63-
64-
// run
64+
6565
memset(&si, 0, sizeof(si));
6666
si.cb = sizeof(si);
6767
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
@@ -91,11 +91,11 @@ char *pw_shell(const char *cmd) {
9191
}
9292
strcat(result, cv_buf);
9393
}
94-
9594
CloseHandle(pi.hProcess);
9695
}
9796
else {
98-
result = NULL; // could not run it
97+
// could not run it
98+
result = NULL;
9999
}
100100

101101
// clean up
@@ -108,55 +108,91 @@ char *pw_shell(const char *cmd) {
108108
}
109109
return result;
110110
}
111-
#endif
112111

113-
/**
114-
* run a program (if retflg wait and return; otherwise just exec())
115-
*/
116-
int dev_run(const char *src, int retflg) {
117-
#if defined(_Win32)
118-
int r;
119-
char *out;
120-
121-
r = ((out = pw_shell(src)) != NULL);
122-
if (r) {
123-
free(out);
112+
int dev_run(const char *src, var_t *r, int wait) {
113+
int result = 1;
114+
if (r != NULL) {
115+
char *buf = pw_shell(src);
116+
if (buf != NULL) {
117+
r->type = V_STR;
118+
r->v.p.ptr = buf;
119+
r->v.p.size = strlen(buf) + 1;
120+
} else {
121+
result = 0;
122+
}
123+
} else if (wait) {
124+
char *out = pw_shell(src);
125+
if (out != NULL) {
126+
free(out);
127+
} else {
128+
result = 0;
129+
}
130+
} else {
131+
HWND hwnd = GetActiveWindow();
132+
ShellExecute(hwnd, "open", src, 0, 0, SW_SHOWNORMAL);
124133
}
134+
return result;
135+
}
125136

126-
if (r && !retflg) {
127-
exit(1); // ok, what to do now ?
128-
}
129-
return r;
130137
#else
131-
if (retflg) {
132-
return (system(src) != -1);
133-
} else {
134-
// execl(src, src, NULL);
135-
// call the shell if we want to behave same like system() function
136-
// -c means the next argument is the command string to execute
137-
// this allow us to execute shell script too!
138-
char *src1;
139-
src1 = malloc(strlen(src) + 3);
140-
if (src1 == NULL) {
141-
exit(-1); // ok, what to do now ?
138+
int dev_run(const char *src, var_t *r, int wait) {
139+
int result = 1;
140+
if (r != NULL) {
141+
r->type = V_STR;
142+
r->v.p.size = POPEN_BUFFSIZE + 1;
143+
r->v.p.ptr = malloc(r->v.p.size);
144+
*r->v.p.ptr = '\0';
145+
146+
int bytes = 0;
147+
int total = 0;
148+
char buf[256];
149+
150+
FILE *fin = popen(src, "r");
151+
if (fin) {
152+
while (!feof(fin)) {
153+
bytes = fread(buf, 1, POPEN_BUFFSIZE, fin);
154+
total += bytes;
155+
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;
159+
r->v.p.ptr = realloc(r->v.p.ptr, r->v.p.size);
160+
}
161+
}
162+
pclose(fin);
163+
} else {
164+
v_zerostr(r);
165+
result = 0;
166+
}
167+
} else if (wait) {
168+
result = (system(src) != -1);
169+
}
170+
else if (fork() == 0) {
171+
// exec separate process
172+
int size = strlen(src) + 3;
173+
char *src1 = malloc(size);
174+
if (src1 != NULL) {
175+
memset(src1, '\0', size);
176+
// double quote the command
177+
*src1 = '"';
178+
strcat(src1, src);
179+
*(src1 + strlen(src) + 1) = '"';
180+
// -c means the next argument is the command string to execute
181+
// this allow us to execute shell script
182+
execlp("sh", "sh", "-c", src1, NULL);
142183
}
143-
memset(src1, '\0', strlen(src) + 3);
144-
*src1 = '"';
145-
strcat(src1, src);
146-
*(src1 + strlen(src) + 1) = '"'; // we need a doublequote around the command
147-
execlp("sh", "sh", "-c", src1, NULL);
148184
exit(-1);
149-
// o.k. some error happens - what to do??? we already closed the screen!!
150185
}
151-
#endif
186+
return result;
152187
}
188+
#endif
153189

154190
#ifndef IMPL_DEV_ENV
155191

156192
/**
157-
* The putenv() function adds or changes the value of environment variables. The argument string
193+
* The putenv() function adds or changes the value of environment variables. The argument string
158194
* is of the form name=value. If name does not already exist in the environment, then string is added
159-
* to the environment. If name does exist, then the value of name in the environment is changed
195+
* to the environment. If name does exist, then the value of name in the environment is changed
160196
* to value. The string pointed to by string becomes part of the environment, so
161197
* altering the string changes the environment.
162198
*
@@ -170,7 +206,7 @@ int dev_putenv(const char *str) {
170206
}
171207

172208
/**
173-
* The getenv() function searches the environment list for a string that matches the string pointed
209+
* The getenv() function searches the environment list for a string that matches the string pointed
174210
* to by name. The strings are of the form name = value.
175211
*
176212
* The getenv() function returns a pointer to the value in the environment, or NULL if there is no match.

src/common/var.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ void v_setstrn(var_t *var, const char *string, int len);
519519
* @ingroup var
520520
*
521521
* Sets a string value to variable 'var'.
522-
* printf() style. Avoid to use on low-memory systems (like PalmOS 3.3).
522+
* printf() style.
523523
*
524524
* @param var is the variable
525525
* @param fmt is the the format

0 commit comments

Comments
 (0)