mkafmmap.c - enscript - GNU Enscript
 (HTM) git clone git://thinkerwim.org/enscript.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       mkafmmap.c (4657B)
       ---
            1 /*
            2  * Create font map for AFM files.
            3  * Copyright (c) 1995, 1996, 1997 Markku Rossi.
            4  *
            5  * Author: Markku Rossi <mtr@iki.fi>
            6  */
            7 
            8 /*
            9  * This file is part of GNU Enscript.
           10  *
           11  * Enscript is free software: you can redistribute it and/or modify
           12  * it under the terms of the GNU General Public License as published by
           13  * the Free Software Foundation, either version 3 of the License, or
           14  * (at your option) any later version.
           15  *
           16  * Enscript is distributed in the hope that it will be useful,
           17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
           18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
           19  * GNU General Public License for more details.
           20  *
           21  * You should have received a copy of the GNU General Public License
           22  * along with Enscript.  If not, see <http://www.gnu.org/licenses/>.
           23  */
           24 
           25 #ifdef HAVE_CONFIG_H
           26 #include <config.h>
           27 #endif
           28 
           29 #include <stdio.h>
           30 
           31 #if HAVE_STDLIB_H
           32 #include <stdlib.h>
           33 #endif
           34 
           35 #if HAVE_STRING_H
           36 #include <string.h>
           37 #endif
           38 
           39 #include "gettext.h"
           40 #define _(String) gettext (String)
           41 
           42 #if HAVE_LC_MESSAGES
           43 #include <locale.h>
           44 #endif
           45 
           46 #include "afm.h"
           47 #include "getopt.h"
           48 
           49 
           50 /*
           51  * Definitions.
           52  */
           53 
           54 #define HANDLE_ERROR(msg)                                        \
           55   if (error != AFM_SUCCESS)                                        \
           56     {                                                                \
           57       char buf[256];                                                \
           58       afm_error_to_string (error, buf);                                \
           59       fprintf (stderr, "%s: %s: %s\n", program, msg, buf);        \
           60       exit (1);                                                        \
           61     }
           62 
           63 
           64 /*
           65  * Prototypes for static functions.
           66  */
           67 
           68 static void usage ();
           69 
           70 
           71 /*
           72  * Static variables.
           73  */
           74 
           75 /* Options. */
           76 
           77 /*
           78  * --output-file, -p
           79  *
           80  * The name of the file to which font map is stored.  If name is NULL,
           81  * leaves output to stdout.
           82  */
           83 static char *fname = "font.map";
           84 
           85 
           86 static char *program;
           87 
           88 static struct option long_options[] =
           89 {
           90   {"output-file",        required_argument,        0, 'p'},
           91   {"help",                no_argument,                0, 'h'},
           92   {"version",                no_argument,                0, 'V'},
           93   {NULL, 0, 0, 0},
           94 };
           95 
           96 /*
           97  * Global functions.
           98  */
           99 
          100 int
          101 main (int argc, char *argv[])
          102 {
          103   AFMError error;
          104   AFMHandle afm;
          105   AFMFont font;
          106   int i;
          107   FILE *ofp;
          108   FILE *mfp;
          109 
          110   program = strrchr (argv[0], '/');
          111   if (program == NULL)
          112     program = argv[0];
          113   else
          114     program++;
          115 
          116   /* Make getopt_long() to use our modified programname. */
          117   argv[0] = program;
          118 
          119   /* Internationalization. */
          120 #if HAVE_SETLOCALE
          121 #if HAVE_LC_MESSAGES
          122   setlocale (LC_MESSAGES, "");
          123 #endif
          124 #endif
          125 #if ENABLE_NLS
          126   bindtextdomain (PACKAGE, LOCALEDIR);
          127   textdomain (PACKAGE);
          128 #endif
          129 
          130   /* Handle arguments. */
          131   while (1)
          132     {
          133       int option_index = 0;
          134       int c;
          135 
          136       c = getopt_long (argc, argv, "p:h", long_options, &option_index);
          137       if (c == -1)
          138         break;
          139 
          140       switch (c)
          141         {
          142         case 'h':                /* help */
          143           usage ();
          144           exit (0);
          145 
          146         case 'p':                /* output file */
          147           /* Check output file "-". */
          148           if (strcmp (optarg, "-") == 0)
          149             fname = NULL;
          150           else
          151             fname = optarg;
          152           break;
          153 
          154         case 'V':                /* version number */
          155           printf ("%s for GNU %s %s\n", program, PACKAGE, VERSION);
          156           exit (0);
          157           break;
          158 
          159         case '?':                /* errors in arguments */
          160           usage ();
          161           exit (1);
          162           break;
          163         }
          164     }
          165 
          166   /* Open output file. */
          167   printf (_("file=%s\n"), fname ? fname : _("stdout"));
          168   if (fname)
          169     {
          170       ofp = fopen (fname, "w");
          171       if (ofp == NULL)
          172         {
          173           char buf[256];
          174 
          175           sprintf (buf, _("%s: couldn't open output file \"%s\""),
          176                    program, fname);
          177           perror (buf);
          178           exit (1);
          179         }
          180       mfp = stdout;
          181     }
          182   else
          183     {
          184       ofp = stdout;
          185       mfp = stderr;
          186     }
          187 
          188   error = afm_create (NULL, 0, &afm);
          189   HANDLE_ERROR (_("couldn't create AFM library"));
          190 
          191   for (i = optind; i < argc; i++)
          192     {
          193       fprintf (mfp, "%s...\n", argv[i]);
          194       error = afm_open_file (afm, AFM_I_MINIMUM, argv[i], &font);
          195       if (error == AFM_SUCCESS)
          196         {
          197           char *cp;
          198           char *sf;
          199           int len;
          200 
          201           cp = strrchr (argv[i], '/');
          202           if (cp == NULL)
          203             cp = argv[i];
          204           else
          205             cp++;
          206 
          207           sf = strrchr (argv[i], '.');
          208           if (sf)
          209             len = sf - cp;
          210           else
          211             len = strlen (cp);
          212 
          213           fprintf (ofp, "%-30s\t%.*s\n", font->global_info.FontName, len, cp);
          214           (void) afm_close_font (font);
          215         }
          216       else
          217         {
          218           char buf[256];
          219           afm_error_to_string (error, buf);
          220           fprintf (mfp, "%s: %s\n", program, buf);
          221         }
          222     }
          223 
          224   if (fname)
          225     fclose (ofp);
          226 
          227   return 0;
          228 }
          229 
          230 
          231 /*
          232  * Static functions.
          233  */
          234 
          235 static void
          236 usage ()
          237 {
          238   printf (_("\
          239 Usage: %s [OPTION]... FILE...\n\
          240 Mandatory arguments to long options are mandatory for short options too.\n\
          241   -h, --help              print this help and exit\n\
          242   -p, --output-file=NAME  print output to file NAME (default file is\n\
          243                           font.map).  If FILE is `-', leavy output to\n\
          244                           stdout.\n\
          245   -V, --version           print version number\n"), program);
          246 }