os-k/kaleid/kernel/sh/argv.c

191 lines
6.3 KiB
C
Raw Normal View History

2019-01-21 15:00:04 +01:00
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Command line parsing utilities //
2019-02-16 23:36:33 +01:00
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
2019-01-21 15:00:04 +01:00
//----------------------------------------------------------------------------//
2019-05-29 14:34:37 +02:00
#include <sh/argv.h>
2019-05-13 23:22:27 +02:00
#include <ex/malloc.h>
2019-02-06 14:07:38 +01:00
//
// Converts command line to an argument vector
//
2019-05-18 19:40:24 +02:00
// This function assumes that *argv is a (ARG_MAX * 2)-wide buffer,
// whose second half will be filled with strings in succession;
// with the address of the nth string stored in the first half,
// specifically at argv[n-1]
2019-02-06 14:07:38 +01:00
//
2019-05-29 14:34:37 +02:00
error_t ShCmdLineToArgVecEx(const char *cmdLine,
int *argcPtr,
char *bufptr,
bool doEscaping)
2019-02-06 14:07:38 +01:00
{
2019-05-18 19:09:46 +02:00
int argc = 0;
2019-02-06 14:07:38 +01:00
char quotes = 0;
bool started = false;
bool escaping = false;
size_t written = 0;
error_t retcode = EOK;
2019-05-18 19:40:24 +02:00
bool maxreached = false;
2019-02-06 14:07:38 +01:00
// An ARG_MAX-wide buffer
2019-05-18 19:40:24 +02:00
char **argv = (char **)bufptr;
assert(argv && cmdLine);
// Another ARG_MAX-wide buffer
2019-05-18 22:53:57 +02:00
char *buffer = bufptr + ARG_MAX;
2019-05-18 19:40:24 +02:00
argv[0] = buffer;
2019-02-06 14:07:38 +01:00
// Null-terminate current argv slot
// and save the start of next string
// Macro'd to avoid copypasting code
#define NULLTERM_AND_SAVE \
*buffer = 0; \
2019-05-18 19:09:46 +02:00
argv[++argc] = ++buffer; \
2019-02-06 14:07:38 +01:00
written += sizeof(char *) + 1; \
// Is character a blank character?
// To be replaced by ctype stuff once
// that's implemented
#define ISBLANK(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
// Both " and ' are valid quotes chars
// XXX aren't there more?
#define ISQUOTE(c) ((c) == '\'' || (c) == '"')
// Go through the command line
2019-05-18 19:09:46 +02:00
for (; *cmdLine; cmdLine++) {
2019-02-06 14:07:38 +01:00
// Make sure we don't go beyond ARG_MAX bytes
2019-05-18 19:40:24 +02:00
maxreached = written >= ARG_MAX - 1;
maxreached = maxreached || argc >= (int)(ARG_MAX/sizeof(char *) - 1);
if (maxreached) {
// Sanity check
2019-05-18 19:40:24 +02:00
assert(written == ARG_MAX - 1);
2019-02-06 14:07:38 +01:00
2019-05-18 19:40:24 +02:00
// All we have left is one byte for the null-terminator
// (or argv maxed out)
2019-02-06 14:07:38 +01:00
*buffer = 0;
// Did we write anything in this slot?
if (started) {
argc++;
}
// We're done, get out of here
retcode = ENOMEM;
break;
}
// Switch to next argv slot
if (ISBLANK(*cmdLine) && !quotes && !escaping) {
// Has slot even started?
if (started) {
started = false;
NULLTERM_AND_SAVE;
}
continue;
}
// Escaping next character
if (*cmdLine == '\\' && !escaping && doEscaping) {
escaping = true;
continue;
}
// Deal with escape sequences
if (escaping) {
if (*cmdLine == 'n') *buffer++ = '\n';
else if (*cmdLine == 'r') *buffer++ = '\r';
else if (*cmdLine == 't') *buffer++ = '\t';
else if (*cmdLine == 'f') *buffer++ = '\f';
else if (*cmdLine == 'v') *buffer++ = '\v';
written++;
started = true;
escaping = false;
continue;
}
// Deal with quotes
if (ISQUOTE(*cmdLine) && !escaping) {
// Quoted text always fills a whole slot
// Note that this is the only way an empty
// string can be put in a slot
if (!quotes && !started) {
quotes = *cmdLine;
started = true;
continue;
}
// End a quote block
if (quotes == *cmdLine && ISBLANK(cmdLine[1])) {
quotes = 0;
started = false;
NULLTERM_AND_SAVE;
continue;
}
// Quotes were either preceeded by unquoted non-blank text
// or couldn't close quoted text block because succeeded
// by text; we consider this " to be escaped and fall through
}
// Just a regular character, or it is being escaped
written++;
started = true;
escaping = false;
*buffer++ = *cmdLine;
}
2019-05-18 19:40:24 +02:00
// Ensures that the last string is null-terminated
*buffer = 0;
2019-02-06 14:07:38 +01:00
// Ensures that argv[argc] == NULL
argv[++argc] = NULL;
// Update *argcPtr, but we don't mind if
// NULL was passed for argcPtr
if (argcPtr) {
*argcPtr = argc;
}
return retcode;
}
#undef ISQUOTE
#undef ISBLANK
#undef NULLTERM_AND_SAVE
2019-05-29 14:34:37 +02:00
error_t ShCmdLineToArgVec(const char *cmdLine,
int *argcPtr,
char *bufptr)
2019-02-06 14:07:38 +01:00
{
2019-05-29 14:34:37 +02:00
return ShCmdLineToArgVecEx(cmdLine, argcPtr, bufptr, false);
2019-02-06 14:07:38 +01:00
}