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
4241bool pmix_show_help_enabled = false;
4342static time_t show_help_time_last_displayed = 0 ;
4443static bool show_help_timer_set = false;
4544static pmix_event_t show_help_timer_event ;
4645static 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 */
4952static 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 ;
0 commit comments