File: msgfmt.c

package info (click to toggle)
xview 3.2p1.4-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 20,068 kB
  • ctags: 24,304
  • sloc: ansic: 241,105; yacc: 1,392; sh: 1,140; makefile: 273; lex: 76; perl: 54; asm: 50; cpp: 15
file content (669 lines) | stat: -rw-r--r-- 14,352 bytes parent folder | download | duplicates (9)
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
#ifndef lint
#ifdef sccs
static char     sccsid[] = "@(#)msgfmt.c 1.3 91/09/14";
#endif
#endif

#include "msgfmt.h"

/*
 * msgfmt - Generate binary tree for runtime gettext() using psffm:
 *           "Portable Source File Format for Messages" file template.
 *	     This file may have previously been generated by the
 *	     c2psffm filter for c source files.
 */

#define    ignore(a)    ((void) a)

char	*consume_whitespace();
char	expand_meta();
char    *calloc();
off_t    tellpt;
off_t    sortit();
char    *mktemp();
char    *savestr();
char    *strcat();
char    *strcpy();

off_t    mesgpt;

/*
 * Strings is a flat file of id, string pairs, used by cflg
 */

char    *strings =    "strings";

char	linebuf[MAX_VALUE_LEN];
char	*domain_name = DEFAULT_DOMAIN;

int    cflg = 0;
int    verbose = 0;
int    debug = 0;
int    readstd;

struct list_struct list;

main(argc, argv)
    int argc;
    char *argv[];
{

    argc--, argv++;
    while (argc > 0 && argv[0][0] == '-') {

        register char *cp = &(*argv++)[1];

        argc--;

        if (*cp == 0) {
            readstd++;
        }

        do switch (*cp++) {

/*
 *	cflag stuff not yet implemented.

        case 'c':
            cflg++;
            continue;
 */

	case 'd':
	    debug++;
	    break;

        case 'v':
            verbose++;
            continue;

        default:
/*
            fprintf(stderr,
                "usage: msgfmt [ -v ] [ -c ] [ - ] [ name ... ]\n");
*/
            fprintf(stderr,
                "usage: msgfmt [ -dv ] [ - ] [ name ... ]\n");
	    break;;

        } while (*cp);	/* do switch ... */

    } /* while (argc ... */


    while (readstd || argc > 0) {

        if (!readstd && freopen(argv[0], "r", stdin) == NULL) {
            perror(argv[0]);
	    exit(2);
	}

    	read_psffm(argv[0]);

        if (readstd == 0) {
            argc--; argv++;
	} else {
            readstd = 0;
	}
    }	/* while (readstd ... */

    exit(0);
}


/* 
 * read_psffm - read in "psffm" format file, check syntax, printing
 * 		error messages as needed, output binary tree to
 *		file <domain>
*/

