char *EvalContextStackPath(const EvalContext *ctx) { Writer *path = StringWriter(); for (size_t i = 0; i < SeqLength(ctx->stack); i++) { StackFrame *frame = SeqAt(ctx->stack, i); switch (frame->type) { case STACK_FRAME_TYPE_BODY: WriterWriteF(path, "/%s", frame->data.body.owner->name); break; case STACK_FRAME_TYPE_BUNDLE: WriterWriteF(path, "/%s", frame->data.bundle.owner->name); break; case STACK_FRAME_TYPE_PROMISE_ITERATION: WriterWriteF(path, "/%s", frame->data.promise.owner->parent_promise_type->name); WriterWriteF(path, "/'%s'", frame->data.promise.owner->promiser); break; case STACK_FRAME_TYPE_PROMISE: break; } } return StringWriterClose(path); }
void BodyPrettyPrint(Writer *writer, Body *body) { Constraint *cp = NULL; char *current_class = NULL; WriterWriteF(writer, "body %s %s", body->type, body->name); ArgumentsPrettyPrint(writer, body->args); WriterWrite(writer, "\n{"); for (cp = body->conlist; cp != NULL; cp = cp->next) { if (current_class == NULL || strcmp(cp->classes, current_class) != 0) { current_class = cp->classes; if (strcmp(current_class, "any") == 0) { WriterWrite(writer, "\n"); } else { WriterWriteF(writer, "\n\n%s::", current_class); } } WriterWriteChar(writer, '\n'); IndentPrint(writer, 1); AttributePrettyPrint(writer, cp, 2); } WriterWrite(writer, "\n}"); }
void AgentDiagnosticsRun(const char *workdir, const AgentDiagnosticCheck checks[], Writer *output) { { char diagnostics_path[CF_BUFSIZE] = { 0 }; snprintf(diagnostics_path, CF_BUFSIZE, "%s/diagnostics", workdir); MapName(diagnostics_path); struct stat sb; if (stat(diagnostics_path, &sb) != 0) { if (mkdir(diagnostics_path, DEFAULTMODE) != 0) { WriterWriteF(output, "Cannot create diagnostics output directory '%s'", diagnostics_path); return; } } } for (int i = 0; checks[i].description; i++) { AgentDiagnosticsResult result = checks[i].check(workdir); WriterWriteF(output, "[ %s ] %s: %s\n", result.success ? "YES" : "NO ", checks[i].description, result.message); AgentDiagnosticsResultDestroy(result); } }
void CfHtmlHeader(Writer *writer, char *title, char *css, char *webdriver, char *header) { if (title == NULL) { title = "Cfengine Knowledge"; } WriterWriteF(writer, "<html>\n" " <head>\n" " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n" " <meta http-equiv=\"refresh\" CONTENT=\"150\">\n" " <title>%s</title>\n" " <link rel=\"stylesheet\" href=\"%s\" type=\"text/css\" media=\"screen\" />\n" " <link rel=\"stylesheet\" href=\"hand_%s\" type=\"text/css\" media=\"handheld\" />\n" " </head>\n" " <body>\n", title, css, css); if (header && (strlen(header) > 0)) { if (strlen(LICENSE_COMPANY) > 0) { WriterWriteF(writer, "<div id=\"company\">%s</div>\n%s\n", LICENSE_COMPANY, header); } else { WriterWriteF(writer, "%s\n", header); } } WriterWriteF(writer, "<div id=\"primary\">\n"); }
void CfHtmlFooter(Writer *writer, char *footer) { if (strlen(footer) > 0) { WriterWriteF(writer, "%s", footer); } WriterWriteF(writer, "</div></body></html>\n"); }
void BundlePrettyPrint(Writer *writer, Bundle *bundle) { SubType *promise_type = NULL; WriterWriteF(writer, "bundle %s %s", bundle->type, bundle->name); ArgumentsPrettyPrint(writer, bundle->args); WriterWrite(writer, "\n{"); for (promise_type = bundle->subtypes; promise_type != NULL; promise_type = promise_type->next) { Promise *pp = NULL; WriterWriteF(writer, "\n%s:\n", promise_type->name); for (pp = promise_type->promiselist; pp != NULL; pp = pp->next) { Constraint *cp = NULL; char *current_class = NULL; if (current_class == NULL || strcmp(cp->classes, current_class) != 0) { current_class = cp->classes; if (strcmp(current_class, "any") != 0) { IndentPrint(writer, 1); WriterWriteF(writer, "%s::", current_class); } } IndentPrint(writer, 2); WriterWrite(writer, pp->promiser); /* FIX: add support * if (pp->promisee != NULL) { fprintf(out, " -> %s", pp->promisee); } */ for (cp = pp->conlist; cp != NULL; cp = cp->next) { WriterWriteChar(writer, '\n'); IndentPrint(writer, 1); AttributePrettyPrint(writer, cp, 3); } } if (promise_type->next != NULL) { WriterWriteChar(writer, '\n'); } } WriterWrite(writer, "\n}"); }
static void WriteOptions(Writer *out, const struct option options[], const char *option_hints[]) { WriterWrite(out, ".SH OPTIONS\n"); for (int i = 0; options[i].name != NULL; i++) { if (options[i].has_arg) { WriterWriteF(out, ".IP \"--%s, -%c\" value\n%s\n", options[i].name, (char) options[i].val, option_hints[i]); } else { WriterWriteF(out, ".IP \"--%s, -%c\"\n%s\n", options[i].name, (char) options[i].val, option_hints[i]); } } }
char *MapNameCopy(const char *s) { Writer *w = StringWriter(); /* c:\a\b -> /cygdrive/c\a\b */ if (s[0] && isalpha(s[0]) && s[1] == ':') { WriterWriteF(w, "/cygdrive/%c", s[0]); s += 2; } for (; *s; s++) { /* a//b//c -> a/b/c */ /* a\\b\\c -> a\b\c */ if (IsFileSep(*s) && IsFileSep(*(s + 1))) { continue; } /* a\b\c -> a/b/c */ WriterWriteChar(w, *s == '\\' ? '/' : *s); } return StringWriterClose(w); }
static int GenerateAvahiConfig(const char *path) { FILE *fout; Writer *writer = NULL; fout = safe_fopen(path, "w+"); if (fout == NULL) { Log(LOG_LEVEL_ERR, "Unable to open '%s'", path); return -1; } writer = FileWriter(fout); fprintf(fout, "<?xml version=\"1.0\" standalone='no'?>\n"); fprintf(fout, "<!DOCTYPE service-group SYSTEM \"avahi-service.dtd\">\n"); XmlComment(writer, "This file has been automatically generated by cf-serverd."); XmlStartTag(writer, "service-group", 0); FprintAvahiCfengineTag(fout); XmlStartTag(writer, "service", 0); XmlTag(writer, "type", "_cfenginehub._tcp",0); DetermineCfenginePort(); XmlStartTag(writer, "port", 0); WriterWriteF(writer, "%d", CFENGINE_PORT); XmlEndTag(writer, "port"); XmlEndTag(writer, "service"); XmlEndTag(writer, "service-group"); fclose(fout); return 0; }
static void WriteName(Writer *out, const char *program, const char *short_description) { static const char *author = ".SH NAME\n" "%s \\- %s\n"; WriterWriteF(out, author, program, short_description); }
static void LogPromiseContext(const EvalContext *ctx, const Promise *pp) { Writer *w = StringWriter(); WriterWrite(w, "Additional promise info:"); if (PromiseGetHandle(pp)) { WriterWriteF(w, " handle '%s'", PromiseGetHandle(pp)); } { Rval retval; if (EvalContextVariableControlCommonGet(ctx, COMMON_CONTROL_VERSION, &retval)) { WriterWriteF(w, " version '%s'", RvalScalarValue(retval)); } } if (PromiseGetBundle(pp)->source_path) { WriterWriteF(w, " source path '%s' at line %zu", PromiseGetBundle(pp)->source_path, pp->offset.line); } switch (pp->promisee.type) { case RVAL_TYPE_SCALAR: WriterWriteF(w, " promisee '%s'", RvalScalarValue(pp->promisee)); break; case RVAL_TYPE_LIST: WriterWrite(w, " promisee "); RlistWrite(w, pp->promisee.item); break; default: break; } if (pp->comment) { WriterWriteF(w, " comment '%s'", pp->comment); } Log(LOG_LEVEL_VERBOSE, "%s", StringWriterData(w)); WriterClose(w); }
char *ReadTexinfoFileF(const char *source_dir, const char *fmt, ...) { Writer *filenamew = StringWriter(); struct stat sb; char *buffer = NULL; FILE *fp = NULL; off_t file_size; va_list ap; va_start(ap, fmt); WriterWriteF(filenamew, "%s/", source_dir); WriterWriteVF(filenamew, fmt, ap); va_end(ap); char *filename = StringWriterClose(filenamew); if (cfstat(filename, &sb) == -1) { if (!GenerateStub(filename)) { CfOut(OUTPUT_LEVEL_INFORM, "", "Unable to write down stub for missing texinfo file"); free(filename); return NULL; } } if ((fp = fopen(filename, "r")) == NULL) { CfOut(OUTPUT_LEVEL_INFORM, "fopen", "Could not read manual source %s\n", filename); free(filename); return NULL; } fseek(fp, 0, SEEK_END); file_size = ftello(fp); fseek(fp, 0, SEEK_SET); buffer = (char *) xcalloc(file_size + 1, sizeof(char)); buffer[file_size] = '\0'; int cnt = fread(buffer, sizeof(char), file_size, fp); if ((ferror(fp)) || (cnt != file_size)) { CfOut(OUTPUT_LEVEL_INFORM, "fread", "Could not read manual source %s\n", filename); free(buffer); fclose(fp); free(filename); return NULL; } fclose(fp); free(filename); return buffer; }
static void WriteAvailability(Writer *out, const char *program) { static const char *availability = ".SH AVAILABILITY\n" "%s is part of CFEngine.\n" ".br\n" "Binary packages may be downloaded from http://cfengine.com/downloads/.\n" ".br\n" "The source code is avaiable at http://github.com/cfengine/\n"; WriterWriteF(out, availability, program); }
void SaveClassEnvironment(EvalContext *ctx, Writer *writer) { { SetIterator it = EvalContextHeapIteratorHard(ctx); const char *context = NULL; while ((context = SetIteratorNext(&it))) { if (!EvalContextHeapContainsNegated(ctx, context)) { WriterWriteF(writer, "%s\n", context); } } } { SetIterator it = EvalContextHeapIteratorSoft(ctx); const char *context = NULL; while ((context = SetIteratorNext(&it))) { if (!EvalContextHeapContainsNegated(ctx, context)) { WriterWriteF(writer, "%s\n", context); } } } { SetIterator it = EvalContextStackFrameIteratorSoft(ctx); const char *context = NULL; while ((context = SetIteratorNext(&it))) { if (!EvalContextHeapContainsNegated(ctx, context)) { WriterWriteF(writer, "%s\n", context); } } } }
static void AddOpenPortsClasses(const char *name, const Item *value, Item **classlist) { Writer *w = StringWriter(); WriterWriteF(w, "@%s=", name); PrintItemList(value, w); if (StringWriterLength(w) <= 1500) { AppendItem(classlist, StringWriterClose(w), NULL); } else { WriterClose(w); } }
static char *GetLockNameExec(Attributes a, Promise *pp) { Writer *w = StringWriter(); if (a.args) { WriterWriteF(w, "%s %s", pp->promiser, a.args); } else { WriterWrite(w, pp->promiser); } return StringWriterClose(w); }
static void WriteHeader(Writer *out, const char *program, time_t last_modified) { char program_upper[256] = { 0 }; snprintf(program_upper, 255, "%s", program); ToUpperStrInplace(program_upper); char date_modified[20] = { 0 }; { struct tm t; gmtime_r(&last_modified, &t); strftime(date_modified, 19, "%Y-%m-%d", &t); } WriterWriteF(out, ".TH %s 8 \"%s\" \"CFEngine\" \"System Administration\"\n", program_upper, date_modified); }
static void WriteSynopsis(Writer *out, const char *program, bool accepts_file_argument) { static const char *synopsis = ".SH SYNOPSIS\n" ".B %s\n" ".RI [ OPTION ]...\n"; WriterWriteF(out, synopsis, program); if (accepts_file_argument) { WriterWrite(out, ".RI [ FILE ]\n"); } else { WriterWrite(out, "\n"); } }
static void ArgumentsPrettyPrint(Writer *writer, Rlist *args) { Rlist *argp = NULL; WriterWriteChar(writer, '('); for (argp = args; argp != NULL; argp = argp->next) { WriterWriteF(writer, "%s", (char *) argp->item); if (argp->next != NULL) { WriterWrite(writer, ", "); } } WriterWriteChar(writer, ')'); }
static void test_parse_object_escaped(void **state) { const char *escaped_string = "\\\"/var/cfenigne/bin/cf-know\\\" "; const char *key = "json_element_key"; Writer *writer = StringWriter(); WriterWriteF(writer, "{ \"%s\" : \"%s\" }", key, escaped_string); const char *json_string = StringWriterData(writer); JsonElement *obj = JsonParse(&json_string); assert_int_not_equal(obj, NULL); assert_string_equal(JsonObjectGetAsString(obj, key), escaped_string); WriterClose(writer); JsonElementDestroy(obj); }
void FnCallPrint(Writer *writer, FnCall *call) { for (Rlist *rp = call->args; rp != NULL; rp = rp->next) { switch (rp->type) { case CF_SCALAR: WriterWriteF(writer, "%s,", (const char *) rp->item); break; case CF_FNCALL: FnCallPrint(writer, (FnCall *) rp->item); break; default: WriterWrite(writer, "(** Unknown argument **)\n"); break; } } }
void GenericAgentWriteHelp(Writer *w, const char *component, const struct option options[], const char *hints[], bool accepts_file_argument) { WriterWriteF(w, "Usage: %s [OPTION]...%s\n", component, accepts_file_argument ? " [FILE]" : ""); WriterWriteF(w, "\nOptions:\n"); for (int i = 0; options[i].name != NULL; i++) { if (options[i].has_arg) { WriterWriteF(w, " --%-12s, -%c value - %s\n", options[i].name, (char) options[i].val, hints[i]); } else { WriterWriteF(w, " --%-12s, -%-7c - %s\n", options[i].name, (char) options[i].val, hints[i]); } } WriterWriteF(w, "\nWebsite: http://www.cfengine.com\n"); WriterWriteF(w, "This software is Copyright (C) 2008,2010-present CFEngine AS.\n"); }
static void AttributePrettyPrint(Writer *writer, Constraint *attribute, int indent_level) { WriterWriteF(writer, "%s => ", attribute->lval); RvalPrettyPrint(writer, attribute->rval); }
static void WriteDescription(Writer *out, const char *description) { WriterWriteF(out, ".SH DESCRIPTION\n%s\n", description); }
// TODO: should be replaced by something not complected with loading static void ShowContext(EvalContext *ctx) { Seq *hard_contexts = SeqNew(1000, NULL); Seq *soft_contexts = SeqNew(1000, NULL); { ClassTableIterator *iter = EvalContextClassTableIteratorNewGlobal(ctx, NULL, true, true); Class *cls = NULL; while ((cls = ClassTableIteratorNext(iter))) { if (cls->is_soft) { SeqAppend(soft_contexts, cls->name); } else { SeqAppend(hard_contexts, cls->name); } } ClassTableIteratorDestroy(iter); } SeqSort(soft_contexts, (SeqItemComparator)strcmp, NULL); SeqSort(hard_contexts, (SeqItemComparator)strcmp, NULL); { Writer *w = NULL; if (LEGACY_OUTPUT) { w = FileWriter(stdout); WriterWriteF(w, "%s> -> Hard classes = {", VPREFIX); } else { w = StringWriter(); WriterWrite(w, "Discovered hard classes:"); } for (size_t i = 0; i < SeqLength(hard_contexts); i++) { const char *context = SeqAt(hard_contexts, i); WriterWriteF(w, " %s", context); } if (LEGACY_OUTPUT) { WriterWrite(w, "}\n"); FileWriterDetach(w); } else { Log(LOG_LEVEL_VERBOSE, "%s", StringWriterData(w)); WriterClose(w); } } { Writer *w = NULL; if (LEGACY_OUTPUT) { w = FileWriter(stdout); WriterWriteF(w, "%s> -> Additional classes = {", VPREFIX); } else { w = StringWriter(); WriterWrite(w, "Additional classes:"); } for (size_t i = 0; i < SeqLength(soft_contexts); i++) { const char *context = SeqAt(soft_contexts, i); WriterWriteF(w, " %s", context); } if (LEGACY_OUTPUT) { WriterWrite(w, "}\n"); FileWriterDetach(w); } else { if (SeqLength(soft_contexts) > 0) { Log(LOG_LEVEL_VERBOSE, "%s", StringWriterData(w)); } WriterClose(w); } } SeqDestroy(hard_contexts); SeqDestroy(soft_contexts); }
static void AmendErrorMessageWithPromiseInformation(EvalContext *ctx, Item **error_message, const Promise *pp) { Rval retval; char *v; if (ScopeControlCommonGet(ctx, COMMON_CONTROL_VERSION, &retval) != DATA_TYPE_NONE) { v = (char *) retval.item; } else { v = "not specified"; } const char *sp; char handle[CF_MAXVARSIZE]; if ((sp = PromiseGetHandle(pp)) || (sp = PromiseID(pp))) { strncpy(handle, sp, CF_MAXVARSIZE - 1); } else { strcpy(handle, "(unknown)"); } char output[CF_BUFSIZE]; if (INFORM || VERBOSE || DEBUG) { snprintf(output, CF_BUFSIZE - 1, "I: Report relates to a promise with handle \"%s\"", handle); AppendItem(error_message, output, NULL); } if (pp && PromiseGetBundle(pp)->source_path) { snprintf(output, CF_BUFSIZE - 1, "I: Made in version \'%s\' of \'%s\' near line %zu", v, PromiseGetBundle(pp)->source_path, pp->offset.line); } else { snprintf(output, CF_BUFSIZE - 1, "I: Promise is made internally by cfengine"); } AppendItem(error_message, output, NULL); if (pp != NULL) { switch (pp->promisee.type) { case RVAL_TYPE_SCALAR: snprintf(output, CF_BUFSIZE - 1, "I: The promise was made to: \'%s\'", (char *) pp->promisee.item); AppendItem(error_message, output, NULL); break; case RVAL_TYPE_LIST: { Writer *w = StringWriter(); WriterWriteF(w, "I: The promise was made to (stakeholders): "); RlistWrite(w, pp->promisee.item); AppendItem(error_message, StringWriterClose(w), NULL); break; } default: break; } if (pp->ref) { snprintf(output, CF_BUFSIZE - 1, "I: Comment: %s\n", pp->ref); AppendItem(error_message, output, NULL); } } }
static void ShowContext(EvalContext *ctx) { { Writer *w = NULL; if (LEGACY_OUTPUT) { w = FileWriter(stdout); WriterWriteF(w, "%s> -> Hard classes = {", VPREFIX); } else { w = StringWriter(); WriterWrite(w, "Discovered hard classes:"); } Seq *hard_contexts = SeqNew(1000, NULL); SetIterator it = EvalContextHeapIteratorHard(ctx); char *context = NULL; while ((context = SetIteratorNext(&it))) { if (!EvalContextHeapContainsNegated(ctx, context)) { SeqAppend(hard_contexts, context); } } SeqSort(hard_contexts, (SeqItemComparator)strcmp, NULL); for (size_t i = 0; i < SeqLength(hard_contexts); i++) { const char *context = SeqAt(hard_contexts, i); WriterWriteF(w, " %s", context); } if (LEGACY_OUTPUT) { WriterWrite(w, "}\n"); FileWriterDetach(w); } else { Log(LOG_LEVEL_VERBOSE, "%s", StringWriterData(w)); WriterClose(w); } SeqDestroy(hard_contexts); } { Writer *w = NULL; if (LEGACY_OUTPUT) { w = FileWriter(stdout); WriterWriteF(w, "%s> -> Additional classes = {", VPREFIX); } else { w = StringWriter(); WriterWrite(w, "Additional classes:"); } Seq *soft_contexts = SeqNew(1000, NULL); SetIterator it = EvalContextHeapIteratorSoft(ctx); char *context = NULL; while ((context = SetIteratorNext(&it))) { if (!EvalContextHeapContainsNegated(ctx, context)) { SeqAppend(soft_contexts, context); } } SeqSort(soft_contexts, (SeqItemComparator)strcmp, NULL); for (size_t i = 0; i < SeqLength(soft_contexts); i++) { const char *context = SeqAt(soft_contexts, i); WriterWriteF(w, " %s", context); } if (LEGACY_OUTPUT) { WriterWrite(w, "}\n"); FileWriterDetach(w); } else { if (SeqLength(soft_contexts) > 0) { Log(LOG_LEVEL_VERBOSE, "%s", StringWriterData(w)); } WriterClose(w); } SeqDestroy(soft_contexts); } { bool have_negated_classes = false; Writer *w = NULL; if (LEGACY_OUTPUT) { w = FileWriter(stdout); WriterWriteF(w, "%s> -> Negated classes = {", VPREFIX); } else { w = StringWriter(); WriterWrite(w, "Negated classes:"); } StringSetIterator it = EvalContextHeapIteratorNegated(ctx); const char *context = NULL; while ((context = StringSetIteratorNext(&it))) { WriterWriteF(w, " %s", context); have_negated_classes = true; } if (LEGACY_OUTPUT) { WriterWrite(w, "}\n"); FileWriterDetach(w); } else { if (have_negated_classes) { Log(LOG_LEVEL_VERBOSE, "%s", StringWriterData(w)); } WriterClose(w); } } }
ENTERPRISE_VOID_FUNC_1ARG_DEFINE_STUB(void, GenericAgentWriteVersion, Writer *, w) { WriterWriteF(w, "%s\n", NameVersion()); }