GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 30, 2014 3:51 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: [js] stdin interaction with GIOchannel
PostPosted: Sat Oct 05, 2013 9:10 am 
Offline
Familiar Face

Joined: Fri Nov 28, 2008 8:48 pm
Posts: 5
hi all, i need some help with this script js(gnomeshell-extension); that execute a bash script and show in the log the stdOUT and stdERR exactly but I can not pass data from stdin in bash script to complete the operation; you have any suggestion about it? this is the code:
Code:
/* -*- mode: js; js-basic-offset: 4; indent-tabs-mode: nil -*- */

/*
    Copyright (C) 2013  Borsato Ivano

    The JavaScript code in this page is free software: you can
    redistribute it and/or modify it under the terms of the GNU
    General Public License (GNU GPL) as published by the Free Software
    Foundation, either version 3 of the License, or (at your option)
    any later version.  The code is distributed WITHOUT ANY WARRANTY;
    without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.

    As additional permission under GNU GPL version 3 section 7, you
    may distribute non-source (e.g., minimized or compacted) forms of
    that code without the copy of the GNU GPL normally required by
    section 4, provided you include this license notice and a URL
    through which recipients can access the Corresponding Source.
*/

const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const MessageTray = imports.ui.messageTray;
const Main = imports.ui.main;

const Gettext = imports.gettext.domain('ScriptCase@iacopodeenosee.gmail.com');
const _ = Gettext.gettext;

const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Lib = Me.imports.convenience;
const Pref = Me.imports.prefs;

let Scripts=null;
let notifyOUT=null;
let strPipeOut=null;

