Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sclang: introduce unixCmd for array of arguments #1856

Merged
merged 1 commit into from
Mar 17, 2016

Conversation

miguel-negrao
Copy link
Member

fixes #1738

Incorporated some of @timblechmann 's feedback.

  • changed from malloc/free to new/delete. I wasn't able to use unique_ptr for sc_process due to some very complicated type error.
  • removed un-const castings.
  • moved function to the same file as other unix primitives. There was no reason for it be in the array primitives file.

tested with:

["/bin/echo","hello","world"].unixCmd
["/bin/echo",1,"world"].unixCmd
[].unixCmd

x = ["/usr/local/bin/scsynth","-u","57128"].unixCmd

pid at x checks out with pgrep.


if (!isKindOfSlot(a, class_string)) return errWrongType;

char *cmdline = (char*)malloc(slotRawObject(a)->size + 1);
err = slotStrVal(a, cmdline, slotRawObject(a)->size + 1);
if(err) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These error checks are not necessary because above the slot was already checked for class_string which is the same check done inside slotStrVal

@crucialfelix
Copy link
Member

If the command is not found or the process exits with an error, it throws a primitive failed, right ?

Is there anyway to easily get both the stdout and the error code ? See Pipe *call / *callSync

@miguel-negrao
Copy link
Member Author

If the command is not found or the process exits with an error, it throws a primitive failed, right ?

No. if execve fails error 127 is reported. If the process exits with an error that error is reported (posted). No primitive failed error it thrown.

    res = WEXITSTATUS(res);

    if(process->postOutput)
        postfl("RESULT = %d\n", res);

Is there anyway to easily get both the stdout and the error code ? See Pipe *call / *callSync

You can get stdOut via unixCmdGetStdOut which uses Pipe internally. If there is an error the error goes to stderr not stdout, therefore it won't be displayed with unixCmdGetStdOut. You can however redirect stderr to stdout in the shell:

"ls; ls --asdasd 2>&1".unixCmdGetStdOut

@jamshark70
Copy link
Contributor

Felix - check the unixCmd help:

http://doc.sccode.org/Classes/String.html#-unixCmd

Arguments:
action
A Function that is called when the process has exited. It is passed two arguments: the exit code and pid of the exited process.

unixCmd is asynchronous, so it can't throw an error in SC if the child process fails.

std::vector<char> v2( str2.begin(), str2.end() );
v2.push_back('\0');
argv[2] = v2.data();
argv[3] = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nullptr is a bit more typesafe than NULL

@miguel-negrao
Copy link
Member Author

I don't understand why when I use vector for the argv strings, although they are printed correctly by cout, for instance 'echo' will not print anything. I'm I doing something wrong ?

@@ -229,9 +230,11 @@ int prArrayPOpen(struct VMGlobals *g, int numArgsPushed)
                        PyrSlot argSlot;
                        getIndexedSlot(obj, &argSlot, i);
                        if (!isKindOfSlot(&argSlot, class_string)) return errWrongType;
-                       char *arg = new char[slotRawObject(&argSlot)->size + 1];
-                       slotStrVal(&argSlot, arg, slotRawObject(&argSlot)->size + 1);
-                       argv[i] = arg;
+                       //char *arg = new char[slotRawObject(&argSlot)->size + 1];
+                       std::vector<char> arg(slotRawObject(&argSlot)->size + 1);
+                       slotStrVal(&argSlot, arg.data(), slotRawObject(&argSlot)->size + 1);
+                       argv[i] = arg.data();
+                       std::cout << "**" << argv[i] << "**\n";
                }
        }

@@ -249,9 +252,9 @@ int prArrayPOpen(struct VMGlobals *g, int numArgsPushed)
        thread thread(std::bind(string_popen_thread_func, process));
        thread.detach();

-       for (int i=1; i<obj->size; ++i) {
+       /*for (int i=1; i<obj->size; ++i) {
                delete [] argv[i];
-       }
+       }*/

        SetInt(a, process->pid);
        return errNone;

running: ["/bin/echo","hello","world"].unixCmd; 0.exit

*** Welcome to SuperCollider 3.7alpha1. *** For help type ctrl-c ctrl-h (Emacs) or :SChelp (vim) or ctrl-U (sced/gedit).
**hello**
**world**

RESULT = 0
empty
cleaning up OSC
terminate called without an active exception
Aborted

@miguel-negrao miguel-negrao force-pushed the fix1738-forMaster branch 5 times, most recently from 6327573 to 9a7b145 Compare February 8, 2016 14:10
@miguel-negrao
Copy link
Member Author

Except for the unique_ptr I think I addressed the other issues. Also, this doesn't work for windows, so for windows the primitive will just post a message, not sure that is the proper to deal with this. I'm not familiar with the windows process code to do changes there.

@miguel-negrao miguel-negrao force-pushed the fix1738-forMaster branch 2 times, most recently from aa52d09 to ea4641c Compare February 8, 2016 14:31
@miguel-negrao
Copy link
Member Author

Doesn't build for osx:

/Users/travis/build/supercollider/supercollider/common/sc_popen.cpp:37:14: error: implicit instantiation of undefined template 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >'

        std::string str0 = "/bin/sh";

                    ^

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:188:33: note: template is declared here

    class _LIBCPP_TYPE_VIS_ONLY basic_string;

                                ^

What could be the issue ?

Perhaps #include was missing. Added that, let's see if it builds.

edit: ok, it builds now.

@crucialfelix crucialfelix added this to the 3.8 milestone Mar 6, 2016
crucialfelix added a commit that referenced this pull request Mar 17, 2016
sclang: introduce unixCmd for array of arguments
@crucialfelix crucialfelix merged commit 7c652b6 into supercollider:master Mar 17, 2016
@miguel-negrao miguel-negrao deleted the fix1738-forMaster branch February 28, 2018 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants