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}"); }
static bool VerifyBundleSequence(EvalContext *ctx, const Policy *policy, const GenericAgentConfig *config) { Rlist *rp; char *name; Rval retval; int ok = true; FnCall *fp; if (!EvalContextVariableControlCommonGet(ctx, COMMON_CONTROL_BUNDLESEQUENCE, &retval)) { Log(LOG_LEVEL_ERR, " No bundlesequence in the common control body"); return false; } if (retval.type != RVAL_TYPE_LIST) { FatalError(ctx, "Promised bundlesequence was not a list"); } for (rp = (Rlist *) retval.item; rp != NULL; rp = rp->next) { switch (rp->type) { case RVAL_TYPE_SCALAR: name = (char *) rp->item; break; case RVAL_TYPE_FNCALL: fp = (FnCall *) rp->item; name = (char *) fp->name; break; default: name = NULL; ok = false; { Writer *w = StringWriter(); WriterWrite(w, "Illegal item found in bundlesequence '"); RvalWrite(w, (Rval) {rp->item, rp->type}); WriterWrite(w, "'"); Log(LOG_LEVEL_ERR, "%s", StringWriterData(w)); WriterClose(w); } break; } if (strcmp(name, CF_NULL_VALUE) == 0) { continue; } if (!config->ignore_missing_bundles && !PolicyGetBundle(policy, NULL, NULL, name)) { Log(LOG_LEVEL_ERR, "Bundle '%s' listed in the bundlesequence is not a defined bundle", name); ok = false; } } return ok; }
void FnCallWrite(Writer *writer, const FnCall *call) { WriterWrite(writer, call->name); WriterWriteChar(writer, '('); for (const Rlist *rp = call->args; rp != NULL; rp = rp->next) { switch (rp->val.type) { case RVAL_TYPE_SCALAR: ScalarWrite(writer, RlistScalarValue(rp), true); break; case RVAL_TYPE_FNCALL: FnCallWrite(writer, RlistFnCallValue(rp)); break; default: WriterWrite(writer, "(** Unknown argument **)\n"); break; } if (rp->next != NULL) { WriterWriteChar(writer, ','); } } WriterWriteChar(writer, ')'); }
char *SearchAndReplace(const char *source, const char *search, const char *replace) { const char *source_ptr = source; if ((source == NULL) || (search == NULL) || (replace == NULL)) { ProgrammingError("Programming error: NULL argument is passed to SearchAndReplace"); } if (strcmp(search, "") == 0) { return xstrdup(source); } Writer *w = StringWriter(); for (;;) { const char *found_ptr = strstr(source_ptr, search); if (found_ptr == NULL) { WriterWrite(w, source_ptr); return StringWriterClose(w); } WriterWriteLen(w, source_ptr, found_ptr - source_ptr); WriterWrite(w, replace); source_ptr += found_ptr - source_ptr + strlen(search); } }
static void RenderHTMLContent(Writer *out, const char *input, size_t len) { for (size_t i = 0; i < len; i++) { switch (input[i]) { case '&': WriterWrite(out, "&"); break; case '"': WriterWrite(out, """); break; case '<': WriterWrite(out, "<"); break; case '>': WriterWrite(out, ">"); break; default: WriterWriteChar(out, input[i]); break; } } }
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 bool VerifyBundleSequence(EvalContext *ctx, const Policy *policy, const GenericAgentConfig *config) { Rlist *fallback = NULL; const Rlist *bundlesequence = EvalContextVariableControlCommonGet(ctx, COMMON_CONTROL_BUNDLESEQUENCE); if (!bundlesequence) { RlistAppendScalar(&fallback, "main"); bundlesequence = fallback; } const char *name; int ok = true; for (const Rlist *rp = bundlesequence; rp != NULL; rp = rp->next) { switch (rp->val.type) { case RVAL_TYPE_SCALAR: name = RlistScalarValue(rp); break; case RVAL_TYPE_FNCALL: name = RlistFnCallValue(rp)->name; break; default: name = NULL; ok = false; { Writer *w = StringWriter(); WriterWrite(w, "Illegal item found in bundlesequence '"); RvalWrite(w, rp->val); WriterWrite(w, "'"); Log(LOG_LEVEL_ERR, "%s", StringWriterData(w)); WriterClose(w); } continue; } if (strcmp(name, CF_NULL_VALUE) == 0) { continue; } if (!config->ignore_missing_bundles && !PolicyGetBundle(policy, NULL, NULL, name)) { Log(LOG_LEVEL_ERR, "Bundle '%s' listed in the bundlesequence is not a defined bundle", name); ok = false; } } RlistDestroy(fallback); return ok; }
void test_multiwrite_file_buffer(void **p) { global_w = StringWriter(); Writer *w = FileWriter(NULL); WriterWrite(w, "123"); WriterWrite(w, "456"); assert_int_equal(StringWriterLength(global_w), 6); assert_string_equal(StringWriterData(global_w), "123456"); WriterClose(w); WriterClose(global_w); assert_int_equal(global_w_closed, true); }
void RvalWrite(Writer *writer, Rval rval) { if (rval.item == NULL) { return; } switch (rval.type) { case RVAL_TYPE_SCALAR: ScalarWrite(writer, RvalScalarValue(rval)); break; case RVAL_TYPE_LIST: RlistWrite(writer, RvalRlistValue(rval)); break; case RVAL_TYPE_FNCALL: FnCallPrint(writer, RvalFnCallValue(rval)); break; case RVAL_TYPE_NOPROMISEE: WriterWrite(writer, "(no-one)"); break; default: ProgrammingError("Unknown rval type %c", rval.type); } }
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 WriteCopyright(Writer *out) { static const char *copyright = ".\\\"Copyright (C) CFEngine AS\n" ".\\\"\n" ".\\\"This file is part of CFEngine 3 - written and maintained by CFEngine AS.\n" ".\\\"\n" ".\\\"This program is free software; you can redistribute it and/or modify it\n" ".\\\"under the terms of the GNU General Public License as published by the\n" ".\\\"Free Software Foundation; version 3.\n" ".\\\"\n" ".\\\"This program is distributed in the hope that it will be useful,\n" ".\\\"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" ".\\\"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" ".\\\"GNU General Public License for more details.\n" ".\\\"\n" ".\\\"You should have received a copy of the GNU General Public License\n" ".\\\"along with this program; if not, write to the Free Software\n" ".\\\"Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA\n" ".\\\"\n" ".\\\"To the extent this program is licensed as part of the Enterprise\n" ".\\\"versions of CFEngine, the applicable Commerical Open Source License\n" ".\\\"(COSL) may apply to this file if you as a licensee so wish it. See\n" ".\\\"included file COSL.txt.\n"; WriterWrite(out, copyright); }
void RvalWrite(Writer *writer, Rval rval) { if (rval.item == NULL) { return; } switch (rval.type) { case RVAL_TYPE_SCALAR: ScalarWrite(writer, RvalScalarValue(rval)); break; case RVAL_TYPE_LIST: RlistWrite(writer, RvalRlistValue(rval)); break; case RVAL_TYPE_FNCALL: FnCallWrite(writer, RvalFnCallValue(rval)); break; case RVAL_TYPE_NOPROMISEE: WriterWrite(writer, "(no-one)"); break; case RVAL_TYPE_CONTAINER: JsonWrite(writer, RvalContainerValue(rval), 0); break; } }
void RvalWriteParts(Writer *writer, const void* item, RvalType type) { if (item == NULL) { return; } switch (type) { case RVAL_TYPE_SCALAR: ScalarWrite(writer, item); break; case RVAL_TYPE_LIST: RlistWrite(writer, item); break; case RVAL_TYPE_FNCALL: FnCallWrite(writer, item); break; case RVAL_TYPE_NOPROMISEE: WriterWrite(writer, "(no-one)"); break; case RVAL_TYPE_CONTAINER: JsonWrite(writer, item, 0); break; } }
static void WriteAuthor(Writer *out) { static const char *author = ".SH AUTHOR\n" "Mark Burgess and CFEngine AS\n"; WriterWrite(out, author); }
void XmlManual(const char *mandir, FILE *fout) { Writer *writer = NULL; int i; SubTypeSyntax *st = NULL; MANUAL_DIRECTORY = (char *) mandir; AddSlash(MANUAL_DIRECTORY); writer = FileWriter(fout); /* XML HEADER */ XmlComment(writer, "AUTOGENERATED SYNTAX DOCUMENTATION BY CF-KNOW"); WriterWrite(writer, "\n"); /* START XML ELEMENT -- SYNTAX-DOCUMENTATION */ XmlStartTag(writer, XMLTAG_DOC_ROOT, 0); /* SPECIAL VARIABLES */ XmlStartTag(writer, XMLTAG_VARSCOPES_ROOT, 0); XmlExportVariables(writer, "const"); XmlExportVariables(writer, "edit"); XmlExportVariables(writer, "match"); XmlExportVariables(writer, "mon"); XmlExportVariables(writer, "sys"); XmlExportVariables(writer, "this"); XmlEndTag(writer, XMLTAG_VARSCOPES_ROOT); /* SPECIAL FUNCTIONS */ XmlStartTag(writer, XMLTAG_FUNCTIONS_ROOT, 0); for (i = 0; CF_FNCALL_TYPES[i].name != NULL; i++) { XmlExportFunction(writer, CF_FNCALL_TYPES[i]); } XmlEndTag(writer, XMLTAG_FUNCTIONS_ROOT); /* CONTROL */ XmlStartTag(writer, XMLTAG_CONTROLS_ROOT, 0); for (i = 0; CF_ALL_BODIES[i].btype != NULL; i++) { XmlExportControl(writer, CF_ALL_BODIES[i]); } XmlEndTag(writer, XMLTAG_CONTROLS_ROOT); /* PROMISE TYPES */ XmlStartTag(writer, XMLTAG_PROMISETYPES_ROOT, 0); for (i = 0; i < CF3_MODULES; i++) { st = CF_ALL_SUBTYPES[i]; XmlExportPromiseType(writer, st); } XmlEndTag(writer, XMLTAG_PROMISETYPES_ROOT); /* END XML ELEMENT -- SYNTAX-DOCUMENTATION */ XmlEndTag(writer, XMLTAG_DOC_ROOT); WriterClose(writer); }
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); }
static void WriteBugs(Writer *out) { static const char *bugs = ".SH BUGS\n" "Please see the public bug-tracker at http://bug.cfengine.com/.\n" ".br\n" "GitHub pull-requests may be submitted to http://github.com/cfengine/core/.\n"; WriterWrite(out, bugs); }
static bool RenderVariablePrimitive(Writer *out, const JsonElement *primitive, const bool escaped) { switch (JsonGetPrimitiveType(primitive)) { case JSON_PRIMITIVE_TYPE_STRING: if (escaped) { RenderHTMLContent(out, JsonPrimitiveGetAsString(primitive), strlen(JsonPrimitiveGetAsString(primitive))); } else { WriterWrite(out, JsonPrimitiveGetAsString(primitive)); } return true; case JSON_PRIMITIVE_TYPE_INTEGER: { char *str = StringFromLong(JsonPrimitiveGetAsInteger(primitive)); WriterWrite(out, str); free(str); } return true; case JSON_PRIMITIVE_TYPE_REAL: { char *str = StringFromDouble(JsonPrimitiveGetAsReal(primitive)); WriterWrite(out, str); free(str); } return true; case JSON_PRIMITIVE_TYPE_BOOL: WriterWrite(out, JsonPrimitiveGetAsBool(primitive) ? "true" : "false"); return true; case JSON_PRIMITIVE_TYPE_NULL: return true; default: assert(!"Unrecognised JSON primitive type"); } return false; }
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) { for (int i = 0; i < nmemb; ++i) { char *b = xstrndup(ptr + i * size, size); WriterWrite(global_w, b); free(b); } return nmemb * size; }
static void WriteCFEngine(Writer *out) { static const char *cfengine = ".SH CFENGINE\n" "CFEngine provides automated configuration management of large-scale computer systems. A system administrator " "describes the desired state of a system using CFEngine policy code. The program \\fBcf-agent\\fR reads policy code " "and attempts to bring the current system state to the desired state described. Policy code is downloaded by " "\\fBcf-agent\\fR from a \\fBcf-serverd\\fR daemon. The daemon \\fBcf-execd\\fR is responsible for running " "\\fBcf-agent\\fR periodically.\n" ".br\n" "Documentation for CFEngine is available at http://cfengine.com/documentation/.\n"; WriterWrite(out, cfengine); }
static void WriteSeeAlso(Writer *out) { static const char *see_also = ".SH \"SEE ALSO\"\n" ".BR cf-promises (8),\n" ".BR cf-agent (8),\n" ".BR cf-serverd (8),\n" ".BR cf-execd (8),\n" ".BR cf-monitord (8),\n" ".BR cf-runagent (8),\n" ".BR cf-key (8)\n"; WriterWrite(out, see_also); }
void test_write_empty_file_buffer(void **p) { global_w = StringWriter(); Writer *w = FileWriter(NULL); WriterWrite(w, ""); assert_int_equal(StringWriterLength(global_w), 0); assert_string_equal(StringWriterData(global_w), ""); WriterClose(w); WriterClose(global_w); assert_int_equal(global_w_closed, true); }
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 char *GetIpAddresses(const EvalContext *ctx) { Writer *ipbuf = StringWriter(); for (Item *iptr = EvalContextGetIpAddresses(ctx); iptr != NULL; iptr = iptr->next) { WriterWrite(ipbuf, iptr->name); if (iptr->next != NULL) { WriterWriteChar(ipbuf, ' '); } } return StringWriterClose(ipbuf); }
void RlistWrite(Writer *writer, const Rlist *list) { WriterWrite(writer, " {"); for (const Rlist *rp = list; rp != NULL; rp = rp->next) { RvalWriteQuoted(writer, rp->val); if (rp->next != NULL) { WriterWriteChar(writer, ','); } } WriterWriteChar(writer, '}'); }
char *Rlist2String(Rlist *list, char *sep) { Writer *writer = StringWriter(); for (const Rlist *rp = list; rp != NULL; rp = rp->next) { RvalWrite(writer, rp->val); if (rp->next != NULL) { WriterWrite(writer, sep); } } return StringWriterClose(writer); }
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]); } } }
static void WritePromiseTheory(Writer *out) { static const char *promise_theory = ".SH PROMISE THEORY\n" "CFEngine is built on principles from promise theory, proposed by Mark Burgess in 2004. " "Promise theory is a model of voluntary cooperation between individual, autonomous " "actors or agents who publish their intentions to one another in the form of promises. " "A promise is a declaration of intent whose purpose is to increase the recipient's certainty " "about a claim of past, present or future behaviour. For a promise to increase certainty, " "the recipient needs to trust the promiser, but trust can also be built on the verification " "that previous promises have been kept, thus trust plays a symbiotic relationship with promises. \n" ".br\n" "For an introduction to promise theory, please see http://arxiv.org/abs/0810.3294/\n"; WriterWrite(out, promise_theory); }
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, ')'); }
void RlistWrite(Writer *writer, const Rlist *list) { WriterWrite(writer, " {"); for (const Rlist *rp = list; rp != NULL; rp = rp->next) { WriterWriteChar(writer, '\''); RvalWrite(writer, (Rval) {rp->item, rp->type}); WriterWriteChar(writer, '\''); if (rp->next != NULL) { WriterWriteChar(writer, ','); } } WriterWriteChar(writer, '}'); }