~ubuntu-branches/ubuntu/trusty/bash/trusty-security

« back to all changes in this revision

Viewing changes to lib/readline/examples/rlcat.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2014-03-03 22:52:05 UTC
  • mfrom: (1.3.5) (2.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20140303225205-87ltrt5kspeq0g1b
Tags: 4.3-1ubuntu1
* Merge with Debian; remaining changes:
  - skel.bashrc:
    - Run lesspipe.
    - Enable ls aliases.
    - Set options in ll alias to -alF.
    - Define an alert alias.
    - Enabled colored grep aliases.
  - etc.bash.bashrc:
    - Add sudo hint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * rlcat - cat(1) using readline
 
3
 *
 
4
 * usage: rlcat
 
5
 */
 
6
 
 
7
/* Copyright (C) 1987-2009 Free Software Foundation, Inc.
 
8
 
 
9
   This file is part of the GNU Readline Library (Readline), a library for
 
10
   reading lines of text with interactive input and history editing.
 
11
 
 
12
   Readline is free software: you can redistribute it and/or modify
 
13
   it under the terms of the GNU General Public License as published by
 
14
   the Free Software Foundation, either version 3 of the License, or
 
15
   (at your option) any later version.
 
16
 
 
17
   Readline is distributed in the hope that it will be useful,
 
18
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
   GNU General Public License for more details.
 
21
 
 
22
   You should have received a copy of the GNU General Public License
 
23
   along with Readline.  If not, see <http://www.gnu.org/licenses/>.
 
24
*/
 
25
 
 
26
#if defined (HAVE_CONFIG_H)
 
27
#  include <config.h>
 
28
#endif
 
29
 
 
30
#ifdef HAVE_UNISTD_H
 
31
#  include <unistd.h>
 
32
#endif
 
33
 
 
34
#include <sys/types.h>
 
35
#include "posixstat.h"
 
36
 
 
37
#include <stdio.h>
 
38
#include <ctype.h>
 
39
#include <string.h>
 
40
#include <errno.h>
 
41
 
 
42
#ifdef HAVE_STDLIB_H
 
43
#  include <stdlib.h>
 
44
#else 
 
45
extern void exit();
 
46
#endif
 
47
 
 
48
#ifndef errno
 
49
extern int errno;
 
50
#endif
 
51
 
 
52
#if defined (READLINE_LIBRARY)
 
53
#  include "readline.h"
 
54
#  include "history.h"
 
55
#else
 
56
#  include <readline/readline.h>
 
57
#  include <readline/history.h>
 
58
#endif
 
59
 
 
60
extern int optind;
 
61
extern char *optarg;
 
62
 
 
63
static int stdcat();
 
64
 
 
65
static char *progname;
 
66
static int vflag;
 
67
 
 
68
static void
 
69
usage()
 
70
{
 
71
  fprintf (stderr, "%s: usage: %s [-vEVN] [filename]\n", progname, progname);
 
72
}
 
73
 
 
74
int
 
75
main (argc, argv)
 
76
     int argc;
 
77
     char **argv;
 
78
{
 
79
  char *temp;
 
80
  int opt, Vflag, Nflag;
 
81
 
 
82
  progname = strrchr(argv[0], '/');
 
83
  if (progname == 0)
 
84
    progname = argv[0];
 
85
  else
 
86
    progname++;
 
87
 
 
88
  vflag = Vflag = Nflag = 0;
 
89
  while ((opt = getopt(argc, argv, "vEVN")) != EOF)
 
90
    {
 
91
      switch (opt)
 
92
        {
 
93
        case 'v':
 
94
          vflag = 1;
 
95
          break;
 
96
        case 'V':
 
97
          Vflag = 1;
 
98
          break;
 
99
        case 'E':
 
100
          Vflag = 0;
 
101
          break;
 
102
        case 'N':
 
103
          Nflag = 1;
 
104
          break;
 
105
        default:
 
106
          usage ();
 
107
          exit (2);
 
108
        }
 
109
    }
 
110
 
 
111
  argc -= optind;
 
112
  argv += optind;
 
113
 
 
114
  if (isatty(0) == 0 || argc || Nflag)
 
115
    return stdcat(argc, argv);
 
116
 
 
117
  rl_variable_bind ("editing-mode", Vflag ? "vi" : "emacs");
 
118
  while (temp = readline (""))
 
119
    {
 
120
      if (*temp)
 
121
        add_history (temp);
 
122
      printf ("%s\n", temp);
 
123
    }
 
124
 
 
125
  return (ferror (stdout));
 
126
}
 
127
 
 
128
static int
 
129
fcopy(fp)
 
130
     FILE *fp;
 
131
{
 
132
  int c;
 
133
  char *x;
 
134
 
 
135
  while ((c = getc(fp)) != EOF)
 
136
    {
 
137
      if (vflag && isascii ((unsigned char)c) && isprint((unsigned char)c) == 0)
 
138
        {
 
139
          x = rl_untranslate_keyseq (c);
 
140
          if (fputs (x, stdout) != 0)
 
141
            return 1;
 
142
        }
 
143
      else if (putchar (c) == EOF)
 
144
        return 1;
 
145
    }
 
146
  return (ferror (stdout));
 
147
}
 
148
 
 
149
int
 
150
stdcat (argc, argv)
 
151
     int argc;
 
152
     char **argv;
 
153
{
 
154
  int  i, fd, r;
 
155
  char *s;
 
156
  FILE *fp;
 
157
 
 
158
  if (argc == 0)
 
159
    return (fcopy(stdin));
 
160
 
 
161
  for (i = 0, r = 1; i < argc; i++)
 
162
    {
 
163
      if (*argv[i] == '-' && argv[i][1] == 0)
 
164
        fp = stdin;
 
165
      else
 
166
        {
 
167
          fp = fopen (argv[i], "r");
 
168
          if (fp == 0)
 
169
            {
 
170
              fprintf (stderr, "%s: %s: cannot open: %s\n", progname, argv[i], strerror(errno));
 
171
              continue;
 
172
            }
 
173
        }
 
174
      r = fcopy (fp);
 
175
      if (fp != stdin)
 
176
        fclose(fp);
 
177
    }
 
178
  return r;
 
179
}