static void RenderHTMLContent(Buffer *out, const char *input, size_t len) { for (size_t i = 0; i < len; i++) { switch (input[i]) { case '&': BufferAppendString(out, "&"); break; case '"': BufferAppendString(out, """); break; case '<': BufferAppendString(out, "<"); break; case '>': BufferAppendString(out, ">"); break; default: BufferAppendChar(out, input[i]); break; } } }
static bool RenderVariablePrimitive(Buffer *out, const JsonElement *primitive, const bool escaped, const bool key_mode) { if (key_mode && JsonElementGetPropertyName(primitive)) { if (escaped) { RenderHTMLContent(out, JsonElementGetPropertyName(primitive), strlen(JsonElementGetPropertyName(primitive))); } else { BufferAppendString(out, JsonElementGetPropertyName(primitive)); } return true; } switch (JsonGetPrimitiveType(primitive)) { case JSON_PRIMITIVE_TYPE_STRING: if (escaped) { RenderHTMLContent(out, JsonPrimitiveGetAsString(primitive), strlen(JsonPrimitiveGetAsString(primitive))); } else { BufferAppendString(out, JsonPrimitiveGetAsString(primitive)); } return true; case JSON_PRIMITIVE_TYPE_INTEGER: { char *str = StringFromLong(JsonPrimitiveGetAsInteger(primitive)); BufferAppendString(out, str); free(str); } return true; case JSON_PRIMITIVE_TYPE_REAL: { char *str = StringFromDouble(JsonPrimitiveGetAsReal(primitive)); BufferAppendString(out, str); free(str); } return true; case JSON_PRIMITIVE_TYPE_BOOL: BufferAppendString(out, JsonPrimitiveGetAsBool(primitive) ? "true" : "false"); return true; case JSON_PRIMITIVE_TYPE_NULL: return true; default: assert(!"Unrecognised JSON primitive type"); } return false; }
static bool RenderVariableContainer(Buffer *out, const JsonElement *container, bool compact) { Writer *w = StringWriter(); if (compact) { JsonWriteCompact(w, container); } else { JsonWrite(w, container, 0); } BufferAppendString(out, StringWriterData(w)); WriterClose(w); return true; }
/** * Expand a #string into Buffer #out, returning the pointer to the string * itself, inside the Buffer #out. If #out is NULL then the buffer will be * created and destroyed internally. * * @retval NULL something went wrong */ char *ExpandScalar(const EvalContext *ctx, const char *ns, const char *scope, const char *string, Buffer *out) { bool out_belongs_to_us = false; if (out == NULL) { out = BufferNew(); out_belongs_to_us = true; } assert(string != NULL); assert(out != NULL); Buffer *current_item = BufferNew(); for (const char *sp = string; *sp != '\0'; sp++) { BufferClear(current_item); ExtractScalarPrefix(current_item, sp, strlen(sp)); BufferAppend(out, BufferData(current_item), BufferSize(current_item)); sp += BufferSize(current_item); if (*sp == '\0') { break; } BufferClear(current_item); char varstring = sp[1]; ExtractScalarReference(current_item, sp, strlen(sp), true); sp += BufferSize(current_item) + 2; if (IsCf3VarString(BufferData(current_item))) { Buffer *temp = BufferCopy(current_item); BufferClear(current_item); ExpandScalar(ctx, ns, scope, BufferData(temp), current_item); BufferDestroy(temp); } if (!IsExpandable(BufferData(current_item))) { VarRef *ref = VarRefParseFromNamespaceAndScope( BufferData(current_item), ns, scope, CF_NS, '.'); DataType value_type; const void *value = EvalContextVariableGet(ctx, ref, &value_type); VarRefDestroy(ref); switch (DataTypeToRvalType(value_type)) { case RVAL_TYPE_SCALAR: assert(value != NULL); BufferAppendString(out, value); continue; break; case RVAL_TYPE_CONTAINER: { assert(value != NULL); const JsonElement *jvalue = value; /* instead of casts */ if (JsonGetElementType(jvalue) == JSON_ELEMENT_TYPE_PRIMITIVE) { BufferAppendString(out, JsonPrimitiveGetAsString(jvalue)); continue; } break; } default: /* TODO Log() */ break; } } if (varstring == '{') { BufferAppendF(out, "${%s}", BufferData(current_item)); } else { BufferAppendF(out, "$(%s)", BufferData(current_item)); } } BufferDestroy(current_item); LogDebug(LOG_MOD_EXPAND, "ExpandScalar( %s : %s . %s ) => %s", SAFENULL(ns), SAFENULL(scope), string, BufferData(out)); return out_belongs_to_us ? BufferClose(out) : BufferGet(out); }
PromiseResult VerifyClassPromise(EvalContext *ctx, const Promise *pp, ARG_UNUSED void *param) { assert(param == NULL); Attributes a = GetClassContextAttributes(ctx, pp); if (!StringMatchFull("[a-zA-Z0-9_]+", pp->promiser)) { Log(LOG_LEVEL_VERBOSE, "Class identifier '%s' contains illegal characters - canonifying", pp->promiser); xsnprintf(pp->promiser, strlen(pp->promiser) + 1, "%s", CanonifyName(pp->promiser)); } if (a.context.nconstraints == 0) { cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "No constraints for class promise '%s'", pp->promiser); return PROMISE_RESULT_FAIL; } if (a.context.nconstraints > 1) { cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Irreconcilable constraints in classes for '%s'", pp->promiser); return PROMISE_RESULT_FAIL; } if (EvalClassExpression(ctx, a.context.expression, pp)) { if (!ValidClassName(pp->promiser)) { cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Attempted to name a class '%s', which is an illegal class identifier", pp->promiser); return PROMISE_RESULT_FAIL; } else { char *tags = NULL; { Buffer *tag_buffer = BufferNew(); BufferAppendString(tag_buffer, "classes promise,attribute_name=label,source=promise"); for (const Rlist *rp = PromiseGetConstraintAsList(ctx, "meta", pp); rp; rp = rp->next) { BufferAppendChar(tag_buffer, ','); BufferAppendString(tag_buffer, RlistScalarValue(rp)); } tags = BufferClose(tag_buffer); } if (/* Persistent classes are always global: */ a.context.persistent > 0 || /* Namespace-scope is global: */ a.context.scope == CONTEXT_SCOPE_NAMESPACE || /* If there is no explicit scope, common bundles define global * classes, other bundles define local classes: */ (a.context.scope == CONTEXT_SCOPE_NONE && 0 == strcmp(PromiseGetBundle(pp)->type, "common"))) { Log(LOG_LEVEL_VERBOSE, "C: + Global class: %s ", pp->promiser); EvalContextClassPutSoft(ctx, pp->promiser, CONTEXT_SCOPE_NAMESPACE, tags); } else { Log(LOG_LEVEL_VERBOSE, "C: + Private class: %s ", pp->promiser); EvalContextClassPutSoft(ctx, pp->promiser, CONTEXT_SCOPE_BUNDLE, tags); } if (a.context.persistent > 0) { Log(LOG_LEVEL_VERBOSE, "C: + Persistent class: '%s'. (%d minutes)", pp->promiser, a.context.persistent); EvalContextHeapPersistentSave(ctx, pp->promiser, a.context.persistent, CONTEXT_STATE_POLICY_RESET, tags); } free(tags); return PROMISE_RESULT_NOOP; } } return PROMISE_RESULT_NOOP; }
Rlist *PipeReadData(const IOData *io, int pipe_timeout_secs, int pipe_termination_check_secs) { char buff[CF_BUFSIZE] = {0}; Buffer *data = BufferNew(); if (!data) { Log(LOG_LEVEL_VERBOSE, "Unable to allocate buffer for handling pipe responses."); return NULL; } int timeout_seconds_left = pipe_timeout_secs; while (!IsPendingTermination() && timeout_seconds_left > 0) { int fd = PipeIsReadWriteReady(io, pipe_termination_check_secs); if (fd < 0) { Log(LOG_LEVEL_DEBUG, "Error reading data from application pipe %d", fd); break; } else if (fd == io->read_fd) { ssize_t res = read(fd, buff, sizeof(buff) - 1); if (res == -1) { if (errno == EINTR) { continue; } else { Log(LOG_LEVEL_ERR, "Unable to read output from application pipe: %s", GetErrorStr()); BufferDestroy(data); return NULL; } } else if (res == 0) /* reached EOF */ { break; } Log(LOG_LEVEL_DEBUG, "Data read from application pipe: %zd [%s]", res, buff); BufferAppendString(data, buff); memset(buff, 0, sizeof(buff)); } else if (fd == 0) /* timeout */ { timeout_seconds_left -= pipe_termination_check_secs; continue; } } char *read_string = BufferClose(data); #ifdef __MINGW32__ bool detect_crlf = true; #else bool detect_crlf = false; #endif Rlist *response_lines = RlistFromStringSplitLines(read_string, detect_crlf); free(read_string); return response_lines; }
bool ExpandScalar(const EvalContext *ctx, const char *ns, const char *scope, const char *string, Buffer *out) { assert(string); if (strlen(string) == 0) { return true; } bool fully_expanded = true; Buffer *current_item = BufferNew(); for (const char *sp = string; *sp != '\0'; sp++) { BufferClear(current_item); ExtractScalarPrefix(current_item, sp, strlen(sp)); BufferAppend(out, BufferData(current_item), BufferSize(current_item)); sp += BufferSize(current_item); if (*sp == '\0') { break; } BufferClear(current_item); char varstring = sp[1]; ExtractScalarReference(current_item, sp, strlen(sp), true); sp += BufferSize(current_item) + 2; if (IsCf3VarString(BufferData(current_item))) { Buffer *temp = BufferCopy(current_item); BufferClear(current_item); ExpandScalar(ctx, ns, scope, BufferData(temp), current_item); BufferDestroy(temp); } if (!IsExpandable(BufferData(current_item))) { DataType type = CF_DATA_TYPE_NONE; const void *value = NULL; { VarRef *ref = VarRefParseFromNamespaceAndScope(BufferData(current_item), ns, scope, CF_NS, '.'); value = EvalContextVariableGet(ctx, ref, &type); VarRefDestroy(ref); } if (value) { switch (DataTypeToRvalType(type)) { case RVAL_TYPE_SCALAR: BufferAppendString(out, value); continue; case RVAL_TYPE_CONTAINER: if (JsonGetElementType((JsonElement*)value) == JSON_ELEMENT_TYPE_PRIMITIVE) { BufferAppendString(out, JsonPrimitiveGetAsString((JsonElement*)value)); continue; } break; default: break; } } } if (varstring == '{') { BufferAppendF(out, "${%s}", BufferData(current_item)); } else { BufferAppendF(out, "$(%s)", BufferData(current_item)); } } BufferDestroy(current_item); return fully_expanded; }