rlcat.c 3.2 KB

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