1. Engineering
  2. Computer Science
  3. you may use c or c for this you will...

Question: you may use c or c for this you will...

Question details

You may use C or C++ for this.

You will use the GNU readline library which reads a line of input from a user and allows editing of the line.

Print a command prompt “> “ and read a line of data using function readline (char * readline(char *prompt)). In general, more detail about library functions can be had by typing “man functionname” at a UNIX prompt. Take the string that is returned and from it create a linked list of tokens. Each token should be in a structure that has a character pointer to the token value and a next pointer that is either NULL or points to the next token. Your program will then print the contents of the linked Roch p. 2 list in square [ ] brackets. The token values will be surrounded by curly braces { } and separated by a comma.

The rules for tokens are as follows and are loosely based on rules used by many UNIX shells:

1. Whitespace (space, tab, etc) ends a token and is skipped unless it is in quotes (see below). You will find the function int isspace(char) useful for detecting whitespace characters.

2. Several characters are considered special tokens: | ; < > & When one of these characters is encountered, the token being built is completed and a new token consisting of the character is created.

3. The special character \ is an escape character. The next character will be part (or the start) of a token. For example, normally the string “now;is” would be three tokens: [{now},{;},{is}]. However, “now\;is” results in a single token: [{now;is}].

4. Items between single or double quotes are treated as parts of the current token: > me gusta UNIX ->  [{me},{gusta},{UNIX}] vs. > me" gusta UNIX" -> [{me gusta UNIX}]

5. Characters that do not meet the above rules are added on to the current token being built.

Examples:

> let\'s do some "crazy \"air quotes\""

results: [{let's},{do},{some},{crazy "air quotes"}]

> ls | sort >sorted_files.txt

results: [{ls},{|},{sort},{>},{sorted_files.txt}]

> cat -n /etc/fstab

results: [{cat},{-n},{/etc/fstab}]

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution