File: dsc.c

package info (click to toggle)
a2ps 1%3A4.14-1.3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,324 kB
  • sloc: ansic: 26,966; sh: 11,844; lex: 2,286; perl: 1,156; yacc: 757; makefile: 609; lisp: 398; ada: 263; objc: 189; f90: 109; ml: 85; sql: 74; pascal: 57; modula3: 33; haskell: 32; sed: 30; java: 29; python: 24
file content (413 lines) | stat: -rw-r--r-- 10,418 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * dsc.c
 *
 * Recording information about the PostScript resources
 *
 * Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
 * Copyright (c) 1995, 96, 97, 98 Akim Demaille, Miguel Santana
 */

/*
 * This file is part of a2ps.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * $Id: dsc.c,v 1.1.1.1.2.1 2007/12/29 01:58:16 mhatta Exp $
 */
#include "a2ps.h"
#include "dsc.h"
#include "jobs.h"
#include "routines.h"
#include "str_ht.h"

/************************************************************************
 * Multivalued hash tables						*
 ************************************************************************/
typedef struct multivalued_entry {
  char * key;			/* E.g., "font", "color" 		*/
  struct string_htable * entries;	/* E.g., "Courier", "Helvetica-Bold"	*/
} multivalued_entry;

static unsigned long
mv_key_hash_1 (struct multivalued_entry *key)
{
  return_STRING_HASH_1 (key->key);
}

static unsigned long
mv_key_hash_2 (struct multivalued_entry *key)
{
  return_STRING_HASH_2 (key->key);
}

static int
mv_key_hash_cmp (struct multivalued_entry *x, struct multivalued_entry *y)
{
  return_STRING_COMPARE (x->key, y->key);
}

/*
 * Create and return a new multivaluated_entry, with key TYPE.
 */
static struct multivalued_entry *
multivalued_entry_new (const char * type)
{
  NEW (struct multivalued_entry, res);
  res->key = xstrdup (type);
  res->entries = string_htable_new ();
  return res;
}

/*
 * Completely free ENTRY and content.
 */
static void
multivalued_entry_free (struct multivalued_entry * entry)
{
  free (entry->key);
  string_htable_free (entry->entries);
  free (entry);
}

/*
 * Return the multivalued_entry related to TYPE in TABLE
 * if there is, NULL otherwise.
 */
static struct multivalued_entry *
multivalued_entry_get (struct hash_table_s * table, const char * type)
{
  static struct multivalued_entry token, * res;

  token.key = (char *) type;
  res = (struct multivalued_entry *) hash_find_item (table, &token);
  return res;
}

/*
 * Add a new multivalued_entry with TYPE, if necessary
 */
static void
multivalued_entry_add (struct hash_table_s * table,
		       struct multivalued_entry * item)
{
  struct multivalued_entry * old_item;

  old_item = multivalued_entry_get (table, item->key);
  if (old_item)
    multivalued_entry_free (old_item);

  hash_insert (table, item);
}

/*
 * In the macro table TABLE, get the sub_table having TYPE (create
 * if necessary), and in this sub table, store a malloc'd copy of VALUE
 */
static void
multivalued_entry_add_couple (hash_table * table,
			      const char * type, const char * value)
{
  struct multivalued_entry * sub_table;

  sub_table = multivalued_entry_get (table, type);
  if (sub_table == NULL) {
    sub_table = multivalued_entry_new (type);
    multivalued_entry_add (table, sub_table);
  }

  string_htable_add (sub_table->entries, value);
}

/*
 * Return the sub hash_table corresponding to TYPE in TABLE
 * NULL if none
 */
static struct string_htable *
multivalued_entry_get_sub_table (struct hash_table_s * table,
				 const char * type)
{
  struct multivalued_entry * item;
  item = multivalued_entry_get (table, type);
  if (!item)
    return NULL;
  return item->entries;
}

/*
 * Return the entries of type TYPE and value VALUE in TABLE
 * if there is.  NULL otherwise
 */
static const char *
multivalued_entry_get_sub_item (struct hash_table_s * table,
				const char * type,
				const char * value)
{
  struct string_htable * sub_table;

  sub_table = multivalued_entry_get_sub_table (table, type);
  if (!sub_table)
    return NULL;
  return string_htable_get (sub_table, value);
}

struct hash_table_s *
multivalued_table_new (void)
{
  static struct hash_table_s * res;

  res = XMALLOC (hash_table, 1);
  hash_init (res, 8,
	     (hash_func_t) mv_key_hash_1,
	     (hash_func_t) mv_key_hash_2,
	     (hash_cmp_func_t) mv_key_hash_cmp);
  return res;
}

void
multivalued_table_free (struct hash_table_s * table)
{
  hash_free (table, (hash_map_func_t) multivalued_entry_free);
  free (table);
}

/************************************************************************
 * Multivalued hash tables						*
 ************************************************************************/
/*
 * Is this resource already recorded?
 */
int
exist_resource (a2ps_job * job, const char * key, const char * value)
{
  return (multivalued_entry_get_sub_item (job->status->needed_resources,
					  key, value)
	  != NULL);
}

/*
 * Used to record the requirements needed
 */
void
add_supplied_resource (a2ps_job * job, const char * key, const char * value)
{
  multivalued_entry_add_couple (job->status->supplied_resources, key, value);
}

