Changeset 23 for trunk/kernel/kern/printk.c
- Timestamp:
- Jun 18, 2017, 10:06:41 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/kern/printk.c
r14 r23 37 37 extern chdev_t txt0_chdev; // allocated in kernel_init.c 38 38 39 ///////////////////////////////////// 40 uint32_t snprintf( char * string, 41 uint32_t length, 42 char * format, ... ) 43 { 44 45 #define TO_STREAM(x) do { string[ps] = (x); ps++; if(ps==length) return -1; } while(0); 46 47 va_list args; // printf arguments 48 uint32_t ps; // write pointer to the string buffer 49 50 ps = 0; 51 va_start( args , format ); 52 53 xprintf_text: 54 55 while ( *format != 0 ) 56 { 57 58 if (*format == '%') // copy argument to string 59 { 60 format++; 61 goto xprintf_arguments; 62 } 63 else // copy one char to string 64 { 65 TO_STREAM( *format ); 66 format++; 67 } 68 } 69 70 va_end( args ); 71 72 // add terminating NUL chracter 73 TO_STREAM( 0 ); 74 return ps; 75 76 xprintf_arguments: 77 78 { 79 char buf[30]; // buffer to display one number 80 char * pbuf; // pointer on first char to display 81 uint32_t len = 0; // number of char to display 82 static const char HexaTab[] = "0123456789ABCDEF"; 83 uint32_t i; 84 85 // Ignore fields width and precision 86 for ( ; (*format >= '0' && *format <= '9') || (*format == '.') ; format++ ); 87 88 switch (*format) 89 { 90 case ('c'): // char conversion 91 { 92 int val = va_arg( args, int ); 93 buf[0] = val; 94 pbuf = buf; 95 len = 1; 96 break; 97 } 98 case ('d'): // decimal signed integer 99 { 100 int val = va_arg( args, int ); 101 if (val < 0) 102 { 103 TO_STREAM( '-' ); 104 val = -val; 105 } 106 for(i = 0; i < 10; i++) 107 { 108 109 buf[9 - i] = HexaTab[val % 10]; 110 if (!(val /= 10)) break; 111 } 112 len = i + 1; 113 pbuf = &buf[9 - i]; 114 break; 115 } 116 case ('u'): // decimal unsigned integer 117 { 118 uint32_t val = va_arg( args, uint32_t ); 119 for(i = 0; i < 10; i++) 120 { 121 buf[9 - i] = HexaTab[val % 10]; 122 if (!(val /= 10)) break; 123 } 124 len = i + 1; 125 pbuf = &buf[9 - i]; 126 break; 127 } 128 case ('x'): // 32 bits hexadecimal 129 case ('l'): // 64 bits hexadecimal 130 { 131 uint32_t imax; 132 uint64_t val; 133 134 if ( *format == 'l' ) // 64 bits 135 { 136 val = va_arg( args, uint64_t); 137 imax = 16; 138 } 139 else // 32 bits 140 { 141 val = va_arg( args, uint32_t); 142 imax = 8; 143 } 144 145 TO_STREAM( '0' ); 146 TO_STREAM( 'x' ); 147 148 for(i = 0; i < imax; i++) 149 { 150 buf[(imax-1) - i] = HexaTab[val % 16]; 151 if (!(val /= 16)) break; 152 } 153 len = i + 1; 154 pbuf = &buf[(imax-1) - i]; 155 break; 156 } 157 case ('s'): /* string */ 158 { 159 char* str = va_arg( args, char* ); 160 while (str[len]) { len++; } 161 pbuf = str; 162 break; 163 } 164 default: // unsupported argument type 165 { 166 return -1; 167 } 168 } // end switch on argument type 169 170 format++; 171 172 // copy argument to string 173 for( i = 0 ; i < len ; i++ ) 174 { 175 TO_STREAM( pbuf[i] ); 176 } 177 178 goto xprintf_text; 179 } 180 } // end xprintf() 181 39 182 /////////////////////////////////////////////////////////////////////////////////// 40 183 // This static function is called by kernel_printf() to display a string on the … … 47 190 /////////////////////////////////////////////////////////////////////////////////// 48 191 // @ channel : TXT channel. 49 // @ busy : TXT device acces mode .192 // @ busy : TXT device acces mode (busy waiting if non zero). 50 193 // @ buf : buffer containing the characters. 51 194 // @ nc : number of characters. … … 66 209 ////////////////////////////////////////////////////////////////////////////////////// 67 210 // @ channel : channel index. 68 // @ busy : TXT device access mode .211 // @ busy : TXT device access mode (busy waiting if non zero). 69 212 // @ format : printf like format. 70 213 // @ args : format arguments. … … 267 410 } 268 411 412 269 413 // Local Variables: 270 414 // tab-width: 4
Note: See TracChangeset
for help on using the changeset viewer.