Don't use fstat for readable pipe chars in dtexec.
[oweals/cde.git] / cde / programs / dtudcexch / xlfdutil.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: xlfdutil.c /main/2 1996/10/14 14:45:50 barstow $ */
24 /*
25  *  All Rights Reserved, Copyright (c) FUJITSU LIMITED 1995
26  *
27  *  This is unpublished proprietary source code of FUJITSU LIMITED
28  *
29  *  Authors: Seiya Miyazaki     FUJITSU LIMITED
30  *           Hiroyuki Chiba     Fujitsu Basic Software Corporation
31  *
32  */
33
34
35 #include <string.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <nl_types.h>
39
40 #include <Xm/Xm.h>
41
42 #include <Xm/Form.h>
43 #include <Xm/PushB.h>
44 #include <Xm/Text.h>
45 #include <Xm/TextF.h>
46 #include <Xm/Label.h>
47 #include <Xm/SeparatoG.h>
48 #include <Xm/List.h>
49 #include <Xm/ToggleB.h>
50 #include <Xm/MessageB.h>
51
52 #include "FaLib.h"
53 #include "falxlfd.h"
54
55 #include "selectxlfd.h"
56
57 #ifdef DEBUG
58 #undef Dprintf
59 #define Dprintf fprintf
60 #else
61 #define Dprintf
62 #endif /* DEBUG */
63
64 /*
65  * parameters
66  */
67 static char* skipSpace() ;
68
69 extern  FalFontDataList         *font_L ;
70 extern  FalxFontDataBuff        KeyBuff ;
71 extern  FalxCodeList            *CodeList ;
72 extern  FalxStyleList           *StyleList ;
73 extern  FalxSizeList            *SizeList ;
74 extern  int                     SelectedOffset ;
75 extern  int                     getmask ;
76
77 extern char     *fullPathName ;
78
79
80 extern Widget   toplevel ;
81 extern Widget
82         xlfdWform, cpyWform,
83         slctBLabel[BUTTONITEMS],slctBText[BUTTONITEMS], slctButton[BUTTONITEMS], 
84         listPop[BUTTONITEMS],   listW[BUTTONITEMS], 
85         errorMD, msgBox ;
86
87
88 /****************************************************************
89  * functions for get font infomation
90 ****************************************************************/
91
92 int
93 GetXLFDInfomations()
94 {
95         Dprintf( stdout, "GetXLFDInfomations()\n" ) ;
96
97         /* initialize */
98         getmask         = 0 ;
99         SelectedOffset  = 0 ;
100         fullPathName    = NULL ;
101         FalxInitDataBuff( &KeyBuff ) ;
102
103         /* get UDC informations */
104         if( FalxGetFontList( &font_L, XtDisplayOfObject( toplevel ) ) ){
105                 Dprintf( stderr, "GetXLFDInfomations() : FalxGetFontList() error.\n" ) ;
106                 return(-1);
107         }
108
109         /* create code area list */
110         if( FalxCreateCodeList( &CodeList, font_L ) ){
111                 Dprintf( stderr, "\nGetXLFDInfomations() : FalxCreateCodeList() error.\n" ) ;
112                 return(-1);
113         }
114
115         /* create style list */
116         if( FalxCreateStyleList( &StyleList, font_L ) ){
117                 Dprintf( stderr, "\nGetXLFDInfomations() : FalxCreateStyleList() error.\n" ) ;
118                 return(-1);
119         }
120
121         /* create size list */
122         if( FalxCreateSizeList( &SizeList, font_L ) ){
123                 Dprintf( stderr, "\nGetXLFDInfomations() : FalxCreateSizeList() error.\n" ) ;
124                 return(-1);
125         }
126
127         return(0) ;
128 }
129
130
131
132
133
134 int
135 GetItemsToDisplay( num, itemcnt, xms_list )
136 int             num ;
137 int             *itemcnt ;
138 XmString        **xms_list ;
139 {
140         int             i, j, cnt ;
141         char            *sp ,string[256], buf[256] ;
142         Arg             args[1];
143         int             length, maxlength ;
144         FalxXlfdList    *xlist ;
145
146         int             tmp_cnt ;
147         char            **tmp_dlist, *str ;
148         XmString        *xms ;
149         int             rlen ;
150         int             rtn ;
151
152
153         switch( num ){
154             case 0 :    /* XLFD         */
155                 /* set key data         */
156                 for( i=1; i<BUTTONITEMS; i++ ){
157                     if( (str = XmTextGetString( slctBText[i] )) == NULL ){
158                         exit(-1) ;
159                     }
160                     if( *str == ' ' || *str == NULL )   continue ;
161                     if( SetKeyBuff( i, str ) ){
162                         exit(-1) ;
163                     }
164                 }
165                 rtn = FalxGetXlfdList( &xlist, font_L, &KeyBuff, getmask ) ;
166                 if( rtn )       return( rtn ) ;
167                 tmp_cnt  = xlist->num ;
168                 tmp_dlist = xlist->list ;
169                 break ;
170             case 1 :    /* code area    */
171                 tmp_cnt = CodeList->num ;
172                 tmp_dlist = (char **)malloc( sizeof(char *) * tmp_cnt ) ;
173                 if( tmp_dlist == NULL )         return(-1) ;
174                 for( i=0; i<tmp_cnt; i++ ){
175                     sp = string ;
176                     rlen = sprintf( sp, "CS:%d ", (CodeList->dlist[i]->fontset) ) ;
177                     sp += rlen ;
178                     for( j=0; j<CodeList->dlist[i]->code_area_num; j++ ){
179                         rlen = sprintf( sp, "%x - %x ",
180                                  CodeList->dlist[i]->alist[j]->udc_start,
181                                  CodeList->dlist[i]->alist[j]->udc_end   ) ;
182                         sp += rlen ;
183                     }
184                     if( (tmp_dlist[i] = strdup( string )) == NULL ){
185                         return(-1) ;
186                     }
187                 }
188                 break ;
189             case 2 :    /* style        */
190                 tmp_cnt  = StyleList->num ;
191                 tmp_dlist = (char **)malloc( sizeof(char *) * tmp_cnt ) ;
192                 for( i=0; i<tmp_cnt; i++ ){
193                     if( (tmp_dlist[i] = strdup( StyleList->list[i] )) == NULL ){
194                         return(-1) ;
195                     }
196                 }
197                 break ;
198             case 3 :    /* size         */
199                 tmp_cnt = SizeList->num ;
200                 tmp_dlist = (char **)malloc( sizeof(char *) * tmp_cnt ) ;
201                 for( i=0; i<tmp_cnt; i++ ){
202                     sprintf( string, "%d ", SizeList->list[i] ) ;
203                     if( (tmp_dlist[i] = strdup( string )) == NULL ){
204                         return(-1) ;
205                     }
206                 }
207                 break ;
208             default :   /* error        */
209                 return(-1) ;
210         }
211         if( (xms = (XmString *)calloc( tmp_cnt, sizeof(XmString) )) == NULL ){
212             return( -1 ) ;
213         }
214
215         /*
216         *       set list for display
217         */
218         maxlength = 0 ;
219         for ( i=0; i<tmp_cnt; i++ ){
220             xms[i] = XmStringCreateLocalized( tmp_dlist[i] );
221         }
222
223         /*
224         *       free allocated memory
225         */
226
227         switch( num ){
228             case 0 :    /* XLFD         */
229                 FalxFreeXlfdList( xlist ) ;
230                 break ;
231             case 1 :    /* code area    */
232             case 2 :    /* style        */
233             case 3 :    /* size         */
234                 for( i=0; i<tmp_cnt; i++ ){
235                     free( tmp_dlist[i] ) ;
236                 }
237                 free( tmp_dlist ) ;
238                 break ;
239             default :   /* error        */
240                 return(-1) ;
241         }
242
243
244         *itemcnt  = tmp_cnt ;
245         *xms_list = xms ;
246
247         return(0) ;
248
249 }
250
251
252
253
254 static char*
255 skipSpace( str, skipcnt )
256 char    *str ;
257 int     skipcnt ;
258 {
259         int     i, spacecnt ;
260         char    *sp ;
261         int     len ;
262
263         if( *str == NULL )      return( NULL ) ;
264         len = strlen( str ) ;
265         sp = str ;
266         for( i=0,spacecnt=0; i<len-1; i++, sp++ ) {
267             if( *sp == NULL )   return( NULL ) ;
268             if( *sp == ' ' )    spacecnt++ ;
269             if( spacecnt == skipcnt )   break ;
270         }
271         sp++ ;
272         return( sp ) ;
273 }
274
275
276
277
278 int
279 SetKeyBuff( num, str )
280 int     num ;
281 char    *str ;
282 {
283         int     i, j ;
284         int     cdset, start, end ;
285         FalxUdcArea     *tmp_list ;
286         char    hyp, *sp ;
287
288         switch( num ){
289             case 0 :            /* XLFD         */
290                 KeyBuff.FontData.xlfdname = str ;
291                 break ;
292             case 1 :            /* code area    */
293                 getmask |= FAL_FONT_MASK_CODE_SET ;
294                 if( KeyBuff.FileData.list != NULL ){
295                     free( KeyBuff.FileData.list ) ;
296                 }
297                 KeyBuff.FileData.list   = NULL ;
298                 tmp_list                = NULL ;
299
300                 sp = str ;
301                 if( sscanf( sp, "CS:%d ", &cdset ) != 1 ){
302                         return(-1) ;
303                 }
304                 if( (sp = skipSpace( sp, 1 )) == NULL ) return( -1 ) ;
305                 num = 0 ;
306                 KeyBuff.FontData.cd_set = cdset ;
307                 while( sscanf( sp, "%x %c %x ", &start, &hyp, &end ) == 3 ){
308                     if( hyp != '-' )    break ;
309                     if( tmp_list == NULL ){
310                         tmp_list = (FalxUdcArea *)malloc( sizeof(FalxUdcArea) ) ;
311                     }else{
312                         tmp_list = (FalxUdcArea *)realloc( KeyBuff.FileData.list,
313                                          sizeof(FalxUdcArea) * (num+1) ) ;
314                     }
315                     if( tmp_list == NULL )      return(-1) ;
316                     tmp_list[num].udc_start = (int)start ;
317                     tmp_list[num].udc_end   = (int)end   ;
318                     num ++ ;
319                     if( (sp = skipSpace( sp, 3 )) == NULL )     break  ;
320                 }
321                 if( num == 0 )  return(-1) ;
322                 KeyBuff.FileData.code_area_num  = num ;
323                 KeyBuff.FileData.list           = tmp_list ;
324                 break ;
325             case 2 :            /* style        */
326                 getmask |= FAL_FONT_MASK_STYLE_NAME ;
327                 KeyBuff.FontData.style.name = str ;
328                 break ;
329             case 3 :            /* size         */
330                 getmask |= FAL_FONT_MASK_LETTER_W ;
331                 getmask |= FAL_FONT_MASK_LETTER_H ;
332                 KeyBuff.FontData.letter.w = atoi( str ) ;
333                 KeyBuff.FontData.letter.h = atoi( str ) ;
334                 break ;
335             default :
336                 return(-1) ;
337         }
338         return(0);
339 }
340
341