ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
posix_tests.c
Go to the documentation of this file.
1 
26 #include "user_config.h"
27 
28 #include "fatfs.h"
29 
30 #ifdef AVR
31 //#include <stdio.h>
32 #include <stdlib.h>
33 #endif
34 #include "lib/time.h"
35 #include "lib/stringsup.h"
36 #include "printf/mathio.h"
37 #include "posix.h"
38 #include "posix_tests.h"
39 
40 #ifdef ESP8266
41 #define BUFSIZE 4096
42 #else
43 #define BUFSIZE 512
44 #endif
45 MEMSPACE
46 void posix_help(int full)
47 {
48  printf("posix help\n");
49  if(full)
50  {
51  printf(
52 #ifdef POSIX_TESTS
53  "posix prefix is optional\n"
54 #endif
55  "posix chmod file NNN\n"
56  "posix cat file [-p]\n"
57  "posix cd dir\n"
58  "posix copy file1 file2\n"
59  "posix hexdump file [-p]\n"
60  "posix log str\n"
61  "posix ls dir [-l]\n"
62  "posix mkdir dir\n"
63  "posix mkfs\n"
64  "posix page NN"
65  "posix pwd\n"
66  "posix stat file\n"
67  "posix sum file\n"
68  "posix rm file\n"
69  "posix rmdir dir\n"
70  "posix rename old new\n"
71  "posix upload file\n"
72  "\n" );
73  }
74 }
75 
84 int posix_tests(int argc,char *argv[])
85 {
86  char *ptr;
87  int ind;
88 
89  ind = 0;
90  ptr = argv[ind++];
91 
92  if(!ptr)
93  return(0);
94 
95  if( MATCH(ptr,"posix") )
96  {
97  ptr = argv[ind++];
98  if ( !ptr || MATCH(ptr,"help") )
99  {
100  posix_help(1);
101  return(1);
102  }
103  }
104 
105  if (MATCHARGS(ptr,"cat", (ind + 1), argc))
106  {
107  int i;
108  int page = 0;
109  for(i=ind;i<argc;++i)
110  {
111  if(MATCH(argv[i],"-p"))
112  page = 1;
113  }
114  for(i=ind;i<argc;++i)
115  {
116  if(!MATCH(argv[i],"-p"))
117  cat(argv[ind], page);
118  }
119  return(1);
120  }
121 
122  if (MATCHARGS(ptr,"chmod",(ind+2),argc))
123  {
124  chmod( argv[ind],strtol(argv[ind+1],NULL,8));
125  return(1);
126  }
127 
128 
129  if (MATCHARGS(ptr,"copy", (ind + 2), argc))
130  {
131  copy(argv[ind],argv[ind+1]);
132  return(1);
133  }
134 
135  if (MATCHARGS(ptr,"cd", (ind + 1), argc))
136  {
137  chdir(argv[ind]);
138  return(1);
139  }
140 
141 
142  if (MATCHARGS(ptr,"hexdump", (ind + 1), argc))
143  {
144  int i;
145  int page = 0;
146  for(i=ind;i<argc;++i)
147  {
148  if(MATCH(argv[i],"-p"))
149  page = 1;
150  }
151  for(i=ind;i<argc;++i)
152  {
153  if(!MATCH(argv[i],"-p"))
154  hexdump(argv[ind], page);
155  }
156  return(1);
157  }
158 
159  if (MATCHARGS(ptr,"log", (ind + 2), argc))
160  {
161  logfile(argv[ind],argv[ind+1]);
162  return(1);
163  }
164 
165  if (MATCHARGS(ptr,"ls", (ind + 0), argc))
166  {
167  int i;
168  int args = 0;
169  for(i=ind;i<argc;++i)
170  {
171  if(!MATCH(argv[i],"-l"))
172  ls(argv[i],1);
173  ++args;
174  }
175  if(!args)
176  {
177  ls("",1);
178  }
179  return(1);
180  }
181 
182  if (MATCHARGS(ptr,"mkfs", (ind + 1), argc))
183  {
184 
185  mkfs(argv[ind++]);
186  return(1);
187  }
188 
189  if (MATCHARGS(ptr,"mkdir", (ind + 1), argc))
190  {
191  int mode = 0777;
192  if((ind+2) <= argc)
193  {
194  mode = strtol(argv[ind+1],NULL,8);
195  }
196  mkdir(argv[ind],mode);
197  return(1);
198  }
199 
200  if (MATCHARGS(ptr,"page", (ind + 1), argc))
201  {
202  setpage(atoi(argv[ind]));
203  return(1);
204  }
205 
206  if (MATCHARGS(ptr,"pwd", (ind + 0), argc))
207  {
208  char path[256];
209  printf("%s\n", getcwd(path, sizeof(path)-2));
210  return(1);
211  }
212 
213  if (MATCHARGS(ptr,"rename", (ind + 2), argc))
214  {
215  rename(argv[ind],argv[ind+1]);
216  return(1);
217  }
218 
219  if (MATCHARGS(ptr,"rm", (ind + 1), argc))
220  {
221  unlink(argv[ind]);
222  return(1);
223  }
224 
225  if (MATCHARGS(ptr,"sum", (ind + 1), argc))
226  {
227  sum(argv[ind]);
228  return(1);
229  }
230 
231  if (MATCHARGS(ptr,"stat", (ind + 1), argc))
232  {
233  struct stat p;
234  stat(argv[ind], &p); // POSIX test
235  dump_stat(&p);
236  return(1);
237  }
238 
239  if (MATCHARGS(ptr,"rmdir", (ind + 1), argc))
240  {
241  rmdir(argv[ind]);
242  return(1);
243  }
244 
245  if (MATCHARGS(ptr,"upload", (ind + 1), argc))
246  {
247  upload(argv[ind]);
248  return(1);
249  }
250 
251  return(0);
252 }
253 
258 MEMSPACE
259 long cat(char *name, int dopage)
260 {
261  FILE *fp;
262  int count = 0;
263  int size = 0;
264  char line[256];
265 
266  fp = fopen(name,"rb");
267  if (!fp)
268  {
269  printf("Can't open: %s\n", name);
270  return(0);
271  }
272  while(fgets(line,sizeof(line)-2,fp) != NULL)
273  {
274  trim_tail(line);
275  size += strlen(line);
276  puts(line);
277  if(dopage)
278  {
279  count = testpage(++count);
280  if(count < 0)
281  break;
282  }
283 
284 #ifdef ESP8266
285  optimistic_yield(1000);
286  wdt_reset();
287 #endif
288  }
289  printf("\n");
290  fclose(fp);
291  printf("%ld bytes\n", (long)size);
292  return(size);
293 }
294 
295 
301 MEMSPACE
302 long copy(char *from,char *to)
303 {
304  FILE *fi,*fo;
305  char *buf;
306  long size = 0;
307  int len;
308 
309  printf("Opening %s\n", from);
310 
311  fi = fopen(from,"rb");
312  if (fi == NULL)
313  {
314  printf("Can't open: %s\n", from);
315  return(0);
316  }
317 
318  printf("Creating %s\n", to);
319  fo = fopen(to,"wb");
320  if (fo == NULL)
321  {
322  printf("Can't open: %s\n", to);
323  fclose(fo);
324  return(0);
325  }
326 
327  buf = safecalloc(BUFSIZE,1);
328  if(!buf)
329  {
330  fclose(fi);
331  fclose(fo);
332  return(0);
333  }
334 
335  printf("\nCopying...\n");
336  while( ( len = fread(buf,1,BUFSIZE,fi) ) > 0)
337  {
338  if( (int) fwrite(buf,1,len,fo) < len)
339  {
340  printf("Write error\n");
341  break;
342  }
343  size += len;
344  printf("Copied: %08ld\r", size);
345 #ifdef ESP8266
346  optimistic_yield(1000);
347  wdt_reset();
348 #endif
349  }
350  printf("%lu bytes copied.\n", size);
351  safefree(buf);
352  fclose(fi);
353  fclose(fo);
354  return(size);
355 }
356 
357 
358 
362 MEMSPACE
363 int hexdump(char *name, int dopage)
364 {
365  long addr;
366  int i,len,count;
367 
368  FILE *fi;
369  char buf[0x20];
370 
371  fi=fopen(name,"rb");
372  if(fi == NULL)
373  {
374  printf("Can' open: %s\n", name);
375  return(0);
376  }
377 
378  count = 0;
379  addr = 0;
380  while( (len = fread(buf,1, 16, fi)) > 0)
381  {
382  printf("%08lx : ", addr);
383 
384  for(i=0;i<len;++i)
385  printf("%02x ",0xff & buf[i]);
386  for(;i<16;++i)
387  printf(" ");
388 
389  printf(" : ");
390 
391  for(i=0;i<len;++i)
392  {
393  if(buf[i] >= 0x20 && buf[i] <= 0x7e)
394  putchar(buf[i]);
395  else
396  putchar('.');
397  }
398  for(;i<16;++i)
399  putchar('.');
400 
401  printf("\n");
402  addr += len;
403  if(dopage)
404  {
405  count = testpage(++count);
406  if(count < 0)
407  break;
408  }
409 #ifdef ESP8266
410  optimistic_yield(1000);
411  wdt_reset();
412 #endif
413  }
414  printf("\n");
415  fclose(fi);
416  return(1);
417 }
418 
422 static int _pagesize = 25;
423 MEMSPACE
424 int setpage(int count)
425 {
426  _pagesize = count;
427  return(_pagesize);
428 }
429 
430 
434 MEMSPACE
435 int testpage(int count)
436 {
437  int c;
438  if(count >= _pagesize)
439  {
440  printf("More..");
441 #ifdef ESP8266
442  while (!kbhit(0))
443  {
444  optimistic_yield(1000);
445  wdt_reset();
446  }
447 #endif
448  c = getchar();
449  printf("\r");
450  if(c == 'q')
451  return(-1); // quit
452  if(c == '\n')
453  return(_pagesize-1); // single line
454  return(0); // new page
455  }
456  return (count);
457 }
458 
459 
464 MEMSPACE
465 int ls_info(char *name, int verbose)
466 {
467  int i;
468  struct stat sp;
469  uint16_t mask;
470  char *cm = "rwx";
471  char attr[12], *p;
472 
473 
474  if(stat(name, &sp) == -1)
475  {
476  printf("can not stat: %s\n", name);
477  return(0);
478  }
479 
480  if(!verbose)
481  {
482  printf("%s\n",basename(name));
483  return(1);
484  }
485 
486  p = attr;
487  if(S_ISDIR(sp.st_mode))
488  *p++ = 'd';
489  else
490  *p++ = '-';
491 
492  mask = 4 << 6;
493  for(i=0;i<9;++i)
494  {
495  // User
496  if( sp.st_mode & mask)
497  *p++ = cm[ i % 3];
498  else
499  *p++ = '-';
500  mask >>= 1;
501  }
502  *p = 0;
503 
504  printf("%s none none %12ld %s %s\n",
505  attr,
506  (long) sp.st_size,
507  mctime((time_t)sp.st_mtime),
508  basename(name));
509  return(1);
510 }
511 
516 MEMSPACE
517 int ls(char *name, int verbose)
518 {
519  struct stat st;
520  DIR *dirp;
521  int files = 0;
522  int len,len2;
523  dirent_t *de;
524  char fullpath[MAX_NAME_LEN+1];
525 
526  fullpath[0] = 0;
527  if(!name || !*name || MATCH(name,".") )
528  {
529  if( !getcwd(fullpath, sizeof(fullpath)-2) )
530  {
531  printf("ls: Can't get current directory\n");
532  return(0);
533 
534  }
535  }
536  else
537  {
538  strcpy(fullpath,name);
539  }
540  len = strlen(fullpath);
541 
542 
543  printf("Listing:[%s]\n",fullpath);
544 
545  if (stat(fullpath, &st))
546  {
547  printf("ls: cannot stat [%s]\n", fullpath);
548  return(0);
549  }
550 
551  switch (st.st_mode & S_IFMT)
552  {
553  case S_IFREG:
554  ls_info(fullpath,verbose);
555  break;
556  case S_IFDIR:
557  dirp = opendir(fullpath);
558  if(!dirp)
559  {
560  printf("opendir failed\n");
561  return(0);
562  }
563  while ( (de = readdir(dirp)) )
564  {
565  if(de->d_name[0] == 0)
566  break;
567  // FIXME neeed beetter string length tests here
568  len2 = strlen(de->d_name);
569  if(len + len2 >= MAX_NAME_LEN)
570  {
571  printf("name:[%s] too long with full path\n",de->d_name);
572  continue;
573  }
574  if(!MATCH(fullpath,"/") )
575  {
576  strcat(fullpath,"/");
577  }
578  strcat(fullpath,de->d_name);
579  files +=ls_info(fullpath,verbose);
580  // restore path
581  fullpath[len] = 0;
582 #ifdef ESP8266
583  optimistic_yield(1000);
584  wdt_reset();
585 #endif
586  }
587  closedir(dirp);
588  break;
589  }
590  printf("Files: %d\n", (int)files);
591  return(files);
592 }
593 
594 
599 MEMSPACE
600 long logfile(char *name, char *str)
601 {
602  long size = 0;
603  FILE *fo;
604 
605  fo = fopen(name,"ab");
606  if (fo)
607  {
608  printf("Can't open: %s\n", name);
609  return(0);
610  }
611 
612  size = strlen(str);
613  if( fwrite(str,1,size,fo) < size)
614  {
615  printf("Write error\n");
616  return(0);
617  }
618  fclose(fo);
619  return(size);
620 }
621 
625 MEMSPACE
626 uint16_t sum(char *name)
627 {
628 
629  FILE *fi;
630  uint16_t sum;
631  int i,len;
632  uint8_t buffer[256];
633 
634  fi=fopen(name,"rb");
635  if(fi == NULL)
636  {
637  printf("Can' open: %s\n", name);
638  return(0);
639  }
640  sum = 0;
641  while( (len = fread(buffer,1, 256, fi)) > 0)
642  {
643  for(i=0;i<len;++i)
644  sum += (0xff & buffer[i]);
645 #ifdef ESP8266
646  optimistic_yield(1000);
647  wdt_reset();
648 #endif
649  }
650  fclose(fi);
651  printf("Sum: %04Xh, %5u\n", (int) sum, (unsigned int) sum);
652  return(sum);
653 }
654 
660 MEMSPACE
661 long upload(char *name)
662 {
663  int len,len2;
664  long size = 0;
665  FILE *fp;
666  char buffer[256];
667 
668  fp = fopen(name, "wb");
669  if( fp == NULL)
670  {
671  printf("Can' open: %s\n", name);
672  return(0);
673  }
674 
675  while(1)
676  {
677  if(fgets(buffer,254,stdin) == NULL)
678  break;
679  len = strlen(buffer);
680  if(len < 1)
681  break;
682  strcat(buffer,"\n");
683  len = strlen(buffer);
684  len2 = fwrite(buffer, 1, len,fp);
685  if(len != len2)
686  break;
687  size += len;
688  }
689 
690  fclose(fp);
691  sync();
692  return(size);
693 }
MEMSPACE long upload(char *name)
Capture an ASCII file to sdcard First blank line exits capture.
Definition: posix_tests.c:661
MEMSPACE WEAK_ATR char * strcat(char *dest, const char *src)
Append string.
Definition: stringsup.c:199
POSIX wrapper for FatFS.
char d_name[MAX_NAME_LEN]
Definition: posix.h:144
MEMSPACE int testpage(int count)
Used to page output of functions like cat, hexdump, etc.
Definition: posix_tests.c:435
unsigned short uint16_t
Definition: send.c:18
MEMSPACE size_t WEAK_ATR strlen(const char *str)
String Length.
Definition: stringsup.c:146
MEMSPACE void dump_stat(struct stat *sp)
Display struct stat, from POSIX stat(0 or fstat(), in ASCII. NOT POSIX.
Definition: posix.c:1260
MEMSPACE void sync(void)
POSIX Sync all pending file changes and metadata on ALL files.
Definition: posix.c:1069
dirent_t * readdir(DIR *dirp)
Definition: posix.c:1755
Master include file for project Includes all project includes and defines here.
Common Linux/POSIX time functions.
#define MATCH(a, b)
Definition: bdffontutil.h:39
MEMSPACE int setpage(int count)
Definition: posix_tests.c:424
MEMSPACE void posix_help(int full)
Definition: posix_tests.c:46
MEMSPACE int ls_info(char *name, int verbose)
list one file
Definition: posix_tests.c:465
MEMSPACE char * fgets(char *str, int size, FILE *stream)
get a string from stdin See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:420
MEMSPACE FILE * fopen(const char *path, const char *mode)
POSIX Open a file with path name and ascii file mode string.
Definition: posix.c:782
MEMSPACE int rmdir(const char *pathname)
POSIX delete a directory.
Definition: posix.c:1671
FILE type structure.
Definition: posix.h:156
Definition: posix.h:136
int mkfs(char *name)
Formt SD card.
Definition: posix.c:1922
MEMSPACE size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
POSIX read nmemb elements from buf, size bytes each, to the stream fd.
Definition: posix.c:803
MEMSPACE long copy(char *from, char *to)
Copy a file.
Definition: posix_tests.c:302
void optimistic_yield(uint32_t interval_us)
Definition: user_task.c:102
Definition: ff.h:184
int putchar(int c)
put a character to stdout See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:351
MEMSPACE WEAK_ATR char * strcpy(char *dest, const char *src)
copy a string
Definition: stringsup.c:161
uint32_t time_t
type of EPOCH result.
Definition: time.h:35
MEMSPACE uint16_t sum(char *name)
sum of a file with 16bit hex and integer results
Definition: posix_tests.c:626
#define NULL
Definition: cpu.h:55
MEMSPACE void trim_tail(char *str)
Trim White space and control characters from end of string.
Definition: stringsup.c:271
MEMSPACE size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
POSIX write nmemb elements from buf, size bytes each, to the stream fd.
Definition: posix.c:868
MEMSPACE int unlink(const char *pathname)
POSIX delete a file.
Definition: posix.c:1693
mode_t st_mode
Definition: posix.h:109
MEMSPACE int hexdump(char *name, int dopage)
hex listing of file with paging, "q" exits
Definition: posix_tests.c:363
Math IO functions, and verious conversion code with floating point support.
int ind
Definition: ili9341.c:373
MEMSPACE int fclose(FILE *stream)
POSIX close a file stream.
Definition: posix.c:1239
static int _pagesize
Used to page output of functions like cat, hexdump, etc.
Definition: posix_tests.c:422
MEMSPACE long strtol(const char *nptr, char **endptr, int base)
Convert ASCII string to number in a given base.
Definition: mathio.c:136
#define stdin
define stdin, stdout and stderr
Definition: posix.h:269
MEMSPACE long cat(char *name, int dopage)
Display the contents of a file.
Definition: posix_tests.c:259
MEMSPACE long logfile(char *name, char *str)
Log string to a file.
Definition: posix_tests.c:600
Various string and character functions.
MEMSPACE int posix_tests(int argc, char *argv[])
POSIX tests.
Definition: posix_tests.c:84
POSIX stat structure.
Definition: posix.h:105
#define S_IFDIR
Definition: posix.h:202
MEMSPACE int puts(const char *str)
put a string to stdout See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:480
#define BUFSIZE
Definition: posix_tests.c:43
#define MEMSPACE
Definition: cpu.h:25
MEMSPACE void wdt_reset(void)
reset watchdog
Definition: system.c:190
MEMSPACE int mkdir(const char *pathname, mode_t mode)
POSIX make a directory.
Definition: posix.c:1619
int closedir(DIR *dirp)
POSIX closedir.
Definition: posix.c:1717
MEMSPACE int chmod(const char *pathname, mode_t mode)
POSIX chmod function - change file access permission Unfortunately file f_open modes and f_chmod mode...
Definition: posix.c:1513
MEMSPACE int printf(const char *format,...)
MEMSPACE int MATCHARGS(char *str, char *pat, int min, int argc)
Match two strings and compare argument index Display message if the number of arguments is too few...
Definition: stringsup.c:471
MEMSPACE int chdir(const char *pathname)
POSIX change directory.
Definition: posix.c:1488
MEMSPACE char * basename(char *str)
POSIX Basename of filename.
Definition: posix.c:1446
MEMSPACE char * getcwd(char *pathname, int len)
POSIX get current working directory.
Definition: posix.c:1596
int kbhit(int uart_no)
Has ANY character been read in stdin.
Definition: uart.c:585
MEMSPACE int stat(char *name, struct stat *buf)
POSIX stat - get file status of named file.
Definition: posix.c:1344
MEMSPACE void safefree(void *p)
Safe free - Only free a pointer if it is in malloc memory range. We want to try to catch frees of sta...
Definition: system.c:165
MEMSPACE char * mctime(time_t timev)
Display Ascii formatted time from timev seconds NOT POSIX.
Definition: posix.c:1323
MEMSPACE int getchar()
functions normally defined as macros
Definition: posix.c:336
off_t st_size
Definition: posix.h:114
POSIX wrapper for FatFS.
DIR * opendir(const char *pathdir)
Definition: posix.c:1736
#define S_IFREG
Definition: posix.h:205
unsigned char uint8_t
Definition: send.c:17
MEMSPACE int atoi(const char *str)
Convert ASCII string to number in base 10.
Definition: mathio.c:281
MEMSPACE int ls(char *name, int verbose)
Directory listing.
Definition: posix_tests.c:517
time_t st_mtime
Definition: posix.h:118
#define MAX_NAME_LEN
Definition: posix.h:133
MEMSPACE void * safecalloc(size_t nmemb, size_t size)
Safe Calloc - Display Error message if Calloc fails.
Definition: system.c:128
#define S_ISDIR(mode)
Definition: posix.h:215
MEMSPACE int rename(const char *oldpath, const char *newpath)
POSIX rename a file by name.
Definition: posix.c:1648
#define S_IFMT
POSIX File types, see fstat and stat.
Definition: posix.h:201