~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/uti/sge_language.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <string.h>
 
35
#include <fcntl.h>
 
36
#include <unistd.h>
 
37
#include <pthread.h>
 
38
 
 
39
#include "sge_mtutil.h"
 
40
#include "basis_types.h"
 
41
#include "sge_language.h"
 
42
#include "sgermon.h"
 
43
#include "sge_prog.h"
 
44
#include "sge_htable.h"
 
45
 
 
46
#ifdef __SGE_COMPILE_WITH_GETTEXT__ 
 
47
 
 
48
/* MT-NOTE: language_mutex guards all language module function calls */
 
49
static pthread_mutex_t language_mutex = PTHREAD_MUTEX_INITIALIZER;
 
50
 
 
51
#   define LANGUAGE_LOCK()            sge_mutex_lock("language_mutex", SGE_FUNC, __LINE__, &language_mutex)
 
52
#   define LANGUAGE_UNLOCK()          sge_mutex_unlock("language_mutex", SGE_FUNC, __LINE__, &language_mutex)
 
53
 
 
54
/*
 
55
 *  environment variable "SGE_ENABLE_MSG_ID"
 
56
 *  supported values: 0 - off
 
57
 *                    1 - on for error messages
 
58
 *                    2 - on for every message
 
59
 */
 
60
#define SGE_ENABLE_MSG_ID "SGE_ENABLE_MSG_ID"  
 
61
#define PACKAGE "GRIDPACKAGE"
 
62
#define LOCALEDIR "GRIDLOCALEDIR"
 
63
#define SGE_DEFAULT_PACKAGE      "gridengine"
 
64
#define SGE_DEFAULT_LOCALEDIR    "locale"
 
65
 
 
66
 
 
67
/* this is to define the functions for the internationalization
 
68
   tools */
 
69
typedef struct {
 
70
    gettext_func_type          gettext_func        ;
 
71
    setlocale_func_type        setlocale_func      ;
 
72
    bindtextdomain_func_type   bindtextdomain_func ;
 
73
    textdomain_func_type       textdomain_func     ;
 
74
} language_functions_struct;
 
75
 
 
76
typedef struct {
 
77
   int id;                    /* message identification number */
 
78
   int category;              /* message category:             0 = common */
 
79
   u_long32 counter;          /* number of calls */
 
80
   const char* message;       /* message string */
 
81
   const char* local_message;  /* local translated message */
 
82
} sge_error_message_t;
 
83
 
 
84
static pthread_once_t message_id_once = { PTHREAD_ONCE_INIT };
 
85
static void          message_id_once_init(void);
 
86
 
 
87
static pthread_key_t message_id_key;
 
88
static void          message_id_destroy(void* theState);
 
89
 
 
90
static language_functions_struct sge_language_functions;
 
91
static int sge_enable_msg_id = 0;    /* used to enable/disable message ids */
 
92
static int sge_enable_msg_id_to_every_message = 0;
 
93
static htable sge_message_hash_table = NULL; 
 
94
 
 
95
/* this is to find out if the sge_init_language_func() was called */
 
96
static bool sge_are_language_functions_installed = false;
 
97
 
 
98
static int sge_get_message_id_output_implementation(void);
 
99
 
 
100
/****** uti/language/sge_init_language() **************************************
 
101
*  NAME
 
102
*     sge_init_language() -- initialize language package for gettext() 
 
103
*
 
104
*  SYNOPSIS
 
105
*     int sge_init_language(char* package, char* localeDir)
 
106
*       
 
107
*
 
108
*  FUNCTION
 
109
*     starts up the language initialization for gettext(). This 
 
110
*     function should be called nearly after the main() function.
 
111
*  
 
112
*     sge_init_language_func() must be called first to install the 
 
113
*     correct function pointers for gettext() setlocale() etc. etc. 
 
114
*
 
115
*  INPUTS
 
116
*     char* package     -  package name like "gridengine" of binary 
 
117
*                          package *.mo file. 
 
118
*                          (if package is NULL sge_init_language tries 
 
119
*                          to get the package name from the invironment 
 
120
*                          variable "GRIDPACKAGE"). 
 
121
*     char* localeDir  -   path to the localisazion directory 
 
122
*                          (if localeDir is NULL sge_init_language 
 
123
*                          tries to get the localization directory path 
 
124
*                          from the invironment variable 
 
125
*                          "GRIDLOCALEDIR"). 
 
126
*
 
127
*  RESULT
 
128
*     int state         -  true for seccess, false on error.
 
129
*
 
130
*  NOTES
 
131
*     MT-NOTE: sge_init_languagefunc() is guarded by language_mutex
 
132
*     
 
133
*  SEE ALSO
 
134
*     uti/language/sge_init_language()
 
135
*     uti/language/sge_init_language_func()
 
136
*     uti/language/sge_gettext()
 
137
*     uti/language/sge_gettext_()
 
138
*     uti/language/sge_gettext__()
 
139
*     uti/language/sge_get_message_id_output()
 
140
*     uti/language/sge_set_message_id_output()
 
141
******************************************************************************/
 
