revert between 56095 -> 55830 in arch
[AROS.git] / rom / usb / poseidon / AddUSBClasses.c
blobaf880736d014aa46c1b3f2ad44fb3e04d334a7cd
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 #define CLASSPATH "SYS:Classes/USB"
23 #define CLASSNAMEMAX 128
25 static const char *template = "QUIET/S,REMOVE/S";
26 const char *version = "$VER: AddUSBClasses 1.8 (24.05.2017) © The AROS Development Team";
27 static IPTR ArgsArray[ARGS_SIZEOF];
28 static struct RDArgs *ArgsHook = NULL;
30 void fail(char *str)
32 if(ArgsHook)
34 FreeArgs(ArgsHook);
35 ArgsHook = NULL;
37 if(str)
39 PutStr(str);
40 exit(20);
42 exit(0);
45 int main(int argc, char *argv[])
47 UBYTE buf[1024];
48 struct Library *ps;
49 struct ExAllControl *exall;
50 struct ExAllData *exdata;
51 STRPTR errmsg = NULL;
52 struct List *puclist;
53 struct Node *puc;
54 UBYTE sbuf[CLASSNAMEMAX];
55 BPTR lock;
56 ULONG ents;
57 ULONG namelen;
58 BOOL exready, isvalid;
60 if(!(ArgsHook = ReadArgs(template, ArgsArray, NULL)))
62 fail("Wrong arguments!\n");
65 if((ps = OpenLibrary("poseidon.library", 4)))
67 if(ArgsArray[ARGS_REMOVE])
69 psdLockWritePBase();
70 psdGetAttrs(PGA_STACK, NULL, PA_ClassList, &puclist, TAG_END);
71 while(puclist->lh_Head->ln_Succ)
73 if(!ArgsArray[ARGS_QUIET])
75 Printf("Removing class %s...\n", puclist->lh_Head->ln_Name);
77 psdUnlockPBase();
78 psdRemClass(puclist->lh_Head);
79 psdLockWritePBase();
81 psdUnlockPBase();
82 } else {
83 if((exall = AllocDosObject(DOS_EXALLCONTROL, NULL)))
85 lock = Lock(CLASSPATH, ACCESS_READ);
86 if(lock)
88 exall->eac_LastKey = 0;
89 exall->eac_MatchString = NULL;
90 exall->eac_MatchFunc = NULL;
93 exready = ExAll(lock, (struct ExAllData *) buf, 1024, ED_NAME, exall);
94 exdata = (struct ExAllData *) buf;
95 ents = exall->eac_Entries;
96 while(ents--)
98 isvalid = TRUE;
99 psdSafeRawDoFmt(sbuf, CLASSNAMEMAX, CLASSPATH "/%s", exdata->ed_Name);
100 namelen = strlen(sbuf);
101 if (((namelen > 4) && (!strcmp(&sbuf[namelen-4], ".dbg"))) || ((namelen > 5) && (!strcmp(&sbuf[namelen-5], ".info"))))
102 isvalid = FALSE;
103 if (isvalid)
105 if(namelen > 4)
107 if(!strcmp(&sbuf[namelen-4], ".elf"))
109 sbuf[namelen-4] = 0;
112 psdGetAttrs(PGA_STACK, NULL, PA_ClassList, &puclist, TAG_END);
113 Forbid();
114 puc = puclist->lh_Head;
115 while(puc->ln_Succ)
117 if(!strncmp(puc->ln_Name, exdata->ed_Name, strlen(puc->ln_Name)))
119 break;
121 puc = puc->ln_Succ;
123 if(puc->ln_Succ)
125 Permit();
126 if(!ArgsArray[ARGS_QUIET])
128 Printf("Skipping class %s...\n", exdata->ed_Name);
130 exdata = exdata->ed_Next;
131 continue;
133 Permit();
135 if(!ArgsArray[ARGS_QUIET])
137 Printf("Adding class %s...", exdata->ed_Name);
139 if(psdAddClass(sbuf, 0))
141 if(!ArgsArray[ARGS_QUIET])
143 PutStr("okay!\n");
145 } else {
146 if(!ArgsArray[ARGS_QUIET])
148 PutStr("failed!\n");
152 exdata = exdata->ed_Next;
154 } while(exready);
155 UnLock(lock);
156 psdClassScan();
157 } else {
158 errmsg = "Failed to lock " CLASSPATH ".\n";
160 FreeDosObject(DOS_EXALLCONTROL, exall);
163 CloseLibrary(ps);
164 } else {
165 errmsg = "Unable to open poseidon.library\n";
167 fail(errmsg);
168 return(0); // never gets here, just to shut the compiler up