-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3206 from grondo/job-output-format
shell: render output file template {{id}} as F58 by default, allow other id encodings
- Loading branch information
Showing
14 changed files
with
491 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -515,3 +515,6 @@ builtin | |
OMP | ||
envfile | ||
regex | ||
encodings | ||
dec | ||
subkey |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/************************************************************\ | ||
* Copyright 2019 Lawrence Livermore National Security, LLC | ||
* (c.f. AUTHORS, NOTICE.LLNS, COPYING) | ||
* | ||
* This file is part of the Flux resource manager framework. | ||
* For details, see https://github.com/flux-framework. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0 | ||
\************************************************************/ | ||
|
||
#if HAVE_CONFIG_H | ||
#include "config.h" | ||
#endif | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include "mustache.h" | ||
#include "src/common/libutil/llog.h" | ||
|
||
struct mustache_renderer { | ||
mustache_tag_f tag_f; | ||
void *tag_arg; | ||
|
||
mustache_log_f llog; | ||
void *llog_data; | ||
}; | ||
|
||
void mustache_renderer_destroy (struct mustache_renderer *mr) | ||
{ | ||
free (mr); | ||
} | ||
|
||
|
||
struct mustache_renderer * mustache_renderer_create (mustache_tag_f tagfn, | ||
void *arg) | ||
{ | ||
struct mustache_renderer *mr = NULL; | ||
|
||
if (!tagfn) { | ||
errno = EINVAL; | ||
return NULL; | ||
} | ||
if (!(mr = calloc (1, sizeof (*mr)))) | ||
return NULL; | ||
mr->tag_f = tagfn; | ||
mr->tag_arg = arg; | ||
return (mr); | ||
} | ||
|
||
void mustache_renderer_set_log (struct mustache_renderer *mr, | ||
mustache_log_f log_f, | ||
void *log_arg) | ||
{ | ||
if (mr) { | ||
mr->llog = log_f; | ||
mr->llog_data = log_arg; | ||
} | ||
} | ||
|
||
char *mustache_render (struct mustache_renderer *mr, const char *template) | ||
{ | ||
char name [1024]; | ||
size_t size; | ||
char *result = NULL; | ||
const char *pos = template; | ||
FILE *fp = NULL; | ||
|
||
if (mr == NULL || template == NULL) { | ||
errno = EINVAL; | ||
return NULL; | ||
} | ||
if (!(fp = open_memstream (&result, &size))) { | ||
llog_error (mr, "open_memstream"); | ||
goto fail; | ||
} | ||
for (;;) { | ||
int len; | ||
char *end; | ||
|
||
/* Look for opening "{{" | ||
*/ | ||
char *start = strstr (pos, "{{"); | ||
if (start == NULL) { | ||
/* No more mustache tags, put rest of string and finish | ||
*/ | ||
if (fputs (pos, fp) < 0) { | ||
llog_error (mr, "fputs(%s): %s", pos, strerror (errno)); | ||
goto fail; | ||
} | ||
break; | ||
} | ||
/* Write any part of template from current position to next tag | ||
*/ | ||
if (start > pos && fwrite (pos, start - pos, 1, fp) == 0) { | ||
llog_error (mr, "fwrite: %s", strerror (errno)); | ||
goto fail; | ||
} | ||
/* Advance past opening braces | ||
*/ | ||
start += 2; | ||
|
||
/* Find end of template tag | ||
*/ | ||
end = strstr (start, "}}"); | ||
if (end == NULL) { | ||
llog_error (mr, "mustache template error at pos=%d", | ||
(int)(start-template)); | ||
/* Copy rest of template and exit | ||
*/ | ||
pos = start - 2; | ||
if (fputs (pos, fp) < 0) { | ||
llog_error (mr, "fputs(%s): %s", pos, strerror (errno)); | ||
goto fail; | ||
} | ||
break; | ||
} | ||
/* Copy mustache tag into 'name' and substitute with callback | ||
*/ | ||
len = end - start; | ||
memcpy (name, start, len); | ||
name[len] = '\0'; | ||
if ((*mr->tag_f) (fp, name, mr->tag_arg) < 0) { | ||
/* If callback fails, just fail to expand the current mustache tag. | ||
*/ | ||
fprintf (fp, "{{%s}}", name); | ||
} | ||
|
||
/* Advance past closing braces '}}' and continue processing | ||
*/ | ||
pos = end + 2; | ||
} | ||
fclose (fp); | ||
return result; | ||
fail: | ||
if (fp) { | ||
fclose (fp); | ||
free (result); | ||
} | ||
return strdup (template); | ||
} | ||
|
||
|
||
/* | ||
* vi:tabstop=4 shiftwidth=4 expandtab | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/************************************************************\ | ||
* Copyright 2020 Lawrence Livermore National Security, LLC | ||
* (c.f. AUTHORS, NOTICE.LLNS, COPYING) | ||
* | ||
* This file is part of the Flux resource manager framework. | ||
* For details, see https://github.com/flux-framework. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0 | ||
\************************************************************/ | ||
|
||
#ifndef _SHELL_MUSTACHE_H | ||
#define _SHELL_MUSTACHE_H | ||
|
||
#include <stdio.h> | ||
|
||
typedef void (*mustache_log_f) (void *arg, | ||
const char *file, | ||
int line, | ||
const char *func, | ||
const char *subsys, | ||
int level, | ||
const char *fmt, | ||
va_list args); | ||
|
||
/* Mustache tag callback function prototype. This function is called | ||
* for any mustache tag 'name' found in the template by the mustache | ||
* template renderer. E.g. if {{foo}} then the tag callback will be | ||
* called with `tag == "foo"`. The function should just write the value | ||
* of `tag` to the FILE stream `fp` if found. | ||
* | ||
* This function should return < 0 on error, in which case the template | ||
* renderer will copy the unsubstituted mustache tag into the result. | ||
*/ | ||
typedef int (*mustache_tag_f) (FILE *fp, const char *tag, void *arg); | ||
|
||
struct mustache_renderer; | ||
|
||
/* Create a mustache renderer, with mustache tags expanded by tag | ||
* callback `cb`. (See callback definition above). | ||
*/ | ||
struct mustache_renderer *mustache_renderer_create (mustache_tag_f cb, | ||
void *arg); | ||
|
||
void mustache_renderer_destroy (struct mustache_renderer *mr); | ||
|
||
/* Set a custom logger for mustache renderer 'mr'. | ||
*/ | ||
void mustache_renderer_set_log (struct mustache_renderer *mr, | ||
mustache_log_f log, | ||
void *log_data); | ||
|
||
/* Render the mustache template 'template' with renderer 'mr'. | ||
*/ | ||
char * mustache_render (struct mustache_renderer *mr, const char *template); | ||
|
||
#endif /* !_SHELL_MUSTACHE_H */ | ||
|
||
/* | ||
* vi:tabstop=4 shiftwidth=4 expandtab | ||
*/ |
Oops, something went wrong.