File: ask.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (662 lines) | stat: -rw-r--r-- 15,956 bytes parent folder | download
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
/****************************************************************
* These routines prompt the user for names of GIS data files
*
* G_ask_new           (prompt, name, element, desc)
* G_ask_old           (prompt, name, element, desc)
* G_ask_any           (prompt, name, element, desc, warn)
* G_ask_in_mapset     (prompt, name, element, desc)
* G_ask_new_file      (prompt, name, element, desc)
* G_ask_old_file      (prompt, name, element, desc)
*
* G_ask_new_ext       (prompt, name, element, desc, option, lister)
* G_ask_old_ext       (prompt, name, element, desc, option, lister)
* G_ask_any_ext       (prompt, name, element, desc, warn, option, lister)
* G_ask_in_mapset_ext (prompt, name, element, desc, option, lister)
*
*   char *prompt      prompt to be printed. can be "" in which
*                     case an appropriate prompt will be printed.
*   char *name        buffer to hold the name input by the user
*   char *element     GIS data element - "cell", "vect", etc.
*   char *desc        a description of element.  Used for prompting
*                     and listing. Will be set to element if given as ""
*                     (eg, if element is "vect", set desc = "vector")
*   char *option      list option. a description of the option.
*                     (eg, "with utms" will prompt as follows:
*                     list -f for a list with utms)
*   int (*lister)()   subroutine to return text for -f option.
*
*
* G_ask_new() requires the user to enter the name of a file
*             which does not exist in the current mapset
*             (but which may exist in other mapsets).
*
* G_ask_old() requires the user to enter the name of a file
*             which already exists.
*
* G_ask_in_mapset() requires the user to enter the name of a file
*                   which exists in the current mapset
*
* G_ask_any() accepts any legal filename. Optionally warns user
*             if the file exists in the current mapset.
*
* G_ask_new_file() requires the user to enter the name of a new file.
*
* G_ask_old_file() requires the user to enter the name of any existing file.
*
* returns:
*   char *    mapset where file was found, or
*             mapset where file is to be created
*   NULL      user hit RETURN to cancel the request
*
* note:
*  These routines have a 'list' function built in.  If a list -f
*  option is also desired, create a lister() routine, and
*  use G_ask_xxx_ext(). The lister() routine will be called as
*  follows:
*
*       lister (name, mapset, buf)
*
*       char *name        name of file 
*       char *mapset      mapset to where file lives
*       char *buf         buffer to hold description.
*                         lister() should copy into buf.
*                         buf will be large (about 400 bytes)
*                         but only first 60 chars will be displayed
*
*
*  for each mapset, lister() will be called once with
*  name set to the empty string "" in order to get an title for the
*  list.  Set buf to null to suppress title, otherwise copy title
*  into buf.  The title will start above the text for the files.
*
*  then for each file in each mapset, lister() will be called
*  to obtain infomation about the file.
*
* also:
*   G_set_ask_return_msg (msg) char *msg;
*   can be used to change the hit RETURN to cancel request message
*   displayed during the ask prompting.
*
*   G_get_ask_return_msg() will return the msg.
******************************************************************/
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "gis.h"
#include "glocale.h"

/*
 * OLD references any mapset
 * NEW, ANY, PRJ are for the current mapset only
 *
 * OLD means must exist in some mapset
 * NEW means must not exist in current mapset
 * ANY means just get a name. If file exists, (optionally) warn user.
 * PRJ means must exist in current mapset
 */

#define OLD 0
#define NEW 1
#define PRJ 2
#define ANY 3
#define ANY_NW 4
#define OLD_FILE 5
#define NEW_FILE 6

static char *ask_return_msg = 0 ;
static char clear_return_msg = 0 ;
static int (*no_lister)() = 0 ;
static int parselist(char *, int , char *);
static char *ask( char *,char *,char *,char *,char *, int (*)(),int );


/*!
 * \brief prompt for new database file
 *
 * The user is asked to enter the name of
 * a new file which does not exist in the current mapset.
 * <b>Note.</b> The file chosen by the user may exist in other mapsets. This
 * routine does not look in other mapsets, since the assumption is that
 * <b>name</b> will be used to create a new file. New files are always created
 * in the current mapset.
 *
 *  \param prompt
 *  \param name
 *  \param element
 *  \param desc
 *  \return char * 
 */

