~ubuntu-branches/ubuntu/precise/grass/precise

« back to all changes in this revision

Viewing changes to imagery/i.ortho.photo/i.photo.target/ask_target.c

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2011-04-13 17:08:41 UTC
  • mfrom: (8.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20110413170841-ss1t9bic0d0uq0gz
Tags: 6.4.1-1
* New upstream version.
* Now build-dep on libjpeg-dev and current libreadline6-dev.
* Removed patch swig: obsolete.
* Policy bumped to 3.9.2, without changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ask_target.c */
 
2
 
 
3
#include <stdlib.h>
 
4
#include <unistd.h>
 
5
#include <string.h>
 
6
#include <grass/gis.h>
 
7
#include <grass/vask.h>
 
8
#include "local_proto.h"
 
9
 
 
10
int ask_target(char *group, char *location, char *mapset)
 
11
{
 
12
    char buf[100];
 
13
    char t1[80], t2[80], t3[80];
 
14
    char cur_location[30], cur_mapset[30];
 
15
 
 
16
    strcpy(cur_location, location);
 
17
    strcpy(cur_mapset, mapset);
 
18
 
 
19
    sprintf(t1, "Please select the target LOCATION and MAPSET for group<%s>",
 
20
            group);
 
21
    sprintf(t2, "CURRENT LOCATION: %s", G_location());
 
22
    sprintf(t3, "CURRENT MAPSET:   %s", G_mapset());
 
23
 
 
24
    V_clear();
 
25
    V_line(1, t1);
 
26
    V_line(4, t2);
 
27
    V_line(5, t3);
 
28
    V_line(9, "TARGET LOCATION:");
 
29
    V_line(10, "TARGET MAPSET:");
 
30
    V_line(12,
 
31
           "(enter list for a list of locations or mapsets within a location)");
 
32
    V_ques(location, 's', 9, 18, 20);
 
33
    V_ques(mapset, 's', 10, 18, 20);
 
34
 
 
35
    for (;;) {
 
36
        if (strcmp(location, "list") == 0)
 
37
            strcpy(location, cur_location);
 
38
        if (strcmp(mapset, "list") == 0)
 
39
            strcpy(mapset, cur_mapset);
 
40
 
 
41
        V_intrpt_ok();
 
42
        if (!V_call())
 
43
            exit(0);
 
44
        if (*location == 0 && *mapset == 0)
 
45
            exit(0);
 
46
 
 
47
        if (*location == 0 || strcmp(location, "list") == 0)
 
48
            list_locations();
 
49
        else if (no_location(location)) {
 
50
            fprintf(stderr, "\n** <%s> - unknown location\n", location);
 
51
            list_locations();
 
52
        }
 
53
        else {
 
54
            G__setenv("LOCATION_NAME", location);
 
55
            if (*mapset == 0 || strcmp(mapset, "list") == 0)
 
56
                list_mapsets();
 
57
            else if (mapset_ok(mapset))
 
58
                break;
 
59
            else
 
60
                list_mapsets();
 
61
        }
 
62
        fprintf(stderr, "Hit RETURN -->");
 
63
        G_gets(buf);
 
64
    }
 
65
 
 
66
    return 0;
 
67
}
 
68
 
 
69
int list_locations(void)
 
70
{
 
71
    char buf[1024];
 
72
 
 
73
    sprintf(buf, "ls -C %s\n", G_gisdbase());
 
74
    fprintf(stderr, "\nKnown locations:\n");
 
75
    system(buf);
 
76
 
 
77
    return 0;
 
78
}
 
79
 
 
80
int no_location(char *location)
 
81
{
 
82
    char buf[1024];
 
83
 
 
84
    sprintf(buf, "%s/%s", G_gisdbase(), location);
 
85
    return access(buf, 0) != 0;
 
86
}
 
87
 
 
88
int list_mapsets(void)
 
89
{
 
90
    char buf[1024];
 
91
    FILE *fd;
 
92
    int any, ok, any_ok;
 
93
    int len, tot_len;
 
94
 
 
95
    sprintf(buf, "ls %s/%s", G_gisdbase(), G_location());
 
96
    fprintf(stderr, "LOCATION %s\n", G_location());
 
97
    fprintf(stderr, "\nAvailable mapsets:\n");
 
98
    fd = popen(buf, "r");
 
99
    any = 0;
 
100
    any_ok = 0;
 
101
    tot_len = 0;
 
102
    if (fd) {
 
103
        while (fscanf(fd, "%s", buf) == 1) {
 
104
            any = 1;
 
105
            len = strlen(buf) + 1;
 
106
            len /= 20;
 
107
            len = (len + 1) * 20;
 
108
            tot_len += len;
 
109
            if (tot_len > 75) {
 
110
                fprintf(stderr, "\n");
 
111
                tot_len = len;
 
112
            }
 
113
            if ((ok = (G__mapset_permissions(buf)) == 1))
 
114
                any_ok = 1;
 
115
            fprintf(stderr, "%s%-*s", ok ? "(+)" : "   ", len, buf);
 
116
        }
 
117
        pclose(fd);
 
118
        if (tot_len)
 
119
            fprintf(stderr, "\n");
 
120
        if (any_ok)
 
121
            fprintf(stderr,
 
122
                    "\nnote: you only have access to mapsets marked with (+)\n");
 
123
        else if (any)
 
124
            fprintf(stderr,
 
125
                    "\nnote: you do not have access to any of these mapsets\n");
 
126
    }
 
127
 
 
128
    return 0;
 
129
}
 
130
 
 
131
int mapset_ok(char *mapset)
 
132
{
 
133
    switch (G__mapset_permissions(mapset)) {
 
134
    case 1:
 
135
        return 1;
 
136
    case 0:
 
137
        fprintf(stderr, "\n** <%s> - permission to mapset denied **\n",
 
138
                mapset);
 
139
        return 0;
 
140
    default:
 
141
        fprintf(stderr, "\n** <%s> - mapset not found **\n", mapset);
 
142
        return 0;
 
143
    }
 
144
}