File: xcmd.mpt

package info (click to toggle)
fis-gtm 6.2-000-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 30,784 kB
  • ctags: 42,554
  • sloc: ansic: 358,483; asm: 4,847; csh: 4,574; sh: 2,261; awk: 200; makefile: 86; sed: 13
file content (91 lines) | stat: -rw-r--r-- 3,613 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;								;
;	Copyright  2012 Fidelity Information Services, Inc.    	;
;								;
;	This source code contains the intellectual property	;
;	of its copyright holder(s), and is made available	;
;	under a license.  If you do not know the terms of	;
;	the license, please stop and do not read further.	;
;								;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	; Utility to execute a shell command and return a non-zero status on error
	;
%XCMD	; Usage: mumps -run %XCMD '<string>'
	; If no $ETRAP defined, use CLIERR^%XCMD overriding a potential $ZTRAP error handler
	if ""=$ETRAP new $ETRAP set $ETRAP="goto CLIERR^%XCMD"
	new etrap set etrap=$ETRAP
	; Protect %XCMD's error handler by NEWing and SETing $ETRAP at the beginning of the XECUTEd command
	xecute "new $ETRAP set $ETRAP=etrap "_$zcmdline
	quit

CLIERR
	for i=1:1:$length($zstatus,",")  quit:i>$length($zstatus,",")  do
	. if $piece($zstatus,",",i)?1(1"%",1"-")3.U1"-"1U1"-".E do 	; split on GTM error format
	. . write:$data(lasti) $piece($zstatus,",",lasti,i-1),!
	. . set lasti=i
	write $piece($zstatus,",",$get(lasti,3),i),!
	zhalt +$piece($zstatus,",",1)
	quit

	; Perform a given command on every line of input
	;
LOOP	; Usage: mumps -run LOOP^%XCMD [--before=|<string>|] [--after=|<string>|] --xec=|<string>|
	; If no $ETRAP defined, use LOOPERR^%XCMD overriding a potential $ZTRAP error handler
	if ""=$ETRAP new $ETRAP set $ETRAP="goto LOOPERR^%XCMD"
	new %cli,%l,%NR,%xcmd,etrap
	set %cli=$zcmdline,etrap=$ETRAP
	for  quit:'$$trimleadingstr(.%cli,"--")  do 	; process command line options
	. if $$trimleadingstr(.%cli,"after=") set %xcmd("after")=$$trimleadingdelimstr(.%cli)
	. else  if $$trimleadingstr(.%cli,"before=") set %xcmd("before")=$$trimleadingdelimstr(.%cli)
	. else  if $$trimleadingstr(.%cli,"xec=") set %xcmd("xec")=$$trimleadingdelimstr(.%cli)
	. else  set $ecode=",U254,"
	. if $$trimleadingstr(.%cli," ")
	set:'$length($get(%xcmd("xec"))) $ecode=",U253,"
	set:$length(%cli) $ecode=",U252,"
	kill %cli
	do cmd($get(%xcmd("before")),0,"",etrap)
	for %NR=1:1 read %l quit:$zeof  do cmd(%xcmd("xec"),%NR,%l,etrap)
	do cmd($get(%xcmd("after")),%NR,%l,etrap)
	quit

cmd(cmd,%NR,%l,ltrap)
	quit:$length(cmd)=0
	; Protect LOOP^%XCMD's internal variables %xcmd and etrap and $ETRAP from modification by the XECUTEd command
	new %xcmd,etrap,$ETRAP
	set $ETRAP=ltrap
	xecute cmd
	quit

LOOPERR
	; attempt to trap internal errors
	set uecode=$piece($ecode,",",2),uemsg=$text(@uecode)
	if $length(uemsg) write $text(+0),@$piece(uemsg,";",2),! zhalt +$extract(uecode,2,$length(uecode))
	use $principal
	write $zstatus,!
	zhalt 1
	quit

	; Remove and optionally return leading delimited string from str
trimleadingdelimstr(str)
	new delim,substr
	set delim=$extract(str,1)
	set substr=$piece(str,delim,2)
	set str=$extract(str,$length(substr)+3,$length(str))
	quit:$quit substr quit

	; Remove and optionally return first piece of s with space as piece separator
trimleadingpiece(str)
	new tmp
	set tmp=$piece(str," ",1)
	set str=$piece(str," ",2,$length(str," "))
	quit:$quit tmp quit

	; Return s without leading $length(x) characters; return 1/0 if called as function
trimleadingstr(str,x)
	if x=$extract(str,1,$length(x)) set str=$extract(str,$length(x)+1,$length(str)) quit:$quit 1 quit
	else  quit:$quit 0 quit

;	Error message texts
U252	;"-F-UNRECCMD Unrecognized commands starting with "_%cli
U253	;"-F-EMPTYXEC String to Xecute with --xec is required but not provided"
U254	;"-F-ILLEGALCMD Illegal command line option(s) starting with --"_%cli