#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include "extern.h"
#include "misc.h"
#include <sac.h>
#include "structs.h"
#ifdef SAC
#include "msgs.h"
#endif
char Comment[SIZE];
char *
nexttok(str, delim, ros)
char *str;
register char *delim;
int ros;
{
static char *savep;
register char *p;
register char *ep;
p = (str == NULL) ? savep : str ;
if (ros)
return(p);
if (p == NULL)
return(NULL);
ep = strpbrk(p, delim);
if (ep == NULL) {
savep = NULL;
return(p);
}
savep = ep + 1;
*ep = '\0';
return(p);
}
void
parse(p, sp)
register char *p;
register struct sactab *sp;
{
char scratch[SIZE];
p = nexttok(p, DELIM, FALSE);
if (p == NULL) {
# ifdef SAC
error(E_BADFILE, EXIT);
# else
Saferrno = E_SAFERR;
error("_sactab file is corrupt");
# endif
}
if (strlen(p) > PMTAGSIZE) {
p[PMTAGSIZE] = '\0';
# ifdef SAC
(void) sprintf(scratch, "tag too long, truncated to <%s>", p);
log(scratch);
# else
(void) fprintf(stderr, "tag too long, truncated to <%s>", p);
# endif
}
(void) strcpy(sp->sc_tag, p);
p = nexttok(NULL, DELIM, FALSE);
if (p == NULL) {
# ifdef SAC
error(E_BADFILE, EXIT);
# else
Saferrno = E_SAFERR;
error("_sactab file is corrupt");
# endif
}
if (strlen(p) > PMTYPESIZE) {
p[PMTYPESIZE] = '\0';
# ifdef SAC
(void) sprintf(scratch, "type too long, truncated to <%s>", p);
log(scratch);
# else
(void) fprintf(stderr, "type too long, truncated to <%s>", p);
# endif
}
(void) strcpy(sp->sc_type, p);
p = nexttok(NULL, DELIM, FALSE);
if (p == NULL) {
# ifdef SAC
error(E_BADFILE, EXIT);
# else
Saferrno = E_SAFERR;
error("_sactab file is corrupt");
# endif
}
sp->sc_flags = 0;
while (*p) {
switch (*p++) {
case 'd':
sp->sc_flags |= D_FLAG;
break;
case 'x':
sp->sc_flags |= X_FLAG;
break;
default:
(void) sprintf(scratch, "Unrecognized flag <%c>", *(p - 1));
# ifdef SAC
log(scratch);
# else
Saferrno = E_SAFERR;
error(scratch);
# endif
break;
}
}
p = nexttok(NULL, DELIM, FALSE);
if (p == NULL) {
# ifdef SAC
error(E_BADFILE, EXIT);
# else
Saferrno = E_SAFERR;
error("_sactab file is corrupt");
# endif
}
sp->sc_rsmax = atoi(p);
p = nexttok(NULL, DELIM, FALSE);
if (p == NULL) {
# ifdef SAC
error(E_BADFILE, EXIT);
# else
Saferrno = E_SAFERR;
error("_sactab file is corrupt");
# endif
}
if ((sp->sc_cmd = malloc((unsigned) (strlen(p) + 1))) == NULL) {
# ifdef SAC
error(E_MALLOC, EXIT);
# else
Saferrno = E_SAFERR;
error("malloc failed");
# endif
}
(void) strcpy(sp->sc_cmd, p);
if ((sp->sc_comment = malloc((unsigned) (strlen(Comment) + 1))) == NULL) {
# ifdef SAC
error(E_MALLOC, EXIT);
# else
Saferrno = E_SAFERR;
error("malloc failed");
# endif
}
(void) strcpy(sp->sc_comment, Comment);
}
char *
trim(p)
register char *p;
{
register char *tp;
tp = strchr(p, COMMENT);
Comment[0] = '\0';
if (tp) {
(void) strcpy(Comment, tp + 1);
*tp = '\0';
tp = strchr(Comment, '\n');
if (tp)
*tp ='\0';
}
for (tp = p + strlen(p) - 1; tp >= p && isspace(*tp); --tp)
*tp = '\0';
return(p);
}
char *
pstate(unchar state)
{
switch (state) {
case NOTRUNNING:
return("NOTRUNNING");
case STARTING:
return("STARTING");
case ENABLED:
return("ENABLED");
case DISABLED:
return("DISABLED");
case STOPPING:
return("STOPPING");
case FAILED:
return("FAILED");
case UNKNOWN:
return("UNKNOWN");
# ifndef SAC
case SSTATE:
return("NO_SAC");
# endif
default:
# ifdef SAC
error(E_BADSTATE, EXIT);
# else
Saferrno = E_SAFERR;
error("Improper message from SAC\n");
# endif
}
return (NULL);
}