aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/tools/am4up.c
blob: 3c9a46fe4a55d428c3b0d7c4bd4c38865db9bb89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Filter to upload files to amforth from minicom
 // Al Williams http://www.hotsolder.com
// Originally for the elf (if you were wondering about the names)
// 17 Sept 2010 -- Public Domain
// 22 Sept 2010 -- Added forced return at end of file
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <time.h>
 
 // Minicom uploader has stdin connected to serial input,
 // stdout connected to serial output
 // stderr connects back to minicom
 // exit code 0 for success or non zero for fail
 
 // To set this up, use Control+AO and configure file transfer 
 // protcols. Set a name and the program file name 
 // Set Name=Y, U/D to U, FullScrn to N, IO-Red to Y, and Multi to N

// OR you can put this in /etc/minicom/minirc.amforth

/****************  Start file on line below
# Machine-generated file - use setup menu in minicom to change parameters.
pu pname10          YUNYNamforth
pu pprog10          am4up
pu baudrate         9600
pu bits             8
pu parity           N
pu stopbits         1
pu minit            
pu mreset           
pu backspace        BS
pu rtscts           No

 **************** Line above was the last line of the file */
// Then start minicom like this:
// minicom -w -D /dev/ttyUSB1 amforth
// This presumes you have am4up (this file compiled with gcc) on your path
// and you are using /dev/ttyUSB1.
// 
// To compile this file:
// gcc -o am4up am4up.c 
// Then copy the am4up executable to your path somewhere 
 
 // The character pacing is handled by waiting for the echo
 // The line pacing is handled by waiting for > or k
 // A ? in the prompt output indicates an error
 // Note you might still be in a defining word so 
 // when you get an error you might have to enter a ; to get back
 // to a normal prompt.
 
 
 // wait for prompt -- errc is an error character
 void elfwait(int errc)
   {
     int c;
     // wait for response
     while ((c=getchar())!='>' && c!='k')
       {
	 fputc(c,stderr);
	 if (c==errc) { fprintf(stderr,"\nError - %c\n",errc); exit(2); }
       }
     fputc(c,stderr);
   }
 
 
 int main(int argc, char *argv[])
 {
   FILE *f=NULL;
   time_t t0,t1;
   int c;
   int lastchar;
   fprintf(stderr,"am4up V2 by Al Williams\nhttp://www.hotsolder.com\nUploading ");
   if (argc>1) 
     {
       fprintf(stderr,"%s...\n",argv[1]);
       f=fopen(argv[1],"r");
     }
   if (!f) { fprintf(stderr, "No file\n"); exit(1); }
   putchar('\r'); putchar('\n');
   elfwait('\0');
   time(&t0);
   lastchar='\n';
   while (lastchar!=EOF)
     {
       int c1;
       c= getc(f);
       if (c==EOF && lastchar=='\n') break;
       // newline == CRLF
       if ((c=='\n'||c=='\r') && lastchar=='\n') continue; // blank line
       if (c=='\\' && lastchar=='\n')
	 {
	   // comment
	   while (c!='\n' && c!=EOF) c=getc(f);
	   if (c==EOF) break;
	   continue;
	 }
       // remember  last character except leading blanks
       if (c==EOF || lastchar!='\n' || !isspace(c)) lastchar=c;
       if (c=='\t') c=' ';
       if (c=='\n') c='\r';
       if (c==EOF) c='\r';  // final return
       putchar(c);  
 // read echo
       do 
	 {
	   c1=getchar(); 
	   fputc(c1,stderr);
	 } while (c1!=c);
       
       if (c=='\r' || c==EOF) 
	 {
	   elfwait('?');
	 }
     }
   time(&t1);
   fprintf(stderr,"\nTransfer time=%ld seconds\n",t1-t0);
 
   return 0;
 }