Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2377 | konopa | 1 | /* Functions for printing on console. */ |
2 | |||
3 | |||
4 | /* Methods: |
||
5 | * init_printing: connects to SERVICE_CONSOLE and initializes used data structures |
||
6 | * print_console: prints char* on console |
||
7 | * print_console_int: prints int argument with specified conversion inside input string |
||
8 | */ |
||
9 | |||
10 | |||
11 | #include <string.h> |
||
12 | #include <ipc/ipc.h> |
||
13 | #include <ipc/services.h> |
||
14 | #include <async.h> |
||
15 | #include "fs.h" |
||
16 | #include "../console/console.h" |
||
17 | |||
18 | #define TOTAL_CONVERSIONS 5 |
||
19 | #define BUFF_SIZE 10 |
||
20 | |||
21 | |||
22 | /* Table of printing conversions for: d, i, u, c, %. */ |
||
23 | char conversions[TOTAL_CONVERSIONS]; |
||
24 | char format_string[3] = "%"; |
||
25 | |||
26 | static int validate_string(char conv); |
||
27 | |||
28 | int init_printing() |
||
29 | { |
||
30 | |||
31 | /* Attempt to connect to SERVICE_CONSOLE fail. */ |
||
32 | if (!connect_to_con(&con_phone, CON_CONN_ATTEMPTS)) |
||
33 | return FALSE; |
||
34 | |||
35 | conversions[0] = 'd'; |
||
36 | conversions[1] = 'i'; |
||
37 | conversions[2] = 'u'; |
||
38 | conversions[3] = 'c'; |
||
39 | conversions[4] = '%'; |
||
40 | |||
41 | } |
||
42 | |||
43 | int validate_string(char conv) |
||
44 | { |
||
45 | |||
46 | int i; |
||
47 | |||
48 | /* Check if the conversion character matches conversions table. */ |
||
49 | for (i = 0; i < TOTAL_CONVERSIONS; i++){ |
||
50 | if (conv == conversions[i]) { |
||
51 | return TRUE; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | return FALSE; |
||
56 | } |
||
57 | |||
58 | int print_console(char *str) |
||
59 | { |
||
60 | |||
61 | int i, len; |
||
62 | char c; |
||
63 | |||
64 | /* Must be connected to the CONSOLE_SERVICE. */ |
||
65 | if (con_phone < 0) |
||
66 | return FALSE; |
||
67 | |||
68 | len = strlen(str); |
||
69 | if (len == 0) |
||
70 | return TRUE; |
||
71 | |||
72 | for (i = 0; i < len; i++) { |
||
73 | c = *str++; |
||
74 | async_req_2(con_phone, CONSOLE_PUTCHAR, c, 0, NULL, NULL); /* Do the work. */ |
||
75 | } |
||
76 | |||
77 | return TRUE; |
||
78 | } |
||
79 | |||
80 | int print_console_int(char *str, int arg) |
||
81 | { |
||
82 | |||
83 | int i, retval, len, front_side, offset; |
||
84 | char conversion; |
||
85 | char buffer[BUFF_SIZE]; |
||
86 | char *front, *back; |
||
87 | |||
88 | len = strlen(str); |
||
89 | |||
90 | if (len == 0) |
||
91 | return TRUE; |
||
92 | |||
93 | front = malloc((len+1) * sizeof(char)); |
||
94 | back = malloc((len+1) * sizeof(char)); |
||
95 | |||
96 | front_side = TRUE; |
||
97 | |||
98 | /* Copy front side of source string. */ |
||
99 | for (i = 0; i < len; i++) { |
||
100 | if (str[i] == '%') { |
||
101 | front_side = FALSE; |
||
102 | break; |
||
103 | } |
||
104 | front[i] = str[i]; |
||
105 | } |
||
106 | |||
107 | /* Character '%' was not found. Print it in a normal way, without int argument. */ |
||
108 | if (front_side) { |
||
109 | return (print_console(str)); |
||
110 | } |
||
111 | |||
112 | /* Character '%' was the last in the input string - error. */ |
||
113 | if (i == (len-1)) { |
||
114 | return FALSE; |
||
115 | } |
||
116 | |||
117 | front[i] = '\0'; |
||
118 | conversion = str[++i]; |
||
119 | retval = validate_string(conversion); |
||
120 | |||
121 | if (!retval) { |
||
122 | return FALSE; |
||
123 | } |
||
124 | |||
125 | offset = ++i; |
||
126 | |||
127 | /* Copy back side of source string. */ |
||
128 | for (;i < len; i++) { |
||
129 | back[i-offset] = str[i]; |
||
130 | } |
||
131 | |||
132 | back[i] = '\0'; |
||
133 | |||
134 | /* Two characters '%' in source string. */ |
||
135 | if (conversion == '%') { |
||
136 | print_console(front); |
||
137 | print_console(back); |
||
138 | return TRUE; |
||
139 | } |
||
140 | |||
141 | /* Other 'normal' case. */ |
||
142 | print_console(front); |
||
143 | format_string[1] = conversion; |
||
144 | sprintf(buffer, format_string, arg); |
||
145 | print_console(buffer); |
||
146 | print_console(back); |
||
147 | |||
148 | return TRUE; |
||
149 | } |