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: 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(cfs_opendir(&dir, "/") != 0) {
86  shell_output_str(&ls_command, "Cannot open directory", "");
87  } else {
88  totsize = 0;
89  while(cfs_readdir(&dir, &dirent) == 0) {
90  totsize += dirent.size;
91  sprintf(buf, "%lu ", (unsigned long)dirent.size);
92  /* printf("'%s'\n", dirent.name);*/
93  shell_output_str(&ls_command, buf, dirent.name);
94  }
95  cfs_closedir(&dir);
96  sprintf(buf, "%lu", (unsigned long)totsize);
97  shell_output_str(&ls_command, "Total size: ", buf);
98  }
99  PROCESS_END();
100 }
101 /*---------------------------------------------------------------------------*/
102 PROCESS_THREAD(shell_append_process, ev, data)
103 {
104  static int fd = 0;
105  struct shell_input *input;
106 
108 
109  PROCESS_BEGIN();
110 
111  fd = cfs_open(data, CFS_WRITE | CFS_APPEND);
112 
113  if(fd < 0) {
114  shell_output_str(&append_command,
115  "append: could not open file for writing: ", data);
116  } else {
117  while(1) {
119  input = data;
120  /* printf("cat input %d %d\n", input->len1, input->len2);*/
121  if(input->len1 + input->len2 == 0) {
122  cfs_close(fd);
123  PROCESS_EXIT();
124  }
125 
126  cfs_write(fd, input->data1, input->len1);
127  cfs_write(fd, input->data2, input->len2);
128 
129  shell_output(&append_command,
130  input->data1, input->len1,
131  input->data2, input->len2);
132  }
133  }
134 
135  PROCESS_END();
136 }
137 /*---------------------------------------------------------------------------*/
138 PROCESS_THREAD(shell_write_process, ev, data)
139 {
140  static int fd = 0;
141  struct shell_input *input;
142  int r;
143 
145 
146  PROCESS_BEGIN();
147 
148  fd = cfs_open(data, CFS_WRITE);
149 
150  if(fd < 0) {
151  shell_output_str(&write_command,
152  "write: could not open file for writing: ", data);
153  } else {
154  while(1) {
156  input = data;
157  /* printf("cat input %d %d\n", input->len1, input->len2);*/
158  if(input->len1 + input->len2 == 0) {
159  cfs_close(fd);
160  PROCESS_EXIT();
161  }
162 
163  r = 0;
164  if(input->len1 > 0) {
165  r = cfs_write(fd, input->data1, input->len1);
166  }
167 
168  if(r >= 0 && input->len2 > 0) {
169  r = cfs_write(fd, input->data2, input->len2);
170  }
171 
172  if(r < 0) {
173  shell_output_str(&write_command, "write: could not write to the file",
174  NULL);
175  } else {
176  shell_output(&write_command,
177  input->data1, input->len1,
178  input->data2, input->len2);
179  }
180  }
181  }
182 
183  PROCESS_END();
184 }
185 /*---------------------------------------------------------------------------*/
186 PROCESS_THREAD(shell_read_process, ev, data)
187 {
188  static int fd = 0;
189  static int block_size = MAX_BLOCKSIZE;
190  char *next;
191  char filename[MAX_FILENAME_LEN];
192  int len;
193  int offset = 0;
194  char buf[MAX_BLOCKSIZE];
195  struct shell_input *input;
196 
198  PROCESS_BEGIN();
199 
200  if(data != NULL) {
201  next = strchr(data, ' ');
202  if(next == NULL) {
203  strncpy(filename, data, sizeof(filename));
204  } else {
205  len = (int)(next - (char *)data);
206  if(len <= 0) {
207  shell_output_str(&read_command,
208  "read: filename too short: ", data);
209  PROCESS_EXIT();
210  }
211  if(len > MAX_FILENAME_LEN) {
212  shell_output_str(&read_command,
213  "read: filename too long: ", data);
214  PROCESS_EXIT();
215  }
216  memcpy(filename, data, len);
217  filename[len] = 0;
218 
219  offset = shell_strtolong(next, NULL);
220  next++;
221  next = strchr(next, ' ');
222  if(next != NULL) {
223  block_size = shell_strtolong(next, NULL);
224  if(block_size > MAX_BLOCKSIZE) {
225  shell_output_str(&read_command,
226  "read: block size too large: ", data);
227  PROCESS_EXIT();
228  }
229  }
230  }
231 
232  fd = cfs_open(filename, CFS_READ);
233  cfs_seek(fd, offset, CFS_SEEK_SET);
234 
235  if(fd < 0) {
236  shell_output_str(&read_command,
237  "read: could not open file for reading: ", filename);
238  } else {
239 
240  while(1) {
241  len = cfs_read(fd, buf, block_size);
242  if(len <= 0) {
243  cfs_close(fd);
244  PROCESS_EXIT();
245  }
246  shell_output(&read_command,
247  buf, len, "", 0);
248 
249  process_post(&shell_read_process, PROCESS_EVENT_CONTINUE, NULL);
250  PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_CONTINUE ||
251  ev == shell_event_input);
252 
253  if(ev == shell_event_input) {
254  input = data;
255  /* printf("cat input %d %d\n", input->len1, input->len2);*/
256  if(input->len1 + input->len2 == 0) {
257  cfs_close(fd);
258  PROCESS_EXIT();
259  }
260  }
261  }
262  }
263  }
264 
265  PROCESS_END();
266 }
267 /*---------------------------------------------------------------------------*/
268 PROCESS_THREAD(shell_rm_process, ev, data)
269 {
270  PROCESS_BEGIN();
271 
272  if(data != NULL) {
273  cfs_remove(data);
274  }
275  PROCESS_END();
276 }
277 /*---------------------------------------------------------------------------*/
278 void
279 shell_file_init(void)
280 {
281  shell_register_command(&ls_command);
282  shell_register_command(&write_command);
283  shell_register_command(&append_command);
284  shell_register_command(&read_command);
285  shell_register_command(&rm_command);
286 }
287 /*---------------------------------------------------------------------------*/
A brief description of what this file is.
int cfs_open(const char *name, int flags)
Open a file.
Definition: cfs-coffee.c:996
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_EXIT()
Exit the currently running process.
Definition: process.h:200
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:1042
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define CFS_WRITE
Specify that cfs_open() should open a file for writing.
Definition: cfs.h:104
#define NULL
The null pointer.
#define CFS_READ
Specify that cfs_open() should open a file for reading.
Definition: cfs.h:90
int cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *record)
Read a directory entry.
Definition: cfs-coffee.c:1264
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
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 CFS_APPEND
Specify that cfs_open() should append written data to the file rather than overwriting it...
Definition: cfs.h:118
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
unsigned long shell_strtolong(const char *str, const char **retstr)
Convert a string to a number.
Definition: shell.c:521
int cfs_remove(const char *name)
Remove a file.
Definition: cfs-coffee.c:1074
#define PROCESS_EXITHANDLER(handler)
Specify an action when a process exits.
Definition: process.h:254
Structure for shell input data.
Definition: shell.h:365
void cfs_closedir(struct cfs_dir *dir)
Close a directory opened with cfs_opendir().
Definition: cfs-coffee.c:1290
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
int shell_event_input
The event number for shell input data.
Definition: shell.c:70
int cfs_opendir(struct cfs_dir *dir, const char *name)
Open a directory for reading directory entries.
Definition: cfs-coffee.c:1253
#define CFS_SEEK_SET
Specify that cfs_seek() should compute the offset from the beginning of the file. ...
Definition: cfs.h:127
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219
void cfs_close(int fd)
Close an open file.
Definition: cfs-coffee.c:1032
CFS header file.