read_psffm(filename)
    char *filename;
{

    static char current_domain[MAX_DOMAIN_LENGTH];
    static char msgfile[MAXPATHLEN];


    char *linebufptr;
    char *current_locale;
    char msgid [MAX_VALUE_LEN+1];
    char msgstr [MAX_VALUE_LEN+1];
    register char *bufptr;
    

    int linenum = 0;
    int	domain_set = 0;
    int whitespace = 0;
    int quotefound = 0;
    int ignored = 0;
    int indomain = 0;
    int inmsgid = 0;
    int inmsgstr = 0;
    char c;


    while (1) {
	memset(linebuf, 0, sizeof(linebuf));
        if (fgets(linebuf, sizeof(linebuf), stdin) == NULL) {
            if (ferror(stdin)) {
                perror(filename);
                exit(2);
            }
            break;
        }
	linenum ++;

	if (verbose) {
	    printf("Scanning line number %d: %s\n", linenum, linebuf);
	}

	linebufptr = linebuf;
	whitespace = 0;

        switch (*linebufptr) {
	case '#': 	/*   comment    */
	case '\n':
	ignored = 1;
	break;

	case '\"':
	case ' ':	/*   whitespace should be handled correctly */
	case '\t':
	    whitespace = 1;
	default:
	    ignored = 0;
	    break;
	}

	if (ignored) {
	    continue;
	}

	/*
	 * Process MSGID Tokens, must not have just seen one unless
	 * this line begins with whitespace -or- "
	 */

	if ((!strncmp(MSGID_TOKEN, linebuf, sizeof(MSGID_TOKEN)-1))
	    || (whitespace && inmsgid)) {
	    if (inmsgid && !whitespace) {
		fprintf(stderr, "Consecutive MSGID tokens encountered ");
		fprintf(stderr, "at line number: %d, exiting\n", linenum);
/*
		exit(1);
*/
	    }
	    if (inmsgstr) {
		sortit(msgid, msgstr, 1);

		memset(msgid, 0, sizeof(msgid));
		memset(msgstr, 0, sizeof(msgstr));
	    }


	    if (inmsgid) {
	        linebufptr = consume_whitespace(linebuf);
	    } else {
	        linebufptr = consume_whitespace(linebuf +
		    sizeof(MSGID_TOKEN)-1);
		bufptr = msgid;
	    }

	    inmsgid = 1;
	    inmsgstr = 0;
	    indomain = 0;
	    goto load_buffer;
	}

	/*
	 * Process MSGSTR Tokens, must not have just seen one unless
	 * this line begins with whitespace -or- "
	 */

	if ((!strncmp(MSGSTR_TOKEN, linebuf, sizeof(MSGSTR_TOKEN)-1))
	    || (whitespace && inmsgstr)) {
	    if (inmsgstr && !whitespace) {
		fprintf(stderr, "Consecutive MSGSTR tokens encountered ");
		fprintf(stderr, "at line number: %d, exiting\n", linenum);
/*
		exit(1);
*/
	    }

	    if (inmsgstr) {
	        linebufptr = consume_whitespace(linebuf);
	    } else {
	        linebufptr = consume_whitespace(linebuf +
		    sizeof(MSGSTR_TOKEN)-1);
		bufptr = msgstr;
	    }

	    inmsgstr = 1;
	    inmsgid = 0;
	    indomain = 0;
	    goto load_buffer;
	}

	/*
	 * Process DOMAIN Tokens, add message id and message string to
	 * sorted list if msgstr was being processed, 
	 * write out file for existing domain if it exists (domain_set = 1)
	*/

	if (!strncmp(DOMAIN_TOKEN, linebuf, sizeof(DOMAIN_TOKEN)-1)) {
	    if (inmsgstr) {
		sortit(msgid, msgstr, 1);
	    }

	    if (inmsgstr || inmsgid) {
		memset(msgid, 0, sizeof(msgid));
		memset(msgstr, 0, sizeof(msgstr));
	    }

	    if (domain_set) {
		output_file(msgfile);
	    }
	    domain_set = 1;
	    indomain = 1;
	    inmsgid = 0;
	    inmsgstr = 0;
		
	    linebufptr = consume_whitespace(linebuf+ sizeof(DOMAIN_TOKEN));
	    memset(current_domain, 0, sizeof(current_domain));
	    bufptr = current_domain;
	}

load_buffer:

	if (*linebufptr++ != '\"') {
	    fprintf(stderr, "Warning, Syntax at line number: %d, ",
		linenum);
	    fprintf(stderr, "Missing \", ignored\n");
	    --linebufptr;
	}

	quotefound = 0;

	while (c=*linebufptr++) {
	    switch (c) {
	    case '\n':
		if (!quotefound) {
		    fprintf(stderr, "Warning, Syntax at line number: %d, ",
			linenum);
		    fprintf(stderr, "Missing \" at EOL, ignored\n");
		}
		break;

	    case '\"':
		quotefound = 1;
	        break;

	    case '\\':
		*bufptr++ = expand_meta(&linebufptr);
		break;

	    default:
	        *bufptr++ = c;
	    }
	}

	if (indomain) {
	    memset(msgfile, 0, sizeof(msgfile));
	    strcpy(msgfile, current_domain);
	    strcat(msgfile, ".mo");
/*
	    strcpy(msgfile, LOCALEDIR);
	    strcat(msgfile, current_locale);
	    strcat(msgfile, "/");
	    strcat(msgfile, current_domain);
*/
	}
    }

    if (inmsgstr) {
	sortit(msgid, msgstr, 1);
    }

    if (inmsgstr || inmsgid) {
	memset(msgid, 0, sizeof(msgid));
	memset(msgstr, 0, sizeof(msgstr));
    }

    if (domain_set) {
	output_file(msgfile);
    }
    return(0);
}