#if 0
/*
 * Returned the needed resource if it is known,
 * NULL otherwise
 */
static const char *
supplied_resource_get (a2ps_job * job, const char * key, const char * value)
{
  return multivalued_entry_get_sub_item (job->status->supplied_resources,
					 key, value);
}
#endif

static void
multivalued_entry_dump (FILE * stream, int first,
			const char * fmt_first, const char * fmt_others,
			struct multivalued_entry * entry)
{
  char ** values;
  int i;

  /* Get all the values in a malloc'd storage.
   * We sort them because:
   * 1. it looks better,
   * 2. fewer sources of differences in regression tests */
  values = (char **) string_htable_dump_sorted (entry->entries);
  for (i = 0 ; values[i] ; i++)
    if (first) {
      fprintf (stream, fmt_first, entry->key, values[i]);
      first = false;
    } else {
      fprintf (stream, fmt_others, entry->key, values[i]);
    }

  /* Release mem */
  free (values);
}

/*
 * Specify the needed resources to the PS prologue
 */
void
dump_supplied_resources (FILE * stream, a2ps_job * job)
{
  int i;

  multivalued_entry ** list;
  list = ((multivalued_entry **)
	  hash_dump (job->status->supplied_resources, NULL, NULL));

  for (i = 0 ; list [i] ; i ++)
    /* i is used as a clue that it is the first */
    multivalued_entry_dump (stream, i == 0,
			    "%%%%DocumentSuppliedResources: %s %s\n",
			    "%%%%+ %s %s\n",
			    list [i]);
  free (list);
}

/*
 * Used to record the requirements needed
 */
void
add_needed_resource (a2ps_job * job, const char * key, const char * value)
{
  multivalued_entry_add_couple (job->status->needed_resources, key, value);
}

/*
 * Returned the needed resource if it is known,
 * NULL otherwise
 */
static const char *
needed_resource_get (a2ps_job * job, const char * key, const char * value)
{
  return multivalued_entry_get_sub_item (job->status->needed_resources,
					 key, value);
}

/*
 * Dump the needed resources _BUT_ the colors
 */
void
dump_needed_resources (FILE * stream, a2ps_job * job)
{
  int i;
  int first = 1;

  multivalued_entry ** list;
  list = ((multivalued_entry **)
	  hash_dump (job->status->needed_resources, NULL, NULL));

  for (i = 0 ; list [i] ; i ++)
    {
      /* Don't print the colors, because they have another section */
      if (strequ (list [i]-> key, "color")
	  /* nor files, since they are yet included */
	  || strequ (list [i]-> key, "file"))
	continue;

      multivalued_entry_dump (stream, first,
			      "%%%%DocumentNeededResources: %s %s\n",
			      "%%%%+ %s %s\n",
			      list [i]);
      first = false;
    }

  free (list);
}

/*
 * Colors used by the document
 */
void
add_process_color (a2ps_job * job, const char * value)
{
  multivalued_entry_add_couple (job->status->needed_resources,
				"color", value);
}

/*
 * Dump the needed colors
 */
void
dump_process_color (FILE * stream, a2ps_job * job)
{
  struct string_htable * color_table;

  color_table = multivalued_entry_get_sub_table (job->status->needed_resources,
						 "color");

  if (color_table)
    {
      int i;
      char ** colors = (char **) string_htable_dump_sorted (color_table);

      if (*colors != NULL) {
	fputs ("%%DocumentProcessColors: ", stream);
	for (i = 0 ; colors [i] ; i++)
	  fprintf (stream, "%s ", colors [i]);
	putc ('\n', stream);
      }
      free (colors);
    }
}

/************************************************************************/
/*	Handling the fonts						*/
/************************************************************************/
/*
 * We will need this fonts.
 * Depending whether it is part of the 13 standard fonts, consider
 * it to be a Needed or an IncludedResource.
 */
void
add_required_font (a2ps_job * job, const char * name)
{
  if (a2ps_printers_font_known_p (job->printers, name))
    {
      /* This is a regular ps fonts.
       * `Needed' it, and `Include" it.
       * We do it only if not yet done to avoid multiple %%Include */
      if (!needed_resource_get (job, "font", name)) {
	add_needed_resource (job, "font", name);
	output (job->divertion, "%%%%IncludeResource: font %s\n", name);
      }
    } else {
      /* This is not a known font.
       * `Supplie' it, and include it */
      add_supplied_resource (job, "font", name);
    }
}

/*
 * Return a malloc'd char ** in which are stored
 * the required_fonts (if there are, NULL otherwise)
 */
char **
required_fonts_get (a2ps_job * job)
{
  struct string_htable * font_table;
  font_table =
    multivalued_entry_get_sub_table (job->status->supplied_resources,
				     "font");

  if (font_table)
    return (char **) string_htable_dump_sorted (font_table);
  return NULL;
}

/*
 * Dump the setup code read in the various prologue (.pro and .ps)
 * files.  The hard part is that we don't want to dump too
 * many definitions of fonts, to avoid running out of memory on
 * too old PS level 1 printers.
 * Nevertheless, I still wait for somebody to tell me if this is
 * really needed (useful is sure, needed is not)
 */
void
dump_setup (FILE * stream, a2ps_job * job)
{
  output_dump (job->status->setup, stream);
}