Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion uspace/app/bdsh/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

deps = [ 'clui', 'fmtutil' ]
deps = [ 'clui', 'fmtutil', 'wildcards' ]
includes += include_directories('.', 'cmds', 'cmds/builtins', 'cmds/modules')
src = files(
'cmds/builtin_cmds.c',
Expand Down Expand Up @@ -65,8 +65,10 @@ src = files(
'scli.c',
'tok.c',
'util.c',

)


test_src = files(
'tok.c',
'test/toktest.c',
Expand Down
71 changes: 66 additions & 5 deletions uspace/app/bdsh/tok.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>

#include "tok.h"
#include "wildcards/wildcards.h"
#include "stdio.h"


/* Forward declarations of static functions */
static char32_t tok_get_char(tokenizer_t *);
Expand All @@ -43,6 +45,62 @@ static bool tok_pending_chars(tokenizer_t *);
static errno_t tok_finish_string(tokenizer_t *);
static void tok_start_token(tokenizer_t *, token_type_t);

/** Callback that pushes tokens after wildcard expansion */
static errno_t push_expanded_wildcard_token(char *text, void *arg) {
tokenizer_t *tok = (tokenizer_t *) arg;

if (tok->outtok_offset >= tok->outtok_size){
return EOVERFLOW;
}

if (tok->outbuf_offset + str_size(text) + 1 >= tok->outbuf_size){
return EOVERFLOW;
}

str_cpy(tok->outbuf + tok->outbuf_offset,
tok->outbuf_size - tok->outbuf_offset, text);

token_t *tokinfo = &tok->outtok[tok->outtok_offset++];
tokinfo->type = tok->current_type;
tokinfo->text = tok->outbuf + tok->outbuf_offset;
tokinfo->byte_start = tok->last_in_offset;
tokinfo->byte_length = str_size(text);
tokinfo->char_start = tok->last_in_char_offset;
tokinfo->char_length = str_length(text);

tok->outbuf_offset += str_size(text) + 1;
tok->outbuf_last_start = tok->outbuf_offset;

return EOK;
}

/** Function that expands current token (if it contains wildcards) and pushes it to the buffer */
static errno_t wildcard_token_expand(tokenizer_t *tok){
tok->outbuf[tok->outbuf_offset] = '\0';
char *text = tok->outbuf + tok->outbuf_last_start;
const char *ctext = str_dup(text);
Comment thread
PatrikPrit15 marked this conversation as resolved.
if (ctext == NULL) {
return ENOMEM;
}
// printf("Pushed token: '%s'\n", ctext);
errno_t rc = expand_wildcard_patterns(ctext, "", push_expanded_wildcard_token, tok);
free((char *)ctext);
// rc = tok_push_token(tok);
if (rc != EOK) {
printf("Error pushing token: %i\n",rc);
return rc;
}

// Update position info for next token
tok->last_in_offset = tok->in_offset;
tok->last_in_char_offset = tok->in_char_offset;
tok->outbuf_last_start = tok->outbuf_offset;

return rc;
}



/** Initialize the token parser
*
* @param tok the tokenizer structure to initialize
Expand Down Expand Up @@ -102,8 +160,9 @@ errno_t tok_tokenize(tokenizer_t *tok, size_t *tokens_length)
* there are several spaces in the input.
*/
if (tok_pending_chars(tok)) {
rc = tok_push_token(tok);
if (rc != EOK) {
rc = wildcard_token_expand(tok);
Comment thread
PatrikPrit15 marked this conversation as resolved.

if (rc != EOK){
return rc;
}
}
Expand Down Expand Up @@ -166,8 +225,9 @@ errno_t tok_tokenize(tokenizer_t *tok, size_t *tokens_length)

/* Push the last token */
if (tok_pending_chars(tok)) {
rc = tok_push_token(tok);
if (rc != EOK) {
rc = wildcard_token_expand(tok);

if (rc != EOK){
return rc;
}
}
Expand Down Expand Up @@ -202,6 +262,7 @@ errno_t tok_finish_string(tokenizer_t *tok)
}
} else {
rc = tok_push_char(tok, tok_get_char(tok));

if (rc != EOK) {
return rc;
}
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions uspace/lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ libs = [

'ui',
'vt',

'wildcards',
]

# Generated list of include directory paths
Expand Down
40 changes: 40 additions & 0 deletions uspace/lib/wildcards/include/wildcards/wildcards.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2025 Patrik Pritrsky
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef MAIN_H
#define MAIN_H
#include <stdbool.h>

typedef errno_t (*wildcards_match_found_callback_t)(char *, void *arg);


bool contains_wildcard(const char *pattern);
errno_t wildcard_comp(const char *pattern, const char *file_name, bool *result);
errno_t expand_wildcard_patterns(const char *pattern, const char *path, wildcards_match_found_callback_t callback, void* arg);

#endif // MAIN_H
65 changes: 65 additions & 0 deletions uspace/lib/wildcards/info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Wildcard support for HelenOS - MSoC Project by Patrik Pritrsky

In this project, I added support for wildcards in the HelenOS shell.
So, if the user wants to delete all text files in a directory, they just
need to type 'rm *.txt', instead of listing them all one by one.

## Features

### Standard wildcard *

Expands to zero or more characters, evaluation happens recursively at
all levels where it occurs.

For example, 'folder*/file*.txt' will find all text files starting with
'file', in all subdirectories of the current directory that start with
'folder'.

### Recursive wildcard **

Used to find files at arbitrary depth.

For example, '**/*.txt' will find all text files, at any depth within
the current directory.

## List of changes to HelenOS

- Added automated tests for wildcards
- Created a function to detect whether a string contains a wildcard
- Created a function to check whether a wildcard pattern matches a
file/directory name
- Created a function for recursive expansion and finding all
occurrences of files/directories that match a path/filename containing
wildcards
- Modified the HelenOS shell tokenizer to support wildcard expansion

# Podpora zástupných znaků (wildcards) v HelenOSím shellu - MSoC Project by Patrik Pritrsky

V tomto projekte som pridal podporu pre zástupné znaky (wildcards) v HelenOS shelle.
Teda, ak používateľ bude chcieť vymazať všetky textové súbory v priečinku,
tak mu stačí napísať 'rm *.txt', namiesto toho, aby ich všetky vymenoval.

## Funkcie

### Štandardný zástupný znak/wildcard *

Expanduje sa na nula alebo viacero znakov,
vyhodnocovanie prebieha rekurzívne na všetkých úrovniach, kde sa nachádza.

Teda napríklad 'priecinok*/subor*.txt', nájde všetky textové súbory začínajúce sa na 'subor',
vo všetkých podprečinkoch aktuálneho priečinku začínajúcich sa na 'priecinok'.

### Rekurzívny zástupný znak/wildcard **

Funguje na nájdenie súborov, ktoré sú ľubovoľne hlboko.

Teda napríklad '**/*.txt', nájde všetky textové súbory, ľubovoľne hlboko v aktuálnom priečinku.

## Zoznam zmien do HelenOS

- Pridanie automatizovaných testov pre zástupné znaky
- Vytvorenie funkcie na detekovanie, či reťazec obsahuje zástupný znak
- Vytvorenie funkcie na porovnanie, či sa zástupný znak pattern zhoduje s názvom súboru/priečinku
- Vytvorenie funkcie na rekurzívne expandovanie a nájdenie všetkých výskytov súborov/priečinkov,
ktoré sa zhodujú s cestou/názvom súboru obsahujúcim zástupné znaky
- Zmena v tokenizátore HelenOS shellu, tak, aby podporoval expanziu zástupných znakov
31 changes: 31 additions & 0 deletions uspace/lib/wildcards/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# Copyright (c) 2025 Patrik Pritrsky
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# - The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

src = files(['src/wildcards.c'])

test_src = files(['test/wildcards_test.c', 'src/wildcards.c'])
Loading