Skip to content

Commit

Permalink
Start to implement Command class
Browse files Browse the repository at this point in the history
  • Loading branch information
ab25cq committed Dec 22, 2016
1 parent 713bcbf commit 3ffa162
Show file tree
Hide file tree
Showing 26 changed files with 1,490 additions and 787 deletions.
244 changes: 244 additions & 0 deletions Command.clc
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
include "File.clc"


dynamic_class Command
{
def initialize() {
}
def callingMethod(method_name:String, params:Array<Object>) : static Command {
str:String = params.items(0).toAnonymous();
printf("class method method_name %s param1 %s\n", array { method_name, str });

return new Command();
}
def callingMethod(method_name:String, params:Array<Object>) : Command {
str:String = params.items(0).toAnonymous();
printf("normal method method_name %s param1 %s\n", array { method_name, str });

return new Command();
}
}

/*
dynamic_typing final class Command implements IInspectable
{
protected String data;
protected int resultCode;

static Array<String> controllingTerminalPrograms = { "vim", "less", "top", "lv", "htop", "emacs", "nano", "vi", "fd", "mc" };

Command() {
self.data = "";
self.resultCode = 0;
}

Command(Command value) {
self.setValue(value);
}

Command(String data, int result_code) {
self.data = data;
self.resultCode = result_code;
}

int resultCode() {
return self.resultCode;
}

virtual String toString() {
return self.data;
}

Command write(Path path, int permission=0666) throws SystemException, Exception {
File f = new File(path, "w", permission);
f.write(self.data.toBytes());
f.close();

return self;
}

static Command excuteCommandWithControllingTerminalByPipe(String method_name, Array<anonymous> params, Block method_block, String pipe_data)
{
int parent2child_write_fd = 0;
int parent2child_read_fd = 0;

System.pipe(parent2child_read_fd, parent2child_write_fd);

pid_t pid = System.fork() {
System.close(parent2child_write_fd);

pid_t pid = System.getpid();

System.setpgid(0.to_pid_t(), 0.to_pid_t());
System.tcsetpgrp(0, pid);

System.dup2(parent2child_read_fd, 0);
System.close(parent2child_read_fd);

try {
System.execvp(method_name, params.select() {|anonymous param| return String->substitutionPosibility(param.type()); } );
} catch(SystemException e) {
System.write(2, (e.getMessage() + "\n").toBytes());
System.exit(2);
}
}

System.setpgid(pid, pid);
System.tcsetpgrp(0, pid);

System.close(parent2child_read_fd);

if(pipe_data != null) {
System.write(parent2child_write_fd, pipe_data.toBytes());
}
System.close(parent2child_write_fd);

pid_t pid2, WaitStatus status = System.waitpid(pid, WaitOption.WUNTRACED);

if(status.WIFSTOPPED()) {
int rcode = status.WSTOPSIG() +128;
String title = method_name;

termios terminfo = new termios();
System.tcgetattr(0, terminfo);

Job job = new Job(title, pid, terminfo);

Clover.jobs.add(job);

System.tcsetpgrp(0, System.getpid());

return new Command("", rcode);
}
else {
System.tcsetpgrp(0, System.getpid());

return new Command("", status.WEXITSTATUS());
}
}

static Command excuteCommandWithControllingTerminal(String method_name, Array<anonymous> params, Block method_block)
{
pid_t pid = System.fork() {
pid_t pid = System.getpid();

System.setpgid(0.to_pid_t(), 0.to_pid_t());
System.tcsetpgrp(0, pid);

try {
System.execvp(method_name, params.select() {|anonymous param| return String->substitutionPosibility(param.type()); } );
} catch(SystemException e) {
System.write(2, (e.getMessage() + "\n").toBytes());
System.exit(2);
}
}

System.setpgid(pid, pid);
System.tcsetpgrp(0, pid);

pid_t pid2, WaitStatus status = System.waitpid(pid, WaitOption.WUNTRACED);

System.tcsetpgrp(0, System.getpid());

if(status.WIFSTOPPED()) {
int rcode = status.WSTOPSIG() +128;
String title = method_name;

termios terminfo = new termios();
System.tcgetattr(0, terminfo);

Job job = new Job(title, pid, terminfo);

Clover.jobs.add(job);

System.tcsetpgrp(0, System.getpid());

return new Command("", rcode);
}
else {
System.tcsetpgrp(0, System.getpid());

return new Command("", status.WEXITSTATUS());
}

return new Command("", status.WEXITSTATUS());
}

static Command executeCommand(String method_name, Array<anonymous> params, Block method_block, String pipe_data)
{
int child2parent_write_fd = 0;
int child2parent_read_fd = 0;
int parent2child_write_fd = 0;
int parent2child_read_fd = 0;

System.pipe(child2parent_read_fd, child2parent_write_fd);
System.pipe(parent2child_read_fd, parent2child_write_fd);

pid_t pid = System.fork() {
System.close(parent2child_write_fd);
System.close(child2parent_read_fd);

System.dup2(parent2child_read_fd, 0);
System.dup2(child2parent_write_fd, 1);

System.close(parent2child_read_fd);
System.close(child2parent_write_fd);

try {
System.execvp(method_name, params.select() {|anonymous param| return String->substitutionPosibility(param.type()); } );
} catch(SystemException e) {
System.write(2, (e.getMessage() + "\n").toBytes());
System.exit(2);
}
}

System.close(parent2child_read_fd);
System.close(child2parent_write_fd);

if(pipe_data != null) {
System.write(parent2child_write_fd, pipe_data.toBytes());
}
System.close(parent2child_write_fd);

Bytes child_output = B"";

while(true) {
Bytes pipe_data = B"";

int readed_byte = System.read(child2parent_read_fd, pipe_data, 10);

if(readed_byte == 0) {
break;
}

child_output += pipe_data;
}

System.close(child2parent_read_fd);

pid_t pid2, WaitStatus status = System.waitpid(pid, WaitOption.WUNTRACED);

return new Command(child_output.toString(), status.WEXITSTATUS());
}

static Command methodMissing(String method_name, Array<anonymous> params, Block method_block)
{
if(Command.controllingTerminalPrograms.include(method_name)) {
return Command.excuteCommandWithControllingTerminal(method_name, params, method_block);
}
else {
return Command.executeCommand(method_name, params, method_block, null);
}
}

Command methodMissing(String method_name, Array<anonymous> params, Block method_block)
{
if(Command.controllingTerminalPrograms.include(method_name)) {
return Command.excuteCommandWithControllingTerminalByPipe(method_name, params, method_block, self.data);
}
else {
return Command.executeCommand(method_name, params, method_block, self.data);
}
}
}
*/
37 changes: 36 additions & 1 deletion FileBase.clc → File.clc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
include "StringBase.clc"
include "String.clc"

