TLIBAPI TSTRPTR TEKlib_GetModDir(TAPTR boot, TTAGITEM *tags) { TSTRPTR sysdir; TSTRPTR s; TINT l; s = (TSTRPTR) TGetTag(tags, TExecBase_ModDir, (TTAG) TEKHOST_MODDIR); l = strlen(s); sysdir = TEKlib_Alloc(boot, l + 1); if (sysdir) strcpy(sysdir, s); return sysdir; }
static TAPTR init_openexecmodule(struct TEKlibInit *init, TTAGITEM *usertags) { /* load exec module */ init->tli_ExecMod = TEKlib_LoadModule(init->tli_BootHnd, init->tli_ProgDir, init->tli_ModDir, "exec", usertags); if (init->tli_ExecMod) { /* get exec module entrypoint */ init->tli_ExecEntry = TEKlib_GetEntry(init->tli_BootHnd, init->tli_ExecMod, "tek_init_exec"); if (init->tli_ExecEntry) { TUINT psize, nsize; TUINT16 version; struct TExecBase *execbase; /* get version and size */ version = (TUINT16) TGetTag(init->tli_HALTags, TExecBase_Version, 0); psize = TEKlib_CallModule(init->tli_BootHnd, init->tli_ExecMod, init->tli_ExecEntry, TNULL, TNULL, version, init->tli_HALTags); nsize = TEKlib_CallModule(init->tli_BootHnd, init->tli_ExecMod, init->tli_ExecEntry, TNULL, TNULL, 0xffff, init->tli_HALTags); /* get memory for the exec module */ execbase = TEKlib_Alloc(init->tli_BootHnd, nsize + psize); if (execbase) { /* initialize execbase */ execbase = (struct TExecBase *) (((TINT8 *) execbase) + nsize); if (TEKlib_CallModule(init->tli_BootHnd, init->tli_ExecMod, init->tli_ExecEntry, TNULL, execbase, 0, init->tli_HALTags)) { init->tli_ExecBase = execbase; return execbase; } TEKlib_Free(init->tli_BootHnd, ((TINT8 *) execbase) - nsize, nsize + psize); } } TEKlib_CloseModule(init->tli_BootHnd, init->tli_ExecMod); } init->tli_ExecBase = TNULL; return TNULL; }
TLIBAPI TAPTR TEKlib_LoadModule(TAPTR boot, TSTRPTR progdir, TSTRPTR moddir, TSTRPTR modname, TTAGITEM *tags) { TAPTR knmod = TNULL; TINT len1, len2, len3; TSTRPTR t; struct TInitModule *imod; struct host_modhandle *handle; handle = TEKlib_Alloc(boot, sizeof(*handle)); if (!handle) return TNULL; imod = host_lookupmodule(modname, tags); if (imod) { handle->entry.func = imod->tinm_InitFunc; handle->type = TYPE_LIB; return handle; } len1 = progdir ? strlen(progdir) : 0; len2 = moddir ? strlen(moddir) : 0; len3 = strlen(modname); /* + mod/ + .so + \0 */ t = TEKlib_Alloc(boot, TMAX(len1, len2) + len3 + 4 + TEKHOST_EXTLEN + 1); if (t) { if (progdir) strcpy(t, progdir); strcpy(t + len1, "mod/"); strcpy(t + len1 + 4, modname); strcpy(t + len1 + 4 + len3, TEKHOST_EXTSTR); TDBPRINTF(3,("trying dlopen %s\n", t)); knmod = host_getmodule(t); if (!knmod) { if (moddir) strcpy(t, moddir); strcpy(t + len2, modname); strcpy(t + len2 + len3, TEKHOST_EXTSTR); TDBPRINTF(3,("trying dlopen %s\n", t)); knmod = host_getmodule(t); } if (!knmod) TDBPRINTF(20,("dlopen %s failed\n", modname)); TEKlib_FreeVec(boot, t); } if (knmod) { handle->entry.object = knmod; handle->type = TYPE_DLL; } else { TEKlib_FreeVec(boot, handle); handle = TNULL; } return handle; }
TLIBAPI TSTRPTR TEKlib_GetProgDir(TAPTR boot, TTAGITEM *tags) { TSTRPTR progdir = TNULL; TSTRPTR *argv; TINT argc; progdir = (TSTRPTR) TGetTag(tags, TExecBase_ProgDir, TEKHOST_PROGDIR); if (progdir) { TINT l = strlen(progdir); TSTRPTR p = TEKlib_Alloc(boot, l + 1); if (p) { strcpy(p, progdir); return p; } return TNULL; } argc = (TINT) TGetTag(tags, TExecBase_ArgC, 0); argv = (TSTRPTR *) TGetTag(tags, TExecBase_ArgV, TNULL); if (argc >= 1 && argv) { TSTRPTR olddir = TEKlib_Alloc(boot, MAX_PATH_LEN); if (olddir) { if (getcwd(olddir, MAX_PATH_LEN)) { progdir = TEKlib_Alloc(boot, MAX_PATH_LEN + 1); if (progdir) { TBOOL success = TFALSE; TINT l = 0; TSTRPTR s = argv[0]; TINT c; while (*s) { s++; l++; } if (l > 0) { success = TTRUE; while ((c = *(--s))) { if (c == '/') break; l--; } if (l > 0) { TSTRPTR d, pathpart = TEKlib_Alloc(boot, l + 1); success = TFALSE; if (pathpart) { s = argv[0]; d = pathpart; while (l--) *d++ = *s++; *d = 0; success = (chdir(pathpart) == 0); TEKlib_FreeVec(boot, pathpart); } } } if (success) success = (getcwd(progdir, MAX_PATH_LEN) != TNULL); if (!(chdir(olddir) == 0)) success = TFALSE; if (success) strcat(progdir, "/"); else { TEKlib_FreeVec(boot, progdir); progdir = TNULL; } } } TEKlib_FreeVec(boot, olddir); } } return progdir; }
static struct TTask *init_tek_create(TTAGITEM *usertags) { struct TEKlibInit *init; TAPTR boot = TEKlib_Init(usertags); if (boot == TNULL) return TNULL; init = TEKlib_Alloc(boot, sizeof(struct TEKlibInit)); if (init) { init->tli_BootHnd = boot; init->tli_ProgDir = TEKlib_GetProgDir(boot, usertags); init->tli_SysDir = TEKlib_GetSysDir(boot, usertags); init->tli_ModDir = TEKlib_GetModDir(boot, usertags); /* load host abstraction module */ if (init_openhalmodule(init, usertags)) { /* load exec module */ if (init_openexecmodule(init, usertags)) { /* place application task into current context */ init->tli_ExecTags[0].tti_Tag = TTask_Name; init->tli_ExecTags[0].tti_Value = (TTAG) TTASKNAME_ENTRY; init->tli_ExecTags[1].tti_Tag = TTask_InitData; init->tli_ExecTags[1].tti_Value = (TTAG) init; init->tli_ExecTags[2].tti_Tag = TTAG_MORE; init->tli_ExecTags[2].tti_Value = (TTAG) usertags; init->tli_AppTask = TExecCreateSysTask(init->tli_ExecBase, TNULL, init->tli_ExecTags); if (init->tli_AppTask) { /* fill in missing fields in execbase */ ((struct TModule *) init->tli_ExecBase)->tmd_HALMod = init->tli_ExecMod; ((struct TModule *) init->tli_ExecBase)->tmd_InitTask = init->tli_AppTask; /* create ramlib task */ init->tli_ExecTags[0].tti_Value = (TTAG) TTASKNAME_RAMLIB; init->tli_IOTask = TExecCreateSysTask(init->tli_ExecBase, init_execfunc, init->tli_ExecTags); if (init->tli_IOTask) { /* create execbase task */ init->tli_ExecTags[0].tti_Value = (TTAG) TTASKNAME_EXEC; init->tli_ExecTask = TExecCreateSysTask(init->tli_ExecBase, init_execfunc, init->tli_ExecTags); if (init->tli_ExecTask) { /* this is the backdoor for the remaining ** initializations in the entrytask context */ if (TExecDoExec(init->tli_ExecBase, TEXEC_CMD_INIT, TNULL)) { struct THandle *ath = (struct THandle *) init->tli_AppTask; /* overwrite apptask destructor */ init->tli_OrgAppTaskHook = ath->thn_Hook; TInitHook(&ath->thn_Hook, init_destroyapptask, TNULL); init_appatoms(init, usertags); /* application is running */ return init->tli_AppTask; } TExecSignal(init->tli_ExecBase, init->tli_ExecTask, TTASK_SIG_ABORT); TDESTROY(init->tli_ExecTask); } TExecSignal(init->tli_ExecBase, init->tli_IOTask, TTASK_SIG_ABORT); TDESTROY(init->tli_IOTask); } TDESTROY(init->tli_AppTask); } init_closeexecmodule(init); } else TDBPRINTF(20, ("could not open Exec module\n")); init_closehalmodule(init); } else TDBPRINTF(20, ("could not open HAL module\n")); TEKlib_FreeVec(boot, init->tli_ProgDir); TEKlib_FreeVec(boot, init->tli_SysDir); TEKlib_FreeVec(boot, init->tli_ModDir); TEKlib_Free(boot, init, sizeof(struct TEKlibInit)); } TEKlib_Exit(boot); return TNULL; }