| 1 | /* $Id: kmkbuiltin.c 613 2006-11-26 06:19:04Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * kMk Builtin command execution.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2005 knut st. osmundsen <bird@innotek.de>
|
|---|
| 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 | #ifdef _MSC_VER
|
|---|
| 32 | # include <io.h>
|
|---|
| 33 | #endif
|
|---|
| 34 | #include "kmkbuiltin/err.h"
|
|---|
| 35 | #include "kmkbuiltin.h"
|
|---|
| 36 |
|
|---|
| 37 | extern char **environ;
|
|---|
| 38 |
|
|---|
| 39 | int kmk_builtin_command(const char *pszCmd)
|
|---|
| 40 | {
|
|---|
| 41 | int argc;
|
|---|
| 42 | char **argv;
|
|---|
| 43 | int rc;
|
|---|
| 44 |
|
|---|
| 45 | /*
|
|---|
| 46 | * Check and skip the prefix.
|
|---|
| 47 | */
|
|---|
| 48 | if (strncmp(pszCmd, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
|
|---|
| 49 | {
|
|---|
| 50 | printf("kmk_builtin: Invalid command prefix '%s'!\n", pszCmd);
|
|---|
| 51 | return 1;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /*
|
|---|
| 55 | * Parse arguments.
|
|---|
| 56 | */
|
|---|
| 57 | argc = 0;
|
|---|
| 58 | argv = NULL;
|
|---|
| 59 | while (*pszCmd)
|
|---|
| 60 | {
|
|---|
| 61 | const char *pszEnd;
|
|---|
| 62 | const char *pszNext;
|
|---|
| 63 | int fEscaped = 0;
|
|---|
| 64 | size_t cch;
|
|---|
| 65 |
|
|---|
| 66 | /*
|
|---|
| 67 | * Find start and end of the current command.
|
|---|
| 68 | */
|
|---|
| 69 | if (*pszCmd == '"' || *pszCmd == '\'')
|
|---|
| 70 | {
|
|---|
| 71 | pszEnd = pszCmd;
|
|---|
| 72 | for (;;)
|
|---|
| 73 | {
|
|---|
| 74 | pszEnd = strchr(pszEnd + 1, *pszCmd);
|
|---|
| 75 | if (!pszEnd)
|
|---|
| 76 | {
|
|---|
| 77 | printf("kmk_builtin: Unbalanced quote in argument %d: %s\n", argc + 1, pszCmd);
|
|---|
| 78 | while (argc--)
|
|---|
| 79 | free(argv[argc]);
|
|---|
| 80 | free(argv);
|
|---|
| 81 | return 1;
|
|---|
| 82 | }
|
|---|
| 83 | /* two quotes -> escaped quote. */
|
|---|
| 84 | if (pszEnd[0] != pszEnd[1])
|
|---|
| 85 | break;
|
|---|
| 86 | fEscaped = 1;
|
|---|
| 87 | }
|
|---|
| 88 | pszNext = pszEnd + 1;
|
|---|
| 89 | pszCmd++;
|
|---|
| 90 | }
|
|---|
| 91 | else
|
|---|
| 92 | {
|
|---|
| 93 | pszEnd = pszCmd;
|
|---|
| 94 | while (!isspace(*pszEnd) && *pszEnd)
|
|---|
| 95 | pszEnd++;
|
|---|
| 96 | pszNext = pszEnd;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /*
|
|---|
| 100 | * Make argument.
|
|---|
| 101 | */
|
|---|
| 102 | if (!(argc % 16))
|
|---|
| 103 | {
|
|---|
| 104 | void *pv = realloc(argv, sizeof(char *) * (argc + 17));
|
|---|
| 105 | if (!pv)
|
|---|
| 106 | {
|
|---|
| 107 | printf("kmk_builtin: out of memory. argc=%d\n", argc);
|
|---|
| 108 | break;
|
|---|
| 109 | }
|
|---|
| 110 | argv = (char **)pv;
|
|---|
| 111 | }
|
|---|
| 112 | cch = pszEnd - pszCmd;
|
|---|
| 113 | argv[argc] = malloc(cch + 1);
|
|---|
| 114 | if (!argv[argc])
|
|---|
| 115 | {
|
|---|
| 116 | printf("kmk_builtin: out of memory. argc=%d len=%d\n", argc, pszEnd - pszCmd + 1);
|
|---|
| 117 | break;
|
|---|
| 118 | }
|
|---|
| 119 | memcpy(argv[argc], pszCmd, cch);
|
|---|
| 120 | argv[argc][cch] = '\0';
|
|---|
| 121 |
|
|---|
| 122 | /* unescape quotes? */
|
|---|
| 123 | if (fEscaped)
|
|---|
| 124 | {
|
|---|
| 125 | char ch = pszCmd[-1];
|
|---|
| 126 | char *pszW = argv[argc];
|
|---|
| 127 | char *pszR = argv[argc];
|
|---|
| 128 | while (*pszR)
|
|---|
| 129 | {
|
|---|
| 130 | if (*pszR == ch)
|
|---|
| 131 | pszR++;
|
|---|
| 132 | *pszW++ = *pszR++;
|
|---|
| 133 | }
|
|---|
| 134 | *pszW = '\0';
|
|---|
| 135 | }
|
|---|
| 136 | /* commit it */
|
|---|
| 137 | argv[++argc] = NULL;
|
|---|
| 138 |
|
|---|
| 139 | /*
|
|---|
| 140 | * Next
|
|---|
| 141 | */
|
|---|
| 142 | pszCmd = pszNext;
|
|---|
| 143 | if (isspace(*pszCmd) && *pszCmd)
|
|---|
| 144 | pszCmd++;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /*
|
|---|
| 148 | * Execute the command if parsing was successful.
|
|---|
| 149 | */
|
|---|
| 150 | if (!*pszCmd)
|
|---|
| 151 | rc = kmk_builtin_command_parsed(argc, argv);
|
|---|
| 152 | else
|
|---|
| 153 | rc = 1;
|
|---|
| 154 |
|
|---|
| 155 | /* clean up and return. */
|
|---|
| 156 | while (argc--)
|
|---|
| 157 | free(argv[argc]);
|
|---|
| 158 | free(argv);
|
|---|
| 159 | return rc;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 | int kmk_builtin_command_parsed(int argc, char **argv)
|
|---|
| 164 | {
|
|---|
| 165 | const char *pszCmd = argv[0];
|
|---|
| 166 | int iumask;
|
|---|
| 167 | int rc;
|
|---|
| 168 |
|
|---|
| 169 | /*
|
|---|
| 170 | * Check and skip the prefix.
|
|---|
| 171 | */
|
|---|
| 172 | if (strncmp(pszCmd, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
|
|---|
| 173 | {
|
|---|
| 174 | printf("kmk_builtin: Invalid command prefix '%s'!\n", pszCmd);
|
|---|
| 175 | return 1;
|
|---|
| 176 | }
|
|---|
| 177 | pszCmd += sizeof("kmk_builtin_") - 1;
|
|---|
| 178 |
|
|---|
| 179 | /*
|
|---|
| 180 | * String switch on the command.
|
|---|
| 181 | */
|
|---|
| 182 | iumask = umask(0);
|
|---|
| 183 | umask(iumask);
|
|---|
| 184 | if (!strcmp(pszCmd, "append"))
|
|---|
| 185 | rc = kmk_builtin_append(argc, argv, environ);
|
|---|
| 186 | else if (!strcmp(pszCmd, "echo"))
|
|---|
| 187 | rc = kmk_builtin_echo(argc, argv, environ);
|
|---|
| 188 | else if (!strcmp(pszCmd, "install"))
|
|---|
| 189 | rc = kmk_builtin_install(argc, argv, environ);
|
|---|
| 190 | else if (!strcmp(pszCmd, "ln"))
|
|---|
| 191 | rc = kmk_builtin_ln(argc, argv, environ);
|
|---|
| 192 | else if (!strcmp(pszCmd, "mkdir"))
|
|---|
| 193 | rc = kmk_builtin_mkdir(argc, argv, environ);
|
|---|
| 194 | else if (!strcmp(pszCmd, "mv"))
|
|---|
| 195 | rc = kmk_builtin_mv(argc, argv, environ);
|
|---|
| 196 | else if (!strcmp(pszCmd, "rm"))
|
|---|
| 197 | rc = kmk_builtin_rm(argc, argv, environ);
|
|---|
| 198 | else if (!strcmp(pszCmd, "rmdir"))
|
|---|
| 199 | rc = kmk_builtin_rmdir(argc, argv, environ);
|
|---|
| 200 | /* rarely used commands: */
|
|---|
| 201 | else if (!strcmp(pszCmd, "cat"))
|
|---|
| 202 | rc = kmk_builtin_cat(argc, argv, environ);
|
|---|
| 203 | else if (!strcmp(pszCmd, "cp"))
|
|---|
| 204 | rc = kmk_builtin_cp(argc, argv, environ);
|
|---|
| 205 | else
|
|---|
| 206 | {
|
|---|
| 207 | printf("kmk_builtin: Unknown command '%s'!\n", pszCmd);
|
|---|
| 208 | return 1;
|
|---|
| 209 | }
|
|---|
| 210 | g_progname = "kmk"; /* paranoia, make sure it's not pointing at a freed argv[0]. */
|
|---|
| 211 | umask(iumask);
|
|---|
| 212 | return rc;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|