1 
2 /****************************************************************************
3  *
4  * MODULE:       g.dirseps
5  * AUTHOR(S):    Paul Kelly
6  * PURPOSE:      Copies input string to stdout, changing directory separator
7  *               characters as specified by flags.
8  *               Used for interoperability between Unix and Windows
9  *               pathnames.
10  * COPYRIGHT:    (C) 2006 by the GRASS Development Team
11  *
12  *               This program is free software under the GNU General Public
13  *               License (>=v2). Read the file COPYING that comes with GRASS
14  *               for details.
15  *
16  *****************************************************************************/
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <grass/gis.h>
21 #include <grass/glocale.h>
22 
main(int argc,char * argv[])23 int main(int argc, char *argv[])
24 {
25     struct Flag *tohost, *tograss;
26     struct Option *path;
27     struct GModule *module;
28 
29     G_set_program_name(argv[0]);
30     G_no_gisinit();
31     G_set_gisrc_mode(G_GISRC_MODE_MEMORY);
32 
33     module = G_define_module();
34     G_add_keyword(_("general"));
35     G_add_keyword(_("map management"));
36     G_add_keyword(_("scripts"));
37     module->label =
38 	_("Internal GRASS utility for converting directory separator characters.");
39     module->description =
40 	"Converts any directory separator characters in "
41 	"the input string to or from native host format, and writes the changed "
42 	"path to standard output. Useful in scripts for Windows compatibility.";
43 
44     tohost = G_define_flag();
45     tohost->key = 'h';
46     tohost->description =
47 	"Convert directory separators to native host format";
48 
49     tograss = G_define_flag();
50     tograss->key = 'g';
51     tograss->description =
52 	"Convert directory separators to GRASS internal format";
53 
54     path = G_define_option();
55     path->key = "path";
56     path->type = TYPE_STRING;
57     path->required = NO;
58     path->description =
59 	"Path to be converted (read from stdin if not specified)";
60 
61     if (G_parser(argc, argv))
62 	exit(EXIT_FAILURE);
63 
64     if (!tohost->answer && !tograss->answer)
65 	G_fatal_error("One of flags -%c or -%c must be specified!",
66 		      tohost->key, tograss->key);
67 
68     if (tohost->answer && tograss->answer)
69 	G_fatal_error("Only one of flags -%c or -%c can be specified!",
70 		      tohost->key, tograss->key);
71 
72     if (path->answer) {
73 	/* Take input from command-line option */
74 	char *pathstring = G_store(path->answer);
75 
76 	if (tohost->answer)
77 	    G_convert_dirseps_to_host(pathstring);
78 
79 	if (tograss->answer)
80 	    G_convert_dirseps_from_host(pathstring);
81 
82 	puts(pathstring);
83 
84     }
85     else {
86 	char inchar;
87 
88 	while ((inchar = getc(stdin)) != EOF) {
89 	    /* Read a character at a time from stdin until EOF
90 	     * and copy to stdout after any conversion */
91 	    if (tohost->answer) {
92 		if (inchar == GRASS_DIRSEP)
93 		    inchar = HOST_DIRSEP;
94 	    }
95 
96 	    if (tograss->answer) {
97 		if (inchar == HOST_DIRSEP)
98 		    inchar = GRASS_DIRSEP;
99 	    }
100 
101 	    putchar(inchar);
102 
103 	}
104     }
105 
106     fflush(stdout);
107 
108     exit(EXIT_SUCCESS);
109 
110 }
111