VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin.c@ 228

Last change on this file since 228 was 228, checked in by bird, 20 years ago

fixing and adding more commands.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/* $Id: kmkbuiltin.c 228 2005-02-09 11:30:52Z bird $ */
2/** @file
3 *
4 * kMk Builtin command execution.
5 *
6 * Copyright (c) 2005 knut st. osmundsen <[email protected]>
7 *
8 *
9 * This file is part of kBuild.
10 *
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#include <string.h>
28#include <stdlib.h>
29#include <stdio.h>
30#include <ctype.h>
31#include "kmkbuiltin.h"
32
33extern char **environ;
34
35extern int kmk_builtin_cp(int argc, char **argv, char **envp);
36extern int kmk_builtin_chmod(int argc, char **argv, char **envp);
37extern int kmk_builtin_echo(int argc, char **argv, char **envp);
38extern int kmk_builtin_mkdir(int argc, char **argv, char **envp);
39extern int kmk_builtin_mv(int argc, char **argv, char **envp);
40extern int kmk_builtin_rm(int argc, char **argv, char **envp);
41extern int kmk_builtin_rmdir(int argc, char **argv, char **envp);
42
43int kmk_builtin_command(const char *pszCmd)
44{
45 int argc;
46 char **argv;
47 char *psz;
48 int rc;
49
50 /*
51 * Check and skip the prefix.
52 */
53 if (strncmp(pszCmd, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
54 {
55 printf("kmk_builtin: Invalid command prefix '%s'!\n", pszCmd);
56 return 1;
57 }
58
59 /*
60 * Parse arguments.
61 */
62 argc = 0;
63 argv = NULL;
64 while (*pszCmd)
65 {
66 const char *pszEnd;
67 const char *pszNext;
68 int fEscaped = 0;
69 size_t cch;
70
71 /*
72 * Find start and end of the current command.
73 */
74 if (*pszCmd == '"' || *pszCmd == '\'')
75 {
76 pszEnd = pszCmd;
77 for (;;)
78 {
79 pszEnd = strchr(pszEnd + 1, *pszCmd);
80 if (!pszEnd)
81 {
82 printf("kmk_builtin: Unbalanced quote in argument %d: %s\n", argc + 1, pszCmd);
83 while (argc--)
84 free(argv[argc]);
85 free(argv);
86 return 1;
87 }
88 /* two quotes -> escaped quote. */
89 if (pszEnd[0] != pszEnd[1])
90 break;
91 fEscaped = 1;
92 }
93 pszNext = pszEnd + 1;
94 pszCmd++;
95 }
96 else
97 {
98 pszEnd = pszCmd;
99 while (!isspace(*pszEnd) && *pszEnd)
100 pszEnd++;
101 pszNext = pszEnd;
102 }
103
104 /*
105 * Make argument.
106 */
107 if (!(argc % 16))
108 {
109 void *pv = realloc(argv, sizeof(char *) * (argc + 17));
110 if (!pv)
111 {
112 printf("kmk_builtin: out of memory. argc=%d\n", argc);
113 break;
114 }
115 argv = (char **)pv;
116 }
117 cch = pszEnd - pszCmd;
118 argv[argc] = malloc(cch + 1);
119 if (!argv[argc])
120 {
121 printf("kmk_builtin: out of memory. argc=%d len=%d\n", argc, pszEnd - pszCmd + 1);
122 break;
123 }
124 memcpy(argv[argc], pszCmd, cch);
125 argv[argc][cch] = '\0';
126
127 /* unescape quotes? */
128 if (fEscaped)
129 {
130 char ch = pszCmd[-1];
131 char *pszW = argv[argc];
132 char *pszR = argv[argc];
133 while (*pszR)
134 {
135 if (*pszR == ch)
136 pszR++;
137 *pszW++ = *pszR++;
138 }
139 *pszW = '\0';
140 }
141 /* commit it */
142 argv[++argc] = NULL;
143
144 /*
145 * Next
146 */
147 pszCmd = pszNext;
148 if (isspace(*pszCmd) && *pszCmd)
149 pszCmd++;
150 }
151
152 /*
153 * Execute the command if parsing was successful.
154 */
155 if (!*pszCmd)
156 rc = kmk_builtin_command_parsed(argc, argv);
157 else
158 rc = 1;
159
160 /* clean up and return. */
161 while (argc--)
162 free(argv[argc]);
163 free(argv);
164 return rc;
165}
166
167
168int kmk_builtin_command_parsed(int argc, char **argv)
169{
170 const char *pszCmd = argv[0];
171 int rc;
172
173 /*
174 * Check and skip the prefix.
175 */
176 if (strncmp(pszCmd, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
177 {
178 printf("kmk_builtin: Invalid command prefix '%s'!\n", pszCmd);
179 return 1;
180 }
181 pszCmd += sizeof("kmk_builtin_") - 1;
182
183 /*
184 * String switch on the command.
185 */
186 if (0) { }
187 //if (!strcmp(pszCmd, "cp"))
188 // rc = kmk_builtin_cp(argc, argv, environ);
189 //else if (!strcmp(pszCmd, "chmod"))
190 // rc = kmk_builtin_chmod(argc, argv, environ);
191 else if (!strcmp(pszCmd, "echo"))
192 rc = kmk_builtin_echo(argc, argv, environ);
193 else if (!strcmp(pszCmd, "mkdir"))
194 rc = kmk_builtin_mkdir(argc, argv, environ);
195 //else if (!strcmp(pszCmd, "mv"))
196 // rc = kmk_builtin_mv(argc, argv, environ);
197 else if (!strcmp(pszCmd, "rm"))
198 rc = kmk_builtin_rm(argc, argv, environ);
199 //else if (!strcmp(pszCmd, "rmdir"))
200 // rc = kmk_builtin_rmdir(argc, argv, environ);
201 else
202 {
203 printf("kmk_builtin: Unknown command '%s'!\n", pszCmd);
204 return 1;
205 }
206 return rc;
207}
208
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette