1119610Sache/*
2119610Sache * rlcat - cat(1) using readline
3119610Sache *
4119610Sache * usage: rlcat
5119610Sache */
6119610Sache
7119610Sache/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
8119610Sache
9119610Sache   This file is part of the GNU Readline Library, a library for
10119610Sache   reading lines of text with interactive input and history editing.
11119610Sache
12119610Sache   The GNU Readline Library is free software; you can redistribute it
13119610Sache   and/or modify it under the terms of the GNU General Public License
14119610Sache   as published by the Free Software Foundation; either version 2, or
15119610Sache   (at your option) any later version.
16119610Sache
17119610Sache   The GNU Readline Library is distributed in the hope that it will be
18119610Sache   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19119610Sache   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20119610Sache   GNU General Public License for more details.
21119610Sache
22119610Sache   The GNU General Public License is often shipped with GNU software, and
23119610Sache   is generally kept in a file called COPYING or LICENSE.  If you do not
24119610Sache   have a copy of the license, write to the Free Software Foundation,
25119610Sache   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
26119610Sache
27119610Sache#if defined (HAVE_CONFIG_H)
28119610Sache#  include <config.h>
29119610Sache#endif
30119610Sache
31119610Sache#ifdef HAVE_UNISTD_H
32119610Sache#  include <unistd.h>
33119610Sache#endif
34119610Sache
35119610Sache#include <sys/types.h>
36119610Sache#include "posixstat.h"
37119610Sache
38119610Sache#include <stdio.h>
39119610Sache#include <ctype.h>
40119610Sache#include <string.h>
41119610Sache#include <errno.h>
42119610Sache
43157184Sache#ifdef HAVE_STDLIB_H
44157184Sache#  include <stdlib.h>
45157184Sache#else
46157184Sacheextern void exit();
47157184Sache#endif
48157184Sache
49119610Sache#ifndef errno
50119610Sacheextern int errno;
51119610Sache#endif
52119610Sache
53119610Sache#if defined (READLINE_LIBRARY)
54119610Sache#  include "readline.h"
55119610Sache#  include "history.h"
56119610Sache#else
57119610Sache#  include <readline/readline.h>
58119610Sache#  include <readline/history.h>
59119610Sache#endif
60119610Sache
61119610Sacheextern int optind;
62119610Sacheextern char *optarg;
63119610Sache
64119610Sachestatic int stdcat();
65119610Sache
66119610Sachestatic char *progname;
67119610Sachestatic int vflag;
68119610Sache
69119610Sachestatic void
70119610Sacheusage()
71119610Sache{
72119610Sache  fprintf (stderr, "%s: usage: %s [-vEVN] [filename]\n", progname, progname);
73119610Sache}
74119610Sache
75119610Sacheint
76119610Sachemain (argc, argv)
77119610Sache     int argc;
78119610Sache     char **argv;
79119610Sache{
80119610Sache  char *temp;
81119610Sache  int opt, Vflag, Nflag;
82119610Sache
83119610Sache  progname = strrchr(argv[0], '/');
84119610Sache  if (progname == 0)
85119610Sache    progname = argv[0];
86119610Sache  else
87119610Sache    progname++;
88119610Sache
89119610Sache  vflag = Vflag = Nflag = 0;
90119610Sache  while ((opt = getopt(argc, argv, "vEVN")) != EOF)
91119610Sache    {
92119610Sache      switch (opt)
93119610Sache	{
94119610Sache	case 'v':
95119610Sache	  vflag = 1;
96119610Sache	  break;
97119610Sache	case 'V':
98119610Sache	  Vflag = 1;
99119610Sache	  break;
100119610Sache	case 'E':
101119610Sache	  Vflag = 0;
102119610Sache	  break;
103119610Sache	case 'N':
104119610Sache	  Nflag = 1;
105119610Sache	  break;
106119610Sache	default:
107119610Sache	  usage ();
108119610Sache	  exit (2);
109119610Sache	}
110119610Sache    }
111119610Sache
112119610Sache  argc -= optind;
113119610Sache  argv += optind;
114119610Sache
115119610Sache  if (isatty(0) == 0 || argc || Nflag)
116119610Sache    return stdcat(argc, argv);
117119610Sache
118119610Sache  rl_variable_bind ("editing-mode", Vflag ? "vi" : "emacs");
119119610Sache  while (temp = readline (""))
120119610Sache    {
121119610Sache      if (*temp)
122119610Sache        add_history (temp);
123119610Sache      printf ("%s\n", temp);
124119610Sache    }
125119610Sache
126119610Sache  return (ferror (stdout));
127119610Sache}
128119610Sache
129119610Sachestatic int
130119610Sachefcopy(fp)
131119610Sache     FILE *fp;
132119610Sache{
133119610Sache  int c;
134119610Sache  char *x;
135119610Sache
136119610Sache  while ((c = getc(fp)) != EOF)
137119610Sache    {
138119610Sache      if (vflag && isascii ((unsigned char)c) && isprint((unsigned char)c) == 0)
139119610Sache	{
140119610Sache	  x = rl_untranslate_keyseq (c);
141119610Sache	  if (fputs (x, stdout) != 0)
142119610Sache	    return 1;
143119610Sache	}
144119610Sache      else if (putchar (c) == EOF)
145119610Sache        return 1;
146119610Sache    }
147119610Sache  return (ferror (stdout));
148119610Sache}
149119610Sache
150119610Sacheint
151119610Sachestdcat (argc, argv)
152119610Sache     int argc;
153119610Sache     char **argv;
154119610Sache{
155119610Sache  int  i, fd, r;
156119610Sache  char *s;
157119610Sache  FILE *fp;
158119610Sache
159119610Sache  if (argc == 0)
160119610Sache    return (fcopy(stdin));
161119610Sache
162119610Sache  for (i = 0, r = 1; i < argc; i++)
163119610Sache    {
164119610Sache      if (*argv[i] == '-' && argv[i][1] == 0)
165119610Sache	fp = stdin;
166119610Sache      else
167119610Sache	{
168119610Sache	  fp = fopen (argv[i], "r");
169119610Sache	  if (fp == 0)
170119610Sache	    {
171119610Sache	      fprintf (stderr, "%s: %s: cannot open: %s\n", progname, argv[i], strerror(errno));
172119610Sache	      continue;
173119610Sache	    }
174119610Sache        }
175119610Sache      r = fcopy (fp);
176119610Sache      if (fp != stdin)
177119610Sache	fclose(fp);
178119610Sache    }
179119610Sache  return r;
180119610Sache}
181