Ignore:
File:
1 edited

Legend:

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

    rcc9f314 r07b7c48  
    6262static sysarg_t console_rows = 0;
    6363static bool should_quit = false;
     64static bool dash_represents_stdin = false;
    6465
    6566static console_ctrl_t *console = NULL;
     
    7374        { "more", no_argument, 0, 'm' },
    7475        { "hex", no_argument, 0, 'x' },
     76        { "stdin", no_argument, 0, 's' },
    7577        { 0, 0, 0, 0 }
    7678};
     
    9395                "  -m, --more       Pause after each screen full\n"
    9496                "  -x, --hex        Print bytes as hex values\n"
     97                "  -s  --stdin      Treat `-' in file list as standard input\n"
    9598                "Currently, %s is under development, some options don't work.\n",
    9699                cmdname, cmdname);
     
    114117static void waitkey()
    115118{
    116         kbd_event_t ev;
     119        cons_event_t ev;
     120        kbd_event_t *kev;
    117121       
    118122        while (true) {
    119                 if (!console_get_kbd_event(console, &ev)) {
     123                if (!console_get_event(console, &ev)) {
    120124                        return;
    121125                }
    122                 if (ev.type == KEY_PRESS) {
    123                         if (ev.key == KC_ESCAPE || ev.key == KC_Q) {
     126                if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS) {
     127                        kev = &ev.ev.key;
     128                       
     129                        if (kev->key == KC_ESCAPE || kev->key == KC_Q) {
    124130                                should_quit = true;
    125131                                return;
    126132                        }
    127                         if (ev.key == KC_C) {
     133                        if (kev->key == KC_C) {
    128134                                paging_enabled = false;
    129135                                return;
    130136                        }
    131                         if (ev.key == KC_ENTER || ev.key == KC_SPACE ||
    132                             ev.key == KC_PAGE_DOWN) {
     137                        if (kev->key == KC_ENTER || kev->key == KC_SPACE ||
     138                            kev->key == KC_PAGE_DOWN) {
    133139                                return;
    134140                        }
     
    172178        off64_t file_size = 0, length = 0;
    173179
    174         fd = open(fname, O_RDONLY);
     180        bool reading_stdin = dash_represents_stdin && (str_cmp(fname, "-") == 0);
     181       
     182        if (reading_stdin) {
     183                fd = fileno(stdin);
     184                /* Allow storing the whole UTF-8 character. */
     185                blen = STR_BOUNDS(1);
     186        } else
     187                fd = open(fname, O_RDONLY);
     188       
    175189        if (fd < 0) {
    176190                printf("Unable to open %s\n", fname);
     
    207221
    208222        do {
    209                 bytes = read(fd, buff + copied_bytes, (
    210                         (length != CAT_FULL_FILE && length - (off64_t)count <= (off64_t)(blen - copied_bytes)) ?
    211                         (size_t)(length - count) :
    212                         (blen - copied_bytes) ) );
     223                size_t bytes_to_read;
     224                if (reading_stdin) {
     225                        bytes_to_read = 1;
     226                } else {
     227                        if ((length != CAT_FULL_FILE) &&
     228                            (length - (off64_t)count <= (off64_t)(blen - copied_bytes))) {
     229                                bytes_to_read = (size_t) (length - count);
     230                        } else {
     231                                bytes_to_read = blen - copied_bytes;
     232                        }
     233                }
     234               
     235                bytes = read(fd, buff + copied_bytes, bytes_to_read);
    213236                bytes += copied_bytes;
    214237                copied_bytes = 0;
     
    242265                        reads++;
    243266                }
     267               
     268                if (reading_stdin)
     269                        fflush(stdout);
    244270        } while (bytes > 0 && !should_quit && (count < length || length == CAT_FULL_FILE));
    245271
     
    284310
    285311        for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
    286                 c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind);
     312                c = getopt_long(argc, argv, "xhvmH:t:b:s", long_options, &opt_ind);
    287313                switch (c) {
    288314                case 'h':
     
    318344                        hex = true;
    319345                        break;
     346                case 's':
     347                        dash_represents_stdin = true;
     348                        break;
    320349                }
    321350        }
Note: See TracChangeset for help on using the changeset viewer.