summaryrefslogtreecommitdiff
path: root/lib/custom_command.tcl
diff options
context:
space:
mode:
Diffstat (limited to 'lib/custom_command.tcl')
-rwxr-xr-xlib/custom_command.tcl98
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/custom_command.tcl b/lib/custom_command.tcl
new file mode 100755
index 0000000..63a42c5
--- /dev/null
+++ b/lib/custom_command.tcl
@@ -0,0 +1,98 @@
+#!/usr/bin/tclsh
+# Part of MCU 8051 IDE ( http://mcu8051ide.sf.net )
+
+############################################################################
+# Copyright (C) 2007-2009 by Martin Ošmera #
+# martin.osmera@gmail.com #
+# #
+# This program is free software; you can redistribute it and#or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation; either version 2 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program; if not, write to the #
+# Free Software Foundation, Inc., #
+# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
+############################################################################
+
+# --------------------------------------------------------------------------
+# DESCRIPTION
+# Execute custom command
+# USAGE:
+# set pid [exec -- tclsh custom_command.tcl [tk appname] $custom_command_NUM($cmd_num) &]
+# --------------------------------------------------------------------------
+
+# Initialize
+package require Tk
+wm withdraw .
+wm command . "$argv0 $argv"
+wm client . [info hostname]
+
+## Perform secure send command
+ # Secure means that it will not crash or something like that in case of any errors.
+ # But instead it will popup an error message to the user (Tk dialog).
+ # @parm List args - Arguments for the send command
+ # @return void
+proc secure_send args {
+ if {[catch {
+ eval "send $args"
+ } result]} {
+ puts "Unknown IO Error :: $result"
+ tk_messageBox \
+ -title "Unknown IO Error" \
+ -icon error \
+ -type ok \
+ -message "$result"
+
+ if {[ \
+ tk_messageBox \
+ -title "X server security workaround" \
+ -icon warning \
+ -type yesno \
+ -message "If the error was related to X server security, it is possible to temporarily workaround it by running this command: \"/bin/sh << 'for i in `xhost`; do xhost -\$i; done'\"\n\nDo you want to do it ?"
+ ] == {yes}} then {
+ catch {
+ exec -- /bin/sh << {xhost -; for i in `xhost`; do xhost -$i; done}
+ }
+ } else {
+ exit 1
+ }
+
+ if {[catch {
+ eval "send $args"
+ }]} {
+ tk_messageBox \
+ -title "Workaround failed" \
+ -icon error \
+ -type ok \
+ -message "Sorry this doesn't work ...\nIt's a strange bug somewhere in your operating system"
+ exit 1
+ }
+
+ return 1
+
+ } else {
+ return 1
+ }
+}
+
+# Load command from standard input
+set cmd {}
+while {![eof stdin]} {
+ append cmd [gets stdin] "\n"
+}
+
+# Execute loaded command
+if {[catch {exec /bin/sh << $cmd} result]} {
+ secure_send [lindex $argv 0] ::X::custom_cmd_error [lindex $argv 1] "{" [regsub -all {[\{\}]} $result {\\&}] "}"
+} else {
+ secure_send [lindex $argv 0] ::X::custom_cmd_finish [lindex $argv 1] "{" [regsub -all {[\{\}]} $result {\\&}] "}"
+}
+
+exit 0