Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtudcfonted / dtlsgpf / lsgpf.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: lsgpf.c /main/7 1996/11/08 02:05:57 cde-fuj $ */
24 /*
25  *  (c) Copyright 1995 FUJITSU LIMITED
26  *  This is source code modified by FUJITSU LIMITED under the Joint
27  *  Development Agreement for the CDEnext PST.
28  *  This is unpublished proprietary source code of FUJITSU LIMITED
29  */
30
31
32 #include        <stdio.h>
33 #include        <errno.h>
34 #include        <locale.h>
35 #include        <string.h>
36
37 #include        "FaLib.h"
38 #include        "falfont.h"
39 #include        "bdfgpf.h"
40 #include        "udcutil.h"
41
42
43 static  void    dsp_info_list();
44 static  void    dsp_title();    
45 static  void    dspinfo_1line();
46 static  void    disp_no_fontslist();
47 static  void    sort_offset_list() ;
48 static  int     sort_fontlist() ;
49 static  void    dsp_error_title() ;
50 static  int     dsp_error_files() ;
51
52 static  void    put_help();
53 static  int     search_fontfile_name() ;
54 static int      get_new_target() ;
55
56 static  char    *utillocale ;
57 static  char    *fontdir ;
58 static  char    *command_name ;
59
60 typedef struct {
61         int             num;
62         struct {
63                 int     start;
64                 int     end;
65         }entry[BUFSIZE];
66 }MISS_ENTRY;
67
68 /* request for sort */
69 #define REQ_STYLE       (1<<1)
70 #define REQ_CHARSIZE    (1<<2)
71 #define REQ_LETTER      (1<<3)
72 #define REQ_DELEQUAL    (1<<16)
73
74 /* error flag */
75 #define NO_FONT         (1<<1)
76 #define SAME_FONT       (1<<2)
77 #define REDEFINED_FONT  (1<<3)
78
79 /* request flag */
80 #define A_OPT   (1<<1)
81 #define L_OPT   (1<<2)
82 #define X_OPT   (1<<3)
83 #define C_OPT   (1<<4)
84
85 int
86 main( argc, argv )
87 int     argc;
88 char    *argv[];
89 {
90         FalFontDataList *lst_m; /* font information list struct */
91         FalFontData     key;    /* font reference information creation struct */
92         MISS_ENTRY      unknown_m ;     /* error */
93         int             i, j;
94         int             code_sw;        /* code set flag */
95
96         int             mask;
97         int             code_set ;
98         int             errflg ;
99         int             dsp_opt ;
100
101         /* initialize */
102         command_name = argv[0] ;
103         errflg = 0 ;
104         unknown_m.num = 0;
105         code_sw = 0;
106         dsp_opt = 0 ;
107
108         /* get locale strings */
109         if( (utillocale = (char *)getenv( "LANG" )) == NULL ){
110             utillocale = "C" ;
111         }
112
113         if( (fontdir = (char *)getenv( "DTUDCFONTS" )) == (char *)NULL ) {
114                 fontdir = DTUDCFONTSLISTDIR ;
115         }
116
117         /*
118          * dtlsgpf check on which options are required.
119          */
120         for (i=1 ; i < argc ; i++) {
121
122             if (!strcmp(argv[i], "-help")) {
123                 put_help(argv[0]);
124                 exit(0);
125             }
126             if (*argv[i] != '-') {
127                 put_help(argv[0]);
128                 exit(1);
129             }
130
131             if ( !strcmp( argv[i], "-codeset" ) ) {
132                 char    *cbuf ;
133                 if ( (i+1) >= argc ){
134                     put_help( argv[0] );
135                     exit( 1 );
136                 }
137                 i++;
138                 code_set = (int)strtol( argv[i], &cbuf, 10 ) ;
139                 if( argv[i] == cbuf ) {
140                     put_help( argv[0] );
141                     exit( 1 );
142                 }
143                 code_sw = 1 ;
144             }
145
146             for (j=1;  argv[i][j] != '\0' ; j++) {
147                 char    req ;   
148                 req = argv[i][j] ;
149                 if ( req == 'l')        dsp_opt |= L_OPT ;
150                 else if ( req == 'a')   dsp_opt |= A_OPT ;
151                 else if ( req == 'x')   dsp_opt |= X_OPT ;
152                 else if ( req == 'C')   dsp_opt |= C_OPT ;
153                 else {
154                         put_help(argv[0]);
155                         exit( 1 );
156                 }
157             }
158         }
159
160         /* "-ax" -> "-a" */
161         if( dsp_opt == ( A_OPT | X_OPT ) )      dsp_opt = A_OPT ;
162
163         /*
164          * display information list of UDC code region
165          */
166         if( dsp_opt & C_OPT ){
167             fprintf(stdout ,"codeset\t\tcode area\n");
168             fprintf(stdout ,"--------------------------------------------\n");
169             if ( DispUdcCpArea( stdout ) == -1 ){
170                 ErrMsgTable_FalGetFontList( argv[0], fal_utyerror, fal_utyderror );
171                 exit( 1 );
172             }
173             exit( 0 );
174         }
175
176         /*
177          * get an information list of UDC fonts
178          */
179         mask = 0;
180         mask =  FAL_FONT_MASK_UNDEFINED | FAL_FONT_MASK_DEFINED ;
181         if( code_sw ) {
182                 mask |= FAL_FONT_MASK_CODE_SET ;
183                 key.cd_set = FALGETFALCODESET( code_set ) ;
184         }
185
186         /* get font information list */
187         if ( FalGetFontList( &key, mask, &lst_m ) == FAL_ERROR ){
188                 ErrMsgTable_FalGetFontList( argv[0], fal_utyerror, fal_utyderror );
189                 exit( 1 );
190         }
191
192         /* sort element of font infomation list */
193         if( sort_fontlist( lst_m ) == FAL_ERROR ) {
194                 FalFreeFontList( lst_m );
195                 return FAL_ERROR ;
196         }
197
198         /*
199          * dtlsgpf shows UDC informations as user's request.
200          */
201         dsp_title( stdout, dsp_opt );
202         if ( lst_m->num == 0) {
203             disp_no_fontslist( dsp_opt ) ;
204         } else {
205             dsp_info_list( argv[0], dsp_opt, lst_m, &unknown_m, &errflg );
206             if (unknown_m.num != 0) {
207                 if( dsp_opt & L_OPT ) {
208                     dsp_error_title(argv[0], &errflg, dsp_opt );
209                     if( dsp_error_files( lst_m, &unknown_m ) ) {
210                         exit( 1 );
211                     }
212                 }
213             }
214         }
215
216         FalFreeFontList( lst_m );
217         exit( 0 );
218 }
219
220
221
222
223 static void
224 dsp_info_list( com, dspopt, lst, unknown_file_lst, errflg )
225 char            *com;
226 int             dspopt;
227 FalFontDataList *lst;
228 MISS_ENTRY      *unknown_file_lst;
229 int             *errflg ;
230 {
231         FalFontDataList srt_lst;
232         int     i, j ;
233         int     entry, entry_end;
234         int     ret;
235         char    *filename;      /* file base's name */
236         char    fname[BUFSIZE];
237         char    tmpfname[BUFSIZE];
238         int     srt_cnt, *sort_list, cnt, mask ;
239
240         if( lst->num == 0 ){
241              return ;
242         }
243
244         srt_lst.num = lst->num ;
245         srt_lst.list = (FalFontData *)malloc( sizeof(FalFontData) * lst->num ) ;
246         if( srt_lst.list == NULL ) {
247                 USAGE("dtlsgpf : malloc error.\n" ) ;
248                 exit(1) ;
249         }
250
251         for (i=0, srt_cnt=0 ; i < lst->num ; i++) {
252                 int     letter_cmp;
253
254                 entry = entry_end = i;
255                 for (j=i ;
256                     (j < lst->num) &&
257                     !strcmp(lst->list[j].style.name, lst->list[entry].style.name) &&
258                     (lst->list[j].size.h == lst->list[entry].size.h  ) ;
259                     j++)
260                 {
261                     if( (lst->list[j].letter.h == 0) ||
262                         (lst->list[j].letter.w == 0) )  break ;
263                         entry_end = j;
264                 }
265
266                 if ( entry == entry_end ) {
267                     if ( dspopt & L_OPT || dspopt & X_OPT )
268                         dspinfo_1line( lst->list[entry], stdout,
269                                         unknown_file_lst, entry,
270                                         errflg, dspopt );       
271                     else        memmove( &(srt_lst.list[srt_cnt++]),
272                                             &(lst->list[entry]),
273                                             sizeof(FalFontData) );
274                     continue;
275                 }
276
277                 letter_cmp = lst->list[entry].letter.h;
278                 for (j=entry ; j <= entry_end ; j++) {
279                         if (lst->list[j].letter.h != letter_cmp) {
280                                 break;
281                         }
282                 }
283
284                 if (j > entry_end) {
285                         ret = GetDefaultFile( lst->list[entry].size.h,
286                             lst->list[entry].style.name, fname);
287                         if ( ret < 0 ) {
288                             fname[0]='\0';
289                             for ( j = entry; j <= entry_end; j++ ) {
290                                 if ( dspopt & L_OPT || dspopt & X_OPT )
291                                     dspinfo_1line( lst->list[j], stdout,
292                                                 unknown_file_lst, j,
293                                                 errflg, dspopt );
294                                 else    memmove(
295                                             &(srt_lst.list[srt_cnt++]),
296                                             &(lst->list[j]),
297                                             sizeof(FalFontData) );
298                             }
299                             i = entry_end;
300                             continue ;
301                         }
302                         for( j = entry; j <= entry_end; j++ ) {
303                             ret = search_fontfile_name(
304                                     lst->list[j], tmpfname,
305                                     unknown_file_lst, j, errflg
306                                      ) ;
307                             if( ret == -1 )             continue ;
308                             else if( ret == -2 )        exit(1) ;
309                             filename = falcom_get_base_name( tmpfname ) ;
310                             if ( !strcmp( filename, fname ) ){
311                                 if ( dspopt & L_OPT || dspopt & X_OPT )
312                                     dspinfo_1line( lst->list[j], stdout,
313                                                     unknown_file_lst, j,
314                                                     errflg, dspopt );
315                                 else    memmove(
316                                             &(srt_lst.list[srt_cnt++]),
317                                             &(lst->list[j]),
318                                             sizeof(FalFontData) );
319                                 break;
320                             }
321                         }
322                         i = entry_end;
323                         continue;
324                 }
325
326
327                 ret = GetDefaultFile( 
328                                 lst->list[entry].size.h, 
329                                 lst->list[entry].style.name, 
330                                 fname
331                         );
332                 if( ret ) {
333                     switch ( ret ) {
334                         case    -1:
335                             for ( j = entry; j <= entry_end; j++ ) {
336                                 if ( dspopt & L_OPT || dspopt & X_OPT )
337                                     dspinfo_1line(
338                                                 lst->list[j], stdout,
339                                                 unknown_file_lst, j,
340                                                 errflg, dspopt );
341                                 else    memmove(
342                                             &(srt_lst.list[srt_cnt++]),
343                                             &(lst->list[j]),
344                                             sizeof(FalFontData) );
345                             }
346                             break ;
347                         case    -2:
348                             unknown_file_lst->entry[ unknown_file_lst->num ].start = entry;
349                             unknown_file_lst->entry[ unknown_file_lst->num ].end   = entry_end;
350                             unknown_file_lst->num++;
351                             *errflg |= REDEFINED_FONT ;
352                             break ;
353                         case    -3:
354                             USAGE4("%s : The user defined character information file cannot be read.\"%s/%s/%s \"\n\n",
355                             com, fontdir, utillocale, UDFLIST_DEF );
356                             exit(1) ;
357                         case    -4:
358                             USAGE4("%s : The mistake is found in the description of the user defined character information file.\"%s/%s/%s \"\n\n",
359                             com, utillocale, fontdir, UDFLIST_DEF );
360                             for ( j = entry; j <= entry_end; j++ ) {
361                                 if ( dspopt & L_OPT || dspopt & X_OPT )
362                                     dspinfo_1line( lst->list[j], stdout,
363                                                 unknown_file_lst, j,
364                                                 errflg, dspopt );
365                                 else    memmove(
366                                             &(srt_lst.list[srt_cnt++]),
367                                             &(lst->list[j]),
368                                             sizeof(FalFontData) );
369                             }
370                             break ;
371                     }
372                     i = entry_end;
373                     continue;
374                 }
375
376                 for ( j = entry; j <= entry_end; j++ ) {
377                         ret = search_fontfile_name(
378                                 lst->list[j], tmpfname,
379                                 unknown_file_lst, j, errflg
380                                 ) ;
381                         if( ret == -1 )         continue ;
382                         else if( ret == -2 )    exit(1) ;
383                         filename = falcom_get_base_name( tmpfname ) ;
384                         if ( !strcmp( filename, fname ) ){
385                             if ( dspopt & L_OPT || dspopt & X_OPT )
386                                 dspinfo_1line( lst->list[j], stdout,
387                                             unknown_file_lst, j,
388                                             errflg, dspopt );   
389                             else        memmove(
390                                             &(srt_lst.list[srt_cnt++]),
391                                             &(lst->list[j]),
392                                             sizeof(FalFontData) );
393                             break;
394                         }
395                 }
396                 if ( j > entry_end ) {
397                         unknown_file_lst->entry[ unknown_file_lst->num ].start = entry;
398                         unknown_file_lst->entry[ unknown_file_lst->num ].end  = entry_end;
399                         unknown_file_lst->num++;
400                         *errflg |= REDEFINED_FONT ;
401                 }
402                 i = entry_end;
403         }
404
405         /*
406          * dtlsgpf displays the infomations of user defined characters. 
407          */
408         if ( !(dspopt & L_OPT) ) { /* display character size and style */
409                 srt_lst.num = srt_cnt ;
410
411             if ( dspopt & A_OPT ) 
412                 mask = REQ_STYLE | REQ_CHARSIZE | REQ_LETTER | REQ_DELEQUAL ; 
413             else 
414                 mask = REQ_CHARSIZE | REQ_DELEQUAL ; 
415
416                 sort_offset_list( &srt_lst, mask, &sort_list, &cnt ) ;
417
418                 for (i=0 ; i<cnt; i++)
419                     dspinfo_1line( srt_lst.list[sort_list[i]], stdout,
420                                     unknown_file_lst, sort_list[i],
421                                     errflg, dspopt );
422                 free( sort_list ) ;
423                 free( srt_lst.list ) ;
424         }
425 }
426
427
428
429
430 static void
431 dsp_error_title(com, errflg, dspopt )
432 char    *com;
433 int     *errflg ;
434 int     dspopt ;
435 {
436     USAGE("\n\n" );
437     if( dspopt & A_OPT ) {
438         if( *errflg & SAME_FONT )
439             USAGE1("%s :  There are fonts that are same character size and have same style, \ncode set and XLFD. It is not possible to select user defined character fonts uniquely.\n\n", com);
440         USAGE("Please inquire of the system manager.\n\n");
441     } else {
442         if( *errflg & NO_FONT )
443             USAGE1("\n%s : Mistake is found in the name of user defined character fonts \nfollowing character size and style.\n\n", com);
444         if( *errflg & REDEFINED_FONT )
445             USAGE3(" Please describe the following file name in the user defined character \ninformation file(%s/%s/%s).\n\n",
446             fontdir, utillocale, UDFLIST_DEF );
447     }
448 }
449
450
451
452
453
454 static int
455 dsp_error_files( lst, unknown )
456 FalFontDataList *lst;
457 MISS_ENTRY      *unknown;
458 {
459         int     i,j;
460         int     entry_start, entry_end;
461         char    *filename;
462         char    tmp_fname[BUFSIZE];
463         int     ret ;
464
465         if ( unknown->num == 0 ){
466                 return  0;
467         }
468         for (i=0 ; i<unknown->num ; i++) {
469                 entry_start = unknown->entry[i].start;
470                 entry_end = unknown->entry[i].end;
471
472                 USAGE1("\tcode set \t%d \n",
473                         FALCODESETTONUM( lst->list[entry_start].cd_set ) ) ;
474                 USAGE1("\tcharacter_size \t%d\n",
475                         lst->list[entry_start].size.h ) ;
476                 USAGE1("\tstyle \t\t%s \n", lst->list[entry_start].style.name ) ;
477                 USAGE("\n" );
478                 for ( j = entry_start; j <= entry_end; j++ ) {
479                         ret = search_fontfile_name( lst->list[j], tmp_fname, NULL, 0, 0 ) ;
480                         if( ret == -1 ){
481                                 USAGE("\t\tThere is no font file that is required." );
482                                 USAGE1("\t%s\n", lst->list[j].xlfdname );
483                                 continue ;
484                         } else if( ret == -2 ){
485                             return -1 ;
486                         }
487                         filename = tmp_fname ;
488                         USAGE1("\t\t%s", filename );
489                         USAGE1("\t%s\n", lst->list[j].xlfdname );
490                 }
491                 USAGE("\n" );
492         }
493         USAGE("\n" );
494         return  0;
495 }
496
497
498
499 #define SORTOFFSETNUM {\
500         offset = size_offset_list[j] ;\
501         size_offset_list[j] = size_offset_list[i] ;\
502         size_offset_list[i] = offset ;\
503 }
504
505
506 static void
507 sort_offset_list( flist, mask, sort_list, data_cnt )
508 FalFontDataList *flist ;
509 int             mask ;
510 int             **sort_list ;
511 int             *data_cnt ;
512 {
513         int     *size_offset_list ;
514         int     cnt, i, j ;
515         int     offset ;
516         int     exist ;
517
518         if( flist->num == 0 ){
519             return ;
520         }
521
522         if( (size_offset_list = (int *)malloc( flist->num * sizeof(int) )) == NULL )
523         {
524                 USAGE("dtlsgpf: malloc error.\n" );
525                 exit(1) ;
526         }
527         for( i=0 ; i < flist->num; i++ )        size_offset_list[i] = -1 ;
528
529         for( i=0, cnt=0; i < flist->num; i++ ) {
530             if( mask & REQ_DELEQUAL ) {
531                 exist = REQ_DELEQUAL ;
532                 for( j=0; j<cnt; j++ ) {
533                         if( size_offset_list[j] == -1 ) break ;
534                         if( (mask & REQ_CHARSIZE) &&
535                         (flist->list[size_offset_list[j]].size.h == flist->list[i].size.h) 
536                         ) {
537                                 exist |= REQ_CHARSIZE ;
538                         }
539                         if( (mask & REQ_LETTER) &&
540                         (flist->list[size_offset_list[j]].letter.h == flist->list[i].letter.h)
541                         ) {
542                                 exist |= REQ_LETTER ;
543                         }
544                         if( (mask & REQ_STYLE) &&
545                         !strcmp(flist->list[size_offset_list[j]].style.name, flist->list[i].style.name)
546                         ) {
547                                 exist |= REQ_STYLE ;
548                         }
549                         if( exist == mask )     break ;
550                         else                    exist = REQ_DELEQUAL ;
551                 }       /* size_offset_list */
552                 if( exist == mask )     continue ;
553             }   /* REQ_DELEQUAL */
554             size_offset_list[cnt++] = i ;
555         }       /* flist */
556         /* sort data */
557         for( i=0; i<cnt-1; i++ ) {
558             for( j=i+1; j<cnt; j++ ) {
559                 if( mask & REQ_LETTER ) {
560                     if( flist->list[size_offset_list[i]].letter.h >
561                         flist->list[size_offset_list[j]].letter.h)
562                     {
563                         SORTOFFSETNUM ;
564                     }
565                 }
566                 if( mask & REQ_CHARSIZE ) {
567                     if( flist->list[size_offset_list[i]].size.h >
568                         flist->list[size_offset_list[j]].size.h)
569                     {
570                         SORTOFFSETNUM ;
571                     }
572                 }
573                 if( mask & REQ_STYLE ) {
574                     if( strcoll(flist->list[size_offset_list[i]].style.name,
575                                 flist->list[size_offset_list[j]].style.name) > 0 )
576                     {
577                         SORTOFFSETNUM ;
578                     }
579                 }
580             }
581         }
582         *sort_list = size_offset_list ;
583         *data_cnt  = cnt ;
584 }
585
586
587
588 static void
589 dsp_title( fp, dspopt )
590 FILE    *fp;
591 int     dspopt ;
592 {
593     if( dspopt & L_OPT || dspopt & X_OPT ) {
594         fprintf(fp, "\n" ) ;
595
596         if( dspopt & X_OPT )    fprintf(fp, " code set" );
597         if( dspopt & L_OPT )    fprintf(fp, "\tfilename\t" );
598         if( (dspopt != X_OPT) && (dspopt != (L_OPT | X_OPT)) )
599             fprintf(fp, "character size\t" );
600         COMM_DSP_LETTER_T ;
601         if( dspopt & A_OPT )    fprintf(fp, "style \t" );
602         COMM_DSP_PRM ;
603         if( dspopt != X_OPT )   fprintf(fp, "\n" ) ;
604         if( dspopt & X_OPT )    fprintf(fp, "\tXLFD\n" );
605         fprintf(fp ,"-----------------------------------------------------------------------------\n");
606     }
607 }
608
609
610
611
612
613 static void
614 dspinfo_1line( data, fp, unknown_file_lst, offset, errflg, dspopt )
615 FalFontData     data;
616 FILE            *fp;
617 MISS_ENTRY      *unknown_file_lst;
618 int             offset ;
619 int             *errflg ;
620 int             dspopt ;
621 {
622         int     dsp_on ;
623         char    *namep;
624         char    tmp_fname[BUFSIZE];
625         int     ret ;
626
627         dsp_on = 0;
628
629         /* get font informations */
630         ret = search_fontfile_name( data, tmp_fname, unknown_file_lst, offset, errflg ) ;
631         if( ret == -1 )         return ;
632         else if( ret == -2 )    exit(1) ;
633         namep = tmp_fname ;
634
635         /* display font informations */
636         if( dspopt & X_OPT )    fprintf( fp, "%d \t", FALCODESETTONUM( data.cd_set ) );
637
638         if( dspopt & L_OPT )    fprintf( fp, "%s\t", namep );
639
640         if( (dspopt != X_OPT) &&
641             (dspopt != (L_OPT | X_OPT))
642         ) {
643             fprintf( fp, "%d", data.size.h );
644         }
645
646         if( dspopt & A_OPT ) {
647             if( data.letter.h ) {
648                 if( !(dspopt & L_OPT) )
649                     fprintf( fp, ".%d", data.letter.h );
650                 else
651                     fprintf( fp, "\t%4d", data.letter.h );
652             } 
653             fprintf( fp, "\t" );
654             fprintf( fp, "%s", data.style.name );
655         }
656
657         if( dspopt & L_OPT ) {
658             fprintf( fp, "\t" );
659             if( data.prm & FAL_FONT_DISPLAY ) {
660                 fprintf( fp, "display");
661                 dsp_on = 1;
662             }
663             if( data.prm & FAL_FONT_PRINTER ) {
664                 if( dsp_on ) {
665                     fprintf( fp, " & ");
666                 }
667                 fprintf( fp, "printer");
668             }
669         }
670         if( dspopt != X_OPT ){
671             fprintf( fp, "\n");
672             if( dspopt & X_OPT )        fprintf( fp, "\t" );
673         }
674         if( dspopt & X_OPT )    fprintf( fp, "%s\n", data.xlfdname );
675         if( dspopt != X_OPT ){
676             if( dspopt & X_OPT )        fprintf( fp, "\n" );
677         }
678 }
679
680
681
682
683
684 static void
685 disp_no_fontslist( dspopt )
686 int     dspopt ;
687 {
688     fprintf( stdout, "\nThere are no fonts that are used for user defined character.\n" ) ;
689 }
690
691
692 static void
693 put_help( prog_name )
694 char    *prog_name;
695 {
696         USAGE1("Usage : %s [-lax][-la][-lx][-C][-codeset number][-help]\n", prog_name);
697         USAGE("\t-l       :  display of file name and character size\n");
698         USAGE("\t-a       :  display of style(FAMILY_NAME) information\n");
699         USAGE("\t-x       :  display of XLFD information\n");
700         USAGE("\t-C       :  display of code area informations\n");
701         USAGE("\t-codeset :  specification of the codeset\n");
702         USAGE("\t-help    :  display of of the command parameter informations\n");
703
704         USAGE("        (If the option is not specified, only the character size are displayed.)\n");
705 }
706
707
708
709 static  int
710 search_fontfile_name( data, fname, unknown_file_lst, offset, errflg )
711 FalFontData     data ;
712 char            *fname ;
713 MISS_ENTRY      *unknown_file_lst;
714 int             offset ;
715 int             *errflg ;
716 {
717         FalFontID       fid ;
718         FalFontDataList *flist ;
719         int             mask ;
720         char            *tmp_fname ;
721
722         mask = 
723                 FAL_FONT_MASK_UNDEFINED | FAL_FONT_MASK_DEFINED |
724                 FAL_FONT_MASK_XLFDNAME | 
725                 FAL_FONT_MASK_SIZE_H |
726                 FAL_FONT_MASK_STYLE_NAME | FAL_FONT_MASK_CODE_SET
727                 ;
728
729         if( data.size.w >= 0 )  mask |= FAL_FONT_MASK_SIZE_W ;
730         if( data.letter.w )     mask |= FAL_FONT_MASK_LETTER_W ;
731         if( data.letter.h )     mask |= FAL_FONT_MASK_LETTER_H ;
732         if( data.letter.x )     mask |= FAL_FONT_MASK_LETTER_X ;
733         if( data.letter.y )     mask |= FAL_FONT_MASK_LETTER_Y ;
734
735         if( data.style.def )    mask |= FAL_FONT_MASK_STYLE_DEF ;
736         if( data.shape.name )   mask |= FAL_FONT_MASK_SHAPE_NAME ;
737         if( data.shape.def )    mask |= FAL_FONT_MASK_SHAPE_DEF ;
738         COMM_SBCHR_SETFONTDATA( data, mask ) ;
739         if( data.prm )          mask |= FAL_FONT_MASK_PERMISSION ;
740
741         fid = FalOpenSysFont( &data, mask, &flist ) ;
742         if( fid == 0 ) {
743                 memset( fname, '\0', sizeof(fname) ) ;
744                 if( flist ){
745                     FalFreeFontList( flist ) ;
746                     if( unknown_file_lst ) {
747                         unknown_file_lst->entry[ unknown_file_lst->num ].start = offset ;
748                         unknown_file_lst->entry[ unknown_file_lst->num ].end  = offset ;
749                         unknown_file_lst->num++;
750                         *errflg |= SAME_FONT ;
751                     }
752                     return(-1);
753                 } else {
754                   switch( fal_utyerror ) {
755                    case _FAL_OPEN_ER :
756                    case _FAL_READ_ER :
757                    case _FAL_STAT_ER :
758                         switch( fal_utyderror ) {
759                         case EACCES :
760                         case ENOENT :
761                             if( unknown_file_lst ) {
762                                 unknown_file_lst->entry[ unknown_file_lst->num ].start = offset ;
763                                 unknown_file_lst->entry[ unknown_file_lst->num ].end  = offset ;
764                                 unknown_file_lst->num++;
765                                 *errflg |= NO_FONT ;
766                             }
767                             return(-1) ;
768                         default :
769                             USAGE2("System call error occured. fal_utyerror = %d fal_utyderror = %d\n", fal_utyerror, fal_utyderror ) ;
770                             return(-2) ;
771                         }
772                    case _FAL_FONT_ER :
773                         if( unknown_file_lst ) {
774                             unknown_file_lst->entry[ unknown_file_lst->num ].start = offset ;
775                             unknown_file_lst->entry[ unknown_file_lst->num ].end  = offset ;
776                             unknown_file_lst->num++;
777                             *errflg |= NO_FONT ;
778                         }
779                         return(-1) ;
780                    case _FAL_MALOC_ER :
781                         USAGE("malloc error occured.\n" ) ;
782                         return(-2) ;
783                    default :
784                         USAGE1("%s : cannot get font information list.\n", command_name ) ;
785                         return(-2) ;
786                   }
787                 }
788         } 
789         if( FalFontIDToFileName( fid, &tmp_fname ) == FAL_ERROR ) {
790                 USAGE1("%s : cannot get file name.\n", command_name ) ;
791                 FalCloseFont( fid ) ;
792                 return(-2) ;
793         }
794         strcpy( fname, tmp_fname ) ;
795         FalFree( tmp_fname ) ;
796         FalCloseFont( fid ) ;
797         return(0) ;
798 }
799
800
801
802 static int
803 sort_fontlist( lst )
804 FalFontDataList *lst;
805 {
806         int             i,j;
807         FalFontDataList srt;
808         FalFontData             target;
809         int                     target_entry;
810
811         if( lst->num == 0 ){
812              return(0) ;
813         }
814
815         srt.num  = lst->num;
816
817         if (( srt.list = (FalFontData *)malloc(sizeof(FalFontData) * srt.num)) == NULL) {
818                 return  FAL_ERROR;
819         }
820         memmove (srt.list, lst->list, sizeof(FalFontData) * srt.num);
821         /* start to sort */
822         for ( i=0 ; i < srt.num -1 ; i++ ) {
823
824                 memmove( &target, &(srt.list[i]), sizeof(FalFontData));
825                 target_entry = i;
826
827                 for ( j=i+1 ; j < srt.num ; j++ ) {
828                         if ( get_new_target( &target, &(srt.list[j]) ) ) {
829                                 memmove( &target, &(srt.list[j]), sizeof(FalFontData));
830                                 target_entry = j;
831                         }
832                 }
833                 if (target_entry != i) {
834                         memmove( &target, &(srt.list[target_entry]), 
835                             sizeof(FalFontData)
836                             );
837                         memmove( &(srt.list[i+1]), &(srt.list[i]), 
838                             sizeof(FalFontData)*(target_entry -i)
839                             );
840                         memmove( &(srt.list[i]), &target, 
841                             sizeof(FalFontData)
842                             );
843                 }
844         }
845         free(lst->list);
846         lst->list = srt.list;
847         return(0);
848 }
849
850
851 /* sort flag                      */
852 /* return data ¡§   sort    ... 1 */
853 /*                  no sort ... 0 */
854
855 static int
856 get_new_target( target, choose )
857 FalFontData     *target;
858 FalFontData     *choose;
859 {
860         FalFontData     diff;
861         DEF_STR_CHK ;
862         DEF_STR_SET ;
863
864         diff.style.def = choose->style.def - target->style.def;
865         str_chk.style =  strcmp( choose->style.name, target->style.name ) ;
866
867         NEW_TARGET_SET( diff, choose, target, str_chk ) ; 
868
869         diff.shape.def = choose->shape.def - target->shape.def;
870         if( (choose->shape.name != NULL) && (target->shape.name != NULL) ) {
871             str_chk.shape =  strcmp( choose->shape.name, target->shape.name ) ;
872         }
873
874         diff.cd_set   = choose->cd_set   - target->cd_set;
875         diff.size.h   = choose->size.h   - target->size.h;
876         diff.size.w   = choose->size.w   - target->size.w;
877         diff.letter.h = choose->letter.h - target->letter.h;
878         diff.letter.w = choose->letter.w - target->letter.w;
879
880         /* codeset */
881         if ( diff.cd_set < 0 )          return  1; 
882         if ( diff.cd_set > 0 )          return  0; 
883
884         /* character size height */
885         if ( diff.size.h < 0 )          return  1; 
886         if ( diff.size.h > 0 )          return  0; 
887
888         /* letter size hieght */
889         if ( diff.letter.h < 0 )        return  1; 
890         if ( diff.letter.h > 0 )        return  0; 
891
892         /* character size wide */
893         if ( diff.size.w < 0 )          return  1; 
894         if ( diff.size.w > 0 )          return  0; 
895
896         /* letter size wide */
897         if ( diff.letter.w < 0 )        return  1; 
898         if ( diff.letter.w > 0 )        return  0; 
899
900         /* style */
901         if ( target->style.def == FAL_FONT_UNKNOWN ) {
902                 if ( choose->style.def > 0 )    return  1; 
903                 /* style is FAL_FONT_UNKNOWN both "target" and "choose" */
904                 if ( str_chk.style < 0 )        return  1; 
905                 if ( str_chk.style > 0)         return  0; 
906         } else if ( choose->style.def == FAL_FONT_UNKNOWN ) {
907                 return  0;
908         }
909         /* target->style.def and choose->style.def is not FAL_FONT_UNKNOWN */
910         if ( diff.style.def < 0 )       return  1; 
911         if ( diff.style.def > 0 )       return  0; 
912
913         NEW_TARGET_CHK( diff, choose, target, str_chk ) ;
914
915         /* character style */
916         if ( target->shape.def == FAL_FONT_UNKNOWN ) {  
917                 if ( choose->shape.def > 0 )    return  1; 
918                 if ( str_chk.shape < 0 )        return  1; 
919                 if ( str_chk.shape > 0 )        return  0; 
920         } else if (choose->shape.def == FAL_FONT_UNKNOWN ) {
921                 return  0;
922         }
923         if ( diff.shape.def < 0 )       return  1; 
924         if ( diff.shape.def > 0 )       return  0; 
925
926         return  0;
927 }
928