Removed draw_double_box() function.
[midnight-commander.git] / src / poptconfig.c
blobee54c9fd08e7a9dba4af33a06d962031e1414188
1 /* (C) 1998 Red Hat Software, Inc. -- Licensing details are in the COPYING
2 file accompanying popt source distributions, available from
3 ftp://ftp.redhat.com/pub/code/popt */
5 /** \file poptconfig.c
6 * \brief Source: a module for configuring popt
7 */
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
13 #include "poptalloca.h"
14 #include <ctype.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
21 #include "popt.h"
22 #include "poptint.h"
24 static void configLine(poptContext con, char * line) {
25 int nameLength = strlen(con->appName);
26 char * opt;
27 struct poptAlias alias;
28 char * entryType;
29 char * longName = NULL;
30 char shortName = '\0';
32 if (strncmp(line, con->appName, nameLength)) return;
33 line += nameLength;
34 if (!*line || !isspace((unsigned char) *line)) return;
35 while (*line && isspace((unsigned char) *line)) line++;
36 entryType = line;
38 while (!*line || !isspace((unsigned char) *line)) line++;
39 *line++ = '\0';
40 while (*line && isspace((unsigned char) *line)) line++;
41 if (!*line) return;
42 opt = line;
44 while (!*line || !isspace((unsigned char) *line)) line++;
45 *line++ = '\0';
46 while (*line && isspace((unsigned char) *line)) line++;
47 if (!*line) return;
49 if (opt[0] == '-' && opt[1] == '-')
50 longName = opt + 2;
51 else if (opt[0] == '-' && !opt[2])
52 shortName = opt[1];
54 if (!strcmp(entryType, "alias")) {
55 if (poptParseArgvString(line, &alias.argc, &alias.argv)) return;
56 alias.longName = longName, alias.shortName = shortName;
57 poptAddAlias(con, alias, 0);
58 } else if (!strcmp(entryType, "exec")) {
59 con->execs = realloc(con->execs,
60 sizeof(*con->execs) * (con->numExecs + 1));
61 if (longName)
62 con->execs[con->numExecs].longName = strdup(longName);
63 else
64 con->execs[con->numExecs].longName = NULL;
66 con->execs[con->numExecs].shortName = shortName;
67 con->execs[con->numExecs].script = strdup(line);
69 con->numExecs++;
73 int poptReadConfigFile(poptContext con, const char * fn) {
74 char * file, * chptr, * end;
75 char * buf, * dst;
76 int fd, rc;
77 int fileLength;
79 fd = open(fn, O_RDONLY);
80 if (fd < 0) {
81 if (errno == ENOENT)
82 return 0;
83 else
84 return POPT_ERROR_ERRNO;
87 fileLength = lseek(fd, 0, SEEK_END);
88 lseek(fd, 0, 0);
90 file = alloca(fileLength + 1);
91 if (read(fd, file, fileLength) != fileLength) {
92 rc = errno;
93 close(fd);
94 errno = rc;
95 return POPT_ERROR_ERRNO;
97 close(fd);
99 dst = buf = alloca(fileLength + 1);
101 chptr = file;
102 end = (file + fileLength);
103 while (chptr < end) {
104 switch (*chptr) {
105 case '\n':
106 *dst = '\0';
107 dst = buf;
108 while (*dst && isspace((unsigned char) *dst)) dst++;
109 if (*dst && *dst != '#') {
110 configLine(con, dst);
112 chptr++;
113 break;
114 case '\\':
115 *dst++ = *chptr++;
116 if (chptr < end) {
117 if (*chptr == '\n')
118 dst--, chptr++;
119 /* \ at the end of a line does not insert a \n */
120 else
121 *dst++ = *chptr++;
123 break;
124 default:
125 *dst++ = *chptr++;
129 return 0;
132 int poptReadDefaultConfig(poptContext con, int useEnv) {
133 char *fn;
134 const char* home;
135 int rc;
137 (void) useEnv;
139 if (!con->appName) return 0;
141 rc = poptReadConfigFile(con, "/etc/popt");
142 if (rc) return rc;
143 if (getuid() != geteuid()) return 0;
145 if ((home = getenv("HOME"))) {
146 fn = alloca(strlen(home) + 20);
147 strcpy(fn, home);
148 strcat(fn, "/.popt");
149 rc = poptReadConfigFile(con, fn);
150 if (rc) return rc;
153 return 0;