struct variable *sys_forkexec(struct context *context) { struct variable *args = (struct variable*)stack_pop(context->operand_stack); bool wait = param_bool(args, 1); const char *app = param_str(args, 2); uint32_t argc = args->list.ordered->length - 3; char **argv = malloc(sizeof(char*) * (argc+1)); for (int i=2; i<argc+3; i++) { //printf("arg %d/%D = %s\n", i-2, argc, param_str(args, i)); argv[i-2] = param_str(args, i); } argv[argc+1] = NULL; pid_t pid = fork(); if (pid < 0) perror("fork"); else if (pid == 0) // child { if (execv(app, argv) < 0) perror("execv"); printf("fork child exit\n"); exit(0); } else if (wait) // parent waits for child to finish { int status; if (waitpid(pid, &status, 0) != pid) perror("waitpid"); } return variable_new_int(context, pid); }
struct variable *sys_alert(struct context *context) { struct variable *args = (struct variable*)stack_pop(context->operand_stack); const char *title = param_str(args, 1); const char *message = param_str(args, 2); hal_alert(title, message); return NULL; }
struct variable *sys_button(struct context *context) { struct variable *value = (struct variable*)stack_pop(context->operand_stack); struct variable *uictx = (struct variable*)array_get(value->list.ordered, 1); struct variable *logic = param_var(value, 2); char *text = param_str(value, 3); char *image = param_str(value, 4); void *widget = param_void(value, 5); int32_t w=0,h=0; void *button = hal_button(context, uictx, &w, &h, logic, text, image, widget); return ui_result(context, button, w, h); }
struct variable *sys_file_listen(struct context *context) { struct variable *arguments = (struct variable*)stack_pop(context->operand_stack); const char *path = param_str(arguments, 1); struct variable *listener = param_var(arguments, 2); gil_unlock(context, "sys_file_listen"); hal_file_listen(context, path, listener); return NULL; }
// deletes file or folder struct variable *sys_mv(struct context *context) { struct variable *value = (struct variable*)stack_pop(context->operand_stack); const char *src = param_str(value, 1); const char *dst = param_str(value, 2); long timestamp = param_int(value, 3); assert_message((strlen(src)>1) && (strlen(dst)>1), "oops"); //printf("mv %s to %s\n", src, dst); create_parent_folder_if_needed(dst); if (rename(src, dst)) perror("rename"); if (timestamp) // to prevent unwanted timestamp updates resulting from the mv file_set_timestamp(dst, timestamp); return NULL; }
struct variable *sys_label(struct context *context) { struct variable *arguments = (struct variable*)stack_pop(context->operand_stack); struct variable *uictx = (struct variable*)array_get(arguments->list.ordered, 1); const char *str = param_str(arguments, 2); void *widget = param_void(arguments, 3); int32_t w=0,h=0; void *label = hal_label(uictx, &w, &h, str, widget); return ui_result(context, label, w, h); }
struct variable *sys_input(struct context *context) { struct variable *arguments = (struct variable*)stack_pop(context->operand_stack); struct variable *uictx = (struct variable*)array_get(arguments->list.ordered, 1); const char *hint = param_str(arguments, 2); bool multiline = param_bool(arguments, 3); bool readonly = param_bool(arguments, 4); void *widget = param_void(arguments, 5); int32_t w=0, h=0; void *input = hal_input(uictx, &w, &h, hint, multiline, readonly, widget); return ui_result(context, input, w, h); }
struct variable *sys_send(struct context *context) { struct variable *arguments = (struct variable*)stack_pop(context->operand_stack); struct variable *sender = (struct variable*)array_get(arguments->list, 1); const char *message = param_str(arguments, 2); assert_message(sender->type == VAR_INT, "non int fd"); int32_t fd = sender->integer; struct thread_argument *ta = (struct thread_argument*)map_get(socket_listeners, (void*)(VOID_INT)fd); printf("send on ssl=%p\n", ta->ssl); if (CyaSSL_write(ta->ssl, message, strlen(message)) != strlen(message)) context->vm_exception = variable_new_str(context, byte_array_from_string("CyaSSL_write error")); return NULL; }
unsigned int CommandMgr::GetOpcodeFromParam(char* param) { if (!param) return 0; long opcode; std::string param_str(param); if (param_str.find("0x") != std::string::npos) opcode = strtol(param, NULL, 0); else opcode = atol(param); if (opcode > 0xFFFF || opcode < 0) return 0; return opcode; }
//-----------------------------------------------------------------------------------------------// int jpi_encode_param(const char* param, char* newParam, const int size) { try { std::string param_str(param); int offset; size_t p; // : offset = 0; while (true) { p = param_str.find_first_of(':', offset); if (p != std::string::npos) { param_str.insert(p, ":"); offset = (p + 2); } else { break; } } //Return int s = param_str.length(); if (s < size) { memcpy(newParam, param_str.c_str(), s); newParam[s] = 0; return s; } else { memcpy(newParam, param_str.c_str(), size); newParam[size-1] = 0; setLastError_I(JPI_ENP_TOOSMALLBUFFEE_E); return size; } return JPI_NO_ERROR; } catch ( ... ) { setLastError_I(JPI_EXC_ERROR); return 0; } }
int eval_node_redir1(NodeType *cmd, NodeType *file) { if (close(1) < 0) { err_sys("cannot close stdout"); } else { char *fname = param_str(file); int fd = open(fname, O_CREAT|O_WRONLY, 0666); if (fd < 0) { char err_msg[256]; sprintf(err_msg, "open file ~s failed", fname); err_sys(err_msg); } else { // fd is 1 int ret = eval_node_cmd(cmd); if (close(fd)) { char err_msg[256]; sprintf(err_msg, "open file ~s failed", fname); err_sys(err_msg); } return 0; } } }
// eval the command in current process int eval_cmd_in_proc(NodeType *pn) { char *cmd = cmd_cmd_str(pn); NodeType *params = cmd_params(pn); int len = list_length(params); char **param_arr = (char**)malloc((len+2) * sizeof(char*)); int i = 0; param_arr[0] = cmd; param_arr[len+1] = NULL; NodeType *head = params; for(i = 0; i < len; i++) { param_arr[i+1] = param_str(pair_car(head)); head = pair_cdr(head); } if (execvp(cmd, param_arr) < 0) { err_sys("execvp failed"); free(param_arr); return -1; } else { free(param_arr); return 0; } }
struct variable *sys_connect(struct context *context) { struct variable *arguments = (struct variable*)stack_pop(context->operand_stack); const char *serveraddr = param_str(arguments, 1); const int32_t serverport = param_int(arguments, 2); struct variable *listener = ((struct variable*)array_get(arguments->list, 2)); int sockfd; struct sockaddr_in servaddr; CYASSL_CTX* ctx; CYASSL* ssl; node_init(); // Create and initialize CYASSL_CTX structure if ( (ctx = CyaSSL_CTX_new(CyaTLSv1_client_method())) == NULL) { context->vm_exception = variable_new_str(context, byte_array_from_string("SSL_CTX_new error")); CyaSSL_Cleanup(); return NULL; } // Load CA certificates into CYASSL_CTX if (CyaSSL_CTX_load_verify_locations(ctx, "./conf/ca-cert.pem", 0) != SSL_SUCCESS) { context->vm_exception = variable_new_str(context, byte_array_from_string("Error loading ca-cert.pem, please check the file.\n")); CyaSSL_CTX_free(ctx); CyaSSL_Cleanup(); return NULL; } // Create Socket file descriptor sockfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(serverport); inet_pton(AF_INET, serveraddr, &servaddr.sin_addr); // Blocking Connect to socket file descriptor connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); // Create CYASSL object if ((ssl = CyaSSL_new(ctx)) == NULL) { context->vm_exception = variable_new_str(context, byte_array_from_string("CyaSSL_new error")); CyaSSL_CTX_free(ctx); CyaSSL_Cleanup(); return NULL; } CyaSSL_set_fd(ssl, sockfd); fprintf(stderr, "Connected on %d -- %p\n", sockfd, ssl); struct thread_argument *ta = (struct thread_argument *)malloc(sizeof(struct thread_argument)); ta->find = context->find; ta->listener = listener; ta->ssl = ssl; ta->fd = sockfd; ta->cya = ctx; if (socket_listeners == NULL) socket_listeners = map_new_ex(NULL, &int_compare, &int_hash, &int_copy, &int_del); map_insert(socket_listeners, (void*)(VOID_INT)sockfd, (void*)(VOID_INT)ta); return variable_new_int(context, sockfd); }