Changeset e7ffdd3 in mainline


Ignore:
Timestamp:
2011-03-22T20:02:28Z (13 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ac897e8
Parents:
5e3eea10
Message:

Implement -m option for cat and add -x option to convert bytes to hex

Location:
uspace/app/bdsh/cmds/modules/cat
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/modules/cat/cat.c

    r5e3eea10 re7ffdd3  
    11/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
     2 * Copyright (c) 2011, Martin Sucha
    23 * All rights reserved.
    34 *
     
    3536#include <str.h>
    3637#include <fcntl.h>
     38#include <io/console.h>
     39#include <io/color.h>
     40#include <io/style.h>
     41#include <errno.h>
     42#include <vfs/vfs.h>
     43#include <assert.h>
    3744
    3845#include "config.h"
     
    4855
    4956static const char *cat_oops = "That option is not yet supported\n";
     57static const char *hexchars = "0123456789abcdef";
     58
     59static size_t chars_per_screen = 0;
     60static size_t chars_remaining = 0;
    5061
    5162static struct option const long_options[] = {
     
    5667        { "buffer", required_argument, 0, 'b' },
    5768        { "more", no_argument, 0, 'm' },
     69        { "hex", no_argument, 0, 'x' },
    5870        { 0, 0, 0, 0 }
    5971};
     
    7587                "  -b, --buffer ##  Set the read buffer size to ##\n"
    7688                "  -m, --more       Pause after each screen full\n"
     89                "  -x, --hex        Print bytes as hex values\n"
    7790                "Currently, %s is under development, some options don't work.\n",
    7891                cmdname, cmdname);
     
    8295}
    8396
    84 static unsigned int cat_file(const char *fname, size_t blen)
     97static void waitprompt()
     98{
     99        sysarg_t rows, cols;
     100        if (console_get_size(fphone(stdout), &cols, &rows) == EOK) {
     101                console_set_pos(fphone(stdout), 0, rows-1);
     102        }
     103        console_set_color(fphone(stdout), COLOR_BLUE, COLOR_WHITE, 0);
     104        printf("Press any key to continue");
     105        fflush(stdout);
     106        console_set_style(fphone(stdout), STYLE_NORMAL);
     107}
     108
     109static void waitkey()
     110{
     111        console_event_t ev;
     112       
     113        while (true) {
     114                if (!console_get_event(fphone(stdin), &ev)) {
     115                        return;
     116                }
     117                if (ev.type == KEY_PRESS) {
     118                        return;
     119                }
     120        }
     121        assert(false);
     122}
     123
     124static void newpage()
     125{
     126        console_clear(fphone(stdout));
     127        chars_remaining = chars_per_screen;
     128}
     129
     130static void paged_char(wchar_t c)
     131{
     132        putchar(c);
     133        if (chars_per_screen > 0) {
     134                chars_remaining--;
     135                if (chars_remaining == 0) {
     136                        fflush(stdout);
     137                        waitprompt();
     138                        waitkey();
     139                        newpage();
     140                }
     141        }
     142}
     143
     144static unsigned int cat_file(const char *fname, size_t blen, bool hex)
    85145{
    86146        int fd, bytes = 0, count = 0, reads = 0;
    87147        off64_t total = 0;
    88148        char *buff = NULL;
     149        int i;
     150        size_t offset = 0;
    89151
    90152        fd = open(fname, O_RDONLY);
     
    109171                        count += bytes;
    110172                        buff[bytes] = '\0';
    111                         printf("%s", buff);
     173                        offset = 0;
     174                        for (i = 0; i < bytes; i++) {
     175                                if (hex) {
     176                                        paged_char(hexchars[((uint8_t)buff[i])/16]);
     177                                        paged_char(hexchars[((uint8_t)buff[i])%16]);
     178                                }
     179                                else {
     180                                        wchar_t c = str_decode(buff, &offset, bytes);
     181                                        if (c == 0) {
     182                                                // reached end of string
     183                                                break;
     184                                        }
     185                                        paged_char(c);
     186                                }
     187                               
     188                        }
    112189                        reads++;
    113190                }
     
    131208        unsigned int argc, i, ret = 0, buffer = 0;
    132209        int c, opt_ind;
     210        bool hex = false;
     211        bool more = false;
     212        sysarg_t rows, cols;
     213        int rc;
    133214
    134215        argc = cli_count_args(argv);
    135216
    136217        for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
    137                 c = getopt_long(argc, argv, "hvmH:t:b:", long_options, &opt_ind);
     218                c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind);
    138219                switch (c) {
    139220                case 'h':
     
    153234                        break;
    154235                case 'm':
    155                         printf("%s", cat_oops);
    156                         return CMD_FAILURE;
     236                        more = true;
     237                        break;
     238                case 'x':
     239                        hex = true;
     240                        break;
    157241                }
    158242        }
     
    168252        if (buffer <= 0)
    169253                buffer = CAT_DEFAULT_BUFLEN;
     254       
     255        if (more) {
     256                rc = console_get_size(fphone(stdout), &cols, &rows);
     257                if (rc != EOK) {
     258                        printf("%s - cannot get console size\n", cmdname);
     259                        return CMD_FAILURE;
     260                }
     261                chars_per_screen = cols * (rows-1);
     262                newpage();
     263        }
    170264
    171265        for (i = optind; argv[i] != NULL; i++)
    172                 ret += cat_file(argv[i], buffer);
     266                ret += cat_file(argv[i], buffer, hex);
    173267
    174268        if (ret)
  • uspace/app/bdsh/cmds/modules/cat/cat.h

    r5e3eea10 re7ffdd3  
    44/* Prototypes for the cat command, excluding entry points */
    55
    6 static unsigned int cat_file(const char *, size_t);
     6static unsigned int cat_file(const char *, size_t, bool);
    77
    88#endif /* CAT_H */
Note: See TracChangeset for help on using the changeset viewer.