Rev 2377 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2377 | Rev 2435 | ||
---|---|---|---|
- | 1 | /* |
|
- | 2 | * Copyright (c) 1987,1997, Prentice Hall |
|
- | 3 | * All rights reserved. |
|
- | 4 | * |
|
- | 5 | * Redistribution and use of the MINIX operating system in source and |
|
- | 6 | * binary forms, with or without modification, are permitted provided |
|
1 | /* Functions for printing on console. */ |
7 | * that the following conditions are met: |
2 | 8 | ||
- | 9 | * - Redistributions of source code must retain the above copyright |
|
- | 10 | * notice, this list of conditions and the following disclaimer. |
|
3 | 11 | ||
- | 12 | * - Redistributions in binary form must reproduce the above |
|
- | 13 | * copyright notice, this list of conditions and the following |
|
- | 14 | * disclaimer in the documentation and/or other materials provided |
|
4 | /* Methods: |
15 | * with the distribution. |
- | 16 | ||
5 | * init_printing: connects to SERVICE_CONSOLE and initializes used data structures |
17 | * - Neither the name of Prentice Hall nor the names of the software |
6 | * print_console: prints char* on console |
18 | * authors or contributors may be used to endorse or promote |
7 | * print_console_int: prints int argument with specified conversion inside input string |
19 | * products derived from this software without specific prior |
- | 20 | * written permission. |
|
- | 21 | ||
- | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND |
|
- | 23 | * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
|
- | 24 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
- | 25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
- | 26 | * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE |
|
- | 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
- | 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
- | 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|
- | 30 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
- | 31 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
|
- | 32 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
- | 33 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
- | 34 | */ |
|
- | 35 | ||
- | 36 | /** @addtogroup FileSystemImpl |
|
- | 37 | * @{ |
|
- | 38 | */ |
|
- | 39 | ||
- | 40 | /** |
|
- | 41 | * @file printing.c |
|
- | 42 | * @brief Functions for printing on console. |
|
8 | */ |
43 | */ |
9 | 44 | ||
10 | 45 | ||
11 | #include <string.h> |
46 | #include <string.h> |
12 | #include <ipc/ipc.h> |
47 | #include <ipc/ipc.h> |
13 | #include <ipc/services.h> |
48 | #include <ipc/services.h> |
14 | #include <async.h> |
49 | #include <async.h> |
15 | #include "fs.h" |
50 | #include "fs.h" |
16 | #include "../console/console.h" |
51 | #include "../console/console.h" |
17 | 52 | ||
18 | #define TOTAL_CONVERSIONS 5 |
53 | #define TOTAL_CONVERSIONS 5 |
19 | #define BUFF_SIZE 10 |
54 | #define BUFF_SIZE 10 |
20 | 55 | ||
21 | 56 | ||
- | 57 | /** |
|
22 | /* Table of printing conversions for: d, i, u, c, %. */ |
58 | * Table of printing conversions for: d, i, u, c, %. |
- | 59 | */ |
|
23 | char conversions[TOTAL_CONVERSIONS]; |
60 | char conversions[TOTAL_CONVERSIONS]; |
24 | char format_string[3] = "%"; |
61 | char format_string[3] = "%"; |
25 | 62 | ||
26 | static int validate_string(char conv); |
63 | static int validate_string(char conv); |
27 | 64 | ||
- | 65 | /** |
|
- | 66 | * Connects to SERVICE_CONSOLE and initializes used data structures |
|
- | 67 | */ |
|
28 | int init_printing() |
68 | int init_printing() |
29 | { |
69 | { |
30 | 70 | ||
31 | /* Attempt to connect to SERVICE_CONSOLE fail. */ |
71 | /* Attempt to connect to SERVICE_CONSOLE fail. */ |
32 | if (!connect_to_con(&con_phone, CON_CONN_ATTEMPTS)) |
72 | if (!connect_to_con(&con_phone, CON_CONN_ATTEMPTS)) |
33 | return FALSE; |
73 | return FALSE; |
34 | 74 | ||
35 | conversions[0] = 'd'; |
75 | conversions[0] = 'd'; |
36 | conversions[1] = 'i'; |
76 | conversions[1] = 'i'; |
37 | conversions[2] = 'u'; |
77 | conversions[2] = 'u'; |
38 | conversions[3] = 'c'; |
78 | conversions[3] = 'c'; |
39 | conversions[4] = '%'; |
79 | conversions[4] = '%'; |
40 | 80 | ||
41 | } |
81 | } |
42 | 82 | ||
- | 83 | /** |
|
- | 84 | * Check whether the conversion character matches the conversions table |
|
- | 85 | */ |
|
43 | int validate_string(char conv) |
86 | int validate_string(char conv) |
44 | { |
87 | { |
45 | 88 | ||
46 | int i; |
89 | int i; |
47 | 90 | ||
48 | /* Check if the conversion character matches conversions table. */ |
91 | /* Check if the conversion character matches conversions table. */ |
49 | for (i = 0; i < TOTAL_CONVERSIONS; i++){ |
92 | for (i = 0; i < TOTAL_CONVERSIONS; i++){ |
50 | if (conv == conversions[i]) { |
93 | if (conv == conversions[i]) { |
51 | return TRUE; |
94 | return TRUE; |
52 | } |
95 | } |
53 | } |
96 | } |
54 | 97 | ||
55 | return FALSE; |
98 | return FALSE; |
56 | } |
99 | } |
57 | 100 | ||
- | 101 | /** |
|
- | 102 | * Prints char* on console |
|
- | 103 | */ |
|
58 | int print_console(char *str) |
104 | int print_console(char *str) |
59 | { |
105 | { |
60 | 106 | ||
61 | int i, len; |
107 | int i, len; |
62 | char c; |
108 | char c; |
63 | 109 | ||
64 | /* Must be connected to the CONSOLE_SERVICE. */ |
110 | /* Must be connected to the CONSOLE_SERVICE. */ |
65 | if (con_phone < 0) |
111 | if (con_phone < 0) |
66 | return FALSE; |
112 | return FALSE; |
67 | 113 | ||
68 | len = strlen(str); |
114 | len = strlen(str); |
69 | if (len == 0) |
115 | if (len == 0) |
70 | return TRUE; |
116 | return TRUE; |
71 | 117 | ||
72 | for (i = 0; i < len; i++) { |
118 | for (i = 0; i < len; i++) { |
73 | c = *str++; |
119 | c = *str++; |
74 | async_req_2(con_phone, CONSOLE_PUTCHAR, c, 0, NULL, NULL); /* Do the work. */ |
120 | async_req_2(con_phone, CONSOLE_PUTCHAR, c, 0, NULL, NULL); /* Do the work. */ |
75 | } |
121 | } |
76 | 122 | ||
77 | return TRUE; |
123 | return TRUE; |
78 | } |
124 | } |
79 | 125 | ||
- | 126 | /** |
|
- | 127 | * Prints int argument with specified conversion inside input string |
|
- | 128 | */ |
|
80 | int print_console_int(char *str, int arg) |
129 | int print_console_int(char *str, int arg) |
81 | { |
130 | { |
82 | 131 | ||
83 | int i, retval, len, front_side, offset; |
132 | int i, retval, len, front_side, offset; |
84 | char conversion; |
133 | char conversion; |
85 | char buffer[BUFF_SIZE]; |
134 | char buffer[BUFF_SIZE]; |
86 | char *front, *back; |
135 | char *front, *back; |
87 | 136 | ||
88 | len = strlen(str); |
137 | len = strlen(str); |
89 | 138 | ||
90 | if (len == 0) |
139 | if (len == 0) |
91 | return TRUE; |
140 | return TRUE; |
92 | 141 | ||
93 | front = malloc((len+1) * sizeof(char)); |
142 | front = malloc((len+1) * sizeof(char)); |
94 | back = malloc((len+1) * sizeof(char)); |
143 | back = malloc((len+1) * sizeof(char)); |
95 | 144 | ||
96 | front_side = TRUE; |
145 | front_side = TRUE; |
97 | 146 | ||
98 | /* Copy front side of source string. */ |
147 | /* Copy front side of source string. */ |
99 | for (i = 0; i < len; i++) { |
148 | for (i = 0; i < len; i++) { |
100 | if (str[i] == '%') { |
149 | if (str[i] == '%') { |
101 | front_side = FALSE; |
150 | front_side = FALSE; |
102 | break; |
151 | break; |
103 | } |
152 | } |
104 | front[i] = str[i]; |
153 | front[i] = str[i]; |
105 | } |
154 | } |
106 | 155 | ||
107 | /* Character '%' was not found. Print it in a normal way, without int argument. */ |
156 | /* Character '%' was not found. Print it in a normal way, without int argument. */ |
108 | if (front_side) { |
157 | if (front_side) { |
109 | return (print_console(str)); |
158 | return (print_console(str)); |
110 | } |
159 | } |
111 | 160 | ||
112 | /* Character '%' was the last in the input string - error. */ |
161 | /* Character '%' was the last in the input string - error. */ |
113 | if (i == (len-1)) { |
162 | if (i == (len-1)) { |
114 | return FALSE; |
163 | return FALSE; |
115 | } |
164 | } |
116 | 165 | ||
117 | front[i] = '\0'; |
166 | front[i] = '\0'; |
118 | conversion = str[++i]; |
167 | conversion = str[++i]; |
119 | retval = validate_string(conversion); |
168 | retval = validate_string(conversion); |
120 | 169 | ||
121 | if (!retval) { |
170 | if (!retval) { |
122 | return FALSE; |
171 | return FALSE; |
123 | } |
172 | } |
124 | 173 | ||
125 | offset = ++i; |
174 | offset = ++i; |
126 | 175 | ||
127 | /* Copy back side of source string. */ |
176 | /* Copy back side of source string. */ |
128 | for (;i < len; i++) { |
177 | for (;i < len; i++) { |
129 | back[i-offset] = str[i]; |
178 | back[i-offset] = str[i]; |
130 | } |
179 | } |
131 | 180 | ||
132 | back[i] = '\0'; |
181 | back[i] = '\0'; |
133 | 182 | ||
134 | /* Two characters '%' in source string. */ |
183 | /* Two characters '%' in source string. */ |
135 | if (conversion == '%') { |
184 | if (conversion == '%') { |
136 | print_console(front); |
185 | print_console(front); |
137 | print_console(back); |
186 | print_console(back); |
138 | return TRUE; |
187 | return TRUE; |
139 | } |
188 | } |
140 | 189 | ||
141 | /* Other 'normal' case. */ |
190 | /* Other 'normal' case. */ |
142 | print_console(front); |
191 | print_console(front); |
143 | format_string[1] = conversion; |
192 | format_string[1] = conversion; |
144 | sprintf(buffer, format_string, arg); |
193 | sprintf(buffer, format_string, arg); |
145 | print_console(buffer); |
194 | print_console(buffer); |
146 | print_console(back); |
195 | print_console(back); |
147 | 196 | ||
148 | return TRUE; |
197 | return TRUE; |
149 | } |
198 | } |
- | 199 | ||
- | 200 | /** |
|
- | 201 | * } |
|
- | 202 | */ |
|
- | 203 | ||
150 | 204 |