123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- #include "system.h"
- #include "poptint.h"
- #include <sys/stat.h>
- #if defined(HAVE_FNMATCH_H)
- #include <fnmatch.h>
- #if defined(__LCLINT__)
- extern int fnmatch (const char *__pattern, const char *__name, int __flags)
- ;
- #endif
- #endif
- #if defined(HAVE_GLOB_H)
- #include <glob.h>
- #if defined(__LCLINT__)
- extern int glob (const char *__pattern, int __flags,
- int (*__errfunc) (const char *, int),
- glob_t *__pglob)
-
- ;
- extern void globfree ( glob_t *__pglob)
- ;
- extern int glob_pattern_p (const char *__pattern, int __quote)
- ;
- #endif
- #if !defined(__GLIBC__)
- static int
- glob_pattern_p (const char * pattern, int quote)
-
- {
- const char * p;
- int open = 0;
- for (p = pattern; *p != '\0'; ++p)
- switch (*p) {
- case '?':
- case '*':
- return 1;
- break;
- case '\\':
- if (quote && p[1] != '\0')
- ++p;
- break;
- case '[':
- open = 1;
- break;
- case ']':
- if (open)
- return 1;
- break;
- }
- return 0;
- }
- #endif
- static int poptGlobFlags = 0;
- static int poptGlob_error( UNUSED(const char * epath),
- UNUSED(int eerrno))
-
- {
- return 1;
- }
- #endif
- static int poptGlob( UNUSED(poptContext con), const char * pattern,
- int * acp, const char *** avp)
-
- {
- const char * pat = pattern;
- int rc = 0;
-
- if (pat[0] == '@' && pat[1] != '(')
- pat++;
- #if defined(HAVE_GLOB_H)
- if (glob_pattern_p(pat, 0)) {
- glob_t _g, *pglob = &_g;
- if (!glob(pat, poptGlobFlags, poptGlob_error, pglob)) {
- if (acp) {
- *acp = (int) pglob->gl_pathc;
- pglob->gl_pathc = 0;
- }
- if (avp) {
- *avp = (const char **) pglob->gl_pathv;
- pglob->gl_pathv = NULL;
- }
- globfree(pglob);
- } else
- rc = POPT_ERROR_ERRNO;
- } else
- #endif
- {
- if (acp)
- *acp = 1;
- if (avp && (*avp = calloc((size_t)(1 + 1), sizeof (**avp))) != NULL)
- (*avp)[0] = xstrdup(pat);
- }
- return rc;
- }
- int poptSaneFile(const char * fn)
- {
- struct stat sb;
- uid_t uid = getuid();
- if (stat(fn, &sb) == -1)
- return 1;
- if ((uid_t)sb.st_uid != uid)
- return 0;
- if (!S_ISREG(sb.st_mode))
- return 0;
- if (sb.st_mode & (S_IWGRP|S_IWOTH))
- return 0;
- return 1;
- }
- int poptReadFile(const char * fn, char ** bp, size_t * nbp, int flags)
- {
- int fdno;
- char * b = NULL;
- off_t nb = 0;
- char * s, * t, * se;
- int rc = POPT_ERROR_ERRNO;
- fdno = open(fn, O_RDONLY);
- if (fdno < 0)
- goto exit;
- if ((nb = lseek(fdno, 0, SEEK_END)) == (off_t)-1
- || lseek(fdno, 0, SEEK_SET) == (off_t)-1
- || (b = calloc(sizeof(*b), (size_t)nb + 1)) == NULL
- || read(fdno, (char *)b, (size_t)nb) != (ssize_t)nb)
- {
- int oerrno = errno;
- (void) close(fdno);
- errno = oerrno;
- goto exit;
- }
- if (close(fdno) == -1)
- goto exit;
- if (b == NULL) {
- rc = POPT_ERROR_MALLOC;
- goto exit;
- }
- rc = 0;
-
- if (flags & POPT_READFILE_TRIMNEWLINES)
- {
- for (t = b, s = b, se = b + nb; *s && s < se; s++) {
- switch (*s) {
- case '\\':
- if (s[1] == '\n') {
- s++;
- continue;
- }
-
- default:
- *t++ = *s;
- break;
- }
- }
- *t++ = '\0';
- nb = (off_t)(t - b);
- }
- exit:
- if (rc != 0) {
- if (b)
- free(b);
- b = NULL;
- nb = 0;
- }
- if (bp)
- *bp = b;
- else if (b)
- free(b);
- if (nbp)
- *nbp = (size_t)nb;
-
- return rc;
- }
- static int configAppMatch(poptContext con, const char * s)
-
- {
- int rc = 1;
- if (con->appName == NULL)
- return rc;
- #if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
- if (glob_pattern_p(s, 1)) {
- static int flags = FNM_PATHNAME | FNM_PERIOD;
- #ifdef FNM_EXTMATCH
- flags |= FNM_EXTMATCH;
- #endif
- rc = fnmatch(s, con->appName, flags);
- } else
- #endif
- rc = strcmp(s, con->appName);
- return rc;
- }
-
- static int poptConfigLine(poptContext con, char * line)
-
-
- {
- char *b = NULL;
- size_t nb = 0;
- char * se = line;
- const char * appName;
- const char * entryType;
- const char * opt;
- struct poptItem_s item_buf;
- poptItem item = &item_buf;
- int i, j;
- int rc = POPT_ERROR_BADCONFIG;
- if (con->appName == NULL)
- goto exit;
-
- memset(item, 0, sizeof(*item));
- appName = se;
- while (*se != '\0' && !_isspaceptr(se)) se++;
- if (*se == '\0')
- goto exit;
- else
- *se++ = '\0';
- if (configAppMatch(con, appName)) goto exit;
- while (*se != '\0' && _isspaceptr(se)) se++;
- entryType = se;
- while (*se != '\0' && !_isspaceptr(se)) se++;
- if (*se != '\0') *se++ = '\0';
- while (*se != '\0' && _isspaceptr(se)) se++;
- if (*se == '\0') goto exit;
- opt = se;
- while (*se != '\0' && !_isspaceptr(se)) se++;
- if (opt[0] == '-' && *se == '\0') goto exit;
- if (*se != '\0') *se++ = '\0';
- while (*se != '\0' && _isspaceptr(se)) se++;
- if (opt[0] == '-' && *se == '\0') goto exit;
-
- if (opt[0] == '-' && opt[1] == '-')
- item->option.longName = opt + 2;
- else if (opt[0] == '-' && opt[2] == '\0')
- item->option.shortName = opt[1];
- else {
- const char * fn = opt;
-
- if ((rc = poptReadFile(fn, &b, &nb, POPT_READFILE_TRIMNEWLINES)) != 0)
- goto exit;
- if (b == NULL || nb == 0)
- goto exit;
-
- if (*se != '\0') {
- size_t nse = strlen(se) + 1;
- if ((b = realloc(b, (nb + nse))) == NULL)
- goto exit;
- (void) stpcpy( stpcpy(&b[nb-1], " "), se);
- nb += nse;
- }
- se = b;
-
- { const char * longName = strrchr(fn, '/');
- if (longName != NULL)
- longName++;
- else
- longName = fn;
- if (longName == NULL)
- goto exit;
-
- if (longName[1] != '\0')
- item->option.longName = longName;
- else
- item->option.shortName = longName[0];
- }
- }
- if (poptParseArgvString(se, &item->argc, &item->argv)) goto exit;
- item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
- for (i = 0, j = 0; i < item->argc; i++, j++) {
- const char * f;
- if (!strncmp(item->argv[i], "--POPTdesc=", sizeof("--POPTdesc=")-1)) {
- f = item->argv[i] + sizeof("--POPTdesc=");
- if (f[0] == '$' && f[1] == '"') f++;
- item->option.descrip = f;
- item->option.argInfo &= ~POPT_ARGFLAG_DOC_HIDDEN;
- j--;
- } else
- if (!strncmp(item->argv[i], "--POPTargs=", sizeof("--POPTargs=")-1)) {
- f = item->argv[i] + sizeof("--POPTargs=");
- if (f[0] == '$' && f[1] == '"') f++;
- item->option.argDescrip = f;
- item->option.argInfo &= ~POPT_ARGFLAG_DOC_HIDDEN;
- item->option.argInfo |= POPT_ARG_STRING;
- j--;
- } else
- if (j != i)
- item->argv[j] = item->argv[i];
- }
- if (j != i) {
- item->argv[j] = NULL;
- item->argc = j;
- }
-
-
- if (!strcmp(entryType, "alias"))
- rc = poptAddItem(con, item, 0);
- else if (!strcmp(entryType, "exec"))
- rc = poptAddItem(con, item, 1);
- exit:
- rc = 0;
- if (b)
- free(b);
- return rc;
- }
- int poptReadConfigFile(poptContext con, const char * fn)
- {
- char * b = NULL, *be;
- size_t nb = 0;
- const char *se;
- char *t, *te;
- int rc;
- int xx;
- if ((rc = poptReadFile(fn, &b, &nb, POPT_READFILE_TRIMNEWLINES)) != 0)
- return (errno == ENOENT ? 0 : rc);
- if (b == NULL || nb == 0)
- return POPT_ERROR_BADCONFIG;
- if ((t = malloc(nb + 1)) == NULL)
- goto exit;
- te = t;
- be = (b + nb);
- for (se = b; se < be; se++) {
- switch (*se) {
- case '\n':
- *te = '\0';
- te = t;
- while (*te && _isspaceptr(te)) te++;
- if (*te && *te != '#')
- xx = poptConfigLine(con, te);
- break;
-
- case '\\':
- *te = *se++;
-
- if (se < be && *se != '\n') {
- te++;
- *te++ = *se;
- }
- break;
- default:
- *te++ = *se;
- break;
- }
- }
- free(t);
- rc = 0;
- exit:
- if (b)
- free(b);
- return rc;
- }
- int poptReadConfigFiles(poptContext con, const char * paths)
- {
- char * buf = (paths ? xstrdup(paths) : NULL);
- const char * p;
- char * pe;
- int rc = 0;
- for (p = buf; p != NULL && *p != '\0'; p = pe) {
- const char ** av = NULL;
- int ac = 0;
- int i;
- int xx;
-
- pe = strchr(p, ':');
- if (pe != NULL && *pe == ':')
- *pe++ = '\0';
- else
- pe = (char *) (p + strlen(p));
- xx = poptGlob(con, p, &ac, &av);
-
- for (i = 0; i < ac; i++) {
- const char * fn = av[i];
- if (av[i] == NULL)
- continue;
-
- if (p[0] == '@' && p[1] != '(') {
- if (fn[0] == '@' && fn[1] != '(')
- fn++;
- xx = poptSaneFile(fn);
- if (!xx && rc == 0)
- rc = POPT_ERROR_BADCONFIG;
- continue;
- }
- xx = poptReadConfigFile(con, fn);
- if (xx && rc == 0)
- rc = xx;
- free((void *)av[i]);
- av[i] = NULL;
- }
- free(av);
- av = NULL;
- }
- if (buf)
- free(buf);
- return rc;
- }
- int poptReadDefaultConfig(poptContext con, UNUSED(int useEnv))
- {
- static const char _popt_sysconfdir[] = POPT_SYSCONFDIR "/popt";
- static const char _popt_etc[] = "/etc/popt";
- char * home;
- struct stat sb;
- int rc = 0;
- if (con->appName == NULL) goto exit;
- if (strcmp(_popt_sysconfdir, _popt_etc)) {
- rc = poptReadConfigFile(con, _popt_sysconfdir);
- if (rc) goto exit;
- }
- rc = poptReadConfigFile(con, _popt_etc);
- if (rc) goto exit;
- #if defined(HAVE_GLOB_H)
- if (!stat("/etc/popt.d", &sb) && S_ISDIR(sb.st_mode)) {
- const char ** av = NULL;
- int ac = 0;
- int i;
- if ((rc = poptGlob(con, "/etc/popt.d/*", &ac, &av)) == 0) {
- for (i = 0; rc == 0 && i < ac; i++) {
- const char * fn = av[i];
- if (fn == NULL || strstr(fn, ".rpmnew") || strstr(fn, ".rpmsave"))
- continue;
- if (!stat(fn, &sb)) {
- if (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))
- continue;
- }
- rc = poptReadConfigFile(con, fn);
- free((void *)av[i]);
- av[i] = NULL;
- }
- free(av);
- av = NULL;
- }
- }
- if (rc) goto exit;
- #endif
- if ((home = getenv("HOME"))) {
- char * fn = malloc(strlen(home) + 20);
- if (fn != NULL) {
- (void) stpcpy(stpcpy(fn, home), "/.popt");
- rc = poptReadConfigFile(con, fn);
- free(fn);
- } else
- rc = POPT_ERROR_ERRNO;
- if (rc) goto exit;
- }
- exit:
- return rc;
- }
- poptContext
- poptFini(poptContext con)
- {
- return poptFreeContext(con);
- }
- poptContext
- poptInit(int argc, const char ** argv,
- const struct poptOption * options, const char * configPaths)
- {
- poptContext con = NULL;
- const char * argv0;
- if (argv == NULL || argv[0] == NULL || options == NULL)
- return con;
- if ((argv0 = strrchr(argv[0], '/')) != NULL) argv0++;
- else argv0 = argv[0];
-
- con = poptGetContext(argv0, argc, (const char **)argv, options, 0);
- if (con != NULL&& poptReadConfigFiles(con, configPaths))
- con = poptFini(con);
- return con;
- }
|