char *
consume_whitespace(buf)
    char *buf;
{
    register char *bufptr = buf;
    char c;
    while (c = *bufptr++) {
	switch (c) {
	    case ' ':
	    case '\t' :
		continue;
	    default:
		return(bufptr-1);
	}
    }
}

char
expand_meta(buf)
    char **buf;
{
    register char c = **buf;

    switch (c) {
        case '"':
	    (*buf)++;
	    return ('\"');
	case '\\':
	    (*buf)++;
	    return ('\\');
        case 'b' :
	    (*buf)++;
	    return ('\b');
        case 'f' :
	    (*buf)++;
	    return ('\f');
        case 'n' :
	    (*buf)++;
	    return ('\n');
        case 'r' :
	    (*buf)++;
	    return ('\r');
        case 't' :
	    (*buf)++;
	    return ('\t');
        case 'v' :
	    (*buf)++;
	    return ('\v');
        default:
	    return ('\\');
    }
}



/*
 * sortit - insertion sort, place message identifier = str and
 * message string = msgstr into list sorted by message identifier
*/

off_t
sortit(msgid, msgstr, new)
    char *msgid, *msgstr;
    int new;
{
    int i, j, lessthan;
    register struct list_struct *hp, *hp0, *hp1;

    if (debug)
	fprintf(stderr,
	    "==> sortit(), msgid = %s, msgstr = %s\n", msgid, msgstr);
    lessthan = 0;
    hp = hp0 = &list;
    while (hp->hnext && !lessthan) {
        hp0 = hp;
        hp = hp->hnext;
        i = istail(hp->msgid, msgid);
        if (i < 0) {
            lessthan = 1;
            hp1 = hp;
            continue;
        }
        if (i == 0) {             /* found! */
            return(hp->hpt);
	}
    }
    if (!lessthan) {
        hp0 = hp;
    }
    if ((hp = (struct list_struct *) calloc(1, sizeof(*hp))) == NULL) {
        perror("msgfmt");
        exit(8);
    }
    hp->hpt = mesgpt;
    hp->msgid = savestr(msgid);
    hp->msg = savestr(msgstr);
    mesgpt += strlen(hp->msgid) + strlen(hp->msg);

    if (hp0->hnext) {
        hp->hnext = hp1;
        hp0->hnext = hp;
        hp->hnew = new;
    } else {
        hp->hnew = new;
        hp0->hnext = hp;
    }
    return(hp->hpt);
}

printlist()
{
    struct list_struct *hp1;

    hp1 = &list;
    fprintf(stderr, "dumping record at offset hpt = %d\n", hp1->hpt);
    fprintf(stderr, "message id is %s\n", hp1->msgid);
    fprintf(stderr, "message string is %s\n", hp1->msg);

    while (hp1->hnext) {
	fprintf(stderr, "dumping record at offset hpt = %d\n", hp1->hpt);
        fprintf(stderr, "message id is %s\n", hp1->hnext->msgid);
	fprintf(stderr, "message string is %s\n", hp1->hnext->msg);
        hp1 = hp1->hnext;
    }

}


/*
 * binary_compute is used for pre-computing a binary search. 
 */
int
binary_compute(i, j, more, less)
    int i,j;
    int more[],less[];
{
    int k;

    if (i > j ) {
        return -99;
    }
    k = (i + j)/2;

    less[k] = binary_compute(i, k-1, more, less);
    more[k] = binary_compute(k+1, j, more, less);
    return k;
}

/*
 * Write out static_message File contains static structure of messages.
 */