142
int sge_init_languagefunc(char *package, char *localeDir) 
 
143
{
 
144
  char* packName = NULL;
 
145
  char* locDir   = NULL;
 
146
  char* language = NULL;
 
147
  char* language_var = NULL;
 
148
  char* pathName = NULL;
 
149
  char* sge_enable_msg_id_string = NULL;
 
150
  int   success  = false;  
 
151
  int   back;
 
152
  int   stop = 0;
 
153
 
 
154
  DENTER_(TOP_LAYER, "sge_init_language");
 
155
 
 
156
  LANGUAGE_LOCK();
 
157
 
 
158
  DPRINTF_(("****** starting localization procedure ... **********\n"));
 
159
 
 
160
 
 
161
  while ( stop <= 2 ) {
 
162
 
 
163
     /* free memory for evt. second run */
 
164
     if (packName != NULL) {
 
165
        free(packName);
 
166
        packName = NULL;
 
167
     }
 
168
     if (locDir   != NULL) {
 
169
        free(locDir);
 
170
        locDir = NULL;
 
171
     }
 
172
     if (language != NULL) {
 
173
        free(language);
 
174
        language = NULL;
 
175
     }
 
176
     if (language_var != NULL) {
 
177
        free(language_var);
 
178
        language_var = NULL;
 
179
     }
 
180
 
 
181
     if (pathName != NULL) {
 
182
        free(pathName);
 
183
        pathName = NULL;
 
184
     }
 
185
 
 
186
     /* try to get the package name */
 
187
     if (package != NULL) {
 
188
        packName = strdup(package);
 
189
     } else if (getenv(PACKAGE) != NULL) {
 
190
        packName = strdup(getenv(PACKAGE));
 
191
        DPRINTF_(("try to get language package name from environment "SFQ"\n",PACKAGE));
 
192
     } else {
 
193
        DPRINTF_(("could not get environment variable "SFQ"\n", PACKAGE));
 
194
     }
 
195
   
 
196
     /* no package name given, using default one */
 
197
     if (packName == NULL) {
 
198
        packName = malloc(sizeof(char)*(strlen(SGE_DEFAULT_PACKAGE)+strlen(sge_get_arch()) + 2));
 
199
        sprintf(packName, "%s/%s", sge_get_arch(), SGE_DEFAULT_PACKAGE);
 
200
     }
 
201
      
 
202
     /* try to get the localization directory */ 
 
203
     if (localeDir != NULL) {
 
204
        locDir = strdup(localeDir);
 
205
     } else if ( getenv(LOCALEDIR) != NULL ) {
 
206
        if (stop != 0) {
 
207
           DPRINTF_(("ignoring environment "SFQ"\n",LOCALEDIR ));
 
208
        } else {
 
209
           locDir = strdup(getenv(LOCALEDIR)); /* only the first time */
 
210
           stop++;
 
211
           DPRINTF_(("try to get language package directory path from environment "SFQ"\n",LOCALEDIR));
 
212
        }
 
213
     } else {
 
214
        DPRINTF_(("could not get environment variable "SFQ"\n", LOCALEDIR));
 
215
     }
 
216
   
 
217
     /* no directory given, using default one */
 
218
     if (locDir == NULL) {
 
219
        const char *sge_root = sge_get_root_dir(0, NULL, 0, 0);
 
220
        char* root = NULL;
 
221
        
 
222
        if (sge_root != NULL) {
 
223
            root = strdup(sge_root);
 
224
        } 
 
225
        
 
226
        if ( root == NULL ) {
 
227
            locDir = strdup("/usr/lib/locale");
 
228
        } else {
 
229
            locDir = malloc(sizeof(char)*(strlen(root)+strlen(SGE_DEFAULT_LOCALEDIR) + 100));
 
230
            sprintf(locDir, "%s/%s", root, SGE_DEFAULT_LOCALEDIR);
 
231
        }
 
232
        free(root);
 
233
        root = NULL;
 
234
     }
 
235
   
 
236
     /* try to get a language stylename (only for output of package path) */
 
237
   
 
238
     if (getenv("LANGUAGE") != NULL) {
 
239
        language_var = strdup(getenv("LANGUAGE"));
 
240
     } else {
 
241
        if (getenv("LANG") != NULL) {
 
242
          language_var = strdup(getenv("LANG"));
 
243
        }
 
244
     }
 
245
   
 
246
     if (language_var == NULL) {
 
247
        DPRINTF_(("environment LANGUAGE or LANG is not set; no language selected - using defaults\n"));
 
248
        language_var = strdup("C");
 
249
     } 
 
250
     
 
251
     /* get correct language value */
 
252
     if ( sge_language_functions.setlocale_func != NULL ) {
 
253
        char* help1 = NULL;
 
254
        help1 = sge_language_functions.setlocale_func(LC_MESSAGES, "");
 
255
        if (help1 != NULL) {
 
256
           char* slash_pos = NULL;
 
257
           DPRINTF_(("setlocale() returns \"%s\"\n",help1)); 
 
258
           slash_pos = strstr(help1,"_");
 
259
           if (slash_pos != NULL) {
 
260
              char* tmp_lang = NULL;
 
261
              DPRINTF_(("cutting of language string after \"_\":\n"));
 
262
              tmp_lang = strdup(help1);
 
263
              slash_pos = strstr(tmp_lang,"_");
 
264
              *slash_pos = 0;  /* cut off "_" */
 
265
              language = strdup(tmp_lang);
 
266
 
 
267
              free(tmp_lang);
 
268
 
 
269
           } else {
 
270
              language = strdup(help1);
 
271
           }
 
272
        } else {
 
273
           DPRINTF_(("setlocale() returns NULL"));   
 
274
           language = strdup(language_var);
 
275
        }
 
276
     }
 
277
 
 
278
     /* packName, locDir and language strings are now surely not NULL,
 
279
        so we can now try to setup the choosen language package (*.mo - file) */
 
280
     pathName = malloc(sizeof(char)*(strlen(locDir)+strlen(language)+strlen(packName)+100));
 
281
     sprintf(pathName,"%s/%s/LC_MESSAGES/%s.mo",locDir,language,packName);
 
282
     DPRINTF_(("locale directory: >%s<\n",locDir));
 
283
     DPRINTF_(("package file:     >%s.mo<\n",packName));
 
284
     DPRINTF_(("language (LANG):  >%s<\n",language));
 
285
     DPRINTF_(("loading message file: %s\n",pathName ));
 
286
   
 
287
     /* is the package file allright */
 
288
     back = open (pathName, O_RDONLY);
 
289
     if (back >= 0) {
 
290
        DPRINTF_(("found message file - ok\n"));
 
291
        success = true;
 
292
        close(back);
 
293
        stop = 2;
 
294
     } else {
 
295
        DPRINTF_(("could not open message file - error\n"));
 
296
        success = false;
 
297
     }
 
298
     if(stop==0) {
 
299
        stop = 2;   /* only when stop = 1: Try to repeat without LOCALEDIR environment variable */
 
300
     } 
 
301
     stop++;
 
302
  }  
 
303
 
 
304
 
 
305
  /* now startup the language package for gettext */
 
306
/*   setlocale(LC_ALL, ""); */
 
307
 
 
308
  if ( (sge_language_functions.setlocale_func != NULL     ) &&
 
309
       (sge_language_functions.bindtextdomain_func != NULL) &&
 
310
       (sge_language_functions.textdomain_func != NULL    ) &&
 
311
       (sge_are_language_functions_installed == true      )    ) {
 
312
     char* help1 = NULL;
 
313
     help1 = sge_language_functions.setlocale_func(LC_MESSAGES, "");
 
314
     if (help1 != NULL) {
 
315
        DPRINTF_(("setlocale() returns \"%s\"\n",help1)); 
 
316
     } else {
 
317
        DPRINTF_(("setlocale() returns NULL\n"));
 
318
     }
 
319
     help1 = sge_language_functions.bindtextdomain_func(packName, locDir );
 
320
     if (help1 != NULL) {
 
321
        DPRINTF_(("bindtextdomain() returns \"%s\"\n",help1));
 
322
     }else {
 
323
        DPRINTF_(("bindtextdomain() returns NULL\n"));
 
324
     }
 
325
     help1 = sge_language_functions.textdomain_func(packName);
 
326
     if (help1 != NULL) {
 
327
        DPRINTF_(("textdomain() returns \"%s\"\n",help1));
 
328
     }else {
 
329
        DPRINTF_(("textdomain() returns NULL\n"));
 
330
     }
 
331
  } else {
 
332
     DPRINTF_(("sge_init_language() called without valid sge_language_functions pointer!\n"));
 
333
     success = false;
 
334
  }
 
335
 
 
336
  free(packName);
 
337
  free(locDir);
 
338
  free(language);
 
339
  free(language_var);
 
340
  free(pathName);
 
341
  packName = NULL;
 
342
  locDir   = NULL;
 
343
  language = NULL; 
 
344
  language_var = NULL;
 
345
  pathName = NULL;
 
346
 
 
347
  if (success == true) {
 
348
     sge_enable_msg_id = 1;
 
349
  } 
 
350
 
 
351
  sge_enable_msg_id_string = getenv(SGE_ENABLE_MSG_ID);
 
352
  if (sge_enable_msg_id_string != NULL) {
 
353
     int env_value = 0;
 
354
     DPRINTF_(("SGE_ENABLE_MSG_ID is set to \"%s\"\n", sge_enable_msg_id_string));  
 
355
     env_value = atoi(sge_enable_msg_id_string);
 
356
     if (env_value == 0) {
 
357
         sge_enable_msg_id = 0;
 
358
     } else {
 
359
         sge_enable_msg_id = 1;
 
360
     }
 
361
     if (env_value == 2) {
 
362
        sge_enable_msg_id_to_every_message = 1;
 
363
     }
 
364
  }
 
365
 
 
366
 
 
367
  if (sge_enable_msg_id == 0) {
 
368
     DPRINTF_(("error id output     : disabled\n"));
 
369
  } else {
 
370
     DPRINTF_(("error id output     : enabled\n"));
 
371
  }
 
372
 
 
373
  if (success == true) {
 
374
    DPRINTF_(("****** starting localization procedure ... success **\n"));
 
375
  } else {
 
376
    DPRINTF_(("****** starting localization procedure ... failed  **\n"));
 
377
  }
 
378
 
 
379
  LANGUAGE_UNLOCK();
 
380
 
 
381
  DRETURN_(success);
 
382
}
 
