File: rdRes.c

package info (click to toggle)
mtink 1.0.16-17
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,932 kB
  • sloc: ansic: 19,264; sh: 1,008; python: 626; xml: 444; makefile: 84
file content (695 lines) | stat: -rw-r--r-- 19,208 bytes parent folder | download | duplicates (4)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/* file rdRes.c
 *
 *  Read a resource file and handle the stuffs for internationalized
 *  printting.
 *  if the wanted text is not available in the internationalized
 *  version return the default (english).
 *
 *  Copyrights: Jean-Jacques Sarton  j.sarton@t-online.de
 */

/*
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <ctype.h>

#include "tres.c"

static char *msgOut     = NULL;
static char **resources = NULL;

/*******************************************************************/
/* Function mfgets()                                               */
/*                                                                 */
/* replacement for fgets, allow simple editing                     */
/*                                                                 */
/*******************************************************************/

static char *mfgets(char *buffer, int size, FILE *fp)
{
   int c;
   int i = 0;

   buffer[i] = 0;
   while( i < size-1 )
   {
      c = getc(fp);
      if ( c == '\b' )
      {
         if ( i )
         {
            i--;
            buffer[i] = 0;
            printf("%c %c",c,c);
         }
      }
      else
      {
         if ( c == '\r' || c == '\n' )
         {
            if ( c == '\r' )
               printf("\r\n");
            buffer[i++] = '\n';
            buffer[i]   = 0;
            break;
         }
         printf("%c",c);
         buffer[i++] = c;
         buffer[i]   = 0;
      }
   }
   
   return buffer;
}

/*******************************************************************/
/* Function freeArray                                              */
/*                                                                 */
/* free all memory allocated to an array of strings including      */
/* the array pointer                                               */
/*                                                                 */
/*******************************************************************/

static void freeArray(char **array, int no)
{
   int i;
   if ( array )
   {
      for ( i = 0; i < no; i++ )
      {
         if ( array[i] ) free(array[i]);
      }
      free(array);
   }
}

/*******************************************************************/
/* Function addFallback                                            */
/*                                                                 */
/* add the fallback resources to the resources read from file      */
/*                                                                 */
/*******************************************************************/

static void addFallback(int entries)
{
   int actEntries=0;
   int newEntries=0;

   if ( resources )
   {
      /* add the fallback resources to the array */
      while(resources[actEntries]) actEntries++;
      while(fallbackResources[newEntries]) newEntries++;
      resources = (char**)realloc(resources, sizeof(char**)*(actEntries + newEntries + 1));
      if ( ! resources )
      {
         resources = fallbackResources;
      }
      newEntries = 0;
      while(fallbackResources[newEntries])
         resources[actEntries++] = fallbackResources[newEntries++];
      resources[actEntries] = NULL;
      
   }
   else
   {
      resources = fallbackResources;
   }
}

/*******************************************************************/
/* Function skipSpace                                              */
/*                                                                 */
/* advance up to the first non space character and return a        */
/* pointer to this position                                        */
/*                                                                 */
/*******************************************************************/

static char *skipSpace(char *s)
{
   while ( *s && isspace(*s) )
      s++;
   return s;
}

/*******************************************************************/
/* Function terminateLine                                          */
/*                                                                 */
/* remove the trailing \n an return the line lenght                */
/*                                                                 */
/*******************************************************************/

static int terminateLine(char *s)
{
   int len = 0;
   while ( *s )
   {
      if ( *s == '\n' )
      {
         *s = '\0';
         break;
      }
      s++;
      len++;
   }
   return len;
}

/*******************************************************************/
/* Function readResourceFile                                       */
/*                                                                 */
/* read the external resource file and build our storage area      */
/*                                                                 */
/*******************************************************************/

static int readResourceFile(FILE *fp)
{
   char  buf[2048];
   char *s;
   int   len;
   int   oldLen  = 0;
   int   entries = 0;
   while(fgets(buf,sizeof(buf), fp))
   {
      len = terminateLine(buf);
      if ( entries && resources[entries-1][oldLen-1] != '\\' )
      {
         s = skipSpace(buf);
         /* correct the line lenght */
         len -= (s-buf);
         if ( *s == '!' || *s == '\0' )
            continue;
      }
      else
      {
         s = buf;
      }
   
      if ( *s == '\0' )
         continue;
      if ( resources == NULL )
      {
         resources = (char **)malloc(sizeof(char*)*(entries+2));
         if ( resources == NULL )
         {
            entries = 0;
            break;
         }
         resources[1] = NULL;
      }
      else if ( resources[entries-1][oldLen-1] != '\\' )
      {
         resources = (char **)realloc(resources, sizeof(char*)*(entries+2));
         if ( resources == NULL )
         {
            entries = 0;
            break;
         }
         resources[entries+1] = NULL;
      }

      
      if ( entries && resources[entries-1][oldLen-1] == '\\' )
      {
         /* line continuation */
         resources[entries-1] = (char*)realloc(resources[entries-1], len+oldLen+1);
         if ( resources[entries-1] == NULL )
         {
            freeArray(resources, entries);
            resources = NULL;
            entries = 0;
            break;
         }
         strcpy(&resources[entries-1][oldLen-1], s);
         oldLen += len -1;
      }
      else
      {
         resources[entries] = strdup(s);
         if ( resources[entries] == NULL )
         {
            freeArray(resources, entries);
            resources = NULL;
            entries = 0;
            break;
         }
         oldLen = len;
         entries++;
      }
   }
   return entries;
}

