src/vflmklib.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. copy_cmdline
  2. banner
  3. x_strdup
  4. check_font_exist
  5. check_argc
  6. map_need_tfm

   1 /* 
   2  * vflmklib.c 
   3  * - commn functions used in vflmkXXX.c
   4  *
   5  *
   6  * by Hirotsugu Kakugawa
   7  *
   8  *  10 May 2001
   9  */
  10 /*
  11  * Copyright (C) 2001  Hirotsugu Kakugawa. 
  12  * All rights reserved.
  13  *
  14  * This program is free software; you can redistribute it and/or modify
  15  * it under the terms of the GNU General Public License as published by
  16  * the Free Software Foundation; either version 2, or (at your option)
  17  * any later version.
  18  * 
  19  * This program is distributed in the hope that it will be useful,
  20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22  * GNU General Public License for more details.
  23  * 
  24  * You should have received a copy of the GNU General Public License
  25  * along with this program; if not, write to the Free Software
  26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
  27  */
  28 
  29 
  30 #include "config.h"
  31 #include "with.h"
  32 #include <stdio.h>
  33 #include <stdlib.h>
  34 #include <ctype.h>
  35 #include <unistd.h>
  36 #include <sys/param.h>
  37 #include <sys/time.h>
  38 
  39 #ifdef WITH_KPATHSEA
  40 # include  "kpathsea/kpathsea.h"
  41 #endif
  42 
  43 #include  "VFlib-3_6.h"
  44 #include  "VFsys.h"
  45 #include  "vflibcap.h"
  46 #include  "fsearch.h"
  47 
  48 
  49 
  50 char*
  51 copy_cmdline(int xargc, char **xargv)
     /* [<][>][^][v][top][bottom][index][help] */
  52 {
  53   int   i, t;
  54   char  *s, *p;
  55 
  56   t = 1;
  57   for (i = 0; i < xargc; i++)
  58     t = t + strlen(xargv[i]) + 1;
  59   if ((s = malloc(t)) == NULL){
  60     fprintf(stderr, "no memory.\n");
  61     exit(1);
  62   }
  63   
  64   p = s;
  65   for (i = 0; i < xargc; i++){
  66     strcpy(p, xargv[i]);
  67     p += strlen(xargv[i]);
  68     *(p++) = ' ';
  69   }
  70   p[t-1] = '\0';
  71 
  72   return s;
  73 }
  74 
  75 
  76 void 
  77 banner(char *name, char *prog, char *cline)
     /* [<][>][^][v][top][bottom][index][help] */
  78 {
  79   char  host[256], ts[256], *p;
  80   time_t   t;
  81 
  82   if (gethostname(host, sizeof(host)) < 0)
  83     strcpy(host, "???");
  84   t = time(NULL);
  85 
  86   strcpy(ts, asctime(localtime(&t)));
  87   if ((p = strchr(ts, '\n')) != NULL)
  88     *p = '\0';
  89 
  90   printf(";; -------------------------------------------------------\n");
  91   printf(";; *** %s ***\n", name);
  92   printf(";; Generated by %s\n", prog);
  93   printf(";; at %s on %s\n", ts, host);
  94   printf(";; Commandline:\n");
  95   printf(";;   %s\n", cline);
  96   printf("\n");
  97 }
  98 
  99 char*
 100 x_strdup(char *s)
     /* [<][>][^][v][top][bottom][index][help] */
 101 {
 102   char  *p;
 103 
 104   if (s == NULL){
 105     fprintf(stderr, "internal error.");
 106     exit(1);
 107   }
 108 
 109   p = malloc(strlen(s) + 1);
 110   if (p == NULL){
 111     fprintf(stderr, "no memory.");
 112     exit(1);
 113   }
 114   
 115   strcpy(p, s);
 116   return p;
 117 }
 118 
 119 char*
 120 check_font_exist(char *file, char **dirs, int ndirs, int kptype, char **ext)
     /* [<][>][^][v][top][bottom][index][help] */
 121 {
 122   int  i, l, j;
 123   char  path[MAXPATHLEN], *p, **e;
 124   char *e0[] = { "", NULL };
 125 
 126   if (ndirs == 0)
 127     return 0;  /* may or may not exist */
 128 
 129   for (i = 0; i < ndirs; i++){
 130     if ((strcmp(dirs[i], "TEXMF") == 0)
 131         || (strcmp(dirs[i], "KPATHSEA") == 0)
 132         || (strcmp(dirs[i], "texmf") == 0)
 133         || (strcmp(dirs[i], "kpathsea") == 0)){
 134       p = kpse_find_file(file, kptype, 0);
 135       if (access(p, R_OK) >= 0)
 136         return 0;  /* exist */
 137     } else {
 138       e = ext;
 139       if (e == NULL)
 140         e = e0;
 141       for (j = 0; e[j] != NULL; j++){
 142         sprintf(path, "%s", dirs[i]);
 143         l = strlen(dirs[i]);
 144         if ((l >= 2) && (dirs[i][l-2] == '/') && (dirs[i][l-1] == '/')){
 145           sprintf(&path[l-2], "/%s", file);
 146         } else if ((l >= 2) && (dirs[i][l-2] != '/') && (dirs[i][l-1] == '/')){
 147           sprintf(&path[l-1], "/%s", file);
 148         } else {
 149           sprintf(&path[l], "/%s", file);
 150         }
 151         strcat(path, e[j]);
 152 #if 0
 153         printf("***%s\n", path);
 154 #endif
 155         if (access(path, R_OK) >= 0){
 156           sprintf(path, "%s%s", file, e[j]);
 157           return  x_strdup(path);    /* exist */
 158         }
 159       }
 160     }
 161   }
 162   
 163   return NULL;  /* not exist */
 164 }
 165 
 166 
 167 void
 168 check_argc(int argc)
     /* [<][>][^][v][top][bottom][index][help] */
 169 {
 170   if (argc == 0){
 171     fprintf(stderr,"Illegal command line argument\n");
 172     fprintf(stderr,"Use '--help' option for help\n");
 173     exit(1);
 174   }
 175 }
 176 
 177 
 178 int
 179 map_need_tfm(char *fontclass)
     /* [<][>][^][v][top][bottom][index][help] */
 180 {
 181   int  i;
 182   static char *db[] = {
 183     "type1", 
 184     "vf", 
 185     NULL};
 186   
 187   for (i = 0; db[i] != NULL; i++){
 188     if (strcmp(fontclass, db[i]) == 0)
 189       return 1;
 190   }
 191   return 0;  
 192 }

/* [<][>][^][v][top][bottom][index][help] */