Just a little correction at the it.po file.
[midnight-commander.git] / src / poptconfig.c
blobfa607767a2c2e881b10a382f1153416b95cd4f1b
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 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
9 #include "poptalloca.h"
10 #include <ctype.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
17 #include "popt.h"
18 #include "poptint.h"
20 static void configLine(poptContext con, unsigned char * line) {
21 int nameLength = strlen(con->appName);
22 char * opt;
23 struct poptAlias alias;
24 char * entryType;
25 char * longName = NULL;
26 char shortName = '\0';
28 if (strncmp(line, con->appName, nameLength)) return;
29 line += nameLength;
30 if (!*line || !isspace(*line)) return;
31 while (*line && isspace(*line)) line++;
32 entryType = line;
34 while (!*line || !isspace(*line)) line++;
35 *line++ = '\0';
36 while (*line && isspace(*line)) line++;
37 if (!*line) return;
38 opt = line;
40 while (!*line || !isspace(*line)) line++;
41 *line++ = '\0';
42 while (*line && isspace(*line)) line++;
43 if (!*line) return;
45 if (opt[0] == '-' && opt[1] == '-')
46 longName = opt + 2;
47 else if (opt[0] == '-' && !opt[2])
48 shortName = opt[1];
50 if (!strcmp(entryType, "alias")) {
51 if (poptParseArgvString(line, &alias.argc, &alias.argv)) return;
52 alias.longName = longName, alias.shortName = shortName;
53 poptAddAlias(con, alias, 0);
54 } else if (!strcmp(entryType, "exec")) {
55 con->execs = realloc(con->execs,
56 sizeof(*con->execs) * (con->numExecs + 1));
57 if (longName)
58 con->execs[con->numExecs].longName = strdup(longName);
59 else
60 con->execs[con->numExecs].longName = NULL;
62 con->execs[con->numExecs].shortName = shortName;
63 con->execs[con->numExecs].script = strdup(line);
65 con->numExecs++;
69 int poptReadConfigFile(poptContext con, char * fn) {
70 char * file, * chptr, * end;
71 char * buf, * dst;
72 int fd, rc;
73 int fileLength;
75 fd = open(fn, O_RDONLY);
76 if (fd < 0) {
77 if (errno == ENOENT)
78 return 0;
79 else
80 return POPT_ERROR_ERRNO;
83 fileLength = lseek(fd, 0, SEEK_END);
84 lseek(fd, 0, 0);
86 file = alloca(fileLength + 1);
87 if (read(fd, file, fileLength) != fileLength) {
88 rc = errno;
89 close(fd);
90 errno = rc;
91 return POPT_ERROR_ERRNO;
93 close(fd);
95 dst = buf = alloca(fileLength + 1);
97 chptr = file;
98 end = (file + fileLength);
99 while (chptr < end) {
100 switch (*chptr) {
101 case '\n':
102 *dst = '\0';
103 dst = buf;
104 while (*dst && isspace((unsigned)*dst)) dst++;
105 if (*dst && *dst != '#') {
106 configLine(con, dst);
108 chptr++;
109 break;
110 case '\\':
111 *dst++ = *chptr++;
112 if (chptr < end) {
113 if (*chptr == '\n')
114 dst--, chptr++;
115 /* \ at the end of a line does not insert a \n */
116 else
117 *dst++ = *chptr++;
119 break;
120 default:
121 *dst++ = *chptr++;
125 return 0;
128 int poptReadDefaultConfig(poptContext con, int useEnv) {
129 char * fn, * home;
130 int rc;
132 if (!con->appName) return 0;
134 rc = poptReadConfigFile(con, "/etc/popt");
135 if (rc) return rc;
136 if (getuid() != geteuid()) return 0;
138 if ((home = getenv("HOME"))) {
139 fn = alloca(strlen(home) + 20);
140 strcpy(fn, home);
141 strcat(fn, "/.popt");
142 rc = poptReadConfigFile(con, fn);
143 if (rc) return rc;
146 return 0;