-
Notifications
You must be signed in to change notification settings - Fork 1
/
apps.c
526 lines (414 loc) · 15.7 KB
/
apps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include "apps.h"
#include "config.h"
#include "categories.h"
#include <fnmatch.h>
ListNode *Apps=NULL;
ListNode *AppsGetList()
{
return(Apps);
}
int AppPlatformMatches(TAction *App, const char *Platforms)
{
if (! StrValid(Platforms)) return (TRUE);
if (InList(App->Platform, Platforms)) return(TRUE);
if ( (PlatformType(App->Platform)==PLATFORM_WINDOWS) && InList("windows", Platforms) ) return(TRUE);
return(FALSE);
}
int AppMatchCompareVars(TAction *App, const char *VarName, const char *Match)
{
const char *ptr;
if (! StrValid(Match)) return(TRUE);
ptr=GetVar(App->Vars, VarName);
if (InList(Match, ptr)) return(TRUE);
return(FALSE);
}
int AppMatches(TAction *Template, TAction *App)
{
if (! StrValid(Template->Platform)) return(TRUE);
if (! AppPlatformMatches(App, Template->Platform) ) return(FALSE);
if (! AppMatchCompareVars(App, "category", GetVar(Template->Vars, "category"))) return(FALSE);
if ((Template->Flags & FLAG_INSTALLED) && (! AppIsInstalled(App)) ) return(FALSE);
if (StrValid(Template->Name))
{
if (! pmatch_one(Template->Name, App->Name, StrLen(App->Name), NULL, NULL, 0)) return(FALSE);
}
return(TRUE);
}
void LoadAppConfigToAct(TAction *Act, const char *Config)
{
char *Name=NULL, *Value=NULL, *Tempstr=NULL;
const char *ptr;
TPlatform *Plt;
ptr=GetNameValuePair(Config," ", "=", &Name, &Value);
while (ptr)
{
StripQuotes(Name);
StripQuotes(Value);
if (StrValid(Name))
{
if (strcmp(Name,"url")==0)
{
Act->URL=CopyStr(Act->URL, Value);
Tempstr=URLBasename(Tempstr, Act->URL);
SetVar(Act->Vars, "url-basename", Tempstr);
Tempstr=CopyStr(Tempstr, Act->URL);
StrRTruncChar(Tempstr, '?');
StrRTruncChar(Tempstr, '/');
SetVar(Act->Vars, "url-path", Tempstr);
}
else if (strcmp(Name,"platform")==0)
{
Act->Platform=CopyStr(Act->Platform, PlatformUnAlias(Value));
Plt=PlatformFind(Act->Platform);
if (Plt)
{
Act->PlatformID=Plt->ID;
if (Plt->Flags & PLATFORM_FLAG_NOEXEC) Act->Flags |= FLAG_NOEXEC;
if (StrValid(Plt->UnpackDir)) SetVar(Act->Vars, "unpack-dir", Plt->UnpackDir);
}
else fprintf(stderr, "PLATFORM NOT FOUND: '%s' for %s\n", Act->Platform, Act->Name);
}
//locale is a special case, we want to be able to use 'locale=' to set a locale string, but we
//also want to be able to use $(locale) as a variable in config. Further, we want to be able to say
//'locale="$(lang):$(country)". But when we do the substitution and set locale to it's final value, we
//destroy the variables by subsituting them. So if we want to try a different locale in future, we now
//can't do that. Thus we set 'locale_template' and use that as the template to substitute into 'locale',
//so 'locale_template' always reamins unchanged.
else if (strcmp(Name, "locale")==0) SetVar(Act->Vars, "locale_template", Value);
else SetVar(Act->Vars, Name, Value);
if (strcasecmp(Name,"install-path")==0) Act->InstallPath=CopyStr(Act->InstallPath, Value);
else if (strcasecmp(Name,"install-name")==0) Act->InstallName=CopyStr(Act->InstallName, Value);
else if (strcasecmp(Name,"dlname")==0) Act->DownName=CopyStr(Act->DownName, Value);
else if (strcasecmp(Name,"install-type")==0)
{
if (strcasecmp(Value,"unpack")==0) Act->InstallType=INSTALL_UNPACK;
if (strcasecmp(Value,"executable")==0) Act->InstallType=INSTALL_EXECUTABLE;
}
else if (strcasecmp(Name,"sha256")==0)
{
strlwr(Value);
SetVar(Act->Vars, Name, Value);
}
else if (strcasecmp(Name,"bundled")==0)
{
//app is bundled with another app! Set the bundled flag, and set a variable
Act->Flags |= FLAG_BUNDLED;
SetVar(Act->Vars, "bundled-with", Value);
}
else if (strcasecmp(Name,"parent")==0) Act->Parent=CopyStr(Act->Parent, Value);
else if (strcasecmp(Name,"copyfiles-from")==0) Act->PostProcess=MCatStr(Act->PostProcess, Name, "=", Value, " ", NULL);
else if (strcasecmp(Name,"copyfiles-to")==0) Act->PostProcess=MCatStr(Act->PostProcess, Name, "=", Value, " ", NULL);
else if (strcasecmp(Name,"movefiles-from")==0) Act->PostProcess=MCatStr(Act->PostProcess, Name, "=", Value, " ", NULL);
else if (strcasecmp(Name,"movefiles-to")==0) Act->PostProcess=MCatStr(Act->PostProcess, Name, "=", Value, " ", NULL);
else if (strcasecmp(Name,"chext")==0) Act->PostProcess=MCatStr(Act->PostProcess, Name, "=", Value, " ", NULL);
else if (strcasecmp(Name,"delete")==0) Act->PostProcess=MCatStr(Act->PostProcess, Name, "=", Value, " ", NULL);
else if (strcasecmp(Name,"rename")==0) Act->PostProcess=MCatStr(Act->PostProcess, Name, "=", Value, " ", NULL);
else if (strcasecmp(Name,"link")==0) Act->PostProcess=MCatStr(Act->PostProcess, Name, "=", Value, " ", NULL);
else if (strcasecmp(Name,"zip")==0) Act->PostProcess=MCatStr(Act->PostProcess, Name, "=", Value, " ", NULL);
else if (strcasecmp(Name,"dlc")==0) Act->Flags |= FLAG_DLC;
else if (strcasecmp(Name,"install_stage2")==0) SetVar(Act->Vars, "install-stage2", Value);
else if (strcasecmp(Name,"category")==0)
{
Tempstr=CategoriesExpand(Tempstr, Value);
SetVar(Act->Vars, "category", Tempstr);
}
else if (strcasecmp(Name,"extra_category")==0)
{
Tempstr=CategoriesExpand(Tempstr, Value);
AppendVar(Act->Vars, "category", Tempstr);
}
}
ptr=GetNameValuePair(ptr," ", "=", &Name, &Value);
}
Destroy(Tempstr);
Destroy(Value);
Destroy(Name);
}
static TAction *AppConfigure(const char *Name, const char *Settings, ListNode *FileWideSettings)
{
ListNode *Curr;
TAction *Act;
char *Config=NULL;
Act=ActionCreate(ACT_NONE, Name);
LoadAppConfigToAct(Act, Settings);
Curr=ListGetNext(FileWideSettings);
while (Curr)
{
if (strcmp(Curr->Tag, "*")==0) Config=MCatStr(Config, (const char *) Curr->Item, " ", NULL);
//it's possible for an app to not have a url if it comes bundled with something else, so be sure to
//check Act->URL exists before feeding to fnmatch
if (Act->URL && (strncmp(Curr->Tag, "url=",4)==0) && (fnmatch(Curr->Tag+4, Act->URL, 0)==0) ) Config=MCatStr(Config, (const char *) Curr->Item, " ", NULL);
Curr=ListGetNext(Curr);
}
Config=CatStr(Config, Settings);
LoadAppConfigToAct(Act, Config);
Destroy(Config);
return(Act);
}
void AppsLoadFromFile(const char *Path, ListNode *Apps)
{
STREAM *S;
ListNode *Node;
TAction *Act;
char *Tempstr=NULL, *Token=NULL, *AppConfig=NULL;
ListNode *FileWideSettings;
const char *ptr;
FileWideSettings=ListCreate();
S=STREAMOpen(Path, "r");
if (S)
{
Tempstr=STREAMReadLine(Tempstr, S);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
if ((StrLen(Tempstr) > 0) && (*Tempstr != '#'))
{
ptr=GetToken(Tempstr, "\\S", &Token, GETTOKEN_QUOTES);
// '*' for the app name means this line applies to all apps in this file
if (
(strcmp(Token, "*")==0) ||
(strncmp(Token, "url=", 4)==0)
) SetVar(FileWideSettings, Token, ptr);
else if (StrValid(Token))
{
Act=AppConfigure(Token, ptr, FileWideSettings);
ListAddNamedItem(Apps, Token, Act);
}
}
Tempstr=STREAMReadLine(Tempstr, S);
}
STREAMClose(S);
}
ListDestroy(FileWideSettings, Destroy);
Destroy(AppConfig);
Destroy(Tempstr);
Destroy(Token);
}
//bundles are situations were more than one application is supplied in the same package
void AppAddToBundle(ListNode *Apps, TAction *App, const char *ParentName)
{
ListNode *Curr;
TAction *Parent;
char *Tempstr=NULL;
//find the parent app/install that this item is bundled with
Curr=ListFindNamedItem(Apps, ParentName);
while (Curr)
{
if (strcmp(Curr->Tag, ParentName)==0)
{
Parent=(TAction *) Curr->Item;
if (strcmp(Parent->Platform, App->Platform)==0)
{
Tempstr=CopyStr(Tempstr, GetVar(Parent->Vars, "bundles"));
Tempstr=MCatStr(Tempstr, " ", App->Name, NULL);
SetVar(Parent->Vars, "bundles", Tempstr);
}
}
Curr=ListGetNext(Curr);
}
Destroy(Tempstr);
}
void AppsProcessBundles(ListNode *Apps)
{
ListNode *Curr;
TAction *App;
const char *ptr;
Curr=ListGetNext(Apps);
while (Curr)
{
App=(TAction *) Curr->Item;
if (App->Flags & FLAG_BUNDLED)
{
ptr=GetVar(App->Vars, "bundled-with");
if (StrValid(ptr)) AppAddToBundle(Apps, App, ptr);
}
Curr=ListGetNext(Curr);
}
}
char *AppsListExpand(char *FileList, const char *ConfigFiles)
{
char *Tempstr=NULL, *Token=NULL;
const char *ptr;
glob_t Glob;
int i;
FileList=CopyStr(FileList, "");
Tempstr=FormatPath(Tempstr, ConfigFiles);
ptr=GetToken(Tempstr, ",", &Token, 0);
while (ptr)
{
glob(Token, 0, 0, &Glob);
for (i=0; i < Glob.gl_pathc; i++) FileList=MCatStr(FileList, Glob.gl_pathv[i], ",", NULL);
globfree(&Glob);
ptr=GetToken(ptr, ",", &Token, 0);
}
Destroy(Tempstr);
Destroy(Token);
return(FileList);
}
ListNode *AppsLoad(const char *ConfigFiles)
{
char *FileList=NULL, *Token=NULL;
const char *ptr;
if (! Apps) Apps=ListCreate();
FileList=AppsListExpand(FileList, ConfigFiles);
ptr=GetToken(FileList, ",", &Token, 0);
while (ptr)
{
AppsLoadFromFile(Token, Apps);
ptr=GetToken(ptr, ",", &Token, 0);
}
AppsProcessBundles(Apps);
Destroy(FileList);
Destroy(Token);
return(Apps);
}
char *AppFormatPath(char *Path, TAction *Act)
{
char *Tempstr=NULL;
const char *ptr;
//first generate sommelier root (usually ~/.sommelier) if it doesn't exist
ptr=GetVar(Act->Vars, "sommelier_root");
if (! StrValid(ptr))
{
if (Config->Flags & FLAG_SYSTEM_INSTALL) Path=SubstituteVarsInString(Path, "/opt/", Act->Vars, 0);
else Path=SubstituteVarsInString(Path, "$(homedir)/.sommelier/", Act->Vars, 0);
SetVar(Act->Vars, "sommelier_root",Path);
}
Tempstr=MCopyStr(Tempstr, GetVar(Act->Vars, "sommelier_root"), "/patches", NULL);
SetVar(Act->Vars, "sommelier_patches_dir", Tempstr);
ptr=GetVar(Act->Vars, "prefix");
if (! StrValid(ptr))
{
Tempstr=CopyStr(Tempstr, GetVar(Act->Vars, "prefix_template"));
Path=SubstituteVarsInString(Path, Tempstr, Act->Vars, 0);
Path=SlashTerminateDirectoryPath(Path);
SetVar(Act->Vars, "prefix",Path);
}
else Path=CopyStr(Path, ptr);
//for dos and golang/go path==prefix
//for wine path is more complex
if (Act->PlatformID==PLATFORM_WINDOWS)
{
Path=CatStr(Path,"drive_c/");
SetVar(Act->Vars, "drive_c",Path);
if (StrValid(Act->InstallPath)) Path=SubstituteVarsInString(Path, Act->InstallPath, Act->Vars, 0);
else Path=SubstituteVarsInString(Path, "$(drive_c)/Program Files/$(name)", Act->Vars, 0);
}
else
{
SetVar(Act->Vars, "drive_c",Path);
}
Path=SlashTerminateDirectoryPath(Path);
SetVar(Act->Vars, "install-dir",Path);
ResolveVar(Act->Vars, "emu-dir");
DestroyString(Tempstr);
return(Path);
}
int AppIsInstalled(TAction *App)
{
char *Tempstr=NULL;
int result=FALSE;
Tempstr=AppFormatPath(Tempstr, App);
if (access(Tempstr, F_OK)==0) result=TRUE;
Destroy(Tempstr);
return(result);
}
int AppFindConfig(TAction *App, const char *Platforms)
{
TAction *AppConfig;
ListNode *Curr;
int result=FALSE;
Curr=ListGetNext(Apps);
while (Curr)
{
if (StrValid(Curr->Tag) && (strcasecmp(App->Name, Curr->Tag)==0))
{
AppConfig=(TAction *) Curr->Item;
//if no platform requested, or app platform matches requested
//then we've found the right one
if (AppPlatformMatches(AppConfig, Platforms))
{
//flags can already have been set by the command-line, so
//we have to | these
App->Flags |= AppConfig->Flags;
App->InstallType |= AppConfig->InstallType;
App->Platform=CopyStr(App->Platform, AppConfig->Platform);
App->DownName=CopyStr(App->DownName, AppConfig->DownName);
App->Parent=CopyStr(App->Parent, AppConfig->Parent);
App->PlatformID=AppConfig->PlatformID;
App->PostProcess=CopyStr(App->PostProcess, AppConfig->PostProcess);
if (! StrValid(App->URL)) App->URL=CopyStr(App->URL, AppConfig->URL);
CopyVars(App->Vars, AppConfig->Vars);
result=TRUE;
break;
}
}
Curr=ListGetNext(Curr);
}
return(result);
}
void AppSetLocale(TAction *App, const char *LocaleStr)
{
char *Lang=NULL, *Country=NULL;
const char *ptr;
ptr=GetToken(LocaleStr, "_", &Lang, 0);
Country=CopyStr(Country, ptr);
StrRTruncChar(Country, '.');
SetVar(App->Vars, "lang", Lang);
SetVar(App->Vars, "country", Country);
strlwr(Country);
SetVar(App->Vars, "country:lwr", Country);
ptr=GetVar(App->Vars, "locale_template");
if (StrValid(ptr))
{
Lang=SubstituteVarsInString(Lang, ptr, App->Vars, 0);
SetVar(App->Vars, "locale", Lang);
}
Destroy(Country);
Destroy(Lang);
}
int AppLoadConfig(TAction *App)
{
int result=FALSE;
char *Tempstr=NULL;
const char *ptr;
Tempstr=PlatformSelect(Tempstr, App);
if (StrValid(Tempstr)) printf("Selected Platforms: %s\n", Tempstr);
//if no platform specified this will use the first matching app config it finds for any platform
result=AppFindConfig(App, Tempstr);
if (StrValid(App->Parent)) SetVar(App->Vars, "name", App->Parent);
else if (StrValid(App->InstallName)) SetVar(App->Vars, "name", App->InstallName);
ptr=getenv("LANGUAGE");
if (! StrValid(ptr)) ptr=getenv("LANG");
if (! StrValid(ptr)) ptr="en_US";
AppSetLocale(App, ptr);
//we don't need the path that is returned here, but this function sets a lot of default variables and paths
if (result) Tempstr=AppFormatPath(Tempstr, App);
Destroy(Tempstr);
return(result);
}
int AppsOutputList(TAction *Template)
{
ListNode *Curr;
int result=FALSE;
TAction *App;
const char *p_dl;
char *Flags=NULL, *Tempstr=NULL;
Curr=ListGetNext(Apps);
while (Curr)
{
App=(TAction *) Curr->Item;
if (AppMatches(Template, App))
{
Flags=CopyStr(Flags, "");
if (StrValid(App->URL)) Flags=CatStr(Flags, "www,");
if (AppIsInstalled(App)) Flags=CatStr(Flags,"i,");
printf("%-25s %12s %-12s %-12s ", App->Name, App->Platform, Flags, GetVar(App->Vars, "category"));
printf("%s\n",GetVar(App->Vars, "comment"));
}
Curr=ListGetNext(Curr);
}
Destroy(Tempstr);
Destroy(Flags);
return(result);
}