#ifdef USE_MMAP
output_file(file)
    char *file;
{
        FILE *xdotcf = fopen(file, "w");
        struct list_struct *lp;
        int     message_count;
        int     string_count_msgid=0;
        int     string_count_msg=0;
        int     msgid_index=0;
        int     msg_index=0;
        int     *less,*more;
        int     i;
 
        if (xdotcf == NULL) {
	    perror("static_messages");
	    exit(6);
	}
 
        message_count = 0;
        lp = &list;
        while (lp->hnext) { /* get offsets and counts */
                lp->hnext->msgid_offset = msgid_index;
                lp->hnext->msg_offset = msg_index;
                msgid_index += strlen(lp->hnext->msgid)+1;
                msg_index += strlen(lp->hnext->msg)+1;
                message_count++;
                lp = lp->hnext;
        }
        string_count_msgid = msgid_index;
        string_count_msg = msg_index;
        if ((less = (int *) calloc(message_count, sizeof(int))) == NULL) {
                perror("msgfmt output_file()");
                exit(8);
        }
        if ((more = (int *) calloc(message_count, sizeof(int))) == NULL) {
                perror("msgfmt output_file()");
                exit(8);
        }
        (void) binary_compute(0, message_count-1, more, less);
        if (message_count == 2) {
                less[1] = -99; /* kludge that works! */
        }
 
        /* write out the message.mo */
        i = (message_count-1)/2; /* this is the first message to check */
        (void) fwrite(&i, sizeof(int), 1, xdotcf);
        (void) fwrite(&message_count, sizeof(int), 1, xdotcf);
        (void) fwrite(&string_count_msgid, sizeof(int), 1, xdotcf);
        (void) fwrite(&string_count_msg, sizeof(int), 1, xdotcf);
        i = sizeof(struct list_struct) * message_count;
        (void) fwrite(&i, sizeof(int), 1, xdotcf);
        i = 0; /* used to get more and less */
        lp = &list;
        while (lp->hnext) { /* put out message struct */
                (void) fwrite(&less[i], sizeof(int), 1, xdotcf);
                (void) fwrite(&more[i], sizeof(int), 1, xdotcf);
                (void) fwrite(&lp->hnext->msgid_offset, sizeof(int), 1, xdotcf);
                (void) fwrite(&lp->hnext->msg_offset, sizeof(int), 1, xdotcf);
                i++;
                lp = lp->hnext;
        }
        lp = &list;
        while (lp->hnext) { /* put out messageid strings */
                (void) fwrite(lp->hnext->msgid, strlen(lp->hnext->msgid)+1, 1, xdotcf);            
                lp = lp->hnext;
        }
        lp = &list;
        while (lp->hnext) { /* put out message strings */
                (void) fwrite(lp->hnext->msg, strlen(lp->hnext->msg)+1, 1, xdotcf);
                lp = lp->hnext;
        }
        ignore(fclose(xdotcf));
        /* clear list */
        list.hpt =0;
        list.msgid_offset =0;
        list.msg_offset =0;
        list.msgid =(char )0;
        list.msg =(char )0;
        list.hnext = NULL;
        list.hnew = 0;
}

#else


output_file(file)
    char *file;
{
    FILE *xdotcf = fopen(file, "w");
    struct list_struct *lp;
    int    message_count;
    int    *less, *more;
    int    i;

    if (xdotcf == NULL){
        perror(file);
	 exit(6);
    }

    message_count = 0;
    lp = &list;
    while (lp->hnext) {
        message_count++;
        lp = lp->hnext;
    }
    if ((less = (int *) calloc(message_count, sizeof(int))) == NULL) {
        perror("msgfmt output_file()");
        exit(8);
    }
    if ((more = (int *) calloc(message_count, sizeof(int))) == NULL) {
        perror("msgfmt output_file()");
        exit(8);
    }
    binary_compute(0, message_count-1, more, less);
    if (message_count == 2) {
        less[1] = 0; /* kludge that works! */
    }

    

    /* put out messages in binary format */

    fprintf(xdotcf,"%d\n%d\n", message_count, (message_count-1)/2);
    lp = &list;
    while (lp->hnext) {
        fprintf(xdotcf,"%s\n", lp->hnext->msgid); /* message id */
        fprintf(xdotcf,"%d\n%d\n", *less++, *more++);
        fprintf(xdotcf,"%s\n", lp->hnext->msg); /* message id */
        lp = lp->hnext;
    }
    ignore(fclose(xdotcf));

    freelist();
}
#endif

/*
 * freelist() - frees all members of the sorted list after the list
 * 		is written out to the binary tree file, called when
 *		changing domains and when exiting the program
*/

freelist()
{
    register struct list_struct *hp, *hp0;

    hp0 = &list;
    hp = hp0->hnext;

    while (hp->hnext) {
        hp0 = hp;
        hp = hp->hnext;
        free(hp0);
    }
    free(hp);
    list.hnext = 0;
}

char *
savestr(cp)
    register char *cp;
{
    register char *dp;

    if ((dp = (char *) calloc(1, strlen(cp) + 1)) == NULL) {
        perror("msgfmt");
        exit(8);
    }
    return(strcpy(dp, cp));
}


istail(str, of)
    register char *str, *of;
{

        return(strcmp(of, str));
}