|
Joel C. wrote: > Is there some kind of QSH initialization or something I have to do > first? You could check this code that wraps the call to that API, is part of the iSeries Toolkit and I've never tried it, just wanting to help.... Carlos K. **************************************************************************** ****************** * Copyright (C) 2002 David M. Morris * * This file is part of the iSeries-toolkit * * The iSeries-toolkit is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License version 2, * * as published by the Free Software Foundation. * * The iseries toolkit 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 file; if not, write to the Free Software Foundation, * * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * You might find a version at http://www.gnu.org/licenses/gpl.txt * * * * Module: QSH * * By: David Morris * * Purpose: Environment variable procedures * **************************************************************************** ****************** H COPYRIGHT('+ H Copyright (C) 2002 David M. Morris + H This module is part of the iSeries-toolkit and is free software; you + H can redistribute it and/or modify it under the terms of the GNU General + H Public License version 2 as published by the Free Software Foundation.') H NoMain H OPTION(*SRCSTMT) /COPY QPROTOSRC,Qsh /COPY QPROTOSRC,Unix /COPY QPROTOSRC,MsgTkt **************************************************************************** ****************** * QzshSystemAPI Run a Qshell Command. * * Input: * * Cmd Command to run. * * Return: * * ErrCod Error code (0=successful, -1=error) * **************************************************************************** ****************** DQzshSystem PR 10I 0 EXTPROC('QzshSystem') D PR_Cmd * VALUE OPTIONS(*STRING) DQzshCheckShellCommand... D PR 10I 0 extproc('QzshCheckShellCommand') D PR_Cmd * VALUE OPTIONS(*string) D PR_Pth * VALUE OPTIONS(*string) D WIFEXITED PR 10I 0 D Status 10I 0 value D WIFSIGNALED PR 10I 0 D Status 10I 0 value D WIFSTOPPED PR 10I 0 D Status 10I 0 value D WIFEXCEPTION PR 10I 0 D Status 10I 0 value D WEXITSTATUS PR 10I 0 D Status 10I 0 value D WSTOPSIG PR 10I 0 D Status 10I 0 value D WTERMSIG PR 10I 0 D Status 10I 0 value D WEXCEPTNUMBER PR 10I 0 D Status 10I 0 value D WEXCEPTMSGID PR 7A D Status 10I 0 value /TITLE Qsh - Run a Qshell Command. **************************************************************************** ****************** * *Qsh Run a Qshell Command. * **************************************************************************** ****************** PQsh B EXPORT DQsh PI LIKE(RtnErr) D Cmd 5000A CONST VARYING ********************* * Return Error Flag * ********************* DRtnErr S N ********************* * Working Variables * ********************* DRtnCod S 10I 0 DMsg S 50A DPos S 5U 0 DStdIn S 10I 0 DClsStdIn S N INZ(*OFF) DStdOut S 10I 0 DClsStdOut S N INZ(*OFF) DStdErr S 10I 0 DClsStdErr S N INZ(*OFF) ************************************** * Open Flags for Standard Unix Files * ************************************** DOpnFlg DS D OpnFlgErr N D OpnFlgPrv N C ' ' CHECK Cmd Pos C* Command must be entered. C IF Pos = *ZEROS C CALLP SndMsg(*OMIT: 'Command passed is blank.') C ELSE C* Trim out command and check to see if it exists. C EVAL Pos = %SCAN(' ': Cmd: Pos) - 1 C EVAL RtnCod = QzshCheckShellCommand( C %SUBST(Cmd:1:Pos): C *NULL) C IF RtnCod <> *ZEROS C CALLP SndUnixErr('Error in Qsh Command: ' + C %TRIM(Cmd)) C ELSE C* Open STDIN, STDOUT, STDERR because this is UNIX -- set flag to C* indicate whether file should be close when this routine ends. C* C EVAL OpnFlg = OpnStdIn(StdIn) C IF OpnFlgErr C EVAL RtnErr = *ON C CALLP SndMsg(*OMIT: 'Error opening STDIN.') C ELSE C EVAL ClsStdIn = NOT OpnFlgPrv C EVAL RtnErr = OpnStdOut(StdOut) C IF OpnFlgErr C EVAL RtnErr = *ON C CALLP SndMsg(*OMIT: 'Error opening STDOUT.') C ELSE C EVAL ClsStdOut = NOT OpnFlgPrv C EVAL RtnErr = OpnStdErr(StdErr) C IF OpnFlgErr C EVAL RtnErr = *ON C CALLP SndMsg(*OMIT: 'Error opening STDERR.') C ELSE C* Files opened OK C EVAL ClsStdErr = NOT OpnFlgPrv C EVAL RtnCod = QzshSystem(Cmd) C* C SELECT C WHEN WIFEXITED(RtnCod) <> *ZEROS AND C WEXITSTATUS(RtnCod) = *ZEROS C EVAL msg = 'Exit status = 0 (success)' C dsply msg C* C WHEN WIFEXITED(RtnCod) <> 0 C EVAL msg = 'Exit status = ' + C %editc(WEXITSTATUS(RtnCod): 'N') C dsply msg C* C WHEN WIFSIGNALED(RtnCod) <> 0 C EVAL msg = 'Ended with signal ' + C %editc(WTERMSIG(RtnCod): 'N') C dsply msg C* C WHEN WIFEXCEPTION(RtnCod) <> 0 C EVAL msg = 'Ended with error ' + C WEXCEPTMSGID(RtnCod) C dsply msg C ENDSL C* C IF RtnCod <> *ZEROS C CALLP SndUnixErr('Error in Qsh Command: ' + C %TRIM(Cmd)) C EVAL RtnErr = *ON C ENDIF C ENDIF C ENDIF C ENDIF C* Close Unix Files C IF ClsStdIn C CALLP ClsIFSFil(StdIn) C ENDIF C IF ClsStdIn C CALLP ClsIFSFil(StdErr) C ENDIF C IF ClsStdIn C CALLP ClsIFSFil(StdOut) C ENDIF C ENDIF C ENDIF C* C RETURN RtnErr PQsh E *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Evaluates to a non-zero value if child terminated normally. * #define WIFEXITED(x) (((x) & 0xFFFF0000) ? 0 : 0x00010000) *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P WIFEXITED B export D WIFEXITED PI 10I 0 D Status 10I 0 value D DS D binary 1 4I 0 D alpha1 1 1A D alpha2 2 2A D alpha3 3 3A D alpha4 4 4A c eval binary = Status c bitoff x'FF' alpha3 c bitoff x'FF' alpha4 c if binary <> 0 c return 0 c else c eval binary = 0 c biton x'01' alpha2 c return binary c endif P E *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Evaluates to a non-zero value if child terminated abnormally. * #define WIFSIGNALED(x) ((x) & 0x00020000) *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P WIFSIGNALED B export D WIFSIGNALED PI 10I 0 D Status 10I 0 value D DS D binary 1 4I 0 D alpha1 1 1A D alpha2 2 2A D alpha3 3 3A D alpha4 4 4A C eval binary = Status C bitoff x'FF' alpha1 C bitoff x'FD' alpha2 C bitoff x'FF' alpha3 C bitoff x'FF' alpha4 C return binary P E *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Evaluates to a non-zero value if status returned for a stopped * child. * #define WIFSTOPPED(x) ((x) & 0x00040000) *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P WIFSTOPPED B export D WIFSTOPPED PI 10I 0 D Status 10I 0 value D DS D binary 1 4I 0 D alpha1 1 1A D alpha2 2 2A D alpha3 3 3A D alpha4 4 4A c eval binary = Status c bitoff x'FF' alpha1 c bitoff x'FB' alpha2 c bitoff x'FF' alpha3 c bitoff x'FF' alpha4 c return binary P E *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Evaluates to a non-zero value if status returned for a * child process that terminated due to an error state. * #define WIFEXCEPTION(x) ((x) & 0x00080000) *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P WIFEXCEPTION B export D WIFEXCEPTION PI 10I 0 D Status 10I 0 value D DS D binary 1 4I 0 D alpha1 1 1A D alpha2 2 2A D alpha3 3 3A D alpha4 4 4A c eval binary = Status c bitoff x'FF' alpha1 c bitoff x'F7' alpha2 c bitoff x'FF' alpha3 c bitoff x'FF' alpha4 c return binary P E *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Evaluates to the low-order 8 bits from the childs exit status. * #define WEXITSTATUS(x) (WIFEXITED(x) ? ((x) & 0x000000FF) : -1) *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P WEXITSTATUS B export D WEXITSTATUS PI 10I 0 D Status 10I 0 value D DS D binary 1 4I 0 D alpha1 1 1A D alpha2 2 2A D alpha3 3 3A D alpha4 4 4A c if WIFEXITED(status) <> 0 c eval binary = status c bitoff x'FF' alpha1 c bitoff x'FF' alpha2 c bitoff x'FF' alpha3 c return binary c else c return -1 c endif P E *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Evaluates to the number of the signal that caused the child to stop. * #define WSTOPSIG(x) (WIFSTOPPED(x) ? ((x) & 0x0000FFFF) : -1) *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P WSTOPSIG B export D WSTOPSIG PI 10I 0 D Status 10I 0 value D DS D binary 1 4I 0 D alpha1 1 1A D alpha2 2 2A D alpha3 3 3A D alpha4 4 4A c if WIFSTOPPED(status) <> 0 c eval binary = status c bitoff x'FF' alpha1 c bitoff x'FF' alpha2 c return binary c else c return -1 c endif P E *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Evaluates to the number of the signal that caused the child to * terminate. * #define WTERMSIG(x) (WIFSIGNALED(x) ? ((x) & 0x0000FFFF) : -1) *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P WTERMSIG B export D WTERMSIG PI 10I 0 D Status 10I 0 value D DS D binary 1 4I 0 D alpha1 1 1A D alpha2 2 2A D alpha3 3 3A D alpha4 4 4A c if WIFSIGNALED(status) <> 0 c eval binary = status c bitoff x'FF' alpha1 c bitoff x'FF' alpha2 c return binary c else c return -1 c endif P E *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Evaluates to the number of the OS/400 Exception that caused the * child to terminate. * #define WEXCEPTNUMBER(x) (WIFEXCEPTION(x) ? ((x) & 0x0000FFFF) : -1) *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P WEXCEPTNUMBER B export D WEXCEPTNUMBER PI 10I 0 D Status 10I 0 value D DS D binary 1 4I 0 D alpha1 1 1A D alpha2 2 2A D alpha3 3 3A D alpha4 4 4A c if WIFEXCEPTION(status) <> 0 c eval binary = status c bitoff x'FF' alpha1 c bitoff x'FF' alpha2 c return binary c else c return -1 c endif P E *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Evaluates to the OS/400 exception msg id for the status code. * returns *blanks if no exception id is indicated. *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P WEXCEPTMSGID B export D WEXCEPTMSGID PI 7A D Status 10I 0 value * Exception numbers returned by WEXCEPTNUMBER() can be turned into * exception id's by using the following process: * * If the number returned (in hex) is 0x0000wwyy, and if zz is the * decimal conversion of ww, then the exception id is "MCHzzyy". * * Example: WEXCEPTNUMBER(my_status) --> 0x00002401 then the * exception id would be "MCH3601". D cvthc PR extproc('cvthc') D OutputHex * value D InputBits * value D OutputSize 10I 0 value D DS D binary 1 4I 0 D alpha1 1 1A D alpha2 2 2A D alpha3 3 3A D alpha4 4 4A D DS D cvtbin 1 2U 0 inz(0) D cvtalpha 2 2A D dsMsgID DS D MCH 1 3A inz('MCH') D first2 4 5A D last2 6 7A c eval binary = WEXCEPTNUMBER(status) c if binary = -1 c return *blanks c endif c eval cvtalpha = alpha3 c move cvtbin first2 c callp cvthc(%addr(last2): %addr(alpha4): 2) c return dsMsgID P E
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.