~ubuntu-branches/ubuntu/saucy/augeas/saucy-proposed

« back to all changes in this revision

Viewing changes to src/augtool.c

  • Committer: Package Import Robot
  • Author(s): Free Ekanayaka
  • Date: 2012-05-20 09:45:39 UTC
  • mfrom: (1.5.1) (19.1.14 precise)
  • Revision ID: package-import@ubuntu.com-20120520094539-3zomeumx6zgt37ft
Tags: 0.10.0-1
* New upstream release
* Pull packaging changes from Ubuntu (see the changelog)
* Pull packaging changes from Igor Pashev's package at mentors.d.o:
  - Update maintainer's email
  - Add upstream sudoers.patch (Closes: #650079)
  - Add upstream debctl.patch (Closes: #650887)
  - Add upstream modprobe.patch (Closes: #641813)
  - Add cpp-linkage.patch for C++ linkage
  - Add gnulib-build-out-of-source.patch for building out of source tree
  - Add regexp-escape.patch for escape() usage on systems without uselocale()
  * Fix build-deps on naturaldocs (build-depends-on-1-revision)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * augtool.c:
3
3
 *
4
 
 * Copyright (C) 2007-2010 David Lutterkort
 
4
 * Copyright (C) 2007-2011 David Lutterkort
5
5
 *
6
6
 * This library is free software; you can redistribute it and/or
7
7
 * modify it under the terms of the GNU Lesser General Public
91
91
    static int current = 0;
92
92
    static char **children = NULL;
93
93
    static int nchildren = 0;
 
94
    static char *ctx = NULL;
 
95
 
 
96
    char *end = strrchr(text, SEP);
 
97
    if (end != NULL)
 
98
        end += 1;
94
99
 
95
100
    if (state == 0) {
96
 
        char *end = strrchr(text, SEP);
97
101
        char *path;
98
102
        if (end == NULL) {
99
 
            if ((path = strdup("/*")) == NULL)
 
103
            if ((path = strdup("*")) == NULL)
100
104
                return NULL;
101
105
        } else {
102
 
            end += 1;
103
106
            CALLOC(path, end - text + 2);
104
107
            if (path == NULL)
105
108
                return NULL;
112
115
        free((void *) children);
113
116
        nchildren = aug_match(aug, path, &children);
114
117
        current = 0;
 
118
 
 
119
        ctx = NULL;
 
120
        if (path[0] != SEP)
 
121
            aug_get(aug, AUGEAS_CONTEXT, (const char **) &ctx);
 
122
 
115
123
        free(path);
116
124
    }
117
125
 
 
126
    if (end == NULL)
 
127
        end = (char *) text;
 
128
 
118
129
    while (current < nchildren) {
119
130
        char *child = children[current];
120
131
        current += 1;
121
 
        if (STREQLEN(child, text, strlen(text))) {
 
132
 
 
133
        char *chend = strrchr(child, SEP) + 1;
 
134
        if (STREQLEN(chend, end, strlen(end))) {
122
135
            if (child_count(child) > 0) {
123
136
                char *c = realloc(child, strlen(child)+2);
124
137
                if (c == NULL)
126
139
                child = c;
127
140
                strcat(child, "/");
128
141
            }
 
142
 
 
143
            /* strip off context if the user didn't give it */
 
144
            if (ctx != NULL) {
 
145
                char *c = realloc(child, strlen(child)-strlen(ctx)+1);
 
146
                if (c == NULL)
 
147
                    return NULL;
 
148
                int ctxidx = strlen(ctx);
 
149
                if (child[ctxidx] == SEP)
 
150
                    ctxidx++;
 
151
                strcpy(c, &child[ctxidx]);
 
152
                child = c;
 
153
            }
 
154
 
129
155
            rl_filename_completion_desired = 1;
130
156
            rl_completion_append_character = '\0';
131
157
            return child;
141
167
    static const char *const commands[] = {
142
168
        "quit", "clear", "defnode", "defvar",
143
169
        "get", "ins", "load", "ls", "match",
144
 
        "mv", "print", "rm", "save", "set", "setm",
 
170
        "mv", "print", "dump-xml", "rm", "save", "set", "setm",
145
171
        "clearm", "span", "help", NULL };
146
172
 
147
173
    static int current = 0;
352
378
        goto error;
353
379
 
354
380
    fprintf(stderr, "augtool %s <http://augeas.net/>\n", version);
355
 
    fprintf(stderr, "Copyright (C) 2009-2010 David Lutterkort\n");
 
381
    fprintf(stderr, "Copyright (C) 2007-2011 David Lutterkort\n");
356
382
    fprintf(stderr, "License LGPLv2+: GNU LGPL version 2.1 or later\n");
357
383
    fprintf(stderr, "                 <http://www.gnu.org/licenses/lgpl-2.1.html>\n");
358
384
    fprintf(stderr, "This is free software: you are free to change and redistribute it.\n");
372
398
    return result;
373
399
}
374
400
 
 
401
static void print_aug_error(void) {
 
402
    if (aug_error(aug) == AUG_ENOMEM) {
 
403
        fprintf(stderr, "Out of memory.\n");
 
404
        return;
 
405
    }
 
406
    if (aug_error(aug) != AUG_NOERROR) {
 
407
        fprintf(stderr, "error: %s\n", aug_error_message(aug));
 
408
        if (aug_error_minor_message(aug) != NULL)
 
409
            fprintf(stderr, "error: %s\n",
 
410
                    aug_error_minor_message(aug));
 
411
        if (aug_error_details(aug) != NULL) {
 
412
            fputs(aug_error_details(aug), stderr);
 
413
            fprintf(stderr, "\n");
 
414
        }
 
415
    }
 
416
}
 
417
 
375
418
static int main_loop(void) {
376
419
    char *line = NULL;
377
420
    int ret = 0;
452
495
            return ret;
453
496
        if (code < 0) {
454
497
            ret = -1;
455
 
            if (aug_error(aug) == AUG_ENOMEM) {
456
 
                fprintf(stderr, "Out of memory.\n");
457
 
                return -1;
458
 
            }
459
 
            if (aug_error(aug) != AUG_NOERROR) {
460
 
                fprintf(stderr, "error: %s\n", aug_error_message(aug));
461
 
                if (aug_error_minor_message(aug) != NULL)
462
 
                    fprintf(stderr, "error: %s\n",
463
 
                            aug_error_minor_message(aug));
464
 
                if (aug_error_details(aug) != NULL)
465
 
                    fprintf(stderr, "error: %s\n", aug_error_details(aug));
466
 
            }
 
498
            print_aug_error();
467
499
        }
468
500
    }
469
501
}
483
515
    }
484
516
    code = run_command(line);
485
517
    free(line);
486
 
    if (code == 0 && auto_save)
 
518
    if (code >= 0 && auto_save)
487
519
        code = run_command("save");
488
520
    return (code == 0 || code == -2) ? 0 : -1;
489
521
}
495
527
 
496
528
    parse_opts(argc, argv);
497
529
 
498
 
    aug = aug_init(root, loadpath, flags);
499
 
    if (aug == NULL) {
 
530
    aug = aug_init(root, loadpath, flags|AUG_NO_ERR_CLOSE);
 
531
    if (aug == NULL || aug_error(aug) != AUG_NOERROR) {
500
532
        fprintf(stderr, "Failed to initialize Augeas\n");
 
533
        if (aug != NULL)
 
534
            print_aug_error();
501
535
        exit(EXIT_FAILURE);
502
536
    }
503
537
    if (print_version) {