grub2: bring back build of aros-side grub2 tools
[AROS.git] / rom / usb / poseidon / AddUSBClasses.c
blob7a1a531f73214f28ff4d333f942e2d15b2970581
1 /*
2 ** AddUSBClasses by Chris Hodges <chrisly@platon42.de>
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <math.h>
9 #include <exec/exec.h>
10 #include <dos/dosextens.h>
11 #include <dos/datetime.h>
12 #include <dos/exall.h>
13 #include <libraries/poseidon.h>
14 #include <proto/poseidon.h>
15 #include <proto/exec.h>
16 #include <proto/dos.h>
18 #define ARGS_QUIET 0
19 #define ARGS_REMOVE 1
20 #define ARGS_SIZEOF 2
22 static const char *template = "QUIET/S,REMOVE/S";
23 const char *version = "$VER: AddUSBClasses 1.7 (03.06.09) by Chris Hodges <chrisly@platon42.de>";
24 static IPTR ArgsArray[ARGS_SIZEOF];
25 static struct RDArgs *ArgsHook = NULL;
27 void fail(char *str)
29 if(ArgsHook)
31 FreeArgs(ArgsHook);
32 ArgsHook = NULL;
34 if(str)
36 PutStr(str);
37 exit(20);
39 exit(0);
42 int main(int argc, char *argv[])
44 struct Library *ps;
45 struct ExAllControl *exall;
46 BPTR lock;
47 STRPTR errmsg = NULL;
48 struct ExAllData *exdata;
49 ULONG ents;
50 struct List *puclist;
51 UBYTE buf[1024];
52 STRPTR sbuf;
53 BOOL exready;
54 struct Node *puc;
55 ULONG namelen;
56 STRPTR classpath;
58 if(!(ArgsHook = ReadArgs(template, ArgsArray, NULL)))
60 fail("Wrong arguments!\n");
63 if((ps = OpenLibrary("poseidon.library", 4)))
65 if(ArgsArray[ARGS_REMOVE])
67 psdLockWritePBase();
68 psdGetAttrs(PGA_STACK, NULL, PA_ClassList, &puclist, TAG_END);
69 while(puclist->lh_Head->ln_Succ)
71 if(!ArgsArray[ARGS_QUIET])
73 Printf("Removing class %s...\n", puclist->lh_Head->ln_Name);
75 psdUnlockPBase();
76 psdRemClass(puclist->lh_Head);
77 psdLockWritePBase();
79 psdUnlockPBase();
80 } else {
81 if((exall = AllocDosObject(DOS_EXALLCONTROL, NULL)))
83 classpath = "SYS:Classes/USB";
84 lock = Lock(classpath, ACCESS_READ);
85 if(lock)
87 exall->eac_LastKey = 0;
88 exall->eac_MatchString = NULL;
89 exall->eac_MatchFunc = NULL;
92 exready = ExAll(lock, (struct ExAllData *) buf, 1024, ED_NAME, exall);
93 exdata = (struct ExAllData *) buf;
94 ents = exall->eac_Entries;
95 while(ents--)
97 sbuf = psdCopyStrFmt("%s/%s", classpath, exdata->ed_Name);
98 if(!sbuf)
100 break;
102 namelen = strlen(sbuf);
103 if(namelen > 4)
105 if(!strcmp(&sbuf[namelen-4], ".elf"))
107 sbuf[namelen-4] = 0;
110 psdGetAttrs(PGA_STACK, NULL, PA_ClassList, &puclist, TAG_END);
111 Forbid();
112 puc = puclist->lh_Head;
113 while(puc->ln_Succ)
115 if(!strncmp(puc->ln_Name, exdata->ed_Name, strlen(puc->ln_Name)))
117 break;
119 puc = puc->ln_Succ;
121 if(puc->ln_Succ)
123 Permit();
124 if(!ArgsArray[ARGS_QUIET])
126 Printf("Skipping class %s...\n", exdata->ed_Name);
128 exdata = exdata->ed_Next;
129 continue;
131 Permit();
133 if(!ArgsArray[ARGS_QUIET])
135 Printf("Adding class %s...", exdata->ed_Name);
137 if(psdAddClass(sbuf, 0))
139 if(!ArgsArray[ARGS_QUIET])
141 PutStr("okay!\n");
143 } else {
144 if(!ArgsArray[ARGS_QUIET])
146 PutStr("failed!\n");
149 psdFreeVec(sbuf);
150 exdata = exdata->ed_Next;
152 } while(exready);
153 UnLock(lock);
154 psdClassScan();
155 } else {
156 errmsg = "Could not lock on SYS:Classes/USB.\n";
158 FreeDosObject(DOS_EXALLCONTROL, exall);
161 CloseLibrary(ps);
162 } else {
163 errmsg = "Unable to open poseidon.library\n";
165 fail(errmsg);
166 return(0); // never gets here, just to shut the compiler up