name) ||
(stat(assoc->name, &fileStat)==-1) || (fileStat.st_size==0) )
{ prErr("\nParallel_load: could not load %s to a local file. "
"Check the spelling or the access rights",assoc->value);
assoc->name[0]='\0'; unlink(tmpFileName);
}
else nbFileLoaded++;
}
return nbFileLoaded;
}
/* -------------------------- FILE - read CGI parameters --------------------------- */
/*GET/POST param to CGI script -> paramList*/
ErrMess initListParamWithCGIparam (initP HTAssocList *paramList)
{ int c,c1,c2, i, allocSize; char *s, *name, *value; /*buff[2049];*/
if (!paramList) return "Server internal error: no parameter list to initialize";
if (strEqual(getenv("REQUEST_METHOD"),"POST"))
{
int l=atoi(getenv("CONTENT_LENGTH"));
if (l==0) return "Server internal error: CONTENT_LENGTH==0.";
else if (l>ALLOC_SIZE) allocSize=ALLOC_SIZE; else allocSize=l;
/* if (!strEqual(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded"))
{ prErr("This script can only be used to decode form results"); return 1; }
*/
s= (char*) my_calloc(1,sizeof(char)*(allocSize+1));
if (!s) return "Server memory problem: cannot store the parameters";
for (i=0, name=s, value=""; (c=getc(stdin))!=EOF && (l>0); i++,l--)
{ if (i==allocSize)
{ allocSize+=ALLOC_SIZE;
s[i]='\0'; s=(char*)my_realloc(s,sizeof(char)*(allocSize+1));
}
if (c=='+') s[i]=' ';
else if (c=='%') { c1=getc(stdin); s[i]=unescapeURLchar(c1,getc(stdin)); l-=2; }
else if (c=='=') { s[i]='\0'; value= &s[i+1]; }
else if (c=='&')
{ s[i]='\0';
if (HTAssocList_addObject(paramList,name,value)==NO)
{ MY_FREE(s); return "Internal error in storing the parameters."; }
i= -1; name=s; value="";
}
else s[i]=c;
}
if (i>0) /* => s[i-1]!='&' */
{ s[i]='\0';
if (HTAssocList_addObject(paramList,name,value)==NO)
{ MY_FREE(s); return "Internal error in storing the parameters"; }
}
}
else if (strEqual(getenv("REQUEST_METHOD"),"GET")) /* no CONTENT_LENGTH */
{
char *query = getenv("QUERY_STRING"); /* *queryBeginning=query */
if (!query || !*query) return "No query information to process.";
allocSize=ALLOC_SIZE; s=(char*) my_calloc(1,sizeof(char)*(allocSize+1));
if (!s) return "Server memory problem: cannot store the parameters";
for (i=0, name=s, value=""; ((c = *query)); i++, query++)
{ if (i==allocSize)
{ allocSize+=ALLOC_SIZE;
s[i]='\0'; s=(char*)my_realloc(s,sizeof(char)*(allocSize+1));
}
if (c=='+') s[i]=' ';
else if (c=='%') { c1= *(++query); c2= *(++query); s[i]=unescapeURLchar(c1,c2); }
else if (c=='=') { s[i]='\0'; value= &s[i+1]; }
else if (c=='&')
{ s[i]='\0'; /* pr("name:%s\n",name); pr("value:%s\n",value); */
if (HTAssocList_addObject(paramList,name,value)==NO)
return "Internal error in storing the parameters";
i=-1; name=s; value="";
}
else s[i]=c;
}
if (i>0) /* && s[i-1]!='&' */
{ s[i]='\0'; /* pr("name2:%s\n",name); pr("value:%s\n",value); */
if (HTAssocList_addObject(paramList,name,value)==NO)
return "Internal error in storing the parameters";
}
}
else return "This script should be referenced with a METHOD of GET or POST.\n"
"If you don't understand this, see this forms overview";
MY_FREE(s); return NULL;
}
/* ------------------------ FILE - read string from file ----------------------- */
void ungetsInBuffer (char *str, varP char *buff)
{ char *s=str; while (*s) s++; while (*buff) buff++;
while (s > str) *(buff++)= *(--s); *buff='\0';
}
char readCharInBufferOrFd (varP char buff[], int fd)
{ char c, *s=buff; while (*s) s++;
/*fpr(stderr,"readCharIn%s (%d): ",(s > buff)?"buffer":"Fd", fd);*/
if (s > buff) { c=*(--s); *s='\0'; } else if (!read(fd,&c,1)) c='\0';
/*fpr(stderr,"%c_",c);*/
return c;
}
/* because of the realloc, the return of this function should not be ignored:
str=dynamicReadUntil(stopMark, fd, initP char *str, pPosToWrite, pMaxSize)*/
char *dynamicReadUntil (char *stopMark, int fd, initP char *str,
varP int *pPosToWrite, varP int *pMaxSize)
{ static char alreadyReadChars[50]="";
int i= *pPosToWrite, j=1, allocSize= *pMaxSize;
char c, stopMarkFirstChar, buff[50];
if (!*stopMark) stopMark= "\n"; stopMarkFirstChar= *stopMark;
for (i=0; ((c= readCharInBufferOrFd(alreadyReadChars,fd))); str[i++]=c)
{ if (i == allocSize)
{ str[i]='\0'; *pMaxSize += ALLOC_SIZE; allocSize= *pMaxSize;
str = (char *) my_realloc (str, sizeof(char)*(allocSize+1));
if (!str) return NULL;
}
if (c==stopMarkFirstChar)
{ while (stopMark[j] && ((c= readCharInBufferOrFd(alreadyReadChars,fd)))
&& (c==stopMark[j])) buff[j++]=c;
if (!stopMark[j]) { str[i]='\0'; *pPosToWrite = i; return str; }
else { buff[j]='\0'; ungetsInBuffer(buff+1,alreadyReadChars); j=1; }
}
}
if (!i) return NULL; else { str[i]='\0'; *pPosToWrite = i; return str; }
}
BOOL ungets (char *str, FILE *fp)
{ char *s=str; while (*s) s++;
for (s--; s >= str; s--)
if (ungetc(*s,fp)==EOF) return NO; /* else pr("ungetc %c\n",*s); */
return YES;
}
/* because of the realloc, the return of this function should not be ignored:
str=dynamicFgetsUntil(stopMark, fp, initP char *str, includeStopMark,
varP pPosToWrite, varP pMaxSize); */
char *dynamicFgetsUntil (char *stopMark /* in lowercase */, FILE *fp, initP char *str,
BOOL includeStopMark, varP int *pPosToWrite, varP int *pMaxSize)
{ int c, i= *pPosToWrite, j, allocSize= *pMaxSize; int iRealloc=0;
char stopMarkFirstChar, buff[50];
if (!*stopMark) stopMark= "\n"; stopMarkFirstChar= *stopMark;
while ((c=getc(fp))!=EOF)
{ str[i++]=c; str[i]='\0';/* '\0' ok but useless */
/* if (i == iRealloc+10)
{ tr("\n\n\n\n\n@@@@@@@@@@@@@@@@@@@@@@@ %d <- '%d%c'\n\n",i,c,c);
if (i-20 > 0) tr("!!!%s!!!\n",str+(i-20));
}
if ( (iRealloc==30000) && (c=='}') )
{ tr("<<<<<<<<<%s>>>>>>>>>>>\n",str+(iRealloc-10));
} */
/* pr("str[%d]='%c', str:'%s'\n",i,c,str); */
if (i==allocSize)
{ if ((str=strRealloc(str,i,varP pMaxSize)))
{ allocSize=*pMaxSize; iRealloc=i;
/* tr("\n\n\n\n\n\n########### iRealloc %d\n",i); */
}
else return NULL;
}
if (c==stopMarkFirstChar) /* 1st c. ok => j=1 below */
{ for (j=1; stopMark[j] && ((c=getc(fp))!=EOF) &&
(tolower(c)==stopMark[j]); j++)
{ buff[j]=c; /* tr("buff[%d]=%c\n",j,c); */ }
if (stopMark[j])
{ buff[j]='\0'; ungetc(c,fp); ungets(buff+1,fp); }
else /* stopMark found */
{ if (!includeStopMark) i--;
else for (j=1; (c= stopMark[j]); j++)
{ if ((i==allocSize) && !(str=strRealloc(str,i,varP pMaxSize)))
return NULL;
str[i++]=c;
}
str[i]='\0'; *pPosToWrite = i;
/* tr("222:%d %d%s......\n",i,j,str+(iRealloc-10)); */
return str;
}
}
}
if (!i) { tr("end file or empty file"); return NULL; }
else { str[i]='\0'; *pPosToWrite = i; tr("333"); return str; }
}
/*
if (fgets(line,499,fp) && (line[0]=='$') && (line[1]=='(')) markForEndOfCommand=")$";
do
{ if (lastLineOfCommand(varP line, markForEndOfCommand))
{ strcat(command,line); line[0]='\0'; break;
}
if (*line) strcat(command,line);
}while (fgets(line,499,fp));
BOOL lastLineOfCommand (varP char *line, char *markForEndOfCommand)
{ char c, *pc, *end;
if (!*line) return YES;
if (!strEqual(markForEndOfCommand,"."))
{ if ((end= strstr(line,markForEndOfCommand)))
{ *(end+strlen(markForEndOfCommand))='\0'; return YES; }
else return NO;
}
for (end=line; *end; end++)
{ if (*end == '.')
{ for (pc=end+1; ((c= *pc)) && isspace(c); pc++)
{ if (charInStr(c,"\n\r")) { *end='\0'; return YES; } }
if (!c) { *end='\0'; return YES; }
}
}
return NO;
}
char *endOfCommand (char *command, char *markForEndOfCommand)
{ char c, *pc, *end;
if (!strEqual(markForEndOfCommand,"."))
{ if (!(end= strstr(command,markForEndOfCommand)))
{ end=command; while (*end) end++; }
}
else
{ for (end=command; *end; end++)
if (*end == '.')
{ for (pc=end+1; ((c= *pc)) && isspace(c); pc++)
{ if (charInStr(c,"\n\r")) return end; }
if (!c) return end;
}
}
return end;
}
*/
/* ------------------- FILE - print string to file (stderr/stdout) ------------------ */
int tr (const char *format, ...) /* print traces */
{ va_list pArgs; if (!format || !*format) return 0; va_start(pArgs,format);
if (!WithTrace) return 0;/*(FpTrace) ? vfprintf(FpTrace,format,pArgs) : 0*/
if (CommandOutputFile) return vfprintf(CommandOutputFile,format,pArgs);
else return vfprintf(stdout,format,pArgs);
/* if (!WithTrace) return vfprintf(stderr,format,pArgs); */
}
#if 1
int prErr (const char *format, ...) /* print error messages */
{ FILE *fp=CommandOutputFile; char buff[1000]; int res;
va_list pArgs; if (!format || !*format) return 0;
if (!fp) fp=stdout; /* stdout instead of stderr for a CGI program */
va_start(pArgs, format); sprintf(buff,"
%s
\n\n",format);
res= vfprintf(fp, buff, pArgs); /* not with return since buff is local */
return res;
} /* the error message may include __FILE__ and __LINE__ */
#else
char *prErr (char *str, ...) /*prErr() version nearly ok; bug for str + two arguments*/
{ va_list ap; char c,cv, *s=str, *svStr=str; FILE *fp=stdout;
if (!s || !*s) return s;
va_start(ap,str);
while ((c= *s))
{ if (c!='%') s++;
else if (!(c= *(++s))) break;
else if (c=='%') s++;
else
{ while ( ((c= *(++s))) && (c!=' ') ); cv= *(s-1);
switch (cv)
{ case 'd': case 'i': case 'c': *s='\0'; fpr(fp,str,va_arg(ap,int));
*s=c; str=s; break;
case 'o': case 'u': case 'x':
case 'X': *s='\0'; fpr(fp,str,va_arg(ap,unsigned int));
*s=c; str=s; break;
case 'f': case 'e': case 'g':
case 'E': case 'G': *s='\0'; fpr(fp,str,va_arg(ap,double));
*s=c; str=s; break;
case 'p': *s='\0'; fpr(fp,str,va_arg(ap,void *)); *s=c; str=s; break;
case 'n': *s='\0'; fpr(fp,str,va_arg(ap,int *)); *s=c; str=s; break;
case 's': *s='\0'; fpr(fp,str,va_arg(ap,char *)); *s=c; str=s; break;
case 'S': *s='\0'; fpr(fp,str,va_arg(ap,wchar_t *)); *s=c; str=s; break;
case 'C': *s='\0'; fpr(fp,str,va_arg(ap,intptr_t)); *s=c; str=s; break;
}
}
}
if (*str) fpr(fp,str); va_end(ap); return svStr;
}
#endif
/* prErr() version not ok: args should be of char *, int, double, ... at the same time
#define MAXARGS 15
char *prErr (char *str, ...)
{ va_list ap; int i; char c, *s=str; FILE *fp=stdout;
char *args[MAXARGS]= {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL};
if (!str || !*str) return str;
va_start(ap,str);
for (i=0; (c= *s); s++)
{ if (c=='%')
{ if (i>=MAXARGS) break;
if ( ((c= *(++s))) && (c!='%') )
{ while ( ((c= *(++s))) && (c!=' ') ); c= *(s--);
switch (c)
{ case 'd': case 'i': case 'c': args[i++]= va_arg(ap,int); break;
case 'o': case 'u': case 'x':
case 'X': args[i++]= va_arg(ap,unsigned int); break;
case 'f': case 'e': case 'g':
case 'E': case 'G': args[i++]= va_arg(ap,double); break;
case 'p': args[i++]= va_arg(ap,void *); break;
case 'n': args[i++]= va_arg(ap,int *); break;
case 's': args[i++]= va_arg(ap,char *); break;
case 'S': args[i++]= va_arg(ap,wchar_t *); break;
case 'C': args[i++]= va_arg(ap,intptr_t); break;
}
}
}
}
va_end(ap);
fpr(fp,str,args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],
args[8],args[9],args[10],args[11],args[12],args[13],args[14]);
return str;
}
*/
void prErrDoc (const char *format, ...)
{ va_list pArgs; va_start(pArgs,format);
pr("\nError message\n");
pr("\n"); vfprintf(stdout,format,pArgs); pr("\n\n\n");
}
#if 0 /* useless now that prErr() behaves similarly */
void mkTextError (char *s)
{ if (!s || !*s) return;
if (CommandOutputFile)
{ char c= s[strlen(s)-1];
if ((c=='\n') || (c=='\r')) fpr(CommandOutputFile,"%s\n",s);
else fpr(CommandOutputFile,"%s\n\n",s);
}else prErr("%s\n",s);
}
void addTextError (char *s)
{ if (!s || !*s) return;
if (CommandOutputFile)
{ char c= s[strlen(s)-1];
if ((c=='\n') || (c=='\r')) fpr(CommandOutputFile,"%s",s);
else fpr(CommandOutputFile,"%s\n",s);
}else prErr("%s\n",s);
}
void addTextToError (char *s)
{ if (!s || !*s) return;
if (CommandOutputFile)
{ char c= s[strlen(s)-1];
if ((c=='\n') || (c=='\r')) fpr(CommandOutputFile,"%s",s);
else fpr(CommandOutputFile,"%s\n",s);
}else prErr("%s\n",s);
}
#endif
void prAnswer (char *s)
{ if (!s || !*s) return;
if (CommandOutputFile) fpr(CommandOutputFile,s);
else if (ResultList && HTList_appendObject(ResultList,my_strdup(s))==NO)
prErr("Cannot put results in memory");
}
void putsAnswer (char *s, BOOL printCommand)
{ if (!s) return;
if (CommandOutputFile) {if(printCommand) fpr(CommandOutputFile,"%s\n\n",s);}
else if (*s && ResultList && HTList_appendObject(ResultList,my_strdup(s))==NO)
prErr("Cannot put a result in memory");
}
void mkTextAnswer (char *s) /* create the first "Answer" in "answers" */
{ if (!s || !*s) return;
if (CommandOutputFile)
{ char c= s[strlen(s)-1];
if ((c!='\n') && (c!='\r')) fpr(CommandOutputFile,"%s\n",s);
else fpr(CommandOutputFile,"%s",s);
}
else if (ResultList && HTList_appendObject(ResultList,my_strdup(s))==NO)
prErr("Cannot add results in memory");
}
void addTextAnswer (char *s) /* create a new "Answer" in "answers" */
{ if (!s || !*s) return;
if (CommandOutputFile)
{ int l=strlen(s)-1;
if ((s[l]=='\n') || (s[l]=='\r')) fpr(CommandOutputFile,"%s",s);
else fpr(CommandOutputFile,"%s\n",s);
}
else if (ResultList && HTList_appendObject(ResultList,my_strdup(s))==NO)
prErr("Cannot add a result in memory");
}
void addTextToAnswer (char *s) /* complete the existing "Answer" */
{ if (!s || !*s) return;
if (CommandOutputFile)
{ int l=strlen(s)-1;
if ((s[l]=='\n') || (s[l]=='\r')) fpr(CommandOutputFile,"%s",s);
else fpr(CommandOutputFile,"%s\n",s);
}
else if (ResultList && HTList_appendObject(ResultList,my_strdup(s))==NO)
prErr("Cannot add to the list of results in memory");
}
void mkTextAnswerN (int nbString, char *s)
{ int i; if (!nbString) return;
mkTextAnswer(s); s += strlen(s)+1;
for (i=1; i after the '<' or end str */
{ char c;
for (;((c= *s)); s++) { if ((c=='<') && isHTMLmarkThere(s)) return s+1; }
return s;
}
#if 1
char *skipAfterThisHTMLmark (char *s) /* in: *(s-1) == '<' */
{ char c;
while ((c= *s))
{ if (c=='"') s=skipQuotedString(s+1);
else if (c=='\'') s=skipStringQuotedLikeACharacter(s+1);
else if (c=='>') return s+1;
else s++;
}
return s;
}
#else
char *skipAfterThisHTMLmark (char *s) /* in: *(s-1) == '<' */
{ char c, *sv_s;
if ((*s=='!') && (*(s+1)=='-') && (*(s+2)=='-'))
{ if ((s= strstr(sv_s=s+3,"-->"))) s+=3;
else { prErr("Error after: %s\n \"-->\" unfound",displayableLineFrom(s));
for (s=sv_s; *s; s++);
}
}
else while ((c= *s))
{ if (c=='"') s=skipQuotedString(s+1);
else if (c=='\'') s=skipStringQuotedLikeACharacter(s+1);
else if (c=='>') return s+1;
else s++;
}
return s;
}
#endif
char *skipSpacesCommentsAndHTMLmarks (char *s)
{ char c; /* static BOOL isInHTMLcomment=NO; to do, with care, in the future */
while ((c= *s))
{ if (isspace(c)) s++;
else if (c=='\\') { c= *(++s); if (c=='\n') s++; else return s-1; }
else if ((c=='&') && (*(s+1)=='n') && (*(s+2)=='b') && (*(s+3)=='s') && (*(s+4)=='p')
&& (*(s+5)==';')) s+=6;
else
{ if ((c=='<') && isHTMLmarkThere(s)) s=skipAfterThisHTMLmark(s+1);
else if (c=='/') { c= *(s+1); if (c=='*') s=skipComments(s+2);
else if (c=='/') s=skipAfterEndOfLine(s+2);
else return s;
}
else return s;
}
}
return s;
}
char *skipSpacesCommentsAndHTMLmarksAndTellIfHTMLmarkFound(char *s,
varP BOOL *pHtmlMarkFound)
{ char c; BOOL htmlMarkFound=*pHtmlMarkFound;
while ((c= *s))
{ if (isspace(c)) s++;
else if (c=='\\') { c= *(++s); if (c=='\n') s++; else return s-1; }
else if ((c=='&') && (*(s+1)=='n') && (*(s+2)=='b') && (*(s+3)=='s') && (*(s+4)=='p')
&& (*(s+5)==';')) s+=6;
else
{ if ((c=='<')&&isHTMLmarkThere(s)) {htmlMarkFound=YES; s=skipAfterThisHTMLmark(s+1);}
else if (c=='/') { c= *(s+1); if (c=='*') s=skipComments(s+2);
else if (c=='/') s=skipAfterEndOfLine(s+2);
else return s;
}
else return s;
}
}
*pHtmlMarkFound=htmlMarkFound; return s;
}
char *skipSpacesCommentsAndHTMLmarksExceptChar (char *s, char stop)
{ char c;
while ((c= *s))
{ if (c==stop) return s;
else if (isspace(c)) s++;
else if (c=='\\') { c= *(++s); if (c=='\n') s++; else return s-1; }
else if ((c=='&') && (*(s+1)=='n') && (*(s+2)=='b') && (*(s+3)=='s') && (*(s+4)=='p')
&& (*(s+5)==';')) s+=6;
else
{ if ((c=='<') && isHTMLmarkThere(s)) s=skipAfterThisHTMLmark(s+1);
else if (c=='/') { c= *(s+1); if (c=='*') s=skipComments(s+2);
else if (c=='/') s=skipAfterEndOfLine(s+2);
else return s;
}
else return s;
}
}
return s;
}
BOOL onlySpacesCommentsOrHTMLmarksIn (char *s)
{ char c;
while ((c= *s))
{ if (isspace(c)) s++;
else if (c=='\\') { c= *(++s); if (c=='\n') s++; else return NO; }
else if ((c=='&') && (*(s+1)=='n') && (*(s+2)=='b') && (*(s+3)=='s') && (*(s+4)=='p')
&& (*(s+5)==';')) s+=6;
else
{ if ((c=='<') && isHTMLmarkThere(s)) s= skipAfterThisHTMLmark(s+1);
else if (c=='/') { c= *(++s); if (c=='*') s=skipComments(s+1);
else if (c=='/') s= skipAfterEndOfLine(s+1);
else return NO;
}
else return NO;
}
}
return YES;
}
char *removeHTMLmarksFrom (char *s)
{ char c, *dest=s, *svDest=s;
while ((c= *s))
{ if ((c=='<') && isHTMLmarkThere(s)) s= skipAfterThisHTMLmark(s+1);
else *dest++ = *s++;
}
*dest = '\0'; return svDest;
}
char *skipUntilTheseHTMLmarks (char *s, char *stopMark1,char *stopMark2)
{ char c, *token; /* return: next plain text || & "' ) s++; if (!c) return s;
if (*stopMark1)
{ *s='\0'; if (strCaseEqual(token,stopMark1)) { *s=c; return token-1; }
if (*stopMark2 && strCaseEqual(token,stopMark2)) {*s=c;return token-1;}
*s= c;
}
while ( ((c= *s)) && (c!='>') )
{ if (c=='"') s=skipQuotedString(s+1);
else if (c=='\'') s=skipStringQuotedLikeACharacter(s+1);
else s++;
}
if (c) s++;
}
else s++;
}
return s;
}
char *skipSpacesAndHTMLmarksExceptMarks (char *s, char *stopMark1,
/* s: HTML area */ char *stopMark2)
{ char c, *token; /* return: next plain text || & "' ) s++; if (!c) return s;
if (*stopMark1)
{ *s='\0'; if (strCaseEqual(token,stopMark1)) { *s=c; return token-1; }
if (*stopMark2 && strCaseEqual(token,stopMark2)) {*s=c;return token-1;}
*s= c;
}
while ( ((c= *s)) && (c!='>') )
{ if (c=='"') s=skipQuotedString(s+1);
else if (c=='\'') s=skipStringQuotedLikeACharacter(s+1);
else s++;
}
if (c) s++;
}
else return s;
}
return s;
}
#if 1
char *skipSpacesCommentsAndHTMLmarksExceptMarks (char *s, char *stopMark1,
/* s: HTML area */ char *stopMark2)
{ char c, *token; /* return: next plain text || & "' ) s++; if (!c) return s;
if (*stopMark1)
{ *s='\0'; if (strCaseEqual(token,stopMark1)) { *s=c; return token-1; }
if (*stopMark2 && strCaseEqual(token,stopMark2)) {*s=c;return token-1;}
*s= c;
}
while ( ((c= *s)) && (c!='>') )
{ if (c=='"') s=skipQuotedString(s+1);
else if (c=='\'') s=skipStringQuotedLikeACharacter(s+1);
else s++;
}
if (c) s++;
}
else if (inHTMLcomment && (c=='-')&&(*(s+1)=='-')&&(*(s+2)=='>'))
{ s+=3; inHTMLcomment=NO; }
else return s;
}
return s;
}
#else
char *skipHTMLmarksExceptMark (char *s, char *stopMark)/* s: HTML area */
{ char c, *token, *sv_s; /* return: next plain text || & ""))) s+=3;
else { prErr("Error after: %s\n \"-->\" unfound",displayableLineFrom(s));
for (s=sv_s; *s; s++);
}
}
else if ((c=='/') || isalpha(c))
{ if (*stopMark)
{ for (token=s; ((c= *s)) && !isspace(c) && (c!='>'); s++); if (!c) return s;
*s='\0'; if (strCaseEqual(token,stopMark)) { *s=c; return token-1; } *s= c;
}
else { while (((c= *s)) && !isspace(c) && (c!='>')) s++; if (!c) return s; }
while ( ((c= *s)) && (c!='>') )
{ if (c=='"') s=skipQuotedString(s+1);
else if (c=='\'') s=skipStringQuotedLikeACharacter(s+1);
else s++;
}
if (c) s++;
}
else return s-1;
}
}
return s;
}
#endif
/* for (j=0; ((c=getc(fp))!=EOF) && isspace(c); j++)
{ if (charInStr(c,"\n\r"))
{ while (j--) ungetc(c,fp); break; }
}
if (!c) { command[i]='\0';
*/
char *skipAfterAttributeAndGetItsValue (char *s, char *attr, char *defaultValue,
/*s:before attrs*/ char **pValue, char *pErasedChar)
{ char c, *token; BOOL attrFound;
while (*s && (*s!='>'))
{ token=s=skipSpaces(s);
while ( ((c= *s)) && !isspace(c) && (c!='=') && (c!='>') ) s++;
*s='\0'; attrFound= strCaseEqual(token,attr); *s= c; s=skipSpaces(s);
if (c == '=')
{ s=skipSpaces(s+1); c= *s;
if (c == '"')
{ s++; token=s; while ( ((c= *s)) && (c!='"' || *(s-1)=='\\')) s++;
if (attrFound)
{ if (c) { *s='\0'; *pValue=token; *pErasedChar= c; return s+1; }
else break;
}
}
else if (c=='\'')
{ s++; token=s; while ( ((c= *s)) && (c!='\'' || *(s-1)=='\\')) s++;
if (attrFound)
{ if (c) { *s='\0'; *pValue=token; *pErasedChar= c; return s+1; }
else break;
}
}
else if (c!='>')
{ token=s; while ( ((c= *s)) && !isspace(c) ) s++;
if (attrFound)
{ if (c) { *s='\0'; *pValue=token; *pErasedChar= c; return s+1; }
else break;
}
}
}/*else this attr. is supposed to have no associated value */
}
*pValue=defaultValue; *pErasedChar='\0'; return s;
}
void hideTrailingHTMLmarksIn (char *s, char *end_s)
{ if (*end_s) return;
#if 0
while ((--end_s>s) && (*end_s=='>')) /* hide non white_separated trailing marks */
{ while (--end_s && (*end_s != '<')) if ((end_s<=s) || (*end_s == '"')) return;
*end_s= '\0';
}
while ((end_s>s) && isspace(*s)) *s-- = '\0';
#else
for (;;) /* hide possibly white_separated trailing marks except */
{ while (--end_s && isspace(*end_s)) if (end_s<=s) return; else *end_s='\0';
if (*end_s != '>') return;
while (--end_s && (*end_s != '<')) if ((end_s<=s) || (*end_s == '"')) return;
if ((*(end_s+1)=='/') && (toupper(*(end_s+2))=='A') && (*(end_s+3)=='>')) return;
*end_s= '\0';
}
#endif
}
/* -------------- HTML FILE/STRING - transform and print HTML file ----------------- */
void prWithoutHTMLmarksIn (char *str)
{ while (*str)
{ char c, *s=skipToNextHTMLmark(str);
if (!*s) { fputs(str,stdout); break; } s--; /* *s=='<' */
c= *s; *s='\0'; if (*str) fputs(str,stdout);
*s= c; str=skipAfterThisHTMLmark(s+1);
}
}
void prWithVisibleURLs (char *str)
{ char c, erasedChar, *url=NULL, *tmpUrl, *s=str;
while (*s)
{ s=skipToNextHTMLmark(s); if (!*s) break;
if ((tolower(*s)=='a') && isspace(*(s+1))) /* "') s=skipAfterThisHTMLmark(s);
}
while ( ((s=skipToNextHTMLmark(s))) && *s )
{ /* pr("-----3: %c%c%c%c -------\n",*s,*(s+1),*(s+2),*(s+3)); */
if ( (*s=='/') && (tolower(*(s+1))=='a') && (*(s+2)=='>') )
{ s+=3; c= *s; *s='\0'; fputs(str,stdout); *s= c;
pr(" [%s] ",url);
str=s; MY_FREE(url); url=NULL; break;
}
}
}
}
puts(str); if (url) MY_FREE(url);
}
void prAccessibleDocFrom (char *url, int level, int maxLevel, BOOL htmlOnly)
{ int i; char erasedChar, *str, *s, *referedUrl,
*allocatedNewDir=NULL, *svCurrentDir=WWWdir;
/* to do: skip comments, transform relatives URLs into absolutes. Done ?? */
if (level>maxLevel) return; if (!url || !*url) return;
if (!(str= loadURLtoString(url))) return; s=str;
if (strchr(url,'/'))
{ char *absoluteNewDir= HTParse(url, WWWdir, PARSE_ALL);
char *endNewCurrentDir= strrchr(absoluteNewDir,'/')+1;
char c=*endNewCurrentDir; *endNewCurrentDir='\0';
if (!(allocatedNewDir= my_strdup(absoluteNewDir))) return;
*endNewCurrentDir=c; HT_FREE(absoluteNewDir); WWWdir= allocatedNewDir;
/* tr("tmp dir from %s: %s.\n",url,WWWdir); */
}
while (*s)
{ s=skipToNextHTMLmark(s); if (!*s) break;
if ((tolower(*s)=='a') && isspace(*(s+1))) /* "') s=skipAfterThisHTMLmark(s);
}
}
else if (!strncasecomp(s,"base",4) && isspace(*(s+4)))
{ s=skipAfterAttributeAndGetItsValue(s+5,"href","",&referedUrl,&erasedChar);
if (*referedUrl)
{ if (allocatedNewDir) MY_FREE(allocatedNewDir);
if (!(allocatedNewDir= my_strdup(referedUrl))) return;
WWWdir= allocatedNewDir;
/* tr("new base in %s: %s.\n",url,WWWdir); */
}
if (erasedChar) { *(s-1)=erasedChar;
if (erasedChar!='>') s=skipAfterThisHTMLmark(s);
}
}
}
if (allocatedNewDir) MY_FREE(allocatedNewDir);
WWWdir=svCurrentDir; MY_FREE(str);
}
void prElemInDoc (char *elem, char *document) /* in Fuchsia/Aqua/Lime/Red/Blue */
{ BOOL imageInDest=NO, elemIsSection=NO, isAnHTMLDoc=YES;
int occurrence=1, wantedOccurrence=1;
char c, *s, *str, *elemInDoc,*sv_elemInDoc=NULL, *mark, *afterElem;
char *fileContent, *absolute_url=HTParse(document, WWWdir, PARSE_ALL);
/*tr("%s %s\n",elem,absolute_url);*/
if (!(fileContent= loadURLtoString(absolute_url))) return; /* error mess. printed */
str=fileContent;
if (*elem=='{')
if (isdigit(*(elem+1)) || (isspace(*(elem+1)) && isdigit(*(elem+2))))
{ wantedOccurrence= (isdigit(*(elem+1))) ? *(elem+1)-'0' : *(elem+2)-'0';
elem=skipRecursiveBraces(elem+2); while (isspace(*elem)) elem++;
pr("Element1:%s.\n",elem);
}
if (!strncasecomp(elem,"{section title:",15))
{ char *e; BOOL found=NO; elemIsSection=YES;
elem+=15; while (isspace(*elem)) elem++;
s=strrchr(elem,'}'); if (s) { while (isspace(*s)) s--; *s='\0'; }
for (s=str; !found && ((elemInDoc= strContentInStr(elem,s,initP &afterElem))); s=afterElem)
{ for (e=elemInDoc; !found && (str') && (strThe section of title \"%s\" has not been found in %s
\n",
elem, absolute_url); return;
}
}
else
{ for (s=str; ((elemInDoc= strContentInStr(elem,s,initP &afterElem))); s=afterElem)
{ if (occurrence==wantedOccurrence) break; occurrence++; sv_elemInDoc=elemInDoc; }
if (!elemInDoc)
{ if (!sv_elemInDoc)
{ prErrDoc("The following document element has not been found in %s:"
"
\n%s", absolute_url, elem); return;
}else elemInDoc=sv_elemInDoc;
}
}
/*if (!(elemInDoc= strContentInStr(elem, str, initP &afterElem)))
{ prErrDoc("The following document element has not been found in %s:
\n%s",
absolute_url, elem); return;
}*/
if ((mark= strcasestr(str,""); isAnHTMLDoc=NO; }
if (strchr(document,'/'))
{ char *absoluteNewDir= HTParse(document, WWWdir, PARSE_ALL);
char *endNewCurrentDir= strrchr(absoluteNewDir,'/')+1;
c=*endNewCurrentDir; *endNewCurrentDir='\0';
pr("\n\n",absoluteNewDir); HT_FREE(absoluteNewDir);
}
else pr("\n\n",WWWdir);
c=*elemInDoc; *elemInDoc='\0'; fputs(str,stdout); *elemInDoc=c;
if (sv_elemInDoc)
pr("[the %d%s occurrence of the desired element has not been "
"found\nbut the %d%s occurrence is here highlighted]\n",
wantedOccurrence, orderSuffixForDigit(wantedOccurrence),
occurrence, orderSuffixForDigit(occurrence));
pr("\n");
if (!elemIsSection && strcasestr(elem,"![]()
Destination of the clicked relation:
\n");
}
c=*afterElem; *afterElem='\0';
pr("%s\n",elemInDoc);
*afterElem=c; str=afterElem;
if (imageInDest)
{ pr(" ----- End of the destination of the "
"clicked relation -----
\n");
}
fputs(str,stdout); if (!isAnHTMLDoc) pr("");
MY_FREE(fileContent);
}
#if 0
void displayCGsUsingBCGapplet (char *cgs)
{ pr("\nCG graphical display\n");
pr("\n");
if (!cgs || !*cgs) pr("No CG to display\n");
else
{ int fdPipeToCG2BCG[2],fdPipeFromCG2BCG[2]; tr("CG to display: %s\n",cgs);
if(pipe(FdPipeToProcessor) == -1) return "Cannot create FdPipeToProcessor !";
if(pipe(FdPipeFromProcessor) == -1)return "Cannot create FdPipeFromProcessor !";
/*fcntl(FdPipeInCommand[0], F_SETFL, O_NDELAY);*/
/*fcntl(FdPipeOutCommand[0],F_SETFL, O_NDELAY);*/
ProcessorPID= startProcess(SANTIAGO_BIN,argv,envp,
FdPipeToProcessor[0],FdPipeFromProcessor[1]);
if (ProcessorPID == -1) return "Cannot start the processor";
tr("Santiago PID:%d\n",ProcessorPID);
/*write(FdPipeToProcessor[1],"test",4); TmpBuff[0]='\0';TmpBuff[1]='\0';
read(FdPipeFromProcessor[0],TmpBuff,2); TmpBuff[2]='\0'; pr("***%s***\n",TmpBuff);*/
FdToProcessor=FdPipeToProcessor[1]; /* no FILE*fp: fdopen() doesn't work */
FdFromProcessor=FdPipeFromProcessor[0];
for (i=0; ((tok= argv[i])) && *tok; i++)
{ write(FdToProcessor,tok,strlen(tok)); write(FdToProcessor," ",1); }
write(FdToProcessor,";\n",2); /* the '\n' is very important */
}
pr("\n\n\n");
}
#endif
/* ***************************** PROCESS ************************************** */
/* call the next function on a string before giving it to popen */
char searchNonEscapedShellCharactersIn (char *s)
{ while (*s)
{ s=skipSpaces(s);
switch (*s)
{ case '\0': return '\0';
case '"' : s=skipQuotedString(s+1); break;
case '\'': s=skipStringQuotedLikeACharacter(s+1); break;
case '\\': s++; if (*s) s++; break;
case';': case '|': case '&': case '(': case ')': case '{': case '}':
case'$': case '*': case '?': case '<': case '>': case '[': case ']':
case'^': return *s;
default: s++;
}
}
return '\0';
}
void runCommandWithWWWfiles (int nbFirstArgNotOption, int nbFirstFile,
char *argv[], int argc, FILE *OutputFp, BOOL withXMP)
{ int i=nbFirstFile, j, nbLoaded=0;
HTAssocList *tmpAssocList,
*assocList=HTAssocList_new(); /*Assoc copy name to orig. file name*/
HTAssoc *attrValuePair; FILE *fp;
HTChunk *chunk=HTChunk_new(1000); char *str; /* chunk/str store the call */
if (!chunk || !assocList) { prErr("Out of memory"); return; }
HTChunk_puts(chunk,argv[0]); HTChunk_putc(chunk,' '); /* command name */
for (j=1; j=19)
{ argc=19; prErr("Only the %d first files will be processed.\n",19-j); }
#if 1
if ((nbLoaded= loadURLsToTmpfiles(argc-i,&argv[i],varP assocList)))
{ tmpAssocList=assocList;
while ((attrValuePair= (HTAssoc *) HTList_nextObject(tmpAssocList)))
if (attrValuePair->name && attrValuePair->name[0]) /*if file copied*/
{ HTChunk_puts(chunk,attrValuePair->name); HTChunk_putc(chunk,' '); }
}
#else
for (; i&1"); str=HTChunk_data(chunk); /* pr("\n%s\n",str); */
if (!(fp= popen(str,"r")))
prErr("Internal problem: cannot call %s.\n",argv[0]);
else if (OutputFp)
{ if (withXMP) fputs("\n",OutputFp); /* or plaintext (!listing) */
while (fgets(str,999,fp))
fputs(doOneReplacementUsingAssocList(str,assocList),OutputFp);
if (withXMP) fputs("\n",OutputFp); pclose(fp);
}
for (tmpAssocList=assocList;
(attrValuePair= HTAssocList_nextObject(tmpAssocList));
unlink(HTAssoc_name(attrValuePair)) );
}
HTChunk_delete(chunk); HTAssocList_delete(assocList);
}
int execCommand (char *command, char *argv[], char *envp[],
int input, int output, int errput)
{ int pid, pidChild, status;
if ((pidChild= fork()) == -1) { prErr("Cannot fork!!!"); return 1; }
if (!pidChild) /* code for the child process */
{ tr("I am the child process\n");
if (input>=0) if (dup2(input,0)==-1)
prErr("Failed to duplicate %d as an input",input);
if (output>=0) if (dup2(output,1)==-1)
prErr("Failed to duplicate %d as an output",output);
if (errput>=0) if (dup2(errput,2)==-1)
prErr("Failed to duplicate %d as an error output",errput);
execve(command,argv,envp);
prErr("%s cannot be executed",command);
exit(1); /* the child must exit now */
}
tr("child %d created\n",pidChild);
while (((pid= wait(&status))) && (pid>0) && (pid!=pidChild));
if (WIFEXITED(status)) return WEXITSTATUS(status); else return -1;
}
int execCommands (int nbCommands, char *commandArray[], char **argvArray[],
char *envp[], int input, int output, int errput)
{ int i, pidChild, status;
/* *pidChildArray= (int *) my_calloc(nbCommands,sizeof(int)); */
for (i=0; i=0) if (dup2(input,0)==-1)
prErr("Failed to duplicate %d as an input",input);
if (output>=0) if (dup2(output,1)==-1)
prErr("Failed to duplicate %d as an output",output);
if (errput>=0) if (dup2(errput,2)==-1)
prErr("Failed to duplicate %d as an error output",errput);
execve(commandArray[i],argvArray[i],envp);
prErr("'%s %s %s ' cannot be executed: %s'", commandArray[i],
argvArray[i][1],argvArray[i][2],strerror(errno));
exit(1); /* the child must exit now */
}
}
tr("childs created. I wait them to end\n");
while (((pidChild= wait(&status))) && (pidChild>0));
tr("All childs have just ended. I go on\n");
if (WIFEXITED(status)) return WEXITSTATUS(status); else return -1;
}
int startProcess (char *command,char *argv[],char *envp[],int input,int output)
{ int pidChild; /* if (pidChild) kill(pidChild, SIGKILL); */
if ((pidChild= fork()) == -1) { prErr("Cannot fork!!!"); return -1; }
if (!pidChild) /* code for the child process */
{ if (input>=0) if (dup2(input,0)==-1)
prErr("Failed to duplicate %d as an input",input);
if (output>=0) if (dup2(output,1)==-1)
prErr("Failed to duplicate %d as an output",output);
execve(command,argv,envp);
prErr("%s cannot be executed",command);
exit(1); /* the child must exit now */
}
tr("%s is running with input:%d output:%d\n",command,input,output);
return pidChild;
}