fixed creation mc-wrapper.csh & mc-wrapper.sh
[midnight-commander.git] / src / profile.c
blob6f6fba100581d75b2ee8fabab63205661385aa2a
1 /*
2 * Initialization-File Functions.
4 * From the Wine project
6 Copyright (C) 1993, 1994 Miguel de Icaza.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include "global.h"
28 #include "profile.h"
30 #define STRSIZE 4096
31 #define overflow (next == &CharBuffer [STRSIZE-1])
33 enum { FirstBrace, OnSecHeader, IgnoreToEOL, KeyDef, KeyDefOnKey, KeyValue };
35 typedef struct TKeys {
36 char *KeyName;
37 char *Value;
38 struct TKeys *link;
39 } TKeys;
41 typedef struct TSecHeader {
42 char *AppName;
43 TKeys *Keys;
44 struct TSecHeader *link;
45 } TSecHeader;
47 typedef struct TProfile {
48 char *FileName;
49 TSecHeader *Section;
50 struct TProfile *link;
51 } TProfile;
53 static TProfile *Base = 0;
55 static TProfile *
56 find_loaded (char *FileName, TSecHeader ** section)
58 TProfile *p = Base;
60 while (p) {
61 if (!g_strcasecmp (FileName, p->FileName)) {
62 *section = p->Section;
63 return p;
65 p = p->link;
67 return NULL;
70 #define TRANSLATION_CHAR '\200'
72 static char *
73 str_untranslate_newline_dup (char *s)
75 int l = 0;
76 char *p = s, *q;
77 g_return_val_if_fail(s, NULL);
78 while (*p) {
79 l++;
80 l += (*p == '\n' || *p == TRANSLATION_CHAR);
81 p++;
83 q = p = g_malloc (l + 1);
84 if (!q)
85 return 0;
86 for (;;) {
87 switch (*s) {
88 case '\n':
89 *p++ = TRANSLATION_CHAR;
90 *p++ = 'n';
91 break;
92 case TRANSLATION_CHAR:
93 if (s[1] == 'n' || s[1] == TRANSLATION_CHAR)
94 *p++ = TRANSLATION_CHAR;
95 *p++ = TRANSLATION_CHAR;
96 break;
97 case '\0':
98 *p = '\0';
99 return q;
100 break;
101 default:
102 *p++ = *s;
104 s++;
106 return 0; /* not reached */
109 static char *
110 str_translate_newline_dup (char *s)
112 char *p, *q;
113 g_return_val_if_fail(s,NULL);
114 q = p = g_malloc (strlen (s) + 1);
115 if (!q)
116 return 0;
117 while (*s) {
118 if (*s == TRANSLATION_CHAR) {
119 switch (*(++s)) {
120 case 'n':
121 *p++ = '\n';
122 break;
123 case TRANSLATION_CHAR:
124 *p++ = TRANSLATION_CHAR;
125 break;
126 case '\0':
127 *p++ = TRANSLATION_CHAR;
128 *p++ = '\0';
129 return q;
130 default:
131 *p++ = TRANSLATION_CHAR;
132 *p++ = *s;
134 } else {
135 *p++ = *s;
137 s++;
139 *p = '\0';
140 return q; /* not reached */
143 static TSecHeader *load (char *file)
145 FILE *f;
146 int state;
147 TSecHeader *SecHeader = 0;
148 char CharBuffer [STRSIZE];
149 char *next = ""; /* Not needed */
150 int c;
152 if ((f = fopen (file, "r"))==NULL)
153 return NULL;
155 state = FirstBrace;
156 while ((c = getc (f)) != EOF){
157 if (c == '\r') /* Ignore Carriage Return */
158 continue;
160 switch (state){
162 case OnSecHeader:
163 if (c == ']' || overflow){
164 *next = '\0';
165 next = CharBuffer;
166 SecHeader->AppName = g_strdup (CharBuffer);
167 state = IgnoreToEOL;
168 } else
169 *next++ = c;
170 break;
172 case IgnoreToEOL:
173 if (c == '\n'){
174 state = KeyDef;
175 next = CharBuffer;
177 break;
179 case FirstBrace:
180 case KeyDef:
181 case KeyDefOnKey:
182 if (c == '['){
183 TSecHeader *temp;
185 temp = SecHeader;
186 SecHeader = g_new (TSecHeader, 1);
187 SecHeader->link = temp;
188 SecHeader->Keys = 0;
189 state = OnSecHeader;
190 next = CharBuffer;
191 break;
193 if (state == FirstBrace) /* On first pass, don't allow dangling keys */
194 break;
196 if ((c == ' ' && state != KeyDefOnKey) || c == '\t')
197 break;
199 if (c == '\n' || overflow) /* Abort Definition */
200 next = CharBuffer;
202 if (c == '=' || overflow){
203 TKeys *temp;
205 temp = SecHeader->Keys;
206 *next = '\0';
207 SecHeader->Keys =g_new (TKeys, 1);
208 SecHeader->Keys->link = temp;
209 SecHeader->Keys->KeyName = g_strdup (CharBuffer);
210 state = KeyValue;
211 next = CharBuffer;
212 } else {
213 *next++ = c;
214 state = KeyDefOnKey;
216 break;
218 case KeyValue:
219 if (overflow || c == '\n'){
220 *next = '\0';
221 SecHeader->Keys->Value = str_translate_newline_dup (CharBuffer);
222 state = c == '\n' ? KeyDef : IgnoreToEOL;
223 next = CharBuffer;
224 #ifdef DEBUG
225 printf ("[%s] (%s)=%s\n", SecHeader->AppName,
226 SecHeader->Keys->KeyName, SecHeader->Keys->Value);
227 #endif
228 } else
229 *next++ = c;
230 break;
232 } /* switch */
234 } /* while ((c = getc (f)) != EOF) */
235 if (c == EOF && state == KeyValue){
236 *next = '\0';
237 SecHeader->Keys->Value = str_translate_newline_dup (CharBuffer);
239 fclose (f);
240 return SecHeader;
243 static void new_key (TSecHeader *section, char *KeyName, char *Value)
245 TKeys *key;
247 key = g_new (TKeys, 1);
248 key->KeyName = g_strdup (KeyName);
249 key->Value = g_strdup (Value);
250 key->link = section->Keys;
251 section->Keys = key;
254 static char *
255 GetSetProfileChar (int set, const char *AppName, char *KeyName,
256 char *Default, char *FileName)
259 TProfile *Current;
260 TSecHeader *section;
261 TKeys *key;
263 Current = find_loaded (FileName, &section);
264 if (!Current) {
265 Current = g_new (TProfile, 1);
266 Current->link = Base;
267 Current->FileName = g_strdup (FileName);
268 Current->Section = load (FileName);
269 Base = Current;
270 section = Current->Section;
273 /* Start search */
274 for (; section; section = section->link){
275 if (section->AppName == 0 || g_strcasecmp (section->AppName, AppName))
276 continue;
277 for (key = section->Keys; key; key = key->link){
278 if ( g_strcasecmp (key->KeyName, KeyName))
279 continue;
280 if (set){
281 g_free (key->Value);
282 key->Value = g_strdup (Default);
284 return key->Value;
286 /* If getting the information, then don't write the information
287 to the INI file, need to run a couple of tests with windog */
288 /* No key found */
289 if (set){
290 new_key (section, KeyName, Default);
291 return 0;
295 /* Non existent section */
296 if (set && Default){
297 section = g_new (TSecHeader, 1);
298 section->AppName = g_strdup (AppName);
299 section->Keys = 0;
300 new_key (section, KeyName, Default);
301 section->link = Current->Section;
302 Current->Section = section;
304 return Default;
307 static short GetSetProfile (int set, const char * AppName, char * KeyName,
308 char * Default, char * ReturnedString,
309 short Size, char * FileName)
312 char *s;
314 s = GetSetProfileChar (set, AppName, KeyName, Default, FileName);
315 if (!set){
316 ReturnedString [Size-1] = 0;
317 strncpy (ReturnedString, s, Size-1);
319 return 1;
322 short GetPrivateProfileString (const char * AppName, char * KeyName,
323 char * Default, char * ReturnedString,
324 short Size, char * FileName)
326 return (GetSetProfile (0, AppName, KeyName, Default, ReturnedString, Size, FileName));
329 char *get_profile_string (const char *AppName, char *KeyName, char *Default,
330 char *FileName)
332 return GetSetProfileChar (0, AppName, KeyName, Default, FileName);
335 int GetPrivateProfileInt (const char * AppName, char * KeyName, int Default,
336 char * File)
338 static char IntBuf [BUF_TINY];
339 static char buf [BUF_TINY];
341 g_snprintf (buf, sizeof (buf), "%d", Default);
343 /* Check the exact semantic with the SDK */
344 GetPrivateProfileString (AppName, KeyName, buf, IntBuf, BUF_TINY, File);
345 if (! g_strcasecmp (IntBuf, "true"))
346 return 1;
347 if (! g_strcasecmp (IntBuf, "yes"))
348 return 1;
349 return (int) atol (IntBuf);
352 int WritePrivateProfileString (const char * AppName, char * KeyName, char * String,
353 char * FileName)
355 return GetSetProfile (1, AppName, KeyName, String, "", 0, FileName);
358 static void dump_keys (FILE * profile, TKeys * p)
360 char *t;
361 if (!p)
362 return;
363 dump_keys (profile, p->link);
364 t = str_untranslate_newline_dup (p->Value);
365 fprintf (profile, "%s=%s\n", p->KeyName, t);
366 g_free (t);
369 static void dump_sections (FILE *profile, TSecHeader *p)
371 if (!p)
372 return;
373 dump_sections (profile, p->link);
374 if (p->AppName [0]){
375 fprintf (profile, "\n[%s]\n", p->AppName);
376 dump_keys (profile, p->Keys);
380 static void dump_profile (TProfile *p)
382 FILE *profile;
384 if (!p)
385 return;
386 dump_profile (p->link);
387 /* .ado: p->FileName can be empty, it's better to jump over */
388 if (p->FileName[0] != (char) 0)
389 if ((profile = fopen (p->FileName, "w")) != NULL){
390 dump_sections (profile, p->Section);
391 fclose (profile);
396 * Must be called at the end of wine run
399 void sync_profiles (void)
401 dump_profile (Base);
404 static void free_keys (TKeys *p)
406 if (!p)
407 return;
408 free_keys (p->link);
409 g_free (p->KeyName);
410 g_free (p->Value);
411 g_free (p);
414 static void free_sections (TSecHeader *p)
416 if (!p)
417 return;
418 free_sections (p->link);
419 free_keys (p->Keys);
420 g_free (p->AppName);
421 p->link = 0;
422 p->Keys = 0;
423 g_free (p);
426 static void free_profile (TProfile *p)
428 if (!p)
429 return;
430 free_profile (p->link);
431 free_sections (p->Section);
432 g_free (p->FileName);
433 g_free (p);
436 void free_profile_name (char *s)
438 TProfile *p;
440 if (!s)
441 return;
443 for (p = Base; p; p = p->link){
444 if (strcmp (s, p->FileName) == 0){
445 free_sections (p->Section);
446 p->Section = 0;
447 p->FileName [0] = 0;
448 return;
453 void free_profiles (void)
455 free_profile (Base);
458 void *profile_init_iterator (char *appname, char *file)
460 TProfile *Current;
461 TSecHeader *section;
463 Current = find_loaded (file, &section);
464 if (!Current) {
465 Current = g_new (TProfile, 1);
466 Current->link = Base;
467 Current->FileName = g_strdup (file);
468 Current->Section = load (file);
469 Base = Current;
470 section = Current->Section;
472 for (; section; section = section->link){
473 if ( g_strcasecmp (section->AppName, appname))
474 continue;
475 return section->Keys;
477 return 0;
480 void *profile_iterator_next (void *s, char **key, char **value)
482 TKeys *keys = (TKeys *) s;
484 if (keys){
485 *key = keys->KeyName;
486 *value = keys->Value;
487 keys = keys->link;
489 return keys;
492 void profile_clean_section (char *appname, char *file)
494 TSecHeader *section;
496 /* We assume the user has called one of the other initialization funcs */
497 if (!find_loaded (file, &section)){
498 fprintf (stderr,"Warning: profile_clean_section called before init\n");
499 return;
501 /* We only disable the section, so it will still be freed, but it */
502 /* won't be find by further walks of the structure */
504 for (; section; section = section->link){
505 if ( g_strcasecmp (section->AppName, appname))
506 continue;
507 section->AppName [0] = 0;
511 int profile_has_section (char *section_name, char *profile)
513 TSecHeader *section;
515 /* We assume the user has called one of the other initialization funcs */
516 if (!find_loaded (profile, &section)){
517 return 0;
519 for (; section; section = section->link){
520 if ( g_strcasecmp (section->AppName, section_name))
521 continue;
522 return 1;
524 return 0;
527 void profile_forget_profile (char *file)
529 TProfile *p;
531 for (p = Base; p; p = p->link){
532 if ( g_strcasecmp (file, p->FileName))
533 continue;
534 p->FileName [0] = 0;