Skip to content

Commit 71442f0

Browse files
committed
FEAT: implemented block argument for QUERY/MODE for FILE, DIR, INPUT (console) and HTTP ports
1 parent ecf594c commit 71442f0

File tree

5 files changed

+72
-6
lines changed

5 files changed

+72
-6
lines changed

src/boot/actions.r

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ query: action [
424424
{Returns information about a port, file, or URL.}
425425
target [port! file! url! block!]
426426
/mode "Get mode information"
427-
field [word! none!] "NONE will return valid modes for port type"
427+
field [word! block! none!] "NONE will return valid modes for port type"
428428
]
429429

430430
modify: action [

src/core/p-console.c

+20
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,26 @@
189189
if (!Set_Console_Mode_Value(req, VAL_WORD_CANON(info), ret))
190190
Trap1(RE_INVALID_ARG, info);
191191
}
192+
else if (IS_BLOCK(info)) {
193+
REBVAL *val;
194+
REBSER *values = Make_Block(2 * BLK_LEN(VAL_SERIES(info)));
195+
REBVAL *word = VAL_BLK_DATA(info);
196+
for (; NOT_END(word); word++) {
197+
if (ANY_WORD(word)) {
198+
if (IS_SET_WORD(word)) {
199+
// keep the set-word in result
200+
val = Append_Value(values);
201+
*val = *word;
202+
VAL_SET_LINE(val);
203+
}
204+
val = Append_Value(values);
205+
if (!Set_Console_Mode_Value(req, VAL_WORD_CANON(word), val))
206+
Trap1(RE_INVALID_ARG, word);
207+
}
208+
else Trap1(RE_INVALID_ARG, word);
209+
}
210+
Set_Series(REB_BLOCK, ret, values);
211+
}
192212
else {
193213
REBSER *obj = CLONE_OBJECT(VAL_OBJ_FRAME(spec));
194214
SET_INTEGER(OFV(obj, STD_CONSOLE_INFO_BUFFER_COLS), req->console.buffer_cols);

src/core/p-file.c

+27
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,38 @@
132132
***********************************************************************/
133133
{
134134
REBSER *obj;
135+
REBVAL *val;
135136

136137
if ( IS_WORD(info) ) {
137138
if(!Set_File_Mode_Value(file, VAL_WORD_CANON(info), ret))
138139
Trap1(RE_INVALID_ARG, info);
140+
} else if (IS_BLOCK(info)) {
141+
// example:
142+
// query/mode file [type size] ;== [file 1234]
143+
// or:
144+
// query/mode file [type: size:] ;== [type: file size: 1234]
145+
// or combined:
146+
// query/mode file [type: size] ;== [type: file 1234]
147+
// When not supported word is used, if will throw an error
148+
149+
REBSER *values = Make_Block(2 * BLK_LEN(VAL_SERIES(info)));
150+
REBVAL *word = VAL_BLK_DATA(info);
151+
for (; NOT_END(word); word++) {
152+
if(ANY_WORD(word)) {
153+
if (IS_SET_WORD(word)) {
154+
// keep the set-word in result
155+
val = Append_Value(values);
156+
*val = *word;
157+
VAL_SET_LINE(val);
158+
}
159+
val = Append_Value(values);
160+
if(!Set_File_Mode_Value(file, VAL_WORD_CANON(word), val))
161+
Trap1(RE_INVALID_ARG, word);
162+
} else Trap1(RE_INVALID_ARG, word);
163+
}
164+
Set_Series(REB_BLOCK, ret, values);
139165
} else {
166+
//@@ oldes: is returning object really still needed?
140167
info = In_Object(port, STD_PORT_SCHEME, STD_SCHEME_INFO, 0);
141168
if (!info || !IS_OBJECT(info)) Trap_Port(RE_INVALID_SPEC, port, -10);
142169
obj = CLONE_OBJECT(VAL_OBJ_FRAME(info));

src/mezz/prot-http.r

+16-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ REBOL [
2323
0.1.4 26-Nov-2012 "Richard Smolak" "Version from Atronix's fork"
2424
0.1.5 10-May-2018 "Oldes" "FIX: Query on URL was returning just none"
2525
0.1.6 21-May-2018 "Oldes" "FEAT: Added support for basic redirection"
26+
0.1.7 03-Dec-2018 "Oldes" "FEAT: Added support for QUERY/MODE action"
2627
]
2728
]
2829

@@ -647,10 +648,10 @@ sys/make-scheme [
647648
query: func [
648649
port [port!]
649650
/mode
650-
field [word! none!]
651-
/local error state
651+
field [word! block! none!]
652+
/local error state result
652653
] [
653-
if all [mode none? field] [ return words-of system/standard/file-info]
654+
if all [mode none? field] [ return words-of system/schemes/http/info]
654655
if none? state: port/state [
655656
open port ;there is port opening in sync-op, but it would also close the port later and so clear the state
656657
attempt [sync-op port [parse-write-dialect port [HEAD]]]
@@ -663,7 +664,18 @@ sys/make-scheme [
663664
state/info/response-parsed
664665
][
665666
either field [
666-
select state/info field
667+
either word? field [
668+
select state/info field
669+
][
670+
result: make block! length? field
671+
foreach word field [
672+
if any-word? word [
673+
if set-word? word [ append result word ]
674+
append result state/info/(to word! word)
675+
]
676+
]
677+
result
678+
]
667679
][ state/info ]
668680
][ none ]
669681
]

src/tests/units/port-test.r3

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ Rebol [
3232
--assert 'file = query/mode file 'type
3333
--assert date? query/mode file 'date
3434
--assert 51732 = query/mode file 'size
35+
--assert [file 51732] = query/mode file [type size]
36+
--assert [type: file size: 51732] = query/mode file [type: size:]
3537

3638
--test-- "query file info (port)"
3739
file: open %units/files/alice29.txt.gz
3840
--assert [name size date type] = query/mode file none
3941
--assert 'file = query/mode file 'type
4042
--assert date? query/mode file 'date
4143
--assert 51732 = query/mode file 'size
44+
--assert [file 51732] = query/mode file [type size]
45+
--assert [type: file size: 51732] = query/mode file [type: size:]
4246
close file
4347
===end-group===
4448

@@ -58,7 +62,10 @@ Rebol [
5862
--assert integer? query/mode system/ports/input 'buffer-cols
5963
--assert integer? query/mode system/ports/input 'buffer-rows
6064
--assert [buffer-cols buffer-rows window-cols window-rows]
61-
= query/mode system/ports/input none
65+
= m: query/mode system/ports/input none
66+
--assert block? v: query/mode system/ports/input m
67+
--assert 4 = length? v
68+
6269
===end-group===
6370

6471
~~~end-file~~~

0 commit comments

Comments
 (0)