Files
sics/site_ansto/anstoutil.c
Ferdi Franceschini 3a7e6a9768 Added "abortbatch" command for use within batch files.
It gives a user the option of  aborting a batch file if their code detects an error.

r2192 | ffr | 2007-10-23 12:46:16 +1000 (Tue, 23 Oct 2007) | 3 lines
2012-11-15 13:26:38 +11:00

81 lines
2.2 KiB
C

#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sics.h>
#include "anstoutil.h"
#define MAXNUMCHAR 32
/** \brief Get configuration parameter
* \param *pCon (r) connection object.
* \param *pTcl (r) Tcl interpreter
* \param *params Tcl array of configuration parameters
* \param *parName name of parameter to get from the array
* \param mustHave indicates optional or mandatory parameters\n
* possible values
* - _REQUIRED
* - _OPTIONAL
* \return (r) a reference to a string representation of the parameter value
*/
char *getParam(SConnection *pCon, Tcl_Interp *pTcl, char *params, char *parName, int mustHave ) {
char *pPtr=NULL;
char pError[ERRLEN];
pPtr = Tcl_GetVar2(pTcl,params,parName,TCL_GLOBAL_ONLY);
if((mustHave == _REQUIRED) && !pPtr){
snprintf(pError, ERRLEN,"ERROR: You must supply an '%s' parameter", parName);
SCWrite(pCon,pError, eError);
}
return pPtr;
}
/** \brief Lookup named port in /etc/services
* \param *pCon (r) connection object.
* \param *portName (r) name of port to look up
* \return
* - port number on success
* - 0 on failure
*/
int getPortNum(SConnection *pCon, char *portName) {
struct servent *sp=NULL;
char pError[ERRLEN];
sp = getservbyname(portName, NULL);
if (sp == NULL) {
snprintf(pError, ERRLEN,"ERROR: '%s' service not found", portName);
SCWrite(pCon,pError, eError);
return 0;
}
return ntohs(sp->s_port);
}
int portNumCmd(SConnection *pCon, SicsInterp *pInter, void *pData, int argc, char *argv[]) {
OutCode eOut = eWarning;
int pn;
int iMacro;
char portNum[MAXNUMCHAR];
assert(pCon != NULL);
assert(pInter != NULL);
switch (argc) {
case 2:
pn = getPortNum(pCon, argv[1]);
snprintf(portNum, MAXNUMCHAR, "%d", pn);
break;
default:
SCWrite(pCon,"Insufficient arguments to portNumCmd",eError);
return 0;
}
//iMacro = SCinMacro(pCon);
//SCsetMacro(pCon,0);
SCWrite(pCon,portNum,eValue);
//SCsetMacro(pCon,iMacro);
return 1;
}
int AbortBatch(SConnection *pCon, SicsInterp *pInter, void *pData, int argc, char *argv[]) {
SCSetInterrupt(pCon,eAbortBatch);
return TCL_ERROR;
}