Skip to content

Commit fe41757

Browse files
committed
opal_show_help() cleanups.
1. Ensure it can print something when not initialized. While it in theory shouldn't be called before opal_show_help_init(), it is very easy to do so. In the case it isn't, just print to stderr. 2. Move some common code to static helper functions, and fix a bug where the "Sorry!" message will print '(null)' for the filename. Signed-off-by: Austen Lauria <awlauria@us.ibm.com>
1 parent da75063 commit fe41757

File tree

1 file changed

+62
-40
lines changed

1 file changed

+62
-40
lines changed

opal/util/show_help.c

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,26 @@ static void opal_show_help_finalize(void)
104104
}
105105
}
106106

107+
static void opal_show_help_output(const char *msg) {
108+
109+
if(-1 < output_stream) {
110+
opal_output(output_stream, "%s", msg);
111+
}
112+
else {
113+
// opal_show_help() was not initialized yet, but we should still be
114+
// able to print to stderr.
115+
fprintf(stderr, "%s", msg);
116+
}
117+
}
118+
107119
static void opal_show_help_cbfunc(pmix_status_t status, void *cbdata)
108120
{
109121
opal_log_info_t *info = (opal_log_info_t *) cbdata;
110122
if(PMIX_SUCCESS != status && PMIX_OPERATION_SUCCEEDED != status) {
111123
// Aggregation/de-duplication functionality is *probably* lost,
112124
// but let's print the error anyway since duplicate error messages
113125
// is better than hiding it.
114-
opal_output(output_stream, "%s", info->msg);
126+
opal_show_help_output(info->msg);
115127
}
116128
PMIX_INFO_DESTRUCT(info->info);
117129
if(info->dirs) {
@@ -146,7 +158,7 @@ static void local_delivery(const char *file, const char *topic, char *msg) {
146158
// Aggregation/de-duplication functionality is *definitely* lost,
147159
// but let's print the error anyway since duplicate error messages
148160
// is better than hiding it.
149-
opal_output(output_stream, "%s", msg);
161+
opal_show_help_output(msg);
150162
PMIX_INFO_DESTRUCT(info);
151163
if(opal_help_want_aggregate) {
152164
PMIX_INFO_DESTRUCT(dirs);
@@ -156,6 +168,15 @@ static void local_delivery(const char *file, const char *topic, char *msg) {
156168
}
157169
}
158170

171+
static void opal_show_help_error(const char *file, const char *topic) {
172+
char *msg;
173+
opal_asprintf(&msg,
174+
"%sSorry! You were supposed to get help about:\n %s\nfrom the file:\n "
175+
" %s\nBut I couldn't find that topic in the file. Sorry!\n%s",
176+
dash_line, topic ? topic : "(Not specified)", file ? file : "(Not specified)", dash_line);
177+
local_delivery(topic, file, msg);
178+
}
179+
159180
/*
160181
* Make one big string with all the lines. This isn't the most
161182
* efficient method in the world, but we're going for clarity here --
@@ -209,13 +230,11 @@ static int array2string(char **outstring, int want_error_header, char **lines)
209230
*/
210231
static int open_file(const char *base, const char *topic)
211232
{
212-
char *filename;
213-
char *err_msg = NULL;
233+
char *filename = NULL, *err_msg = NULL;
214234
size_t base_len;
215-
int i;
235+
int i, rc = OPAL_SUCCESS;
216236

217237
/* If no filename was supplied, use the default */
218-
219238
if (NULL == base) {
220239
base = default_filename;
221240
}
@@ -229,46 +248,54 @@ static int open_file(const char *base, const char *topic)
229248
*/
230249
for (i = 0; NULL != search_dirs[i]; i++) {
231250
filename = opal_os_path(false, search_dirs[i], base, NULL);
232-
opal_show_help_yyin = fopen(filename, "r");
233-
if (NULL == opal_show_help_yyin) {
234-
opal_asprintf(&err_msg, "%s: %s", filename, strerror(errno));
235-
base_len = strlen(base);
236-
if (4 > base_len || 0 != strcmp(base + base_len - 4, ".txt")) {
237-
free(filename);
238-
opal_asprintf(&filename, "%s%s%s.txt", search_dirs[i], OPAL_PATH_SEP, base);
239-
opal_show_help_yyin = fopen(filename, "r");
251+
if(filename) {
252+
opal_show_help_yyin = fopen(filename, "r");
253+
if (NULL == opal_show_help_yyin) {
254+
opal_asprintf(&err_msg, "%s: %s", filename, strerror(errno));
255+
base_len = strlen(base);
256+
if (4 > base_len || 0 != strcmp(base + base_len - 4, ".txt")) {
257+
free(filename);
258+
opal_asprintf(&filename, "%s%s%s.txt", search_dirs[i], OPAL_PATH_SEP, base);
259+
opal_show_help_yyin = fopen(filename, "r");
260+
}
261+
}
262+
if (NULL != opal_show_help_yyin) {
263+
break;
240264
}
241-
}
242-
free(filename);
243-
if (NULL != opal_show_help_yyin) {
244-
break;
245265
}
246266
}
247267
}
248268

249269
/* If we still couldn't open it, then something is wrong */
250270
if (NULL == opal_show_help_yyin) {
251-
char *tmp;
252-
opal_asprintf(&tmp,
253-
"%sSorry! You were supposed to get help about:\n %s\nBut I couldn't open "
254-
"the help file:\n %s. Sorry!\n%s",
255-
dash_line, topic, err_msg, dash_line);
256-
local_delivery(topic, err_msg, tmp);
257-
free(err_msg);
258-
return OPAL_ERR_NOT_FOUND;
259-
}
260-
261-
if (NULL != err_msg) {
262-
free(err_msg);
271+
const char *file_ptr = NULL;
272+
if(err_msg) {
273+
file_ptr = err_msg;
274+
}
275+
else if(filename) {
276+
file_ptr = filename;
277+
}
278+
else {
279+
file_ptr = base;
280+
}
281+
opal_show_help_error(file_ptr, topic);
282+
rc = OPAL_ERR_NOT_FOUND;
283+
goto cleanup;
263284
}
264285

265286
/* Set the buffer */
266-
267287
opal_show_help_init_buffer(opal_show_help_yyin);
268288

269-
/* Happiness */
289+
cleanup:
270290

271-
return OPAL_SUCCESS;
291+
if(filename) {
292+
free(filename);
293+
}
294+
if(err_msg) {
295+
free(err_msg);
296+
}
297+
298+
return rc;
272299
}
273300

274301
/*
@@ -302,13 +329,8 @@ static int find_topic(const char *base, const char *topic)
302329
break;
303330

304331
case OPAL_SHOW_HELP_PARSE_DONE: {
305-
char *msg;
306-
opal_asprintf(&msg,
307-
"%sSorry! You were supposed to get help about:\n %s\nfrom the file:\n "
308-
" %s\nBut I couldn't find that topic in the file. Sorry!\n%s",
309-
dash_line, topic, base, dash_line);
310-
local_delivery(topic, base, msg);
311-
return OPAL_ERR_NOT_FOUND;
332+
opal_show_help_error(topic, base);
333+
return OPAL_ERR_NOT_FOUND;
312334
}
313335
default:
314336
break;

0 commit comments

Comments
 (0)