/*******************************************************************/
/* Function checkRessourceString                                   */
/*                                                                 */
/* compare the key with the resource string, return NULL or the    */
/* message string found                                            */
/*                                                                 */
/*******************************************************************/

static char *checkRessourceString(char *res, char *key)
{
   char *msg = NULL;
   int len = strlen(key);
   if ( strncmp(res,key,len) == 0 )
   {
      if ( res[len] == ':' )
      {
         res += len;
         res++;
         while ( *res && *res == ' ' ) res++;
         msg = res;
      }
   }
   return msg;
}

/*******************************************************************/
/* Function _searchString                                          */
/*                                                                 */
/* search a string in our resource array and return it             */
/* if localized string is not found return the default string      */
/* if nothing is found return NULL                                 */
/* convert resource strings as made by Xt                          */
/*******************************************************************/

char *_searchString(char *lang, char *key)
{
   char **msg = resources;
   char *defaultMsg   = NULL;
   char *localizedMsg = NULL;
   char  *s;
   int utf;

   while( *msg )
   {
      /* go to the first . or * character */
      s = *msg;
      while ( *s && ( *s != '.' && *s != '*' ) ) s++;
      if ( *s ) s++;
      if ( *s && lang )
      {
         if ( strstr(lang,"8" ) )
            utf = 1;
         else
            utf = 0;
         if ( !utf && s[0] == lang[0] &&  s[1] == lang[1] &&
              (s[2] == '.' || s[2] == '*')
            )
         {
            /* may be that we have found the wanted string */
            
            if ( (localizedMsg = checkRessourceString(s+3, key)) )
            {
               return localizedMsg;
            }
            
         }
         else if ( utf && s[0] == lang[0] &&  s[1] == lang[1] && s[2] == lang[2]  &&
              (s[3] == '.' || s[3] == '*')
            )
         {
            /* may be that we have found the wanted string */
            
            if ( (localizedMsg = checkRessourceString(s+4, key)) )
            {
               return localizedMsg;
            }
            
         }
         else
         {
            /* check for the default string */
            if ( ! defaultMsg )
               defaultMsg = checkRessourceString(s, key);
         }
      }
      msg++;
   }
   return defaultMsg;
}

/*******************************************************************/
/* Function searchString                                           */
/*                                                                 */
/* search a string in our resource array and return it after       */
/* converting as made by Xt                                        */
/*                                                                 */
/*******************************************************************/

char *searchString(char *lang, char *key)
{
   char *s, *t;
   char *msg = _searchString(lang, key);
   if ( msgOut )
   {
      free(msgOut);
      msgOut = NULL;
   }

   if ( msg == NULL || *msg == '\0' )
   {
      ;
   }
   else
   {
      /* replace '\*' with the correct value */
      s = msg;
      t = msgOut = (char*)calloc(strlen(s)+1,1);
      while ( *s )
      {
         switch(*s)
         {
            case '\\':
               s++;
               if ( *s == 'n' )
               {
                  *t++ = '\n';
                  s++;
               }
               else
               {
                  *t++ = *s++;
               }
            break;
            default:
               *t++ = *s++;
         }
      }
   }
   return msgOut;
}

/*******************************************************************/
/* Function printText                                              */
/*                                                                 */
/* search the string referenced by lang and key, then print it     */
/* according to the passed format arguments                        */
/*                                                                 */
/*******************************************************************/

void printText(char *fmt, char *lang, char *key)
{
   char *msg;
   msg = searchString(lang, key);
   if ( msg )
      printf(fmt, msg);
   else
      printf("\nMsg <%s> NOT FOUND !\n", key);
}

/*******************************************************************/
/* Function fprintText                                             */
/*                                                                 */
/* search the string referenced by lang and key, then print it     */
/* according to the passed format arguments                        */
/*                                                                 */
/*******************************************************************/

void fprintText(FILE *out, char *fmt, char *lang, char *key)
{
   char *msg;

   msg = searchString(lang, key);
   if ( msg )
      fprintf(out,fmt, msg);
   else
      fprintf(out,"\nMsg <%s> NOT FOUND !\n", key);
}

#define fgets mfgets

/*******************************************************************/
/* Function askYn                                                  */
/*                                                                 */
/* align head stuff, ask for yes/no or similar things              */
/* only the first choice is checked. Other inputs are              */
/* defaulted to False                                              */
/*                                                                 */
/*******************************************************************/

int askYn(char *lang, char *yn)
{
   char  in[1024];
   char *answers = searchString(lang, yn);
   char *s;

   if ( answers && *answers )
   {
      fgets(in, sizeof(in),stdin);
      s = in;
      while ( *s && isspace(*s) ) s++;
      if ( toupper(*s) == toupper(*answers) )
      {
         return 1;
      }
   }
   return 0;
}

