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