class System
{
typedef mode_t int;
typedef dev_t ulong;
typedef uid_t int;
typedef gid_t int;
typedef DIR pointer;

S_IFMT: static int;
S_IFDIR: static int;
Expand Down Expand Up @@ -70,6 +71,9 @@ class System
def realpath(path:String): static native String throws Exception;
def dirname(path:String): static native String;
def basename(path:String): static native String;
def opendir(path:String): static native DIR throws Exception;
def readdir(dir:DIR): static native String@Nullable;
def closedir(dir:DIR): static native int throws Exception;
}

class tm
Expand Down Expand Up @@ -596,6 +600,37 @@ class Path
System.mkdir(self, mode);
}
*/
}

class Directory
{
path:String;
dir:DIR;

def initialize(path:String) throws Exception {
self.path = path;
self.dir = System.opendir(path);
}

def finalize() {
if(self.dir != 0.to_pointer) {
System.closedir(self.dir);
}
}

def readdir(): String@Nullable throws Exception {
if(self.dir == 0.to_pointer) {
throw new Exception("This directory is not opened");
}

return System.readdir(self.dir);
}

def closedir() {
if(self.dir != 0.to_pointer) {
System.closedir(self.dir);
self.dir = 0.to_pointer;
}
}
}

4 changes: 3 additions & 1 deletion ListBase.clc → List.clc
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ class SortableList<T:ISortable>
}

