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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
/*
* delegate.c
*
* Handling the delegations
* Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
* Copyright (c) 1995, 96, 97, 98, 99 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.
*/
#include "a2ps.h"
#include "jobs.h"
#include "fjobs.h"
#include "output.h"
#include "delegate.h"
#include "message.h"
#include "routines.h"
#include "metaseq.h"
#include "dsc.h"
#include "buffer.h"
#include "lister.h"
#include "quotearg.h"
/* Priviledge access to job and delegations */
extern struct a2ps_job *job;
extern struct hash_table_s *delegation_table;
/************************************************************************/
/* Handling of the hash table of the delegations */
/************************************************************************/
/*
* The hashing is done on the contract, not the name (who
* cares the name of the contract!)
*/
static unsigned long
delegate_hash_1 (void const *key)
{
return_STRING_HASH_1 (((const struct delegation *) key)->contract);
}
static unsigned long
delegate_hash_2 (void const *key)
{
return_STRING_HASH_2 (((const struct delegation *) key)->contract);
}
static int
delegate_hash_cmp (void const *x, void const *y)
{
return_STRING_COMPARE (((const struct delegation *) x)->contract,
((const struct delegation *) y)->contract);
}
static int
delegate_hash_qcmp (const struct delegation **x,
const struct delegation **y)
{
return_STRING_COMPARE ((*x)->name, (*y)->name);
}
static size_t
delegate_name_len (struct delegation *delegation)
{
return strlen (delegation->name);
}
static void
delegate_name_fputs (struct delegation *delegation, FILE * stream)
{
fputs (delegation->name, stream);
}
static void
delegate_free (struct delegation *delegation)
{
free (delegation->name);
free (delegation->contract);
free (delegation->command);
free (delegation);
}
/*
* Create the table handling the subcontracts
*/
struct hash_table_s *
delegation_table_new (void)
{
NEW (struct hash_table_s, res);
hash_init (res, 8,
delegate_hash_1, delegate_hash_2, delegate_hash_cmp);
return res;
}
/*
* Free the whole table
*/
void
delegation_table_free (struct hash_table_s *table)
{
hash_free (table, (hash_map_func_t) delegate_free);
free (table);
}
/************************************************************************/
/* Use of the subcontracts */
/************************************************************************/
/*
* Read a (PS) file, and extract the NeededResources etc.
* To include in the whole file Prolog.
* Get the number of pages too.
*/
/*
* Read a line "Delegation: <CONTRACT_LINE>" in a config file
*/
#define error_if_null(_str_) \
if (_str_ == NULL) \
error_at_line (1, 0, filename, line, \
_("missing argument for `%s'"), quotearg (contract_line));
void
add_delegation (const char *filename, int line,
char *contract_line)
{
char *cp, *cp2;
struct delegation *contract;
contract = XMALLOC (struct delegation, 1);
/* Structure of the line:
<name of contract> <source type>:<destination type> <command> */
cp = strtok (contract_line, " \t\n");
error_if_null (cp);
contract->name = xstrdup (cp);
cp = strtok (NULL, " \t\n:");
error_if_null (cp);
cp2 = strtok (NULL, " \t\n");
error_if_null (cp2);
contract->contract = XMALLOC (char, strlen (cp) + strlen (cp2) + 2);
sprintf (contract->contract, "%s:%s", cp, cp2);
cp = strtok (NULL, "\n");
error_if_null (cp);
contract->command = xstrdup (cp + strspn (cp, "\t "));
/* Put it in the table */
hash_insert (delegation_table, contract);
}
/*
* Return the subcontract if there is, otherwise NULL
*/
struct delegation *
get_subcontract (const char *src_type, const char *dest_type)
{
struct delegation token;
token.contract = ALLOCA (char, strlen (src_type) + strlen (dest_type) + 2);
sprintf (token.contract, "%s:%s", src_type, dest_type);
return (struct delegation *) hash_find_item (delegation_table, &token);
}
/*
* Return the command associated a sub contract.
* if EVALUATE, return the evaluated command.
* The result is not malloc'ed, and must be used before
* any other call to expand_user_string.
*/
char *
get_delegate_command (struct delegation *contract,
struct file_job *file,
int evaluate)
{
if (evaluate)
return (char *) expand_user_string (job, file,
(const uchar *) "delegating command",
(const uchar *) contract->command);
else
return contract->command;
}
/*
* Subcontract FILE to CONTRACTOR
* This should produce a tmp file, then insert in the OUTPUT struture
* the routine to dump and remove it.
* Return true open success, false otherwise
*/
enum continuation_e
{
no_continuation, needed_resource
};
int
subcontract (struct file_job *fjob, buffer_t * buffer,
struct delegation *contractor)
{
char *command, *stdin_content_filename = NULL;
FILE *in_stream, *out_stream;
int lines_read = 0;
char buf[512];
/* Here we store the type of the last %%??Resource: tag we saw,
* to be ready to handle continuation (%%+ ) */
enum continuation_e continuation = no_continuation;
/* This is an awful kludge. I dunno how to do it nicely... The
problem is that a2ps can be fed by stdin, but delegations cannot.
So we first dump stdin into a temporary file. */
if (fjob->name == job->stdin_filename) /* not strcmp */
{
/* Dump the content of the buffer */
tempname_ensure (fjob->stdin_tmpname);
stdin_content_filename = fjob->stdin_tmpname;
buffer_save (buffer, stdin_content_filename);
/* We change the name of the file so that the correct file name
is used in the command (that of the temporary file containing
stdin). */
fjob->name = (uchar *) stdin_content_filename;
command = get_delegate_command (contractor, fjob, 1);
fjob->name = job->stdin_filename;
}
else
{
command = get_delegate_command (contractor, fjob, 1);
}
/* First, before it break :), say what you do */
message (msg_file,
(stderr, "Delegating `%s' to `%s' (%s)\n",
fjob->name, contractor->name, command));
/* Open a pipe from the delegation, and the temp file in which
* the result is stored */
tempname_ensure (fjob->delegation_tmpname);
out_stream = fopen (fjob->delegation_tmpname, "w");
if (!out_stream)
{
error (0, errno, _("cannot create file `%s'"),
quotearg (fjob->delegation_tmpname));
return false;
}
in_stream = popen (command, "r");
if (!in_stream)
{
fclose (out_stream);
error (0, errno, _("cannot open a pipe on `%s'"),
quotearg (command));
return false;
}
/* Make the file know its first page/sheet */
file_job_synchronize_sheets (job);
file_job_synchronize_pages (job);
/* Now, read the file, update the PS info, and store the result in
* out_stream */
while (fgets (buf, sizeof (buf), in_stream))
{
/* This is not exactely the number of lines,
* but anyway it is only used to be sure something was read,
* to track the failure of a delegation */
lines_read++;
#define PAGE_TAG "%%Page: "
if (strprefix (PAGE_TAG, buf))
{
/* We suppose that it has respected the number of virtual
* pages per sheet */
job->pages += job->rows * job->columns;
job->sheets++;
file_job_synchronize_sheets (job);
file_job_synchronize_pages (job);
}
#define NEEDED_RES_TAG "%%DocumentNeededResources: "
else if (strprefix (NEEDED_RES_TAG, buf))
{
/* The needed resources must be included too.
* Take care of the %%+ continuation tag */
char *value, *res, *buf_copy;
continuation = needed_resource;
astrcpy (buf_copy, buf + strlen (NEEDED_RES_TAG));
res = strtok (buf_copy, " \n\t");
/* This while saves us from a special case of
* %%DocumentNeededResources: (atend)
*/
while ((value = strtok (NULL, " \n\t")))
add_needed_resource (job, res, value);
}
#define CONTINUATION_TAG "%%+ "
else if (strprefix (CONTINUATION_TAG, buf))
{
char *value, *res, *buf_copy;
astrcpy (buf_copy, buf + strlen (CONTINUATION_TAG));
res = strtok (buf_copy, " \n\t");
while ((value = strtok (NULL, " \n\t")))
switch (continuation)
{
case needed_resource:
add_needed_resource (job, res, value);
break;
default:
break;
}
}
/* The content should be left untouched */
fputs (buf, out_stream);
}
pclose (in_stream);
fclose (out_stream);
/* If a temporary file was created to deal with stdin, unlink it. */
if (stdin_content_filename)
unlink (stdin_content_filename);
/* FIXME: This is a trial to see when there is an error */
if (lines_read == 0)
return false;
/* This one must not be cut by the page selection */
{
int saved_redirection_of_output;
saved_redirection_of_output = output_is_to_void (job->divertion);
output_to_void (job->divertion, false);
/* Protect the rest of the file and give sane environment */
output (job->divertion, "BeginInclude\n");
output (job->divertion, "%%%%BeginDocument: %s\n", fjob->name);
output_delayed_routine (job->divertion,
(delayed_routine_t) stream_dump,
(void *) fjob->delegation_tmpname);
/* remove the file after its use */
output_delayed_routine (job->divertion,
(delayed_routine_t) unlink2,
(void *) fjob->delegation_tmpname);
output (job->divertion, "%%%%EndDocument\n");
output (job->divertion, "EndInclude\n");
output_to_void (job->divertion, saved_redirection_of_output);
}
/* The pages are no longer ordered. Respect DSC */
job->status->page_are_ordered = false;
return true;
}
/************************************************************************/
/* "Visible" interface of the subcontracts */
/************************************************************************/
/*
* Print a single contract on STREAM
*/
static void
dump_contract (FILE * stream, struct delegation *contract)
{
char *cp, *cp2;
cp = xstrdup (contract->contract);
cp = strtok (cp, ":");
cp2 = strtok (NULL, ":");
/* E.g.: Delegation `PsNup', from ps to ps */
fprintf (stream, _("Delegation `%s', from %s to %s\n"),
contract->name, cp, cp2);
fprintf (stream, "\t%s\n", contract->command);
free (cp);
}
/*
* List the subcontracts for --list-subcontracts
*/
void
delegations_list_long (struct hash_table_s *contracts,
FILE * stream)
{
int i;
struct delegation **ordered_contracts;
ordered_contracts =
((struct delegation **)
hash_dump (contracts, NULL, (qsort_cmp_t) delegate_hash_qcmp));
fputs (_("Applications configured for delegation"), stream);
putc ('\n', stream);
for (i = 0; ordered_contracts[i]; i++)
dump_contract (stream, ordered_contracts[i]);
putc ('\n', stream);
free (ordered_contracts);
}
/*
* For --list-features
*/
void
delegations_list_short (struct hash_table_s *contracts,
FILE * stream)
{
struct delegation **ordered_contracts;
ordered_contracts =
((struct delegation **)
hash_dump (contracts, NULL, (qsort_cmp_t) delegate_hash_qcmp));
fputs (_("Applications configured for delegation"), stream);
putc ('\n', stream);
lister_fprint_vertical (NULL, stream,
(void *) ordered_contracts, (size_t) -1,
(lister_width_t) delegate_name_len,
(lister_print_t) delegate_name_fputs);
free (ordered_contracts);
}
|