Skip to content

Commit c63efce

Browse files
committed
setEnv
1 parent eb6ecda commit c63efce

File tree

7 files changed

+88
-3
lines changed

7 files changed

+88
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ This is in it's very early stage... it only supports one liners!
794794
795795
Some short tips:
796796
help() gives you a list of available functions
797-
printEnv() lists the names of variables defined in this session
797+
printVars() lists the names of variables defined in this session
798798
quit() quits this interpreter interactive mode
799799
800800
>> a = 1337
@@ -839,7 +839,7 @@ This is in it's very early stage... it only supports one liners!
839839
840840
Some short tips:
841841
help() gives you a list of available functions
842-
printEnv() lists the names of variables defined in this session
842+
printVars() lists the names of variables defined in this session
843843
quit() quits this interpreter interactive mode
844844
845845
>> help()

samples/ric_lib.ric

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,17 @@ print("'1336' as a string + 1 is: '" + stringed + "'")
5959
testString = "The length of this text before the column including the space behind and the column itself is: "
6060
print(testString + len(testString))
6161

62-
exitCode = 2
6362

63+
64+
# Manipulating the environment
65+
# Setting a variable
66+
setEnv("HELLO_WORLD", "HI")
67+
environment = listEnv()
68+
? [ environment.contains("HELLO_WORLD") ] {
69+
print(getEnv("HELLO_WORLD"))
70+
}
71+
72+
exitCode = 2
6473
print ( "I will exit with exit code: " + exitCode )
6574

6675
exit ( exitCode )

src/lib.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ libFunction_t ric_library[] = {
135135
DECLARE_LIB_FUNCTION("find", 1, ric_find_files),
136136
DECLARE_LIB_FUNCTION("cd", 1, ric_cd),
137137
DECLARE_LIB_FUNCTION("osName", 0, ric_os_name),
138+
DECLARE_LIB_FUNCTION("setEnv", 2, ric_set_env),
138139
// libctx
139140
DECLARE_LIB_FUNCTION("setTimeout", 2, ric_set_timeout),
140141
DECLARE_LIB_FUNCTION("setInterval", 2, ric_set_interval),

src/library/libos.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,39 @@ int ric_env_keys(LIBRARY_PARAMS()) {
513513
PUSH_VECTOR(stv.vec, sp, sc);
514514
return 0;
515515
}
516+
517+
int ric_set_env(LIBRARY_PARAMS()) {
518+
stackval_t stv_key, stv_val;
519+
heapval_t *hpv;
520+
int dummy;
521+
void *sp = PROVIDE_CONTEXT()->sp;
522+
size_t *sc = PROVIDE_CONTEXT()->sc;
523+
void *hp = PROVIDE_CONTEXT()->hp;
524+
525+
// Pop value first, then key (stack order)
526+
POP_VAL(&stv_key, sp, sc);
527+
POP_VAL(&stv_val, sp, sc);
528+
529+
if (stv_key.type != TEXT || stv_val.type != TEXT) {
530+
fprintf(stderr, "error: function '%s' expected (string, string) as arguments.\n",
531+
LIBRARY_FUNC_NAME());
532+
exit(1);
533+
}
534+
535+
char *key = stv_key.t;
536+
char *val = stv_val.t;
537+
538+
if (setenv(key, val, 1) != 0) {
539+
perror("setenv failed");
540+
exit(1);
541+
}
542+
543+
// Return empty string as success indicator
544+
stackval_t stv;
545+
stv.type = TEXT;
546+
stv.t = strdup("");
547+
ALLOC_HEAP(&stv, hp, &hpv, &dummy);
548+
PUSH_STRING(stv.t, sp, sc);
549+
550+
return 0;
551+
}

src/library/libos.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ int ric_os_name(LIBRARY_PARAMS());
2222
int ric_mkdir(LIBRARY_PARAMS());
2323
int ric_find_files(LIBRARY_PARAMS());
2424
int ric_env_keys(LIBRARY_PARAMS());
25+
int ric_set_env(LIBRARY_PARAMS());
2526

2627
#endif

src/library/libos_nt.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,40 @@ int ric_env_keys(LIBRARY_PARAMS()) {
509509
PUSH_VECTOR(stv.vec, sp, sc);
510510
return 0;
511511
}
512+
513+
int ric_set_env(LIBRARY_PARAMS()) {
514+
stackval_t stv_key, stv_val;
515+
heapval_t *hpv;
516+
int dummy;
517+
expr_t *ret = NULL;
518+
void *sp = PROVIDE_CONTEXT()->sp;
519+
size_t *sc = PROVIDE_CONTEXT()->sc;
520+
void *hp = PROVIDE_CONTEXT()->hp;
521+
522+
// Pop value first, then key (stack order)
523+
POP_VAL(&stv_key, sp, sc);
524+
POP_VAL(&stv_val, sp, sc);
525+
526+
if (stv_key.type != TEXT || stv_val.type != TEXT) {
527+
fprintf(stderr, "error: function '%s' expected (string, string) as arguments.\n",
528+
LIBRARY_FUNC_NAME());
529+
exit(1);
530+
}
531+
532+
char *key = stv_key.t;
533+
char *val = stv_val.t;
534+
535+
if (_putenv_s(key, val) != 0) {
536+
fprintf(stderr, "error: _putenv_s failed in '%s' for key='%s'\n", LIBRARY_FUNC_NAME(), key);
537+
exit(1);
538+
}
539+
540+
// Return empty string as success indicator
541+
stackval_t stv;
542+
stv.type = TEXT;
543+
stv.t = strdup("");
544+
ALLOC_HEAP(&stv, hp, &hpv, &dummy);
545+
PUSH_STRING(stv.t, sp, sc);
546+
547+
return 0;
548+
}

tests/test_ric_lib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def test_output():
5454
"0",
5555
"'1336' as a string + 1 is: '1337'",
5656
"The length of this text before the column including the space behind and the column itself is: 95",
57+
"HI",
5758
"I will exit with exit code: 2"
5859
]
5960

0 commit comments

Comments
 (0)