1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2006 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include "opal/mca/installdirs/installdirs.h"
#include "opal/util/show_help.h"
#include "opal/util/show_help_lex.h"
#include "opal/util/printf.h"
#include "opal/util/argv.h"
#include "opal/util/os_path.h"
#include "opal/constants.h"
static int open_file(const char *base, const char *topic);
static int find_topic(const char *base, const char *topic);
static int read_message(char ***lines);
static int output(bool want_error_header, char **lines,
const char *base, const char *topic,
va_list arglist);
static int destroy_message(char **lines);
/*
* 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";
static const char *dash_line = "--------------------------------------------------------------------------\n";
int opal_show_help(const char *filename, const char *topic,
bool want_error_header, ...)
{
int ret;
va_list arglist;
char **array = NULL;
if (OPAL_SUCCESS != (ret = open_file(filename, topic))) {
return ret;
}
if (OPAL_SUCCESS != (ret = find_topic(filename, topic))) {
fclose(opal_show_help_yyin);
return ret;
}
ret = read_message(&array);
opal_show_help_finish_parsing();
fclose(opal_show_help_yyin);
if (OPAL_SUCCESS != ret) {
destroy_message(array);
return ret;
}
va_start(arglist, want_error_header);
output(want_error_header, array, filename, topic, arglist);
va_end(arglist);
destroy_message(array);
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. */
filename = opal_os_path( false, opal_install_dirs.pkgdatadir, base, NULL );
opal_show_help_yyin = fopen(filename, "r");
free(filename);
if (NULL == opal_show_help_yyin) {
asprintf(&filename, "%s/%s.txt", opal_install_dirs.pkgdatadir, base);
opal_show_help_yyin = fopen(filename, "r");
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) */
asprintf(&filename, "%s/%s.%s", opal_install_dirs.pkgdatadir, base, lang);
opal_show_help_yyin = fopen(filename, "r");
free(filename);
if (NULL == opal_show_help_yyin) {
asprintf(&filename, "%s/%s.%s", opal_install_dirs.pkgdatadir,
base, default_language);
opal_show_help_yyin = fopen(filename, "r");
free(filename);
}
/* If we still couldn't find it, try with no extension */
if (NULL == opal_show_help_yyin) {
filename = opal_os_path( false, opal_install_dirs.pkgdatadir, base, NULL );
opal_show_help_yyin = fopen(filename, "r");
free(filename);
}
#endif
/* If we still couldn't open it, then something is wrong */
if (NULL == opal_show_help_yyin) {
fprintf(stderr, dash_line);
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");
fprintf(stderr, dash_line);
return OPAL_ERR_NOT_FOUND;
}
/* Set the buffer */
opal_show_help_init_buffer(opal_show_help_yyin);
/* Happiness */
return OPAL_SUCCESS;
}
/*
* 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) {
token = opal_show_help_yylex();
switch (token) {
case OPAL_SHOW_HELP_PARSE_TOPIC:
tmp = strdup(opal_show_help_yytext);
if (NULL == tmp) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
tmp[strlen(tmp) - 1] = '\0';
ret = strcmp(tmp + 1, topic);
free(tmp);
if (0 == ret) {
return OPAL_SUCCESS;
}
break;
case OPAL_SHOW_HELP_PARSE_MESSAGE:
break;
case OPAL_SHOW_HELP_PARSE_DONE:
fprintf(stderr, dash_line);
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");
fprintf(stderr, dash_line);
return OPAL_ERR_NOT_FOUND;
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.
*/
static int read_message(char ***array)
{
char *tmp;
int token;
while (1) {
token = opal_show_help_yylex();
switch (token) {
case OPAL_SHOW_HELP_PARSE_MESSAGE:
tmp = strdup(opal_show_help_yytext);
if (NULL == tmp) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
opal_argv_append_nosize(array, tmp);
break;
default:
return OPAL_SUCCESS;
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. :-)
*/
static int output(bool want_error_header, char **lines,
const char *base, const char *topic,
va_list arglist)
{
int i, count;
size_t len;
char *concat;
/* See how much space we need */
len = want_error_header ? 2 * strlen(dash_line) : 0;
count = opal_argv_count(lines);
for (i = 0; i < count; ++i) {
if (NULL == lines[i]) {
break;
}
len += strlen(lines[i]) + 1;
}
/* Malloc it out */
concat = (char*) malloc(len + 1);
if (NULL == concat) {
fprintf(stderr, dash_line);
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");
fprintf(stderr, dash_line);
return OPAL_ERR_OUT_OF_RESOURCE;
}
/* Fill the big string */
*concat = '\0';
if (want_error_header) {
strcat(concat, dash_line);
}
for (i = 0; i < count; ++i) {
if (NULL == lines[i]) {
break;
}
strcat(concat, lines[i]);
strcat(concat, "\n");
}
if (want_error_header) {
strcat(concat, dash_line);
}
/* Apply formatting */
vfprintf( stderr, concat, arglist );
/* All done */
free(concat);
return OPAL_SUCCESS;
}
/*
* Free all the strings in the array and destruct the array
*/
static int destroy_message(char **lines)
{
int i, count;
count = opal_argv_count(lines);
for (i = 0; i < count; ++i) {
if (NULL == lines[i]) {
break;
}
free(lines[i]);
}
return OPAL_SUCCESS;
}
|