253 lines
9.8 KiB
HTML
253 lines
9.8 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html lang="en-us" xml:lang="en-us">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="security" content="public" />
|
|
<meta name="Robots" content="index,follow" />
|
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
|
<meta name="DC.Type" content="reference" />
|
|
<meta name="DC.Title" content="Examples: Using the generic terminal APIs" />
|
|
<meta name="abstract" content="These two examples illustrate programs that implement a generic terminal and a simple interpreter." />
|
|
<meta name="description" content="These two examples illustrate programs that implement a generic terminal and a simple interpreter." />
|
|
<meta name="DC.Relation" scheme="URI" content="apiexmp.htm" />
|
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
|
|
<meta name="DC.Format" content="XHTML" />
|
|
<meta name="DC.Identifier" content="apiexusterm" />
|
|
<meta name="DC.Language" content="en-us" />
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
|
<title>Examples: Using the generic terminal APIs</title>
|
|
</head>
|
|
<body id="apiexusterm"><a name="apiexusterm"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Examples: Using the generic terminal APIs</h1>
|
|
<div><p>These two examples illustrate programs that implement
|
|
a generic terminal and a simple interpreter.</p>
|
|
<div class="section"><div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm">Code license and disclaimer information</a> for important
|
|
legal information.</div>
|
|
</div>
|
|
<div class="section" id="apiexusterm__HDRTRMLPGM"><a name="apiexusterm__HDRTRMLPGM"><!-- --></a><h4 class="sectiontitle">Terminal program</h4><p>This program starts
|
|
and runs a generic terminal.</p>
|
|
<p>This program demonstrates the use of the
|
|
generic terminal functions Qp0zStartTerminal(), Qp0zRunTerminal(), Qp0zEndTerminal(),
|
|
and Qp0zGetTerminalPid().</p>
|
|
<p>Use the Create Bound C Program (CRTBNDC) command
|
|
to create this program (see <a href="#apiexusterm__HDRCRTRMLP">Creating the terminal and interpreter programs</a>).</p>
|
|
<p>Call
|
|
this program with no parameters (see <a href="#apiexusterm__HDRCALTRML">Calling the terminal program</a>).</p>
|
|
<pre>/* Includes */
|
|
#include <qp0ztrml.h>
|
|
#include <qp0z1170.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
/* Constants */
|
|
#define NUM_PREDEFINED_ENVS 2
|
|
|
|
/* Global Variables */
|
|
extern char **environ;
|
|
|
|
int main (int argc, char *argv[])
|
|
{
|
|
char *args[2]; /* Argument array */
|
|
int envCount; /* Count of currently defined env vars */
|
|
int index; /* Loop index */
|
|
char **envp; /* For walking environ array */
|
|
char **envs; /* Environment variable array */
|
|
Qp0z_Terminal_T handle; /* Terminal handle */
|
|
Qp0z_Terminal_Attr_T ta; /* Terminal attributes */
|
|
pid_t pid; /* Process ID of interpreter */
|
|
int rc; /* Return code */
|
|
|
|
/********************************************************************/
|
|
/* Build the argument array. */
|
|
/********************************************************************/
|
|
|
|
args[0] = "/QSYS.LIB/QGPL.LIB/ECHOINT.PGM";
|
|
args[1] = NULL;
|
|
|
|
/********************************************************************/
|
|
/* Build the environment variable array. */
|
|
/********************************************************************/
|
|
|
|
/* Make sure environ is set in this activation group. */
|
|
Qp0zInitEnv();
|
|
|
|
/* Count the number of environment variables currently defined in this
|
|
process. Qp0zStartTerminal() will make sure the interpreter
|
|
process does not have too many environment variables. */
|
|
for (envCount = 0, envp = environ; *envp != NULL; ++envp, ++envCount);
|
|
|
|
/* Allocate storage for the environment variable array. */
|
|
envs = (char **)malloc(sizeof(char *) *
|
|
(envCount + NUM_PREDEFINED_ENVS));
|
|
if (envs == NULL) {
|
|
perror("malloc() failed");
|
|
exit(1);
|
|
}
|
|
|
|
/* Copy the current environment variables to the array. */
|
|
for (index = 0; environ[index] != NULL; ++index) {
|
|
envs[index] = environ[index];
|
|
}
|
|
|
|
/* Set QIBM_USE_DESCRIPTOR_STDIO variable for using descriptors. This
|
|
will override the current value of the variable. */
|
|
envs[index++] = "QIBM_USE_DESCRIPTOR_STDIO=Y";
|
|
|
|
/* Null terminate array of environment variables. */
|
|
envs[index] = NULL;
|
|
|
|
/********************************************************************/
|
|
/* Set the terminal attributes. */
|
|
/********************************************************************/
|
|
|
|
memset(&ta, '\0', sizeof(Qp0z_Terminal_Attr_T));
|
|
ta.Buffer_Size = 8196;
|
|
ta.Inherit.pgroup = SPAWN_NEWPGROUP;
|
|
ta.Title = "Echo Interpreter";
|
|
ta.Cmd_Key_Line1 = "F3=Exit F9=Retrieve";
|
|
ta.Cmd_Key_Line2 = "F17=Top F18=Bottom";
|
|
|
|
/********************************************************************/
|
|
/* Start and run the terminal. */
|
|
/********************************************************************/
|
|
|
|
/* Start the terminal. */
|
|
if (Qp0zStartTerminal(
|
|
<em>handle</em>, args, envs, ta) != 0) {
|
|
perror("Qp0zStartTerminal() failed");
|
|
exit(1);
|
|
}
|
|
|
|
/* Get the PID of the interpreter process. */
|
|
if (Qp0zGetTerminalPid(handle, &pid) != 0) {
|
|
perror("Qp0zGetTerminalPid() failed");
|
|
exit(1);
|
|
}
|
|
|
|
/* Run the terminal. */
|
|
rc = Qp0zRunTerminal(handle);
|
|
switch (rc) {
|
|
case QP0Z_TERMINAL_F12:
|
|
case QP0Z_TERMINAL_F3:
|
|
case QP0Z_TERMINAL_ENDED:
|
|
/* Do nothing */
|
|
break;
|
|
|
|
default:
|
|
perror("Qp0zRunTerminal() failed");
|
|
exit(1);
|
|
break;
|
|
}
|
|
|
|
/* End the terminal. */
|
|
Qp0zEndTerminal(handle);
|
|
|
|
return 0;
|
|
}</pre>
|
|
</div>
|
|
<div class="section" id="apiexusterm__HDRINTPGM"><a name="apiexusterm__HDRINTPGM"><!-- --></a><h4 class="sectiontitle">Interpreter program</h4><p>This program
|
|
is a simple echo interpreter that is used by the terminal program (see <a href="#apiexusterm__HDRTRMLPGM">Terminal program</a>).</p>
|
|
<p>Use the Create Bound C Program
|
|
(CRTBNDC) command to create this program (see <a href="#apiexusterm__HDRCRTRMLP">Creating the terminal and interpreter programs</a>).</p>
|
|
<pre>/* Echo interpreter */
|
|
#include <stdio.h>
|
|
#include <signal.h>
|
|
#include <stdlib.h>
|
|
|
|
static void SignalHandler(int);
|
|
|
|
int main (int argc, char *argv[])
|
|
{
|
|
char buffer[8192]; /* Buffer for reading input */
|
|
struct sigaction sigact; /* Signal action */
|
|
|
|
/* Set up a signal handler for SIGHUP. The terminal
|
|
sends this signal when the user presses F3 to exit. */
|
|
sigemptyset(&sigact.sa_mask);
|
|
sigact.sa_flags = 0;
|
|
sigact.sa_handler = SignalHandler;
|
|
if (sigaction(SIGHUP, &sigact, NULL) != 0) {
|
|
perror("sigaction(SIGHUP) failed.");
|
|
exit(2);
|
|
}
|
|
|
|
/* Set up a signal handler for SIGINT. The terminal sends
|
|
this signal when the user presses SysReq 2. */
|
|
sigemptyset(&sigact.sa_mask);
|
|
sigact.sa_flags = 0;
|
|
sigact.sa_handler = SignalHandler;
|
|
if (sigaction(SIGINT, &sigact, NULL) != 0) {
|
|
perror("sigaction(SIGINT) failed.");
|
|
exit(2);
|
|
}
|
|
|
|
/* Switch stdout to use line-mode buffering. */
|
|
setvbuf(stdout, NULL, _IOLBF, 128);
|
|
printf("Echo interpreter starting ...\n");
|
|
printf("Enter text:\n");
|
|
|
|
/* Do forever. */
|
|
while (1) {
|
|
/* Read a line from stdin. */
|
|
gets(buffer);
|
|
|
|
/* End and clean up any allocated
|
|
resources when stdin is closed. */
|
|
if (feof(stdin)) {
|
|
printf("Echo interpreter ending ...\n");
|
|
exit(0);
|
|
}
|
|
|
|
/* Echo the line to stdout. */
|
|
printf("%s\n", buffer);
|
|
} /* End of while */
|
|
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
SignalHandler(int signo)
|
|
{
|
|
printf("Ending for signal %d\n", signo);
|
|
exit(1);
|
|
}</pre>
|
|
</div>
|
|
<div class="section" id="apiexusterm__HDRCRTRMLP"><a name="apiexusterm__HDRCRTRMLP"><!-- --></a><h4 class="sectiontitle">Creating the terminal and interpreter programs</h4><p>The
|
|
following examples show how to create the example programs (<a href="#apiexusterm__HDRTRMLPGM">Terminal program</a> and <a href="#apiexusterm__HDRINTPGM">Interpreter program</a>). These examples assume that the source
|
|
for the terminal program is member TERMINAL in the file QGPL/QCSRC and that
|
|
the source for the interpreter program is member INTERPRET in the file QGPL/QCSRC.</p>
|
|
<p><strong>Create
|
|
the terminal program:</strong></p>
|
|
<pre>CRTBNDC PGM(QGPL/TERMINAL)
|
|
SRCFILE(QGPL/QCSRC)
|
|
SRCMBR(TERMINAL)
|
|
SYSIFCOPT(*IFSIO)
|
|
TEXT('Example Terminal program')</pre>
|
|
<p><strong>Create the interpreter program:</strong></p>
|
|
<pre>CRTBNDC PGM(QGPL/INTERPRET)
|
|
SRCFILE(QGPL/QCSRC)
|
|
SRCMBR(INTERPRET)
|
|
SYSIFCOPT(*IFSIO)
|
|
TEXT('Example Interpreter program')</pre>
|
|
</div>
|
|
<div class="section" id="apiexusterm__HDRCALTRML"><a name="apiexusterm__HDRCALTRML"><!-- --></a><h4 class="sectiontitle">Calling the terminal program</h4><p>The
|
|
following example shows how to start the example program:</p>
|
|
<pre>CALL PGM(QGPL/TERMINAL)</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="apiexmp.htm" title="Contains example programs that use APIs and exit programs.">Examples: APIs</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |