1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
|
/*
Copyright 2012 Alexandre Rostovtsev
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libdaemon/dfork.h>
#include <glib.h>
#include <gio/gio.h>
#include <polkit/polkit.h>
#include "utils.h"
#include "config.h"
static GRegex *indent_regex = NULL;
static GRegex *comment_regex = NULL;
static GRegex *separator_regex = NULL;
static GRegex *var_equals_regex = NULL;
static GRegex *single_quoted_regex = NULL;
static GRegex *double_quoted_regex = NULL;
static GRegex *unquoted_regex = NULL;
/* Always returns TRUE */
gboolean
_g_match_info_clear (GMatchInfo **match_info)
{
if (match_info == NULL || *match_info == NULL)
return TRUE;
g_match_info_free (*match_info);
*match_info = NULL;
return TRUE;
}
gchar *
strstr0 (const gchar *haystack, const gchar *needle)
{
if (haystack == NULL || needle == NULL)
return NULL;
return strstr (haystack, needle);
}
struct check_polkit_data {
const gchar *unique_name;
const gchar *action_id;
gboolean user_interaction;
GAsyncReadyCallback callback;
gpointer user_data;
PolkitAuthority *authority;
PolkitSubject *subject;
};
void
check_polkit_data_free (struct check_polkit_data *data)
{
if (data == NULL)
return;
if (data->subject != NULL)
g_object_unref (data->subject);
if (data->authority != NULL)
g_object_unref (data->authority);
g_free (data);
}
gboolean
check_polkit_finish (GAsyncResult *res,
GError **error)
{
GSimpleAsyncResult *simple;
simple = G_SIMPLE_ASYNC_RESULT (res);
if (g_simple_async_result_propagate_error (simple, error))
return FALSE;
return g_simple_async_result_get_op_res_gboolean (simple);
}
static void
check_polkit_authorization_cb (GObject *source_object,
GAsyncResult *res,
gpointer _data)
{
struct check_polkit_data *data;
PolkitAuthorizationResult *result;
GSimpleAsyncResult *simple;
GError *err = NULL;
data = (struct check_polkit_data *) _data;
if ((result = polkit_authority_check_authorization_finish (data->authority, res, &err)) == NULL) {
g_simple_async_report_take_gerror_in_idle (NULL, data->callback, data->user_data, err);
goto out;
}
if (!polkit_authorization_result_get_is_authorized (result)) {
g_simple_async_report_error_in_idle (NULL, data->callback, data->user_data, POLKIT_ERROR, POLKIT_ERROR_NOT_AUTHORIZED, "Authorizing for '%s': not authorized", data->action_id);
goto out;
}
simple = g_simple_async_result_new (NULL, data->callback, data->user_data, check_polkit_async);
g_simple_async_result_set_op_res_gboolean (simple, TRUE);
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
out:
check_polkit_data_free (data);
if (result != NULL)
g_object_unref (result);
}
static void
check_polkit_authority_cb (GObject *source_object,
GAsyncResult *res,
gpointer _data)
{
struct check_polkit_data *data;
GError *err = NULL;
data = (struct check_polkit_data *) _data;
if ((data->authority = polkit_authority_get_finish (res, &err)) == NULL) {
g_simple_async_report_take_gerror_in_idle (NULL, data->callback, data->user_data, err);
check_polkit_data_free (data);
return;
}
if (data->unique_name == NULL || data->action_id == NULL ||
(data->subject = polkit_system_bus_name_new (data->unique_name)) == NULL) {
g_simple_async_report_error_in_idle (NULL, data->callback, data->user_data, POLKIT_ERROR, POLKIT_ERROR_FAILED, "Authorizing for '%s': failed sanity check", data->action_id);
check_polkit_data_free (data);
return;
}
polkit_authority_check_authorization (data->authority, data->subject, data->action_id, NULL, (PolkitCheckAuthorizationFlags) data->user_interaction, NULL, check_polkit_authorization_cb, data);
}
void
check_polkit_async (const gchar *unique_name,
const gchar *action_id,
const gboolean user_interaction,
GAsyncReadyCallback callback,
gpointer user_data)
{
struct check_polkit_data *data;
data = g_new0 (struct check_polkit_data, 1);
data->unique_name = unique_name;
data->action_id = action_id;
data->user_interaction = user_interaction;
data->callback = callback;
data->user_data = user_data;
polkit_authority_get_async (NULL, check_polkit_authority_cb, data);
}
enum ShellEntryType {
SHELL_ENTRY_TYPE_INDENT,
SHELL_ENTRY_TYPE_COMMENT,
SHELL_ENTRY_TYPE_SEPARATOR,
SHELL_ENTRY_TYPE_ASSIGNMENT,
};
struct ShellEntry {
enum ShellEntryType type;
gchar *string;
gchar *variable; /* only relevant for assignments */
gchar *unquoted_value; /* only relevant for assignments */
};
gchar *
shell_source_var (GFile *file,
const gchar *variable,
GError **error)
{
gchar *argv[4] = { "sh", "-c", NULL, NULL };
gchar *filename = NULL, *quoted_filename = NULL;
gchar *output = NULL;
GFileInfo *info;
const GFileAttributeInfo *attribute_info;
filename = g_file_get_path (file);
if ((info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_ACCESS_CAN_READ, G_FILE_QUERY_INFO_NONE, NULL, error)) == NULL) {
g_prefix_error (error, "Unable to source '%s': ", filename);
goto out;
}
if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR &&
g_file_info_get_file_type (info) != G_FILE_TYPE_SYMBOLIC_LINK) {
g_propagate_error (error,
g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Unable to source '%s': not a regular file", filename));
goto out;
}
if (!g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ)) {
g_propagate_error (error,
g_error_new (G_FILE_ERROR, G_FILE_ERROR_ACCES,
"Unable to source '%s': permission denied", filename));
goto out;
}
quoted_filename = g_shell_quote (filename);
argv[2] = g_strdup_printf (". %s; echo -n %s", quoted_filename, variable);
if (!g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &output, NULL, NULL, error)) {
g_prefix_error (error, "Unable to source '%s': ", filename);
}
out:
g_free (filename);
g_free (quoted_filename);
if (info != NULL)
g_object_unref (info);
if (argv[2] != NULL)
g_free (argv[2]);
return output;
}
static void
shell_entry_free (struct ShellEntry *entry)
{
if (entry == NULL)
return;
g_free (entry->string);
g_free (entry->variable);
g_free (entry->unquoted_value);
g_free (entry);
}
void
shell_parser_free (ShellParser *parser)
{
if (parser == NULL)
return;
if (parser->file != NULL)
g_object_unref (parser->file);
if (parser->filename != NULL)
g_free (parser->filename);
if (parser->entry_list != NULL)
g_list_free_full (parser->entry_list, (GDestroyNotify)shell_entry_free);
g_free (parser);
}
ShellParser *
shell_parser_new (GFile *file,
GError **error)
{
gchar *filebuf = NULL;
GError *local_err = NULL;
ShellParser *ret = NULL;
if (file == NULL)
return NULL;
if (!g_file_load_contents (file, NULL, &filebuf, NULL, NULL, &local_err)) {
if (local_err != NULL) {
/* Inability to parse or open is a failure; file not existing at all is *not* a failure */
if (local_err->code == G_IO_ERROR_NOT_FOUND) {
g_error_free (local_err);
ret = g_new0 (ShellParser, 1);
g_object_ref (file);
ret->file = file;
ret->filename = g_file_get_path (file);
return ret;
} else {
gchar *filename;
filename = g_file_get_path (file);
g_propagate_prefixed_error (error, local_err, "Unable to read '%s':", filename);
g_free (filename);
}
}
return NULL;
}
ret = shell_parser_new_from_string (file, filebuf, error);
g_free (filebuf);
return ret;
}
ShellParser *
shell_parser_new_from_string (GFile *file,
gchar *filebuf,
GError **error)
{
ShellParser *ret = NULL;
GError *local_err = NULL;
gchar *s;
if (file == NULL || filebuf == NULL)
return NULL;
ret = g_new0 (ShellParser, 1);
g_object_ref (file);
ret->file = file;
ret->filename = g_file_get_path (file);
gboolean want_separator = FALSE; /* Do we expect the next entry to be a separator or comment? */
s = filebuf;
while (*s != 0) {
g_debug ("Scanning string: ``%s''", s);
gboolean matched = FALSE;
GMatchInfo *match_info = NULL;
struct ShellEntry *entry = NULL;
matched = g_regex_match (comment_regex, s, 0, &match_info);
if (matched) {
entry = g_new0 (struct ShellEntry, 1);
entry->type = SHELL_ENTRY_TYPE_COMMENT;
entry->string = g_match_info_fetch (match_info, 0);
ret->entry_list = g_list_prepend (ret->entry_list, entry);
s += strlen (entry->string);
g_debug ("Scanned comment: ``%s''", entry->string);
_g_match_info_clear (&match_info);
want_separator = FALSE;
continue;
}
_g_match_info_clear (&match_info);
matched = g_regex_match (separator_regex, s, 0, &match_info);
if (matched) {
entry = g_new0 (struct ShellEntry, 1);
entry->type = SHELL_ENTRY_TYPE_SEPARATOR;
entry->string = g_match_info_fetch (match_info, 0);
ret->entry_list = g_list_prepend (ret->entry_list, entry);
s += strlen (entry->string);
g_debug ("Scanned separator: ``%s''", entry->string);
_g_match_info_clear (&match_info);
want_separator = FALSE;
continue;
}
_g_match_info_clear (&match_info);
matched = g_regex_match (indent_regex, s, 0, &match_info);
if (matched) {
entry = g_new0 (struct ShellEntry, 1);
entry->type = SHELL_ENTRY_TYPE_INDENT;
entry->string = g_match_info_fetch (match_info, 0);
ret->entry_list = g_list_prepend (ret->entry_list, entry);
s += strlen (entry->string);
g_debug ("Scanned indent: ``%s''", entry->string);
_g_match_info_clear (&match_info);
continue;
}
_g_match_info_clear (&match_info);
matched = g_regex_match (var_equals_regex, s, 0, &match_info);
if (matched) {
gchar *raw_value = NULL, *temp1 = NULL, *temp2 = NULL;
/* If we expect a separator and get an assignment instead, fail */
if (want_separator)
goto no_match;
entry = g_new0 (struct ShellEntry, 1);
entry->type = SHELL_ENTRY_TYPE_ASSIGNMENT;
entry->string = g_match_info_fetch (match_info, 0);
entry->variable = g_match_info_fetch (match_info, 1);
s += strlen (entry->string);
g_debug ("Scanned variable: ``%s''", entry->string);
_g_match_info_clear (&match_info);
want_separator = TRUE;
while (*s != 0) {
g_debug ("Scanning string for values: ``%s''", s);
gboolean matched2 = FALSE;
matched2 = g_regex_match (single_quoted_regex, s, 0, &match_info);
if (matched2)
goto append_value;
_g_match_info_clear (&match_info);
matched2 = g_regex_match (double_quoted_regex, s, 0, &match_info);
if (matched2)
goto append_value;
_g_match_info_clear (&match_info);
matched2 = g_regex_match (unquoted_regex, s, 0, &match_info);
if (matched2)
goto append_value;
_g_match_info_clear (&match_info);
break;
append_value:
if (raw_value == NULL) {
raw_value = g_match_info_fetch (match_info, 0);
s += strlen (raw_value);
g_debug ("Scanned value: ``%s''", raw_value);
} else {
temp1 = raw_value;
temp2 = g_match_info_fetch (match_info, 0);
raw_value = g_strconcat (temp1, temp2, NULL);
s += strlen (temp2);
g_debug ("Scanned value: ``%s''", temp2);
g_free (temp1);
g_free (temp2);
}
_g_match_info_clear (&match_info);
}
if (raw_value != NULL) {
entry->unquoted_value = g_shell_unquote (raw_value, &local_err);
g_debug ("Unquoted value: ``%s''", entry->unquoted_value);
temp1 = entry->string;
temp2 = raw_value;
entry->string = g_strconcat (temp1, temp2, NULL);
g_free (temp1);
g_free (temp2);
ret->entry_list = g_list_prepend (ret->entry_list, entry);
if (local_err != NULL)
goto no_match;
}
continue;
}
no_match:
/* Nothing matches, parsing has failed! */
_g_match_info_clear (&match_info);
if (local_err != NULL)
g_propagate_prefixed_error (error, local_err, "Unable to parse '%s':", ret->filename);
else
g_propagate_error (error,
g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Unable to parse '%s'", ret->filename));
shell_parser_free (ret);
return NULL;
}
ret->entry_list = g_list_reverse (ret->entry_list);
return ret;
}
gboolean
shell_parser_is_empty (ShellParser *parser)
{
if (parser == NULL || parser->entry_list == NULL)
return TRUE;
return FALSE;
}
gboolean
shell_parser_set_variable (ShellParser *parser,
const gchar *variable,
const gchar *value,
gboolean add_if_unset)
{
GList *curr = NULL;
struct ShellEntry *found_entry = NULL;
gchar *quoted_value = NULL;
gboolean ret = FALSE;
g_assert (parser != NULL);
g_assert (variable != NULL);
for (curr = parser->entry_list; curr != NULL; curr = curr->next) {
struct ShellEntry *entry;
entry = (struct ShellEntry *)(curr->data);
if (entry->type == SHELL_ENTRY_TYPE_ASSIGNMENT && g_strcmp0 (variable, entry->variable) == 0)
found_entry = entry;
}
quoted_value = g_shell_quote (value);
if (found_entry != NULL) {
g_free (found_entry->string);
found_entry->string = g_strdup_printf ("%s=%s", variable, quoted_value);
ret = TRUE;
} else {
if (add_if_unset) {
found_entry = g_new0 (struct ShellEntry, 1);
found_entry->type = SHELL_ENTRY_TYPE_ASSIGNMENT;
found_entry->variable = g_strdup (variable);
found_entry->string = g_strdup_printf ("%s=%s", variable, quoted_value);
parser->entry_list = g_list_append (parser->entry_list, found_entry);
ret = TRUE;
}
}
g_free (quoted_value);
return ret;
}
void
shell_parser_clear_variable (ShellParser *parser,
const gchar *variable)
{
GList *curr = NULL;
gboolean ret = FALSE;
g_assert (parser != NULL);
g_assert (variable != NULL);
for (curr = parser->entry_list; curr != NULL; ) {
struct ShellEntry *entry;
entry = (struct ShellEntry *)(curr->data);
if (entry->type == SHELL_ENTRY_TYPE_ASSIGNMENT && g_strcmp0 (variable, entry->variable) == 0) {
GList *prev, *next;
prev = curr->prev;
next = curr->next;
curr->prev = NULL;
curr->next = NULL;
g_list_free_full (curr, (GDestroyNotify)shell_entry_free);
if (prev != NULL)
prev->next = next;
if (next != NULL)
next->prev = prev;
curr = next;
} else
curr = curr->next;
}
}
gboolean
shell_parser_save (ShellParser *parser,
GError **error)
{
gboolean ret = FALSE;
GList *curr = NULL;
GFileOutputStream *os;
g_assert (parser != NULL && parser->file != NULL && parser->filename != NULL);
if ((os = g_file_replace (parser->file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, error)) == NULL) {
g_prefix_error (error, "Unable to save '%s': ", parser->filename);
goto out;
}
for (curr = parser->entry_list; curr != NULL; curr = curr->next) {
struct ShellEntry *entry;
gsize written;
entry = (struct ShellEntry *)(curr->data);
if (!g_output_stream_write_all (G_OUTPUT_STREAM (os), entry->string, strlen (entry->string), &written, NULL, error)) {
g_prefix_error (error, "Unable to save '%s': ", parser->filename);
goto out;
}
}
if (!g_output_stream_close (G_OUTPUT_STREAM (os), NULL, error)) {
g_prefix_error (error, "Unable to save '%s': ", parser->filename);
g_output_stream_close (G_OUTPUT_STREAM (os), NULL, NULL);
goto out;
}
ret = TRUE;
out:
if (os)
g_object_unref (os);
return ret;
}
gboolean
shell_parser_set_and_save (GFile *file,
GError **error,
const gchar *first_var_name,
const gchar *first_alt_var_name,
const gchar *first_value,
...)
{
va_list ap;
ShellParser *parser;
gboolean ret = FALSE;
const gchar *var_name, *alt_var_name, *value;
va_start (ap, first_value);
if ((parser = shell_parser_new (file, error)) == NULL)
goto out;
var_name = first_var_name;
alt_var_name = first_alt_var_name;
value = first_value;
do {
if (alt_var_name == NULL) {
if (!shell_parser_set_variable (parser, var_name, value, TRUE)) {
g_propagate_error (error,
g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Unable to set %s in '%s'", var_name, parser->filename));
goto out;
}
} else {
if (!shell_parser_set_variable (parser, var_name, value, FALSE) &&
!shell_parser_set_variable (parser, alt_var_name, value, FALSE) &&
!shell_parser_set_variable (parser, var_name, value, TRUE)) {
g_propagate_error (error,
g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Unable to set %s or %s in '%s'", var_name, alt_var_name, parser->filename));
goto out;
}
}
} while ((var_name = va_arg (ap, const gchar*)) != NULL ?
alt_var_name = va_arg (ap, const gchar*), value = va_arg (ap, const gchar*), 1 : 0);
if (!shell_parser_save (parser, error))
goto out;
ret = TRUE;
out:
va_end (ap);
if (parser != NULL)
shell_parser_free (parser);
return ret;
}
gchar **
shell_parser_source_var_list (GFile *file,
const gchar * const *var_names,
GError **error)
{
ShellParser *parser;
gchar **ret = NULL, **value;
const gchar* const* var_name;
if (var_names == NULL)
return NULL;
if ((parser = shell_parser_new (file, error)) == NULL)
return NULL;
ret = g_new0 (gchar *, g_strv_length ((gchar **)var_names) + 1);
for (var_name = var_names, value = ret; *var_name != NULL; var_name++, value++) {
GList *curr;
for (curr = parser->entry_list; curr != NULL; curr = curr->next) {
struct ShellEntry *entry;
entry = (struct ShellEntry *)(curr->data);
if (entry->type == SHELL_ENTRY_TYPE_ASSIGNMENT && g_strcmp0 (*var_name, entry->variable) == 0)
*value = g_strdup (entry->unquoted_value);
}
}
shell_parser_free (parser);
return ret;
}
void
utils_destroy (void)
{
if (indent_regex != NULL) {
g_regex_unref (indent_regex);
indent_regex = NULL;
}
if (comment_regex != NULL) {
g_regex_unref (comment_regex);
comment_regex = NULL;
}
if (separator_regex != NULL) {
g_regex_unref (separator_regex);
separator_regex = NULL;
}
if (var_equals_regex != NULL) {
g_regex_unref (var_equals_regex);
var_equals_regex = NULL;
}
if (single_quoted_regex != NULL) {
g_regex_unref (single_quoted_regex);
single_quoted_regex = NULL;
}
if (double_quoted_regex != NULL) {
g_regex_unref (double_quoted_regex);
double_quoted_regex = NULL;
}
if (unquoted_regex != NULL) {
g_regex_unref (unquoted_regex);
unquoted_regex = NULL;
}
}
void
utils_init (void)
{
if (indent_regex == NULL) {
indent_regex = g_regex_new ("^[ \\t]+", G_REGEX_ANCHORED, 0, NULL);
g_assert (indent_regex != NULL);
}
if (comment_regex == NULL) {
comment_regex = g_regex_new ("^#[^\\n]*\\n", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (comment_regex != NULL);
}
if (separator_regex == NULL) {
separator_regex = g_regex_new ("^[ \\t;\\n\\r]*[;\\n][ \\t;\\n\\r]*", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (separator_regex != NULL);
}
if (var_equals_regex == NULL) {
var_equals_regex = g_regex_new ("^([a-zA-Z_][a-zA-Z0-9_]*)(?:(?:\\\\\\n)*)=(?:(?:\\\\\\n)*)", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (var_equals_regex != NULL);
}
if (single_quoted_regex == NULL) {
single_quoted_regex = g_regex_new ("^'[^']*'", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (single_quoted_regex != NULL);
}
/* We do not want to allow $(...) or `...` constructs in double-quoted
* strings because they might have side effects, but ${...} is OK */
if (double_quoted_regex == NULL) {
double_quoted_regex = g_regex_new ("^\"(?:[^\"`\\$]|\\\\[\"`\\$]|\\$\\{)*\"", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (double_quoted_regex != NULL);
}
if (unquoted_regex == NULL) {
unquoted_regex = g_regex_new ("^(?:[^\\s\"'`\\$\\|&<>;]|\\\\[\\s\"'`\\$\\|&<>;]|\\$\\{)+", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (unquoted_regex != NULL);
}
}
|