383
 
 
384
/****** uti/language/sge_init_language_func() **********************************
 
385
*  NAME
 
386
*     sge_init_language() -- install language functions
 
387
*
 
388
*  SYNOPSIS
 
389
*     void sge_init_language_func(gettext_func_type new_gettext, 
 
390
*                         setlocale_func_type new_setlocale, 
 
391
*                         bindtextdomain_func_type new_bindtextdomain, 
 
392
*                         textdomain_func_type new_textdomain);
 
393
*
 
394
*  FUNCTION
 
395
*     set the function pointer for the gettext(), setlocale(), 
 
396
*     bindtextdomain() and textdomain() function calls. This function 
 
397
*     must called before any call to sge_init_language() and 
 
398
*     sge_gettext().  
 
399
*
 
400
*  INPUTS
 
401
*     gettext_func_type        - pointer for gettext()
 
402
*     setlocale_func_type      - pointer for setlocale()
 
403
*     bindtextdomain_func_type - pointer for bindtextdomain()
 
404
*     textdomain_func_type     - pointer for textdomain()
 
405
*
 
406
*  NOTES
 
407
*     MT-NOTE: sge_init_language_func() is guarded by language_mutex
 
408
*     
 
409
*  SEE ALSO
 
410
*     uti/language/sge_init_language()
 
411
*     uti/language/sge_init_language_func()
 
412
*     uti/language/sge_gettext()
 
413
*     uti/language/sge_gettext_()
 
414
*     uti/language/sge_gettext__()
 
415
*     uti/language/sge_get_message_id_output()
 
416
*     uti/language/sge_set_message_id_output()
 
417
******************************************************************************/
 
