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
|
/*
* generate.c -- Input files and pretty printing.
*
* Copyright (c) 1995-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 "main.h"
extern int isdir PARAMS ((const char *path));
/* Name of the file in which the tmp sample file is stored. */
char *sample_tmpname = NULL;
/*
* What kind of treatement should be applied
*/
enum style_kind_e
{
no_style, binary, sshparser, unprintable, delegate
};
static enum style_kind_e
string_to_style_kind (const char * string)
{
if (strequ (string, "binary"))
return binary;
else if (strequ (string, "UNPRINTABLE"))
return unprintable;
else if (strequ (string, "plain"))
return no_style;
else if (strequ (string, "delegate"))
return delegate;
return sshparser;
}
/************************************************************************/
/* The inputs */
/************************************************************************/
static buffer_t *
input_new (uchar * name)
{
NEW (buffer_t, res);
struct file_job * file_job;
struct stat statbuf; /* to get file modification time */
struct tm *tm;
a2ps_open_input_session (job, name);
file_job = CURRENT_FILE (job);
/* Retrieve file modification date and hour */
if (IS_EMPTY(name) || ustrequ (name, "-"))
{
file_job->is_stdin = true;
file_job->name = job->stdin_filename;
/* Create the buffer in charge of stdin */
buffer_stream_set (res, stdin, end_of_line);
/* Ask it to make a sample of the file. */
tempname_ensure (sample_tmpname);
buffer_sample_get (res, sample_tmpname);
}
else
{
FILE * input_stream;
/* This is a true file (not stdin) */
file_job->is_stdin = false;
/* Printing a file given by its path */
if (isdir ((char *) name))
{
error (0, 0, _("`%s' is a directory"), quotearg ((char *) name));
file_job->printable = false;
}
file_job->name = (uchar *) name;
if ((input_stream = fopen ((char *) name, "r")) == NULL)
{
error (0, errno,
_("cannot open file `%s'"), quotearg ((char *) name));
file_job->printable = false;
}
else if (stat ((char *) name, &statbuf) == -1)
{
error (0, errno, _("cannot get informations on file `%s'"),
quotearg ((char *) name));
file_job->printable = false;
}
else
{
time_t tim = statbuf.st_mtime;
tm = localtime (&tim);
memcpy (&(file_job->mod_tm), tm, sizeof (*tm));
}
/* Create the buffer in charge of the input stream */
buffer_stream_set (res, input_stream, end_of_line);
}
/*
* What should be done out of this file?
* Find the command associated to that file
* - UNPRINTABLE for unprintable
* - requested style sheet key
* - style sheet key
*/
if (!file_job->printable)
file_job->type = "UNPRINTABLE";
else if (!IS_EMPTY (style_request))
file_job->type = style_request;
else
file_job->type = get_command (file_job->name,
(sample_tmpname
? (uchar *) sample_tmpname
: file_job->name));
/* Remove the sample file */
if (sample_tmpname)
unlink (sample_tmpname);
return res;
}
static void
input_end (buffer_t * buffer)
{
if (buffer->stream && buffer->stream!= stdin)
fclose (buffer->stream);
buffer_release (buffer);
free (buffer);
a2ps_close_input_session (job);
}
/************************************************************************/
/* The producers */
/************************************************************************/
/*
* Make on message on what we did
*/
static void
msg_file_pages_printed (a2ps_job * Job, const char * stylename)
{
int sheets;
sheets = CURRENT_FILE (Job)->sheets;
if (Job->duplex)
sheets = (sheets + 1) / 2;
if (CURRENT_FILE (Job)->pages == 1)
/* 1 page on 1 sheet */
message (msg_report2,
(stderr, _("[%s (%s): 1 page on 1 sheet]\n"),
CURRENT_FILE (Job)->name,
stylename));
else if (sheets == 1)
/* several pages on 1 sheet */
message (msg_report2,
(stderr, _("[%s (%s): %d pages on 1 sheet]\n"),
CURRENT_FILE (Job)->name,
stylename,
CURRENT_FILE (Job)->pages));
else
/* several sheets */
message (msg_report2,
(stderr, _("[%s (%s): %d pages on %d sheets]\n"),
CURRENT_FILE (Job)->name,
stylename,
CURRENT_FILE (Job)-> pages,
sheets));
}
/*
* Total printed
*/
void
msg_job_pages_printed (a2ps_job * Job)
{
int sheets;
uchar *ucp;
sheets = Job->sheets;
if (Job->duplex)
sheets = (sheets + 1) / 2;
/* Make a nice message to tell where the output is sent */
ucp = a2ps_destination_to_string (Job);
/* Report the pages */
if (Job->pages == 1)
/* 1 page on 1 sheet "sent to the default printer" etc. */
message (msg_report1,
(stderr, _("[Total: 1 page on 1 sheet] %s\n"), ucp));
else if (sheets == 1)
/* several pages on 1 sheet */
message (msg_report1,
(stderr, _("[Total: %d pages on 1 sheet] %s\n"),
Job->pages, ucp));
else
/* several sheets */
message (msg_report1,
(stderr, _("[Total: %d pages on %d sheets] %s\n"),
Job-> pages, sheets, ucp));
/* Report the number of lines that were too long. */
if (macro_meta_sequence_get (Job, "cfg.wrapped")
&& Job->lines_folded)
{
if (Job->lines_folded == 1)
message (msg_report1,
(stderr, _("[1 line wrapped]\n")));
else
message (msg_report1,
(stderr, _("[%d lines wrapped]\n"),
Job->lines_folded));
}
free (ucp);
}
/*
* Total printed
*/
void
msg_nothing_printed (void)
{
message (msg_report1,
(stderr, _("[No output produced]\n")));
}
void
print_toc (const uchar * name, const uchar * value, int * native_jobs)
{
buffer_t toc_buffer;
uchar * toc_content;
/* Create a entry for the toc, as if it were a regular file */
a2ps_open_input_session (job, xustrdup (name));
/* But it is not a regular file: we need to be able to know
* that it is indeed a toc, so that --pages=toc can be honored */
CURRENT_FILE (job)->is_toc = true;
austrcpy (toc_content,
expand_user_string (job, CURRENT_FILE (job),
name, value));
buffer_string_set (&toc_buffer, toc_content, end_of_line);
/* We typeset it with PreScript */
ssh_print_postscript (job, &toc_buffer, get_style_sheet ("pre"));
(*native_jobs)++;
a2ps_close_input_session (job);
}
/*
* Called by the main loop.
* The file to print is the last of the darray job->jobs.
* Return true if was a success, false otherwise
*/
void
print (uchar * filename, int * native_jobs, int * delegated_jobs)
{
char buf[512];
struct delegation * contract = NULL;
struct style_sheet * sheet;
buffer_t * input_buffer;
enum style_kind_e style_kind;
struct file_job * file_job;
/*
* First, open that file and get info about it
* It may seem useless in some cases (e.g. the file will be delegated)
* but it ensures that readbility, and stat can be correctly done.
*/
input_buffer = input_new (filename);
/* Get the file_job _after_ it has been created by input_new */
file_job = CURRENT_FILE (job);
if (delegate_p
&& (contract =
get_subcontract (file_job->type,
output_format_to_key (job->output_format))))
style_kind = delegate;
else
style_kind = string_to_style_kind (file_job->type);
message (msg_file,
(stderr, "Getting ready to print file `%s', with command `%s'\n",
file_job->name, file_job->type));
/*
* Then do it
*/
switch (style_kind)
{
case delegate:
/* In ps generation, we must begin a new page */
page_flush (job);
sprintf (buf, _("%s, delegated to %s"),
file_job->type, contract->name);
if (subcontract (file_job, input_buffer, contract))
{
(*delegated_jobs)++;
msg_file_pages_printed (job, buf);
}
else
message (msg_report2, (stderr, _("[%s (%s): failed. Ignored]\n"),
file_job->name, buf));
break;
case unprintable:
/* The job will not be processed correctly */
message (msg_report2,
(stderr,
_("[%s (unprintable): ignored]\n"), file_job->name));
break;
case binary:
if (job->print_binaries)
goto plain_print;
message (msg_report2,
(stderr,
_("[%s (binary): ignored]\n"), file_job->name));
break;
case sshparser:
/* If highlight_level == none, don't */
if (highlight_level == 0)
goto plain_print;
sheet = get_style_sheet (file_job->type);
if (!sheet)
goto plain_print;
buffer_set_lower_case (input_buffer,
sheet->sensitiveness == case_insensitive);
ssh_print_postscript (job, input_buffer, sheet);
msg_file_pages_printed (job, (const char *) sheet->name);
(*native_jobs)++;
break;
plain_print:
case no_style:
plain_print_postscript (job, input_buffer);
msg_file_pages_printed (job, _("plain"));
(*native_jobs)++;
break;
}
input_end (input_buffer);
}
/*
* Called by the main loop.
* Almost like the above `PRINT' function, but just reports the guesses.
* This is a dirty hack of sth OK in 4.11
*/
void
guess (uchar * filename)
{
buffer_t * buffer;
struct file_job * file_job;
buffer = input_new (filename);
file_job = CURRENT_FILE (job);
printf ("[%s (%s)]\n", file_job->name, file_job->type);
/* Close the files, free, and close the input session */
if (buffer->stream && buffer->stream != stdin)
fclose (buffer->stream);
}
|