f03f2b79212b9e310708fa4cb6a77130a8d0b002
[enscript.git] / src / mkafmmap.c
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  * This program 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 2, or (at your option)
14  * any later version.
15  *
16  * This program 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 this program; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <stdio.h>
32
33 #if HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36
37 #if HAVE_STRING_H
38 #include <string.h>
39 #endif
40
41 #if ENABLE_NLS
42 #include <libintl.h>
43 #define _(String) gettext (String)
44 #else
45 #define _(String) String
46 #endif
47
48 #if HAVE_LC_MESSAGES
49 #include <locale.h>
50 #endif
51
52 #include "afm.h"
53 #include "getopt.h"
54
55
56 /*
57  * Definitions.
58  */
59
60 #define HANDLE_ERROR(msg)                                       \
61   if (error != AFM_SUCCESS)                                     \
62     {                                                           \
63       char buf[256];                                            \
64       afm_error_to_string (error, buf);                         \
65       fprintf (stderr, "%s: %s: %s\n", program, msg, buf);      \
66       exit (1);                                                 \
67     }
68
69
70 /*
71  * Prototypes for static functions.
72  */
73
74 static void usage ();
75
76
77 /*
78  * Static variables.
79  */
80
81 /* Options. */
82
83 /*
84  * --output-file, -p
85  *
86  * The name of the file to which font map is stored.  If name is NULL,
87  * leaves output to stdout.
88  */
89 static char *fname = "font.map";
90
91
92 static char *program;
93
94 static struct option long_options[] =
95 {
96   {"output-file",       required_argument,      0, 'p'},
97   {"help",              no_argument,            0, 'h'},
98   {"version",           no_argument,            0, 'V'},
99   {NULL, 0, 0, 0},
100 };
101
102 /*
103  * Global functions.
104  */
105
106 int
107 main (int argc, char *argv[])
108 {
109   AFMError error;
110   AFMHandle afm;
111   AFMFont font;
112   int i;
113   FILE *ofp;
114   FILE *mfp;
115
116   program = strrchr (argv[0], '/');
117   if (program == NULL)
118     program = argv[0];
119   else
120     program++;
121
122   /* Make getopt_long() to use our modified programname. */
123   argv[0] = program;
124
125   /* Internationalization. */
126 #if HAVE_SETLOCALE
127 #if HAVE_LC_MESSAGES
128   setlocale (LC_MESSAGES, "");
129 #endif
130 #endif
131 #if ENABLE_NLS
132   bindtextdomain (PACKAGE, LOCALEDIR);
133   textdomain (PACKAGE);
134 #endif
135
136   /* Handle arguments. */
137   while (1)
138     {
139       int option_index = 0;
140       int c;
141
142       c = getopt_long (argc, argv, "p:h", long_options, &option_index);
143       if (c == -1)
144         break;
145
146       switch (c)
147         {
148         case 'h':               /* help */
149           usage ();
150           exit (0);
151
152         case 'p':               /* output file */
153           /* Check output file "-". */
154           if (strcmp (optarg, "-") == 0)
155             fname = NULL;
156           else
157             fname = optarg;
158           break;
159
160         case 'V':               /* version number */
161           printf ("%s for GNU %s %s\n", program, PACKAGE, VERSION);
162           exit (0);
163           break;
164
165         case '?':               /* errors in arguments */
166           usage ();
167           exit (1);
168           break;
169         }
170     }
171
172   /* Open output file. */
173   printf (_("file=%s\n"), fname ? fname : _("stdout"));
174   if (fname)
175     {
176       ofp = fopen (fname, "w");
177       if (ofp == NULL)
178         {
179           char buf[256];
180
181           sprintf (buf, _("%s: couldn't open output file \"%s\""),
182                    program, fname);
183           perror (buf);
184           exit (1);
185         }
186       mfp = stdout;
187     }
188   else
189     {
190       ofp = stdout;
191       mfp = stderr;
192     }
193
194   error = afm_create (NULL, 0, &afm);
195   HANDLE_ERROR (_("couldn't create AFM library"));
196
197   for (i = optind; i < argc; i++)
198     {
199       fprintf (mfp, "%s...\n", argv[i]);
200       error = afm_open_file (afm, AFM_I_MINIMUM, argv[i], &font);
201       if (error == AFM_SUCCESS)
202         {
203           char *cp;
204           char *sf;
205           int len;
206
207           cp = strrchr (argv[i], '/');
208           if (cp == NULL)
209             cp = argv[i];
210           else
211             cp++;
212
213           sf = strrchr (argv[i], '.');
214           if (sf)
215             len = sf - cp;
216           else
217             len = strlen (cp);
218
219           fprintf (ofp, "%-30s\t%.*s\n", font->global_info.FontName, len, cp);
220           (void) afm_close_font (font);
221         }
222       else
223         {
224           char buf[256];
225           afm_error_to_string (error, buf);
226           fprintf (mfp, "%s: %s\n", program, buf);
227         }
228     }
229
230   if (fname)
231     fclose (ofp);
232
233   return 0;
234 }
235
236
237 /*
238  * Static functions.
239  */
240
241 static void
242 usage ()
243 {
244   printf (_("\
245 Usage: %s [OPTION]... FILE...\n\
246 Mandatory arguments to long options are mandatory for short options too.\n\
247   -h, --help              print this help and exit\n\
248   -p, --output-file=NAME  print output to file NAME (default file is\n\
249                           font.map).  If FILE is `-', leavy output to\n\
250                           stdout.\n\
251   -V, --version           print version number\n"), program);
252 }