char *G_ask_new (prompt, name, element, desc)
    char *prompt;
    char *name;
    char *element;
    char *desc;
{
    return ask (prompt, name, element, desc, (char *) NULL, no_lister, NEW);
}

char *G_ask_new_ext (prompt, name, element, desc, option, lister)
    char *prompt;
    char *name;
    char *element;
    char *desc;
    char *option;
    int (*lister)();
{
    return ask (prompt, name, element, desc, option, lister, NEW);
}


/*!
 * \brief prompt for existing database file
 *
 * The user is asked to enter the
 * name of an existing database file.
 * <b>Note.</b> This routine looks for the file in the current mapset as well
 * as other mapsets. The mapsets that are searched are determined from the user's
 * mapset search path. See Mapset_Search_Path for some more details
 * about the search path.
 *
 *  \param prompt
 *  \param name
 *  \param element
 *  \param label
 *  \return char * 
 */

char *G_ask_old (prompt, name, element, desc)
    char *prompt;
    char *name;
    char *element;
    char *desc;
{
    return ask (prompt, name, element, desc, (char *) NULL, no_lister, OLD);
}

char *G_ask_old_ext (prompt, name, element, desc, option, lister)
    char *prompt;
    char *name;
    char *element;
    char *desc;
    char *option;
    int (*lister)();
{
    return ask (prompt, name, element, desc, option, lister, OLD);
}


/*!
 * \brief prompt for any valid file name
 *
 * The user is asked to enter
 * any leg al file name. If <b>warn</b> is 1 and the file chosen exists in the
 * current mapset, then the user is asked if it is ok to overwrite the file. If
 * <b>warn</b> is 0, then any leg al name is accepted and no warning is issued
 * to the user if the file exists.
 *
 *  \param prompt
 *  \param name
 *  \param element
 *  \param label
 *  \param warn
 *  \return char * 
 */

char *G_ask_any (prompt, name, element, desc, warn)
    char *prompt;
    char *name;
    char *element;
    char *desc;
{
    return ask (prompt, name, element, desc, (char *) NULL, no_lister, warn?ANY:ANY_NW);
}

char *G_ask_any_ext (prompt, name, element, desc, warn, option, lister)
    char *prompt;
    char *name;
    char *element;
    char *desc;
    char *option;
    int (*lister)();
{
    return ask (prompt, name, element, desc, option, lister, warn?ANY:ANY_NW);
}


/*!
 * \brief prompt for existing database file
 *
 * The user is asked to enter the
 * name of an file which exists in the current mapset.
 * <b>Note.</b> The file chosen by the user may or may not exist in other
 * mapsets. This routine does not look in other mapsets, since the assumption is
 * that <b>name</b> will be used to modify a file. GRASS only permits users to
 * modify files in the current mapset.
 *
 *  \param prompt
 *  \param name
 *  \param element
 *  \param label
 *  \return char * 
 */

char *
G_ask_in_mapset (prompt, name, element, desc)
    char *prompt;
    char *name;
    char *element;
    char *desc;
{
    return ask (prompt, name, element, desc, (char *) NULL, no_lister, PRJ);
}

char *
G_ask_in_mapset_ext (prompt, name, element, desc, option, lister)
    char *prompt;
    char *name;
    char *element;
    char *desc;
    char *option;
    int (*lister)();
{
    return ask (prompt, name, element, desc, option, lister, PRJ);
}


/*!
 * \brief prompt for new file
 *
 * The user is asked to enter the name of an file which doesn't exist.
 *
 *  \param prompt
 *  \param name
 *  \param element
 *  \param label
 *  \return char * 
 */

char *
G_ask_new_file (prompt, name, element, desc)
    char *prompt;
    char *name;
    char *element;
    char *desc;
{
    /* element is a dummy parameter for this function */
    return ask (prompt, name, element, desc, (char *) NULL, no_lister, NEW_FILE);
}

/* do we need this function?
char *
G_ask_new_file_ext (prompt, name, element, desc, option, lister)
    char *prompt;
    char *name;
    char *element;
    char *desc;
    char *option;
    int (*lister)();
{
    return ask (prompt, name, element, desc, option, lister, NEW_FILE);
}
*/


