source: mainline/uspace/app/bdsh/cmds/modules/cat/cat.c@ 9bdf1f2a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9bdf1f2a was 7c014d1, checked in by Martin Decky <martin@…>, 14 years ago

console and framebuffer server rewrite

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/*
2 * Copyright (c) 2008 Tim Post
3 * Copyright (c) 2011, Martin Sucha
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <unistd.h>
33#include <getopt.h>
34#include <str.h>
35#include <fcntl.h>
36#include <io/console.h>
37#include <io/color.h>
38#include <io/style.h>
39#include <io/keycode.h>
40#include <errno.h>
41#include <vfs/vfs.h>
42#include <assert.h>
43
44#include "config.h"
45#include "util.h"
46#include "errors.h"
47#include "entry.h"
48#include "cat.h"
49#include "cmds.h"
50
51static const char *cmdname = "cat";
52#define CAT_VERSION "0.0.1"
53#define CAT_DEFAULT_BUFLEN 1024
54
55static const char *cat_oops = "That option is not yet supported\n";
56static const char *hexchars = "0123456789abcdef";
57
58static bool paging_enabled = false;
59static size_t chars_remaining = 0;
60static size_t lines_remaining = 0;
61static sysarg_t console_cols = 0;
62static sysarg_t console_rows = 0;
63static bool should_quit = false;
64
65static console_ctrl_t *console = NULL;
66
67static struct option const long_options[] = {
68 { "help", no_argument, 0, 'h' },
69 { "version", no_argument, 0, 'v' },
70 { "head", required_argument, 0, 'H' },
71 { "tail", required_argument, 0, 't' },
72 { "buffer", required_argument, 0, 'b' },
73 { "more", no_argument, 0, 'm' },
74 { "hex", no_argument, 0, 'x' },
75 { 0, 0, 0, 0 }
76};
77
78/* Dispays help for cat in various levels */
79void help_cmd_cat(unsigned int level)
80{
81 if (level == HELP_SHORT) {
82 printf("`%s' shows the contents of files\n", cmdname);
83 } else {
84 help_cmd_cat(HELP_SHORT);
85 printf(
86 "Usage: %s [options] <file1> [file2] [...]\n"
87 "Options:\n"
88 " -h, --help A short option summary\n"
89 " -v, --version Print version information and exit\n"
90 " -H, --head ## Print only the first ## bytes\n"
91 " -t, --tail ## Print only the last ## bytes\n"
92 " -b, --buffer ## Set the read buffer size to ##\n"
93 " -m, --more Pause after each screen full\n"
94 " -x, --hex Print bytes as hex values\n"
95 "Currently, %s is under development, some options don't work.\n",
96 cmdname, cmdname);
97 }
98
99 return;
100}
101
102static void waitprompt()
103{
104 console_set_pos(console, 0, console_rows-1);
105 console_set_color(console, COLOR_WHITE, COLOR_BLUE, 0);
106
107 printf("ENTER/SPACE/PAGE DOWN - next page, "
108 "ESC/Q - quit, C - continue unpaged");
109 fflush(stdout);
110
111 console_set_style(console, STYLE_NORMAL);
112}
113
114static void waitkey()
115{
116 kbd_event_t ev;
117
118 while (true) {
119 if (!console_get_kbd_event(console, &ev)) {
120 return;
121 }
122 if (ev.type == KEY_PRESS) {
123 if (ev.key == KC_ESCAPE || ev.key == KC_Q) {
124 should_quit = true;
125 return;
126 }
127 if (ev.key == KC_C) {
128 paging_enabled = false;
129 return;
130 }
131 if (ev.key == KC_ENTER || ev.key == KC_SPACE ||
132 ev.key == KC_PAGE_DOWN) {
133 return;
134 }
135 }
136 }
137 assert(false);
138}
139
140static void newpage()
141{
142 console_clear(console);
143 chars_remaining = console_cols;
144 lines_remaining = console_rows - 1;
145}
146
147static void paged_char(wchar_t c)
148{
149 putchar(c);
150 if (paging_enabled) {
151 chars_remaining--;
152 if (c == '\n' || chars_remaining == 0) {
153 chars_remaining = console_cols;
154 lines_remaining--;
155 }
156 if (lines_remaining == 0) {
157 fflush(stdout);
158 waitprompt();
159 waitkey();
160 newpage();
161 }
162 }
163}
164
165static unsigned int cat_file(const char *fname, size_t blen, bool hex)
166{
167 int fd, bytes = 0, count = 0, reads = 0;
168 char *buff = NULL;
169 int i;
170 size_t offset = 0;
171
172 fd = open(fname, O_RDONLY);
173 if (fd < 0) {
174 printf("Unable to open %s\n", fname);
175 return 1;
176 }
177
178 if (NULL == (buff = (char *) malloc(blen + 1))) {
179 close(fd);
180 printf("Unable to allocate enough memory to read %s\n",
181 fname);
182 return 1;
183 }
184
185 do {
186 bytes = read(fd, buff, blen);
187 if (bytes > 0) {
188 count += bytes;
189 buff[bytes] = '\0';
190 offset = 0;
191 for (i = 0; i < bytes && !should_quit; i++) {
192 if (hex) {
193 paged_char(hexchars[((uint8_t)buff[i])/16]);
194 paged_char(hexchars[((uint8_t)buff[i])%16]);
195 }
196 else {
197 wchar_t c = str_decode(buff, &offset, bytes);
198 if (c == 0) {
199 /* Reached end of string */
200 break;
201 }
202 paged_char(c);
203 }
204
205 }
206 reads++;
207 }
208 } while (bytes > 0 && !should_quit);
209
210 close(fd);
211 if (bytes == -1) {
212 printf("Error reading %s\n", fname);
213 free(buff);
214 return 1;
215 }
216
217 free(buff);
218
219 return 0;
220}
221
222/* Main entry point for cat, accepts an array of arguments */
223int cmd_cat(char **argv)
224{
225 unsigned int argc, i, ret = 0, buffer = 0;
226 int c, opt_ind;
227 bool hex = false;
228 bool more = false;
229 sysarg_t rows, cols;
230 int rc;
231
232 /*
233 * reset global state
234 * TODO: move to structure?
235 */
236 paging_enabled = false;
237 chars_remaining = 0;
238 lines_remaining = 0;
239 console_cols = 0;
240 console_rows = 0;
241 should_quit = false;
242 console = console_init(stdin, stdout);
243
244 argc = cli_count_args(argv);
245
246 for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
247 c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind);
248 switch (c) {
249 case 'h':
250 help_cmd_cat(HELP_LONG);
251 return CMD_SUCCESS;
252 case 'v':
253 printf("%s\n", CAT_VERSION);
254 return CMD_SUCCESS;
255 case 'H':
256 printf("%s", cat_oops);
257 return CMD_FAILURE;
258 case 't':
259 printf("%s", cat_oops);
260 return CMD_FAILURE;
261 case 'b':
262 printf("%s", cat_oops);
263 break;
264 case 'm':
265 more = true;
266 break;
267 case 'x':
268 hex = true;
269 break;
270 }
271 }
272
273 argc -= optind;
274
275 if (argc < 1) {
276 printf("%s - incorrect number of arguments. Try `%s --help'\n",
277 cmdname, cmdname);
278 return CMD_FAILURE;
279 }
280
281 if (buffer <= 0)
282 buffer = CAT_DEFAULT_BUFLEN;
283
284 if (more) {
285 rc = console_get_size(console, &cols, &rows);
286 if (rc != EOK) {
287 printf("%s - cannot get console size\n", cmdname);
288 return CMD_FAILURE;
289 }
290 console_cols = cols;
291 console_rows = rows;
292 paging_enabled = true;
293 newpage();
294 }
295
296 for (i = optind; argv[i] != NULL && !should_quit; i++)
297 ret += cat_file(argv[i], buffer, hex);
298
299 if (ret)
300 return CMD_FAILURE;
301 else
302 return CMD_SUCCESS;
303}
304
Note: See TracBrowser for help on using the repository browser.