Skip to content

Commit 89e5302

Browse files
Added new utilities find, grep and sort
1 parent ef382b8 commit 89e5302

14 files changed

+456
-2
lines changed

Makefile

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ASM=ez80asm
22

33
.PHONY: binaries
4-
binaries: mos/memfill.bin mos/more.bin mos/font.bin mos/fontctl.bin mos/comp.bin mos/nano.bin bin/loadfont.bin bin/recode.bin bin/mc.bin bin/12amc.ovl
4+
binaries: mos/memfill.bin mos/more.bin mos/font.bin mos/fontctl.bin mos/comp.bin mos/nano.bin bin/loadfont.bin bin/recode.bin mos/find.bin mos/grep.bin bin/sort.bin bin/mc.bin bin/12amc.ovl
55

66
loadfont/src/codepages.h: loadfont/src/gen_codepages.py
77
cd loadfont/src;python3 gen_codepages.py >codepages.h
@@ -32,6 +32,19 @@ bin/recode.bin: recode/src/*.[ch] loadfont/src/codepages.h
3232
mkdir -p bin
3333
cd recode;make;mv bin/recode.bin ../bin
3434

35+
mos/find.bin: find/src/*.[ch]
36+
mkdir -p bin
37+
cd find;make;mv bin/find.bin ../mos
38+
39+
mos/grep.bin: grep/src/*.[ch]
40+
mkdir -p bin
41+
cd grep;make;mv bin/grep.bin ../mos
42+
43+
bin/sort.bin: sort/src/*.[ch]
44+
mkdir -p bin
45+
cd sort;make;mv bin/sort.bin ../bin
46+
47+
3548
bin/12amc.ovl: mc/src/*.[ch]
3649
mkdir -p bin
3750
cd mc;make;mv bin/mc.bin ../bin/12amc.ovl

README.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,53 @@ This converts a text file from CP1252 to Latin9 (ISO8859-15) without
155155
changing the end-of-line characters.
156156

157157

158+
### find
159+
160+
This program finds files (in the entire file system) whose name matches a
161+
specific pattern.
162+
163+
Example:
164+
`find *.c`
165+
166+
This will find all files whose name matches the pattern `*.c` in the whole
167+
file system.
168+
169+
You can use the '>' character to redirect the output to a file. Example:
170+
`find *.asm >all_asmfiles.txt`
171+
172+
### grep
173+
174+
This program searches test files for specific strings.
175+
176+
Example:
177+
`grep dirlist *.c`
178+
179+
This will find all lines containing "dirlist" in all C source files.
180+
181+
You can do a case-insensitive grep using the `-i` option. It will only consider
182+
ASCII letters. You can also enclose the search string in double quotes to
183+
search for a string containint spaces"
184+
185+
Example:
186+
`grep -i "these few words" *.txt`
187+
188+
You can use the '>' character to redirect the output to a file. Example:
189+
`grep dirlist *.c >results.txt`
190+
191+
### sort
192+
193+
This program sorts lines in a text file in ASCII-lexigographic order.
194+
195+
Example:
196+
197+
`sort myfile.txt`
198+
199+
You can use the '>' character to redirect the output to a file.
200+
Example:
201+
202+
`sort myfile.txt >sorted.txt`
203+
204+
158205
### nano
159206

160207
This is an editor with nano-style key bindings.
@@ -170,7 +217,7 @@ Example command using a buffer:
170217
It can take a third parameter to specify the line number on which the cursor must start. Example:
171218
`nano myfile.txt &90000 231`
172219

173-
The edditor runs in MODE 0 (it selects this mode when started) and tries to return to the mode from which it was called.
220+
The editor runs in MODE 0 (it selects this mode when started) and tries to return to the mode from which it was called.
174221
Key bindings are nano-style, but not all are implemented (far from).
175222

176223
Implemented key bindings: Control-A, Control-E to go to the start or end of a line, Control-Y and Control-V for page-up and

bin/12amc.ovl

49 Bytes
Binary file not shown.

bin/loadfont.bin

49 Bytes
Binary file not shown.

bin/recode.bin

49 Bytes
Binary file not shown.

bin/sort.bin

6.35 KB
Binary file not shown.

find/Makefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = find
6+
ICON = icon.png
7+
DESCRIPTION = "Utility to find files"
8+
COMPRESSED = NO
9+
ARCHIVED = NO
10+
INIT_LOC = 0b0000
11+
BSSHEAP_LOW = 0b4000
12+
BSSHEAP_HIGH = 0B7FFF
13+
STACK_HIGH = 0B7FFF
14+
15+
CFLAGS = -Werror -Wall -Wextra -Oz -DCEDEV
16+
CXXFLAGS = -Werror -Wall -Wextra -Oz - DCEDEV
17+
LDHAS_ARG_PROCESSING = 1
18+
LDHAS_EXIT_HANDLER:=0
19+
# ----------------------------
20+
21+
include $(shell cedev-config --makefile)
22+

find/src/main.c

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* find
2+
* search the entire file system for file names conforming to a certain pattern
3+
*/
4+
5+
#include <stdlib.h>
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include <ctype.h>
9+
#include <stdbool.h>
10+
#include <mos_api.h>
11+
12+
13+
#define MAX_DIRS 200
14+
char *dirnames[MAX_DIRS];
15+
int write_idx=0;
16+
int read_idx=0;
17+
18+
static bool glob_is_match(char *name, char *pat)
19+
{
20+
char *p;
21+
if (*name == 0 && *pat == 0) {
22+
return true;
23+
} else if (*pat=='?' && *name != 0) {
24+
return glob_is_match(name+1, pat+1);
25+
} else if (*pat=='*') {
26+
p = name;
27+
do {
28+
if (glob_is_match(p, pat+1)) {
29+
return true;
30+
}
31+
} while (*p++);
32+
return false;
33+
} else if (toupper(*pat) == toupper(*name)) {
34+
return glob_is_match(name+1, pat+1);
35+
} else {
36+
return false;
37+
}
38+
}
39+
40+
int
41+
main(int argc, char *argv[])
42+
{
43+
DIR dir_struct;
44+
FILINFO file_struct;
45+
int res;
46+
char namebuf[256];
47+
if (argc < 2) {
48+
fprintf(stderr,"Usage: find <pattern>\n");
49+
return 19;
50+
}
51+
dirnames[write_idx++] = strdup("/");
52+
while (dirnames[read_idx] != NULL) {
53+
res = ffs_dopen(&dir_struct,dirnames[read_idx]);
54+
if (res == 0) {
55+
for (;;) {
56+
res = ffs_dread(&dir_struct,&file_struct);
57+
if (res != 0 || file_struct.fname[0]==0)
58+
break;
59+
strcpy(namebuf, dirnames[read_idx]);
60+
if (dirnames[read_idx][1] != 0)
61+
strcat(namebuf,"/");
62+
strcat(namebuf, file_struct.fname);
63+
if ((file_struct.fattrib & 0x10) != 0) {
64+
if (write_idx < MAX_DIRS-1)
65+
dirnames[write_idx++] = strdup(namebuf);
66+
} else {
67+
if (glob_is_match(file_struct.fname,argv[1]))
68+
printf("%s\n",namebuf);
69+
}
70+
}
71+
}
72+
read_idx++;
73+
}
74+
return 0;
75+
}

