* bus/desktop-file.c (parse_key_value): Ignore locales allowing
[platform/upstream/dbus.git] / bus / desktop-file.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* desktop-file.c  .desktop file parser
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  * Copyright (C) 2003  Red Hat Inc.
6  *
7  * Licensed under the Academic Free License version 2.1
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24 #include <dbus/dbus-sysdeps.h>
25 #include <dbus/dbus-internals.h>
26 #include "desktop-file.h"
27 #include "utils.h"
28
29 typedef struct
30 {
31   char *key;
32   char *value;
33 } BusDesktopFileLine;
34
35 typedef struct
36 {
37   char *section_name;
38   
39   int n_lines;
40   BusDesktopFileLine *lines;
41   int n_allocated_lines;  
42 } BusDesktopFileSection;
43
44 struct BusDesktopFile
45 {
46   int n_sections;
47   BusDesktopFileSection *sections;
48   int n_allocated_sections;
49 };
50
51 /**
52  * Parser for service files.
53  */
54 typedef struct
55 {
56   DBusString data; /**< The data from the file */
57
58   BusDesktopFile *desktop_file; /**< The resulting object */
59   int current_section;    /**< The current section being parsed */
60   
61   int pos;          /**< Current position */
62   int len;          /**< Length */
63   int line_num;     /**< Current line number */
64   
65 } BusDesktopFileParser;
66
67 #define VALID_KEY_CHAR 1
68 #define VALID_LOCALE_CHAR 2
69 unsigned char valid[256] = { 
70    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0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, 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
82    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
83    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
84    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
85    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
86 };
87
88 static void report_error (BusDesktopFileParser *parser,
89                           char                 *message,
90                           const char           *error_name,
91                           DBusError            *error);
92
93 static void
94 parser_free (BusDesktopFileParser *parser)
95 {
96   bus_desktop_file_free (parser->desktop_file);
97   
98   _dbus_string_free (&parser->data);
99 }
100
101 static void
102 bus_desktop_file_line_free (BusDesktopFileLine *line)
103 {
104   dbus_free (line->key);
105   dbus_free (line->value);
106 }
107
108 static void
109 bus_desktop_file_section_free (BusDesktopFileSection *section)
110 {
111   int i;
112
113   for (i = 0; i < section->n_lines; i++)
114     bus_desktop_file_line_free (&section->lines[i]);
115
116   dbus_free (section->lines);
117   dbus_free (section->section_name);
118 }
119
120 void
121 bus_desktop_file_free (BusDesktopFile *desktop_file)
122 {
123   int i;
124
125   for (i = 0; i < desktop_file->n_sections; i++)
126     bus_desktop_file_section_free (&desktop_file->sections[i]);
127   dbus_free (desktop_file->sections);
128
129   dbus_free (desktop_file);
130 }
131
132 static dbus_bool_t
133 grow_lines_in_section (BusDesktopFileSection *section)
134 {
135   BusDesktopFileLine *lines;
136   
137   int new_n_lines;
138
139   if (section->n_allocated_lines == 0)
140     new_n_lines = 1;
141   else
142     new_n_lines = section->n_allocated_lines*2;
143
144   lines = dbus_realloc (section->lines,
145                         sizeof (BusDesktopFileLine) * new_n_lines);
146
147   if (lines == NULL)
148     return FALSE;
149   
150   section->lines = lines;
151   section->n_allocated_lines = new_n_lines;
152
153   return TRUE;
154 }
155
156 static dbus_bool_t
157 grow_sections (BusDesktopFile *desktop_file)
158 {
159   int new_n_sections;
160   BusDesktopFileSection *sections;
161   
162   if (desktop_file->n_allocated_sections == 0)
163     new_n_sections = 1;
164   else
165     new_n_sections = desktop_file->n_allocated_sections*2;
166
167   sections = dbus_realloc (desktop_file->sections,
168                            sizeof (BusDesktopFileSection) * new_n_sections);
169   if (sections == NULL)
170     return FALSE;
171   
172   desktop_file->sections = sections;
173   
174   desktop_file->n_allocated_sections = new_n_sections;
175
176   return TRUE;
177 }
178
179 static char *
180 unescape_string (BusDesktopFileParser *parser,
181                  const DBusString     *str,
182                  int                   pos,
183                  int                   end_pos,
184                  DBusError            *error)
185 {
186   char *retval, *q;
187
188   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
189   
190   /* len + 1 is enough, because unescaping never makes the
191    * string longer
192    */
193   retval = dbus_malloc (end_pos - pos + 1);
194   if (retval == NULL)
195     {
196       BUS_SET_OOM (error);
197       return NULL;
198     }
199
200   q = retval;
201   
202   while (pos < end_pos)
203     {
204       if (_dbus_string_get_byte (str, pos) == 0)
205         {
206           /* Found an embedded null */
207           dbus_free (retval);
208           report_error (parser, "Text to be unescaped contains embedded nul",
209                         BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
210           return NULL;
211         }
212
213       if (_dbus_string_get_byte (str, pos) == '\\')
214         {
215           pos ++;
216
217           if (pos >= end_pos)
218             {
219               /* Escape at end of string */
220               dbus_free (retval);
221               report_error (parser, "Text to be unescaped ended in \\",
222                             BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
223               return NULL;
224             }
225
226           switch (_dbus_string_get_byte (str, pos))
227             {
228             case 's':
229               *q++ = ' ';
230               break;
231            case 't':
232               *q++ = '\t';
233               break;
234            case 'n':
235               *q++ = '\n';
236               break;
237            case 'r':
238               *q++ = '\r';
239               break;
240            case '\\':
241               *q++ = '\\';
242               break;
243            default:
244              /* Invalid escape code */
245              dbus_free (retval);
246              report_error (parser, "Text to be unescaped had invalid escape sequence",
247                            BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
248              return NULL;
249             }
250           pos++;
251         }
252       else
253         {
254           *q++ =_dbus_string_get_byte (str, pos);
255
256           pos++;
257         }
258     }
259
260   *q = 0;
261
262   return retval;
263 }
264
265 static BusDesktopFileSection* 
266 new_section (BusDesktopFile *desktop_file,
267              const char     *name)
268 {
269   int n;
270   char *name_copy;
271   
272   if (desktop_file->n_allocated_sections == desktop_file->n_sections)
273     {
274       if (!grow_sections (desktop_file))
275         return NULL;
276     }
277
278   name_copy = _dbus_strdup (name);
279   if (name_copy == NULL)
280     return NULL;
281
282   n = desktop_file->n_sections;
283   desktop_file->sections[n].section_name = name_copy;
284
285   desktop_file->sections[n].n_lines = 0;
286   desktop_file->sections[n].lines = NULL;
287   desktop_file->sections[n].n_allocated_lines = 0;
288
289   if (!grow_lines_in_section (&desktop_file->sections[n]))
290     {
291       dbus_free (desktop_file->sections[n].section_name);
292       desktop_file->sections[n].section_name = NULL;
293       return NULL;
294     }
295
296   desktop_file->n_sections += 1;
297   
298   return &desktop_file->sections[n];  
299 }
300
301 static BusDesktopFileSection* 
302 open_section (BusDesktopFileParser *parser,
303               char                 *name)
304 {  
305   BusDesktopFileSection *section;
306
307   section = new_section (parser->desktop_file, name);
308   if (section == NULL)
309     return NULL;
310   
311   parser->current_section = parser->desktop_file->n_sections - 1;
312   _dbus_assert (&parser->desktop_file->sections[parser->current_section] == section);
313   
314   return section;
315 }
316
317 static BusDesktopFileLine *
318 new_line (BusDesktopFileParser *parser)
319 {
320   BusDesktopFileSection *section;
321   BusDesktopFileLine *line;
322   
323   section = &parser->desktop_file->sections[parser->current_section];
324
325   if (section->n_allocated_lines == section->n_lines)
326     {
327       if (!grow_lines_in_section (section))
328         return NULL;
329     }
330
331   line = &section->lines[section->n_lines++];
332
333   memset (line, 0, sizeof (BusDesktopFileLine));
334     
335   return line;
336 }
337
338 static dbus_bool_t
339 is_blank_line (BusDesktopFileParser *parser)
340 {
341   int p;
342   char c;
343   
344   p = parser->pos;
345
346   c = _dbus_string_get_byte (&parser->data, p);
347
348   while (c && c != '\n')
349     {
350       if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'))
351         return FALSE;
352       
353       p++;
354       c = _dbus_string_get_byte (&parser->data, p);
355     }
356
357   return TRUE;
358 }
359
360 static void
361 parse_comment_or_blank (BusDesktopFileParser *parser)
362 {
363   int line_end;
364   
365   if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
366     line_end = parser->len;
367
368   if (line_end == parser->len)
369     parser->pos = parser->len;
370   else
371     parser->pos = line_end + 1;
372   
373   parser->line_num += 1;
374 }
375
376 static dbus_bool_t
377 is_valid_section_name (const char *name)
378 {
379   /* 5. Group names may contain all ASCII characters except for control characters and '[' and ']'. */
380
381   while (*name)
382     {
383       if (!((*name >= 'A' && *name <= 'Z') || (*name >= 'a' || *name <= 'z') ||
384             *name == '\n' || *name == '\t'))
385         return FALSE;
386       
387       name++;
388     }
389
390   return TRUE;
391 }
392
393 static dbus_bool_t
394 parse_section_start (BusDesktopFileParser *parser, DBusError *error)
395 {
396   int line_end;
397   char *section_name;
398
399   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
400   
401   if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
402     line_end = parser->len;
403   
404   if (line_end - parser->pos <= 2 ||
405       _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
406     {
407       report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
408       parser_free (parser);
409       return FALSE;
410     }
411
412   section_name = unescape_string (parser,
413                                   &parser->data, parser->pos + 1, line_end - 1,
414                                   error);
415
416   if (section_name == NULL)
417     {
418       parser_free (parser);
419       return FALSE;
420     }
421
422   if (!is_valid_section_name (section_name))
423     {
424       report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
425       parser_free (parser);
426       dbus_free (section_name);
427       return FALSE;
428     }
429
430   if (open_section (parser, section_name) == NULL)
431     {
432       dbus_free (section_name);
433       return FALSE;
434     }
435
436   if (line_end == parser->len)
437     parser->pos = parser->len;
438   else
439     parser->pos = line_end + 1;
440   
441   parser->line_num += 1;
442
443   dbus_free (section_name);
444   
445   return TRUE;
446 }
447
448 static dbus_bool_t
449 parse_key_value (BusDesktopFileParser *parser, DBusError *error)
450 {
451   int line_end;
452   int key_start, key_end;
453   int value_start;
454   int p;
455   char *value, *tmp;
456   DBusString key;
457   BusDesktopFileLine *line;
458
459   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
460   
461   if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
462     line_end = parser->len;
463   
464   p = parser->pos;
465   key_start = p;
466   while (p < line_end &&
467          (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR))
468     p++;
469   key_end = p;
470   
471   if (key_start == key_end)
472     {
473       report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
474       parser_free (parser);
475       return FALSE;
476     }
477
478   /* We ignore locales for now */
479   if (p < line_end && _dbus_string_get_byte (&parser->data, p) == '[')
480     {
481       if (line_end == parser->len)
482         parser->pos = parser->len;
483       else
484         parser->pos = line_end + 1;
485           
486       parser->line_num += 1;
487
488       return TRUE;
489     }
490   
491   /* Skip space before '=' */
492   while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
493     p++;
494
495   if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
496     {
497       report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
498       parser_free (parser);
499       return FALSE;
500     }
501
502   if (p == line_end)
503     {
504       report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
505       parser_free (parser);
506       return FALSE;
507     }
508
509   /* Skip the '=' */
510   p++;
511
512   /* Skip space after '=' */
513   while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
514     p++;
515
516   value_start = p;
517   
518   value = unescape_string (parser, &parser->data, value_start, line_end, error);
519   if (value == NULL)
520     {
521       parser_free (parser);
522       return FALSE;
523     }
524
525   line = new_line (parser);
526   if (line == NULL)
527     {
528       parser_free (parser);
529       return FALSE;
530     }
531   
532   if (!_dbus_string_init (&key))
533     {
534       parser_free (parser);
535       return FALSE;
536     }
537   
538   if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
539                               &key, 0))
540     {
541       parser_free (parser);
542       return FALSE;
543     }
544   
545   if (!_dbus_string_steal_data (&key, &tmp))
546     {
547       parser_free (parser);
548       return FALSE;
549     }
550   
551   _dbus_string_free (&key);
552   
553   line->key = tmp;
554   line->value = value;
555
556   if (line_end == parser->len)
557     parser->pos = parser->len;
558   else
559     parser->pos = line_end + 1;
560   
561   parser->line_num += 1;
562
563   return TRUE;
564 }
565
566 static void
567 report_error (BusDesktopFileParser *parser,
568               char                 *message,
569               const char           *error_name,
570               DBusError            *error)
571 {
572   const char *section_name = NULL;
573
574   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
575   
576   if (parser->current_section != -1)
577     section_name = parser->desktop_file->sections[parser->current_section].section_name;
578
579   if (section_name)
580     dbus_set_error (error, error_name,
581                     "Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
582   else
583     dbus_set_error (error, error_name,
584                     "Error at line %d: %s\n", parser->line_num, message);
585 }
586
587 #if 0
588 static void
589 dump_desktop_file (BusDesktopFile *file)
590 {
591   int i;
592
593   for (i = 0; i < file->n_sections; i++)
594     {
595       int j;
596       
597       printf ("[%s]\n", file->sections[i].section_name);
598
599       for (j = 0; j < file->sections[i].n_lines; j++)
600         {
601           printf ("%s=%s\n", file->sections[i].lines[j].key,
602                   file->sections[i].lines[j].value);
603         }
604     }
605 }
606 #endif
607
608 BusDesktopFile*
609 bus_desktop_file_load (DBusString *filename,
610                        DBusError  *error)
611 {
612   DBusString str;
613   BusDesktopFileParser parser;
614   DBusStat sb;
615
616   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
617   
618   /* Clearly there's a race here, but it's just to make it unlikely
619    * that we do something silly, we still handle doing it below.
620    */
621   if (!_dbus_stat (filename, &sb, error))
622     return NULL;
623
624   if (sb.size > _DBUS_ONE_KILOBYTE * 128)
625     {
626       dbus_set_error (error, DBUS_ERROR_FAILED,
627                       "Desktop file size (%ld bytes) is too large", (long) sb.size);
628       return NULL;
629     }
630   
631   if (!_dbus_string_init (&str))
632     return NULL;
633   
634   if (!_dbus_file_get_contents (&str, filename, error))
635     {
636       _dbus_string_free (&str);
637       return NULL;
638     }
639
640   if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
641     {
642       _dbus_string_free (&str);
643       dbus_set_error (error, DBUS_ERROR_FAILED,
644                       "invalid UTF-8");   
645       return NULL;
646     }
647   
648   parser.desktop_file = dbus_new0 (BusDesktopFile, 1);
649   if (parser.desktop_file == NULL)
650     {
651       _dbus_string_free (&str);
652       BUS_SET_OOM (error);
653       return NULL;
654     }
655   
656   parser.data = str;
657   parser.line_num = 1;
658   parser.pos = 0;
659   parser.len = _dbus_string_get_length (&parser.data);
660   parser.current_section = -1;
661
662   while (parser.pos < parser.len)
663     {
664       if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
665         {
666           if (!parse_section_start (&parser, error))
667             {
668               return NULL;
669             }
670         }
671       else if (is_blank_line (&parser) ||
672                _dbus_string_get_byte (&parser.data, parser.pos) == '#')
673         parse_comment_or_blank (&parser);
674       else
675         {
676           if (!parse_key_value (&parser, error))
677             {
678               return NULL;
679             }
680         }
681     }
682
683   _dbus_string_free (&parser.data);
684
685   return parser.desktop_file;
686 }
687
688 static BusDesktopFileSection *
689 lookup_section (BusDesktopFile *desktop_file,
690                 const char     *section_name)
691 {
692   BusDesktopFileSection *section;
693   int i;
694   
695   if (section_name == NULL)
696     return NULL;
697   
698   for (i = 0; i < desktop_file->n_sections; i ++)
699     {
700       section = &desktop_file->sections[i];
701
702       if (strcmp (section->section_name, section_name) == 0)
703         return section;
704     }
705   
706   return NULL;
707 }
708
709 static BusDesktopFileLine *
710 lookup_line (BusDesktopFile        *desktop_file,
711              BusDesktopFileSection *section,
712              const char            *keyname)
713 {
714   BusDesktopFileLine *line;
715   int i;
716
717   for (i = 0; i < section->n_lines; i++)
718     {
719       line = &section->lines[i];
720       
721       if (strcmp (line->key, keyname) == 0)
722         return line;
723     }
724   
725   return NULL;
726 }
727
728 dbus_bool_t
729 bus_desktop_file_get_raw (BusDesktopFile  *desktop_file,
730                           const char      *section_name,
731                           const char      *keyname,
732                           const char     **val)
733 {
734   BusDesktopFileSection *section;
735   BusDesktopFileLine *line;
736
737   *val = NULL;
738
739   section = lookup_section (desktop_file, section_name);
740   
741   if (!section)
742     return FALSE;
743
744   line = lookup_line (desktop_file,
745                       section,
746                       keyname);
747
748   if (!line)
749     return FALSE;
750   
751   *val = line->value;
752   
753   return TRUE;
754 }
755
756 dbus_bool_t
757 bus_desktop_file_get_string (BusDesktopFile  *desktop_file,
758                              const char      *section,
759                              const char      *keyname,
760                              char           **val)
761 {
762   const char *raw;
763   
764   *val = NULL;
765   
766   if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw))
767     return FALSE;
768
769   *val = _dbus_strdup (raw);
770
771   /* FIXME we don't distinguish "key not found" from "out of memory" here,
772    * which is broken.
773    */
774   if (*val == NULL)
775     return FALSE;
776   
777   return TRUE;
778 }