Question: 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

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\"" [{let's},{do},{some},{crazy "air quotes"}]

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

> cat -n /etc/fstab [{cat},{-n},{/etc/fstab}]

Step by Step Solution

3.44 Rating (151 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To solve this problem well walk through the implementation stepbystep on how you might construct a simple program that uses the GNU Readline library t... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Document Format (2 attachments)

PDF file Icon

609abc1ad126f_30932.pdf

180 KBs PDF File

Word file Icon

609abc1ad126f_30932.docx

120 KBs Word File

Students Have Also Explored These Related Programming Questions!