418
void sge_init_language_func(gettext_func_type new_gettext, 
 
419
                            setlocale_func_type new_setlocale,
 
420
                            bindtextdomain_func_type new_bindtextdomain,
 
421
                            textdomain_func_type new_textdomain) 
 
422
{
 
423
   DENTER_(TOP_LAYER, "sge_init_language_func");
 
424
 
 
425
   LANGUAGE_LOCK();
 
426
 
 
427
   /* initialize the functions pointer to NULL */
 
428
   sge_language_functions.gettext_func = NULL;
 
429
   sge_language_functions.setlocale_func = NULL;
 
430
   sge_language_functions.bindtextdomain_func = NULL;
 
431
   sge_language_functions.textdomain_func = NULL;
 
432
 
 
433
   /* ok, the functions have now NULL pointer */
 
434
   sge_are_language_functions_installed = true; 
 
435
 
 
436
   /* set the new functions */
 
437
   if (new_gettext != NULL) {
 
438
      sge_language_functions.gettext_func = new_gettext;
 
439
   }
 
440
 
 
441
   if (new_setlocale != NULL) {
 
442
      sge_language_functions.setlocale_func = new_setlocale;
 
443
   }
 
444
 
 
445
   if (new_bindtextdomain != NULL) {
 
446
      sge_language_functions.bindtextdomain_func = new_bindtextdomain;
 
447
   }
 
448
 
 
449
   if (new_textdomain != NULL) {
 
450
      sge_language_functions.textdomain_func = new_textdomain;
 
451
   } 
 
452
 
 
453
   LANGUAGE_UNLOCK();
 
454
 
 
455
   DRETURN_VOID_;
 
456
}
 
