Jump to content

[SOLVED] CLI closes without error after inputting text with sdtin


XaeroDegreaz

Recommended Posts

Hi, thanks for taking the time to check out this post.

 

To make a long story short, I'm taking input from the command line, and using that input to make a decision, and then load another class based on that decision. For some reason it keeps exiting, without loading the class and without error:

 

This is the first part of the code:

 

$xml = simplexml_load_file("Config.xml");

$em = new ExtensionManager();

$extension = $em->ScanExtensions((string)$xml->default_extension);

echo("This text will not appear");

//# This seems to call, but it exits immediately afterwards and it SHOULDN'T because inside is an infinite loop.
$e = new $extenstion();

 

Here is the ScanExtension method:

 

public function ScanExtensions($defaultExtension) {
Logger::log(__CLASS__, "Listing valid extensions...");

if ($handle = opendir('./Extensions')) {

	echo("*********************************************\n");
	$i = 1;
	$list = array();
	while (false !== ($file = readdir($handle))) {

		if ($file != "." && $file != ".." && is_dir("./Extensions/$file")) {

			if(file_exists("./Extensions/$file/$file.php")) {
				echo("[$i]\t$file\n");
				$list[] = $file;
				$i++;
			}

		}

	}
	closedir($handle);
	echo("*********************************************\n");
}

$out = fopen("php://stdout", "w");
$in = fopen("php://stdin", "r");

fwrite($out, "Which project should I launch? [blank = $defaultExtension]: ");
$ProjectName = trim(fgets($in));

fclose($out);
fclose($in);

if($ProjectName > 0) {
	//# This text will not appear.. Logger::log is a simple echo class/method I have created
	Logger::log(__CLASS__, "Loading extension ".$list[$ProjectName-1].".");
	//sleep(3);
	$ext =  $list[$ProjectName-1];
}else {
	//# Neither will this logger output appear.
	Logger::log(__CLASS__, "Loading default extension $defaultExtension.");
	//sleep(3);
	$ext = $defaultExtension;
}

return $ext;

}

 

This method goes through everything fine. It accepts user input and seems to return the correct string stored in $ext (I know it is working because I purposely made some errors in the class it should call, and it appears in my log file)

 

It just simply exits without any errors at all! I have error_reporting set to E_ALL. There is nothing wrong with the class file that gets called, because I can launch it just fine by commenting out the call to the ScanExtension method, and typing $e = new Example() in the first block of outlined code above.

 

I'm not sure what's going on. I have tried taking the ScanExtension code from the method and just placing it beneath the first code block outlined above, and I get the same results.

 

Any help at all would be greatly appreciated!

 

 

Link to comment
Share on other sites

It runs all the way throgh to the return statement, but it seems to dislike outputing code into the console in that method, for some reason.

 

This project is actually pretty large and I have been coding it myself for several months now. I just decided for my next release that I would like to have the startup a little more interactive by allowing users to create a project through the console (which oddly enough I'm using stdin for user input control and it works for the project creator) and to also allow them to choose which extension they would like to run.

 

It's open source and available at https://launchpad.net/smartsocket

 

However, the current additions that I am doing are not yet pushed to the repository.

Link to comment
Share on other sites

I trouble shooted all the way through the code and finally came to the conclusion that stdout was the culprit.

 

After juggling around the code, I can get it to work if I don't fclose stdout. It seems as though you cannot close stdout if you are gonna echo more text to screen? Is that what it is??

 

$out = fopen("php://stdout", "w");
$in = fopen("php://stdin", "r");

fwrite($out, "Which project should I launch? [blank = $defaultExtension]: ");
$ProjectName = trim(fgets($in));

//# This Logger will print to screen just fine		
Logger::log(__CLASS__, "Testing: $ProjectName / ".$list[(int)$ProjectName]);
fclose($out);
//fclose($in);
//# If I put the logger here, however, the CLI will quit. It is a simple echoing class, outlined below.
//Logger::log(__CLASS__, "Testing: $ProjectName / ".$list[(int)$ProjectName]);

$tst = new $list[(int)$ProjectName]();

 

Logger.php

<?php
Class Logger {

static public function log($class, $message, $die=false) {

	if($die) {
		print("# $class (FATAL): $message\n");
		while(true){};
		exit();
	}

	print("# $class\t: $message\n");
}
}
?>

 

 

See, nothing fancy.

 

I'm happy to have found the cause of my original problem, but now I'm faced with another problem. It takes a few seconds to close the CLI on my own now. It used to be instant. I assume because the CLI is trying to close STDOUT?

 

Is there a way past this?

Link to comment
Share on other sites

I tried that, however, I get the NOTICE that those constants are undefined.

 

[07-Jun-2009 04:24:57] PHP Notice:  Use of undefined constant STDIN - assumed 'STDIN' in D:\...ExtensionManager.php on line 36

[07-Jun-2009 04:24:57] PHP Notice:  Use of undefined constant STDOUT - assumed 'STDOUT' in D:\...ExtensionManager.php on line 36

[07-Jun-2009 04:24:57] PHP Notice:  Use of undefined constant STDERR - assumed 'STDERR' in D:\...ExtensionManager.php on line 36

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.