def sort(): SortableList<T> {
return self.mergeSort();
list:SortableList<T> = self.mergeSort();
self.setValue(list);
return self;
}
}
13 changes: 9 additions & 4 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ $(OBJ): src/*.h Makefile configure
install:
rm -f *.clcl
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 -no-load-fundamental-classes -class Fundamental.clc
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 -no-load-fundamental-classes -class StringBase.clc
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 -no-load-fundamental-classes -class ListBase.clc
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 -no-load-fundamental-classes -class FileBase.clc
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 -no-load-fundamental-classes -class String.clc
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 -no-load-fundamental-classes -class List.clc
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 -no-load-fundamental-classes -class File.clc
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 -no-load-fundamental-classes -class Command.clc
mkdir -p ~/.clover2
$(INSTALL) -m 644 PcreOVec.clcl ~/.clover2
$(INSTALL) -m 644 System.clcl ~/.clover2
Expand Down Expand Up @@ -118,6 +119,8 @@ install:
$(INSTALL) -m 644 Tuple9.clcl ~/.clover2
$(INSTALL) -m 644 Tuple10.clcl ~/.clover2
$(INSTALL) -m 644 File.clcl ~/.clover2
$(INSTALL) -m 644 Directory.clcl ~/.clover2
$(INSTALL) -m 644 Command.clcl ~/.clover2
$(INSTALL) -m 644 tm.clcl ~/.clover2
$(INSTALL) -m 644 stat.clcl ~/.clover2
mkdir -p "$(libdir)"
Expand Down Expand Up @@ -152,7 +155,7 @@ permission:
# clean
########################################################
clean:
rm -fR clover2 clover2.dSYM cclover2 cclover2.dSYM src/*.o libclover* config.log config.status *.stackdump autom4te.cache .DS_Store core.* a.out *.clcl *.clo code/*.clo *.clm a.txt b.txt c.txt d.txt a.c
rm -fR clover2 clover2.dSYM cclover2 cclover2.dSYM src/*.o libclover* config.log config.status *.stackdump autom4te.cache .DS_Store core.* a.out *.clcl *.clo code/*.clo *.clm a.c

distclean: clean
rm -fR config.h Makefile autom4te.cache
Expand Down Expand Up @@ -209,4 +212,6 @@ test:
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 code/list2.cl && ./clover2 code/list2.clo
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 code/time.cl && ./clover2 code/time.clo
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 code/file.cl && ./clover2 code/file.clo
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 code/dir.cl && ./clover2 code/dir.clo
export LD_LIBRARY_PATH=.$(LD_LIBRARY_PATH); ./cclover2 code/command.cl && ./clover2 code/command.clo

File renamed without changes.
1 change: 1 addition & 0 deletions a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ABCDEFGHI
3 changes: 3 additions & 0 deletions b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
aaa
bbb
ccc
2 changes: 2 additions & 0 deletions c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
abc
def
2 changes: 2 additions & 0 deletions code/command.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Command.ls("-al").less("-q");
18 changes: 18 additions & 0 deletions code/dir.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
dir:Directory = new Directory("test_dir");

entries:SortableList<String> = new SortableList<String>();

while(true) {
entry:String = dir.readdir();

if(entry.identifyWith(null)) {
break;
}

entries.add(entry);
}

dir.closedir();

Clover.test("Directory test1", entries.sort().equals(sortable_list {"a", "b", "c", ".", ".."}.sort()));

2 changes: 1 addition & 1 deletion code/file.cl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Clover.test("file test8", p"/home/ab25cq/repo/clover/a.txt".extname().equals(p"t
Clover.test("file test9", p"/bin".to_stat().groupName().equals("root"));
Clover.test("file test10", p"/bin".to_stat().userName().equals("root"));
Clover.test("file test11", p"a.txt".to_stat().S_ISREG());
Clover.test("file test12", p"a.txt".to_stat().mtime().dayOfMonth() == 11 && p"a.txt".to_stat().mtime().month() == 12);
Clover.test("file test12", p"a.txt".to_stat().mtime().dayOfMonth() == 21 && p"a.txt".to_stat().mtime().month() == 12);
Clover.test("file test13", p"a.txt".read().equals(b"ABCDEFGHI\n"));

f2 := new File("b.txt", System.O_CREAT|System.O_TRUNC|System.O_WRONLY, 0644);
Expand Down
1 change: 1 addition & 0 deletions d.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GGG
Loading

0 comments on commit 3ffa162

Please sign in to comment.