1 /* $NetBSD: doscommain.c,v 1.1 2024/06/29 13:45:14 rin Exp $ */
2
3 /*
4 * Copyright (c) 1996
5 * Matthias Drochner. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 /* argument line processing for DOS .COM programs */
30
31 #include <lib/libsa/stand.h>
32
33 /* The Program Segment Prefix */
34
35 static struct psp{
36 char mist1[0x2c];
37 short envseg;
38 char mist2[0x80-2-0x2c];
39 char cmdlen;
40 char cmd[127];
41 } *PSP = (struct psp*)0;
42
43 static char* argv[64]; /* theor max */
44
45 static int whitespace(char);
46
47 static int
whitespace(char c)48 whitespace(char c)
49 {
50 if ((c == '\0') || (c == ' ') || (c == '\t')
51 || (c == '\r') || (c == '\n'))
52 return (1);
53 return 0;
54 }
55
56 enum state {skipping, doing_arg, doing_long_arg};
57
58 /* build argv/argc, start real main() */
59 int doscommain(void);
60 extern int main(int, char**);
61
62 int
doscommain(void)63 doscommain(void)
64 {
65 int argc, i;
66 enum state s;
67
68 argv[0] = "???"; /* we don't know */
69 argc = 1;
70 s = skipping;
71
72 for (i = 0; i < PSP->cmdlen; i++){
73
74 if (whitespace(PSP->cmd[i])) {
75 if (s == doing_arg) {
76 /* end of argument word */
77 PSP->cmd[i] = '\0';
78 s = skipping;
79 }
80 continue;
81 }
82
83 if (PSP->cmd[i] == '"') {
84 /* start or end long arg
85 * (end only if next char is whitespace)
86 * XXX but '" ' cannot be in argument
87 */
88 switch (s) {
89 case skipping:
90 /* next char begins new argument word */
91 argv[argc++] = &PSP->cmd[i + 1];
92 s = doing_long_arg;
93 break;
94 case doing_long_arg:
95 if (whitespace(PSP->cmd[i + 1])) {
96 PSP->cmd[i] = '\0';
97 s = skipping;
98 }
99 case doing_arg:
100 /* ignore in the middle of arguments */
101 default:
102 break;
103 }
104 continue;
105 }
106
107 /* all other characters */
108 if (s == skipping) {
109 /* begin new argument word */
110 argv[argc++] = &PSP->cmd[i];
111 s = doing_arg;
112 }
113 }
114 if (s != skipping)
115 PSP->cmd[i] = '\0'; /* to be sure */
116
117 /* start real main() */
118 return main(argc, argv);
119 }
120