457
 
 
458
/****** uti/language/sge_set_message_id_output() *******************************
 
459
*  NAME
 
460
*     sge_set_message_id_output() -- enable message id number adding
 
461
*
 
462
*  SYNOPSIS
 
463
*     void sge_set_message_id_output(int flag) 
 
464
*
 
465
*  FUNCTION
 
466
*     This procedure is used to enable the adding of message id's when 
 
467
*     showing error messages. This function is used in the macro 
 
468
*     SGE_ADD_MSG_ID(x) to enable the message id for errors.
 
469
*
 
470
*  INPUTS
 
471
*     int flag - 0 = off ; 1 = on
 
472
*
 
473
*  NOTES
 
474
*     MT-NOTE: sge_set_message_id_output() is guarded by language_mutex
 
475
*     
 
476
*  SEE ALSO
 
477
*     uti/language/sge_init_language()
 
478
*     uti/language/sge_init_language_func()
 
479
*     uti/language/sge_gettext()
 
480
*     uti/language/sge_gettext_()
 
481
*     uti/language/sge_gettext__()
 
482
*     uti/language/sge_get_message_id_output()
 
483
*     uti/language/sge_set_message_id_output()
 
484
*******************************************************************************/
 
485
void sge_set_message_id_output(int flag) {
 
486
   int *buf = NULL;
 
487
 
 
488
   DENTER_(CULL_LAYER, "sge_set_message_id_output");
 
489
 
 
490
   pthread_once(&message_id_once, message_id_once_init);
 
491
   
 
492
   buf = (int*) pthread_getspecific(message_id_key);
 
493
 
 
494
   if (buf != NULL) {
 
495
      *buf = flag;
 
496
   }
 
497
 
 
498
   DRETURN_VOID_;
 
499
}
 
