Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 699530 | Differences between
and this patch

Collapse All | Expand All

(-)a/src/utils.c (-3 / +79 lines)
Lines 489-502 shell_parser_set_variable (ShellParser *parser, Link Here
489
    if (found_entry != NULL) {
489
    if (found_entry != NULL) {
490
        g_free (found_entry->string);
490
        g_free (found_entry->string);
491
        found_entry->string = g_strdup_printf ("%s=%s", variable, quoted_value);
491
        found_entry->string = g_strdup_printf ("%s=%s", variable, quoted_value);
492
        g_free (found_entry->unquoted_value);
493
        found_entry->unquoted_value = g_strdup (value);
492
        ret = TRUE;
494
        ret = TRUE;
493
    } else {
495
    } else {
494
        if (add_if_unset) {
496
        if (add_if_unset) {
497
            GList *appended;
498
            GList *last = g_list_last (parser->entry_list);
499
            struct ShellEntry *last_entry = NULL;
500
            if (last != NULL)
501
                last_entry = (struct ShellEntry *)last->data;
502
/* Add a separator before the new variable if there is none */
503
            if (last_entry != NULL &&
504
                last_entry->type != SHELL_ENTRY_TYPE_SEPARATOR &&
505
                last_entry->type != SHELL_ENTRY_TYPE_COMMENT) {
506
                last_entry = g_new0 (struct ShellEntry, 1);
507
                last_entry->type = SHELL_ENTRY_TYPE_SEPARATOR;
508
                last_entry->string = g_strdup ("\n");
509
                appended = g_list_alloc();
510
                appended->prev = last;
511
                appended->next = NULL;
512
                appended->data = (gpointer)last_entry;
513
                last->next = appended;
514
                last = appended;
515
            }
516
/* Add a the new variable at the end of the parser */
495
            found_entry = g_new0 (struct ShellEntry, 1);
517
            found_entry = g_new0 (struct ShellEntry, 1);
496
            found_entry->type = SHELL_ENTRY_TYPE_ASSIGNMENT;
518
            found_entry->type = SHELL_ENTRY_TYPE_ASSIGNMENT;
497
            found_entry->variable = g_strdup (variable);
519
            found_entry->variable = g_strdup (variable);
498
            found_entry->string = g_strdup_printf ("%s=%s", variable, quoted_value);
520
            found_entry->unquoted_value = g_strdup (value);
499
            parser->entry_list = g_list_append (parser->entry_list, found_entry);
521
            found_entry->string = g_strdup_printf ("%s=%s", variable,
522
                                                            quoted_value);
523
            appended = g_list_alloc();
524
            appended->prev = last;
525
            appended->next = NULL;
526
            appended->data = (gpointer)found_entry;
527
            if (last != NULL)
528
                last->next = appended;
529
            else
530
                parser->entry_list = appended; /* parser was empty */
531
/* Add a separator at the end of the parser */
532
            last = appended;
533
            last_entry = g_new0 (struct ShellEntry, 1);
534
            last_entry->type = SHELL_ENTRY_TYPE_SEPARATOR;
535
            last_entry->string = g_strdup ("\n");
536
            appended = g_list_alloc();
537
            appended->prev = last;
538
            appended->next = NULL;
539
            appended->data = (gpointer)last_entry;
540
            last->next = appended;
541
500
            ret = TRUE;
542
            ret = TRUE;
501
        }
543
        }
502
    }
544
    }
Lines 527-534 shell_parser_clear_variable (ShellParser *parser, Link Here
527
            curr->prev = NULL;
569
            curr->prev = NULL;
528
            curr->next = NULL;
570
            curr->next = NULL;
529
            g_list_free_full (curr, (GDestroyNotify)shell_entry_free);
571
            g_list_free_full (curr, (GDestroyNotify)shell_entry_free);
572
            /* for a normal variable assignment, there is a separator
573
             * before and a separator after, so that we need to remove
574
             * one (otherwise, after a while, the file while have a lot
575
             * of blank lines!), but we need to choose which one. Things are
576
             * slightly more complicated, because one or both separators
577
             * may be comments, which we do not want to remove, and also,
578
             * we may be at the end or the beginning of the file:
579
             *   - if we are at the beginning of the file, remove next,
580
             *     except if next is a comment
581
             *   - if we are at the end of file or if next is a comment,
582
             *     remove prev, except if prev is a comment
583
             *   - if the preceding is a comment, remove next,
584
             *     except if it is a comment
585
             *   - it may happen that both prev and next are NULL, in which
586
             *     case, we do not remove anything.
587
             * Summary: if next is a separator, remove it, else, if prev
588
             * is a separator, remove it.
589
             */
590
            if (next != NULL &&
591
                ((struct ShellEntry*)next->data)->type ==
592
                                    SHELL_ENTRY_TYPE_SEPARATOR) {
593
                GList *next_next = next->next;
594
                next->prev = next->next = NULL;
595
                g_list_free_full (next, (GDestroyNotify)shell_entry_free);
596
                next = next_next;
597
            } else if (prev != NULL &&
598
                       ((struct ShellEntry*)next->data)->type ==
599
                                    SHELL_ENTRY_TYPE_SEPARATOR) {
600
                GList *prev_prev=prev->prev;
601
                prev->prev = prev->next = NULL;
602
                g_list_free_full (prev, (GDestroyNotify)shell_entry_free);
603
                prev = prev_prev;
604
            }
530
            if (prev != NULL)
605
            if (prev != NULL)
531
                prev->next = next;
606
                prev->next = next;
607
            else
608
                parser->entry_list = next;
532
            if (next != NULL)
609
            if (next != NULL)
533
                next->prev = prev;
610
                next->prev = prev;
534
            curr = next;
611
            curr = next;
535
- 

Return to bug 699530