aboutsummaryrefslogtreecommitdiff
path: root/wordsplit.3
blob: 4f86f1a3cce55f989706ac2cf650a9203890d593 (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
.\" This file is part of wordsplit -*- nroff -*-
.\" Copyright (C) 2009-2021 Sergey Poznyakoff
.\"
.\" Wordsplit is free software; you can redistribute it and/or modify
.\" it under the terms of the GNU General Public License as published by
.\" the Free Software Foundation; either version 3, or (at your option)
.\" any later version.
.\"
.\" Wordsplit is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public License
.\" along with wordsplit.  If not, see <http://www.gnu.org/licenses/>.
.\"
.TH WORDSPLIT 3 "June 22, 2023" "WORDSPLIT" "Wordsplit User Reference"
.SH NAME
wordsplit \- split string into words 
.SH SYNOPSIS
.B #include <wordsplit.h>
.sp
\fBint wordsplit (const char *\fIs\fB,\
 wordsplit_t *\fIws\fB, int \fIflags\fB);\fR
.sp
\fBint wordsplit_len (const char *\fIs\fB,\
 \fBsize_t \fIlen\fR,\
 \fBwordsplit_t *\fIp\fB,\
 int \fIflags\fB);
.sp 
\fBvoid wordsplit_free (wordsplit_t *\fIp\fB);\fR
.sp
\fBvoid wordsplit_free_words (wordsplit_t *\fIws\fB);\fR
.sp
\fBvoid wordsplit_getwords (wordsplit_t *\fIws\fB,\
 int *\fIwordc\fB, char ***\fIwordv\fB);
.sp
\fBvoid wordsplit_perror (wordsplit_t *\fIws\fB);\fR
.sp
\fBconst char *wordsplit_strerror (wordsplit_t *\fIws\fB);\fR
.sp
\fBvoid wordsplit_clearerr (wordsplit_t *\fIws\fB);\fR
.SH DESCRIPTION
The function \fBwordsplit\fR splits the string \fIs\fR into words
using a set of rules governed by \fIflags\fR.  Depending on
\fIflags\fR, the function performs the following operations:
whitespace trimming, tilde expansion, variable expansion, quote
removal, command substitution, and path expansion.  On success,
\fBwordsplit\fR returns 0 and stores the words found in the member
\fBws_wordv\fR and the number of words in the member \fBws_wordc\fR.
On error, a non-zero error code is returned.
.PP
The function \fBwordsplit_len\fR acts similarly, except that it
accesses only first \fBlen\fR bytes of the string \fIs\fR, which is
not required to be null-terminated.
.PP
When no longer needed, the resources allocated by a call to one of
these functions must be freed using
.BR wordsplit_free .
.PP
The function
.B wordsplit_free_words
frees only the memory allocated for elements of
.I ws_wordv
after which it resets
.I ws_wordv to
.B NULL
and 
.I ws_wordc
to zero.
.PP
The usual calling sequence is:
.PP
.EX
wordsplit_t ws;
int rc;

if (wordsplit(s, &ws, WRDSF_DEFFLAGS)) {
    for (i = 0; i < ws.ws_wordc; i++) {
        /* do something with ws.ws_wordv[i] */
    }
}
wordsplit_free(&ws);
.EE
.PP
Notice, that \fBwordsplit_free\fR must be called after each invocation
of \fBwordsplit\fR or \fBwordsplit_len\fR, even if it resulted in
error.
.PP
The function
.B wordsplit_getwords
returns in \fIwordv\fR an array of words, and in \fIwordc\fR the number
of elements in \fIwordv\fR.  The array can be used after calling
.BR wordsplit_free .
The caller becomes responsible for freeing the memory allocated for
each element of the array and the array pointer itself.
.PP
The function
.B wordsplit_perror
prints error message from the last invocation of \fBwordsplit\fR.  It
uses the function pointed to by the
.I ws_error
member.  By default, it outputs the message on the standard error.
.PP
For more sophisticated error reporting, the function
.B wordsplit_strerror
can be used.  It returns a pointer to the string describing the error.
The caller should treat this pointer as a constant string.  It should
not try to alter or deallocate it.
.PP
The function
.B wordsplit_clearerr
clears the error condition associated with \fIws\fR.
.SH INCREMENTAL MODE
In incremental mode \fBwordsplit\fR parses one word per invocation.
It returns \fBWRDSF_OK\fR on success and \fBWRDSF_NOINPUT\fR when 
entire input string has been processed.
.PP
This mode is enabled if the flag \fBWRDSF_INCREMENTAL\fR is set in
the \fIflags\fR argument.  Subsequent calls to \fBwordsplit\fR must
have \fBNULL\fR as first argument.  Each successful
call will return exactly one word in \fBws.ws_wordv[0]\fR.
.PP
An example usage:
.PP
.EX
wordsplit_t ws;
int rc;
flags = WRDSF_DEFFLAGS|WRDSF_INCREMENTAL;

for (rc = wordsplit(s, &ws, flags); rc == WRDSF_OK;
     rc = wordsplit(NULL, &ws, flags)) {
     process(ws.ws_wordv[0]);
}     

if (rc != WRDSE_NOINPUT)
     wordsplit_perror(&ws);

wordsplit_free(&ws);
.EE
.SH OPTIONS
The number of flags is limited to 32 (the width of \fBuint32_t\fR data
type).  By the time of this writing each bit is already occupied by a
corresponding flag. However, the number of features \fBwordsplit\fR
provides requires still more. Additional features can be requested by
setting a corresponding \fIoption bit\fR in the \fBws_option\fR field
of the \fBstruct wordsplit\fR argument. To inform wordsplit functions
that this field is initialized the \fBWRDSF_OPTIONS\fR flag must be set.
.PP
Option symbolic names begin with \fBWRDSO_\fR. They are discussed in
detail in the subsequent chapters.
.SH EXPANSION
Expansion is performed on the input after it has been split into
words. The kinds of expansion to be performed are controlled by the
appropriate bits set in the \fIflags\fR argument.  Whatever expansion
kinds are enabled, they are always run in the order described in this
section.
.SS Whitespace trimming
Whitespace trimming removes any leading and trailing whitespace from
the initial word array.  It is enabled by the
.B WRDSF_WS
flag.  Whitespace trimming is enabled automatically if the word
delimiters (\fIws_delim\fR member) contain whitespace characters
(\fB\(dq \\t\\n\(dq\fR), which is the default.
.SS Variable expansion
Variable expansion replaces each occurrence of
.BI $ NAME
or
.BI ${ NAME }
with the value of the variable \fINAME\fR.  It is enabled by default
and can be disabled by setting the \fBWRDSF_NOVAR\fR flag.  The caller
is responsible for supplying the table of available variables.  Two
mechanisms are provided: environment array and a callback function.
.PP
Environment array is a \fBNULL\fR-terminated array of variables,
stored in the \fIws_env\fR member.  The \fBWRDSF_ENV\fR flag must be
set in order to instruct \fBwordsplit\fR to use this array.
.PP
By default, elements of the \fIws_env\fR array have the form
.IR NAME = VALUE .
An alternative format is enabled by the
.B WRDSF_ENV_KV
flag.  When it is set, each variable is described by two consecutive
elements in the array:
.IR ws_env [ n ]
containing the variable name, and
.IR ws_env [ "n+1" ]
containing its value. If the latter is \fBNULL\fR, the corresponding
variable is undefined.
.PP
More sophisticated variable tables can be implemented using 
callback function.  The \fIws_getvar\fR member should be set to point
to that function and \fBWRDSF_GETVAR\fR flag must be set.  The
function itself shall be defined as
.PP
.EX
int getvar (char **ret, const char *var, size_t len, void *clos);
.EE
.PP
The function shall look up the variable identified by the first
\fIlen\fR bytes of the string \fIvar\fR.  If the variable is found,
the function shall store a copy of its value (allocated using
\fBmalloc\fR(3)) in the memory location pointed to by \fBret\fR, and
return \fBWRDSE_OK\fR.  If the variable is not found, the function shall
return \fBWRDSE_UNDEF\fR.  Otherwise, a non-zero error code shall be
returned.
.PP
If \fIws_getvar\fR returns
.BR WRDSE_USERERR ,
it must store the pointer to the error description string in
.BR *ret .
In any case (whether returning \fB0\fR or \fBWRDSE_USERERR\fR), the
data returned in \fBret\fR must be allocated using
.BR malloc (3).
.PP
If both
.I ws_env
and
.I ws_getvar
are used, the variable is first looked up in
.IR ws_env .
If it is not found there, the 
.I ws_getvar
callback is invoked.
This order is reverted if the \fBWRDSO_GETVARPREF\fR option is set.
.PP
During variable expansion, the forms below cause
.B wordsplit
to test for a variable that is unset or null.  Omitting the
colon results in a test only for a variable that is unset.
.TP
.BI ${ variable :- word }
.BR "Use Default Values" .
If \fIvariable\fR is unset or null, the expansion of \fIword\fR is substituted.
Otherwise, the value of \fIvariable\fR is substituted.
.TP
.BI ${ variable := word }
.BR "Assign Default Values" .
If \fIvariable\fR is unset or null, the expansion of \fIword\fR is
assigned to \fIvariable\fR.  The value of \fIvariable\fR is then substituted.
.TP
.BI ${ variable :? word }
.BR "Display Error if Null or Unset" .
If \fIvariable\fR is null or unset, the expansion of \fIword\fR (or a
message to that effect if word is not present) is output using
.IR ws_error .
Otherwise, the value of \fIvariable\fR is substituted.
.TP
.BI ${ variable :+ word }
.BR "Use Alternate Value" .
If \fIvariable\fR is null or unset, nothing is substituted, otherwise the
expansion of \fIword\fR is substituted.
.PP
Unless the above forms are used, a reference to an undefined variable
expands to empty string.  Three flags affect this behavior.  If the
\fBWRDSF_UNDEF\fR flag is set, expanding undefined variable triggers
a \fBWRDSE_UNDEF\fR error.  If the \fBWRDSF_WARNUNDEF\fR flag is set,
a non-fatal warning is emitted for each undefined variable.  Finally,
if the \fBWRDSF_KEEPUNDEF\fR flag is set, references to undefined
variables are left unexpanded.
.PP
If two or three of these flags are set simultaneously, the behavior is
undefined.
.SS Positional argument expansion
\fIPositional arguments\fR are special parameters that can be
referenced in the input string by their ordinal number.  The numbering
begins at \fB0\fR.  The syntax for referencing positional arguments is
the same as for the variables, except that argument index is used
instead of the variable name.  If the index is between 0 and 9, the
\fB$\fIN\fR form is acceptable.  Otherwise, the index must be enclosed
in curly braces: \fB${\fIN\fB}\fR.
.PP
During argument expansion, references to positional arguments are
replaced with the corresponding values.
.PP
Argument expansion is requested by the \fBWRDSO_PARAMV\fR option bit.
The NULL-terminated array of variables shall be supplied in the
.I ws_paramv
member.  The
.I ws_paramc
member shall be initialized to the number of elements in
.IR ws_paramv .
.PP
Setting the \fBWRDSO_PARAM_NEGIDX\fR option together with
\fBWRDSO_PARAMV\fR enables negative positional argument references.
A negative reference has the form \fB${-\fIN\fB}\fR.  It is expanded
to the value of the argument with index \fB\fIws_paramc\fR \- \fIN\fR.
.SS Quote removal
During quote removal, single or double quotes surrounding a sequence
of characters are removed and the sequence itself is treated as a
single word.  Characters within single quotes are treated verbatim.
Characters within double quotes undergo variable expansion and
backslash interpretation (see below).
.PP
Recognition of single quoted strings is enabled by the
\fBWRDSF_SQUOTE\fR flag.  Recognition of double quotes is enabled by
the \fBWRDSF_DQUOTE\fR flag.  The macro \fBWRDSF_QUOTE\fR enables both.
.SS Backslash interpretation
Backslash interpretation translates unquoted
.I escape sequences
into corresponding characters.  An escape sequence is a backslash followed
by one or more characters.  By default, that is if no flags are
supplied, no escape sequences are defined, and each sequence
\fB\\\fIC\fR is reproduced verbatim.
.PP
There are several ways to enable backslash interpretation and to
define escape sequences.  The simplest one is to use the
\fBWRDSF_CESCAPES\fR flag.  This flag defines the C-like escape
sequences:
.PP
.nf
.ta 8n 18n 42n
.ul
	Sequence	Expansion	ASCII
	\fB\\\\\fR	\fB\\\fR	134
	\fB\\\(dq\fR	\fB\(dq\fR	042
	\fB\\a\fR	audible bell	007	
	\fB\\b\fR	backspace	010
	\fB\\f\fR	form-feed	014
	\fB\\n\fR	new line	012
	\fB\\r\fR	charriage return	015
	\fB\\t\fR	horizontal tabulation	011
	\fB\\v\fR	vertical tabulation	013
.fi
.sp
The sequence \fB\\x\fINN\fR or \fB\\X\fINN\fR, where \fINN\fR stands
for a two-digit hex number is replaced with ASCII character \fINN\fR.
The sequence \fB\\0\fINNN\fR, where \fINNN\fR stands for a three-digit
octal number is replaced with ASCII character whose code is \fINNN\fR.
.PP
Additionally, outside of quoted strings (if these are enabled by the
use of \fBWRDSF_DQUOTE\fR flag) backslash character can be used to
escape horizontal whitespace: horizontal space (ASCII 32) and
tab (ASCII 9) characters.
.PP
The \fBWRDSF_CESCAPES\fR bit is included in the default flag
set \fBWRDSF_DEFFLAGS\fR.
.PP
The \fBWRDSF_ESCAPE\fR flag provides a more elaborate way of defining
escape sequences.  If it is set, the \fBws_escape\fR member must be
initialized.  This member provides escape tables for unquoted words
(\fBws_escape[WRDSX_WORD]\fR) and quoted strings
(\fBws_escape[WRDSX_QUOTE]\fR).  Each table is a string consisting of
an even number of characters.  In each pair of characters, the first
one is a character that can appear after backslash, and the following
one is its translation.  For example, the table of C escapes is
represented as follows:
.TP
\fB\(dq\\\\\\\\"\\"a\\ab\\bf\\fn\\nr\\rt\\tv\\v\(dq\fR
.PP
It is valid to initialize \fBws_escape\fR elements to NULL.  In this
case, no backslash translation occurs.
.PP
For convenience, the global variable
.B wordsplit_escape
defines several most often used escape translation tables:
.PP
.EX
extern char const *wordsplit_escape[];
.EE
.PP
It is indexed by the following constants:
.TP
.B WS_ESC_C
C-style escapes, the definition of which is shown above.  This is the
translation table that is used within quoted strings when
.B WRDSF_CESCAPES
is in effect.
.TP
.B WS_ESC_C_WS
The \fBWS_ESC_C\fR table augmented by two entries: for horizontal tab
character and whitespace.  This is the table that is used for unquoted
words when
.B WRDSF_CESCAPES
is in effect.
.TP
.B WS_ESC_DQ
Backslash character escapes double-quote and itself.  Useful for
handling doubly-quoted strings in various Internet protocols.
.TP
.B WS_ESC_DQ_WS
Escape double-quote, backslash, horizontal tab and whitespace characters.
.PP
Interpretation of octal and hex escapes is controlled by the following
bits in \fBws_options\fR:
.TP
.B WRDSO_BSKEEP_WORD
When an unrecognized escape sequence is encountered in a word,
preserve it on output.  If that bit is not set, the backslash is
removed from such sequences.
.TP
.B WRDSO_OESC_WORD
Handle octal escapes in words.
.TP
.B WRDSO_XESC_WORD
Handle hex escapes in words.
.TP
.B WRDSO_BSKEEP_QUOTE
When an unrecognized escape sequence is encountered in a doubly-quoted
string, preserve it on output.  If that bit is not set, the backslash is
removed from such sequences.
.TP
.B WRDSO_OESC_QUOTE
Handle octal escapes in doubly-quoted strings.
.TP
.B WRDSO_XESC_QUOTE
Handle hex escapes in doubly-quoted strings.
.SS Command substitution
During \fIcommand substitution\fR, each word is scanned for commands.
Each command found is executed and replaced by the output it creates.
.PP
The syntax is:
.PP
.RS +4
.BI $( command )
.RE
.PP
Command substitutions may be nested.
.PP
Unless the substitution appears within double quotes, word splitting and
pathname expansion are performed on its result.
.PP
To enable command substitution, the caller must initialize the
.I ws_command
member with the address of the substitution function and make sure the
.B WRDSF_NOCMD
flag is not set.
.PP
The substitution function should be defined as follows:
.PP
.RS +4
\fBint \fIcommand\fB\
 (char **\fIret\fB,\
 const char *\fIcmd\fB,\
 size_t \fIlen,\fB\
 char **\fIargv\fB,\
 void *\fIclos\fB);\fR
.RE
.PP
On input, the first \fIlen\fR bytes of \fIcmd\fR contain the command
invocation as it appeared between
.BR $( " and " ),
with all expansions performed. 
.PP
The \fIargv\fR parameter contains the command 
line split into words using the same settings as the input \fIws\fR structure.
.PP
The \fIclos\fR parameter supplies user-specific data, passed in the
\fIws_closure\fR member).
.PP
On success, the function stores a pointer to the
output string in the memory location pointed to by \fIret\fR and
returns \fBWRDSE_OK\fR (\fB0\fR).  On error, it must return one of the
error codes described in the section 
.BR "ERROR CODES" .
If 
.BR WRDSE_USERERR ,
is returned, a pointer to the error description string must be stored in
.BR *ret .
.PP
When \fBWRDSE_OK\fR or \fBWRDSE_USERERR\fR is returned, the
data stored in \fB*ret\fR must be allocated using
.BR malloc (3).
.SS Tilde and pathname expansion
Both expansions are performed if the
.B WRDSF_PATHEXPAND
flag is set.
.PP
.I Tilde expansion
affects any word that begins with an unquoted tilde
character (\fB~\fR).  If the tilde is followed immediately by a slash,
it is replaced with the home directory of the current user (as
determined by his \fBpasswd\fR entry).  A tilde alone is handled the
same way.  Otherwise, the characters between the tilde and first slash
character (or end of string, if it doesn't contain any) are treated as
a login name. and are replaced (along with the tilde itself) with the
home directory of that user.  If there is no user with such login
name, the word is left unchanged.
.PP
During
.I pathname expansion
each unquoted word is scanned for characters 
.BR * ", " ? ", and " [ .
If any of these appears, the word is considered a \fIpattern\fR (in
the sense of
.BR glob (3))
and is replaced with an alphabetically sorted list of file names matching the
pattern.
.PP
If no matches are found for a word
and the \fIws_options\fR member has the
.B WRDSO_NULLGLOB
bit set, the word is removed.
.PP
If the \fBWRDSO_FAILGLOB\fR option is set, an error message is output
for each such word using
.IR ws_error .
.PP
When matching a pattern, the dot at the start of a name or immediately
following a slash must be matched explicitly, unless 
the \fBWRDSO_DOTGLOB\fR option is set.
.SH VARIABLE NAMES
By default a shell-like lexical structure of a variable name is
assumed.  A valid variable name begins with an alphabetical 
character or underscore and contains alphabetical characters, digits
and underscores.
.PP
The set of characters that constitute a variable name can be
augmented.  To do so, initialize the \fBws_namechar\fR member to the
C string containing the characters to be added, set the
\fBWRDSO_NAMECHAR\fR bit in \fBws_options\fR and set the
\fBWRDSF_OPTIONS\fR bit in the \fIflags\fR argument.
.PP
For example, to allow for colons in variable names, do:
.PP
.EX
struct wordsplit ws;
ws.ws_namechar = ":";
ws.ws_options = WRDSO_NAMECHAR;
wordsplit(str, &ws, WRDSF_DEFFLAGS|WRDSF_OPTIONS);
.EE
.PP
Certain characters cannot be allowed to be a name costituent.  These
are:
.BR $ ,
.BR { ,
.BR } ,
.BR * ,
.BR @ ,
.BR \- ,
.BR + ,
.BR ? ,
and
.BR = .
If any of these appears in \fBws_namechar\fR, the \fBwordsplit\fR (and
\fBwordsplit_len\fR) function will return the
.B WRDSE_USAGE
error.
.SH LIMITING THE NUMBER OF WORDS
The maximum number of words to be returned can be limited by setting
the \fBws_maxwords\fR member to the desired count, and setting the
\fBWRDSO_MAXWORDS\fR option, e.g.:
.sp
.EX
struct wo