int execute(char **arguments) { if (arguments[0] == NULL) { return 1; } int length = sizeof(arguments); printf("%d\n", length); for(int x = 0; x < length-1; x++) { printf("%s\n", arguments[x]); } //for(int j = 0; j < (length-1); j++){ for (int i = 0; i < string_list_size(); i++) { if (strcmp(arguments[0], string_list[i]) == 0) { return (*string_func[i])(arguments); } else { return launch_shell(arguments); } } // } //return lsh_launch(args); }
static zx_status_t session_io_cb(port_fd_handler_t* fh, unsigned pollevt, uint32_t evt) { vc_t* vc = containerof(fh, vc_t, fh); if (pollevt & POLLIN) { char data[1024]; ssize_t r = read(vc->fd, data, sizeof(data)); if (r > 0) { vc_write(vc, data, r, 0); return ZX_OK; } } if (pollevt & (POLLRDHUP | POLLHUP)) { // shell sessions get restarted on exit if (vc->is_shell) { zx_task_kill(vc->proc); vc->proc = ZX_HANDLE_INVALID; int fd = openat(vc->fd, "0", O_RDWR); if (fd < 0) { goto fail; } if (launch_shell(vc, fd, NULL) < 0) { goto fail; } return ZX_OK; } } fail: session_destroy(vc); return ZX_ERR_STOP; }
int main(int argc, char **argv) { debug_init(); debug(INFO, "starting...\n"); int shell_pid = 0; pts_fn = fbterm_openpty(&pty); if (shell_pid = fork()) { if (fb_init("/dev/fb0")) { exit(-1); } if (fbterm_init(&term[0]) < 0) { debug(1, "Error initalizing fbterm\n"); exit(-1); } kbd_fd = open(KBD_PATH, O_RDONLY); if (kbd_fd < 0) { debug(1, "Error initalizing keyboard\n"); exit(-1); } pthread_t aqkb; pthread_create(&aqkb, NULL, aqkb_thread, NULL); fbterm_main(); } else { launch_shell(); } return 0; }
int main(int argc, char* argv[]) { char filename[FILE_NAME_SIZE] = ""; int will_launch_shell = TRUE; int read_from_file = FALSE; /* initialize memory to all zero */ int memory[MEMORY_SIZE] = {0}; int i = 0; for (i = 0; i < argc; ++i) { if ( strncmp(argv[i], "-f", sizeof("-f")) == 0 ) { will_launch_shell = FALSE; read_from_file = TRUE; strncat(filename, argv[i+1], FILE_NAME_SIZE); } } /* decide if reading from file, or shell */ if (will_launch_shell) { i = launch_shell(memory); } else if (read_from_file) { i = load_sml_file(filename, memory); } else { puts("An error occured. Shutting down"); exit(EXIT_FAILURE); } if (i > MEMORY_SIZE) { puts("Out of memory! Shutting down"); exit(EXIT_FAILURE); } else { puts("*** Program loading completed ***"); puts("*** Program execution begins... ***"); } /* initialize special registers */ int accumulator = 0; int instruction_counter = 0; int instruction_register = 0; int math_opperand_register = 0; int op_code = 0; int operand = 0; /* execute instructions */ for (instruction_counter = 0; instruction_counter < i; ++instruction_counter) { instruction_register = memory[instruction_counter]; if ((instruction_register > 9999) || (instruction_register < -9999)) { puts("*** ERROR: instruction too large for memory ***"); puts("*** Shutting down ***"); exit(EXIT_FAILURE); } Opcode op_code = instruction_register / 100; operand = instruction_register % 100; switch (op_code) { /* Read a word from the terminal into memory */ case READ: printf("? "); scanf("%d", &memory[operand]); break; /* Read a word from the terminal as ascii into memory */ case READ_ASCII: printf("? "); scanf("%s", &memory[operand]); break; /* Write a word from memory to the terminal */ case WRITE: printf("%d", memory[operand]); break; /* Write a word from memory in its ascii representation */ case WRITE_ASCII: printf("%c", memory[operand]); break; /* Write newline to the terminal */ case WRITE_NEWLINE: printf("\n"); break; /* Load a word from memory to the accumulator */ case LOAD: accumulator = memory[operand]; break; /* Store a word from the accumulator into memory */ case STORE: memory[operand] = accumulator; break; /* Arithmetic opperations */ case ADD: math_opperand_register = memory[operand]; accumulator += math_opperand_register; break; case SUBTRACT: math_opperand_register = memory[operand]; accumulator -= math_opperand_register; break; case DIVIDE: math_opperand_register = memory[operand]; accumulator /= math_opperand_register; break; case MULTIPLY: math_opperand_register = memory[operand]; accumulator *= math_opperand_register; break; case MOD: math_opperand_register = memory[operand]; accumulator %= math_opperand_register; break; /* unconditional branch */ case BRANCH: instruction_counter = operand; break; /* branch if zero */ case BRANCHZERO: if (accumulator == 0) { instruction_counter = operand; } break; /* branch if negative */ case BRANCHNEG: if (accumulator < 0) { instruction_counter = operand; } break; /* display memory and registers */ case HALT: puts("*** Simpletron execution terminated ***"); computer_dump(accumulator, instruction_counter, instruction_register, math_opperand_register, op_code, operand, memory, MEMORY_SIZE); break; case NOOP: break; default: printf("*** %3d: not a valid op_code ***\n", op_code); puts( "*** Execution FAILED shutting down ***"); computer_dump(accumulator, instruction_counter, instruction_register, math_opperand_register, op_code, operand, memory, MEMORY_SIZE); exit(EXIT_FAILURE); break; } } // end for loop return 0; }
static zx_status_t session_create(vc_t** out, int* out_fd, bool make_active, bool special) { int fd; // The ptmx device can start later than these threads int retry = 30; while ((fd = open("/dev/misc/ptmx", O_RDWR | O_NONBLOCK)) < 0) { if (--retry == 0) { return ZX_ERR_IO; } usleep(100000); } int client_fd = openat(fd, "0", O_RDWR); if (client_fd < 0) { close(fd); return ZX_ERR_IO; } vc_t* vc; if (vc_create(&vc, special)) { close(fd); close(client_fd); return ZX_ERR_INTERNAL; } zx_status_t r; if ((r = port_fd_handler_init(&vc->fh, fd, POLLIN | POLLRDHUP | POLLHUP)) < 0) { vc_destroy(vc); close(fd); close(client_fd); return r; } vc->fd = fd; if (make_active) { vc_set_active(-1, vc); } pty_window_size_t wsz = { .width = vc->columns, .height = vc->rows, }; ioctl_pty_set_window_size(fd, &wsz); vc->fh.func = session_io_cb; *out = vc; *out_fd = client_fd; return ZX_OK; } static void start_shell(bool make_active, const char* cmd) { vc_t* vc; int fd; if (session_create(&vc, &fd, make_active, cmd != NULL) < 0) { return; } vc->is_shell = true; if (launch_shell(vc, fd, cmd) < 0) { session_destroy(vc); } else { port_wait(&port, &vc->fh.ph); } }