00001
00005
00006
00007
00008
00009 #include "system.h"
00010 #include "poptint.h"
00011
00012
00013 static void configLine(poptContext con, char * line)
00014
00015 {
00016 int nameLength = strlen(con->appName);
00017 const char * entryType;
00018 const char * opt;
00019 poptItem item = alloca(sizeof(*item));
00020 int i, j;
00021
00022 memset(item, 0, sizeof(*item));
00023
00024 if (strncmp(line, con->appName, nameLength)) return;
00025 line += nameLength;
00026 if (*line == '\0' || !isspace(*line)) return;
00027
00028 while (*line != '\0' && isspace(*line)) line++;
00029 entryType = line;
00030 while (*line == '\0' || !isspace(*line)) line++;
00031 *line++ = '\0';
00032
00033 while (*line != '\0' && isspace(*line)) line++;
00034 if (*line == '\0') return;
00035 opt = line;
00036 while (*line == '\0' || !isspace(*line)) line++;
00037 *line++ = '\0';
00038
00039 while (*line != '\0' && isspace(*line)) line++;
00040 if (*line == '\0') return;
00041
00042 if (opt[0] == '-' && opt[1] == '-')
00043 item->option.longName = opt + 2;
00044 else if (opt[0] == '-' && opt[2] == '\0')
00045 item->option.shortName = opt[1];
00046
00047 if (poptParseArgvString(line, &item->argc, &item->argv)) return;
00048
00049
00050 item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
00051 for (i = 0, j = 0; i < item->argc; i++, j++) {
00052 const char * f;
00053 if (!strncmp(item->argv[i], "--POPTdesc=", sizeof("--POPTdesc=")-1)) {
00054 f = item->argv[i] + sizeof("--POPTdesc=");
00055 if (f[0] == '$' && f[1] == '"') f++;
00056 item->option.descrip = f;
00057 item->option.argInfo &= ~POPT_ARGFLAG_DOC_HIDDEN;
00058 j--;
00059 } else
00060 if (!strncmp(item->argv[i], "--POPTargs=", sizeof("--POPTargs=")-1)) {
00061 f = item->argv[i] + sizeof("--POPTargs=");
00062 if (f[0] == '$' && f[1] == '"') f++;
00063 item->option.argDescrip = f;
00064 item->option.argInfo &= ~POPT_ARGFLAG_DOC_HIDDEN;
00065 item->option.argInfo |= POPT_ARG_STRING;
00066 j--;
00067 } else
00068 if (j != i)
00069 item->argv[j] = item->argv[i];
00070 }
00071 if (j != i) {
00072 item->argv[j] = NULL;
00073 item->argc = j;
00074 }
00075
00076
00077
00078 if (!strcmp(entryType, "alias"))
00079 (void) poptAddItem(con, item, 0);
00080 else if (!strcmp(entryType, "exec"))
00081 (void) poptAddItem(con, item, 1);
00082 }
00083
00084
00085 int poptReadConfigFile(poptContext con, const char * fn)
00086 {
00087 const char * file, * chptr, * end;
00088 char * buf;
00089 char * dst;
00090 int fd, rc;
00091 off_t fileLength;
00092
00093 fd = open(fn, O_RDONLY);
00094 if (fd < 0)
00095 return (errno == ENOENT ? 0 : POPT_ERROR_ERRNO);
00096
00097 fileLength = lseek(fd, 0, SEEK_END);
00098 if (fileLength == -1 || lseek(fd, 0, 0) == -1) {
00099 rc = errno;
00100 (void) close(fd);
00101 errno = rc;
00102 return POPT_ERROR_ERRNO;
00103 }
00104
00105 file = alloca(fileLength + 1);
00106 if (read(fd, (char *)file, fileLength) != fileLength) {
00107 rc = errno;
00108 (void) close(fd);
00109 errno = rc;
00110 return POPT_ERROR_ERRNO;
00111 }
00112 if (close(fd) == -1)
00113 return POPT_ERROR_ERRNO;
00114
00115 dst = buf = alloca(fileLength + 1);
00116
00117 chptr = file;
00118 end = (file + fileLength);
00119
00120 while (chptr < end) {
00121 switch (*chptr) {
00122 case '\n':
00123 *dst = '\0';
00124 dst = buf;
00125 while (*dst && isspace(*dst)) dst++;
00126 if (*dst && *dst != '#')
00127 configLine(con, dst);
00128 chptr++;
00129 break;
00130 case '\\':
00131 *dst++ = *chptr++;
00132 if (chptr < end) {
00133 if (*chptr == '\n')
00134 dst--, chptr++;
00135
00136 else
00137 *dst++ = *chptr++;
00138 }
00139 break;
00140 default:
00141 *dst++ = *chptr++;
00142 break;
00143 }
00144 }
00145
00146
00147 return 0;
00148 }
00149
00150 int poptReadDefaultConfig(poptContext con, int useEnv) {
00151 char * fn, * home;
00152 int rc;
00153
00154 if (!con->appName) return 0;
00155
00156 rc = poptReadConfigFile(con, "/etc/popt");
00157 if (rc) return rc;
00158 if (getuid() != geteuid()) return 0;
00159
00160 if ((home = getenv("HOME"))) {
00161 fn = alloca(strlen(home) + 20);
00162 strcpy(fn, home);
00163 strcat(fn, "/.popt");
00164 rc = poptReadConfigFile(con, fn);
00165 if (rc) return rc;
00166 }
00167
00168 return 0;
00169 }