<commit_before>/**
 * This file is part of Erjang - A JVM-based Erlang VM
 *
 * Copyright (c) 2009 by Trifork
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

package erjang.driver;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import erjang.EAtom;
import erjang.EBitString;
import erjang.ECons;
import erjang.EObject;
import erjang.EProc;
import erjang.ERT;
import erjang.ESeq;
import erjang.EString;
import erjang.ETuple2;
import erjang.ErlangError;
import erjang.NotImplemented;
import erjang.m.erlang.ErlPort;


/**
 * UNUSED CLASS I KEEP AROUND BECAUSE IT HAS SOME CODE I MAY USE LATER
 * 
 * A n EPort instance that corresponds to an external executable
 */
public class EExecDriverTask extends EDriverTask {

	private Process process;
	
	private DataOutputStream out;
	private DataInputStream in;

	private DataInputStream err;

	private ETuple2 name;
	
	/**
	 * @param owner
	 * @param name
	 * @param portSetting
	 */
	public EExecDriverTask(EProc owner, ETuple2 name, EObject portSetting) {
			super(owner.self_handle(), new ExecDriverInstance(name));

			this.name = name;
			
		// argument can be any list, ... turn it into a string
		ESeq es = name.elem2.testString();
		if (es == null) {		
			ECons cons;
			EAtom am;
			if ((cons = name.elem2.testCons()) != null) {
				es = EString.make(cons);
			} else if ((am = name.elem2.testAtom()) != null) {
				es = EString.fromString(am.getName());
			} else {
				throw ERT.badarg(name, portSetting);
			}
		}
		
		String full_cmd = es.stringValue();
		
		String[] cmd;
		if (name.elem1 == ErlPort.am_spawn) {
			cmd = full_cmd.split(" +");
		} else {
			cmd = new String[] { full_cmd };
		}
		
		parseOptions(cmd, portSetting);
		

		String[] envp = new String[env.size()];
		int pos = 0;
		for (Map.Entry<String, String> e : env.entrySet()) {
			envp[pos++] = e.getKey() + "=" + e.getValue();
		}

		
		File f = new File(cmd[0]);
		if (f.exists() && f.canExecute()) {
			// ok !
		} else if (f.isAbsolute()) {
			throw new ErlangError(EAtom.intern("enoent"));
		} else {

			String cmd_path = env.get("PATH");
			for (String elm : cmd_path.split(File.pathSeparator)) {
				File dir = new File(elm);
				f = new File(dir, cmd[0]);
				if (f.exists()) {
					cmd[0] = f.getAbsolutePath();
					break;
				} else {
					f = null;
				}
			}
		}

		System.err.println("executing "+f);		
		
		if (f == null) {
			throw new ErlangError(EAtom.intern("enoent"));
		}

		if (!f.canExecute()) {
			throw new ErlangError(EAtom.intern("eaccess"));
		}

		try {
			this.process = Runtime.getRuntime().exec(cmd, envp, new File(cwd));
		} catch (IOException e1) {
			throw new ErlangError(e1);
		}
		
		List<String> al = new ArrayList<String>();
		Collections.addAll(al, cmd);
		
		System.err.println("EXEC "+ al);

		this.out = new DataOutputStream(this.process.getOutputStream());
		this.in = new DataInputStream(this.process.getInputStream());
		this.err = new DataInputStream(this.process.getErrorStream());
	}

	public void send(EObject val) {
		
		// first, grok argument
		ByteBuffer buf;
		<commit_msg>Debug message for exec drivers<commit_after>/**
 * This file is part of Erjang - A JVM-based Erlang VM
 *
 * Copyright (c) 2009 by Trifork
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

package erjang.driver;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import erjang.EAtom;
import erjang.EBitString;
import erjang.ECons;
import erjang.EObject;
import erjang.EProc;
import erjang.ERT;
import erjang.ESeq;
import erjang.EString;
import erjang.ETuple2;
import erjang.ErlangError;
import erjang.NotImplemented;
import erjang.m.erlang.ErlPort;


/**
 * UNUSED CLASS I KEEP AROUND BECAUSE IT HAS SOME CODE I MAY USE LATER
 * 
 * A n EPort instance that corresponds to an external executable
 */
public class EExecDriverTask extends EDriverTask {

	private Process process;
	
	private DataOutputStream out;
	private DataInputStream in;

	private DataInputStream err;

	private ETuple2 name;
	
	/**
	 * @param owner
	 * @param name
	 * @param portSetting
	 */
	public EExecDriverTask(EProc owner, ETuple2 name, EObject portSetting) {
			super(owner.self_handle(), new ExecDriverInstance(name));

			this.name = name;
			
		// argument can be any list, ... turn it into a string
		ESeq es = name.elem2.testString();
		if (es == null) {		
			ECons cons;
			EAtom am;
			if ((cons = name.elem2.testCons()) != null) {
				es = EString.make(cons);
			} else if ((am = name.elem2.testAtom()) != null) {
				es = EString.fromString(am.getName());
			} else {
				throw ERT.badarg(name, portSetting);
			}
		}
		
		String full_cmd = es.stringValue();
		
		String[] cmd;
		if (name.elem1 == ErlPort.am_spawn) {
			cmd = full_cmd.split(" +");
		} else {
			cmd = new String[] { full_cmd };
		}
		
		parseOptions(cmd, portSetting);
		

		String[] envp = new String[env.size()];
		int pos = 0;
		for (Map.Entry<String, String> e : env.entrySet()) {
			envp[pos++] = e.getKey() + "=" + e.getValue();
		}

		
		File f = new File(cmd[0]);
		if (f.exists() && f.canExecute()) {
			// ok !
		} else if (f.isAbsolute()) {
			throw new ErlangError(EAtom.intern("enoent"));
		} else {

			String cmd_path = env.get("PATH");
			for (String elm : cmd_path.split(File.pathSeparator)) {
				File dir = new File(elm);
				f = new File(dir, cmd[0]);
				if (f.exists()) {
					cmd[0] = f.getAbsolutePath();
					break;
				} else {
					f = null;
				}
			}
		}

		System.err.println("lauching "+cmd[0]+" "+(f==null?"(not found)":""));		
		
		if (f == null) {
			throw new ErlangError(EAtom.intern("enoent"));
		}

		if (!f.canExecute()) {
			throw new ErlangError(EAtom.intern("eaccess"));
		}

		try {
			this.process = Runtime.getRuntime().exec(cmd, envp, new File(cwd));
		} catch (IOException e1) {
			throw new ErlangError(e1);
		}
		
		List<String> al = new ArrayList<String>();
		Collections.addAll(al, cmd);
		
		System.err.println("EXEC "+ al);

		this.out = new DataOutputStream(this.process.getOutputStream());
		this.in = new DataInputStream(this.process.getInputStream());
		this.err = new DataInputStream(this.process.getErrorStream());
	}

	public void send(EObject val) {
		
		// first, grok argument
		ByteBuffer buf;
		
{'fraction_non_alphanumeric': 0.13157894736842105, 'fraction_numerical': 0.005657894736842106, 'mean_word_length': 3.4166182452062754, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1209130', 'n_tokens_mistral': 2860, 'n_tokens_neox': 2499, 'n_words': 722}