Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #define BUF_SIZE 1024
- #define MAX_ARGS 32
- char* readline(char* buf, size_t size) {
- int length;
- if (fgets(buf, BUF_SIZE, stdin) != NULL) {
- length = strlen(buf);
- while (buf[length - 1] == '\n' && length != 0) {
- buf[length - 1] = '\0';
- length -= 1;
- }
- // ignoring whitespace check for now
- if (length == 0) {
- return NULL;
- }
- } else {
- return NULL;
- }
- return buf;
- }
- bool speechmarkArg(char* str, char** args, int* index, int* argCount) {
- static const char key[] = {"\""};
- int length = strlen(str);
- // increment index to point to the first character of the argument
- *index += 1;
- // get the length of the argument
- int argLength = strcspn(str + (*index), key);
- // no closing speechmark was found, return an error
- if ((*index + argLength) == length) {
- return false;
- }
- // store the new argument in the array of arguments
- args[*argCount] = malloc((argLength + 1) * sizeof(char));
- memcpy(args[*argCount], str + (*index), argLength);
- strcat(args[*argCount], "\0");
- // update index to point to the closing speechmark. Upon returning
- // to the splut function, the for loop will immediately cycle past
- *argCount += 1;
- *index += argLength;
- // return true on success
- return true;
- }
- bool whitespaceArg(char* str, char** args, int* index, int* argCount) {
- static const char key[] = {"\" "};
- char delim;
- // get the length of the argument
- int argLength = strcspn(str + (*index), key);
- // store the delimiting character
- delim = str[*index + argLength];
- // store the new argument in the array of arguments
- args[*argCount] = malloc((argLength + 1) * sizeof(char));
- memcpy(args[*argCount], str + (*index), argLength);
- strcat(args[*argCount], "\0");
- // update the index and argcount
- *index += argLength;
- *argCount += 1;
- // either return to the split function immediatley or
- // create add another argument based on the delimiting
- // character which was found
- if (delim == ' ' || delim == '\0') {
- return true;
- } else {
- if (!speechmarkArg(str, args, index, argCount)) {
- return false;
- }
- }
- // return true on success
- return true;
- }
- void split(char* buf, char** args, size_t max) {
- int length = strlen(buf);
- int argCount = 0;
- for (int i = 0; i < length; i += 1) {
- if (buf[i] == ' ') {
- continue;
- } else if (buf[i] == '\"') {
- if (!speechmarkArg(buf, args, &i, &argCount)) {
- puts("error: expected closing quotation mark");
- break;
- }
- } else {
- if (!whitespaceArg(buf, args, &i, &argCount)) {
- puts("error: expected closing quotation mark");
- break;
- }
- }
- }
- args[argCount] = NULL;
- }
- int main() {
- char buffer[BUF_SIZE];
- char** args = malloc(MAX_ARGS * sizeof(char*));
- while (true) {
- if (readline(buffer, BUF_SIZE) != NULL) {
- split(buffer, args, MAX_ARGS);
- }
- for(int i = 0;; i += 1) {
- if (args[i] == NULL) {
- break;
- }
- puts(args[i]);
- }
- }
- free (args);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement