2 Copyright © 1995-2010, The AROS Development Team. All rights reserved.
9 /*********************************************************************************************/
15 #include <aros/macros.h>
18 #include <aros/debug.h>
20 #include <proto/exec.h>
21 #include <proto/iffparse.h>
22 #include <proto/dos.h>
24 #include <prefs/prefhdr.h>
29 #ifdef BIGENDIAN_PREFS
30 #define GET_WORD AROS_BE2WORD
31 #define GET_LONG AROS_BE2LONG
37 /*********************************************************************************************/
39 #define PREFS_PATH_ENVARC "ENVARC:SYS/serial.prefs"
40 #define PREFS_PATH_ENV "ENV:SYS/serial.prefs"
42 /*********************************************************************************************/
51 /*********************************************************************************************/
53 struct SerialPrefs serialprefs
;
55 /*********************************************************************************************/
57 #define SER_DEFAULT_BAUDRATE 19200
58 #define SER_DEFAULT_IBUFFER 512
59 #define SER_DEFAULT_OBUFFER 512
61 #define SER_DEFAULT_IHANDSHAKE HSHAKE_NONE
62 #define SER_DEFAULT_OHANDSHAKE HSHAKE_NONE
64 #define SER_DEFAULT_PARITY PARITY_NONE
65 #define SER_DEFAULT_BITS_PER_CHAR 8
66 #define SER_DEFAULT_STOP_BITS 1
68 /*********************************************************************************************/
70 static BOOL
Prefs_Load(STRPTR from
)
74 BPTR fh
= Open(from
, MODE_OLDFILE
);
77 retval
= Prefs_ImportFH(fh
);
84 /*********************************************************************************************/
86 BOOL
Prefs_ImportFH(BPTR fh
)
88 struct SerialPrefs loadprefs
;
89 struct IFFHandle
*iff
;
92 if ((iff
= AllocIFF()))
94 iff
->iff_Stream
= (IPTR
)fh
;
98 if (!OpenIFF(iff
, IFFF_READ
))
100 D(bug("LoadPrefs: OpenIFF okay.\n"));
102 if (!StopChunk(iff
, ID_PREF
, ID_SERL
))
104 D(bug("LoadPrefs: StopChunk okay.\n"));
106 if (!ParseIFF(iff
, IFFPARSE_SCAN
))
108 struct ContextNode
*cn
;
110 D(bug("LoadPrefs: ParseIFF okay.\n"));
112 cn
= CurrentChunk(iff
);
114 if (cn
->cn_Size
== sizeof(struct SerialPrefs
))
116 D(bug("LoadPrefs: ID_SERL chunk size okay.\n"));
118 if (ReadChunkBytes(iff
, &loadprefs
, sizeof(struct SerialPrefs
)) == sizeof(struct SerialPrefs
))
120 D(bug("LoadPrefs: Reading chunk successful.\n"));
122 CopyMem(loadprefs
.sp_Reserved
, serialprefs
.sp_Reserved
, sizeof(serialprefs
.sp_Reserved
));
123 serialprefs
.sp_Unit0Map
= GET_LONG(loadprefs
.sp_Unit0Map
);
124 serialprefs
.sp_BaudRate
= GET_LONG(loadprefs
.sp_BaudRate
);
125 serialprefs
.sp_InputBuffer
= GET_LONG(loadprefs
.sp_InputBuffer
);
126 serialprefs
.sp_OutputBuffer
= GET_LONG(loadprefs
.sp_OutputBuffer
);
127 serialprefs
.sp_InputHandshake
= loadprefs
.sp_InputHandshake
;
128 serialprefs
.sp_OutputHandshake
= loadprefs
.sp_OutputHandshake
;
129 serialprefs
.sp_Parity
= loadprefs
.sp_Parity
;
130 serialprefs
.sp_BitsPerChar
= loadprefs
.sp_BitsPerChar
;
131 serialprefs
.sp_StopBits
= loadprefs
.sp_StopBits
;
133 D(bug("LoadPrefs: Everything okay :-)\n"));
138 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
139 } /* if (!StopChunk(iff, ID_PREF, ID_SERL)) */
141 } /* if (!OpenIFF(iff, IFFF_READ)) */
143 } /* if ((iff = AllocIFF())) */
147 /*********************************************************************************************/
149 BOOL
Prefs_ExportFH(BPTR fh
)
151 struct SerialPrefs saveprefs
;
152 struct IFFHandle
*iff
;
155 BOOL delete_if_error
= FALSE
;
158 CopyMem(serialprefs
.sp_Reserved
, saveprefs
.sp_Reserved
, sizeof(serialprefs
.sp_Reserved
));
159 saveprefs
.sp_Unit0Map
= GET_LONG(serialprefs
.sp_Unit0Map
);
160 saveprefs
.sp_BaudRate
= GET_LONG(serialprefs
.sp_BaudRate
);
161 saveprefs
.sp_InputBuffer
= GET_LONG(serialprefs
.sp_InputBuffer
);
162 saveprefs
.sp_OutputBuffer
= GET_LONG(serialprefs
.sp_OutputBuffer
);
163 saveprefs
.sp_InputHandshake
= serialprefs
.sp_InputHandshake
;
164 saveprefs
.sp_OutputHandshake
= serialprefs
.sp_OutputHandshake
;
165 saveprefs
.sp_Parity
= serialprefs
.sp_Parity
;
166 saveprefs
.sp_BitsPerChar
= serialprefs
.sp_BitsPerChar
;
167 saveprefs
.sp_StopBits
= serialprefs
.sp_StopBits
;
169 D(bug("SavePrefsFH: fh: %lx\n", fh
));
171 if ((iff
= AllocIFF()))
173 iff
->iff_Stream
= (IPTR
) fh
;
174 D(bug("SavePrefsFH: stream opened.\n"));
177 delete_if_error
= TRUE
;
182 if (!OpenIFF(iff
, IFFF_WRITE
))
184 D(bug("SavePrefsFH: OpenIFF okay.\n"));
186 if (!PushChunk(iff
, ID_PREF
, ID_FORM
, IFFSIZE_UNKNOWN
))
188 D(bug("SavePrefsFH: PushChunk(FORM) okay.\n"));
190 if (!PushChunk(iff
, ID_PREF
, ID_PRHD
, sizeof(struct FilePrefHeader
)))
192 struct FilePrefHeader head
;
194 D(bug("SavePrefsFH: PushChunk(PRHD) okay.\n"));
196 head
.ph_Version
= PHV_CURRENT
;
201 head
.ph_Flags
[3] = 0;
203 if (WriteChunkBytes(iff
, &head
, sizeof(head
)) == sizeof(head
))
205 D(bug("SavePrefsFH: WriteChunkBytes(PRHD) okay.\n"));
209 if (!PushChunk(iff
, ID_PREF
, ID_SERL
, sizeof(struct SerialPrefs
)))
211 D(bug("SavePrefsFH: PushChunk(LCLE) okay.\n"));
213 if (WriteChunkBytes(iff
, &saveprefs
, sizeof(saveprefs
)) == sizeof(saveprefs
))
215 D(bug("SavePrefsFH: WriteChunkBytes(SERL) okay.\n"));
216 D(bug("SavePrefsFH: Everything okay :-)\n"));
221 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
222 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
227 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
229 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
231 } /* if (!OpenIFF(iff, IFFFWRITE)) */
234 } /* if ((iff = AllocIFF())) */
237 if (!retval
&& delete_if_error
)
239 DeleteFile(filename
);
247 /*********************************************************************************************/
249 BOOL
Prefs_HandleArgs(STRPTR from
, BOOL use
, BOOL save
)
255 if (!Prefs_Load(from
))
257 ShowMessage("Can't read from input file");
263 if (!Prefs_Load(PREFS_PATH_ENV
))
265 if (!Prefs_Load(PREFS_PATH_ENVARC
))
269 "Can't read from file " PREFS_PATH_ENVARC
270 ".\nUsing default values."
279 fh
= Open(PREFS_PATH_ENV
, MODE_NEWFILE
);
287 ShowMessage("Cant' open " PREFS_PATH_ENV
" for writing.");
292 fh
= Open(PREFS_PATH_ENVARC
, MODE_NEWFILE
);
300 ShowMessage("Cant' open " PREFS_PATH_ENVARC
" for writing.");
306 /*********************************************************************************************/
308 BOOL
Prefs_Default(VOID
)
310 serialprefs
.sp_Reserved
[0] = 0;
311 serialprefs
.sp_Reserved
[1] = 0;
312 serialprefs
.sp_Reserved
[2] = 0;
313 serialprefs
.sp_Unit0Map
= 0;
314 serialprefs
.sp_BaudRate
= SER_DEFAULT_BAUDRATE
;
315 serialprefs
.sp_InputBuffer
= SER_DEFAULT_IBUFFER
;
316 serialprefs
.sp_OutputBuffer
= SER_DEFAULT_OBUFFER
;
317 serialprefs
.sp_InputHandshake
= SER_DEFAULT_IHANDSHAKE
;
318 serialprefs
.sp_OutputHandshake
= SER_DEFAULT_OHANDSHAKE
;
319 serialprefs
.sp_Parity
= SER_DEFAULT_PARITY
;
320 serialprefs
.sp_BitsPerChar
= SER_DEFAULT_BITS_PER_CHAR
;
321 serialprefs
.sp_StopBits
= SER_DEFAULT_STOP_BITS
;