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

bug report: process cmd error on Linux #2

Closed
LycsHub opened this issue Mar 1, 2022 · 5 comments
Closed

bug report: process cmd error on Linux #2

LycsHub opened this issue Mar 1, 2022 · 5 comments

Comments

@LycsHub
Copy link

LycsHub commented Mar 1, 2022

Env

  • OS: Pop!_OS 20.04 LTS(base on Ubuntu 20.04)

Log

bQ8YJH.md.png

Reason

cmd = new String[]{"/bin/sh", "-c", "dot", "-Tpng" + dotPath[i] + "-o", imgPath};

Like windows if branch , replace "+" to "," may fix the bug.

@SummerSec
Copy link
Owner

thank you,I fixed this problem in the official version.

@LycsHub
Copy link
Author

LycsHub commented Mar 1, 2022

hey bro, thx your quick fix,but its still has some problems.
At the same place , I edit some source code to compatible with Linux and it work good for me.

       if (isWindowsOS) {
                //windows下的命令
                cmd = new String[]{"cmd.exe", "/c", "dot", "-Tpng", dotPath[i], "-o", imgPath};
            } else {
                //linux下的命令
//              imgPath =  ConstatField.ASTResultTemp  + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("\\") + 1, dotPath[i].lastIndexOf(".")) + ".png";

                // fixme:for example path ,change yourself, the imgPath in unix will be like:
                // $home/proj/tools/datas/result/ast/1225819235468336//$home/proj/tools/datas/ast/1225819235466361/1225833393958640.png
                // has tow slash in imgPath
                imgPath = ConstatField.ASTResultTemp + ConstatField.separator + dotPath[i].lastIndexOf(".") + ".png";
                //fixme: use `/usr/bash -c` may has problem in some shell like  zsh
                cmd = new String[]{"dot", "-Tpng" , dotPath[i] , "-o", imgPath};
            }
            try {
                logger.info("开始生成图片 " + dotPath[i]);
                logger.info("正在执行命令 "+ Arrays.toString(cmd));
                sleep(1000);
                String charsetName = isWindowsOS ? "GBK" : "UTF-8";
                Runtime.getRuntime().exec(cmd);
//                byte[] bytes = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A").next().getBytes(charsetName);

                byte[] bytes = null;
                Scanner runCmd = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A");
                //fixme: on linux : detect hasNext otherwise will throw a NoSuchElementException
                if (runCmd.hasNext())
                    bytes = runCmd.next().getBytes(charsetName);
//...

by the way the cfg module may same problems

@SummerSec
Copy link
Owner

Thanks for the suggestion, this looks great, can you submit a pull request for this project?

hey bro, thx your quick fix,but its still has some problems. At the same place , I edit some source code to compatible with Linux and it work good for me.

       if (isWindowsOS) {
                //windows下的命令
                cmd = new String[]{"cmd.exe", "/c", "dot", "-Tpng", dotPath[i], "-o", imgPath};
            } else {
                //linux下的命令
//              imgPath =  ConstatField.ASTResultTemp  + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("\\") + 1, dotPath[i].lastIndexOf(".")) + ".png";

                // fixme:for example path ,change yourself, the imgPath in unix will be like:
                // $home/proj/tools/datas/result/ast/1225819235468336//$home/proj/tools/datas/ast/1225819235466361/1225833393958640.png
                // has tow slash in imgPath
                imgPath = ConstatField.ASTResultTemp + ConstatField.separator + dotPath[i].lastIndexOf(".") + ".png";
                //fixme: use `/usr/bash -c` may has problem in some shell like  zsh
                cmd = new String[]{"dot", "-Tpng" , dotPath[i] , "-o", imgPath};
            }
            try {
                logger.info("开始生成图片 " + dotPath[i]);
                logger.info("正在执行命令 "+ Arrays.toString(cmd));
                sleep(1000);
                String charsetName = isWindowsOS ? "GBK" : "UTF-8";
                Runtime.getRuntime().exec(cmd);
//                byte[] bytes = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A").next().getBytes(charsetName);

                byte[] bytes = null;
                Scanner runCmd = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A");
                //fixme: on linux : detect hasNext otherwise will throw a NoSuchElementException
                if (runCmd.hasNext())
                    bytes = runCmd.next().getBytes(charsetName);
//...

by the way the cfg module may same problems

@SummerSec
Copy link
Owner

Because my OS is Windows, I can't do the test well on Linux environment. If it is convenient for you, could you please submit a pr?

@LycsHub
Copy link
Author

LycsHub commented Mar 1, 2022

ok , I'll submit a pr later :)

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

No branches or pull requests

2 participants