grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / prefs / serial / prefs.c
blobac2cbb75088bf9c85fbe560d23a5ab2c1aa94adb
1 /*
2 Copyright © 1995-2010, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /*********************************************************************************************/
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
15 #include <aros/macros.h>
17 /* #define DEBUG 1 */
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>
26 #include "prefs.h"
27 #include "misc.h"
29 #ifdef BIGENDIAN_PREFS
30 #define GET_WORD AROS_BE2WORD
31 #define GET_LONG AROS_BE2LONG
32 #else
33 #define GET_WORD(x) x
34 #define GET_LONG(x) x
35 #endif
37 /*********************************************************************************************/
39 #define PREFS_PATH_ENVARC "ENVARC:SYS/serial.prefs"
40 #define PREFS_PATH_ENV "ENV:SYS/serial.prefs"
42 /*********************************************************************************************/
44 struct FilePrefHeader
46 UBYTE ph_Version;
47 UBYTE ph_Type;
48 UBYTE ph_Flags[4];
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)
72 BOOL retval = FALSE;
74 BPTR fh = Open(from, MODE_OLDFILE);
75 if (fh)
77 retval = Prefs_ImportFH(fh);
78 Close(fh);
81 return retval;
84 /*********************************************************************************************/
86 BOOL Prefs_ImportFH(BPTR fh)
88 struct SerialPrefs loadprefs;
89 struct IFFHandle *iff;
90 BOOL retval = FALSE;
92 if ((iff = AllocIFF()))
94 iff->iff_Stream = (IPTR)fh;
96 InitIFFasDOS(iff);
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"));
135 retval = TRUE;
138 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
139 } /* if (!StopChunk(iff, ID_PREF, ID_SERL)) */
140 CloseIFF(iff);
141 } /* if (!OpenIFF(iff, IFFF_READ)) */
142 FreeIFF(iff);
143 } /* if ((iff = AllocIFF())) */
144 return retval;
147 /*********************************************************************************************/
149 BOOL Prefs_ExportFH(BPTR fh)
151 struct SerialPrefs saveprefs;
152 struct IFFHandle *iff;
153 BOOL retval = FALSE;
154 #if 0 /* unused */
155 BOOL delete_if_error = FALSE;
156 #endif
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"));
176 #if 0 /* unused */
177 delete_if_error = TRUE;
178 #endif
180 InitIFFasDOS(iff);
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;
197 head.ph_Type = 0;
198 head.ph_Flags[0] =
199 head.ph_Flags[1] =
200 head.ph_Flags[2] =
201 head.ph_Flags[3] = 0;
203 if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head))
205 D(bug("SavePrefsFH: WriteChunkBytes(PRHD) okay.\n"));
207 PopChunk(iff);
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"));
218 retval = TRUE;
220 PopChunk(iff);
221 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
222 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
223 else
225 PopChunk(iff);
227 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
228 PopChunk(iff);
229 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
230 CloseIFF(iff);
231 } /* if (!OpenIFF(iff, IFFFWRITE)) */
232 FreeIFF(iff);
234 } /* if ((iff = AllocIFF())) */
236 #if 0 /* unused */
237 if (!retval && delete_if_error)
239 DeleteFile(filename);
241 #endif
244 return retval;
247 /*********************************************************************************************/
249 BOOL Prefs_HandleArgs(STRPTR from, BOOL use, BOOL save)
251 BPTR fh;
253 if (from)
255 if (!Prefs_Load(from))
257 ShowMessage("Can't read from input file");
258 return FALSE;
261 else
263 if (!Prefs_Load(PREFS_PATH_ENV))
265 if (!Prefs_Load(PREFS_PATH_ENVARC))
267 ShowMessage
269 "Can't read from file " PREFS_PATH_ENVARC
270 ".\nUsing default values."
272 Prefs_Default();
277 if (use || save)
279 fh = Open(PREFS_PATH_ENV, MODE_NEWFILE);
280 if (fh)
282 Prefs_ExportFH(fh);
283 Close(fh);
285 else
287 ShowMessage("Cant' open " PREFS_PATH_ENV " for writing.");
290 if (save)
292 fh = Open(PREFS_PATH_ENVARC, MODE_NEWFILE);
293 if (fh)
295 Prefs_ExportFH(fh);
296 Close(fh);
298 else
300 ShowMessage("Cant' open " PREFS_PATH_ENVARC " for writing.");
303 return TRUE;
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;
323 return TRUE;