1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/*
* include/lexer.h
*
* HUSH lexical analysis module
* CISC 301 -- Operating Systems, Project 2
*
* Copyright (C) 2025 Douglas B. Rumbaugh <dbrumbaugh@harrisburgu.edu>
*
* Distributed under the Modified BSD License
*
*/
#ifndef H_HUSH_LEXER
#define H_HUSH_LEXER
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
enum token_type {
TKN_COMMAND,
TKN_OUT_REDIR,
TKN_IN_REDIR,
TKN_PIPE,
TKN_FILENAME,
TKN_ARG,
TKN_INVALID,
TKN_VARKEY,
TKN_VARVAL
};
typedef struct token {
char *text;
enum token_type type;
struct token *next;
} token;
/*
* Accepts a null-terminate string representing
* a full command and returns the pointer to the
* head of a list of parsed tokens. The provided
* string must have been trimmed to remove all
* leading and trailing whitespace characters.
*
* The tokens contain pointers into the provided
* command string, and so the parsed command is
* only valid so long as the provided string has
* not been altered.
*
* This function's behavior is undefined if the
* input string is *not* null-terminated, or if
* it has leading or trailing whitespace.
*/
token *parse_command(char *cmdstr);
/*
* Accepts the pointer to the head of a list
* of parsed tokens and validates that the
* tokens are syntactically valid. If the
* sequence is valid, NULL will be returned.
* If the sequence is invalid, a pointer to
* the first invalid token will be returned.
*/
token *validate_command(token *cmd);
/*
* Removes all leading and trailing whitespace from
* the provided string, and returns a pointer to the
* beginning of the trimmed string. Necessary as the
* first stage of parsing.
*/
char *trim(char *str);
void print_parsed_command(FILE *file, token *cmd);
void destroy_tokens(token *tokens);
#endif
|