Skip to content

Commit b46e350

Browse files
committed
Update show-help system
Allow authors to include a link to another file/topic in their descriptions, with "show_help" picking up the referenced info and inserting it into the output at that point. Syntax is: ``` ``` Remove dependency on Flex for this subsystem. Signed-off-by: Ralph Castain <[email protected]>
1 parent 735b29a commit b46e350

File tree

8 files changed

+123
-308
lines changed

8 files changed

+123
-308
lines changed

config/pmix.m4

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,6 @@ AC_DEFUN([PMIX_SETUP_CORE],[
993993
pmix_config_prefix[src/include/Makefile]
994994
pmix_config_prefix[src/util/Makefile]
995995
pmix_config_prefix[src/util/keyval/Makefile]
996-
pmix_config_prefix[src/util/showhelp/Makefile]
997996
pmix_config_prefix[src/mca/base/Makefile]
998997
pmix_config_prefix[src/tools/pevent/Makefile]
999998
pmix_config_prefix[src/tools/pmix_info/Makefile]

src/Makefile.am

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ SUBDIRS = \
2626
include \
2727
class \
2828
util/keyval \
29-
util/showhelp \
3029
util \
3130
mca/base \
3231
$(MCA_pmix_FRAMEWORKS_SUBDIRS) \
@@ -38,7 +37,6 @@ DIST_SUBDIRS = \
3837
include \
3938
class \
4039
util/keyval \
41-
util/showhelp \
4240
util \
4341
mca/base \
4442
$(MCA_pmix_FRAMEWORKS_SUBDIRS) \

src/util/Makefile.am

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,9 @@ sources = \
9999
libpmix_util_la_SOURCES = $(headers) $(sources)
100100

101101
libpmix_util_la_LIBADD = \
102-
keyval/libpmixutilkeyval.la \
103-
showhelp/libpmixutilshowhelp.la
102+
keyval/libpmixutilkeyval.la
104103
libpmix_util_la_DEPENDENCIES = \
105-
keyval/libpmixutilkeyval.la \
106-
showhelp/libpmixutilshowhelp.la
104+
keyval/libpmixutilkeyval.la
107105

108106
# Unconditionally install the header files
109107

src/util/pmix_cmd_line.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,29 +189,22 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
189189
pmix_argv_free(argv);
190190
return PMIX_OPERATION_SUCCEEDED;
191191
}
192-
/* see if the argument is one of our options */
193-
found = false;
194-
for (n=0; NULL != myoptions[n].name; n++) {
195-
if (0 == strcmp(ptr, myoptions[n].name)) {
196-
// it is, so they requested help on this option
197-
str = pmix_show_help_string(helpfile, ptr, false);
198-
if (NULL != str) {
199-
printf("%s", str);
200-
free(str);
201-
}
202-
pmix_argv_free(argv);
203-
return PMIX_OPERATION_SUCCEEDED;
204-
}
205-
}
206-
if (!found) {
207-
// let the user know we don't recognize that option
192+
/* see if we have help on that subject */
193+
str = pmix_show_help_string(helpfile, ptr, false);
194+
if (NULL == str) {
195+
// let the user know we don't recognize that topic
208196
str = pmix_show_help_string("help-cli.txt", "unknown-option", true,
209197
ptr, pmix_tool_basename);
210198
if (NULL != str) {
211199
printf("%s", str);
212200
free(str);
213201
}
202+
} else {
203+
printf("%s", str);
204+
free(str);
214205
}
206+
pmix_argv_free(argv);
207+
return PMIX_OPERATION_SUCCEEDED;
215208
} else if (NULL == optarg) {
216209
// high-level help request
217210
str = pmix_show_help_string(helpfile, "usage", false,

src/util/pmix_show_help.c

Lines changed: 112 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@
3737
#include "src/util/pmix_output.h"
3838
#include "src/util/pmix_printf.h"
3939
#include "src/util/pmix_show_help.h"
40-
#include "src/util/showhelp/showhelp_lex.h"
4140

4241
bool pmix_show_help_enabled = false;
4342
static time_t show_help_time_last_displayed = 0;
4443
static bool show_help_timer_set = false;
4544
static pmix_event_t show_help_timer_event;
4645
static int output_stream = -1;
4746

47+
static pmix_status_t load_array(char ***array,
48+
const char *filename,
49+
const char *topic);
50+
4851
/* How long to wait between displaying duplicate show_help notices */
4952
static struct timeval show_help_interval = {5, 0};
5053

@@ -401,12 +404,15 @@ static pmix_status_t array2string(char **outstring, int want_error_header, char
401404
/*
402405
* Find the right file to open
403406
*/
404-
static pmix_status_t open_file(const char *base, const char *topic)
407+
static pmix_status_t open_file(const char *base,
408+
const char *topic,
409+
FILE **fptr)
405410
{
406411
char *filename;
407412
char *err_msg = NULL;
408413
size_t base_len;
409414
int i;
415+
FILE *fp = NULL;
410416

411417
/* If no filename was supplied, use the default */
412418

@@ -423,8 +429,8 @@ static pmix_status_t open_file(const char *base, const char *topic)
423429
*/
424430
for (i = 0; NULL != search_dirs[i]; i++) {
425431
filename = pmix_os_path(false, search_dirs[i], base, NULL);
426-
pmix_showhelp_yyin = fopen(filename, "r");
427-
if (NULL == pmix_showhelp_yyin) {
432+
fp = fopen(filename, "r");
433+
if (NULL == fp) {
428434
if (NULL != err_msg) {
429435
free(err_msg);
430436
}
@@ -435,23 +441,22 @@ static pmix_status_t open_file(const char *base, const char *topic)
435441
base_len = strlen(base);
436442
if (4 > base_len || 0 != strcmp(base + base_len - 4, ".txt")) {
437443
free(filename);
438-
if (0
439-
> asprintf(&filename, "%s%s%s.txt", search_dirs[i], PMIX_PATH_SEP, base)) {
444+
if (0 > asprintf(&filename, "%s%s%s.txt", search_dirs[i], PMIX_PATH_SEP, base)) {
440445
free(err_msg);
441446
return PMIX_ERR_OUT_OF_RESOURCE;
442447
}
443-
pmix_showhelp_yyin = fopen(filename, "r");
448+
fp = fopen(filename, "r");
444449
}
445450
}
446451
free(filename);
447-
if (NULL != pmix_showhelp_yyin) {
452+
if (NULL != fp) {
448453
break;
449454
}
450455
}
451456
}
452457

453458
/* If we still couldn't open it, then something is wrong */
454-
if (NULL == pmix_showhelp_yyin) {
459+
if (NULL == fp) {
455460
char *msg;
456461
pmix_asprintf(&msg, "%sSorry! You were supposed to get help about:\n %s\nBut I couldn't open "
457462
"the help file:\n %s. Sorry!\n%s", dash_line, topic, err_msg, dash_line);
@@ -464,102 +469,142 @@ static pmix_status_t open_file(const char *base, const char *topic)
464469
free(err_msg);
465470
}
466471

467-
/* Set the buffer */
472+
*fptr = fp;
473+
/* Happiness */
474+
return PMIX_SUCCESS;
475+
}
468476

469-
pmix_showhelp_init_buffer(pmix_showhelp_yyin);
477+
#define PMIX_MAX_LINE_LENGTH 1024
470478

471-
/* Happiness */
479+
static char *localgetline(FILE *fp)
480+
{
481+
char *ret, *buff;
482+
char input[PMIX_MAX_LINE_LENGTH];
483+
int i = 0;
484+
485+
ret = fgets(input, PMIX_MAX_LINE_LENGTH, fp);
486+
if (NULL != ret) {
487+
if ('\0' != input[0]) {
488+
input[strlen(input) - 1] = '\0'; /* remove newline */
489+
}
490+
buff = strdup(&input[i]);
491+
return buff;
492+
}
472493

473-
return PMIX_SUCCESS;
494+
return NULL;
474495
}
475496

476497
/*
477498
* In the file that has already been opened, find the topic that we're
478499
* supposed to output
479500
*/
480-
static pmix_status_t find_topic(const char *base, const char *topic)
501+
static pmix_status_t find_topic(FILE *fp,
502+
const char *base,
503+
const char *topic)
481504
{
482-
int token, ret;
483-
char *tmp;
505+
char *line, *cptr;
506+
PMIX_HIDE_UNUSED_PARAMS(base);
484507

485508
/* Examine every topic */
486509

487-
while (1) {
488-
token = pmix_showhelp_yylex();
489-
switch (token) {
490-
case PMIX_SHOW_HELP_PARSE_TOPIC:
491-
tmp = strdup(pmix_showhelp_yytext);
492-
if (NULL == tmp) {
493-
return PMIX_ERR_OUT_OF_RESOURCE;
494-
}
495-
tmp[strlen(tmp) - 1] = '\0';
496-
ret = strcmp(tmp + 1, topic);
497-
free(tmp);
498-
if (0 == ret) {
499-
return PMIX_SUCCESS;
500-
}
501-
break;
502-
503-
case PMIX_SHOW_HELP_PARSE_MESSAGE:
504-
break;
505-
506-
case PMIX_SHOW_HELP_PARSE_DONE: {
507-
char *msg;
508-
pmix_asprintf(&msg, "%sSorry! You were supposed to get help about:\n %s\nBut I couldn't open "
509-
"the help file:\n %s. Sorry!\n%s", dash_line, topic, base, dash_line);
510-
local_delivery(base, topic, msg);
511-
return PMIX_ERR_NOT_FOUND;
510+
while (NULL != (line = localgetline(fp))) {
511+
/* topics start with a '[' in the first position */
512+
if ('[' != line[0]) {
513+
free(line);
514+
continue;
512515
}
513-
default:
514-
break;
516+
/* find the end of the topic name */
517+
cptr = strchr(line, ']');
518+
if (NULL == cptr) {
519+
/* this is not a valid topic */
520+
free(line);
521+
continue;
522+
}
523+
*cptr = '\0';
524+
if (0 == strcmp(&line[1], topic)) {
525+
/* this is the topic */
526+
free(line);
527+
return PMIX_SUCCESS;
515528
}
529+
/* not the topic we want */
530+
free(line);
516531
}
517532

518-
/* Never get here */
533+
return PMIX_ERR_NOT_FOUND;
519534
}
520535

521536
/*
522537
* We have an open file, and we're pointed at the right topic. So
523538
* read in all the lines in the topic and make a list of them.
524539
*/
525-
static pmix_status_t read_topic(char ***array)
540+
static pmix_status_t read_topic(FILE *fp, char ***array)
526541
{
527-
int token, rc;
528-
529-
while (1) {
530-
token = pmix_showhelp_yylex();
531-
switch (token) {
532-
case PMIX_SHOW_HELP_PARSE_MESSAGE:
533-
/* pmix_argv_append_nosize does strdup(pmix_show_help_yytext) */
534-
rc = pmix_argv_append_nosize(array, pmix_showhelp_yytext);
535-
if (rc != PMIX_SUCCESS) {
542+
int rc;
543+
char *line, *file, *tp;
544+
545+
while (NULL != (line = localgetline(fp))) {
546+
/* the topic ends when we see either the end of
547+
* the file (indicated by a NULL return) or the
548+
* beginning of the next topic */
549+
if (0 == strncmp(line, "#include#", strlen("#include#"))) {
550+
/* keyword "include" found - check for file/topic */
551+
file = &line[strlen("#include#")];
552+
if (NULL == file) {
553+
/* missing filename */
554+
free(line);
555+
return PMIX_ERR_BAD_PARAM;
556+
}
557+
/* see if they provided a topic */
558+
tp = strchr(file, '#');
559+
if (NULL != tp) {
560+
*tp = '\0'; // NULL-terminate the filename
561+
++tp;
562+
}
563+
rc = load_array(array, file, tp);
564+
if (PMIX_SUCCESS != rc) {
565+
free(line);
536566
return rc;
537567
}
538-
break;
539-
540-
default:
568+
}
569+
if ('#' == line[0]) {
570+
/* skip comments */
571+
free(line);
572+
continue;
573+
}
574+
if ('[' == line[0]) {
575+
/* start of the next topic */
576+
free(line);
541577
return PMIX_SUCCESS;
542578
}
579+
/* save the line */
580+
rc = pmix_argv_append_nosize(array, line);
581+
free(line);
582+
if (rc != PMIX_SUCCESS) {
583+
return rc;
584+
}
543585
}
544586

545-
/* Never get here */
587+
return PMIX_SUCCESS;
546588
}
547589

548-
static pmix_status_t load_array(char ***array, const char *filename, const char *topic)
590+
static pmix_status_t load_array(char ***array,
591+
const char *filename,
592+
const char *topic)
549593
{
550594
int ret;
595+
FILE *fp;
551596

552-
if (PMIX_SUCCESS != (ret = open_file(filename, topic))) {
597+
ret = open_file(filename, topic, &fp);
598+
if (PMIX_SUCCESS != ret) {
553599
return ret;
554600
}
555601

556-
ret = find_topic(filename, topic);
602+
ret = find_topic(fp, filename, topic);
557603
if (PMIX_SUCCESS == ret) {
558-
ret = read_topic(array);
604+
ret = read_topic(fp, array);
559605
}
560606

561-
fclose(pmix_showhelp_yyin);
562-
pmix_showhelp_yylex_destroy();
607+
fclose(fp);
563608

564609
if (PMIX_SUCCESS != ret) {
565610
pmix_argv_free(*array);
@@ -568,7 +613,9 @@ static pmix_status_t load_array(char ***array, const char *filename, const char
568613
return ret;
569614
}
570615

571-
char *pmix_show_help_vstring(const char *filename, const char *topic, int want_error_header,
616+
char *pmix_show_help_vstring(const char *filename,
617+
const char *topic,
618+
int want_error_header,
572619
va_list arglist)
573620
{
574621
int rc;

src/util/showhelp/Makefile.am

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)