123a0898aSmrg/*
223a0898aSmrg * Copyright 1999 SuSE, Inc.
323a0898aSmrg *
423a0898aSmrg * Permission to use, copy, modify, distribute, and sell this software and its
523a0898aSmrg * documentation for any purpose is hereby granted without fee, provided that
623a0898aSmrg * the above copyright notice appear in all copies and that both that
723a0898aSmrg * copyright notice and this permission notice appear in supporting
823a0898aSmrg * documentation, and that the name of SuSE not be used in advertising or
923a0898aSmrg * publicity pertaining to distribution of the software without specific,
1023a0898aSmrg * written prior permission.  SuSE makes no representations about the
1123a0898aSmrg * suitability of this software for any purpose.  It is provided "as is"
1223a0898aSmrg * without express or implied warranty.
1323a0898aSmrg *
1423a0898aSmrg * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
1523a0898aSmrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
1623a0898aSmrg * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1723a0898aSmrg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
1841c30155Smrg * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1923a0898aSmrg * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2023a0898aSmrg *
2123a0898aSmrg * Author:  Keith Packard, SuSE, Inc.
2223a0898aSmrg */
2323a0898aSmrg
2423a0898aSmrg#ifdef HAVE_CONFIG_H
2523a0898aSmrg#include <config.h>
2623a0898aSmrg#endif
2723a0898aSmrg#include "builtin.h"
2823a0898aSmrg
2923a0898aSmrgstatic BuiltinDirPtr
3023a0898aSmrgBuiltinDirsDup (const BuiltinDirPtr a_dirs,
3123a0898aSmrg                int a_dirs_len)
3223a0898aSmrg{
3323a0898aSmrg    BuiltinDirPtr dirs=NULL ;
3423a0898aSmrg    int i=0 ;
3523a0898aSmrg
3623a0898aSmrg    if (!a_dirs)
3723a0898aSmrg        return NULL ;
3823a0898aSmrg
397f7f5e4eSmrg    dirs = calloc (a_dirs_len, sizeof (BuiltinDirRec)) ;
4023a0898aSmrg    if (!dirs)
4123a0898aSmrg        return NULL ;
4223a0898aSmrg
4323a0898aSmrg    for (i=0; i < a_dirs_len; i++) {
447f7f5e4eSmrg	dirs[i].file_name = strdup(a_dirs[i].file_name);
457f7f5e4eSmrg	dirs[i].font_name = strdup(a_dirs[i].font_name);
4623a0898aSmrg    }
4723a0898aSmrg    return dirs ;
4823a0898aSmrg}
4923a0898aSmrg
5023a0898aSmrg/**
5123a0898aSmrg * Copy a_save back into a_cur
5223a0898aSmrg * @param a_cur the instance of BuiltinDir to restore
5323a0898aSmrg * @param a_saved the saved instance of BuiltinDir to copy into a_cur
5423a0898aSmrg * @return 0 if went okay, 1 otherwise.
5523a0898aSmrg */
5623a0898aSmrgstatic int
5723a0898aSmrgBuiltinDirRestore (BuiltinDirPtr a_cur,
5823a0898aSmrg                   const BuiltinDirPtr a_saved)
5923a0898aSmrg{
6023a0898aSmrg    if (!a_cur)
6123a0898aSmrg        return 1 ;
6223a0898aSmrg    if (!a_saved)
6323a0898aSmrg        return 0 ;
6423a0898aSmrg
6523a0898aSmrg    if (a_saved->font_name)
6623a0898aSmrg        memmove (a_cur->font_name, a_saved->font_name, strlen (a_saved->font_name)) ;
6723a0898aSmrg    return 0 ;
6823a0898aSmrg}
6923a0898aSmrg
7023a0898aSmrg
7123a0898aSmrgstatic int
7223a0898aSmrgBuiltinDirsRestore (BuiltinDirPtr a_cur_tab,
7323a0898aSmrg                    const BuiltinDirPtr a_saved_tab,
7423a0898aSmrg                    int a_tab_len)
7523a0898aSmrg{
7623a0898aSmrg    int i=0 ;
7723a0898aSmrg
7823a0898aSmrg    if (!a_cur_tab)
7923a0898aSmrg        return 1 ;
8023a0898aSmrg    if (!a_saved_tab)
8123a0898aSmrg        return 0 ;
8223a0898aSmrg
8323a0898aSmrg    for (i=0 ; i < a_tab_len; i++) {
8423a0898aSmrg        if (BuiltinDirRestore (&a_cur_tab[i], &a_saved_tab[i]))
8523a0898aSmrg            return 1 ;
8623a0898aSmrg    }
8723a0898aSmrg    return 0 ;
8823a0898aSmrg}
8923a0898aSmrg
9023a0898aSmrgstatic BuiltinAliasPtr
9123a0898aSmrgBuiltinAliasesDup (const BuiltinAliasPtr a_aliases,
9223a0898aSmrg                   int a_aliases_len)
9323a0898aSmrg{
9423a0898aSmrg    BuiltinAliasPtr aliases=NULL ;
9523a0898aSmrg    int i=0 ;
9623a0898aSmrg
9723a0898aSmrg    if (!a_aliases)
9823a0898aSmrg        return NULL ;
9923a0898aSmrg
1007f7f5e4eSmrg    aliases = calloc (a_aliases_len, sizeof (BuiltinAliasRec)) ;
10123a0898aSmrg    if (!aliases)
10223a0898aSmrg        return NULL ;
10323a0898aSmrg
10423a0898aSmrg    for (i=0; i < a_aliases_len; i++) {
1057f7f5e4eSmrg	aliases[i].font_name = strdup(a_aliases[i].font_name);
10623a0898aSmrg    }
10723a0898aSmrg    return aliases ;
10823a0898aSmrg}
10923a0898aSmrg
11023a0898aSmrg/**
11123a0898aSmrg * Copy a_save back into a_cur
11223a0898aSmrg * @param a_cur the instance of BuiltinAlias to restore
11323a0898aSmrg * @param a_saved the saved instance of BuiltinAlias to copy into a_cur
11423a0898aSmrg * @return 0 if went okay, 1 otherwise.
11523a0898aSmrg */
11623a0898aSmrgstatic int
11723a0898aSmrgBuiltinAliasRestore (BuiltinAliasPtr a_cur,
11823a0898aSmrg                     const BuiltinAliasPtr a_save)
11923a0898aSmrg{
12023a0898aSmrg    if (!a_cur)
12123a0898aSmrg        return 1 ;
12223a0898aSmrg    if (!a_save)
12323a0898aSmrg        return 0 ;
12423a0898aSmrg    if (a_save->alias_name)
12523a0898aSmrg        memmove (a_cur->alias_name, a_save->alias_name, strlen (a_save->alias_name)) ;
12623a0898aSmrg    if (a_save->font_name)
12723a0898aSmrg        memmove (a_cur->font_name, a_save->font_name, strlen (a_save->font_name)) ;
12823a0898aSmrg    return 0 ;
12923a0898aSmrg}
13023a0898aSmrg
13123a0898aSmrgstatic int
13223a0898aSmrgBuiltinAliasesRestore (BuiltinAliasPtr a_cur_tab,
13323a0898aSmrg                       const BuiltinAliasPtr a_saved_tab,
13423a0898aSmrg                       int a_tab_len)
13523a0898aSmrg{
13623a0898aSmrg    int i=0 ;
13723a0898aSmrg
13823a0898aSmrg    if (!a_cur_tab)
13923a0898aSmrg        return 1 ;
14023a0898aSmrg    if (!a_saved_tab)
14123a0898aSmrg        return 0 ;
14223a0898aSmrg
14323a0898aSmrg    for (i=0 ; i < a_tab_len; i++) {
14423a0898aSmrg        if (BuiltinAliasRestore (&a_cur_tab[i], &a_saved_tab[i]))
14523a0898aSmrg            return 1 ;
14623a0898aSmrg    }
14723a0898aSmrg    return 0 ;
14823a0898aSmrg}
14923a0898aSmrg
15023a0898aSmrgint
1510145ab54SsnjBuiltinReadDirectory (const char *directory, FontDirectoryPtr *pdir)
15223a0898aSmrg{
15323a0898aSmrg    FontDirectoryPtr	dir;
15423a0898aSmrg    int			i;
15523a0898aSmrg
15623a0898aSmrg    static BuiltinDirPtr saved_builtin_dir;
15723a0898aSmrg    static BuiltinAliasPtr saved_builtin_alias;
15823a0898aSmrg
15923a0898aSmrg    dir = FontFileMakeDir ("", builtin_dir_count);
16023a0898aSmrg
16123a0898aSmrg    if (saved_builtin_dir)
16223a0898aSmrg    {
16323a0898aSmrg        BuiltinDirsRestore ((BuiltinDirPtr) builtin_dir,
16423a0898aSmrg                            saved_builtin_dir,
16523a0898aSmrg                            builtin_dir_count) ;
16623a0898aSmrg    }
16723a0898aSmrg    else
16823a0898aSmrg    {
16923a0898aSmrg        saved_builtin_dir = BuiltinDirsDup ((const BuiltinDirPtr) builtin_dir,
17023a0898aSmrg                                            builtin_dir_count) ;
17123a0898aSmrg    }
17223a0898aSmrg
17323a0898aSmrg    if (saved_builtin_alias)
17423a0898aSmrg    {
17523a0898aSmrg        BuiltinAliasesRestore ((BuiltinAliasPtr) builtin_alias,
17623a0898aSmrg                               saved_builtin_alias,
17723a0898aSmrg                               builtin_alias_count) ;
17823a0898aSmrg    }
17923a0898aSmrg    else
18023a0898aSmrg    {
18123a0898aSmrg        saved_builtin_alias = BuiltinAliasesDup ((const BuiltinAliasPtr) builtin_alias,
18223a0898aSmrg                                                 builtin_alias_count) ;
18323a0898aSmrg    }
18423a0898aSmrg
18523a0898aSmrg    for (i = 0; i < builtin_dir_count; i++)
18623a0898aSmrg    {
18723a0898aSmrg	if (!FontFileAddFontFile (dir,
18823a0898aSmrg				  (char *) builtin_dir[i].font_name,
18923a0898aSmrg				  (char *) builtin_dir[i].file_name))
19023a0898aSmrg	{
19123a0898aSmrg	    FontFileFreeDir (dir);
19223a0898aSmrg	    return BadFontPath;
19323a0898aSmrg	}
19423a0898aSmrg    }
19523a0898aSmrg    for (i = 0; i < builtin_alias_count; i++)
19623a0898aSmrg    {
19741c30155Smrg	if (!FontFileAddFontAlias (dir,
19823a0898aSmrg				   (char *) builtin_alias[i].alias_name,
19923a0898aSmrg				   (char *) builtin_alias[i].font_name))
20023a0898aSmrg	{
20123a0898aSmrg	    FontFileFreeDir (dir);
20223a0898aSmrg	    return BadFontPath;
20323a0898aSmrg	}
20423a0898aSmrg    }
20523a0898aSmrg    FontFileSortDir (dir);
20623a0898aSmrg    *pdir = dir;
20723a0898aSmrg    return Successful;
20823a0898aSmrg}
209