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