summaryrefslogtreecommitdiff
path: root/src/generate.c
blob: 99856bec05cd08fc4e9c92e1794bbaf4bc48ec6c (plain)
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
/* generate.c - input files and pretty printing
   Copyright 1995-2017 Free Software Foundation, Inc.

   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; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */

#include <config.h>

#include "main.h"
#include "isdir.h"

/* 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 (STREQ (string, "binary"))
    return binary;
  else if (STREQ (string, "UNPRINTABLE"))
    return unprintable;
  else if (STREQ (string, "plain"))
    return no_style;
  else if (STREQ (string, "delegate"))
    return delegate;
  return sshparser;
}
/************************************************************************/
/*			The inputs					*/
/************************************************************************/
static buffer_t *
input_new (char * name)
{
  buffer_t * res = XMALLOC (buffer_t);
  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) || STREQ (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 = name;
      if ((input_stream = fopen (name, "r")) == NULL)
	{
	  error (0, errno,
		 _("cannot open file `%s'"), quotearg (name));
	  file_job->printable = false;
	}
      else if (stat (name, &statbuf) == -1)
	{
	  error (0, errno, _("cannot get informations on file `%s'"),
		 quotearg (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
				   ? 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);

  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)
{
  size_t 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): %zu pages on 1 sheet]\n"),
	      CURRENT_FILE (Job)->name,
	      stylename,
	      CURRENT_FILE (Job)->pages));
  else
    /* several sheets */
    message (msg_report2,
	     (stderr, _("[%s (%s): %zu pages on %zu sheets]\n"),
	      CURRENT_FILE (Job)->name,
	      stylename,
	      CURRENT_FILE (Job)-> pages,
	      sheets));
}

/*
 * Total printed
 */
void
msg_job_pages_printed (a2ps_job * Job)
{
  size_t sheets;
  char *cp;

  sheets = Job->sheets;
  if (Job->duplex)
    sheets = (sheets + 1) / 2;

  /* Make a nice message to tell where the output is sent */
  cp = 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"), cp));
  else if (sheets == 1)
    /* several pages on 1 sheet */
    message (msg_report1,
	     (stderr, _("[Total: %zu pages on 1 sheet] %s\n"),
	      Job->pages, cp));
  else
    /* several sheets */
    message (msg_report1,
	     (stderr, _("[Total: %zu pages on %zu sheets] %s\n"),
	      Job->pages, sheets, cp));

  /* 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, _("[%zu lines wrapped]\n"),
		  Job->lines_folded));
    }
}
/*
 * Total printed
 */
void
msg_nothing_printed (void)
{
  message (msg_report1,
	   (stderr, _("[No output produced]\n")));
}

void
print_toc (const char * name, const char * value, int * native_jobs)
{
  buffer_t toc_buffer;
  char * toc_content;

  /* Create a entry for the toc, as if it were a regular file */
  a2ps_open_input_session (job, xstrdup (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;

  astrcpy (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 (char * 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 (char * 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. */
  if (buffer->stream && buffer->stream != stdin)
    fclose (buffer->stream);
}