/* stack operator **nest** > Creates a fresh stack with current TOS on it. Saves current dstack onto > rstack. */ bvm_cache *nest(bvm_cache *this_bvm){ // nest# mword *nest_body = dstack_get(this_bvm,0); // mword *new_stack = dstack_get(this_bvm,1); mword *save_TOS = dstack_get(this_bvm,1); popd(this_bvm); popd(this_bvm); mword *save_dstack = (mword*)icar(this_bvm->dstack_ptr); mword *save_ustack = (mword*)icar(this_bvm->ustack_ptr); clear(this_bvm); // clear the stack //rgive(this_bvm, new_stack); pushd(this_bvm, save_TOS, IMMORTAL); mword *nest_return = (mword*)icdr(icar(this_bvm->code_ptr)); mword *nest_rstack_entry = consa2(this_bvm, save_dstack, consa2(this_bvm, save_ustack, consa2(this_bvm, nest_return, nil))); pushr(this_bvm, nest_rstack_entry, _hash8(C2B("/babel/tag/nest"))); this_bvm->code_ptr = consa2(this_bvm, nest_body,nil); icar(this_bvm->advance_type) = BVM_CONTINUE; return this_bvm; }
/* stack operator **take** > Takes TOS items from the stack and puts them into a list. > If TOS == -1, the entire stack is taken. */ bvm_cache *take(bvm_cache *this_bvm){ // take# int count = (int)icar(dstack_get(this_bvm,0)); popd(this_bvm); mword *result = nil; mword *list_entry; mword *temp; int i; if(count == -1){ // while(!is_nil(this_bvm->dstack_ptr)){ while(!dstack_empty(this_bvm)){ list_entry = dstack_get(this_bvm,0); result = consa2(this_bvm, list_entry, result); popd(this_bvm); } } else{ for(i=0;i<count;i++){ list_entry = dstack_get(this_bvm,0); result = consa2(this_bvm, list_entry, result); popd(this_bvm); } } pushd(this_bvm, result, IMMORTAL); return this_bvm; }
void main() { char prefix[50],ch,str[50],operand1[50],operand2[50],oper_ator[2]; int i=0,k=0,opndcnt=0; clrscr(); printf("\t\t\t * Prefix to Postfix *"); printf("\n\nEnter the Prefix Expression : "); gets(prefix); while((ch=prefix[i++])!='\0') { if(isalnum(ch)) { str[0]=ch; str[1]='\0'; pushd(str); operand_count++; if(operand_count>=2) { strcpy(operand2,popd()); strcpy(operand1,popd()); strcpy(str,operand1); strcat(str,operand2); ch=popr(); oper_ator[0]=ch; oper_ator[1]='\0'; strcat(str,oper_ator); pushd(str); operand_count-=1; } } else { pushr(ch); /* operator followed by single operand*/ if(operand_count==1) operand_count=0; } } if(!empty(topd)) { strcpy(operand2,popd()); strcpy(operand1,popd()); strcpy(str,operand1); strcat(str,operand2); ch=popr(); oper_ator[0]=ch; oper_ator[1]='\0'; strcat(str,oper_ator); pushd(str); } printf("The Postfix Expression is : "); puts(opnds[topd]); getch(); }
int rotate_stack(int n) { stack_entry_t *new_stack; size_t i; if(n == 0) return 0; if(pushd() != 0) return -1; new_stack = reallocarray(NULL, stack_size, sizeof(*stack)); if(new_stack == NULL) return -1; for(i = 0U; i < stack_top; ++i) { new_stack[(i + n)%stack_top] = stack[i]; } free(stack); stack = new_stack; return popd(); }
int run_cmd(int argc, char **argv) { if (strcmp(argv[0],"echo")==0) { return echo(argc,argv); } else if (strcmp(argv[0],"cd")==0 || strcmp(argv[0],"chdir")==0) { return cd(argc,argv); } else if (strcmp(argv[0],"exit")==0) { fprintf(stderr,"exit\n"); exit(0); return 0; } else if (strcmp(argv[0],"exec")==0) { return exec(argc,argv); } else if (strcmp(argv[0],"pushd")==0) { return pushd(argc,argv); } else if (strcmp(argv[0],"popd")==0) { return popd(argc,argv); } else if (strcmp(argv[0],"dirs")==0) { return dirs(argc,argv); } else if (strcmp(argv[0],"help")==0) { return help(argc,argv); } else if (strcmp(argv[0],"exitc")==0) { return exitc(argc,argv); } else if (strcmp(argv[0],"pwd")==0) { return pwd(argc,argv); } else if (strcmp(argv[0],"setpipe")==0) { return setpipe(argc,argv); } else if (strcmp(argv[0],"runpipe")==0) { return runpipe(argc,argv); } else { return run(argc,argv); } }
/* stack operator **zap** > Named after the Joy operator > Removes the TOS */ bvm_cache *zap(bvm_cache *this_bvm){ // zap# popd(this_bvm); return this_bvm; }
int serv_touch(char *arg) { char corr[40]; if (!arg || !arg[0]) return serv_list(NULL); if (!strstr(arg, ".conf")) { snprintf(corr, sizeof(corr), "%s.conf", arg); arg = corr; } pushd(FINIT_RCSD); if (!fexist(arg)) { popd(); if (!strstr(arg, "finit.conf")) errx(1, "Service %s is not enabled", arg); arg = FINIT_CONF; } /* libite:touch() follows symlinks */ if (utimensat(AT_FDCWD, arg, NULL, AT_SYMLINK_NOFOLLOW)) err(1, "Failed marking %s for reload", arg); return 0; }
/* stack operator **give** > Undoes take */ bvm_cache *give(bvm_cache *this_bvm){ // give# mword *list = dstack_get(this_bvm,0); popd(this_bvm); rgive(this_bvm,(mword*)list); return this_bvm; }
/* stack operator **down** (<-) > Analogous to doing a cdr on the stack itself. If the code\_ptr > becomes nil after executing one or more downs without a balancing > up, as many ups as are required to undo the downs will be executed > automatically. */ bvm_cache *down(bvm_cache *this_bvm){ // down# if(is_nil(this_bvm->dstack_ptr)) return this_bvm; pushu(this_bvm, dstack_get(this_bvm,0), IMMORTAL); popd(this_bvm); return this_bvm; }
//big ass switchy thingy int find_and_exec(const char * command_name, const char * parameters){ int ret; if(command_name == NULL){ return 0; } else if(strcmp(command_name,"pwd") == 0){ return pwd(); } else if(strcmp(command_name,"cd") == 0){ return cd(parameters); } else if(strcmp(command_name,"echo") == 0){ return echo(parameters); } else if(strcmp(command_name,"pushd") == 0){ return pushd(parameters); } else if(strcmp(command_name,"popd") == 0){ return popd(); } else if(strcmp(command_name,"exit") == 0){ exitc(parameters); } else if(strcmp(command_name,"set") == 0){ printf("Did you mean export? If you are looking for windows try your walls\n"); return 0; } else if(strcmp(command_name,"history") == 0){ print_stack_lines(history); return 0; } else if(strcmp(command_name,"unsetenv") == 0){ return unsetenv(parameters); } else { int pid = fork(); if(pid == -1){return 1;} else if(pid == 0){ if(parameters == NULL) { execlp(command_name,command_name,NULL); } else { execvp(command_name,split_spaces_cmd(command_name,parameters)); } } else { wait(&ret); return ret; } } return 0; }
static void rmfiles (char *prefix) { #ifdef _WIN32 /* Win32 doesn't have POSIX dirent functions. */ WIN32_FIND_DATA ffd; HANDLE hnd; int go_on; string temp = concat (prefix, "*"); string directory = xdirname (prefix); pushd (directory); hnd = FindFirstFile(temp, &ffd); go_on = (hnd != INVALID_HANDLE_VALUE); while (go_on) { /* FIXME: can this remove read-only files also, like rm -f does? */ DeleteFile(ffd.cFileName); go_on = (FindNextFile(hnd, &ffd) == TRUE); } FindClose(hnd); free(temp); popd (); #else /* not _WIN32 */ DIR *dp; struct dirent *de; int temp_len = strlen (prefix); string directory = "./"; const_string base = xbasename (prefix); /* Copy the directory part of PREFIX with the trailing slash, if any. */ if (base != prefix) { directory = (string) xmalloc (base - prefix + 1); directory[0] = '\0'; strncat (directory, prefix, base - prefix); } /* Find matching files and delete them. */ if ((dp = opendir (directory)) != 0) { while ((de = readdir (dp)) != 0) { string found = concat (directory, de->d_name); if (FILESTRNCASEEQ (found, prefix, temp_len)) /* On POSIX-compliant systems, including DJGPP, this will also remove read-only files and empty directories, like rm -f does. */ if (remove (found)) perror (found); free (found); } } #endif /* not _WIN32 */ }
static void do_list(const char *heading, const char *path) { int num; int prev; int width; char buf[screen_cols]; size_t i, len; glob_t gl; pushd(path); if (glob("*.conf", 0, NULL, &gl)) { chdir(cwd); return; } if (gl.gl_pathc <= 0) goto done; memset(buf, 0, sizeof(buf)); snprintf(buf, sizeof(buf), "%s :: %s ", heading, path); len = strlen(buf); for (size_t i = len; i < (sizeof(buf) - 1); i++) buf[i] = '-'; printf("\e[1m%s\e[0m\n", buf); width = calc_width(gl.gl_pathv, gl.gl_pathc); if (width <= 0) goto done; num = (screen_cols - 2) / width; if ((num - 1) * 2 + num * width > screen_cols) num--; prev = 0; for (i = 0; i < gl.gl_pathc; i++) { if (i > 0 && !(i % num)) { puts(""); prev = 0; } if (prev) printf(" "); printf("%-*s", width, gl.gl_pathv[i]); prev++; } puts("\n"); done: globfree(&gl); popd(); }
/* stack operator **sel** (?) > Selects one of the top two values on the stack: > `{f} {X} {Y}| -> {X}|` > `{t} {X} {Y}| -> {Y}|` > `{f} [X] {Y}| -> [X]|` > `{t} [X] {Y}| -> {Y}|` > etc. > > Where f = 0 and t != 0 */ bvm_cache *sel(bvm_cache *this_bvm){ // sel# mword *temp = pop_udr_stack(this_bvm->dstack_ptr); if(!is_false(icar(temp))){ popd(this_bvm); } else{ remove_from_udr_stack(this_bvm, this_bvm->dstack_ptr, 1); } return this_bvm; }
void mt_exit(int code) { #if 0 if (cwd) { popd(); } #endif if (cleanup) { if (dry_run) fprintf(stderr, "%s: doing cleanup ...\n", progname); else do_cleanup(); } if (newdest) { DeleteFile(texfile); } exit(code); }
/* Executes a builtin command, redirecting input and output as appropriate. * and output as appropriate. As with all exec_XXX() functions, assumes that * the parent has forked if necessary, but unlike the others, returns the status * of the command executed. * * @simple = CMD to be executed * * Returns: Status of command executed */ int exec_builtin(CMD* simple) { // Keep track of old stdin, stdout, stderr for after execution of built-in int old_stdin = dup(0); int old_stdout = dup(1); int old_stderr = dup(2); // Failures for built-in commands to redirect should not cause the command // to abort, but it should set ? appropriately int redirect_status = 0; if (simple->fromFile && redirect_input(simple) < 0) redirect_status = errno; if (simple->toFile && redirect_output(simple) < 0) { int fd_devnull = open("/dev/null", O_WRONLY); dup2(fd_devnull, 1); if (ISREDERR(simple->toType)) dup2(fd_devnull, 2); close(fd_devnull); redirect_status = errno; } char* cmd_name = simple->argv[0]; int builtin_status = 0; if (!strcmp(cmd_name, "cd")) { builtin_status = cd(simple->argc, simple->argv); } else if (!strcmp(cmd_name, "pushd")) { builtin_status = pushd(simple->argc, simple->argv); } else if (!strcmp(cmd_name, "popd")) { builtin_status = popd(simple->argc, simple->argv); } else { builtin_status = 1; } dup2(old_stdin, 0); close(old_stdin); dup2(old_stdout, 1); close(old_stdout); dup2(old_stderr, 2); close(old_stderr); return builtin_status ? builtin_status : redirect_status; }