The_Glidd
The_Glidd

Reputation: 75

Parsing user input in c

I'm having trouble wrapping my mind around parsing user input in c. My task (homework) is to read user input, then parse in the same way BASH does, so delimiters are ' ', |, >, etc. My (wrong) solution so far uses strtok. I've been advised to use sscanf, but haven't been able to wrap my mind around how that will work for all cases of user input.

I'd love a strategy that will point me in the right direction. Here's what I have so far:

#include <stdio.h>
#include <unistd.h>
#include <string.h>

#define MAX_LINE 80


int main ()
{

    const char delim[]=" \\|\\>\\1>\\2>\\>>\\2>>\\&>\\<";

    char* args[MAX_LINE/2 + 1];
    char tok[MAX_LINE];
    char* token;

    printf("osh>");

    fgets(tok, sizeof(tok), stdin);

    token = strtok(tok,delim);

    while (token != NULL)
    {   
        printf("%s\n", token);

        token = strtok(NULL, delim);
    }            

    return 0;

}

Upvotes: 0

Views: 3093

Answers (2)

The_Glidd
The_Glidd

Reputation: 75

Thanks for the help on this. I ended up going a different route entirely, basically keeping track of the contents of each index of the fgets result, then parsing from there. I didn't end up using any c-ish methods (i.e. strtok) to do the job.

Here's a sample snippet.

        {
            //integers correspond to ASCII values
            LEN++;
            if ((line[i+1] == 60) || (line[i+1] == 62) || (line[i+1] == 124) || (line[i+1] == 38) || (line[i+1] == 32) || (line[i+1] == 10))
            {
                memcpy(substring, &line[string_start], LEN);
                substring[LEN] = '\0';
                args[token_number] = malloc(strlen(substring) + 1);
                strcpy(args[token_number], substring);

                token_number++;
                string_start = i+1;
                LEN = 0;  
            }

            i++;
        }

Upvotes: 0

John Yost
John Yost

Reputation: 693

Method 1) You can use pointer arithmetic to locate the delimiter while still using strtok to extract the delimited strings. This seems to me the easiest solution but requires pointer arithmetic. Be sure you don't try to access 'tok' beyond the end of the array or before the array (by over-decrementing the pointer).

Example:

token = strtok(tok, delim);
char verb = *token--;

Method 2) You could use sscanf in the same manner looking for strings, then single characters, then strings... and so forth till you hit the end of the line.

For either method you need to store the strings and delimiters somewhere and maintain the order so you can reconstruct the sequence.

Good luck.

Upvotes: 2

Related Questions