fixed bash/dash/sh issue (Ubuntu)
[zpugcc/jano.git] / toolchain / gdb / readline / examples / rlcat.c
blob176b9f44b68e7c85b771ab5e58dff5f30765d692
1 /*
2 * rlcat - cat(1) using readline
4 * usage: rlcat
5 */
7 /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
9 This file is part of the GNU Readline Library, a library for
10 reading lines of text with interactive input and history editing.
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.
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.
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. */
27 #if defined (HAVE_CONFIG_H)
28 # include <config.h>
29 #endif
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #include <sys/types.h>
36 #include "posixstat.h"
38 #include <stdio.h>
39 #include <ctype.h>
40 #include <string.h>
41 #include <errno.h>
43 #ifndef errno
44 extern int errno;
45 #endif
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
55 extern int optind;
56 extern char *optarg;
58 static int stdcat();
60 static char *progname;
61 static int vflag;
63 static void
64 usage()
66 fprintf (stderr, "%s: usage: %s [-vEVN] [filename]\n", progname, progname);
69 int
70 main (argc, argv)
71 int argc;
72 char **argv;
74 char *temp;
75 int opt, Vflag, Nflag;
77 progname = strrchr(argv[0], '/');
78 if (progname == 0)
79 progname = argv[0];
80 else
81 progname++;
83 vflag = Vflag = Nflag = 0;
84 while ((opt = getopt(argc, argv, "vEVN")) != EOF)
86 switch (opt)
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);
106 argc -= optind;
107 argv += optind;
109 if (isatty(0) == 0 || argc || Nflag)
110 return stdcat(argc, argv);
112 rl_variable_bind ("editing-mode", Vflag ? "vi" : "emacs");
113 while (temp = readline (""))
115 if (*temp)
116 add_history (temp);
117 printf ("%s\n", temp);
120 return (ferror (stdout));
123 static int
124 fcopy(fp)
125 FILE *fp;
127 int c;
128 char *x;
130 while ((c = getc(fp)) != EOF)
132 if (vflag && isascii ((unsigned char)c) && isprint((unsigned char)c) == 0)
134 x = rl_untranslate_keyseq (c);
135 if (fputs (x, stdout) != 0)
136 return 1;
138 else if (putchar (c) == EOF)
139 return 1;
141 return (ferror (stdout));
145 stdcat (argc, argv)
146 int argc;
147 char **argv;
149 int i, fd, r;
150 char *s;
151 FILE *fp;
153 if (argc == 0)
154 return (fcopy(stdin));
156 for (i = 0, r = 1; i < argc; i++)
158 if (*argv[i] == '-' && argv[i][1] == 0)
159 fp = stdin;
160 else
162 fp = fopen (argv[i], "r");
163 if (fp == 0)
165 fprintf (stderr, "%s: %s: cannot open: %s\n", progname, argv[i], strerror(errno));
166 continue;
169 r = fcopy (fp);
170 if (fp != stdin)
171 fclose(fp);
173 return r;