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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# expr — Evaluate Expressions
## Overview
`expr` evaluates arithmetic, string, and logical expressions from the command
line and writes the result to standard output. It implements a recursive
descent parser with automatic type coercion between strings, numeric strings,
and integers.
**Source**: `expr/expr.c` (single file)
**Origin**: BSD 4.4, University of California, Berkeley
**License**: BSD-3-Clause
## Synopsis
```
expr expression
```
## Source Analysis
### Value Types
```c
enum value_type {
INTEGER, /* Pure integer (from arithmetic) */
NUMERIC_STRING, /* String that looks like a number */
STRING, /* General string */
};
struct value {
enum value_type type;
union {
intmax_t ival;
char *sval;
};
};
```
`expr` automatically coerces between types during operations. A value
like `"42"` starts as `NUMERIC_STRING` and is promoted to `INTEGER` for
arithmetic.
### Parser Architecture
`expr` uses a recursive descent parser with operator precedence:
```
parse_expr()
└── parse_or() /* | operator (lowest precedence) */
└── parse_and() /* & operator */
└── parse_compare() /* =, !=, <, >, <=, >= */
└── parse_add() /* +, - */
└── parse_mul() /* *, /, % */
└── parse_primary() /* atoms, ( expr ), : regex */
```
### Operators
#### Arithmetic Operators
| Operator | Description | Example |
|----------|-------------|---------|
| `+` | Addition | `expr 2 + 3` → `5` |
| `-` | Subtraction | `expr 5 - 2` → `3` |
| `*` | Multiplication | `expr 4 \* 3` → `12` |
| `/` | Integer division | `expr 10 / 3` → `3` |
| `%` | Modulo | `expr 10 % 3` → `1` |
#### Comparison Operators
| Operator | Description | Example |
|----------|-------------|---------|
| `=` | Equal | `expr abc = abc` → `1` |
| `!=` | Not equal | `expr abc != def` → `1` |
| `<` | Less than | `expr 1 \< 2` → `1` |
| `>` | Greater than | `expr 2 \> 1` → `1` |
| `<=` | Less or equal | `expr 1 \<= 1` → `1` |
| `>=` | Greater or equal | `expr 2 \>= 1` → `1` |
Comparisons between numeric strings use numeric ordering; otherwise
locale-aware string comparison (`strcoll`) is used.
#### Logical Operators
| Operator | Description | Example |
|----------|-------------|---------|
| `\|` | OR (short-circuit) | `expr 0 \| 5` → `5` |
| `&` | AND (short-circuit) | `expr 1 \& 2` → `1` |
#### String/Regex Operators
| Operator | Description | Example |
|----------|-------------|---------|
| `:` | Regex match | `expr hello : 'hel\(.*\)'` → `lo` |
| `match` | Same as `:` | `expr match hello 'h.*'` |
| `substr` | Substring | `expr substr hello 2 3` → `ell` |
| `index` | Character position | `expr index hello l` → `3` |
| `length` | String length | `expr length hello` → `5` |
### Regex Matching
The `:` operator uses POSIX basic regular expressions (`regcomp` with
`REG_NOSUB` or group capture):
```c
/* expr STRING : REGEX */
/* Returns captured \(...\) group or match length */
```
If the regex contains `\(...\)`, the captured substring is returned.
Otherwise, the length of the match is returned.
### Overflow Checking
All arithmetic operations check for integer overflow:
```c
static intmax_t
safe_add(intmax_t a, intmax_t b)
{
if ((b > 0 && a > INTMAX_MAX - b) ||
(b < 0 && a < INTMAX_MIN - b))
errx(2, "integer overflow");
return a + b;
}
```
### Locale Awareness
String comparisons use `strcoll(3)` for locale-correct ordering:
```c
/* Compare as strings using locale collation */
result = strcoll(left->sval, right->sval);
```
## System Calls Used
| Syscall | Purpose |
|---------|---------|
| `regcomp(3)` / `regexec(3)` | Regular expression matching |
| `strcoll(3)` | Locale-aware string comparison |
## Examples
```sh
# Arithmetic
expr 2 + 3 # → 5
expr 10 / 3 # → 3
expr 7 % 4 # → 3
# String length
expr length "hello" # → 5
# Regex match (capture group)
expr "hello-world" : 'hello-\(.*\)' # → world
# Regex match (length)
expr "hello" : '.*' # → 5
# Substring
expr substr "hello" 2 3 # → ell
# Index (first occurrence)
expr index "hello" "lo" # → 3
# Comparisons
expr 42 = 42 # → 1
expr abc \< def # → 1
# Logical OR (returns first non-zero/non-empty)
expr 0 \| 5 # → 5
expr "" \| alt # → alt
# In shell scripts
count=$(expr $count + 1)
```
## Exit Codes
| Code | Meaning |
|------|---------|
| 0 | Expression is neither null nor zero |
| 1 | Expression is null or zero |
| 2 | Expression is invalid |
| 3 | Internal error |
## Differences from GNU expr
- No `--help` or `--version`
- Identical POSIX semantics for `:` operator
- Locale-aware string comparison by default
- Overflow results in error, not wraparound
|