Ignore:
Timestamp:
2011-03-29T19:32:53Z (13 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
eea1dd5
Parents:
2cf95e8 (diff), 93ebe4e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes

File:
1 edited

Legend:

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

    r2cf95e8 r3317724  
    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 <io/keycode.h>
     42#include <errno.h>
     43#include <vfs/vfs.h>
     44#include <assert.h>
    3745
    3846#include "config.h"
     
    4856
    4957static const char *cat_oops = "That option is not yet supported\n";
     58static const char *hexchars = "0123456789abcdef";
     59
     60static bool paging_enabled = false;
     61static size_t chars_remaining = 0;
     62static size_t lines_remaining = 0;
     63static sysarg_t console_cols = 0;
     64static sysarg_t console_rows = 0;
     65static bool should_quit = false;
    5066
    5167static struct option const long_options[] = {
     
    5672        { "buffer", required_argument, 0, 'b' },
    5773        { "more", no_argument, 0, 'm' },
     74        { "hex", no_argument, 0, 'x' },
    5875        { 0, 0, 0, 0 }
    5976};
     
    7592                "  -b, --buffer ##  Set the read buffer size to ##\n"
    7693                "  -m, --more       Pause after each screen full\n"
     94                "  -x, --hex        Print bytes as hex values\n"
    7795                "Currently, %s is under development, some options don't work.\n",
    7896                cmdname, cmdname);
     
    82100}
    83101
    84 static unsigned int cat_file(const char *fname, size_t blen)
     102static void waitprompt()
     103{
     104        console_set_pos(fphone(stdout), 0, console_rows-1);
     105        console_set_color(fphone(stdout), COLOR_BLUE, COLOR_WHITE, 0);
     106        printf("ENTER/SPACE/PAGE DOWN - next page, "
     107               "ESC/Q - quit, C - continue unpaged");
     108        fflush(stdout);
     109        console_set_style(fphone(stdout), STYLE_NORMAL);
     110}
     111
     112static void waitkey()
     113{
     114        console_event_t ev;
     115       
     116        while (true) {
     117                if (!console_get_event(fphone(stdin), &ev)) {
     118                        return;
     119                }
     120                if (ev.type == KEY_PRESS) {
     121                        if (ev.key == KC_ESCAPE || ev.key == KC_Q) {
     122                                should_quit = true;
     123                                return;
     124                        }
     125                        if (ev.key == KC_C) {
     126                                paging_enabled = false;
     127                                return;
     128                        }
     129                        if (ev.key == KC_ENTER || ev.key == KC_SPACE ||
     130                            ev.key == KC_PAGE_DOWN) {
     131                                return;
     132                        }
     133                }
     134        }
     135        assert(false);
     136}
     137
     138static void newpage()
     139{
     140        console_clear(fphone(stdout));
     141        chars_remaining = console_cols;
     142        lines_remaining = console_rows-1;
     143}
     144
     145static void paged_char(wchar_t c)
     146{
     147        putchar(c);
     148        if (paging_enabled) {
     149                chars_remaining--;
     150                if (c == '\n' || chars_remaining == 0) {
     151                        chars_remaining = console_cols;
     152                        lines_remaining--;
     153                }
     154                if (lines_remaining == 0) {
     155                        fflush(stdout);
     156                        waitprompt();
     157                        waitkey();
     158                        newpage();
     159                }
     160        }
     161}
     162
     163static unsigned int cat_file(const char *fname, size_t blen, bool hex)
    85164{
    86165        int fd, bytes = 0, count = 0, reads = 0;
    87166        off64_t total = 0;
    88167        char *buff = NULL;
     168        int i;
     169        size_t offset = 0;
    89170
    90171        fd = open(fname, O_RDONLY);
     
    109190                        count += bytes;
    110191                        buff[bytes] = '\0';
    111                         printf("%s", buff);
     192                        offset = 0;
     193                        for (i = 0; i < bytes && !should_quit; i++) {
     194                                if (hex) {
     195                                        paged_char(hexchars[((uint8_t)buff[i])/16]);
     196                                        paged_char(hexchars[((uint8_t)buff[i])%16]);
     197                                }
     198                                else {
     199                                        wchar_t c = str_decode(buff, &offset, bytes);
     200                                        if (c == 0) {
     201                                                // reached end of string
     202                                                break;
     203                                        }
     204                                        paged_char(c);
     205                                }
     206                               
     207                        }
    112208                        reads++;
    113209                }
    114         } while (bytes > 0);
     210        } while (bytes > 0 && !should_quit);
    115211
    116212        close(fd);
     
    131227        unsigned int argc, i, ret = 0, buffer = 0;
    132228        int c, opt_ind;
     229        bool hex = false;
     230        bool more = false;
     231        sysarg_t rows, cols;
     232        int rc;
     233       
     234        // reset global state
     235        // TODO: move to structure?
     236        paging_enabled = false;
     237        chars_remaining = 0;
     238        lines_remaining = 0;
     239        console_cols = 0;
     240        console_rows = 0;
     241        should_quit = false;
    133242
    134243        argc = cli_count_args(argv);
    135244
    136245        for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
    137                 c = getopt_long(argc, argv, "hvmH:t:b:", long_options, &opt_ind);
     246                c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind);
    138247                switch (c) {
    139248                case 'h':
     
    144253                        return CMD_SUCCESS;
    145254                case 'H':
    146                         printf(cat_oops);
     255                        printf("%s", cat_oops);
    147256                        return CMD_FAILURE;
    148257                case 't':
    149                         printf(cat_oops);
     258                        printf("%s", cat_oops);
    150259                        return CMD_FAILURE;
    151260                case 'b':
    152                         printf(cat_oops);
     261                        printf("%s", cat_oops);
    153262                        break;
    154263                case 'm':
    155                         printf(cat_oops);
    156                         return CMD_FAILURE;
     264                        more = true;
     265                        break;
     266                case 'x':
     267                        hex = true;
     268                        break;
    157269                }
    158270        }
     
    168280        if (buffer <= 0)
    169281                buffer = CAT_DEFAULT_BUFLEN;
    170 
    171         for (i = optind; argv[i] != NULL; i++)
    172                 ret += cat_file(argv[i], buffer);
     282       
     283        if (more) {
     284                rc = console_get_size(fphone(stdout), &cols, &rows);
     285                if (rc != EOK) {
     286                        printf("%s - cannot get console size\n", cmdname);
     287                        return CMD_FAILURE;
     288                }
     289                console_cols = cols;
     290                console_rows = rows;
     291                paging_enabled = true;
     292                newpage();
     293        }
     294
     295        for (i = optind; argv[i] != NULL && !should_quit; i++)
     296                ret += cat_file(argv[i], buffer, hex);
    173297
    174298        if (ret)
Note: See TracChangeset for help on using the changeset viewer.