grep/Makefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = grep
6+
ICON = icon.png
7+
DESCRIPTION = "Utility search files for strings"
8+
COMPRESSED = NO
9+
ARCHIVED = NO
10+
INIT_LOC = 0B0000
11+
BSSHEAP_LOW = 0B5000
12+
BSSHEAP_HIGH = 0B7FFF
13+
STACK_HIGH = 0B7FFF
14+
15+
CFLAGS = -Werror -Wall -Wextra -Oz -DCEDEV
16+
CXXFLAGS = -Werror -Wall -Wextra -Oz - DCEDEV
17+
LDHAS_ARG_PROCESSING = 1
18+
LDHAS_EXIT_HANDLER:=0
19+
# ----------------------------
20+
21+
include $(shell cedev-config --makefile)
22+

grep/src/main.c

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/* grep
2+
* search a file for a certain string.
3+
*/
4+
5+
6+
#include <stdlib.h>
7+
#include <stdio.h>
8+
#include <string.h>
9+
#include <ctype.h>
10+
#include <stdbool.h>
11+
#include <mos_api.h>
12+
13+
14+
static bool glob_is_match(char *name, char *pat)
15+
{
16+
char *p;
17+
if (*name == 0 && *pat == 0) {
18+
return true;
19+
} else if (*pat=='?' && *name != 0) {
20+
return glob_is_match(name+1, pat+1);
21+
} else if (*pat=='*') {
22+
p = name;
23+
do {
24+
if (glob_is_match(p, pat+1)) {
25+
return true;
26+
}
27+
} while (*p++);
28+
return false;
29+
} else if (toupper(*pat) == toupper(*name)) {
30+
return glob_is_match(name+1, pat+1);
31+
} else {
32+
return false;
33+
}
34+
}
35+
36+
bool nocase = false;
37+
38+
static char buf[1024];
39+
static char linebuf[256];
40+
int file_idx = 0;
41+
int buf_filled = 0;
42+
FILE *f;
43+
static int nextchar(void)
44+
{
45+
if (file_idx == buf_filled) {
46+
buf_filled = fread(buf, 1, 1024, f);
47+
if (buf_filled == 0) {
48+
return -1;
49+
}
50+
file_idx = 0;
51+
}
52+
return buf[file_idx++];
53+
}
54+
55+
static bool nextline(void)
56+
{
57+
int linelength = 0;
58+
int c;
59+
for (;;) {
60+
c=nextchar();
61+
if (c==-1) {
62+
linebuf[linelength] = 0;
63+
return linelength != 0;
64+
}
65+
else if (c=='\n') {
66+
linebuf[linelength] = 0;
67+
return true;
68+
}
69+
else if (linelength==255) {
70+
linebuf[linelength] = 0;
71+
return true;
72+
} else {
73+
linebuf[linelength++]=c;
74+
}
75+
}
76+
}
77+
78+
static bool str_match(char *line,char *pat)
79+
{
80+
char *p=line;
81+
char *q, *r;
82+
char c1, c2;
83+
for (;;) {
84+
q=p; p++;
85+
r=pat;
86+
for (;;) {
87+
c1=*q++;
88+
c2=*r++;
89+
if (c2==0) return true;
90+
if (c1==0) return false;
91+
if (nocase) {
92+
if (c1 >= 'a' && c1 <= 'z') c1-=0x20;
93+
if (c2 >= 'a' && c2 <= 'z') c2-=0x20;
94+
}
95+
if (c1 != c2) break;
96+
}
97+
}
98+
}
99+
100+
static void search_file(char *fname, char *string_pat)
101+
{
102+
f=fopen(fname,"r");
103+
if (!f) {
104+
fprintf(stderr,"Error opening file %s\n",fname);
105+
return;
106+
}
107+
file_idx = 0;
108+
buf_filled = 0;
109+
while (nextline()) {
110+
if (str_match(linebuf,string_pat))
111+
printf("%s: %s\n",fname,linebuf);
112+
}
113+
fclose(f);
114+
}
115+
116+
117+
int
118+
main(int argc, char *argv[])
119+
{
120+
unsigned int nopts = 0;
121+
DIR dir_struct;
122+
FILINFO file_struct;
123+
int res;
124+
125+
if (argc < 3) {
126+
fprintf(stderr,"Usage: grep [-i] <string> <files>\n");
127+
return 19;
128+
}
129+
if (strcmp(argv[1],"-i")==0) {
130+
nocase = true;
131+
nopts += 1;
132+
}
133+
res = ffs_dopen(&dir_struct,".");
134+
if (res == 0) {
135+
for (;;) {
136+
res = ffs_dread(&dir_struct,&file_struct);
137+
if (res != 0 || file_struct.fname[0]==0)
138+
break;
139+
if ((file_struct.fattrib & 0x10) == 0 &&
140+
glob_is_match(file_struct.fname,argv[2+nopts])) {
141+
search_file(file_struct.fname,argv[1+nopts]);
142+
}
143+
}
144+
ffs_dclose(&dir_struct);
145+
}
146+
return 0;
147+
}

mos/find.bin

5 KB
Binary file not shown.

mos/grep.bin

13.2 KB
Binary file not shown.

sort/Makefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = sort
6+
ICON = icon.png
7+
DESCRIPTION = "Utility to sort files"
8+
COMPRESSED = NO
9+
ARCHIVED = NO
10+
INIT_LOC = 040000
11+
BSSHEAP_LOW = 050000
12+
BSSHEAP_HIGH = 0B7FFF
13+
STACK_HIGH = 0B7FFF
14+
15+
CFLAGS = -Werror -Wall -Wextra -Oz -DCEDEV
16+
CXXFLAGS = -Werror -Wall -Wextra -Oz - DCEDEV
17+
LDHAS_ARG_PROCESSING = 1
18+
LDHAS_EXIT_HANDLER:=0
19+
# ----------------------------
20+
21+
include $(shell cedev-config --makefile)
22+

0 commit comments

Comments
 (0)