Files
sics/help.c
koennecke b26b8fc735 - Changed strncpy to strlcpy, strncat to strlcat
- Added strlcpy and strlcat to SICS
- Added a driver for the POLDI power supplies


SKIPPED:
	psi/A1931.c
	psi/autowin.c
	psi/bruker.c
	psi/docho.c
	psi/dornier2.c
	psi/dspcode.c
	psi/ease.c
	psi/ecb.c
	psi/ecbcounter.c
	psi/ecbdriv.c
	psi/el734dc.c
	psi/el734driv.c
	psi/el734hp.c
	psi/el737driv.c
	psi/el737hpdriv.c
	psi/el737hpdrivsps.c
	psi/el737hpv2driv.c
	psi/el755driv.c
	psi/eurodriv.c
	psi/haakedriv.c
	psi/itc4driv.c
	psi/julcho.c
	psi/linadriv.c
	psi/lmd200.c
	psi/lscsupport.c
	psi/ltc11.c
	psi/make_gen
	psi/oicom.c
	psi/oxinst.c
	psi/pimotor.c
	psi/pipiezo.c
	psi/polterwrite.c
	psi/psi.c
	psi/sanscook.c
	psi/sanslirebin.c
	psi/sanswave.c
	psi/sinqhmdriv.c
	psi/sinqhttp.c
	psi/slsecho.c
	psi/slsmagnet.c
	psi/slsvme.c
	psi/sps.c
	psi/swmotor.c
	psi/swmotor2.c
	psi/tabledrive.c
	psi/tasscan.c
	psi/tdchm.c
	psi/velodorn.c
	psi/velodornier.c
2010-04-13 15:08:38 +00:00

165 lines
4.0 KiB
C

/*-----------------------------------------------------------------------
Implementation file for the SICS help system.
copyright: see file COPYRIGHT
Mark Koennecke, December 2003
-----------------------------------------------------------------------*/
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include "fortify.h"
#include "sics.h"
#include "help.h"
extern char *stptok(const char *s, char *tok, size_t toklen, char *brk);
/*----------------------------------------------------------------------*/
#define PATHSEP ":"
#define DIRSEP "/"
static char *helpDirs = NULL;
static char *defaultFile = "master.txt";
/*----------------------------------------------------------------------*/
void KillHelp(void *pData)
{
KillDummy(pData);
if (helpDirs != NULL) {
free(helpDirs);
helpDirs = NULL;
}
}
/*-----------------------------------------------------------------------*/
static FILE *findHelpFile(char *name)
{
FILE *fd = NULL;
char pBueffel[256];
char dir[132];
char *pPtr;
if (helpDirs == NULL) {
return NULL;
}
pPtr = helpDirs;
while ((pPtr = stptok(pPtr, dir, 131, PATHSEP)) != NULL) {
strcpy(pBueffel, dir);
strcat(pBueffel, DIRSEP);
strlcat(pBueffel, name, (254 - strlen(pBueffel)));
fd = fopen(pBueffel, "r");
if (fd != NULL) {
return fd;
}
}
/*
this means: not found!
*/
return NULL;
}
/*----------------------------------------------------------------------*/
static void printHelpFile(SConnection * pCon, FILE * fd)
{
char line[132];
while (fgets(line, 131, fd) != NULL) {
SCWrite(pCon, line, eValue);
}
}
/*----------------------------------------------------------------------*/
static void configureHelp(SConnection * pCon,
char *option, char *parameter)
{
char *pPtr = NULL;
int length;
strtolower(option);
if (strcmp(option, "adddir") == 0) {
if (parameter == NULL) {
SCWrite(pCon, helpDirs, eValue);
return;
} else {
pPtr = helpDirs;
if (pPtr != NULL) {
length = strlen(pPtr) + strlen(PATHSEP) + strlen(parameter) + 2;
helpDirs = (char *) malloc(length * sizeof(char));
memset(helpDirs, 0, length * sizeof(char));
strcpy(helpDirs, pPtr);
strcat(helpDirs, PATHSEP);
strcat(helpDirs, parameter);
free(pPtr);
} else {
helpDirs = strdup(parameter);
}
}
} else if (strcmp(option, "defaultfile") == 0) {
if (parameter == NULL) {
SCWrite(pCon, defaultFile, eValue);
return;
} else {
if (defaultFile != NULL) {
free(defaultFile);
}
defaultFile = strdup(parameter);
}
} else {
SCWrite(pCon, "Unknown option to configure", eWarning);
SCWrite(pCon, "Known options: defaultfile, adddir", eWarning);
}
}
/*-----------------------------------------------------------------------*/
int SicsHelp(SConnection * pCon, SicsInterp * pSics, void *pData,
int argc, char *argv[])
{
char helpFile[256];
FILE *fd = NULL;
strlcpy(helpFile, defaultFile, 255);
if (argc > 1) {
strtolower(argv[1]);
/*
check for configure
*/
if (strcmp(argv[1], "configure") == 0) {
if (argc < 3) {
SCWrite(pCon, "ERROR: need an option to configure", eError);
return 0;
}
if (argc > 3) {
configureHelp(pCon, argv[2], argv[3]);
} else {
configureHelp(pCon, argv[2], NULL);
}
SCSendOK(pCon);
return 1;
}
/*
the parameter is a help file name
*/
strlcpy(helpFile, argv[1], 255);
strlcat(helpFile, ".txt", 255);
}
/*
print the helpFile
*/
fd = findHelpFile(helpFile);
if (fd == NULL) {
SCWrite(pCon, "ERROR: failed to locate helpFile:", eError);
SCWrite(pCon, helpFile, eError);
return 0;
}
printHelpFile(pCon, fd);
fclose(fd);
return 1;
}
/*-----------------------------------------------------------------------*/
void HelpInit(void)
{
AddCommand(pServ->pSics, "help", SicsHelp, KillHelp, NULL);
}