52 #if defined(AJA_LINUX) || defined (AJA_MAC)
55 #include <sys/ioctl.h>
56 #include <sys/types.h>
59 #define POPT_SYSCONFDIR "."
64 #pragma GCC diagnostic ignored "-Wunused-const-variable"
67 #if defined(AJA_WINDOWS)
69 #pragma warning (disable:4302)
70 #pragma warning (disable:4311)
71 #pragma warning (disable:4996)
78 typedef signed __int8 int8_t;
79 typedef signed __int16 int16_t;
80 typedef signed __int32 int32_t;
81 typedef signed __int64 int64_t;
82 typedef unsigned __int8 uint8_t;
83 typedef unsigned __int16 uint16_t;
84 typedef unsigned __int32 uint32_t;
85 typedef unsigned __int64 uint64_t;
88 static uid_t getuid() {
return 0; }
90 #define strtoll _strtoi64
92 #define execvp(a, b) _spawnvp(_P_OVERLAY, (a), (b))
95 static bool access(
char* t,
int x) {
return true;}
97 #define POPT_SYSCONFDIR "."
99 #define ssize_t SSIZE_T
101 #define open(a, b) _open((a), (b))
102 #define close(a) _close((a))
103 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
108 inline char * stpcpy (
char *dest,
const char * src) {
109 register char *d = dest;
110 register const char *s = src;
114 while (*s++ !=
'\0');
119 #pragma GCC diagnostic ignored "-Wunused-parameter"
120 #if defined (AJA_LINUX)
121 #pragma GCC diagnostic ignored "-Wmisleading-indentation"
123 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
126 #if defined(AJA_BAREMETAL)
127 static uid_t getuid() {
return 0; }
128 #define POPT_SYSCONFDIR "."
130 static bool access(
char* t,
int x) {
return true;}
133 # define LLONG_MIN (-LLONG_MAX-1)
135 # define LLONG_MAX __LONG_LONG_MAX__
149 #define _isspaceptr(_chp) isspace((int)(*(unsigned char *)(_chp)))
161 void *
xcalloc (
size_t nmemb,
size_t size);
163 void *
xrealloc (
void * ptr,
size_t size);
165 char *
xstrdup (
const char *str);
168 #define xmalloc(_size) malloc(_size)
169 #define xcalloc(_nmemb, _size) calloc((_nmemb), (_size))
170 #define xrealloc(_ptr, _size) realloc((_ptr), (_size))
171 #define xstrdup(_str) strdup(_str)
192 if (p !=
NULL) free((
void *)p);
198 #define __PBM_NBITS (8 * sizeof (__pbm_bits))
199 #define __PBM_IX(d) ((d) / __PBM_NBITS)
200 #define __PBM_MASK(d) ((__pbm_bits) 1 << (((unsigned)(d)) % __PBM_NBITS))
204 #define __PBM_BITS(set) ((set)->bits)
206 #define PBM_ALLOC(d) calloc(__PBM_IX (d) + 1, sizeof(__pbm_bits))
207 #define PBM_FREE(s) _free(s);
208 #define PBM_SET(d, s) (__PBM_BITS (s)[__PBM_IX (d)] |= __PBM_MASK (d))
209 #define PBM_CLR(d, s) (__PBM_BITS (s)[__PBM_IX (d)] &= ~__PBM_MASK (d))
210 #define PBM_ISSET(d, s) ((__PBM_BITS (s)[__PBM_IX (d)] & __PBM_MASK (d)) != 0)
213 uint32_t *pc, uint32_t *pb);
240 #define poptArgType(_opt) ((_opt)->argInfo & _poptArgMask)
241 #define poptGroup(_opt) ((_opt)->argInfo & _poptGroupMask)
243 #define F_ISSET(_opt, _FLAG) ((_opt)->argInfo & POPT_ARGFLAG_##_FLAG)
244 #define LF_ISSET(_FLAG) (argInfo & POPT_ARGFLAG_##_FLAG)
245 #define CBF_ISSET(_opt, _FLAG) ((_opt)->argInfo & POPT_CBFLAG_##_FLAG)
250 #define _POPTHELP_MAXLINE ((size_t)79)
254 {
"help",
'?', 0,
NULL, (int)
'?',
N_(
"Show this help message"),
NULL },
255 {
"usage",
'\0', 0,
NULL, (int)
'u',
N_(
"Display brief usage message"),
NULL },
264 {
"help",
'?', 0,
NULL, (int)
'?',
N_(
"Show this help message"),
NULL },
265 {
"usage",
'\0', 0,
NULL, (int)
'u',
N_(
"Display brief usage message"),
NULL },
266 {
"",
'\0', 0,
NULL, 0,
N_(
"Terminate options"),
NULL },
272 #define poptSubstituteHelpI18N(opt) \
274 if ((&opt) == poptHelpOptions) (opt) = *poptHelpOptionsI18N; \
314 int POPT_fprintf (FILE* stream,
const char *format, ...);
322 #define D_(dom, str) str
323 #define POPT_(foo) foo
331 #include <sys/stat.h>
342 int * acp,
const char *** avp)
344 const char * pat = pattern;
348 if (pat[0] ==
'@' && pat[1] !=
'(')
354 if (avp && (*avp = (
const char**)calloc((
size_t)(1 + 1),
sizeof (**avp))) !=
NULL)
364 uid_t uid = getuid();
366 if (stat(fn, &sb) == -1)
368 if ((uid_t)sb.st_uid != uid)
370 if (!S_ISREG(sb.st_mode))
372 if (sb.st_mode & (S_IWGRP|S_IWOTH))
389 || (nb = file.
Tell()) == (off_t)-1
391 || (b = (
char*)calloc(
sizeof(*b), (
size_t)nb + 1)) ==
NULL
392 || file.
Read((uint8_t *)b, (uint32_t)nb) != (uint32_t)nb)
410 for (t = b, s = b, se = b + nb; *s && s < se; s++) {
464 const char * appName;
465 const char * entryType;
475 memset(item, 0,
sizeof(*item));
489 if (*se !=
'\0') *se++ =
'\0';
492 if (*se ==
'\0')
goto exit;
495 if (opt[0] ==
'-' && *se ==
'\0')
goto exit;
496 if (*se !=
'\0') *se++ =
'\0';
499 if (opt[0] ==
'-' && *se ==
'\0')
goto exit;
501 if (opt[0] ==
'-' && opt[1] ==
'-')
502 item->option.longName = opt + 2;
503 else if (opt[0] ==
'-' && opt[2] ==
'\0')
504 item->option.shortName = opt[1];
506 const char * fn = opt;
511 if (b ==
NULL || nb == 0)
516 size_t nse = strlen(se) + 1;
517 if ((b = (
char*)realloc(b, (nb + nse))) ==
NULL)
519 (
void) stpcpy( stpcpy(&b[nb-1],
" "), se);
525 {
const char * longName = strrchr(fn,
'/');
526 if (longName !=
NULL)
530 if (longName ==
NULL)
533 if (longName[1] !=
'\0')
534 item->option.longName = longName;
536 item->option.shortName = longName[0];
543 for (i = 0, j = 0; i < item->argc; i++, j++) {
545 if (!strncmp(item->argv[i],
"--POPTdesc=",
sizeof(
"--POPTdesc=")-1)) {
546 f = item->argv[i] +
sizeof(
"--POPTdesc=");
547 if (f[0] ==
'$' && f[1] ==
'"') f++;
548 item->option.descrip = f;
552 if (!strncmp(item->argv[i],
"--POPTargs=",
sizeof(
"--POPTargs=")-1)) {
553 f = item->argv[i] +
sizeof(
"--POPTargs=");
554 if (f[0] ==
'$' && f[1] ==
'"') f++;
555 item->option.argDescrip = f;
561 item->argv[j] = item->argv[i];
564 item->argv[j] =
NULL;
568 if (!strcmp(entryType,
"alias"))
570 else if (!strcmp(entryType,
"exec"))
581 char * b =
NULL, *be;
588 return (errno == ENOENT ? 0 : rc);
589 if (b ==
NULL || nb == 0)
592 if ((t = (
char*)malloc(nb + 1)) ==
NULL)
597 for (se = b; se < be; se++) {
603 if (*te && *te !=
'#')
609 if (se < be && *se !=
'\n') {
636 for (p = buf; p !=
NULL && *p !=
'\0'; p = pe) {
637 const char ** av =
NULL;
643 pe = (
char*)strchr(p,
':');
644 if (pe !=
NULL && *pe ==
':')
647 pe = (
char *) (p + strlen(p));
652 for (i = 0; i < ac; i++) {
653 const char * fn = av[i];
657 if (p[0] ==
'@' && p[1] !=
'(') {
658 if (fn[0] ==
'@' && fn[1] !=
'(')
683 static const char _popt_sysconfdir[] = POPT_SYSCONFDIR
"/popt";
684 static const char _popt_etc[] =
"/etc/popt";
690 if (strcmp(_popt_sysconfdir, _popt_etc)) {
698 if ((home = getenv(
"HOME"))) {
699 char * fn = (
char*)malloc(strlen(home) + 20);
701 (
void) stpcpy(stpcpy(fn, home),
"/.popt");
721 const struct poptOption * options,
const char * configPaths)
729 if ((argv0 = strrchr(
argv[0],
'/')) !=
NULL) argv0++;
730 else argv0 =
argv[0];
745 #define POPT_WCHAR_HACK
746 #ifdef POPT_WCHAR_HACK
766 if (key->shortName ==
'?')
804 size_t n = strlen(s);
808 memset ((
void *)&t, 0,
sizeof (t));
810 n = mbsrtowcs (
NULL, &s, n, &t);
824 return (
const char*)opt->
arg;
835 const char * translation_domain)
849 || !strcmp(opt->
argDescrip,
N_(
"Options implemented via popt alias/exec:")))
872 default:
return POPT_(
"ARG");
886 const char * translation_domain)
888 const char * defstr =
D_(translation_domain,
"default");
889 char * le = (
char*)malloc(4*lineLength + 1);
895 le = stpcpy(le, defstr);
904 le += sprintf(le,
"%d", arg.
intp[0]);
907 le += sprintf(le,
"%hd", arg.
shortp[0]);
910 le += sprintf(le,
"%ld", arg.
longp[0]);
913 le += sprintf(le,
"%lld", arg.
longlongp[0]);
916 {
double aDouble = (double) arg.
floatp[0];
917 le += sprintf(le,
"%g", aDouble);
920 le += sprintf(le,
"%g", arg.
doublep[0]);
923 le += sprintf(le,
"%p", opt->
arg);
926 le += sprintf(le,
"%p", opt->
arg);
929 {
const char * s = arg.
argv[0];
931 le = stpcpy(le,
"null");
933 size_t limit = 4*lineLength - (le - l) -
sizeof(
"\"\")");
936 strncpy(le, s, limit); le[limit] =
'\0'; le += (slen = strlen(le));
937 if (slen == limit && s[limit])
938 le[-1] = le[-2] = le[-3] =
'.';
964 const char * translation_domain)
966 size_t maxLeftCol = columns->
cur;
967 size_t indentLength = maxLeftCol + 5;
968 size_t lineLength = columns->
max - indentLength;
969 const char * help =
D_(translation_domain, opt->
descrip);
970 const char * argDescrip =
getArgDescrip(opt, translation_domain);
976 size_t nb = maxLeftCol + 1;
981 if (
F_ISSET(opt, TOGGLE)) nb +=
sizeof(
"[no]") - 1;
982 if (argDescrip) nb += strlen(argDescrip);
984 left = (
char*)malloc(nb);
985 if (left ==
NULL)
return;
987 left[maxLeftCol] =
'\0';
989 #define prtlong (opt->longName != NULL)
993 char *dash =
F_ISSET(opt, ONEDASH) ? (
char*)
"-" : (
char*)
"--";
996 (
void) stpcpy(stpcpy(stpcpy(left+2,
", "), dash), opt->
longName);
997 }
else if (prtshort) {
1004 : (
F_ISSET(opt, ONEDASH) ? (
char*)
"-" : (
char*)
"--");
1005 const char *longName = opt->
longName;
1009 if (longName[0] ==
'n' && longName[1] ==
'o') {
1010 longName +=
sizeof(
"no") - 1;
1011 if (longName[0] ==
'-')
1016 (
void) stpcpy(stpcpy(stpcpy(stpcpy(left,
" "), dash), toggle), longName);
1021 char * le = left + strlen(left);
1027 if (
F_ISSET(opt, SHOW_DEFAULT)) {
1030 char * t = (
char*)malloc((help ? strlen(help) : 0) +
1031 strlen(defs) +
sizeof(
" "));
1035 te = stpcpy(te, help);
1038 defs = (
char*)
_free(defs);
1058 le = stpcpy(le, argDescrip);
1067 if (!strchr(
" =(", argDescrip[0]))
1070 le = stpcpy(leo = le, argDescrip);
1081 POPT_fprintf(fp,
" %-*s ", (
int)(maxLeftCol+displaypad), left);
1087 left = (
char*)
_free(left);
1091 helpLength = strlen(help);
1092 while (helpLength > lineLength) {
1096 ch = help + lineLength - 1;
1099 if (ch == help)
break;
1108 {
char * fmthelp =
xstrdup(help);
1110 fmthelp[ch - help] =
'\0';
1111 sprintf(format,
"%%s\n%%%ds", (
int) indentLength);
1120 helpLength = strlen(help);
1123 if (helpLength) fprintf(fp,
"%s\n", help);
1127 defs = (
char*)
_free(defs);
1128 left = (
char*)
_free(left);
1138 const char * translation_domain)
1142 const char * argDescrip;
1149 if (len > max) max = len;
1150 }
else if (!
F_ISSET(opt, DOC_HIDDEN)) {
1151 len =
sizeof(
" ")-1;
1153 len +=
sizeof(
"-X, ")-1;
1155 len += (
F_ISSET(opt, ONEDASH) ?
sizeof(
"-") :
sizeof(
"--")) - 1;
1164 if (!strchr(
" =(", argDescrip[0])) len +=
sizeof(
"=")-1;
1170 if (
F_ISSET(opt, OPTIONAL)) len +=
sizeof(
"[]")-1;
1171 if (len > max) max = len;
1190 const char * translation_domain)
1196 for (i = 0, item = items; i < nitems; i++, item++) {
1215 const char * translation_domain)
1218 const char *sub_transdom;
1237 if (sub_transdom ==
NULL)
1238 sub_transdom = translation_domain;
1256 size_t len = (size_t)6;
1262 if (fn ==
NULL)
return len;
1263 if (strchr(fn,
'/')) fn = strrchr(fn,
'/') + 1;
1265 fprintf(fp,
" %s", fn);
1266 len += strlen(fn) + 1;
1299 const char *translation_domain)
1301 size_t len =
sizeof(
" []")-1;
1302 const char * argDescrip =
getArgDescrip(opt, translation_domain);
1306 #define prtlong (opt->longName != NULL)
1308 return columns->
cur;
1310 len =
sizeof(
" []")-1;
1312 len +=
sizeof(
"-c")-1;
1314 if (prtshort) len +=
sizeof(
"|")-1;
1315 len += (
F_ISSET(opt, ONEDASH) ?
sizeof(
"-") :
sizeof(
"--")) - 1;
1322 if (!strchr(
" =(", argDescrip[0])) len +=
sizeof(
"=")-1;
1328 if ((columns->
cur + len) > columns->
max) {
1330 columns->
cur = (size_t)7;
1337 fprintf(fp,
"%s%s%s",
1338 (prtshort ?
"|" :
""),
1339 (
F_ISSET(opt, ONEDASH) ?
"-" :
"--"),
1345 if (!strchr(
" =(", argDescrip[0])) fprintf(fp,
"=");
1346 fprintf(fp,
"%s", argDescrip);
1350 return columns->
cur + len + 1;
1363 const char * translation_domain)
1368 for (i = 0; i < nitems; i++, item++) {
1372 translation_domain = (
const char *)opt->
arg;
1379 return columns->
cur;
1403 const char * translation_domain,
1409 translation_domain = (
const char *)opt->
arg;
1415 for (i = 0; i < done->
nopts; i++) {
1416 const void * that = done->
opts[i];
1417 if (that ==
NULL || that != opt->
arg)
1422 if (opt->
arg ==
NULL || i < done->nopts)
1428 translation_domain, done);
1435 return columns->
cur;
1450 size_t nb = (size_t)300;
1451 char * s = (str !=
NULL ? str : (
char*)calloc((
size_t)1, nb));
1452 size_t len = (size_t)0;
1471 if (s != str && *s !=
'\0') {
1472 fprintf(fp,
" [-%s]", s);
1473 len = strlen(s) +
sizeof(
" [-]")-1;
1486 memset(done, 0,
sizeof(*done));
1490 columns->
cur = done->maxopts *
sizeof(*done->opts);
1492 done->opts = (
const void**)calloc((
size_t)1, columns->
cur);
1493 if (done->opts !=
NULL)
1494 done->opts[done->nopts++] = (
const void *) con->
options;
1504 if (columns->
cur > columns->
max) fprintf(fp,
"\n ");
1509 if (done->opts !=
NULL)
1530 #define _JLU3_jlu32lpair 1
1531 #define jlu32lpair poptJlu32lpair
1574 const unsigned char uc[4];
1575 }
endian = { 0x11223344 };
1576 # define HASH_LITTLE_ENDIAN (endian.uc[0] == (unsigned char) 0x44)
1577 # define HASH_BIG_ENDIAN (endian.uc[0] == (unsigned char) 0x11)
1580 # define ROTL32(x, s) (((x) << (s)) | ((x) >> (32 - (s))))
1584 #define _JLU3_INIT(_h, _size) (0xdeadbeef + ((uint32_t)(_size)) + (_h))
1630 #define _JLU3_MIX(a,b,c) \
1632 a -= c; a ^= ROTL32(c, 4); c += b; \
1633 b -= a; b ^= ROTL32(a, 6); a += c; \
1634 c -= b; c ^= ROTL32(b, 8); b += a; \
1635 a -= c; a ^= ROTL32(c,16); c += b; \
1636 b -= a; b ^= ROTL32(a,19); a += c; \
1637 c -= b; c ^= ROTL32(b, 4); b += a; \
1665 #define _JLU3_FINAL(a,b,c) \
1667 c ^= b; c -= ROTL32(b,14); \
1668 a ^= c; a -= ROTL32(c,11); \
1669 b ^= a; b -= ROTL32(a,25); \
1670 c ^= b; c -= ROTL32(b,16); \
1671 a ^= c; a -= ROTL32(c,4); \
1672 b ^= a; b -= ROTL32(a,14); \
1673 c ^= b; c -= ROTL32(b,24); \
1676 #if defined(_JLU3_jlu32lpair)
1693 void jlu32lpair(
const void *key,
size_t size, uint32_t *pc, uint32_t *pb)
1695 union {
const void *ptr;
size_t i; } u;
1707 const uint32_t *k = (
const uint32_t *)key;
1710 while (size > (
size_t)12) {
1729 case 12: c += k[2]; b+=k[1]; a+=k[0];
break;
1730 case 11: c += k[2]&0xffffff; b+=k[1]; a+=k[0];
break;
1731 case 10: c += k[2]&0xffff; b+=k[1]; a+=k[0];
break;
1732 case 9: c += k[2]&0xff; b+=k[1]; a+=k[0];
break;
1733 case 8: b += k[1]; a+=k[0];
break;
1734 case 7: b += k[1]&0xffffff; a+=k[0];
break;
1735 case 6: b += k[1]&0xffff; a+=k[0];
break;
1736 case 5: b += k[1]&0xff; a+=k[0];
break;
1737 case 4: a += k[0];
break;
1738 case 3: a += k[0]&0xffffff;
break;
1739 case 2: a += k[0]&0xffff;
break;
1740 case 1: a += k[0]&0xff;
break;
1745 const uint16_t *k = (
const uint16_t *)key;
1749 while (size > (
size_t)12) {
1750 a += k[0] + (((uint32_t)k[1])<<16);
1751 b += k[2] + (((uint32_t)k[3])<<16);
1752 c += k[4] + (((uint32_t)k[5])<<16);
1759 k8 = (
const uint8_t *)k;
1762 c += k[4]+(((uint32_t)k[5])<<16);
1763 b += k[2]+(((uint32_t)k[3])<<16);
1764 a += k[0]+(((uint32_t)k[1])<<16);
1767 c += ((uint32_t)k8[10])<<16;
1770 b += k[2]+(((uint32_t)k[3])<<16);
1771 a += k[0]+(((uint32_t)k[1])<<16);
1776 b += k[2]+(((uint32_t)k[3])<<16);
1777 a += k[0]+(((uint32_t)k[1])<<16);
1780 b += ((uint32_t)k8[6])<<16;
1783 a += k[0]+(((uint32_t)k[1])<<16);
1788 a += k[0]+(((uint32_t)k[1])<<16);
1791 a += ((uint32_t)k8[2])<<16;
1803 const uint8_t *k = (
const uint8_t *)key;
1806 while (size > (
size_t)12) {
1808 a += ((uint32_t)k[1])<<8;
1809 a += ((uint32_t)k[2])<<16;
1810 a += ((uint32_t)k[3])<<24;
1812 b += ((uint32_t)k[5])<<8;
1813 b += ((uint32_t)k[6])<<16;
1814 b += ((uint32_t)k[7])<<24;
1816 c += ((uint32_t)k[9])<<8;
1817 c += ((uint32_t)k[10])<<16;
1818 c += ((uint32_t)k[11])<<24;
1826 case 12: c += ((uint32_t)k[11])<<24;
1827 case 11: c += ((uint32_t)k[10])<<16;
1828 case 10: c += ((uint32_t)k[9])<<8;
1830 case 8: b += ((uint32_t)k[7])<<24;
1831 case 7: b += ((uint32_t)k[6])<<16;
1832 case 6: b += ((uint32_t)k[5])<<8;
1834 case 4: a += ((uint32_t)k[3])<<24;
1835 case 3: a += ((uint32_t)k[2])<<16;
1836 case 2: a += ((uint32_t)k[1])<<8;
1857 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1858 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1859 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1860 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1861 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1862 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1863 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
1864 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
1870 const char *p = str;
1874 if (((
unsigned)*p & 0xc0) != (
unsigned)0x80)
1882 const char *p = str;
1884 while (*p !=
'\0') {
1886 if (((
unsigned)*p & 0xc0) != (
unsigned)0x80)
1892 #if !defined(POPT_fprintf)
1901 size_t nb = (size_t)1;
1908 while ((b = (
char*)realloc(b, nb+1)) !=
NULL) {
1909 va_start(ap, format);
1910 rc = vsnprintf(b, nb, format, ap);
1913 if ((
size_t)rc < nb)
1915 nb = (size_t)(rc + 1);
1917 nb += (nb < (size_t)100 ? (
size_t)100 : nb);
1922 rc = fprintf(stream,
"%s", b);
1937 #define POPT_ARGV_ARRAY_GROW_DELTA 5
1940 int * argcPtr,
const char *** argvPtr)
1942 size_t nb = (argc + 1) *
sizeof(*argv);
1943 const char ** argv2;
1947 if (argc <= 0 || argv ==
NULL)
1949 for (i = 0; i < argc; i++) {
1950 if (argv[i] ==
NULL)
1952 nb += strlen(argv[i]) + 1;
1955 dst = (
char*)malloc(nb);
1958 argv2 = (
const char **) dst;
1959 dst += (argc + 1) *
sizeof(*argv);
1962 for (i = 0; i < argc; i++) {
1964 dst = stpcpy(dst, argv[i]);
1985 const char ** argv = (
const char**)malloc(
sizeof(*argv) * argvAlloced);
1987 size_t buflen = strlen(s) + 1;
1988 char * buf, * bufOrig =
NULL;
1991 if (argv ==
NULL)
return rc;
1992 buf = bufOrig = (
char*)calloc((
size_t)1, buflen);
1999 for (src = s; *src !=
'\0'; src++) {
2000 if (quote == *src) {
2002 }
else if (quote !=
'\0') {
2009 if (*src != quote) *buf++ =
'\\';
2013 if (*argv[argc] !=
'\0') {
2015 if (argc == argvAlloced) {
2017 argv = (
const char**)realloc(argv,
sizeof(*argv) * argvAlloced);
2018 if (argv ==
NULL)
goto exit;
2022 }
else switch (*src) {
2039 if (strlen(argv[argc])) {
2046 if (bufOrig) free(bufOrig);
2047 if (argv) free(argv);
2066 size_t maxlinelen =
sizeof(line);
2068 size_t maxargvlen = (size_t)480;
2079 argstr = (
char*)calloc(maxargvlen,
sizeof(*argstr));
2082 while (fgets(line, (
int)maxlinelen, fp) !=
NULL) {
2089 linelen = strlen(p);
2090 if (linelen >= maxlinelen-1) {
2095 if (*p ==
'\0' || *p ==
'\n')
continue;
2096 if (*p ==
'#')
continue;
2100 while (*q !=
'\0' && (!
_isspaceptr(q)) && *q !=
'=')
2111 argvlen += (t = (size_t)(q - p)) + (
sizeof(
" --")-1);
2112 if (argvlen >= maxargvlen) {
2113 maxargvlen = (t > maxargvlen) ? t*2 : maxargvlen*2;
2114 argstr = (
char*)realloc(argstr, maxargvlen);
2117 strcat(argstr,
" --");
2139 t = (size_t)(x - p);
2140 argvlen += t + (
sizeof(
"' --='")-1);
2141 if (argvlen >= maxargvlen) {
2142 maxargvlen = (t > maxargvlen) ? t*2 : maxargvlen*2;
2143 argstr = (
char*)realloc(argstr, maxargvlen);
2146 strcat(argstr,
" --");
2148 strcat(argstr,
"=\"");
2150 strcat(argstr,
"\"");
2164 int _popt_debug = 0;
2223 const void * myData,
int shorty)
2252 {
const void *cbData = (cbopt->
descrip ? cbopt->
descrip : myData);
2265 const struct poptOption * options,
unsigned int flags)
2270 memset(con, 0,
sizeof(*con));
2292 if (getenv(
"POSIXLY_CORRECT") || getenv(
"POSIX_ME_HARDER"))
2314 if (con ==
NULL)
return;
2349 for (i = con->
numExecs - 1; i >= 0; i--) {
2350 item = con->
execs + i;
2358 if (i < 0)
return 0;
2379 {
char *s = (
char*)malloc((
longName ? strlen(
longName) : 0) +
sizeof(
"--"));
2384 s = stpcpy( stpcpy(s,
"-"),
longName);
2404 const char *
longName,
size_t longNameLen)
2406 const char * optLongName = opt->
longName;
2413 if (optLongName[0] ==
'n' && optLongName[1] ==
'o') {
2414 optLongName +=
sizeof(
"no") - 1;
2415 if (optLongName[0] ==
'-')
2420 longNameLen -=
sizeof(
"no") - 1;
2427 rc = (int)(strlen(optLongName) == longNameLen);
2429 rc = (int)(strncmp(optLongName,
longName, longNameLen) == 0);
2435 const char *
longName,
size_t longNameLen,
2437 const char * nextArg)
2466 if (i < 0)
return 0;
2484 av = (
const char**)malloc((ac + 1 + 1) *
sizeof(*av));
2486 for (i = 0; i < ac; i++) {
2501 return (rc ? rc : 1);
2519 if (strchr(argv0,
'/'))
2522 if ((path = getenv(
"PATH")) ==
NULL || (path =
xstrdup(path)) ==
NULL)
2526 if ((t = (
char*)malloc(strlen(path) + strlen(argv0) +
sizeof(
"/"))) !=
NULL)
2527 for (s = path; s && *s; s = se) {
2530 if ((se = strchr(s,
':')))
2534 (
void) stpcpy(stpcpy(stpcpy(t, s),
"/"), argv0);
2537 if (!access(t, X_OK))
2542 if (!(s && *s) && t !=
NULL)
2543 t = (
char*)
_free(t);
2544 path = (
char*)
_free(path);
2563 argv = (
poptArgv)malloc(
sizeof(*argv) *
2568 char *s = (
char *)malloc(strlen(con->
execPath) + strlen(item->
argv[0]) +
sizeof(
"/"));
2575 if (argv[argc++] ==
NULL) {
2580 if (item->
argc > 1) {
2581 memcpy(argv + argc, item->
argv + 1,
sizeof(*argv) * (item->
argc - 1));
2582 argc += (item->
argc - 1);
2598 execvp(argv[0], (
char *
const *)argv);
2603 free((
void *)argv[0]);
2611 const char *
longName,
size_t longNameLen,
2614 const void ** callbackData,
2634 if (
arg.ptr ==
NULL)
continue;
2639 if (opt2 ==
NULL)
continue;
2641 if (callback && *callback
2642 && callbackData && *callbackData ==
NULL)
2669 *callback = (cb ? cbarg.
cb :
NULL);
2677 unsigned argx,
int delete_arg)
2688 for (i = os->
next; i < os->
argc; i++) {
2691 if (*os->
argv[i] ==
'-')
2704 }
while (arg ==
NULL);
2711 const char * a =
NULL;
2713 size_t tn = strlen(s) + 1;
2716 te = t = (
char*)malloc(tn);
2719 while ((c = *s++) !=
'\0') {
2722 if (!(s[0] ==
'#' && s[1] ==
':' && s[2] ==
'+'))
2729 s +=
sizeof(
"#:+") - 1;
2732 {
size_t pos = (size_t) (te - t);
2733 if ((t = (
char*)realloc(t, tn)) ==
NULL)
2735 te = stpcpy(t + pos, a);
2746 if ((t + tn) > te) {
2747 if ((te = (
char*)realloc(t, (
size_t)(te - t))) ==
NULL)
2773 if (*bitsp ==
NULL) {
2787 size_t ns = (s ? strlen(s) : 0);
2791 if (bits ==
NULL || ns == 0)
2796 for (ns = 0; ns < (size_t)
_poptBitsK; ns++) {
2797 uint32_t h = h0 + (uint32_t)ns * h1;
2806 size_t ns = (s ? strlen(s) : 0);
2811 if (bits ==
NULL || ns == 0)
2816 for (ns = 0; ns < (size_t)
_poptBitsK; ns++) {
2817 uint32_t h = h0 + (uint32_t)ns * h1;
2834 memset(bits, 0, nw * nbw);
2840 size_t ns = (s ? strlen(s) : 0);
2844 if (bits ==
NULL || ns == 0)
2849 for (ns = 0; ns < (size_t)
_poptBitsK; ns++) {
2850 uint32_t h = h0 + (uint32_t)ns * h1;
2870 for (i = 0; i < nw; i++) {
2871 abits[i] &= bbits[i];
2874 return (rc ? 1 : 0);
2890 for (i = 0; i < nw; i++) {
2891 abits[i] |= bbits[i];
2894 return (rc ? 1 : 0);
2917 UNUSED(
unsigned int argInfo),
const char * s)
2928 while ((t = te) !=
NULL && *t) {
2929 while (*te !=
'\0' && *te !=
',')
2946 tbuf = (
char*)
_free(tbuf);
2951 UNUSED(
unsigned int argInfo),
const char * val)
2963 if ((*argvp = (
const char**)
xrealloc(*argvp, (
argc + 1 + 1) *
sizeof(**argvp))) !=
NULL) {
2975 if (aLongLong != 0 &&
LF_ISSET(RANDOM)) {
2980 aLongLong = ~aLongLong;
2986 *(
unsigned long long *)arg |= (
unsigned long long)aLongLong;
2989 *(
unsigned long long *)arg &= (
unsigned long long)aLongLong;
2992 *(
unsigned long long *)arg ^= (
unsigned long long)aLongLong;
3004 if (arg ==
NULL || (((
unsigned long)arg) & (
sizeof(*arg)-1)))
3007 if (aLong != 0 &&
LF_ISSET(RANDOM)) {
3014 case 0: *arg = aLong;
break;
3015 case POPT_ARGFLAG_OR: *(
unsigned long *)arg |= (
unsigned long)aLong;
break;
3016 case POPT_ARGFLAG_AND: *(
unsigned long *)arg &= (
unsigned long)aLong;
break;
3017 case POPT_ARGFLAG_XOR: *(
unsigned long *)arg ^= (
unsigned long)aLong;
break;
3028 if (arg ==
NULL || (((
unsigned long)arg) & (
sizeof(*arg)-1)))
3031 if (aLong != 0 &&
LF_ISSET(RANDOM)) {
3038 case 0: *arg = (int) aLong;
break;
3039 case POPT_ARGFLAG_OR: *(
unsigned int *)arg |= (
unsigned int) aLong;
break;
3040 case POPT_ARGFLAG_AND: *(
unsigned int *)arg &= (
unsigned int) aLong;
break;
3041 case POPT_ARGFLAG_XOR: *(
unsigned int *)arg ^= (
unsigned int) aLong;
break;
3052 if (arg ==
NULL || (((
unsigned long)arg) & (
sizeof(*arg)-1)))
3055 if (aLong != 0 &&
LF_ISSET(RANDOM)) {
3062 case 0: *arg = (short) aLong;
3064 case POPT_ARGFLAG_OR: *(
unsigned short *)arg |= (
unsigned short) aLong;
3084 unsigned int argInfo = opt->
argInfo;
3088 const char * longName = con->
os->
argv[con->
os->
next-1];
3089 while (*longName ==
'-') longName++;
3112 UNUSED(
unsigned int argInfo),
3117 *llp = strtoll(val, &
end, 0);
3121 if (!(
end && *
end ==
'\0'))
3167 rc = !(aNUM == LLONG_MIN || aNUM == LLONG_MAX)
3172 rc = !(aNUM < (long long)LONG_MIN || aNUM > (
long long)LONG_MAX)
3177 rc = !(aNUM < (long long)INT_MIN || aNUM > (
long long)INT_MAX)
3182 rc = !(aNUM < (long long)SHRT_MIN || aNUM > (
long long)SHRT_MAX)
3192 double aDouble = 0.0;
3195 int saveerrno = errno;
3198 if (errno == ERANGE) {
3214 #define POPT_ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
3215 if ((FLT_MIN -
POPT_ABS(aDouble)) > DBL_EPSILON
3216 || (
POPT_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
3219 arg.
floatp[0] = (float) aDouble;
3224 con->
maincall = (int (*)(int,
const char **))opt->
arg;
3227 fprintf(stdout,
POPT_(
"option type (%u) not implemented in popt\n"),
3244 const char * origOptString =
NULL;
3246 const void * cbData =
NULL;
3247 const char * longArg =
NULL;
3269 const char * optString;
3270 size_t optStringLen;
3281 if (origOptString ==
NULL)
3285 (*origOptString ==
'-' && origOptString[1] ==
'\0'))
3299 optString = origOptString;
3301 if (optString[0] ==
'\0')
3304 if (optString[1] ==
'-' && !optString[2]) {
3312 if (*optString ==
'-')
3318 for (oe = optString; *oe && *oe !=
'='; oe++)
3320 optStringLen = (size_t)(oe - optString);
3325 if (
handleAlias(con, optString, optStringLen,
'\0', longArg)) {
3364 if (*nextCharArg !=
'\0')
3376 if (*nextCharArg !=
'\0')
3377 con->
os->
nextCharArg = nextCharArg + (int)(*nextCharArg ==
'=');
3419 &&
F_ISSET(opt, STRIP) && canstrip)
3456 {
char *s = (
char*)malloc((opt->
longName ? strlen(opt->
longName) : 0) +
sizeof(
"--"));
3481 return (opt ? opt->
val : -1);
3496 const char * ret =
NULL;
3504 const char * ret =
NULL;
3525 if (items !=
NULL) {
3527 while (--nitems >= 0) {
3541 if (con ==
NULL)
return con;
3567 memset(item, 0,
sizeof(*item));
3568 item->option.longName = alias.
longName;
3569 item->option.shortName = alias.
shortName;
3571 item->option.arg = 0;
3572 item->option.val = 0;
3573 item->option.descrip =
NULL;
3574 item->option.argDescrip =
NULL;
3575 item->argc = alias.
argc;
3576 item->argv = alias.
argv;
3587 items = &con->
execs;
3599 *items = (
poptItem)realloc((*items), ((*nitems) + 1) *
sizeof(**items));
3600 if ((*items) ==
NULL)
3603 item = (*items) + (*nitems);
3637 return POPT_(
"missing argument");
3639 return POPT_(
"unknown option");
3641 return POPT_(
"mutually exclusive logical operations requested");
3643 return POPT_(
"opt->arg should not be NULL");
3645 return POPT_(
"aliases nested too deeply");
3647 return POPT_(
"error in parameter quoting");
3649 return POPT_(
"invalid numeric value");
3651 return POPT_(
"number too large or too small");
3653 return POPT_(
"memory allocation failed");
3655 return POPT_(
"config file failed sanity test");
3657 return strerror(errno);
3659 return POPT_(
"unknown error");
3698 for (i = 1; i <
argc; i++) {
3703 for (i = 1; i <
argc; i++) {