bool genheader(compile_t* c) { // Open a header file. const char* file_h = suffix_filename(c->opt->output, "", c->filename, ".h"); FILE* fp = fopen(file_h, "wt"); if(fp == NULL) { errorf(NULL, "couldn't write to %s", file_h); return false; } fprintf(fp, "#ifndef pony_%s_h\n" "#define pony_%s_h\n" "\n" "/* This is an auto-generated header file. Do not edit. */\n" "\n" "#include <stdint.h>\n" "#include <stdbool.h>\n" "\n" "#ifdef __cplusplus\n" "extern \"C\" {\n" "#endif\n" "\n" "#ifdef _MSC_VER\n" "typedef struct __int128_t { uint64_t low; int64_t high; } __int128_t;\n" "typedef struct __uint128_t { uint64_t low; uint64_t high; } " "__uint128_t;\n" "#endif\n" "\n", c->filename, c->filename ); printbuf_t* buf = printbuf_new(); print_types(c, fp, buf); fwrite(buf->m, 1, buf->offset, fp); printbuf_free(buf); fprintf(fp, "\n" "#ifdef __cplusplus\n" "}\n" "#endif\n" "\n" "#endif\n" ); fclose(fp); return true; }
static int features_parse_option(struct tunefs_operation *op, char *arg) { int rc = 1; errcode_t err; struct feature_op_state *state = NULL; struct check_supported_context ctxt = { .sc_string = arg, }; if (!arg) { errorf("No features specified\n"); goto out; } err = ocfs2_malloc0(sizeof(struct feature_op_state), &state); if (err) { tcom_err(err, "while processing feature options"); goto out; } state->fo_op = op; err = ocfs2_parse_feature(arg, &state->fo_feature_set, &state->fo_reverse_set); if (err) { tcom_err(err, "while parsing feature options \"%s\"", arg); goto out; } ctxt.sc_state = state; ctxt.sc_action = FEATURE_ENABLE; ocfs2_feature_foreach(&state->fo_feature_set, check_supported_func, &ctxt); if (ctxt.sc_error) goto out; ctxt.sc_action = FEATURE_DISABLE; ocfs2_feature_reverse_foreach(&state->fo_reverse_set, check_supported_func, &ctxt); if (ctxt.sc_error) goto out; rc = 0; out: if (!rc) op->to_private = state; else if (state) ocfs2_free(&state); return rc; }
int procinit(Proc *p, void (*fn)(void*), void *arg, int stksz) { Thread *t; t = createthread(fn, arg, stksz); if(t == nil){ errorf("procinit -- could not create thread\n"); return -1; } p->t = t; return 0; }
static void handle_attribute_asm(const attribute_t *attribute, entity_t *entity) { attribute_argument_t *argument = attribute->a.arguments; assert(argument->kind == ATTRIBUTE_ARGUMENT_EXPRESSION); expression_t *expression = argument->v.expression; if (expression->kind != EXPR_STRING_LITERAL) errorf(&attribute->pos, "Invalid asm attribute expression"); symbol_t *sym = symbol_table_insert(expression->string_literal.value->begin); entity->function.actual_name = sym; assert(argument->next == NULL); return; }
/* * Move fd from user space (0 <= fd < 10) to shell space (fd >= 10), * set close-on-exec flag. See FDBASE in sh.h, maybe 24 not 10 here. */ short savefd(int fd) { int nfd = fd; if (fd < FDBASE && (nfd = fcntl(fd, F_DUPFD, FDBASE)) < 0 && errno == EBADF) return (-1); if (nfd < 0 || nfd > SHRT_MAX) errorf("too many files open in shell"); fcntl(nfd, F_SETFD, FD_CLOEXEC); return ((short)nfd); }
/* * Move fd from user space (0 <= fd < 10) to shell space (fd >= 10), * set close-on-exec flag. See FDBASE in sh.h, maybe 24 not 10 here. */ short savefd(int fd) { int nfd = fd; if (fd < FDBASE && (nfd = fcntl(fd, F_DUPFD, FDBASE)) < 0 && (errno == EBADF || errno == EPERM)) return (-1); if (nfd < 0 || nfd > SHRT_MAX) errorf(Ttoo_many_files); fcntl(nfd, F_SETFD, FD_CLOEXEC); return ((short)nfd); }
/*-------------------------------------------------------------------------*/ string_t * trim_all_spaces (const string_t * txt) /* Trim the input string <txt> by removing all leading and trailing * space, and by folding embedded space runs into just one each. * Return the new string with one ref; the refcount of <txt> is not changed. * * Throw an error when out of memory. */ { char * dest; const char * src; size_t dest_ix, src_ix, srclen; string_t * rc; dest = alloca(mstrsize(txt)); if (dest == NULL) errorf("Stack overflow (%zu bytes)\n", mstrsize(txt)); src = get_txt((string_t *const)txt); srclen = mstrsize(txt); src_ix = 0; dest_ix = 0; /* Blank out trailing spaces */ while (srclen > 0 && src[srclen-1] == ' ') srclen--; /* Skip leading spaces */ while (src_ix < srclen && *src == ' ') src_ix++, src++; /* Copy characters, but fold embedded spaces. */ for ( ; src_ix < srclen; src_ix++, src++, dest_ix++) { dest[dest_ix] = *src; /* If this and the next character is a space, forward * src until the last space in this run. */ if (' ' == *src) { while (src_ix+1 < srclen && ' ' == src[1]) src_ix++, src++; } } memsafe(rc = new_n_mstring(dest, dest_ix), dest_ix, "trimmed result"); return rc; } /* trim_all_spaces() */
static void addr(Node *n) { int sz; int offset; Sym *sym; switch(n->t) { case NUNOP: expr(n->Unop.operand); break; case NSEL: expr(n->Sel.operand); if(isptr(n->Sel.operand->type)) offset = structoffsetfromname(n->Sel.operand->type->Ptr.subty, n->Sel.name); else if(isstruct(n->Sel.operand->type)) offset = structoffsetfromname(n->Sel.operand->type, n->Sel.name); else panic("internal error"); if(offset < 0) panic("internal error"); outi("addq $%d, %%rax\n", offset); break; case NIDENT: sym = n->Ident.sym; switch(sym->k) { case SYMGLOBAL: outi("leaq %s(%%rip), %%rax\n", sym->Global.label); break; case SYMLOCAL: outi("leaq %d(%%rbp), %%rax\n", sym->Local.slot->offset); break; default: panic("internal error"); } break; case NIDX: expr(n->Idx.idx); sz = n->type->size; if(sz != 1) { outi("imul $%d, %%rax\n", sz); } pushq("rax"); expr(n->Idx.operand); popq("rcx"); outi("addq %%rcx, %%rax\n"); break; default: errorf("unimplemented addr\n"); } }
// utime int fs_entry_utime( struct fs_core* core, char const* path, struct utimbuf* tb, uint64_t user, uint64_t volume ) { int err = 0; uint64_t parent_id = 0; char* parent_name = NULL; struct fs_entry* fent = fs_entry_resolve_path_and_parent_info( core, path, user, volume, true, &err, &parent_id, &parent_name ); if( !fent || err ) { if( !err ) err = -ENOMEM; return err; } // check permissions if( tb == NULL && !IS_WRITEABLE( fent->mode, fent->owner, fent->volume, user, volume ) ) { fs_entry_unlock( fent ); return -EACCES; } if( tb != NULL && fent->owner != user ) { fs_entry_unlock( fent ); return -EACCES; } if( tb != NULL ) { fent->mtime_sec = tb->modtime; fent->atime = tb->actime; } else { struct timespec ts; clock_gettime( CLOCK_REALTIME, &ts ); fent->mtime_sec = ts.tv_sec; fent->mtime_nsec = ts.tv_nsec; fent->atime = fent->mtime_sec; } fent->atime = currentTimeSeconds(); // post update struct md_entry up; fs_entry_to_md_entry( core, &up, fent, parent_id, parent_name ); int rc = ms_client_update( core->ms, &fent->write_nonce, &up ); if( rc != 0 ) { errorf("ms_client_update(%s) rc = %d\n", path, rc ); } md_entry_free( &up ); fs_entry_unlock( fent ); return rc; }
static void rand_Get128BitsPipe (u8 buf128[16]) { FILE *f; MD5_CTX md5; u8 buf[1024]; size_t r, totr; debugf ("getting 128 bits by hashing output of %s", o_randomcmd); MD5Init (&md5); f = popen (o_randomcmd, "r"); if (!f) errorf (ERF_ERN|ERF_EXIT, "could not popen () command \"%s\" for random seeding", o_randomcmd); for (totr = 0; ;) { r = fread (buf, 1, sizeof (buf), f); if (r > 0) { MD5Update (&md5, buf, r); totr += r; } if (r < sizeof(buf)) { if (!feof (f)) errorf (ERF_ERN|ERF_EXIT, "error reading from pipe \"%s\"", o_randomcmd); break; } } if (totr < MINIMUM_PIPE_BYTES) errorf (ERF_EXIT, "pipe command \"%s\" must output at least %ld bytes (got %ld)", o_randomcmd, (long) MINIMUM_PIPE_BYTES, totr); if (pclose (f)) errorf (ERF_ERN|ERF_EXIT, "error on pclose ()"); MD5Final (buf128, &md5); }
int setup_back_program(char *backend, int f[2]) { int sock[2]; if (socketpair(PF_UNIX,SOCK_STREAM,0,sock) != 0) errorf("Couldn't create socket: %s\n",strerror(errno)); /* Now run the program */ switch (child_pid=fork()) { case -1: /* Error */ errorf("Error in fork(): %s\n",strerror(errno)); case 0: /* Child */ if (close(sock[0]) != 0) { errorf("Error in close(sock[0]): %s\n",strerror(errno)); } if (close(STDIN_FILENO) != 0) { errorf("Error in close(STDIN_FILENO): %s\n",strerror(errno)); } if (dup2(sock[1],STDIN_FILENO) != STDIN_FILENO) { errorf("Error in dup2(sock[1],STDIN_FILENO): %s\n",strerror(errno)); } if (close(STDOUT_FILENO) != 0) { errorf("Error in close(STDOUT_FILENO): %s\n",strerror(errno)); } if (dup2(sock[1],STDOUT_FILENO) != STDOUT_FILENO) { errorf("Error in dup2(sock[1],STDOUT_FILENO): %s\n",strerror(errno)); } if (close(sock[1]) != 0) { errorf("Error in close(sock[1]): %s\n",strerror(errno)); } execl("/bin/sh","sh","-c",backend,NULL); /* Only returns if there is an error. */ errorf("exec error: %s\n",strerror(errno)); } /* Parent */ return f[0]=f[1]=sock[0]; }
void shell_run(tree_t e, struct tree_rd_ctx *ctx) { const int ndecls = tree_decls(e); hash_t *decl_hash = hash_new(ndecls * 2, true); for (int i = 0; i < ndecls; i++) { tree_t d = tree_decl(e, i); hash_put(decl_hash, tree_ident(d), d); } Tcl_Interp *interp = Tcl_CreateInterp(); shell_cmd_t shell_cmds[] = { CMD(quit, NULL, "Exit simulation"), CMD(run, NULL, "Start or resume simulation"), CMD(restart, e, "Restart simulation"), CMD(show, decl_hash, "Display simulation objects"), CMD(help, shell_cmds, "Display this message"), CMD(copyright, NULL, "Display copyright information"), CMD(signals, e, "Find signal objects in the design"), CMD(now, NULL, "Display current simulation time"), CMD(watch, decl_hash, "Trace changes to a signal"), CMD(unwatch, decl_hash, "Stop tracing signals"), { NULL, NULL, NULL, NULL} }; qsort(shell_cmds, ARRAY_LEN(shell_cmds) - 1, sizeof(shell_cmd_t), compare_shell_cmd); for (shell_cmd_t *c = shell_cmds; c->name != NULL; c++) Tcl_CreateObjCommand(interp, c->name, c->fn, c->cd, NULL); show_banner(); char *line; while ((line = shell_get_line())) { switch (Tcl_Eval(interp, line)) { case TCL_OK: break; case TCL_ERROR: errorf("%s", Tcl_GetStringResult(interp)); break; } free(line); } printf("\nBye.\n"); Tcl_Finalize(); }
static int enable_backup_super(ocfs2_filesys *fs, int flags) { errcode_t err = 0; struct ocfs2_super_block *super = OCFS2_RAW_SB(fs->fs_super); struct tools_progress *prog; if (OCFS2_HAS_COMPAT_FEATURE(super, OCFS2_FEATURE_COMPAT_BACKUP_SB)) { verbosef(VL_APP, "Backup superblock feature is already enabled; " "nothing to enable\n"); goto out; } if (!tools_interact("Enable the backup superblock feature on " "device \"%s\"? ", fs->fs_devname)) goto out; prog = tools_progress_start("Enable backup-super", "backup-super", 2); if (!prog) { err = TUNEFS_ET_NO_MEMORY; tcom_err(err, "while initializing the progress display"); goto out; } tunefs_block_signals(); err = check_backup_offsets(fs); tools_progress_step(prog, 1); if (!err) err = fill_backup_supers(fs); if (!err) { super->s_feature_compat |= OCFS2_FEATURE_COMPAT_BACKUP_SB; err = ocfs2_write_super(fs); if (err) tcom_err(err, "while writing out the superblock\n"); } tunefs_unblock_signals(); tools_progress_step(prog, 1); tools_progress_stop(prog); if (err) errorf("Unable to enable the backup superblock feature on " "device \"%s\"\n", fs->fs_devname); out: return err; }
void openpipe(int *pv) { int lpv[2]; if (pipe(lpv) < 0) errorf("can't create pipe - try again"); pv[0] = savefd(lpv[0]); if (pv[0] != lpv[0]) close(lpv[0]); pv[1] = savefd(lpv[1]); if (pv[1] != lpv[1]) close(lpv[1]); }
Thread* createthread(void (*fn)(void*), void *arg, int stksz) { Thread *t; Lock runwait; pthread_attr_t at; pthread_t p; t = malloc(sizeof *t); if(t == nil) return nil; if(initlock(&runwait) != 0){ free(t); return nil; } pthread_attr_init(&at); if(stksz > 0){ if(pthread_attr_setstacksize(&at, stksz) != 0) errorf("createthread -- given stack size too small (%u)\n", stksz); } pthread_attr_setdetachstate(&at, PTHREAD_CREATE_JOINABLE); t->name = nil; t->fn = fn; t->arg = arg; t->runwait = runwait; lock(&t->runwait, 1); if(pthread_create(&p, &at, run, t) != 0){ errorf("createthread -- pthread_create failed\n"); free(t); return nil; } t->t = p; t->attr = at; unlock(&t->runwait); return t; }
// Attempt to parse the specified source file and add it to the given AST // @return true on success, false on error static bool parse_source_file(ast_t* package, const char* file_path, pass_opt_t* options) { assert(package != NULL); assert(file_path != NULL); source_t* source = source_open(file_path); if(source == NULL) { errorf(file_path, "couldn't open file %s", file_path); return false; } return module_passes(package, options, source); }
Cond* createcond(void) { Cond *p; if((p = malloc(sizeof *p)) == nil){ errorf("createcond -- out of memory\n"); return nil; } if(initcond(p) != 0){ free(p); return nil; } return p; }
int test_bare_file(void) { errorf("\n"); /* prints the function name */ File *file = newfile(".", "buf.c"); assert(strcmp(getname(file), "buf.c") == 0); assert(strcmp(getpath(file), "./buf.c") == 0); char *dir = getdirname(file); assert(strcmp(dir, ".") == 0); free(dir); char *base = getbasename(file); assert(strcmp(base, "buf.c") == 0); free(base); free(file); return 0; }
error *objectGoAddr(QObject_ *object, GoAddr **addr) { QObject *qobject = static_cast<QObject *>(object); GoValue *goValue = dynamic_cast<GoValue *>(qobject); if (goValue) { *addr = goValue->addr; return 0; } GoPaintedValue *goPaintedValue = dynamic_cast<GoPaintedValue *>(qobject); if (goPaintedValue) { *addr = goPaintedValue->addr; return 0; } return errorf("QML object is not backed by a Go value"); }
/* notationGet: Get the name of the notation currently in effect, including the preceeding '%' character. Caller has the responsibility of freeing the result. [Ash] */ char * notationGet(void) { Datum ret; if (currentNotation != NEWOTHER) errorf("notationGet: called when currentNotation != NEWOTHER"); makearr(0); pushMasterStack("notationGet"); call(lookup("notationGet", basecontext), pop(), 0); popMasterStack(); ret = pop(); muststr(ret); return strdup(ret.u.s); }
static const char *make_tempsubdir(const char *tempdir) { assert(obstack_object_size(&file_obst) == 0); obstack_printf(&file_obst, "%s/XXXXXX", tempdir); obstack_1grow(&file_obst, '\0'); char *templ = obstack_finish(&file_obst); const char *dir = mkdtemp(templ); if (dir == NULL) { position_t const pos = { templ, 0, 0, 0 }; errorf(&pos, "mkdtemp could not create a directory from template"); panic("abort"); } return dir; }
extern unsigned long dllversion(void* dll, const char* path) { Dll_plugin_version_f pvf; if (pvf = (Dll_plugin_version_f)dlllook(dll, "plugin_version")) return (*pvf)(); if (path) { state.error = 1; sfsprintf(state.errorbuf, sizeof(state.errorbuf), "plugin_version() not found"); errorf("dll", NiL, 1, "dllversion: %s: %s", path, state.errorbuf); } return 0; }
error *objectGoRef(QObject_ *object, GoRef *ref) { QObject *qobject = static_cast<QObject *>(object); GoValue *goValue = dynamic_cast<GoValue *>(qobject); if (goValue) { *ref = goValue->ref; return 0; } GoPaintedValue *goPaintedValue = dynamic_cast<GoPaintedValue *>(qobject); if (goPaintedValue) { *ref= goPaintedValue->ref; return 0; } return errorf("QML object is not backed by a Go value"); }
int test_relative_file(void) { errorf("\n"); /* prints the function name */ File *file = newfile(".", "../tmp/foo"); assert(strcmp(getname(file), "../tmp/foo") == 0); assert(strcmp(getpath(file), "./../tmp/foo") == 0); char *dir = getdirname(file); assert(strcmp(dir, "./../tmp") == 0); free(dir); char *base = getbasename(file); assert(strcmp(base, "foo") == 0); free(base); free(file); return 0; }
static int query_parse_option(struct tunefs_operation *op, char *arg) { int argtype; if (!arg) { errorf("No query format specified\n"); return 1; } /* * We want to make sure that there are no "standard" specifiers in * the format, only our own. */ if (parse_printf_format(arg, 1, &argtype)) { errorf("Unknown type specifier in the query format: " "\"%s\"\n", arg); return 1; } op->to_private = arg; return 0; }
bool process_all_units(compilation_env_t *env) { if (units == NULL) { errorf(NULL, "no input files specified"); return false; } for (compilation_unit_t *unit = units; unit != NULL; unit = unit->next) { if (unit->type == COMPILATION_UNIT_AUTODETECT) unit->type = autodetect_input(unit->name); if (unit->original_name && env->outname && streq(unit->original_name, env->outname)) { errorf(NULL, "output file '%s' is the same as the input file", env->outname); return false; } stat_ev_ctx_push_str("compilation_unit", unit->name); bool ok = process_unit(env, unit); stat_ev_ctx_pop("compilation_unit"); if (!ok) { return false; } } return true; }
bool package_init(pass_opt_t* opt) { // package_add_paths for command line paths has already been done. Here, we // append the paths from an optional environment variable, and then the paths // that are relative to the compiler location on disk. package_add_paths(getenv("PONYPATH"), opt); if(!add_exec_dir(opt)) { errorf(opt->check.errors, NULL, "Error adding package paths relative to ponyc binary location"); return false; } // Finally we add OS specific paths. #ifdef PLATFORM_IS_POSIX_BASED add_path("/usr/local/lib", opt); add_path("/opt/local/lib", opt); #endif // Convert all the safe packages to their full paths. strlist_t* full_safe = NULL; strlist_t* safe = opt->safe_packages; while(safe != NULL) { const char* path; safe = strlist_pop(safe, &path); // Lookup (and hence normalise) path. path = find_path(NULL, path, NULL, NULL, opt); if(path == NULL) { strlist_free(full_safe); strlist_free(safe); opt->safe_packages = NULL; return false; } full_safe = strlist_push(full_safe, path); } opt->safe_packages = full_safe; if(opt->simple_builtin) package_add_magic_src("builtin", simple_builtin, opt); return true; }
static void stmt(Node *n) { switch(n->t){ case NDECL: decl(n); out(".text\n"); break; case NRETURN: ereturn(n); break; case NIF: eif(n); break; case NWHILE: ewhile(n); break; case NFOR: efor(n); break; case NDOWHILE: dowhile(n); break; case NBLOCK: block(n); break; case NSWITCH: eswitch(n); break; case NGOTO: outi("jmp %s\n", n->Goto.l); break; case NCASE: out("%s:\n", n->Case.l); stmt(n->Case.stmt); break; case NLABELED: out("%s:\n", n->Labeled.l); stmt(n->Labeled.stmt); break; case NEXPRSTMT: if(n->ExprStmt.expr) expr(n->ExprStmt.expr); break; default: errorf("unimplemented emit stmt %d\n", n->t); } }
static bool reachable_actors(compile_t* c, ast_t* program) { // Look for C-API actors in every package. bool found = false; ast_t* package = ast_child(program); while(package != NULL) { ast_t* module = ast_child(package); while(module != NULL) { ast_t* entity = ast_child(module); while(entity != NULL) { if(ast_id(entity) == TK_ACTOR) { ast_t* c_api = ast_childidx(entity, 5); if(ast_id(c_api) == TK_AT) { // We have an actor marked as C-API. if(!reachable_methods(c, entity)) return false; found = true; } } entity = ast_sibling(entity); } module = ast_sibling(module); } package = ast_sibling(package); } if(!found) { errorf(NULL, "no C-API actors found in package '%s'", c->filename); return false; } paint(c->reachable); return true; }
/* A dup2() with error checking */ int ksh_dup2(int ofd, int nfd, bool errok) { int rv; if (((rv = dup2(ofd, nfd)) < 0) && !errok && (errno != EBADF)) errorf(Ttoo_many_files); #ifdef __ultrix /*XXX imake style */ if (rv >= 0) fcntl(nfd, F_SETFD, 0); #endif return (rv); }