#include #include #include #include #include #include #include #include "errhdl.h" #include "util.h" /* when changing FBUF_LEN, change also the constant in the fscanf call in subroutine LscExeCmd */ #define FBUF_LEN 132 char *str_split(char *str, char sep, char *list[], int *n) { int i; char *s, *e; s=str; for (i=0; i<*n; i++) { list[i]=s; e=strchr(s, sep); if (e==NULL) { *n=i+1; return(NULL); } s=e+1; e--; while (e>str && *e==' ') e--; /* trim sequence */ e[1]='\0'; } return(s); } char *str_split1(char *str, char sep) { char *s, *e; e=strchr(str, sep); if (e==NULL) { s=NULL; e=str+strlen(str); } else { s=e+1; } e--; while (e>str && *e==' ') e--; /* trim sequence */ e[1]='\0'; return(s); } char *str_ntrim(char *dest, int ldest, const char *src, int lsrc) { char *s, *e; if (lsrcdest && *e==' ') e--; /* trim sequence */ e[1]='\0'; return(s); } char *str_read_until(FILE *fil, char *term, char *buf, char *end) { char *s; char fmt[24]; int i, l, siz; char ch; siz=end-buf-1; if (siz<1) return(NULL); sprintf(fmt, "%s%d[^%s%s", "%", siz, term, "]%n%c"); i=fscanf(fil, fmt, buf, &l, &ch); if (i<0) { /* eof */ buf[0]='\0'; return(&buf[0]); } else if (i==0) { /* fscanf returns 0 if first char is terminator */ buf[0]=fgetc(fil); return(&buf[0]); } else if (i==1) { /* terminator not found -> read until eof */ buf[l]='\0'; return(&buf[l]); } else { buf[l]=ch; if (l==siz && NULL==strchr(term, ch)) return(NULL); return(&buf[l]); } } char *str_read_arg(char *file, char *args[], int nargs) { FILE *fil; char *str, *s, *e, *p, *q; char ch; int i, l, size; struct stat statbuf; i=stat(file, &statbuf); if (i<0) ERR_MSG("file not found"); size=statbuf.st_size+4; if (nargs>0) size+=size/2+100; /* max size */ while (1) { ERR_SP(str=malloc(size)); e=&str[size-1]; ERR_SP(fil=fopen(file, "r")); s=str; while (1) { p=str_read_until(fil, "#!", s, e); if (p==NULL) break; if (*p=='!') { q=str_read_until(fil, "\n", p, e); if (q==NULL) { p=NULL; break; } s=p; *s='\n'; s++; } else if (*p=='#') { ch=fgetc(fil); i=ch-'0'; if (i<0 || i>=nargs) { s=p+1; *s=ch; s++; } else { l=strlen(args[i]); if (s+l>=e) { p=NULL; break; } strcpy(p, args[i]); s=p+l; } } else { assert(*p=='\0'); break; } } ERR_SI(fclose(fil)); if (p!=NULL) break; size=size*3/2; /* allocation not sufficient -> try again */ free(str); } assert(strlen(str)ch2) { return(i); } return(0); } int str_ncpy(char *dst, const char *src, int maxdest) { strncpy(dst, src, maxdest); if (dst[maxdest-1]!='\0') { dst[maxdest-1]='\0'; ERR_MSG("destination string too short"); } return(0); OnError: return(-1); } int str_ncat(char *dst, const char *src, int maxdest) { strncat(dst, src, maxdest-strlen(dst)-1); if (dst[maxdest-1]!='\0') { dst[maxdest-1]='\0'; ERR_MSG("destination string too short"); } return(0); OnError: return(-1); } void util_delay(int tmo_msec) { struct timeval tmo; tmo.tv_sec=tmo_msec / 1000; tmo.tv_usec=(tmo_msec % 1000)*1000+1; select(1,NULL,NULL,NULL,&tmo); }