/*
*
* ©K. D. Hedger. Mon 12 Jun 12:02:04 BST 2017 keithdhedger@gmail.com
* This file (main.cpp) is part of LFSMakePkg.
* LFSMakePkg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
* LFSMakePkg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with LFSMakePkg. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define scriptsArg 1
#define rootArg 2
#define whatToDo 3
#define startArg 4
#define RED "\e[1;31m"
#define GREEN "\e[1;32m"
#define NORMAL "\e[0;0m"
#define BLUE "\e[1;34m"
#define BOLDBLACK "\e[1;38m"
#define DELAY 75
enum {NONL=0,NLONERR,NLONOUT,NLONBOTH,CRONBOTH};
struct dependsStruct
{
char* scriptPath;
char* version;
char* name;
char* doWhat;
};
struct scriptsStruct
{
char* scriptPath;
char* dependsString;
char* version;
char* name;
char* installedVersion;
bool installed;
bool checked;
};
scriptsStruct scripts[1000];
dependsStruct dependsList[1000];
int numScripts=0;
char* scriptsFolder=NULL;
char* rootFolder=NULL;
char* libFolder=NULL;
int numDepends=0;
const char* spinner="-\\|/";
int spincnt=0;
int spindelay=0;
bool quiet=false;
char *strBuffer[PATH_MAX]={0,};
char *pBuffer=(char*)&strBuffer;
bool anyVersion=false;
char* cleanFolderPath(const char* path)
{
#if 1
char tpath[PATH_MAX]={0,};
char *cleanpath=NULL;
bool lastwasslash=false;
unsigned topos=0;
for(int j=0;j/dev/null",libFolder,scripts[numScripts].name);
fp=popen(command,"r");
line[0]=0;
fgets(line,1024,fp);
if(strlen(line)>0)
{
dash=strrchr(line,'-');
line[(long)dash-(long)line]=0;
dash=strrchr(line,'-');
dash=(char*)(long)dash+1;
version=strdup(dash);
scripts[numScripts].installed=true;
scripts[numScripts].installedVersion=version;
}
else
{
scripts[numScripts].installed=false;
scripts[numScripts].installedVersion=NULL;
}
free(command);
pclose(fp);
}
void getData(void)
{
char line[2048];
int fd;
int dataread=-1;
char *scriptpath=NULL;
asprintf(&scriptpath,"%s%s",rootFolder,scripts[numScripts].scriptPath);
fd=open(scriptpath,O_RDONLY);
if(fd!=-1)
{
dataread=read(fd,&line[0],2047);
if(dataread==-1)
{
printOut(RED "ERROR " NORMAL "Can't read any data from ",scripts[numScripts].scriptPath,NLONOUT);
close(fd);
exit(200);
}
line[dataread-1]=0;
scripts[numScripts].name=getNameFromData(line);
scripts[numScripts].version=getVersionFromData(line);
scripts[numScripts].dependsString=getDependsFromData(line);
checkInstalled();
close(fd);
}
else
{
printOut(RED "ERROR " NORMAL "Can't open file ",scripts[numScripts].scriptPath,NLONOUT);
exit(200);
}
free(scriptpath);
}
void getScripts(void)
{
char *command;
char line[1024];
FILE *fp;
char *sf=NULL;
unsigned offset=0;
if(strlen(rootFolder)>1)
offset=strlen(rootFolder)-1;
asprintf(&command,"find %s -mindepth 3 -maxdepth 3 -iname \"*.LFSBuild\" |sort",scriptsFolder);
fp=popen(command,"r");
while(fgets(line,1024,fp)!=NULL)
{
line[strlen(line)-1]=0;
scripts[numScripts].scriptPath=strdup(&line[offset]);
scripts[numScripts].checked=false;
getData();
numScripts++;
fprintf(stderr,GREEN "Locating scripts " NORMAL "%c\r",spinner[spincnt]);
spindelay++;
if(spindelay>DELAY)
{
spindelay=0;
spincnt++;
if(spincnt==4)
spincnt=0;
}
}
pclose(fp);
free(command);
printf("\n");
}
int getScriptStructFromName(char* name)
{
int retval=-1;
for(int j=0; j0)
result=1;
if(mult==0)
result=0;
if(mult<0)
result=-1;
return(result);
}
char* chompedStr(char* str)
{
char* newstr;
long start;
long end;
start=0;
while(isspace(str[start])!=false)
start++;
end=strlen(str)-1;
while(isspace(str[end])!=false)
end--;
newstr=strndup(&str[start],end-start+1);
return(newstr);
}
int multiCheck(char* want,char* installed,char* inscript)
{
double realvers1=0;
double realvers2=0;
double realvers3=0;
char* holdstr=NULL;
char* saveptr=NULL;
double mult=0;
int result=0;
holdstr=strdup(want);
strtok_r(holdstr,".",&saveptr);
mult=10000000000;
do
{
realvers1=realvers1+(atoi(holdstr)*mult);
mult=mult/1000;
}
while((holdstr=strtok_r(NULL,".",&saveptr))!=NULL);
free(holdstr);
holdstr=strdup(installed);
strtok_r(holdstr,".",&saveptr);
mult=10000000000;
do
{
realvers2=realvers2+(atoi(holdstr)*mult);
mult=mult/1000;
}
while((holdstr=strtok_r(NULL,".",&saveptr))!=NULL);
free(holdstr);
holdstr=strdup(inscript);
strtok_r(holdstr,".",&saveptr);
mult=10000000000;
do
{
realvers3=realvers3+(atoi(holdstr)*mult);
mult=mult/1000;
}
while((holdstr=strtok_r(NULL,".",&saveptr))!=NULL);
free(holdstr);
if((realvers1-realvers2)<=0)
{
return(0);
}
if((realvers1-realvers3)<=0)
{
return(1);
}
else
{
return(-1);
}
}
void listDepends(char* depstr)
{
char* holdstr=NULL;
char* wantname=NULL;
char* wantversion=NULL;
char* dash=NULL;
bool gotit=false;
int scriptnum=0;
char* saveptr=NULL;
int versres=0;
char* strippedstring=NULL;
holdstr=strdup(depstr);
strtok_r(holdstr," ",&saveptr);
do
{
strippedstring=chompedStr(holdstr);
dash=strrchr(strippedstring,'-');
if((dash!=NULL) && (isdigit(dash[1])==true))
{
wantversion=strdup(&dash[1]);
wantname=strndup(strippedstring,(long)dash-(long)strippedstring);
}
else
{
wantname=strdup(strippedstring);
wantversion=strdup("0");
}
gotit=false;
for(int j=0; j