/*!
 * \brief prompt for existing file
 *
 * The user is asked to enter the name of an file which exists.
 *
 *  \param prompt
 *  \param name
 *  \param element
 *  \param label
 *  \return char * 
 */

char *
G_ask_old_file (prompt, name, element, desc)
    char *prompt;
    char *name;
    char *element;
    char *desc;
{
    /* element is a dummy parameter for this function */
    return ask (prompt, name, element, desc, (char *) NULL, no_lister, OLD_FILE);
}

/* do we need this function?
char *
G_ask_old_file_ext (prompt, name, element, desc, option, lister)
    char *prompt;
    char *name;
    char *element;
    char *desc;
    char *option;
    int (*lister)();
{
    return ask (prompt, name, element, desc, option, lister, OLD_FILE);
}
*/


/*!
 * \brief set Hit RETURN msg
 *
 * The "Hit
 * RETURN to cancel request" part of the prompt in the prompting routines
 * described above, is modified to "Hit RETURN <b>msg.</b>"
 *
 *  \param msg
 *  \return int
 */

int G_set_ask_return_msg (char *msg)
{
    if (ask_return_msg) free (ask_return_msg);
    ask_return_msg = G_store (msg);
    clear_return_msg = 0;

    return 0;
}


/*!
 * \brief get Hit RETURN msg
 *
 *  The current
 * <i>msg</i> (as set by <i>G_set_ask_return_msg</i>) is returned.
 *
 *  \param void
 *  \return char * 
 */

char *G_get_ask_return_msg()
{
    static char none[80];
	strcpy(none,_("to cancel request"));
    return (ask_return_msg == NULL ? none : ask_return_msg);
}