500
 
 
501
/****** uti/language/sge_get_message_id_output() *******************************
 
502
*  NAME
 
503
*     sge_get_message_id_output() -- check if message id should be added
 
504
*
 
505
*  SYNOPSIS
 
506
*     int sge_get_message_id_output(void) 
 
507
*
 
508
*  FUNCTION
 
509
*     This function returns the value stored in the static global 
 
510
*     variable sge_message_id_view_flag.
 
511
*
 
512
*  RESULT
 
513
*     int - value of sge_message_id_view_flag
 
514
*
 
515
*  NOTES
 
516
*     MT-NOTE: sge_get_message_id_output() is guarded by language_mutex
 
517
*     
 
518
*  SEE ALSO
 
519
*     uti/language/sge_init_language()
 
520
*     uti/language/sge_init_language_func()
 
521
*     uti/language/sge_gettext()
 
522
*     uti/language/sge_gettext_()
 
523
*     uti/language/sge_gettext__()
 
524
*     uti/language/sge_get_message_id_output()
 
525
*     uti/language/sge_set_message_id_output()
 
526
*     uti/language/sge_get_message_id_output_implementation()
 
527
*******************************************************************************/
 
528
int sge_get_message_id_output(void) 
 
529
{
 
530
   int ret;
 
531
 
 
532
   DENTER_(TOP_LAYER, "sge_get_message_id_output");
 
533
 
 
534
   LANGUAGE_LOCK();
 
535
   ret = sge_get_message_id_output_implementation();
 
536
   LANGUAGE_UNLOCK();
 
537
 
 
538
   DRETURN_(ret);
 
539
}
 
540
 
 
541
/****** uti/language/sge_get_message_id_output_implementation() ***************
 
542
*  NAME
 
543
*     sge_get_message_id_output_implementation() -- pure implementation of 
 
544
*        sge_get_message_id_output_implementation() that does not lock
 
545
*        language_mutex
 
546
*
 
547
*  SYNOPSIS
 
548
*     int sge_get_message_id_output_implementation(void) 
 
549
*
 
550
*  FUNCTION
 
551
*     When the sge_get_message_id_output() functionality is needed from within
 
552
*     the language modules the language_mutex may not be obtained because this 
 
553
*     mutex is already owned by the same thread. 
 
554
*
 
555
*  RESULT
 
556
*     int - value of sge_message_id_view_flag
 
557
*
 
558
*  NOTES
 
559
*     MT-NOTE: The caller of sge_get_message_id_output_implementation() must
 
560
*     MT-NOTE: have obtained the language_mutex mutex due to access to 
 
561
*     MT-NOTE: 'sge_enable_msg_id' and 'sge_enable_msg_id_to_every_message'.
 
562
*     
 
563
*  SEE ALSO
 
564
*     uti/language/sge_init_language()
 
565
*     uti/language/sge_init_language_func()
 
566
*     uti/language/sge_gettext()
 
567
*     uti/language/sge_gettext_()
 
568
*     uti/language/sge_gettext__()
 
569
*     uti/language/sge_get_message_id_output()
 
570
*     uti/language/sge_set_message_id_output()
 
571
*******************************************************************************/
 
572
static int sge_get_message_id_output_implementation(void) 
 
573
{
 
574
   int *buf;
 
575
   DENTER_(CULL_LAYER, "sge_get_message_id_output_implementation");
 
576
 
 
577
   if (sge_enable_msg_id_to_every_message == 1) {
 
578
      DRETURN_(1);
 
579
   }
 
580
 
 
581
   if (sge_enable_msg_id == 0) {
 
582
      DRETURN_(0);
 
583
   }
 
584
 
 
585
   pthread_once(&message_id_once, message_id_once_init);
 
586
 
 
587
   buf = (int*) pthread_getspecific(message_id_key);
 
588
 
 
589
   if (buf != NULL) { 
 
590
      DRETURN_(0);
 
591
   } else {
 
592
      DRETURN_(*buf);
 
593
   }
 
594
}
 
595
 
 
596
/****** uti/language/sge_gettext() *********************************************
 
597
*  NAME
 
598
*     sge_gettext() -- dummy gettext() function 
 
599
*
 
600
*  SYNOPSIS
 
601
*     const char* sge_gettext(char *x) 
 
602
*
 
603
*  FUNCTION
 
604
*     This function returns the given argument
 
605
*
 
606
*  INPUTS
 
607
*     char *x - string
 
608
*
 
609
*  RESULT
 
610
*     const char* - input string
 
611
*
 
612
*  SEE ALSO
 
613
*     uti/language/sge_init_language()
 
614
*     uti/language/sge_init_language_func()
 
615
*     uti/language/sge_gettext()
 
616
*     uti/language/sge_gettext_()
 
617
*     uti/language/sge_gettext__()
 
618
*     uti/language/sge_get_message_id_output()
 
619
*     uti/language/sge_set_message_id_output()
 
620
*******************************************************************************/
 
