Fix for the (stupid) case of app (always) passing
[tangerine.git] / rom / dos / readitem.c
blobbf80d28a266b1f562b30dbb192dc4e59e5c587cc
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: english
7 */
8 #include <proto/exec.h>
9 #include <dos/rdargs.h>
10 #include <dos/dosasl.h>
11 #include <dos/dosextens.h>
12 #include "dos_intern.h"
14 /*****************************************************************************
16 NAME */
17 #include <proto/dos.h>
19 AROS_LH3(LONG, ReadItem,
21 /* SYNOPSIS */
22 AROS_LHA(STRPTR, buffer, D1),
23 AROS_LHA(LONG, maxchars, D2),
24 AROS_LHA(struct CSource *, input, D3),
26 /* LOCATION */
27 struct DosLibrary *, DOSBase, 135, Dos)
29 /* FUNCTION
30 Read an item from a given character source. Items are words
31 or quoted strings seperated by whitespace or '=' just like on
32 the commandline. The seperator is unread and the read string
33 is terminated by a NUL character.
35 INPUTS
36 buffer - Buffer to be filled.
37 maxchars - Size of the buffer. Must be at least 1 (for the terminator).
38 input - A ready to use CSource structure or NULL which means
39 "read from the input stream".
41 RESULT
42 One of ITEM_UNQUOTED - Normal word read.
43 ITEM_QUOTED - Quoted string read.
44 ITEM_NOTHING - End of line found. Nothing read.
45 ITEM_EQUAL - '=' read. Buffer is empty.
46 ITEM_ERROR - An error happened. IoErr() gives additional
47 information in that case.
49 NOTES
50 This function handles conversion of '**', '*"', etc inside quotes.
52 EXAMPLE
54 BUGS
56 SEE ALSO
58 INTERNALS
60 *****************************************************************************/
62 AROS_LIBFUNC_INIT
63 AROS_LIBBASE_EXT_DECL(struct DosLibrary *,DOSBase)
65 /* Macro to get a character from the input source */
66 #define GET(c) \
67 if(input!=NULL) \
68 { \
69 if(input->CS_CurChr>=input->CS_Length) \
70 c=EOF; \
71 else \
72 c=input->CS_Buffer[input->CS_CurChr++]; \
73 }else \
74 { \
75 c=FGetC(Input()); \
76 if(c==EOF&&*result) \
77 return ITEM_ERROR; \
80 /* Macro to push the character back */
81 #define UNGET() {if(input!=NULL) input->CS_CurChr--; else UnGetC(Input(),-1);}
83 STRPTR b=buffer;
84 LONG c;
85 LONG *result=&((struct Process *)FindTask(NULL))->pr_Result2;
87 /* Skip leading whitespace characters */
90 GET(c);
91 }while(c==' '||c=='\t'||c=='\n');
93 if(!c||c=='\n'||c==EOF)
96 End of line found. Note that unlike the Amiga DOS original
97 this funtion doesn't know about ';' comments. Comments are
98 the shell's job, IMO. I don't need them here.
100 if(c!=EOF)
101 UNGET();
102 *b=0;
103 return ITEM_NOTHING;
104 }else if(c=='=')
106 /* Found '='. Return it. */
107 *b=0;
108 return ITEM_EQUAL;
109 }else if(c=='\"')
110 /* Quoted item found. Convert Contents. */
111 for(;;)
113 if(!maxchars)
115 *buffer=0;
116 *result=ERROR_BUFFER_OVERFLOW;
117 return ITEM_ERROR;
119 maxchars--;
120 GET(c);
121 /* Convert ** to *, *" to ", *n to \n and *e to 0x1b. */
122 if(c=='*')
124 GET(c);
125 /* Check for premature end of line. */
126 if(!c||c=='\n'||c==EOF)
128 if(c!=EOF)
129 UNGET();
130 *buffer=0;
131 *result=ERROR_UNMATCHED_QUOTES;
132 return ITEM_ERROR;
133 }else if(c=='n'||c=='N')
134 c='\n';
135 else if(c=='e'||c=='E')
136 c=0x1b;
137 }else if(!c||c=='\n'||c==EOF)
139 if(c!=EOF)
140 UNGET();
141 *buffer=0;
142 *result=ERROR_UNMATCHED_QUOTES;
143 return ITEM_ERROR;
144 }else if(c=='\"')
146 /* " ends the item. */
147 *b=0;
148 return ITEM_QUOTED;
150 *b++=c;
152 else
154 /* Unquoted item. Store first character. */
155 if(!maxchars)
157 *buffer=0;
158 *result=ERROR_BUFFER_OVERFLOW;
159 return ITEM_ERROR;
161 maxchars--;
162 *b++=c;
163 /* Read upto the next terminator. */
164 for(;;)
166 if(!maxchars)
168 *buffer=0;
169 *result=ERROR_BUFFER_OVERFLOW;
170 return ITEM_ERROR;
172 maxchars--;
173 GET(c);
174 /* Check for terminator */
175 if(!c||c==' '||c=='\t'||c=='\n'||c=='='||c==EOF)
177 if(c!=EOF)
178 UNGET();
179 *b=0;
180 return ITEM_UNQUOTED;
182 *b++=c;
185 AROS_LIBFUNC_EXIT
186 } /* ReadItem */