forked from bminor/bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externs.hh
753 lines (578 loc) · 18.2 KB
/
externs.hh
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
/* externs.hh -- extern function declarations which do not appear in their
own header file. */
/* Copyright (C) 1993-2021 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash 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 of the License, or
(at your option) any later version.
Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.
*/
/* Make sure that this is included *after* config.h! */
#if !defined(_EXTERNS_H_)
#define _EXTERNS_H_
#include "bashtypes.hh"
#include "general.hh"
#include "posixstat.hh"
#include <unistd.h>
namespace bash
{
struct STRING_INT_ALIST;
/* Declarations for functions defined in stringlib.c */
int find_string_in_alist (char *, STRING_INT_ALIST *, int);
int find_index_in_alist (char *, STRING_INT_ALIST *, int);
// Cons a new string from STRING starting at START and ending at END,
// not including END.
//
// This should be replaced with std::string's substr (pos, count).
static inline char *
substring (const char *string, size_t start, size_t end)
{
size_t len;
char *result;
len = end - start;
result = new char[len + 1];
memcpy (result, string + start, len);
result[len] = '\0';
return result;
}
char *strsub (const char *, const char *, const char *, bool);
char *strcreplace (const char *, int, const char *, bool);
void strip_leading (char *);
void strip_trailing (char *, int, bool);
/* Functions from error.cc */
void itrace (const char *format, ...)
__attribute__ ((__format__ (printf, 1, 2)));
/* Functions from version.cc. */
std::string shell_version_string ();
void show_shell_version (int);
// functions from builtins/common.hh
/* Functions from the bash library, lib/sh/libsh.a. These should really
go into a separate include file. */
/* Enum type for the flags argument to sh_modcase. */
enum sh_modcase_flags
{
CASE_NOOP = 0,
CASE_LOWER = 0x0001,
CASE_UPPER = 0x0002,
CASE_CAPITALIZE = 0x0004,
CASE_UNCAP = 0x0008,
CASE_TOGGLE = 0x0010,
CASE_TOGGLEALL = 0x0020,
CASE_UPFIRST = 0x0040,
CASE_LOWFIRST = 0x0080,
CASE_USEWORDS = 0x1000
};
static inline sh_modcase_flags &
operator&= (sh_modcase_flags &a, const sh_modcase_flags &b)
{
a = static_cast<sh_modcase_flags> (static_cast<uint32_t> (a)
& static_cast<uint32_t> (b));
return a;
}
static inline sh_modcase_flags
operator~(const sh_modcase_flags &a)
{
return static_cast<sh_modcase_flags> (~static_cast<uint32_t> (a));
}
/* declarations for functions defined in lib/sh/casemod.c */
char *sh_modcase (const char *, const char *, sh_modcase_flags);
/* declarations for functions defined in lib/sh/clktck.c */
long get_clk_tck ();
/* declarations for functions defined in lib/sh/clock.c */
void clock_t_to_secs (clock_t, time_t *, int *);
void print_clock_t (FILE *, clock_t);
/* from lib/sh/eaccess.c */
int sh_stat (const char *, struct stat *);
/* Enum for functions defined in lib/sh/fmtulong.c */
enum fmt_flags
{
FL_NOFLAGS = 0,
FL_PREFIX = 0x01, /* add 0x, 0X, or 0 prefix as appropriate */
FL_ADDBASE = 0x02, /* add base# prefix to converted value */
FL_HEXUPPER = 0x04, /* use uppercase when converting to hex */
FL_UNSIGNED = 0x08 /* don't add any sign */
};
std::string fmtulong (unsigned long, int, fmt_flags = FL_NOFLAGS);
/* Declarations for functions defined in lib/sh/fmtulong.c */
std::string fmtullong (unsigned long long, int, fmt_flags = FL_NOFLAGS);
/* Declarations for functions defined in lib/sh/fmtumax.c */
std::string fmtumax (uint64_t, int, fmt_flags = FL_NOFLAGS);
/* Declarations for functions defined in lib/sh/fnxform.c */
#if defined(__APPLE__)
char *fnx_fromfs (char *, size_t);
char *fnx_tofs (char *, size_t);
#else
static inline char *
fnx_fromfs (char *fname, size_t)
{
return fname;
}
static inline char *
fnx_tofs (char *fname, size_t)
{
return fname;
}
#endif
/* Declarations for functions defined in lib/sh/input_avail.c */
int input_avail (int);
/* Inline declarations of functions previously defined in lib/sh/itos.c */
static inline std::string
inttostr (int64_t i)
{
return fmtumax (static_cast<uint64_t> (i), 10, FL_NOFLAGS);
}
// Integer to C++ string conversion.
static inline std::string
itos (int64_t i)
{
return fmtumax (static_cast<uint64_t> (i), 10, FL_NOFLAGS);
}
// Integer to C++ string conversion.
static inline std::string
mitos (int64_t i)
{
return fmtumax (static_cast<uint64_t> (i), 10, FL_NOFLAGS);
}
// Unsigned integer to C++ string conversion.
static inline std::string
uinttostr (uint64_t i)
{
return fmtumax (i, 10, FL_UNSIGNED);
}
// Integer to C++ string conversion.
static inline std::string
uitos (uint64_t i)
{
return fmtumax (i, 10, FL_UNSIGNED);
}
/* declarations for functions defined in lib/sh/netconn.c */
bool isnetconn (int);
/* declarations for functions defined in lib/sh/netopen.c */
int netopen (const char *);
/* Declarations for functions defined in lib/sh/oslib.c */
int getmaxgroups ();
long getmaxchild ();
/* declarations for functions defined in lib/sh/random.c */
int brand ();
void sbrand (unsigned long); /* set bash random number generator. */
void seedrand (); /* seed generator randomly */
void seedrand32 ();
uint32_t get_urandom32 ();
/* declarations for functions defined in lib/sh/setlinebuf.c */
int sh_setlinebuf (FILE *);
/* declarations for functions defined in lib/sh/shaccess.c */
int sh_eaccess (const char *, int);
/* declarations for functions defined in lib/sh/shmatch.c */
int sh_regmatch (const char *, const char *, int);
/* defines for flags argument to sh_regmatch. */
enum sh_match_flags
{
SHMAT_NOFLAGS = 0,
SHMAT_SUBEXP = 0x001, /* save subexpressions in SH_REMATCH */
SHMAT_PWARN = 0x002 /* print a warning message on invalid regexp */
};
/* declarations for functions defined in lib/sh/shmbchar.c */
size_t mbstrlen (const char *);
/* declarations for functions defined in lib/sh/shquote.c */
std::string sh_single_quote (string_view);
std::string sh_double_quote (string_view);
std::string sh_mkdoublequoted (string_view, int, int);
std::string sh_un_double_quote (string_view);
std::string sh_backslash_quote (string_view, string_view, int);
std::string sh_backslash_quote_for_double_quotes (string_view);
std::string sh_quote_reusable (string_view, int);
bool sh_contains_shell_metas (string_view);
static inline bool
sh_contains_quotes (string_view string)
{
string_view::iterator it;
for (it = string.begin (); it != string.end (); ++it)
{
if (*it == '\'' || *it == '"' || *it == '\\')
return true;
}
return false;
}
/* declarations for functions defined in lib/sh/spell.c */
int spname (const char *, char *);
char *dirspell (const char *);
/* declarations for functions and structures defined in lib/sh/stringlist.c */
// String lists and vectors are now the same thing.
typedef std::vector<char *> STRINGLIST;
#if 0
static inline STRINGLIST *
strlist_create (size_t n)
{
return new STRINGLIST (n);
}
// Note: call resize () directly, with or without size check.
static inline STRINGLIST *
strlist_resize (STRINGLIST *sl, size_t n)
{
if (n > sl->size ())
sl->resize (n);
return sl;
}
#endif
int strlist_remove (STRINGLIST *, char *);
STRINGLIST *strlist_copy (const STRINGLIST *);
STRINGLIST *strlist_merge (const STRINGLIST *, const STRINGLIST *);
void strlist_append (STRINGLIST *, const STRINGLIST *);
void strlist_prefix_suffix (STRINGLIST *, const char *, const char *);
static inline void
strlist_print (const STRINGLIST *sl, const char *prefix)
{
if (sl == nullptr)
return;
STRINGLIST::const_iterator it;
for (it = sl->begin (); it != sl->end (); ++it)
printf ("%s%s\n", prefix ? prefix : "", *it);
}
// Custom comparison function for sorting string pointer vectors.
struct string_ptr_comp
{
bool
operator() (const std::string *s1, const std::string *s2)
{
#if defined(HAVE_STRCOLL)
return strcoll (s1->c_str (), s2->c_str ()) < 0;
#else
return strcmp (s1->c_str (), s2->c_str ()) < 0;
#endif
}
};
/* Comparison routine for use by qsort that conforms to the new Posix
requirements (http://austingroupbugs.net/view.php?id=1070).
Perform a bytewise comparison if *S1 and *S2 collate equally. */
struct strlist_posixcmp
{
bool
operator() (const char *s1, const char *s2)
{
int result;
#if defined(HAVE_STRCOLL)
result = strcoll (s1, s2);
if (result != 0)
return (result < 0);
#endif
return strcmp (s1, s2) < 0;
}
};
/* Comparison routine for use with qsort() on arrays of strings. Uses
strcoll(3) if available, otherwise it uses strcmp(3). */
struct strlist_strcmp
{
bool
operator() (const char *s1, const char *s2)
{
#if defined(HAVE_STRCOLL)
return strcoll (s1, s2) < 0;
#else /* !HAVE_STRCOLL */
return strcmp (s1, s2) < 0;
#endif /* !HAVE_STRCOLL */
}
};
/* Sort ARRAY, a null terminated array of pointers to strings. */
static inline void
strlist_sort (STRINGLIST *array, bool posix)
{
if (posix)
{
strlist_posixcmp posixcmp;
std::sort (array->begin (), array->end (), posixcmp);
}
else
{
strlist_strcmp strcollcmp;
std::sort (array->begin (), array->end (), strcollcmp);
}
}
/* declarations for functions defined in lib/sh/stringvec.c */
// There is now a single STRINGLIST typedef for both sets of functions.
/* Free the contents of sl, a C++ vector of char *. */
static inline void
strlist_flush (STRINGLIST *sl)
{
if (sl == nullptr)
return;
STRINGLIST::iterator it;
for (it = sl->begin (); it != sl->end (); ++it)
delete[](*it);
}
// Free the contents of sl, a C++ vector of char *, then the array itself.
static inline void
strlist_dispose (STRINGLIST *sl)
{
if (sl == nullptr)
return;
strlist_flush (sl);
delete sl;
}
// Search for and remove the first occurrence of the specified name. Returns
// true if an item was found; false otherwise.
static inline bool
strlist_remove (STRINGLIST *sl, string_view name)
{
if (sl == nullptr)
return false;
STRINGLIST::iterator it;
for (it = sl->begin (); it != sl->end (); ++it)
if (name == *it)
{
delete[](*it);
return true;
}
return false;
}
// Find NAME in STRINGLIST. Return the index of NAME, or -1 if not present.
static inline int
strlist_search (const STRINGLIST *array, string_view name)
{
STRINGLIST::const_iterator it;
for (it = array->begin (); it != array->end (); ++it)
if (name == *it)
return static_cast<int> (it - array->begin ());
return -1;
}
class WORD_LIST;
STRINGLIST *strlist_from_word_list (const WORD_LIST *, size_t);
WORD_LIST *strlist_to_word_list (const STRINGLIST *, size_t);
/* declarations for functions defined in lib/sh/strtrans.cc */
std::string ansic_quote (string_view);
bool ansic_shouldquote (string_view);
std::string ansiexpand (string_view, size_t, size_t);
/* declarations for functions defined in lib/sh/strvec.cc */
/* Cons up a new array of words. The words are taken from LIST, which is a
WORD_LIST *. If IP is non-null, it gets the number of words in the returned
array. STARTING_INDEX says where to start filling in the returned array; it
can be used to reserve space at the beginning of the array. */
static inline char **
strvec_from_word_list (WORD_LIST *list, size_t starting_index, size_t *ip)
{
size_t count = list->size ();
char **array = new char *[1 + count + starting_index];
memset (array, 0, starting_index * sizeof (char *));
for (count = starting_index; list; count++, list = list->next ())
array[count] = savestring (list->word->word);
array[count] = nullptr;
if (ip)
*ip = count;
return array;
}
static inline void
strvec_dispose (char **array)
{
if (array == nullptr)
return;
for (int i = 0; array[i]; i++)
delete[] array[i];
delete[] array;
}
/* Return the length of ARRAY, a NULL terminated array of char *. */
static inline size_t
strvec_len (char **array)
{
size_t i;
for (i = 0; array[i]; i++);
return i;
}
/* declarations for functions defined in lib/sh/timeval.cc */
void timeval_to_secs (struct timeval *tvp, time_t *sp, int *sfp);
void print_timeval (FILE *fp, struct timeval *tvp);
/* declarations for functions defined in lib/sh/tmpfile.cc */
enum mktmp_flags
{
MT_NOFLAGS = 0,
MT_USETMPDIR = 0x0001,
MT_READWRITE = 0x0002,
MT_USERANDOM = 0x0004,
MT_TEMPLATE = 0x0008
};
char *sh_mktmpname (char *, int);
int sh_mktmpfd (string_view, int, char **);
/* extern FILE *sh_mktmpfp (string_view, int, char **); */
char *sh_mktmpdir (char *, int);
/* declarations for functions defined in lib/sh/uconvert.c */
int uconvert (char *, long *, long *, char **);
/* declarations for functions defined in lib/sh/ufuncs.c */
unsigned int falarm (unsigned int, unsigned int);
unsigned int fsleep (unsigned int, unsigned int);
/* declarations for functions defined in lib/sh/unicode.c */
int u32cconv (unsigned long, char *);
void u32reset ();
/* declarations for functions defined in lib/sh/wcsnwidth.c */
#if defined(HANDLE_MULTIBYTE)
ssize_t wcsnwidth (const wchar_t *, size_t, size_t);
#endif
/* declarations for functions defined in lib/sh/zgetline.c */
ssize_t zgetline (int, char **, size_t *, int, bool);
/* declarations for functions defined in lib/sh/zmapfd.c */
int zmapfd (int, char **, string_view);
/* declarations for functions defined in lib/glob/gmisc.c */
bool match_pattern_char (string_view, string_view, int);
int umatchlen (string_view, size_t);
#if defined(HANDLE_MULTIBYTE)
bool match_pattern_wchar (const wchar_t *, const wchar_t *, int);
int wmatchlen (const wchar_t *, size_t);
#endif
// This value was previously stored in lib/sh/zread.c.
#ifndef ZBUFSIZ
#define ZBUFSIZ 4096
#endif
// Inline functions moved from general.cc.
/* Return non-zero if the characters from SAMPLE are not all valid
characters to be found in the first line of a shell script. We
check up to the first newline, or SAMPLE_LEN, whichever comes first.
All of the characters must be printable or whitespace. */
static inline bool
check_binary_file (string_view sample, size_t sample_len)
{
unsigned char c;
for (size_t i = 0; i < sample_len; i++)
{
c = static_cast<unsigned char> (sample[i]);
if (c == '\n')
return false;
if (c == '\0')
return true;
}
return false;
}
/* **************************************************************** */
/* */
/* Functions to manipulate pipes */
/* */
/* **************************************************************** */
static inline int
sh_openpipe (int *pv)
{
int r;
if ((r = pipe (pv)) < 0)
return r;
pv[0] = move_to_high_fd (pv[0], 1, 64);
pv[1] = move_to_high_fd (pv[1], 1, 64);
return 0;
}
static inline int
sh_closepipe (int *pv)
{
if (pv[0] >= 0)
close (pv[0]);
if (pv[1] >= 0)
close (pv[1]);
pv[0] = pv[1] = -1;
return 0;
}
/* Just a wrapper for the define in include/filecntl.h */
static inline int
sh_setclexec (int fd)
{
return SET_CLOSE_ON_EXEC (fd);
}
/* Return true if file descriptor FD is valid; false otherwise. */
static inline bool
sh_validfd (int fd)
{
return fcntl (fd, F_GETFD, 0) >= 0;
}
static inline bool
fd_ispipe (int fd)
{
errno = 0;
return (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
}
#ifdef _POSIXSTAT_H_
bool same_file (const char *, const char *, struct stat *, struct stat *);
#endif
/* **************************************************************** */
/* */
/* Functions to inspect pathnames */
/* */
/* **************************************************************** */
static inline bool
file_exists (const char *fn)
{
struct stat sb;
return stat (fn, &sb) == 0;
}
static inline bool
file_isdir (const char *fn)
{
struct stat sb;
return (stat (fn, &sb) == 0) && S_ISDIR (sb.st_mode);
}
static inline bool
file_iswdir (const char *fn)
{
return file_isdir (fn) && sh_eaccess (fn, W_OK) == 0;
}
/* Return 1 if STRING is "." or "..", optionally followed by a directory
separator */
static inline bool
path_dot_or_dotdot (string_view string)
{
if (string.empty () || string[0] != '.')
return false;
/* string[0] == '.' */
if (PATHSEP (string[1]) || (string[1] == '.' && PATHSEP (string[2])))
return true;
return false;
}
/* Return true if STRING contains an absolute pathname. Used by `cd'
to decide whether or not to look up a directory name in $CDPATH. */
static inline bool
absolute_pathname (string_view string)
{
if (string.empty ())
return false;
if (ABSPATH (string))
return true;
if (string[0] == '.' && PATHSEP (string[1])) /* . and ./ */
return true;
if (string[0] == '.' && string[1] == '.'
&& PATHSEP (string[2])) /* .. and ../ */
return true;
return false;
}
/* Return the `basename' of the pathname in STRING (the stuff after the
last '/'). If STRING is `/', just return it. */
static inline const char *
base_pathname (const char *string) noexcept
{
if (string[0] == '/' && string[1] == '\0')
return string;
const char *p = strrchr (string, '/');
return p ? ++p : string;
}
char *make_absolute (string_view, string_view);
char *full_pathname (char *);
/* Return a printable representation of FN without special characters. The
caller is responsible for freeing memory if this returns something other
than its argument. If FLAGS is non-zero, we are printing for portable
re-input and should single-quote filenames appropriately. */
static inline std::string
printable_filename (string_view fn, int flags)
{
if (ansic_shouldquote (fn))
return ansic_quote (fn);
else if (flags && sh_contains_shell_metas (fn))
return sh_single_quote (fn);
else
return to_string (fn);
}
char *extract_colon_unit (const char *, size_t *);
void tilde_initialize ();
char *bash_tilde_find_word (const char *s, int, size_t *);
char *bash_tilde_expand (const char *s, int);
#if !defined(HAVE_GROUP_MEMBER)
int group_member (gid_t);
#endif
std::string conf_standard_path ();
} // namespace bash
#endif /* _EXTERNS_H_ */