621
const char *sge_gettext(char *x) {
 
622
   return x;
 
623
}
 
624
 
 
625
/****** uti/language/sge_gettext_() ********************************************
 
626
*  NAME
 
627
*     sge_gettext_() -- add error id to message
 
628
*
 
629
*  SYNOPSIS
 
630
*     const char* sge_gettext_(int msg_id, const char *msg_str) 
 
631
*
 
632
*  FUNCTION
 
633
*     This function is used for adding the message id to the translated 
 
634
*     gettext message string. The message id is only added when the 
 
635
*     function sge_get_message_id_output() returns not "0" and the 
 
636
*     message string contains at least one SPACE character.
 
637
*
 
638
*  INPUTS
 
639
*     int msg_id          - message id
 
640
*     const char *msg_str - message to translate
 
641
*
 
642
*  RESULT
 
643
*     const char* - translated (L10N) message with message id 
 
644
*
 
645
*  SEE ALSO
 
646
*     uti/language/sge_init_language()
 
647
*     uti/language/sge_init_language_func()
 
648
*     uti/language/sge_gettext()
 
649
*     uti/language/sge_gettext_()
 
650
*     uti/language/sge_gettext__()
 
651
*     uti/language/sge_get_message_id_output()
 
652
*     uti/language/sge_set_message_id_output()
 
653
*******************************************************************************/
 
654
const char *sge_gettext_(int msg_id, const char *msg_str) 
 
655
{
 
656
   /* extern */
 
657
#ifndef __SGE_COMPILE_WITH_GETTEXT__
 
658
   return msg_str;
 
659
#else
 
660
   union {
 
661
      sge_error_message_t *l;
 
662
      void *p;
 
663
   } message_p;
 
664
   long key;
 
665
 
 
666
   DENTER_(CULL_LAYER, "sge_gettext_");
 
667
 
 
668
   message_p.l = NULL;
 
669
 
 
670
   if (msg_str == NULL) {
 
671
      DRETURN_(NULL);
 
672
   } 
 
673
 
 
674
   key = msg_id;
 
675
   /* check if message is not just one word (strstr) */
 
676
   if ( (sge_get_message_id_output_implementation() != 0)  && 
 
677
        (strstr(msg_str, " ") != NULL)   ) {  
 
678
       
 
679
      if (sge_message_hash_table == NULL) {
 
680
         sge_message_hash_table = sge_htable_create(8,   /* 2^8 = 256 table start size */
 
681
                                                    dup_func_long, 
 
682
                                                    hash_func_long, 
 
683
                                                    hash_compare_long);
 
684
      }
 
685
      if (sge_htable_lookup(sge_message_hash_table, &key, (const void**)&message_p.p) == False) {
 
686
         /* add element */ 
 
687
         sge_error_message_t* new_mp = NULL;
 
688
         char* org_message = NULL;
 
689
         char* trans_message = NULL;
 
690
         const char* gettext_return_string = NULL;
 
691
 
 
692
         
 
693
         gettext_return_string = sge_gettext__((char*)msg_str);
 
694
 
 
695
         org_message   = malloc(strlen(msg_str)+1);
 
696
         trans_message = malloc(strlen(gettext_return_string)+1+8); /* max "(99999) "*/
 
697
         new_mp        = malloc(sizeof(sge_error_message_t));
 
698
         if (new_mp != NULL && org_message != NULL && trans_message != NULL) {
 
699
            DPRINTF_(("add new hash table entry for message id: %d\n",msg_id));
 
700
            new_mp->id = msg_id;
 
701
            new_mp->category = 0;
 
702
            new_mp->counter = 1;
 
703
            strcpy(org_message, msg_str);
 
704
            new_mp->message = org_message;
 
705
            if (msg_id <= 99999) {
 
706
               sprintf( trans_message , "[%d] %s", msg_id, gettext_return_string);
 
707
            } else {
 
708
               sprintf( trans_message , "%s", gettext_return_string);
 
709
            }
 
710
            new_mp->local_message = trans_message;
 
711
 
 
712
            sge_htable_store(sge_message_hash_table, &key, new_mp);
 
713
            DRETURN_(new_mp->local_message);
 
714
         }
 
715
      } else {
 
716
         /* check element */
 
717
         DPRINTF_(("using old hash entry for message id: %d\n",msg_id));
 
718
         if (strcmp(msg_str,message_p.l->message) != 0) {
 
719
            DPRINTF_(("duplicate message id error: returning gettext() message"));
 
720
            DPRINTF_(("msg in : \"%s\"\n",msg_str));
 
721
            DPRINTF_(("msg out: \"%s\"\n",message_p.l->message));
 
722
            DRETURN_(sge_gettext__((char*)msg_str));
 
723
         } else {
 
724
            message_p.l->counter = (message_p.l->counter) + 1;
 
725
            DPRINTF_(("message count: "sge_U32CFormat"\n", sge_u32c(message_p.l->counter)));
 
726
            DRETURN_(message_p.l->local_message);
 
727
         }
 
728
      } 
 
729
   }
 
730
   DRETURN_(sge_gettext__((char*)msg_str));
 
731
#endif
 
732
}
 
