finish migrating from arosmesa to mesa3dgl
[AROS.git] / rom / dos / allocdosobject.c
blobb542adb6a78a4576616de1bd1f1029a7ace69aa6
1 /*
2 Copyright © 1995-2013 The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
8 #include <exec/memory.h>
9 #include <proto/exec.h>
10 #include <dos/exall.h>
11 #include <utility/tagitem.h>
12 #include <proto/utility.h>
13 #include <dos/rdargs.h>
14 #include <dos/dostags.h>
15 #include "dos_intern.h"
17 /*****************************************************************************
19 NAME */
20 #include <proto/dos.h>
22 AROS_LH2(APTR, AllocDosObject,
24 /* SYNOPSIS */
25 AROS_LHA(ULONG , type, D1),
26 AROS_LHA(const struct TagItem *, tags, D2),
28 /* LOCATION */
29 struct DosLibrary *, DOSBase, 38, Dos)
31 /* FUNCTION
32 Creates a new dos object of a given type. This memory has to be
33 freed with FreeDosObject().
35 INPUTS
36 type - Object type.
37 tags - Pointer to taglist array with additional information. See
38 <dos/dostags.h> for a list of all supported tags.
40 RESULT
41 Pointer to new object or NULL, to indicate an error.
43 NOTES
45 EXAMPLE
47 BUGS
49 SEE ALSO
51 INTERNALS
53 *****************************************************************************/
55 AROS_LIBFUNC_INIT
56 APTR mem;
58 switch(type)
60 case DOS_FILEHANDLE:
61 mem = AllocVec(sizeof(struct FileHandle), MEMF_CLEAR);
63 if (mem != NULL)
65 struct FileHandle *fh = (struct FileHandle *)mem;
67 fh->fh_Pos = -1;
68 fh->fh_End = -1;
70 fh->fh_Func2 = DOS_FH_MAGIC;
72 return mem;
74 case DOS_FIB:
75 return AllocVec(sizeof(struct FileInfoBlock), MEMF_CLEAR);
77 case DOS_STDPKT:
78 return allocdospacket();
80 case DOS_EXALLCONTROL:
81 return AllocVec(sizeof(struct InternalExAllControl), MEMF_CLEAR);
83 case DOS_CLI:
85 struct CommandLineInterface *cli = NULL;
86 struct TagItem defaults[] =
88 /* 0 */ { ADO_DirLen, 255 },
89 /* 1 */ { ADO_CommNameLen, 255 },
90 /* 2 */ { ADO_CommFileLen, 255 },
91 /* 3 */ { ADO_PromptLen, 255 },
92 { TAG_END, 0 }
95 STRPTR dir = NULL;
96 STRPTR command = NULL;
97 STRPTR file = NULL;
98 STRPTR prompt = NULL;
100 /* C has no exceptions. This is a simple replacement. */
101 #define ENOMEM_IF(a) if(a) goto enomem /* Throw out of memory. */
103 cli = AllocVec(sizeof(struct CommandLineInterface), MEMF_CLEAR);
104 ENOMEM_IF(cli == NULL);
106 cli->cli_FailLevel = RETURN_ERROR;
107 cli->cli_Background = DOSTRUE;
108 ApplyTagChanges(defaults, (struct TagItem *)tags);
110 dir = AllocVec(defaults[0].ti_Data + 1, MEMF_PUBLIC | MEMF_CLEAR);
111 ENOMEM_IF(dir == NULL);
113 AROS_BSTR_setstrlen(MKBADDR(dir), 0);
114 cli->cli_SetName = MKBADDR(dir);
116 command = AllocVec(defaults[1].ti_Data + 1,
117 MEMF_PUBLIC | MEMF_CLEAR);
118 ENOMEM_IF(command == NULL);
120 AROS_BSTR_setstrlen(MKBADDR(command), 0);
121 cli->cli_CommandName = MKBADDR(command);
123 file = AllocVec(defaults[2].ti_Data + 1, MEMF_PUBLIC | MEMF_CLEAR);
124 ENOMEM_IF(file == NULL);
126 AROS_BSTR_setstrlen(MKBADDR(file), 0);
127 cli->cli_CommandFile = MKBADDR(file);
129 prompt = AllocVec(defaults[3].ti_Data + 1,
130 MEMF_PUBLIC | MEMF_CLEAR);
131 ENOMEM_IF(prompt == NULL);
133 AROS_BSTR_setstrlen(MKBADDR(prompt), 0);
134 cli->cli_Prompt = MKBADDR(prompt);
136 return cli;
138 enomem:
139 FreeVec(cli);
141 FreeVec(dir);
142 FreeVec(command);
143 FreeVec(file);
144 FreeVec(prompt);
146 SetIoErr(ERROR_NO_FREE_STORE);
148 return NULL;
151 case DOS_RDARGS:
152 return AllocVec(sizeof(struct RDArgs), MEMF_CLEAR);
155 SetIoErr(ERROR_BAD_NUMBER);
157 return NULL;
159 AROS_LIBFUNC_EXIT
160 } /* AllocDosObject */
162 /* Internal routines for packet allocation. Does not require DOSBase. */
163 struct DosPacket *allocdospacket(void)
165 struct StandardPacket *sp = AllocVec(sizeof(struct StandardPacket), MEMF_CLEAR);
167 if (sp == NULL)
168 return NULL;
170 sp->sp_Pkt.dp_Link = &sp->sp_Msg;
171 sp->sp_Msg.mn_Node.ln_Name = (char *)&sp->sp_Pkt;
173 return &sp->sp_Pkt;