const CommScript = new Lang.Class({
    Name: 'ScriptCase.CommunicationScript',

    // Create the application itself
    _init: function(cmd) {
        Lib.TalkativeLog('init Communication script');
       
               
        [success, this.argv] = GLib.shell_parse_argv(cmd);
        if(success){
           
            [success, this.pid,this.std_in,this.std_out,this.std_err] =
                GLib.spawn_async_with_pipes(null,this.argv, null,GLib.SpawnFlags.
                    SEARCH_PATH, function () {}, null, null);
            if(success){
           
                Lib.TalkativeLog('argv '+this.argv);
                Lib.TalkativeLog('pid '+this.pid);
                Lib.TalkativeLog('std_in '+this.std_in);
                Lib.TalkativeLog('std_out '+this.std_out);
                Lib.TalkativeLog('std_err  '+this.std_err);
               
                Lib.TalkativeLog('read log from socket');
               
                this.IOchannelOUT=GLib.IOChannel.unix_new(this.std_out);
                this.IOchannelOUT.set_flags(GLib.IOFlags.IS_READABLE | GLib.IOFlags.NONBLOCK);
                this.tagWatchOUT = GLib.io_add_watch(this.IOchannelOUT,GLib.PRIORITY_DEFAULT,
                    GLib.IOCondition.IN | GLib.IOCondition.HUP | GLib.IOCondition.ERR,
                    this._loadPipeOUT,null,function() {});
                   
                this.IOchannelERR=GLib.IOChannel.unix_new(this.std_err);
                this.IOchannelERR.set_flags(GLib.IOFlags.IS_READABLE | GLib.IOFlags.NONBLOCK);
                this.tagWatchERR = GLib.io_add_watch(this.IOchannelERR,GLib.PRIORITY_DEFAULT,
                    GLib.IOCondition.IN | GLib.IOCondition.HUP | GLib.IOCondition.ERR,
                    this._loadPipeERR,null,function() {});
                   
                this.IOchannelIN=GLib.IOChannel.unix_new(this.std_in);
                this.IOchannelIN.set_flags(GLib.IOFlags.IS_WRITABLE |GLib.IOFlags.IS_READABLE | GLib.IOFlags.NONBLOCK);
                this.tagWatchIN = GLib.io_add_watch(this.IOchannelIN,GLib.PRIORITY_DEFAULT,
                    GLib.IOCondition.OUT | GLib.IOCondition.HUP | GLib.IOCondition.ERR,
                    this._writePipeIN,null,function() {});
            }
        }
    },
   
    _loadPipeOUT: function(channel, condition, data) {
        var length,strLog,ret;
        Lib.TalkativeLog('OUT # Load pipe');
        if (condition === GLib.IOCondition.HUP || condition === GLib.IOCondition.ERR){
            Lib.TalkativeLog('OUT # Read end of pipe died!');
           
            channel.shutdown(true);
            channel.close();
            GLib.source_remove(this.tagWatchOUT);

            return false;
        }
       
        [ret,strLog,length] = channel.read_line();
       
        if (ret === GLib.IOStatus.ERROR){
            Lib.TalkativeLog('OUT # Error reading');
           
            channel.shutdown(true);
            channel.close();
            GLib.source_remove(this.tagWatchOUT);
           
            return false;
        }
        else if (ret === GLib.IOStatus.EOF) {
            Lib.TalkativeLog('OUT # EOF');

            channel.shutdown(true);
            channel.close();
            GLib.source_remove(this.tagWatchOUT);

            return false;
        } else {
            strPipeOut=strPipeOut+strLog;
            Lib.TalkativeLog('OUT # msg -len '+length+' -str '+strLog);
        }

        return true;
    },
   
    _loadPipeERR: function(channel, condition, data) {
        var strLog,length,ret;
        Lib.TalkativeLog('ERR # Load pipe');
        if (condition === GLib.IOCondition.HUP || condition === GLib.IOCondition.ERR){
            Lib.TalkativeLog('ERR # Read end of pipe died!');
           
            channel.shutdown(true);
            channel.close();
            GLib.source_remove(this.tagWatchERR);

            return false;
        }
       
        [ret,strLog,length] = channel.read_line();
       
        if (ret === GLib.IOStatus.ERROR){
            Lib.TalkativeLog('ERR # Error reading');
           
            channel.shutdown(true);
            channel.close();
            GLib.source_remove(this.tagWatchERR);
           
            return false;
        }
        else if (ret === GLib.IOStatus.EOF) {
            Lib.TalkativeLog('ERR # EOF');

            channel.shutdown(true);
            channel.close();
            GLib.source_remove(this.tagWatchERR);
           
            return false;
        } else {
            Lib.TalkativeLog('ERR # msg -len '+length+' -str '+strLog);
        }

        return true;
    },
   
    _writePipeIN: function(channel, condition, data) {
        var strLog,length,ret;
        Lib.TalkativeLog('IN # Load pipe');
        if (condition === GLib.IOCondition.HUP || condition === GLib.IOCondition.ERR){
            Lib.TalkativeLog('IN # Read end of pipe died!');
           
            channel.shutdown(true);
            channel.close();
            GLib.source_remove(this.tagWatchIN);

            return false;
        } else if (condition===GLib.IOCondition.OUT){
            Lib.TalkativeLog('IN # Input session');
           
            var strIP="8.8.4.4";
            var ret = channel.write_chars(strIP,strIP.length);
           
            if (ret === GLib.IOStatus.ERROR){
                Lib.TalkativeLog('IN # Error reading');
               
                channel.shutdown(true);
                channel.close();
                GLib.source_remove(this.tagWatchIN);
               
                return false;
            }
            else if (ret === GLib.IOStatus.EOF) {
                Lib.TalkativeLog('IN # EOF');

                channel.shutdown(true);
                channel.close();
                GLib.source_remove(this.tagWatchIN);
               
                return false;
            } else if (ret === GLib.IOStatus.NORMAL)  {
                Lib.TalkativeLog('IN # event NORMAL');
               
                return false;
            } else if (ret === GLib.IOStatus.AGAIN){
                Lib.TalkativeLog('IN # event AGAIN');
               
                return true;
            }
        } else
            return true;
    }
});


function connectScript(cmd) {
    Lib.TalkativeLog('connection to communication script');

    if(!Scripts){
        Scripts = new CommScript(cmd);
    }
};

the bash script is simply this:
Code:
#!/bin/bash
echo "enter ip andress"
read -p "Enter ip: " IP
echo $IP

ping -c 3 $IP

thx. 8y3


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group