733
 
 
734
/****** uti/language/sge_gettext__() *******************************************
 
735
*  NAME
 
736
*     sge_gettext__() -- get translated message from message file
 
737
*
 
738
*  SYNOPSIS
 
739
*     char *sge_gettext__(char *x)
 
740
*
 
741
*  FUNCTION
 
742
*     makes a call to sge_language_functions.gettext_func(x) if 
 
743
*     gettext_func is not NULL, otherwise it returns the input
 
744
*     string. 
 
745
*
 
746
*  INPUTS
 
747
*     char *x - pointer to message which should be internationalizied    
 
748
*
 
749
*  RESULT
 
750
*     char*   - pointer internationalized message
 
751
*
 
752
*  NOTE
 
753
*     MT-NOTE: not guarded b/c sge_gettext__() is used only in infotext utility
 
754
 
755
*  SEE ALSO
 
756
*     uti/language/sge_init_language()
 
757
*     uti/language/sge_init_language_func()
 
758
*     uti/language/sge_gettext()
 
759
*     uti/language/sge_gettext_()
 
760
*     uti/language/sge_gettext__()
 
761
*     uti/language/sge_get_message_id_output()
 
762
*     uti/language/sge_set_message_id_output()
 
763
*******************************************************************************/
 
764
const char *sge_gettext__(char *x) 
 
765
{
 
766
   char *z;
 
767
   DENTER_(BASIS_LAYER, "sge_gettext__");
 
768
 
 
769
   if ( (sge_language_functions.gettext_func != NULL) && 
 
770
        (sge_are_language_functions_installed == true)   ) {
 
771
      z = sge_language_functions.gettext_func(x);
 
772
   } else {
 
773
      z = x;
 
774
      DPRINTF_(("sge_gettext() called without valid gettext function pointer!\n"));
 
775
   }
 
776
 
 
777
   /*z = gettext(x);*/
 
778
  
 
779
   /*DPRINTF_(("gettext: '%s' -> '%s'\n", x ? x : "", z ? z : ""));*/ 
 
780
 
 
781
   DRETURN_(z);
 
782
}
 
783
 
 
784
static void message_id_destroy(void* theState) 
 
785
{
 
786
   if (theState != NULL) {
 
787
      free(theState);
 
788
   }
 
789
}
 
790
 
 
791
static void message_id_once_init(void)
 
792
{
 
793
  int *buf;
 
794
  int res;
 
795
  pthread_key_create(&message_id_key, &message_id_destroy);
 
796
 
 
797
  buf = (int*) sge_malloc(sizeof(int));
 
798
  *buf = 0;
 
799
 
 
800
  res = pthread_setspecific(message_id_key, (const void*)buf);
 
801
 
 
802
  if (0 != res) {
 
803
     fprintf(stderr, "pthread_set_specific(%s) failed: %s\n", "log_buffer_getspecific", strerror(res));
 
804
     abort();
 
805
  }
 
806
}
 
807
#endif