annotate fluid-mnf/misc.c @ 348:37b5f94de802

fluid-mnf: sensible target tty specification
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 13 Mar 2020 06:41:44 +0000
parents 1cd24530c0ae
children f89a20e7adc7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
311
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /******************************************************************************
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * FLUID (Flash Loader Utility Independent of Device)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 *
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * Copyright Texas Instruments, 2001.
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * Mads Meisner-Jensen, mmj@ti.com.
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 *
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 * Miscellaneous Utility Functions
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 *
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 * $Id: misc.c 1.14 Fri, 11 Oct 2002 08:40:21 +0200 mmj $
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 *
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 ******************************************************************************/
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include "misc.h"
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include "fluid.h"
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include "trace.h"
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 #include <stdio.h>
341
1cd24530c0ae fluid-mnf/misc.c: passes compilation
Mychaela Falconia <falcon@freecalypso.org>
parents: 311
diff changeset
18 #include <string.h>
311
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 #include <time.h>
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 #if defined(MSC) || defined(BCC)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 #include "windows.h"
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 #endif
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 /******************************************************************************
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 * File And Path Name Functions
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 ******************************************************************************/
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 // Return the length of the directory name preceding the leafname. The
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 // terminating slash is included in the length. Length can be zero, if there
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 // is no dirname.
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 int dirname_len(const char *pathname)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 char *p;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 int len;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 tr(TrUtility, "dirname_len('%s')\n", pathname);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if ((len = strlen(pathname)) == 0)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 return 0;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 p = (char *) (pathname + len - 1);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 tr(TrUtility, "{ %d }\n", len);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 while (*p != '/' && *p != '\\' && len-- > 1)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 p--;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 return len;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 // Construct a full pathname from <dirname> and <leafname>. The full
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 // pathname is copied in the buffer <buf> of size <size>. On success, the
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 // full length of the pathname is returned. Otherwise, if the buffer is too
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 // small, -1 is returned. NOTE: The <dirname> is supposed to end with a
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 // slash. If not, the leafname part (if present) of the <dirname> will be
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 // removed.
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 int pathname_make(char *buf, int size, const char *dirname, const char *leafname)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 int dir_len, leaf_len, result;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 tr(TrBegin|TrUtility, "pathname_make(*, %d, '%s', '%s') {\n",
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 size, dirname, leafname);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 dir_len = dirname_len(dirname);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 leaf_len = strlen(leafname);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 if (dir_len > size - leaf_len - 1)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 result = E_BUFFER;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 else {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 memcpy(buf, dirname, dir_len);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 strcpy(buf + dir_len, leafname);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 result = dir_len + leaf_len;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 tr(TrEnd|TrUtility, "} %d\n", result);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 return result;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 /******************************************************************************
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 * Utility Funcions
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 ******************************************************************************/
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 // Return the base 2 logarithm of <number>. Return -1 if <number> is zero.
341
1cd24530c0ae fluid-mnf/misc.c: passes compilation
Mychaela Falconia <falcon@freecalypso.org>
parents: 311
diff changeset
88 unsigned int my_log2(unsigned int number)
311
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 int result = -1;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 while (number > 0) {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 number >>= 1;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 result++;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 return result;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 /******************************************************************************
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 * Progress and Timer Functions
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 ******************************************************************************/
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 static int progress_mul;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 static int progress_index;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 static char progress_string[35] = "(---------------------------------)";
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 static char progress_backup[33] =
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 "\b\b\b\b\b\b\b\b" "\b\b\b\b\b\b\b\b" "\b\b\b\b\b\b\b\b" "\b\b\b\b\b\b\b\b";
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 void progress_begin(int n)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 tr(TrUtility, "progress_begin(%d)\n", n);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 progress_index = 0;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 switch (arg_progress) {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 case 'a':
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 // We select a progress multiplier that is a power of two. Then we
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 // generate a progress indicator that is in the range [16..32[ chars
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 // in length.
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 progress_mul = n / 32;
341
1cd24530c0ae fluid-mnf/misc.c: passes compilation
Mychaela Falconia <falcon@freecalypso.org>
parents: 311
diff changeset
122 progress_mul = 1 << (my_log2(progress_mul) + 1);
311
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 n = (n + progress_mul - 1) / progress_mul;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 progress_string[1 + n] = ')';
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 progress_string[1 + n + 1] = 0;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 flowf(NORMAL, "%s\b%s",
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 progress_string, &progress_backup[32 - n]);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 case 'c':
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 case 'd':
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 void progress_update_simple(int n)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 tr(TrUtility, "progress_update_simple(%d)\n", n);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 switch (arg_progress) {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 case 'c':
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 flowf(NORMAL, "%c", n);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 void progress_update(int n)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 char ch;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 tr(TrUtility, "progress_update(%d)\n", n);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 switch (arg_progress) {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 case 'a':
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 if (n / progress_mul > progress_index) {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 progress_index++;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 flowf(NORMAL, "*");
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 n = n % progress_mul;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 ch = n + (n <= 9 ? '0' : 'A' - 10);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 flowf(NORMAL, "%c\b", ch);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 case 'c':
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167 break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 case 'd':
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 flowf(NORMAL, ".");
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 void progress_end(int n)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 switch (arg_progress) {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 case 'a':
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 if (n % progress_mul)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 flowf(NORMAL, "*) ");
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 else
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 flowf(NORMAL, ") ");
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 case 'c':
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 case 'd':
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 flowf(NORMAL, " ");
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 int stopwatch_start(void)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 #if defined(MSC) || defined(BCC)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 return GetTickCount();
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 #else
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 return 1000 * time(NULL);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 #endif
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 int stopwatch_stop(int time_start)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200 {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 #if defined(MSC) || defined(BCC)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 return GetTickCount() - time_start;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
203 #else
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204 return 1000 * (time(NULL) - time_start);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 #endif
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209 /******************************************************************************
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 * Hexdumping
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 ******************************************************************************/
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 void hexdump(unsigned char *buf, int size, unsigned int addr, int unitsize)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214 {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215 int n, i;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 char string[(8+1+1) + (1+16+1+1) + (3*16) + 1];
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 char *s;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 while (size > 0)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 s = string;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 s += sprintf(s, "%8x ", addr); // print offset
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224 n = (size > 16 ? 16 : size);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226 // print the textual representation
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
227 for (i = 0; i < n; i++) {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
228 if (buf[i] >= ' ' && buf[i] < 127)
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
229 *s++ = buf[i];
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230 else
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231 *s++ = '.';
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
232 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
233 // pad textual representation with spaces
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
234 for (i = 0; i < 16 - n; i++) {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
235 *s++ = ' ';
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
236 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
237 *s++ = ' ';
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
238
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
239 // print hexedecimal representation
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
240 for (i = 0; i < n; i += unitsize) {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
241 switch (unitsize) {
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
242 case 1: s += sprintf(s, "%02x ", *(uint8 *) (buf+i)); break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
243 case 2: s += sprintf(s, "%04x ", *(uint16 *) (buf+i)); break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
244 case 4:
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
245 s += sprintf(s, "%08x ", (int) (*(uint32 *) (buf+i)));
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
246 break;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
247 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
248 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
249 buf += 16;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
250 addr += 16;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
251 size -= 16;
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
252 puts(string);
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
253 }
9cecc930d78f fluid-mnf: original source from TI,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
254 }