WKern
Loading...
Searching...
No Matches
regexcmd.c
Go to the documentation of this file.
1/*
2WKern - A Bare Metal OS / Kernel I am maKing (For Fun)
3Copyright (C) 2025 Wdboyes13
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18#include <KShell/shell.h>
19#include <global.h>
20#include <io/kio.h>
21#include <mem/kmem.h>
22#include <slre/slre.h>
23#include <utils/util.h>
24
28void Regexc() {
29 char regexp[100];
30 char *res = (char *)Kmalloc(300, 8);
31
32 if (!res) {
33 Kprintf("Malloc failed!\n");
34 return;
35 }
36
37 Kprintf("Enter regex expression: ");
38 Kgetstr(regexp, 99);
39
40 Kprintf("Enter text: ");
41 Kgetstr(res, 299);
42
43 // Allocate space for capture groups (we'll use 1 group here)
44 struct slre_cap caps[1];
45
46 int matched = slre_match(regexp, res, Kstrlen(res), caps, 1, 0);
47 if (matched > 0) {
48 Kprintf("Match! Matched ");
49 KprintHex(matched);
50 Kputchar('\n');
51
52 // Print matched substring
53 int len = caps[0].len;
54 Kprintf("Matched text: ");
55 for (int i = 0; i < len; i++) {
56 Kputchar(caps[0].ptr[i]);
57 }
58 Kputchar('\n');
59 } else {
60 Kprintf("No match.\n");
61 }
62
63 Kfree(res);
64}
void * Kmalloc(size_t size, size_t align)
Allocate memory.
Definition alloc.c:48
void Kfree(void *ptr)
Free allocated memory.
Definition alloc.c:75
void Kgetstr(char *str, int length)
Reads a line of input from the keyboard (blocking).
Definition keyin.c:186
void Kprintf(const char *fmt,...)
Formatted output to the screen.
Definition printer.c:152
void KprintHex(u32 num)
Prints a 32-bit unsigned integer in hexadecimal format prefixed with "0x".
Definition printer.c:92
void Kputchar(char c)
Outputs a single character to the screen at the current cursor position.
Definition printer.c:46
void Regexc()
Interactive regex command.
Definition regexcmd.c:28
size_t Kstrlen(const char *str)
Get string length.
Definition strings.c:121