/*******************************************************************/
/* Function askNo                                                  */
/*                                                                 */
/* align head stuff, ask for pattern no in the range first to last */
/*                                                                 */
/*******************************************************************/

int askNo(int first, int last)
{
   int number = 0;
   char  in[20];

   do
   {
      printf("%d ... %d : ", first, last);
      fgets(in,sizeof(in), stdin);
      number = atoi(in);
   } while ( !(number >= first && number <= last) );

   return number;
}

/*******************************************************************/
/* Function askForChoice                                           */
/*                                                                 */
/* Print a list of choice ans a number from 1....no                */
/* then call askNo and return the the result -1 ( 0...no-1 )       */
/*                                                                 */
/*                                                                 */
/*******************************************************************/

int askForChoice(char *lang, char *choice[], int no)
{
   int    c = 0;
   int    i;
   char **array;
   char  *s;
   array = (char**)calloc(no+1, sizeof(char*));

   for ( i = 0; i < no; i++ )
   {
      if ( choice[i] == NULL )
      {

         freeArray(array, i);
         return -1;
      }
      else
      {
         s = searchString(lang, choice[i]);
         if ( s == NULL )
            s = choice[i];
         array[i] = strdup(s);
         c = strlen(array[i]) > c ? strlen(array[i]) : c;
      }
   }
   
   /* print choice textes */
   for ( i = 0; i < no; i++ )
   {
      printf("- %*s (%2d)\n", c, array[i], i+1);
   }
   i = askNo(1,no);
   return i-1;
}

/*******************************************************************/
/* Function initResource                                           */
/*                                                                 */
/* Read textes from an external file                               */
/*                                                                 */
/*******************************************************************/

int initResource()
{
   int   i;
   int   entries;
   FILE *fp;
   char  homeFile[1024+8] = { '\0', };
   char *homePath = (char*)getenv("HOME");
   char *path[] =
   {
      homeFile,
      "/usr/lib/ttink/Ttink",
      "/usr/local/lib/ttink/Ttink",
      "/opt/mtink/Ttink",
      "./Ttink"
   };
   
   if ( homePath && strlen(homePath) < 1024 )
   {
      strcpy(homeFile,homePath);
      strcat(homeFile,"/Ttink");
   }

   for ( i = 0; i < 5; i++ )
   {
      if ( path[i] && (fp = fopen(path[i],"r")) )
      {
         /* read the file */
         entries = readResourceFile(fp);
         fclose(fp);
         addFallback(entries);
         return 1;
      }
   }
   addFallback(0);
   return 0;
}

#if RESCHECK
int searchCmp(char *key, char **res)
{
   char **cmp = res;
   char  *s;
   char  *e;
   while (*res)
   {
      s = *res;
      if ( *s == '.' || *s == '*' )
      {
         e = strchr(s, ':');
         if ( e )
         {
            if ( s[3] == '.' )
            {
               if ( strncmp(key,s+4,e-s-4) == 0)
               {
                  return 1;
               }
            }
            else
            {
               if ( strncmp(key,s+1,e-s-1) == 0)
               {
                  return 1;
               }
            }
         }
      }
      res++;
   }
   return 0;
}

void checkForMissing(char **refRes, char **cmpRes)
{
   char **ref = refRes;
   char   lang[20];
   char   key[100];
   char  *s;
   char  *e;
   while (*ref)
   {
      s = *ref;
      if ( *s == '.' || *s == '*' )
      {
         e = strchr(s, ':');
         if ( e )
         {
            if ( s[3] == '.' )
            {
               strncpy(key,s+4,e-s-3);
               key[e-s-3] = '\0';
            }
            else
            {
               strncpy(key,s+1,e-s);
               key[e-s] = '\0';
            }
            if ( searchCmp(key,cmpRes ) == 0 )
            {
               printf(" <.%s>\n",key);
            }
         }
      }
      ref++;
   }
}


int main(int argc, char **argv)
{
   FILE  *refFp = NULL;
   FILE  *cmpFp = NULL;
   int    entriesRef;
   int    entriesCmp;
   char **resourcesRef;
   if ( argc < 3 )
   {
      printf("Syntax: checkResource refFile cmpFile\n");
      exit(1);
   }
   if ( (refFp = fopen(argv[1],"r")) != NULL )
   {
      if ( (cmpFp = fopen(argv[2],"r")) != NULL )
      {
         entriesRef = readResourceFile(refFp);
         resourcesRef = resources;
         resources = NULL;
         entriesCmp = readResourceFile(cmpFp);
         
         printf("Missed resources\n");
         checkForMissing(resourcesRef,resources);

         printf("Resources not expected\n");
         checkForMissing(resources,resourcesRef);
         
         fclose(refFp);
         fclose(cmpFp);
      }
      else
      {
         fclose(refFp);
         printf("can't open file %s\n",argv[2]);
         exit(1);
      }
   }
   else
   {
      printf("can't open file %s\n",argv[1]);
      exit(1);
   }
   return 0;
}
#endif