Skip to content

Commit

Permalink
Merge pull request #399 from zowe/v2.x/staging
Browse files Browse the repository at this point in the history
merge config manager error msg on rc
  • Loading branch information
1000TurquoisePogs authored Aug 31, 2023
2 parents 12a2d8e + bc4d49c commit c5756a2
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 30 deletions.
41 changes: 35 additions & 6 deletions c/configmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,12 @@ static Json *readYamlIntoJson(ConfigManager *mgr, char *filename, bool allowMiss
trace(mgr,DEBUG,"before read YAML mgr=0x%p file=%s\n",mgr,filename);
bool wasMissing = false;
yaml_document_t *doc = readYAML2(filename,errorBuffer,YAML_ERROR_MAX,&wasMissing);
/*
* errorBuffer is ebcdic.
*
* parser explanation was passed into 'convertToNative()' to go from
* ascii to ebcdic.
*/
trace(mgr,DEBUG,"yaml doc at 0x%p, allowMissing=%d wasMissing=%d\n",doc,allowMissingFile,wasMissing);
if (doc){
if (mgr->traceLevel >= 1){
Expand All @@ -682,15 +688,32 @@ static Json *readYamlIntoJson(ConfigManager *mgr, char *filename, bool allowMiss
} else if (allowMissingFile && wasMissing){
return NULL;
} else{
trace(mgr,INFO,"WARNING, yaml read failed, errorBuffer='%s'\n",errorBuffer);
#ifdef __ZOWE_OS_ZOS
a2e(errorBuffer,YAML_ERROR_MAX);
#ifndef __ZOWE_OS_ZOS
/*
* Let's convert to ascii if we aren't on z/OS?
*/
e2a(errorBuffer,sizeof(errorBuffer));
#endif
trace(mgr,INFO,"WARNING, yaml read failed, errorBuffer='%s'\n",errorBuffer);
trace(mgr,INFO,"%s - %s\n","ZWEL0318E",errorBuffer);
return NULL;
}
}

static char *getAbsolutePathOrKeepRelativePath(const char *filename, ConfigManager *mgr) {

char *fileOrPath = SLHAlloc(mgr->slh, USS_MAX_PATH_LENGTH+1);

if (fileOrPath) {
memset(fileOrPath, 0, USS_MAX_PATH_LENGTH+1);
if (!realpath(filename, fileOrPath)) {
trace(mgr, DEBUG, "Couldn't get absolute path for '%s'. errno=%d\n", filename, errno);
strcpy(fileOrPath, filename);
}
}

return fileOrPath;
}

static Json *readJson(ConfigManager *mgr, CFGConfig *config, ConfigPathElement *pathElement, bool *nullAllowedPtr){
*nullAllowedPtr = false;
if (pathElement == NULL){
Expand All @@ -702,7 +725,13 @@ static Json *readJson(ConfigManager *mgr, CFGConfig *config, ConfigPathElement *
return NULL;
}
if (pathElement->flags & CONFIG_PATH_OMVS_FILE){
return readYamlIntoJson(mgr,pathElement->data,false);
/*
* This probably won't ever return null because we'd have to fail an allocation of 1024 bytes...
* Just in case, we will default to pathElement->data if 'absolutePath' comes back as null because
* it should be the same thing anyways.
*/
char *absolutePath = getAbsolutePathOrKeepRelativePath(pathElement->data,mgr);
return readYamlIntoJson(mgr,absolutePath ? absolutePath : pathElement->data,false);
} else if (pathElement->flags & CONFIG_PATH_MVS_PARMLIB){
char pdsMemberSpec[MAX_PDS_NAME];
trace(mgr,DEBUG,"pathElement=0x%p config=0x%p\n",pathElement,config);
Expand Down Expand Up @@ -1729,7 +1758,7 @@ static int simpleMain(int argc, char **argv){
}
int loadStatus = cfgLoadConfiguration(mgr,configName);
if (loadStatus){
trace(mgr,INFO,"Failed to load configuration, element may be bad, or less likey a bad merge\n");
trace(mgr,INFO,"%s - Failed to load configuration, element may be bad, or less likely a bad merge.\n", "ZWEL0319E");
return loadStatus;
}
trace(mgr,DEBUG,"configuration parms are loaded\n");
Expand Down
1 change: 0 additions & 1 deletion c/embeddedjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1890,7 +1890,6 @@ Json *evaluateJsonTemplates(EmbeddedJS *ejs, ShortLivedHeap *slh, Json *json){
visitJSON(json,NULL,NULL,-1,evaluationVisitor,&evalContext);
return json;
} else {
printf("top json is not an object\n");
return NULL;
}
}
Expand Down
80 changes: 57 additions & 23 deletions c/yaml2json.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,51 +122,85 @@ static int yamlReadHandler(void *data, unsigned char *buffer, size_t size, size_
return rc;
}

static void decodeParserError(yaml_parser_t *parser, char *errorBuf, size_t errorBufSize) {
static void decodeParserError(yaml_parser_t *parser, char *errorBuf, size_t errorBufSize, const char *filename) {
/*
* Convert diagnostics (problem and context) from ascii to ebcdic so that the error buffer that is returned from this function
* is in native codepage.
*/
size_t problemLen = strlen(parser->problem);
char *problemNative = safeMalloc(problemLen + 1, "parser problem");
if (problemNative) {
memset(problemNative, 0, problemLen + 1);
strcpy(problemNative, parser->problem);
convertToNative(problemNative, problemLen);
}
size_t contextLen = 0;
char *contextNative = NULL;
if (parser->context) {
contextLen = strlen(parser->context);
contextNative = safeMalloc(contextLen + 1, "parser context");
if (contextNative) {
memset(contextNative, 0, contextLen + 1);
strcpy(contextNative, parser->context);
convertToNative(contextNative, contextLen);
}
}
switch (parser->error) {
case YAML_MEMORY_ERROR:
snprintf(errorBuf, errorBufSize, "YAML memory error: not enough memory for parsing");
snprintf(errorBuf, errorBufSize, "Couldn't allocate enough memory to process file '%s'.", filename);
break;
case YAML_READER_ERROR: {
size_t problemLen = strlen(parser->problem);
char problemNative[problemLen + 1];
snprintf (problemNative, problemLen + 1, "%s", parser->problem);
convertToNative(problemNative, problemLen);
if (parser->problem_value != -1) {
snprintf(errorBuf, errorBufSize, "YAML reader error: %s: #%X at %ld", problemNative, parser->problem_value, (long)parser->problem_offset);
snprintf(errorBuf,
errorBufSize,
"Couldn't read file '%s': %s, #%x at %zu.",
filename, problemNative, parser->problem_value, parser->problem_offset);
} else {
snprintf(errorBuf, errorBufSize, "YAML reader error: %s at %ld", problemNative, (long)parser->problem_offset);
snprintf(errorBuf, errorBufSize, "Couldn't read file %s: %s at %zu.", filename, problemNative, parser->problem_offset);
}
break;
}
case YAML_SCANNER_ERROR:
if (parser->context) {
snprintf(errorBuf, errorBufSize, "YAML scanner error: %s at line %d, column %d"
"%s at line %d, column %d\n", parser->context,
snprintf(errorBuf, errorBufSize,
"Couldn't scan file '%s': %s at line %d, column %d, "
"%s at line %d, column %d.",
filename, contextNative,
(int)parser->context_mark.line+1, (int)parser->context_mark.column+1,
parser->problem, (int)parser->problem_mark.line+1,
problemNative, (int)parser->problem_mark.line+1,
(int)parser->problem_mark.column+1);
} else {
snprintf(errorBuf, errorBufSize, "YAML scanner error: %s at line %d, column %d",
parser->problem, (int)parser->problem_mark.line+1,
(int)parser->problem_mark.column+1);
snprintf(errorBuf, errorBufSize,
"Couldn't scan file '%s': %s at line %d, column %d.",
filename, problemNative, (int)parser->problem_mark.line+1,
(int)parser->problem_mark.column+1);
}
break;
case YAML_PARSER_ERROR:
if (parser->context) {
snprintf(errorBuf, errorBufSize, "YAML parser error: %s at line %d, column %d\n"
"%s at line %d, column %d", parser->context,
(int)parser->context_mark.line+1, (int)parser->context_mark.column+1,
parser->problem, (int)parser->problem_mark.line+1,
(int)parser->problem_mark.column+1);
snprintf(errorBuf, errorBufSize,
"Couldn't parse file '%s': %s at line %d, column %d, "
"%s at line %d, column %d.",
filename, contextNative,
(int)parser->context_mark.line+1, (int)parser->context_mark.column+1,
problemNative, (int)parser->problem_mark.line+1,
(int)parser->problem_mark.column+1);
} else {
snprintf(errorBuf, errorBufSize, "YAML parser error: %s at line %d, column %d",
parser->problem, (int)parser->problem_mark.line+1,
snprintf(errorBuf, errorBufSize,
"Couldn't parse file '%s': %s at line %d, column %d.",
filename, problemNative, (int)parser->problem_mark.line+1,
(int)parser->problem_mark.column+1);
}
break;
default:
snprintf(errorBuf, errorBufSize, "YAML parser: unknown error");
snprintf(errorBuf, errorBufSize, "Couldn't process file '%s' because of an unknown error type '%d'.", filename, parser->error);
}
/*
* We've already copied the information into the errorBuffer so we can free these now.
*/
safeFree(problemNative, problemLen);
if (contextNative) {
safeFree(contextNative, contextLen);
}
}

Expand All @@ -193,7 +227,7 @@ yaml_document_t *readYAML2(const char *filename, char *errorBuf, size_t errorBuf
}
yaml_parser_set_input(&parser, yamlReadHandler, file);
if (!yaml_parser_load(&parser, document)) {
decodeParserError(&parser, errorBuf, errorBufSize);
decodeParserError(&parser, errorBuf, errorBufSize, filename);
break;
}
if (!yaml_document_get_root_node(document)){
Expand Down

0 comments on commit c5756a2

Please sign in to comment.