Deleted Added
full compact
rlcat.c (119610) rlcat.c (157184)
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
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#ifdef HAVE_STDLIB_H
44# include <stdlib.h>
45#else
46extern void exit();
47#endif
48
43#ifndef errno
44extern 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
55extern int optind;
56extern char *optarg;
57
58static int stdcat();
59
60static char *progname;
61static int vflag;
62
63static void
64usage()
65{
66 fprintf (stderr, "%s: usage: %s [-vEVN] [filename]\n", progname, progname);
67}
68
69int
70main (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
123static int
124fcopy(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
144int
145stdcat (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}
49#ifndef errno
50extern int errno;
51#endif
52
53#if defined (READLINE_LIBRARY)
54# include "readline.h"
55# include "history.h"
56#else
57# include <readline/readline.h>
58# include <readline/history.h>
59#endif
60
61extern int optind;
62extern char *optarg;
63
64static int stdcat();
65
66static char *progname;
67static int vflag;
68
69static void
70usage()
71{
72 fprintf (stderr, "%s: usage: %s [-vEVN] [filename]\n", progname, progname);
73}
74
75int
76main (argc, argv)
77 int argc;
78 char **argv;
79{
80 char *temp;
81 int opt, Vflag, Nflag;
82
83 progname = strrchr(argv[0], '/');
84 if (progname == 0)
85 progname = argv[0];
86 else
87 progname++;
88
89 vflag = Vflag = Nflag = 0;
90 while ((opt = getopt(argc, argv, "vEVN")) != EOF)
91 {
92 switch (opt)
93 {
94 case 'v':
95 vflag = 1;
96 break;
97 case 'V':
98 Vflag = 1;
99 break;
100 case 'E':
101 Vflag = 0;
102 break;
103 case 'N':
104 Nflag = 1;
105 break;
106 default:
107 usage ();
108 exit (2);
109 }
110 }
111
112 argc -= optind;
113 argv += optind;
114
115 if (isatty(0) == 0 || argc || Nflag)
116 return stdcat(argc, argv);
117
118 rl_variable_bind ("editing-mode", Vflag ? "vi" : "emacs");
119 while (temp = readline (""))
120 {
121 if (*temp)
122 add_history (temp);
123 printf ("%s\n", temp);
124 }
125
126 return (ferror (stdout));
127}
128
129static int
130fcopy(fp)
131 FILE *fp;
132{
133 int c;
134 char *x;
135
136 while ((c = getc(fp)) != EOF)
137 {
138 if (vflag && isascii ((unsigned char)c) && isprint((unsigned char)c) == 0)
139 {
140 x = rl_untranslate_keyseq (c);
141 if (fputs (x, stdout) != 0)
142 return 1;
143 }
144 else if (putchar (c) == EOF)
145 return 1;
146 }
147 return (ferror (stdout));
148}
149
150int
151stdcat (argc, argv)
152 int argc;
153 char **argv;
154{
155 int i, fd, r;
156 char *s;
157 FILE *fp;
158
159 if (argc == 0)
160 return (fcopy(stdin));
161
162 for (i = 0, r = 1; i < argc; i++)
163 {
164 if (*argv[i] == '-' && argv[i][1] == 0)
165 fp = stdin;
166 else
167 {
168 fp = fopen (argv[i], "r");
169 if (fp == 0)
170 {
171 fprintf (stderr, "%s: %s: cannot open: %s\n", progname, argv[i], strerror(errno));
172 continue;
173 }
174 }
175 r = fcopy (fp);
176 if (fp != stdin)
177 fclose(fp);
178 }
179 return r;
180}