static char *ask (
    char *prompt,
    char *name,
    char *element,
    char *desc,
    char *option,
    int (*lister)(),
    int type)
{
    char tmapset[256];
    char xname[512], xmapset[512];
    int  name_is_qualified;
    int  ok;
    char tprompt[256];
    char input[256];
    char *mapset;
    char *cur_mapset;

    G__check_gisinit();

    fflush (stdout);
/* RETURN msg */
    if(clear_return_msg)
    {
	free (ask_return_msg);
	ask_return_msg = 0;
    }
    clear_return_msg = ask_return_msg ? 1 : 0;

/* make sure option is valid */
    if (lister && (option == 0 || *option == 0))
	lister = 0;

/* set name to NO NAME at outset */
    *name = 0;

/*
 * if element description not given, make it the same as the
 * element name
 */
    if (desc == 0 || *desc == 0)
	    desc = element;

/*
 * if no prompt is given, build an approriate prompt
 */
    if (prompt == 0 || *prompt == 0)
    {
	switch (type)
	{
	case NEW:
	case NEW_FILE:
	    sprintf(prompt = tprompt,_("Enter a new %s file name"), desc);
	    break;
	case OLD:
	case PRJ:
	case OLD_FILE:
	    sprintf(prompt = tprompt,_("Enter the name of an existing %s file"), desc);
	    break;
	default:
	    sprintf(prompt = tprompt,_("Enter %s file name"), desc);
	    break;
	}
    }

/*
 * get the current mapset name
 */
    cur_mapset = G_mapset();

    while (1)
    {
/*
 * print the prompt and input the request
 */
	do{
	    fprintf (stderr,"\n%s\n", prompt);
	    /* no listing function implemented for old_file and new_file */
	    if (type != OLD_FILE && type != NEW_FILE)
	        fprintf (stderr,_("Enter 'list' for a list of existing %s files\n"), desc);
	    if (lister)
	    {
		fprintf (stderr,_("Enter 'list -f' for "));
		if (option && *option)
		    fprintf (stderr,_("a list %s"), option);
		else
		    fprintf (stderr,_("an extended list"));
		fprintf (stderr,"\n");
	    }

	    fprintf (stderr,_("Hit RETURN %s\n"), G_get_ask_return_msg());
	    fprintf (stderr,"> ");
	}
	while (!G_gets(input));

	G_strip (input);
	fprintf (stderr,"<%s>\n", input);

/*
 * if the user just hit return (or blanks only)
 * return NULL
 */
	if (*input == 0)
	    return 0;

	if (type == OLD_FILE || type == NEW_FILE)
	{
	    int exist;

	    exist = (access(input, 0) == 0);
	    if (type == OLD_FILE && !exist)
	    {
	    	fprintf (stderr,_("\n** %s - not found **\n"), input);
	    	continue;
	    }
	    if (type == NEW_FILE && exist)
	    {
		char question[200];
		sprintf(question,
			_("\n** %s exists. ok to overwrite? "), input);
	    	if (!G_yes (question,0))
			continue;
	    }
	    strcpy (name,input);
	    return G_store (input);
	}
/*
 * 'list' does a list without extension. if we are looking for a new
 * file only list the current mapset. Otherwise list all mapsets
 * in the mapset search list
 *
 * 0  not a list request
 * 1  list
 * 2  list -f
 * 3  list mapset
 * 4  list -f mapset
 */
  
	switch (parselist (input, lister?1:0, tmapset))
	{
	case 0:
	    break;
	case 1:
	    G_list_element (element, desc, type==OLD?"":cur_mapset, no_lister);
	    continue;
	case 2:
	    G_list_element (element, desc, type==OLD?"":cur_mapset, lister);
	    continue;
	case 3:
	    G_list_element (element, desc, tmapset, no_lister);
	    continue;
	case 4:
	    G_list_element (element, desc, tmapset, lister);
	    continue;
	default:
	    fprintf (stderr,"** illegal request **\n");
	    continue;
	}

	if((name_is_qualified = G__name_is_fully_qualified (input, xname, xmapset)))
	    ok = G_legal_filename (xname) >= 0;
	else
	    ok = G_legal_filename (input) >= 0;
	if (!ok)
	{
	    fprintf (stderr,_("\n**<%s> illegal name **\n"), input);
	    continue;
	}
/*
 * now look for the file.
 *
 * new files must be simple names
 * and must not exist in the current mapset
 */
	if (type != OLD)
	{
	    if (name_is_qualified)
	    {
		if(strcmp (cur_mapset, xmapset) != 0)
		{
		    fprintf (stderr,_("\n** %s - illegal request **\n"), input);
		    continue;
		}
		strcpy (input, xname);
	    }
	    mapset = G_find_file (element, input, cur_mapset);
	    switch (type)
	    {
	    case NEW:

		if (!mapset)
		{
		    strcpy (name,input);
		    return cur_mapset;
		}
		fprintf (stderr,_("\n** %s - exists, select another name **\n"), input);
		break;

	    case ANY:
	    case ANY_NW:

		if (mapset && type == ANY)
		{
		    char question[200];
		    sprintf(question,
			_("\n** %s exists. ok to overwrite? "), input);
		    if (!G_yes (question,0))
			break;
		}
		strcpy (name,input);
		return cur_mapset;

	    case PRJ:

		if (mapset)
		{
		    strcpy (name,input);
		    return cur_mapset;
		}
		fprintf (stderr,_("\n** %s - not found **\n"), input);
		break;

	    default:
		G_fatal_error (_("ask: can't happen"));
	    }
	}
/*
 * old names can be simple or qualified
 * and must exist
 */
	else
	{
	    mapset = G_find_file (element, input, "");
	    if (mapset)
	    {
		if (name_is_qualified)
		    strcpy (name,xname);
		else
		    strcpy (name,input);
		return mapset;
	    }
	    fprintf (stderr,_("\n** %s - not found **\n"), input);
	}
    }

    return NULL;
}

static int parselist ( char *input, int option, char *mapset)
{
    char list[30];
    char f1[30];
    char f2[30];
    char f3[30];
    int count;

    *list = *f1 = *f2 = 0;
    count = sscanf (input, "%s%s%s%s", list, f1, f2, f3);
    if (count < 1)
	return 0;
    if (strcmp (list, "list") != 0)
	return 0;

    if (count == 1) return 1;	/* list */
    if (count > 3)  return -1;	/* illegal */

    if (*f1 == '-')		/* list -f */
    {
	if (!option)
	    return -1;
	if (f1[1] == 0 || f1[1] != 'f' || f1[2] != 0)
	    return -1;
	if (count == 2)
	    return 2;
	strcpy (mapset, f2);
	    return 4;
    }
    if (count != 2)
	return -1;
    strcpy (mapset, f1);
    return 3;
}