Skip to content

Latest commit

 

History

History
195 lines (157 loc) · 4.5 KB

mojosetup.c

File metadata and controls

195 lines (157 loc) · 4.5 KB
 
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
#include "universal.h"
#include "platform.h"
#include "fileio.h"
#include "gui.h"
/* !!! FIXME: None of these should be here. */
#include <dlfcn.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
int GArgc = 0;
char **GArgv = NULL;
MojoGui *GGui = NULL;
static boolean mojoInputToPhysicalFile(MojoInput *in, const char *fname)
{
FILE *out = NULL;
boolean iofailure = false;
char buf[1024];
size_t br;
STUBBED("mkdir first?");
if (in == NULL)
return false;
STUBBED("fopen?");
unlink(fname);
out = fopen(fname, "wb");
if (out == NULL)
return false;
while (!iofailure)
{
br = in->read(in, buf, sizeof (buf));
STUBBED("how to detect read failures?");
if (br == 0) /* we're done! */
break;
else if (br < 0)
iofailure = true;
else
{
if (fwrite(buf, br, 1, out) != 1)
iofailure = true;
} /* else */
} /* while */
fclose(out);
if (iofailure)
{
unlink(fname);
return false;
} /* if */
return true;
} /* mojoInputToPhysicalFile */
static void *loadGuiPlugin(MojoArchive *ar)
{
char fname[256];
void *retval = NULL;
boolean rc;
MojoInput *io = ar->openCurrentEntry(ar);
if (io == NULL)
return false;
STUBBED("Don't copy if it's a physical file already?");
STUBBED("Filename creation has to change");
{
struct timeval tv;
gettimeofday(&tv, NULL);
snprintf(fname, sizeof (fname), "/tmp/mojosetup-gui-%ld.so", tv.tv_sec);
}
rc = mojoInputToPhysicalFile(io, fname);
io->close(io);
if (rc)
{
retval = dlopen(fname, RTLD_NOW | RTLD_GLOBAL);
STUBBED("abstract out dlopen!");
if (retval != NULL)
{
STUBBED("abstract out dlsym, too. :)");
void *entry = dlsym(retval, MOJOGUI_ENTRY_POINT_STR);
if (entry == NULL) /* not a compatible plugin. */
{
STUBBED("aaaaaaand, dlclose")
dlclose(retval);
unlink(fname);
retval = NULL;
} /* if */
} /* if */
} /* if */
return retval;
} /* loadGuiPlugin */
typedef struct S_DLLLIST { void *lib; struct S_DLLLIST *next; } DllList;
static boolean findGuiPlugin(void)
{
const MojoArchiveEntryInfo *entinfo = NULL;
DllList plugins = { NULL, NULL };
DllList *i = NULL;
if (GGui != NULL)
return true;
STUBBED("DllList should track filename, isstatic, and entrypoint, too");
/*
* !!! FIXME: Have a global MojoArchive that represents the install
* !!! FIXME: (either an archive, a physical dir, etc.)
*/
STUBBED("use a global MojoArchive for basedir");
MojoArchive *dir = MojoArchive_newFromDirectory(".");
if (dir == NULL)
return false;
dir->restartEnumeration(dir);
while ((entinfo = dir->enumEntry(dir)) != NULL)
{
void *lib;
DllList *item;
/* Not a file? */
if (entinfo->type != MOJOARCHIVE_ENTRY_FILE)
continue;
/* not in the gui dir? */
if (strncmp(entinfo->filename, "guiplugins/", 4) != 0)
continue;
lib = loadGuiPlugin(dir);
if (lib == NULL)
continue;
item = malloc(sizeof (DllList));
if (item == NULL)
continue;
item->lib = lib;
item->next = plugins.next;
plugins.next = item;
} /* while */
STUBBED("Add static plugins to the list");
STUBBED("Choose plugin by priority");
for (i = plugins.next; i != NULL; i = i->next)
{
MOJOGUI_STRUCT * (*entry)(void) = dlsym(i->lib, MOJOGUI_ENTRY_POINT_STR);
MOJOGUI_STRUCT *gui = entry();
if ( gui->priority(gui) != ((uint8) MOJOGUI_PRIORITY_NEVER_TRY) )
{
if (gui->init(gui))
{
GGui = gui;
break;
} /* if */
} /* if */
} /* for */
STUBBED("close libs, delete tmp files, free list.");
STUBBED("Don't close this when it's a global.");
dir->close(dir);
return (GGui != NULL);
} /* findGuiPlugin */
/*
* This is called from main()/WinMain()/whatever.
*/
int MojoSetup_main(int argc, char **argv)
{
GArgc = argc;
GArgv = argv;
if (!findGuiPlugin())
return 1;
GGui->msgbox(GGui, "Title", "text goes here.");
GGui->deinit(GGui);
STUBBED("cleanup gui lib, etc.\n");
return 0;
} /* MojoSetup_main */
/* end of mojosetup.c ... */