/*___INFO__MARK_BEGIN__*/ /************************************************************************* * * The Contents of this file are made available subject to the terms of * the Sun Industry Standards Source License Version 1.2 * * Sun Microsystems Inc., March, 2001 * * * Sun Industry Standards Source License Version 1.2 * ================================================= * The contents of this file are subject to the Sun Industry Standards * Source License Version 1.2 (the "License"); You may not use this file * except in compliance with the License. You may obtain a copy of the * License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html * * Software provided under this License is provided on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. * See the License for the specific provisions governing your rights and * obligations concerning the Software. * * The Initial Developer of the Original Code is: Sun Microsystems, Inc. * * Copyright: 2001 by Sun Microsystems, Inc. * * All Rights Reserved. * ************************************************************************/ /*___INFO__MARK_END__*/ /* Interactive formatted localized text*/ /* __ _ _ ____*/ /* -> infotext binary */ #include #include #include #include #include #include "sgermon.h" #include "basis_types.h" #include "sge_language.h" #include "msg_utilbin.h" #include "sge_dstring.h" #include "version.h" typedef struct sge_infotext_opt { int e; /* print to stderr */ int n; /* no new line */ int u; /* underline output */ char* D; /* dash string */ int S; /* nr of spaces */ char* yes; /* yes parameter for -ask */ char* no; /* no parameter for -ask */ char* def; /* default parameter for -ask */ int log; /* file logging on/off */ } sge_infotext_options; static void sge_infotext_welcome(void); static void sge_infotext_raw(char *format_string); static void sge_infotext_usage(void); static int sge_infotext_get_nr_of_substrings(const char* buffer, const char* substring); #if defined(ALPHA) || defined(SOLARISAMD64) || defined(ALPHA5) || defined(HPUX) || defined(NETBSD) || __GNUC__ >= 3 static char *sge_infotext_string_replace(dstring* buf, const char *arg, const char *what, const char *with, int only_first ); #endif static char* sge_infotext_string_input_parsing(dstring* buf,char* string); static char* sge_infotext_string_output_parsing(dstring* buf,char* string); static void sge_infotext_print_line(dstring* dash_buf, sge_infotext_options* options, dstring* line); static void sge_infotext_format_output(dstring* dash_buf, sge_infotext_options* options, char* text); static char* sge_infotext_get_next_word(dstring* buf,char* text); static const char* sge_infotext_build_dash(dstring* dash_buf, sge_infotext_options* options); static char* sge_infotext_build_test_msgstr(dstring* buffer, char* text); static char* sge_infotext_make_line_break(dstring* buffer, char* text); static int sge_infotext_all_spaces(char* text); int main(int argc, char *argv[]); static int sge_infotext_all_spaces(char* text) { int i; int back = 0; if (text != NULL) { for(i=0;i\n", text)); line = 0; for (h=0; h < strlen(text) ; h++) { if (h>=2) { if ( text [h-2] == '\\' && text[h-1] == 'n' ) { line = 0; sge_dstring_append(buffer,"\"\n\""); } } if ((line > 80 && text[h] == ' ') ) { line = 0; sge_dstring_append(buffer,"\"\n\""); } hbuf[0] = text[h]; sge_dstring_append(buffer, hbuf); DPRINTF(("<%s>\n", (char*) sge_dstring_get_string(buffer))); DPRINTF(("text[%d] = %c\n", h, text[h])); line++; } sge_dstring_append(buffer,"\""); DEXIT; return (char*) sge_dstring_get_string(buffer); } static char* sge_infotext_build_test_msgstr(dstring* buffer, char* text) { int h; char app_text[2]; app_text[1] = 0; sge_dstring_copy_string(buffer,""); for (h=0; h < strlen(text) ; h++) { app_text[0] = text[h]; sge_dstring_append(buffer,app_text); if (text[h] != '%' && text[h] != '\\' ) { if (h>0) { if ( text[h-1] == '\\' ) { continue; } if ( text[h-1] == '%' ) { continue; } } if (text[h] != ' ') { sge_dstring_append(buffer,"."); } } } return (char*) sge_dstring_get_string(buffer); } static const char* sge_infotext_build_dash(dstring* dash_buf, sge_infotext_options* options) { int i; sge_dstring_copy_string(dash_buf,""); for(i=0;iS;i++) { sge_dstring_append(dash_buf," "); } if (strlen(options->D) > 0) { sge_dstring_append(dash_buf,_SGE_GETTEXT__(options->D)); sge_dstring_append(dash_buf," "); } return sge_dstring_get_string(dash_buf); } static char* sge_infotext_get_next_word(dstring* buf, char* text) { char* p1; char* buffer; int i,b,not_last,nr_spaces; int start; sge_dstring_copy_string(buf,text); buffer = (char*)sge_dstring_get_string(buf); p1 = buffer; nr_spaces = 0; for (start=0;start 1) { int stop; stop = strlen(p1); for (i=0;in; bool done; DENTER(TOP_LAYER,"sge_infotext_format_output" ); DPRINTF(("format 1\n")); options->n = 0; column_var = getenv("SGE_INFOTEXT_MAX_COLUMN"); if (column_var != NULL) { max_column = atoi(column_var); } tp = text; DPRINTF(("do strcpy")); if (sge_dstring_get_string(dash_buf) == NULL) { DPRINTF(("dash_buf is null")); } sge_dstring_copy_dstring(&line,dash_buf); DPRINTF(("strcpy done")); sge_dstring_copy_string(&dash,""); DPRINTF(("copy done")); for (i=0;i< sge_dstring_strlen(&line);i++) { sge_dstring_append(&dash," "); } nr_word = 0; DPRINTF(("while\n")); done = false; while (!done) { char* next_word = NULL; int new_line_buffer; next_word = sge_infotext_get_next_word(&tmp_buf, tp); if (strlen(next_word) == 0 ) { if (nr_word != 0) { options->n = new_line_opt; sge_infotext_print_line(dash_buf,options,&line); } nr_word = 0; done = true; break; } if(nr_word != 0 ) { if (sge_infotext_all_spaces(next_word) != 0 ) { sge_dstring_append(&line," "); } } sge_dstring_append(&line,next_word); nr_word++; tp = (char*)strstr(tp,next_word); for(i=0;i max_column || strstr(sge_dstring_get_string(&line),"\n") != NULL ) { #if 0 /* uncomment this code if a word break should be done ================================================== */ int z; int l; sge_dstring_copy_dstring(&line2,&line); line_buf = (char*) sge_dstring_get_string(&line); line2_buf = (char*) sge_dstring_get_string(&line2); l=0; for(z=0;z <= sge_dstring_strlen(&line);z++) { line2_buf[l++] = line_buf[z]; if (l >= max_column ) { line2_buf[l]=0; sge_infotext_print_line(dash_buf,options,&line2); sge_dstring_copy_dstring(&line2, &dash); l=sge_dstring_strlen(&dash); } } if (l>0) { line2_buf[l]=0; sge_infotext_print_line(dash_buf,options,&line2); } #endif #if 1 /* this will do no word break */ /* ========================== */ new_line_buffer = options->n; next_word = sge_infotext_get_next_word(&tmp_buf,tp); if (strlen(next_word) == 0) { options->n = 0; } else { options->n = 1; } sge_infotext_print_line(dash_buf,options,&line); options->n = new_line_buffer; #endif nr_word = 0; sge_dstring_copy_dstring(&line,&dash); } else { next_word = sge_infotext_get_next_word(&tmp_buf,tp); if (strlen(next_word) == 0) { options->n = new_line_opt; sge_infotext_print_line(dash_buf,options,&line); nr_word = 0; done = true; break; } if (sge_dstring_strlen(&line) + strlen(next_word) + 1 > max_column && nr_word != 0) { sge_infotext_print_line(dash_buf,options,&line); nr_word = 0; sge_dstring_copy_dstring(&line,&dash); } } } DPRINTF(("free strings\n")); options->n = new_line_opt; sge_dstring_free(&dash); sge_dstring_free(&tmp_buf); sge_dstring_free(&line); sge_dstring_free(&line2); /* Ugly, but needed for remote invocation with -wait or -ask options */ fflush((options->e == 1) ? stderr : stdout); DEXIT; } static void sge_infotext_print_line(dstring* dash_buf, sge_infotext_options* options, dstring* line_arg) { int i; FILE* output; int line_length; int lc; char* line_buf; dstring line = DSTRING_INIT; dstring dash = DSTRING_INIT; dstring line_buf_buffer = DSTRING_INIT; sge_dstring_copy_dstring(&line,dash_buf); sge_dstring_copy_string(&dash,"\n"); for(i=0;i< sge_dstring_strlen(&line);i++) { sge_dstring_append(&dash," "); } sge_dstring_copy_dstring(&line, line_arg); line_length = 0; lc = 0; line_buf = (char*)sge_dstring_get_string(&line); for(i=0;ie == 1) { output=stderr; } if ((getenv("SGE_NOMSG") != NULL && options->log == 1) || (getenv("SGE_NOMSG") == NULL && options->log == 0)) { fprintf(output,"%s",sge_dstring_get_string(&line)); if (options->n != 1 && line_length > 0 ) { fprintf(output,"\n"); } if (options->u == 1 && line_length > 0) { if (options->n == 1) { fprintf(output,"\n"); } for(i=0;in != 1) { fprintf(output,"\n"); } } } sge_dstring_free(&line); sge_dstring_free(&dash); sge_dstring_free(&line_buf_buffer); } static char* sge_infotext_string_input_parsing(dstring* string_buffer,char* string) { char* h1 = NULL; char* h2 = NULL; char buf[10]; int special_sign = 0; if (string == NULL) { return NULL; } if (strlen(string) == 0) { sge_dstring_copy_string(string_buffer,""); return (char*)sge_dstring_get_string(string_buffer); } h1 = string; h2 = h1; h2++; strcpy(buf,"a"); sge_dstring_copy_string(string_buffer,""); while (*h1 != 0 && *h2 != 0) { special_sign = 0; if (*h1 == '\\' && *h2 == 'n') { sge_dstring_append(string_buffer, "\n"); special_sign = 1; } if (*h1 == '\\' && *h2 == '\"') { sge_dstring_append(string_buffer, "\""); special_sign = 1; } if (*h1 == '\\' && *h2 == 'r') { sge_dstring_append(string_buffer, "\r"); special_sign = 1; } if (*h1 == '\\' && *h2 == 't') { sge_dstring_append(string_buffer, "\t"); special_sign = 1; } if (special_sign == 1) { h1++; h2++; if (*h1 == 0 || *h2 == 0) { break; } h1++; h2++; continue; } buf[0] = *h1; sge_dstring_append(string_buffer, buf); h1++; h2++; } if (special_sign != 1) { buf[0] = *h1; sge_dstring_append(string_buffer, buf); } return (char*)sge_dstring_get_string(string_buffer); } static char* sge_infotext_string_output_parsing(dstring* string_buffer,char* string) { char* h1 = NULL; char buf[10]; sge_dstring_copy_string(string_buffer,""); strcpy(buf,"a"); h1 = string; while (*h1 != 0) { /* if (*h1 == '\\') { sge_dstring_append(string_buffer, "\\\\"); h1++; continue; } */ if (*h1 == '\n') { sge_dstring_append(string_buffer, "\\n"); h1++; continue; } if (*h1 == '\"') { sge_dstring_append(string_buffer, "\\\""); h1++; continue; } buf[0] = *h1; sge_dstring_append(string_buffer, buf); h1++; } return (char*)sge_dstring_get_string(string_buffer); } #if defined(ALPHA) || defined(SOLARISAMD64) || defined(ALPHA5) || defined(HPUX) || defined(IRIX65) || defined(NETBSD) || __GNUC__ >= 3 static char *sge_infotext_string_replace(dstring* tmp_buf, const char *arg, const char *what, const char *with, int only_first) { int i; char *p1; const char *p2; dstring arg_copy = DSTRING_INIT; sge_dstring_copy_string(&arg_copy, arg); p2 = sge_dstring_get_string(&arg_copy); p1 = strstr(p2, what); if (p1 == NULL) { sge_dstring_copy_string(tmp_buf,arg); sge_dstring_free(&arg_copy); return (char*) sge_dstring_get_string(tmp_buf); } sge_dstring_copy_string(tmp_buf,""); while (p1 != NULL) { *p1 = 0; sge_dstring_append(tmp_buf,p2); sge_dstring_append(tmp_buf,with); p2 = p1; for(i=0;i\"\n"); printf(" S - nr of spaces, e.g. -S \"5\"\n\n"); printf("infotext [-log] [-auto 0|1|true|false] [-wait] [-ask YES NO] [-def YES|NO]\n"); printf(" FORMAT_STRING ARGUMENTS\n\n"); printf("YES - user answer for exit status 0, e.g. -ask \"y\"\n"); printf("NO - user answer for exit status 1, e.g. -ask \"n\"\n\n"); printf("options:\n"); printf(" auto - switch auto off/on [0|false/1|true], this will don't ask or wait,\n"); printf(" just use default\n"); printf(" wait - wait for return key\n"); printf(" ask - wait for user input\n"); printf(" def - default answer when user is just pressing RETURN\n\n"); printf(" log - write output to log, if SGE_NOMSG is set\n"); printf("used environment variables:\n"); printf("SGE_INFOTEXT_MAX_COLUMN - column for word break (default 79)\n"); } /*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/ int main( int argc, char* argv[] ) { int no_options = 0; int ret_val = 0; int show_help = 0; int do_test = 0; int do_raw = 0; int do_message = 0; int do_message_space = 0; int do_ask = 0; int do_wait = 0; int do_auto = 0; int auto_option_used = 0; int do_def = 0; int args_ok = 1; int i = 0; int last_option = 0; int arg_start = 0; int max_args = 0; int string_arguments = 0; int first_arg = 0; int real_args = 0; char* help_str = NULL; sge_infotext_options options; dstring buffer = DSTRING_INIT; dstring buffer2 = DSTRING_INIT; dstring sge_infotext_dash_buffer = DSTRING_INIT; dstring tmp_buf = DSTRING_INIT; DENTER_MAIN(TOP_LAYER, "sge_infotext"); #ifdef __SGE_COMPILE_WITH_GETTEXT__ sge_init_language_func((gettext_func_type) gettext, (setlocale_func_type) setlocale, (bindtextdomain_func_type) bindtextdomain, (textdomain_func_type) textdomain); sge_init_language(NULL,NULL); #endif /* __SGE_COMPILE_WITH_GETTEXT__ */ options.e = 0; options.n = 0; options.u = 0; options.D = ""; options.S = 0; options.no = ""; options.yes = ""; options.def = ""; options.log = 0; for(i=1; i< argc; i++) { char* arg = argv[i]; if (arg[0] == '-' && arg_start == 0 && no_options == 0) { int o_start = 0; char* option = NULL; int h; int opt_length; /* option */ while( arg[o_start++] == '-' ); option = &arg[--o_start]; last_option = o_start; if ( strcmp(option,"help") == 0) { show_help = 1; break; } if ( strcmp(option,"test") == 0) { do_test = 1; break; } if ( strcmp(option,"log") == 0) { options.log = 1; continue; } if ( strcmp(option,"__eoc__") == 0) { no_options = 1; continue; } if ( strcmp(option,"raw") == 0) { do_raw = 1; continue; } if ( strcmp(option,"message") == 0) { do_message = 1; continue; } if ( strcmp(option,"message-space") == 0) { do_message_space = 1; do_message = 1; continue; } if (strcmp(option,"wait") == 0) { do_wait = 1; continue; } if (strcmp(option,"auto") == 0) { do_auto = 1; auto_option_used=1; if ( (i+1) < argc) { i++; if (strcmp(argv[i], "false") == 0) { do_auto = 0; continue; } if (strcmp(argv[i], "true") == 0) { continue; } if (atoi(argv[i]) == 0) { do_auto = 0; continue; } } else { printf("no complete -auto option argument\n"); args_ok = 0; } continue; } if ( strcmp(option,"ask") == 0) { do_ask = 1; if ( (i+2) < argc) { i++; options.yes = argv[i]; i++; options.no = argv[i]; } else { printf("no complete -ask option arguments\n"); args_ok = 0; } continue; } if ( strcmp(option,"def") == 0) { do_def = 1; if ( (i+1) < argc) { i++; options.def = argv[i]; } else { printf("no complete -def option argument\n"); args_ok = 0; } continue; } opt_length = strlen(option); for(h=0;h arg_start) { if (arg[0] != ' ') { sge_dstring_append(&buffer," "); } } sge_dstring_append(&buffer, sge_infotext_string_input_parsing(&tmp_buf,arg)); } first_arg = i; real_args = 0; for(i=first_arg; i < argc ; i++) { /* printf("args[%d] is: \"%s\"\n",i,argv[i]); */ real_args++; } if (real_args < string_arguments) { printf("infotext: too few arguments\n"); exit(10); } /* if we have to much args add the rest to the string buffer */ while(real_args > string_arguments) { char* arg = argv[first_arg]; char* hcp = NULL; hcp = (char*)sge_dstring_get_string(&buffer); if (strcmp(hcp,"") != 0 ) { if (arg[0] != ' ') { /* Andre */ /* sge_dstring_append(&buffer," "); */ sge_dstring_append(&buffer,""); } } sge_dstring_append(&buffer,sge_infotext_string_input_parsing(&tmp_buf, arg)); first_arg++; real_args--; } DPRINTF(("pass 3\n")); /* 3rd pass - localize format string */ if (do_message == 1) { dstring help_buf = DSTRING_INIT; dstring help_buf2 = DSTRING_INIT; if (strlen(options.D) > 0) { DPRINTF(("strlen(options.D) > 0\n")); printf("#\n# This is a (dash) sign, used for enumerations\n"); printf("msgid \"\"\n%s\n", sge_infotext_make_line_break(&help_buf2,options.D)); if (do_message_space == 0) { DPRINTF(("do_message_space == 0\n")); printf("msgstr \"\"\n\n"); } else { DPRINTF(("do_message_space == 1\n")); sge_infotext_build_test_msgstr(&help_buf, options.D); printf("msgstr %s\n\n", sge_infotext_make_line_break(&help_buf2, (char*)sge_dstring_get_string(&help_buf))); } } if (strlen(options.yes) > 0) { DPRINTF(("strlen(options.yes) > 0\n")); printf("# This is used as shortcut for yes\n"); printf("msgid \"\"\n%s\n", sge_infotext_make_line_break(&help_buf2,options.yes)); if(do_message_space == 0) { DPRINTF(("do_message_space == 0\n")); printf("msgstr \"\"\n\n"); } else { DPRINTF(("do_message_space == 1\n")); sge_infotext_build_test_msgstr(&help_buf, options.yes); printf("msgstr %s\n\n", sge_infotext_make_line_break(&help_buf2, (char*)sge_dstring_get_string(&help_buf))); } } if (strlen(options.no) > 0) { DPRINTF(("strlen(options.no) > 0\n")); printf("# This is used as shortcut for no\n"); printf("msgid \"\"\n%s\n", sge_infotext_make_line_break(&help_buf2,options.no)); if(do_message_space == 0) { DPRINTF(("do_message_space == 0\n")); printf("msgstr \"\"\n\n"); } else { DPRINTF(("do_message_space == 1\n")); sge_infotext_build_test_msgstr(&help_buf, options.no); printf("msgstr %s\n\n", sge_infotext_make_line_break(&help_buf2, (char*)sge_dstring_get_string(&help_buf))); } } if (strlen(options.def) > 0) { DPRINTF(("strlen(options.def) > 0\n")); printf("# This is shortcut default value\n"); printf("msgid \"\"\n%s\n", sge_infotext_make_line_break(&help_buf2,options.def)); if(do_message_space == 0) { DPRINTF(("do_message_space == 0\n")); printf("msgstr \"\"\n\n"); } else { DPRINTF(("do_message_space == 1\n")); sge_infotext_build_test_msgstr(&help_buf, options.def); printf("msgstr %s\n\n", sge_infotext_make_line_break(&help_buf2, (char*)sge_dstring_get_string(&help_buf))); } } DPRINTF(("Here we are\n")); printf("msgid \"\"\n%s\n", sge_infotext_make_line_break(&help_buf2, sge_infotext_string_output_parsing(&tmp_buf, (char*)sge_dstring_get_string(&buffer)))); if(do_message_space == 0) { printf("msgstr \"\"\n"); } else { sge_infotext_build_test_msgstr(&help_buf, sge_infotext_string_output_parsing(&tmp_buf, (char*)sge_dstring_get_string(&buffer))); printf("msgstr %s\n\n", sge_infotext_make_line_break(&help_buf2, (char*)sge_dstring_get_string(&help_buf))); } printf("\n"); sge_dstring_free(&sge_infotext_dash_buffer); sge_dstring_free(&tmp_buf); sge_dstring_free(&help_buf); sge_dstring_free(&help_buf2); exit(0); } help_str = (char*) sge_dstring_get_string(&buffer); sge_dstring_copy_string(&buffer2, (char*)_SGE_GETTEXT__(help_str)); DPRINTF(("format string is: \"%s\"\n",help_str)); DPRINTF(("l10n string is: \"%s\"\n", (char*) sge_dstring_get_string(&buffer2))); /* format output */ /* printf("options: %d %d %d \"%s\" %d \"%s\" \"%s\" \"%s\"\n", options.e, options.n, options.u, options.D, options.S, options.yes , options.no ,options.def); */ /* 4th pass - insert parameters */ DPRINTF(("pass 4\n")); { if (real_args > 0) { #if defined(SOLARISAMD64) || defined(ALPHA) || defined(ALPHA5) || defined(HPUX) || defined(IRIX65) || defined(NETBSD) || __GNUC__ >= 3 for(i=0;i