File: stream.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 (170 lines) | stat: -rw-r--r-- 3,637 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
/*
 * stream.c
 *
 * Uniform access to pipe and files
 *
 * Copyright (c) 1988-1993 Miguel Santana
 * Copyright (c) 1995-1999 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: stream.c,v 1.1.1.1.2.1 2007/12/29 01:58:24 mhatta Exp $
 */

#include "a2ps.h"
#include "stream.h"
#include "routines.h"
#include "message.h"
#include "quotearg.h"
#include <assert.h>

/* Open for reading. */

static inline struct stream *
_stream_ropen (const char * command, bool is_file)
{
  NEW (struct stream, res);

  res->is_file = is_file;
  if (!res->is_file)
    res->fp = xrpopen (command);
  else
    {
      if (!IS_EMPTY (command))
	res->fp = xrfopen (command);
      else
	res->fp  = stdin;
    }
  return res;
}

/*
 * Inline wrapper
 */
struct stream *
stream_ropen (const char * command, bool is_file)
{
  return _stream_ropen (command, is_file);
}

/*
 * Open for writing
 */
static inline struct stream *
_stream_wopen (const char * command, bool is_file,
	       enum backup_type backup_type)
{
  NEW (struct stream, res);

  res->is_file = is_file;
  if (!res->is_file)
    res->fp = xwpopen (command);
  else
    {
      if (!IS_EMPTY (command))
	res->fp = fopen_backup (command, backup_type);
      else
	res->fp = stdout;
    }
  return res;
}

/*
 * Inline wrapper
 */
struct stream *
stream_wopen (const char * command, bool is_file)
{
  return _stream_wopen (command, is_file, none);
}

struct stream *
stream_wopen_backup (const char * command, bool is_file,
		     enum backup_type backup_type)
{
  return _stream_wopen (command, is_file, backup_type);
}


/* Open a r or w stream depending in PERL_COMMAND.  PERL_COMMAND can
   be:
   - `> file', open a w-stream on FILE, and backup is asked.
   - `| cmd', open a w-pipe on CMD.
   - `cmd |', open a r-pipe on CMD.
   - otherwise, PERL_COMMAND is a file to read.

   Once the decoding done, NAME points to the first char of the file
   name.
*/

struct stream *
stream_perl_open_backup (const char * perl_command,
			 enum backup_type backup,
			 const char **name)
{
  char * cp;
  int len;

  assert (perl_command);
  message (msg_file,
	   (stderr, "perl-open (%s)\n", quotearg (perl_command)));

  *name = perl_command + strspn (perl_command, "\t >|");

  switch (*perl_command)
    {
    case '|':
      return _stream_wopen (*name, false, none);

    case '>':
      return _stream_wopen (*name, true, backup);

    default:
      /* Open for reading. */
      len = strlen (perl_command);
      switch (perl_command [len - 1])
	{
	case '|':
	  /* Read a pipe. */
	  cp = ALLOCA (char, len);
	  strncpy (cp, *name, len - 1);
	  return _stream_ropen (cp, false);

	default:
	  /* Read a file. */
	  return _stream_ropen (*name, true);
	}
    }
}

/*
 * Closes and frees.
 */
void
stream_close (struct stream * stream)
{
  if (stream->is_file)
    fclose (stream->fp);
  else
    pclose (stream->fp);
  free (stream);
}