+Real implementation of 'captive-cmdline' client
[captive.git] / src / client / cmdline / cmd_cd.c
1 /* $Id$
2  * client cmdline interface command "cd" for libcaptive
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <glib/gerror.h>
24 #include <popt.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "captive/client-directory.h"
28 #include <glib/gprintf.h>
29
30 #include "cmd_cd.h"     /* self */
31 #include "main.h"
32
33
34 /* Config: */
35 #define STATICS_NUM (5)
36
37
38 const gchar *cmdline_cwd;
39
40
41 GQuark cmdline_cmd_cd_error_quark(void)
42 {
43 GQuark r=0;
44
45         if (!r)
46                 r=g_quark_from_static_string("cmdline-cmd-cd");
47
48         return r;
49 }
50
51
52 const struct poptOption cmd_cd_table[]={
53                 POPT_AUTOHELP
54                 POPT_TABLEEND
55                 };
56
57
58 /* Returns: statically allocated absolute pathname string */
59 G_CONST_RETURN gchar *cmdline_path_from_cwd(const gchar *relative)
60 {
61 static gchar *statics[STATICS_NUM];
62 static int staticsi=0;
63 gchar *r,*r_end,*s,*d;
64
65         /* 'relative' may be NULL for the '.' meaning */
66
67         if (!cmdline_cwd || (relative && *relative==G_DIR_SEPARATOR)) { /* bootstrap or absolute */
68                 g_assert(g_path_is_absolute(relative));
69                 r=g_strdup(relative);
70                 }
71         else if (!relative)
72                 r=g_strdup(cmdline_cwd);
73         else
74                 r=g_build_filename(cmdline_cwd,relative,NULL);
75         g_assert(g_path_is_absolute(r));
76
77         /* coalesce '/'es */
78         for (d=s=r;*s;s++) {
79                 if (*s==G_DIR_SEPARATOR && d>r && d[-1]==G_DIR_SEPARATOR)
80                         continue;
81                 *d++=*s;
82                 }
83         g_assert(d>r);
84         if (d>(r+1) && d[-1]==G_DIR_SEPARATOR)
85                 d--;
86         *d++=G_DIR_SEPARATOR;
87         r_end=d;
88
89         /* 'r' is NOT NULL-terminated here! */
90
91         for (d=s=r+1;s<r_end;) {
92                 if (!strncmp(s,"./",2)) {
93                         s+=2;
94                         continue;
95                         }
96                 if (!strncmp(s,"../",3)) {
97                         s+=3;
98                         g_assert(d[-1]==G_DIR_SEPARATOR);
99                         if (d>r+1) {
100                                 do {
101                                         d--;
102                                         } while (d[-1]!=G_DIR_SEPARATOR);
103                                 }
104                         continue;
105                         }
106                 *d++=*s++;
107                 }
108         g_assert(d[-1]==G_DIR_SEPARATOR);       /* trailing '/' */
109         if (d>r+1)      /* leave at least "/" */
110                 d--;
111         *d='\0';
112
113         g_assert(g_path_is_absolute(r));
114
115         g_free(statics[staticsi]);
116         statics[staticsi++]=r;
117         staticsi%=G_N_ELEMENTS(statics);
118
119         return r;
120 }
121
122
123 void cmd_cd(const char **cmd_argv,GError **errp)
124 {
125 CaptiveDirectoryObject *captive_directory_object;
126
127         g_return_if_fail(!errp || !*errp);
128
129         if (cmd_argv[0]) {
130 const gchar *targetdir=cmdline_path_from_cwd(cmd_argv[0]);
131
132                 if (!errvfsresult_to_gerr(errp,captive_directory_new_open(
133                                 &captive_directory_object,      /* captive_directory_object_return */
134                                 targetdir))) {  /* pathname */
135                         err_cleanup(errp);
136                         g_set_error(errp,CMDLINE_CMD_CD_ERROR,CMDLINE_CMD_CD_ERROR_CANNOT_OPEN_DIRECTORY,
137                                         _("Cannot open directory: %s"),targetdir);
138                         return;
139                         }
140
141                 g_object_unref(captive_directory_object);
142
143                 g_free((/*de-const*/ gchar *)cmdline_cwd);
144                 cmdline_cwd=g_strdup(targetdir);
145                 g_assert(g_path_is_absolute(cmdline_cwd));
146                 }
147
148         g_printf("Guest-OS CWD: %s\n",cmdline_cwd);
149 }