2004-09-05 20:05:37 +04:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
2006-08-23 04:29:35 +04:00
|
|
|
* Copyright (c) 2004-2006 The University of Tennessee and The University
|
2005-11-05 22:57:48 +03:00
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-09-05 20:05:37 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2004-09-05 20:05:37 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <locale.h>
|
|
|
|
|
2006-03-12 07:35:01 +03:00
|
|
|
#include "opal/install_dirs.h"
|
2005-07-04 06:38:44 +04:00
|
|
|
#include "opal/util/show_help.h"
|
|
|
|
#include "opal/util/show_help_lex.h"
|
2005-07-04 06:16:57 +04:00
|
|
|
#include "opal/util/printf.h"
|
2005-07-04 04:13:44 +04:00
|
|
|
#include "opal/util/argv.h"
|
2006-08-23 04:29:35 +04:00
|
|
|
#include "opal/util/os_path.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/constants.h"
|
2004-09-05 20:05:37 +04:00
|
|
|
|
|
|
|
static int open_file(const char *base, const char *topic);
|
|
|
|
static int find_topic(const char *base, const char *topic);
|
2005-07-02 18:19:49 +04:00
|
|
|
static int read_message(char ***lines);
|
|
|
|
static int output(bool want_error_header, char **lines,
|
2004-09-05 20:05:37 +04:00
|
|
|
const char *base, const char *topic,
|
|
|
|
va_list arglist);
|
2005-07-02 18:19:49 +04:00
|
|
|
static int destroy_message(char **lines);
|
2004-09-05 20:05:37 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private variables
|
|
|
|
*/
|
|
|
|
#if 0
|
|
|
|
/* not attempting i18n-like support right now */
|
|
|
|
static const char *default_language = "C";
|
|
|
|
#endif
|
|
|
|
static const char *default_filename = "help-messages";
|
2005-06-17 05:04:21 +04:00
|
|
|
static const char *dash_line = "--------------------------------------------------------------------------\n";
|
2004-09-05 20:05:37 +04:00
|
|
|
|
|
|
|
|
2005-07-04 06:38:44 +04:00
|
|
|
int opal_show_help(const char *filename, const char *topic,
|
2004-09-05 20:05:37 +04:00
|
|
|
bool want_error_header, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list arglist;
|
2005-07-03 08:11:47 +04:00
|
|
|
char **array = NULL;
|
2004-09-05 20:05:37 +04:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
if (OPAL_SUCCESS != (ret = open_file(filename, topic))) {
|
2004-09-05 20:05:37 +04:00
|
|
|
return ret;
|
|
|
|
}
|
2006-02-12 04:33:29 +03:00
|
|
|
if (OPAL_SUCCESS != (ret = find_topic(filename, topic))) {
|
2005-07-04 06:38:44 +04:00
|
|
|
fclose(opal_show_help_yyin);
|
2004-09-05 20:05:37 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = read_message(&array);
|
2005-07-04 06:38:44 +04:00
|
|
|
opal_show_help_finish_parsing();
|
|
|
|
fclose(opal_show_help_yyin);
|
2006-02-12 04:33:29 +03:00
|
|
|
if (OPAL_SUCCESS != ret) {
|
2005-07-02 20:02:26 +04:00
|
|
|
destroy_message(array);
|
2004-09-05 20:05:37 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(arglist, want_error_header);
|
2005-07-02 20:02:26 +04:00
|
|
|
output(want_error_header, array, filename, topic, arglist);
|
2004-09-05 20:05:37 +04:00
|
|
|
va_end(arglist);
|
|
|
|
|
2005-07-02 20:02:26 +04:00
|
|
|
destroy_message(array);
|
2004-09-05 20:05:37 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the right file to open
|
|
|
|
*/
|
|
|
|
static int open_file(const char *base, const char *topic)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
/* not attempting i18n-like support right now */
|
|
|
|
const char *lang;
|
|
|
|
#endif
|
|
|
|
char *filename;
|
|
|
|
|
|
|
|
/* If no filename was supplied, use the default */
|
|
|
|
|
|
|
|
if (NULL == base) {
|
|
|
|
base = default_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't try any i18n-like support right now */
|
|
|
|
#if 1
|
|
|
|
/* Try to open the file. If we can't find it, try it with a .txt
|
|
|
|
extension. */
|
|
|
|
|
2006-08-23 04:29:35 +04:00
|
|
|
filename = opal_os_path( false, OPAL_PKGDATADIR, base, NULL );
|
2005-07-04 06:38:44 +04:00
|
|
|
opal_show_help_yyin = fopen(filename, "r");
|
2004-09-05 20:05:37 +04:00
|
|
|
free(filename);
|
2005-07-04 06:38:44 +04:00
|
|
|
if (NULL == opal_show_help_yyin) {
|
2006-03-12 07:35:01 +03:00
|
|
|
asprintf(&filename, "%s/%s.txt", OPAL_PKGDATADIR, base);
|
2005-07-04 06:38:44 +04:00
|
|
|
opal_show_help_yyin = fopen(filename, "r");
|
2004-09-05 20:05:37 +04:00
|
|
|
free(filename);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* What's our locale? */
|
|
|
|
|
|
|
|
lang = setlocale(LC_MESSAGES, "");
|
|
|
|
if (NULL == lang) {
|
|
|
|
lang = default_language;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do we have a file matching that locale? If not, open the
|
|
|
|
default language (because we know that we have that one) */
|
|
|
|
|
2006-03-12 07:35:01 +03:00
|
|
|
asprintf(&filename, "%s/%s.%s", OPAL_PKGDATADIR, base, lang);
|
2005-07-04 06:38:44 +04:00
|
|
|
opal_show_help_yyin = fopen(filename, "r");
|
2004-09-05 20:05:37 +04:00
|
|
|
free(filename);
|
2005-07-04 06:38:44 +04:00
|
|
|
if (NULL == opal_show_help_yyin) {
|
2006-03-12 07:35:01 +03:00
|
|
|
asprintf(&filename, "%s/%s.%s", OPAL_PKGDATADIR,
|
2004-09-05 20:05:37 +04:00
|
|
|
base, default_language);
|
2005-07-04 06:38:44 +04:00
|
|
|
opal_show_help_yyin = fopen(filename, "r");
|
2004-09-05 20:05:37 +04:00
|
|
|
free(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we still couldn't find it, try with no extension */
|
|
|
|
|
2005-07-04 06:38:44 +04:00
|
|
|
if (NULL == opal_show_help_yyin) {
|
2006-08-23 04:29:35 +04:00
|
|
|
filename = opal_os_path( false, OPAL_PKGDATADIR, base, NULL );
|
2005-07-04 06:38:44 +04:00
|
|
|
opal_show_help_yyin = fopen(filename, "r");
|
2004-09-05 20:05:37 +04:00
|
|
|
free(filename);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If we still couldn't open it, then something is wrong */
|
|
|
|
|
2005-07-04 06:38:44 +04:00
|
|
|
if (NULL == opal_show_help_yyin) {
|
2005-06-17 05:04:21 +04:00
|
|
|
fprintf(stderr, dash_line);
|
2004-09-05 20:05:37 +04:00
|
|
|
fprintf(stderr, "Sorry! You were supposed to get help about:\n %s\nfrom the file:\n %s\n", topic, base);
|
|
|
|
fprintf(stderr, "But I couldn't find any file matching that name. Sorry!\n");
|
2005-06-17 05:04:21 +04:00
|
|
|
fprintf(stderr, dash_line);
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_NOT_FOUND;
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|
|
|
|
|
2004-10-16 01:38:42 +04:00
|
|
|
/* Set the buffer */
|
|
|
|
|
2005-07-04 06:38:44 +04:00
|
|
|
opal_show_help_init_buffer(opal_show_help_yyin);
|
2004-10-16 01:38:42 +04:00
|
|
|
|
2004-09-05 20:05:37 +04:00
|
|
|
/* Happiness */
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In the file that has already been opened, find the topic that we're
|
|
|
|
* supposed to output
|
|
|
|
*/
|
|
|
|
static int find_topic(const char *base, const char *topic)
|
|
|
|
{
|
|
|
|
int token, ret;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
/* Examine every topic */
|
|
|
|
|
|
|
|
while (1) {
|
2005-07-04 06:38:44 +04:00
|
|
|
token = opal_show_help_yylex();
|
2004-09-05 20:05:37 +04:00
|
|
|
switch (token) {
|
2005-07-04 06:38:44 +04:00
|
|
|
case OPAL_SHOW_HELP_PARSE_TOPIC:
|
|
|
|
tmp = strdup(opal_show_help_yytext);
|
2004-09-05 20:05:37 +04:00
|
|
|
if (NULL == tmp) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|
|
|
|
tmp[strlen(tmp) - 1] = '\0';
|
|
|
|
ret = strcmp(tmp + 1, topic);
|
|
|
|
free(tmp);
|
|
|
|
if (0 == ret) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-07-04 06:38:44 +04:00
|
|
|
case OPAL_SHOW_HELP_PARSE_MESSAGE:
|
2004-09-05 20:05:37 +04:00
|
|
|
break;
|
|
|
|
|
2005-07-04 06:38:44 +04:00
|
|
|
case OPAL_SHOW_HELP_PARSE_DONE:
|
2005-06-17 05:04:21 +04:00
|
|
|
fprintf(stderr, dash_line);
|
2004-09-05 20:05:37 +04:00
|
|
|
fprintf(stderr, "Sorry! You were supposed to get help about:\n %s\nfrom the file:\n %s\n", topic, base);
|
|
|
|
fprintf(stderr, "But I couldn't find that topic in the file. Sorry!\n");
|
2005-06-17 05:04:21 +04:00
|
|
|
fprintf(stderr, dash_line);
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_NOT_FOUND;
|
2004-09-05 20:05:37 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Never get here */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have an open file, and we're pointed at the right topic. So
|
|
|
|
* read in all the lines in the topic and make a list of them.
|
|
|
|
*/
|
2005-07-02 18:19:49 +04:00
|
|
|
static int read_message(char ***array)
|
2004-09-05 20:05:37 +04:00
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
int token;
|
|
|
|
|
|
|
|
while (1) {
|
2005-07-04 06:38:44 +04:00
|
|
|
token = opal_show_help_yylex();
|
2004-09-05 20:05:37 +04:00
|
|
|
switch (token) {
|
2005-07-04 06:38:44 +04:00
|
|
|
case OPAL_SHOW_HELP_PARSE_MESSAGE:
|
|
|
|
tmp = strdup(opal_show_help_yytext);
|
2004-09-05 20:05:37 +04:00
|
|
|
if (NULL == tmp) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append_nosize(array, tmp);
|
2004-09-05 20:05:37 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-09-05 20:05:37 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Never get here */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make one big string with all the lines. This isn't the most
|
|
|
|
* efficient method in the world, but we're going for clarity here --
|
|
|
|
* not optimization. :-)
|
|
|
|
*/
|
2005-07-02 18:19:49 +04:00
|
|
|
static int output(bool want_error_header, char **lines,
|
2004-09-05 20:05:37 +04:00
|
|
|
const char *base, const char *topic,
|
|
|
|
va_list arglist)
|
|
|
|
{
|
2005-07-02 18:19:49 +04:00
|
|
|
int i, count;
|
2004-10-23 23:24:00 +04:00
|
|
|
size_t len;
|
2005-11-23 00:53:39 +03:00
|
|
|
char *concat;
|
2004-09-05 20:05:37 +04:00
|
|
|
|
|
|
|
/* See how much space we need */
|
|
|
|
|
2005-06-17 05:04:21 +04:00
|
|
|
len = want_error_header ? 2 * strlen(dash_line) : 0;
|
2005-07-04 04:13:44 +04:00
|
|
|
count = opal_argv_count(lines);
|
2005-07-02 18:19:49 +04:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
if (NULL == lines[i]) {
|
2004-09-05 20:05:37 +04:00
|
|
|
break;
|
|
|
|
}
|
2005-07-02 18:19:49 +04:00
|
|
|
len += strlen(lines[i]) + 1;
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Malloc it out */
|
|
|
|
|
2004-10-18 19:38:19 +04:00
|
|
|
concat = (char*) malloc(len + 1);
|
2004-09-05 20:05:37 +04:00
|
|
|
if (NULL == concat) {
|
2005-06-17 05:04:21 +04:00
|
|
|
fprintf(stderr, dash_line);
|
2004-09-05 20:05:37 +04:00
|
|
|
fprintf(stderr, "Sorry! You were supposed to get help about:\n %s\nfrom the file:\n %s\n", topic, base);
|
|
|
|
fprintf(stderr, "But memory seems to be exhausted. Sorry!\n");
|
2005-06-17 05:04:21 +04:00
|
|
|
fprintf(stderr, dash_line);
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill the big string */
|
|
|
|
|
|
|
|
*concat = '\0';
|
|
|
|
if (want_error_header) {
|
2005-06-17 05:04:21 +04:00
|
|
|
strcat(concat, dash_line);
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|
2005-07-02 18:19:49 +04:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
if (NULL == lines[i]) {
|
2004-09-05 20:05:37 +04:00
|
|
|
break;
|
|
|
|
}
|
2005-07-02 18:19:49 +04:00
|
|
|
strcat(concat, lines[i]);
|
2004-09-05 20:05:37 +04:00
|
|
|
strcat(concat, "\n");
|
|
|
|
}
|
|
|
|
if (want_error_header) {
|
2005-06-17 05:04:21 +04:00
|
|
|
strcat(concat, dash_line);
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Apply formatting */
|
2005-11-23 00:53:39 +03:00
|
|
|
vfprintf( stderr, concat, arglist );
|
2004-09-05 20:05:37 +04:00
|
|
|
|
|
|
|
/* All done */
|
|
|
|
|
|
|
|
free(concat);
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free all the strings in the array and destruct the array
|
|
|
|
*/
|
2005-07-02 18:19:49 +04:00
|
|
|
static int destroy_message(char **lines)
|
2004-09-05 20:05:37 +04:00
|
|
|
{
|
2005-07-02 18:19:49 +04:00
|
|
|
int i, count;
|
2004-09-05 20:05:37 +04:00
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
count = opal_argv_count(lines);
|
2005-07-02 18:19:49 +04:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
if (NULL == lines[i]) {
|
2004-09-05 20:05:37 +04:00
|
|
|
break;
|
|
|
|
}
|
2005-07-02 18:19:49 +04:00
|
|
|
free(lines[i]);
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-09-05 20:05:37 +04:00
|
|
|
}
|