Contiki 3.x
shell-file.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * File-related shell commands
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "contiki.h"
41 #include "shell-file.h"
42 #include "cfs/cfs.h"
43 
44 #include <stdio.h>
45 #include <string.h>
46 
47 #define MAX_FILENAME_LEN 40
48 #define MAX_BLOCKSIZE 40
49 
50 /*---------------------------------------------------------------------------*/
51 PROCESS(shell_ls_process, "ls");
52 SHELL_COMMAND(ls_command,
53  "ls",
54  "ls <dirname>: list files",
55  &shell_ls_process);
56 PROCESS(shell_append_process, "append");
57 SHELL_COMMAND(append_command,
58  "append",
59  "append <filename>: append to file",
60  &shell_append_process);
61 PROCESS(shell_write_process, "write");
62 SHELL_COMMAND(write_command,
63  "write",
64  "write <filename>: write to file",
65  &shell_write_process);
66 PROCESS(shell_read_process, "read");
67 SHELL_COMMAND(read_command,
68  "read",
69  "read <filename> [offset] [block size]: read from a file, with the offset and the block size as options",
70  &shell_read_process);
71 PROCESS(shell_rm_process, "rm");
72 SHELL_COMMAND(rm_command,
73  "rm",
74  "rm <filename>: remove the file named filename",
75  &shell_rm_process);
76 /*---------------------------------------------------------------------------*/
77 PROCESS_THREAD(shell_ls_process, ev, data)
78 {
79  static struct cfs_dir dir;
80  static cfs_offset_t totsize;
81  struct cfs_dirent dirent;
82  char buf[32];
83  PROCESS_BEGIN();
84 
85  if(data != NULL) {
86  if(cfs_opendir(&dir, data) != 0) {
87  shell_output_str(&ls_command, "Cannot open directory", "");
88  } else {
89  totsize = 0;
90  while(cfs_readdir(&dir, &dirent) == 0) {
91  totsize += dirent.size;
92  sprintf(buf, "%lu ", (unsigned long)dirent.size);
93  /* printf("'%s'\n", dirent.name);*/
94  shell_output_str(&ls_command, buf, dirent.name);
95  }
96  cfs_closedir(&dir);
97  sprintf(buf, "%lu", (unsigned long)totsize);
98  shell_output_str(&ls_command, "Total size: ", buf);
99  }
100  }
101  PROCESS_END();
102 }
103 /*---------------------------------------------------------------------------*/
104 PROCESS_THREAD(shell_append_process, ev, data)
105 {
106  static int fd = 0;
107  struct shell_input *input;
108 
110 
111  PROCESS_BEGIN();
112 
113  fd = cfs_open(data, CFS_WRITE | CFS_APPEND);
114 
115  if(fd < 0) {
116  shell_output_str(&append_command,
117  "append: could not open file for writing: ", data);
118  } else {
119  while(1) {
121  input = data;
122  /* printf("cat input %d %d\n", input->len1, input->len2);*/
123  if(input->len1 + input->len2 == 0) {
124  cfs_close(fd);
125  PROCESS_EXIT();
126  }
127 
128  cfs_write(fd, input->data1, input->len1);
129  cfs_write(fd, input->data2, input->len2);
130 
131  shell_output(&append_command,
132  input->data1, input->len1,
133  input->data2, input->len2);
134  }
135  }
136 
137  PROCESS_END();
138 }
139 /*---------------------------------------------------------------------------*/
140 PROCESS_THREAD(shell_write_process, ev, data)
141 {
142  static int fd = 0;
143  struct shell_input *input;
144  int r;
145 
147 
148  PROCESS_BEGIN();
149 
150  fd = cfs_open(data, CFS_WRITE);
151 
152  if(fd < 0) {
153  shell_output_str(&write_command,
154  "write: could not open file for writing: ", data);
155  } else {
156  while(1) {
158  input = data;
159  /* printf("cat input %d %d\n", input->len1, input->len2);*/
160  if(input->len1 + input->len2 == 0) {
161  cfs_close(fd);
162  PROCESS_EXIT();
163  }
164 
165  r = 0;
166  if(input->len1 > 0) {
167  r = cfs_write(fd, input->data1, input->len1);
168  }
169 
170  if(r >= 0 && input->len2 > 0) {
171  r = cfs_write(fd, input->data2, input->len2);
172  }
173 
174  if(r < 0) {
175  shell_output_str(&write_command, "write: could not write to the file",
176  NULL);
177  } else {
178  shell_output(&write_command,
179  input->data1, input->len1,
180  input->data2, input->len2);
181  }
182  }
183  }
184 
185  PROCESS_END();
186 }
187 /*---------------------------------------------------------------------------*/
188 PROCESS_THREAD(shell_read_process, ev, data)
189 {
190  static int fd = 0;
191  static int block_size = MAX_BLOCKSIZE;
192  char *next;
193  char filename[MAX_FILENAME_LEN];
194  int len;
195  int offset = 0;
196  char buf[MAX_BLOCKSIZE];
197  struct shell_input *input;
198 
200  PROCESS_BEGIN();
201 
202  if(data != NULL) {
203  next = strchr(data, ' ');
204  if(next == NULL) {
205  strncpy(filename, data, sizeof(filename));
206  } else {
207  len = (int)(next - (char *)data);
208  if(len <= 0) {
209  shell_output_str(&read_command,
210  "read: filename too short: ", data);
211  PROCESS_EXIT();
212  }
213  if(len > MAX_FILENAME_LEN) {
214  shell_output_str(&read_command,
215  "read: filename too long: ", data);
216  PROCESS_EXIT();
217  }
218  memcpy(filename, data, len);
219  filename[len] = 0;
220 
221  offset = shell_strtolong(next, NULL);
222  next++;
223  next = strchr(next, ' ');
224  if(next != NULL) {
225  block_size = shell_strtolong(next, NULL);
226  if(block_size > MAX_BLOCKSIZE) {
227  shell_output_str(&read_command,
228  "read: block size too large: ", data);
229  PROCESS_EXIT();
230  }
231  }
232  }
233 
234  fd = cfs_open(filename, CFS_READ);
235  cfs_seek(fd, offset, CFS_SEEK_SET);
236 
237  if(fd < 0) {
238  shell_output_str(&read_command,
239  "read: could not open file for reading: ", filename);
240  } else {
241 
242  while(1) {
243  len = cfs_read(fd, buf, block_size);
244  if(len <= 0) {
245  cfs_close(fd);
246  PROCESS_EXIT();
247  }
248  shell_output(&read_command,
249  buf, len, "", 0);
250 
251  process_post(&shell_read_process, PROCESS_EVENT_CONTINUE, NULL);
252  PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_CONTINUE ||
253  ev == shell_event_input);
254 
255  if(ev == shell_event_input) {
256  input = data;
257  /* printf("cat input %d %d\n", input->len1, input->len2);*/
258  if(input->len1 + input->len2 == 0) {
259  cfs_close(fd);
260  PROCESS_EXIT();
261  }
262  }
263  }
264  }
265  }
266 
267  PROCESS_END();
268 }
269 /*---------------------------------------------------------------------------*/
270 PROCESS_THREAD(shell_rm_process, ev, data)
271 {
272  PROCESS_BEGIN();
273 
274  if(data != NULL) {
275  cfs_remove(data);
276  }
277  PROCESS_END();
278 }
279 /*---------------------------------------------------------------------------*/
280 void
281 shell_file_init(void)
282 {
283  shell_register_command(&ls_command);
284  shell_register_command(&write_command);
285  shell_register_command(&append_command);
286  shell_register_command(&read_command);
287  shell_register_command(&rm_command);
288 }
289 /*---------------------------------------------------------------------------*/
int cfs_open(const char *name, int flags)
Open a file.
Definition: cfs-coffee.c:1011
#define CFS_APPEND
Specify that cfs_open() should append written data to the file rather than overwriting it...
Definition: cfs.h:118
#define CFS_WRITE
Specify that cfs_open() should open a file for writing.
Definition: cfs.h:104
int cfs_opendir(struct cfs_dir *dir, const char *name)
Open a directory for reading directory entries.
Definition: cfs-coffee.c:1283
void cfs_close(int fd)
Close an open file.
Definition: cfs-coffee.c:1047
#define CFS_SEEK_SET
Specify that cfs_seek() should compute the offset from the beginning of the file. ...
Definition: cfs.h:127
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
Definition: shell.c:383
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1503
unsigned long shell_strtolong(const char *str, const char **retstr)
Convert a string to a number.
Definition: shell.c:521
#define CFS_READ
Specify that cfs_open() should open a file for reading.
Definition: cfs.h:90
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
void shell_output(struct shell_command *c, void *data1, int len1, const void *data2, int len2)
Output data from a shell command.
Definition: shell.c:395
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219
int shell_event_input
The event number for shell input data.
Definition: shell.c:70
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
#define PROCESS_EXITHANDLER(handler)
Specify an action when a process exits.
Definition: process.h:254
#define NULL
The null pointer.
Structure for shell input data.
Definition: shell.h:365
int cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *record)
Read a directory entry.
Definition: cfs-coffee.c:1294
int cfs_remove(const char *name)
Remove a file.
Definition: cfs-coffee.c:1094
void cfs_closedir(struct cfs_dir *dir)
Close a directory opened with cfs_opendir().
Definition: cfs-coffee.c:1320
cfs_offset_t cfs_seek(int fd, cfs_offset_t offset, int whence)
Seek to a specified position in an open file.
Definition: cfs-coffee.c:1057
CFS header file.
A brief description of what this file is.
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120