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