utprint.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /******************************************************************************
  2. *
  3. * Module Name: utprint - Formatted printing routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2014, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utprint")
  46. #define ACPI_FORMAT_SIGN 0x01
  47. #define ACPI_FORMAT_SIGN_PLUS 0x02
  48. #define ACPI_FORMAT_SIGN_PLUS_SPACE 0x04
  49. #define ACPI_FORMAT_ZERO 0x08
  50. #define ACPI_FORMAT_LEFT 0x10
  51. #define ACPI_FORMAT_UPPER 0x20
  52. #define ACPI_FORMAT_PREFIX 0x40
  53. /* Local prototypes */
  54. static acpi_size
  55. acpi_ut_bound_string_length(const char *string, acpi_size count);
  56. static char *acpi_ut_bound_string_output(char *string, const char *end, char c);
  57. static char *acpi_ut_format_number(char *string,
  58. char *end,
  59. u64 number,
  60. u8 base, s32 width, s32 precision, u8 type);
  61. static char *acpi_ut_put_number(char *string, u64 number, u8 base, u8 upper);
  62. /* Module globals */
  63. static const char acpi_gbl_lower_hex_digits[] = "0123456789abcdef";
  64. static const char acpi_gbl_upper_hex_digits[] = "0123456789ABCDEF";
  65. /*******************************************************************************
  66. *
  67. * FUNCTION: acpi_ut_bound_string_length
  68. *
  69. * PARAMETERS: string - String with boundary
  70. * count - Boundary of the string
  71. *
  72. * RETURN: Length of the string. Less than or equal to Count.
  73. *
  74. * DESCRIPTION: Calculate the length of a string with boundary.
  75. *
  76. ******************************************************************************/
  77. static acpi_size
  78. acpi_ut_bound_string_length(const char *string, acpi_size count)
  79. {
  80. u32 length = 0;
  81. while (*string && count) {
  82. length++;
  83. string++;
  84. count--;
  85. }
  86. return (length);
  87. }
  88. /*******************************************************************************
  89. *
  90. * FUNCTION: acpi_ut_bound_string_output
  91. *
  92. * PARAMETERS: string - String with boundary
  93. * end - Boundary of the string
  94. * c - Character to be output to the string
  95. *
  96. * RETURN: Updated position for next valid character
  97. *
  98. * DESCRIPTION: Output a character into a string with boundary check.
  99. *
  100. ******************************************************************************/
  101. static char *acpi_ut_bound_string_output(char *string, const char *end, char c)
  102. {
  103. if (string < end) {
  104. *string = c;
  105. }
  106. ++string;
  107. return (string);
  108. }
  109. /*******************************************************************************
  110. *
  111. * FUNCTION: acpi_ut_put_number
  112. *
  113. * PARAMETERS: string - Buffer to hold reverse-ordered string
  114. * number - Integer to be converted
  115. * base - Base of the integer
  116. * upper - Whether or not using upper cased digits
  117. *
  118. * RETURN: Updated position for next valid character
  119. *
  120. * DESCRIPTION: Convert an integer into a string, note that, the string holds a
  121. * reversed ordered number without the trailing zero.
  122. *
  123. ******************************************************************************/
  124. static char *acpi_ut_put_number(char *string, u64 number, u8 base, u8 upper)
  125. {
  126. const char *digits;
  127. u64 digit_index;
  128. char *pos;
  129. pos = string;
  130. digits = upper ? acpi_gbl_upper_hex_digits : acpi_gbl_lower_hex_digits;
  131. if (number == 0) {
  132. *(pos++) = '0';
  133. } else {
  134. while (number) {
  135. (void)acpi_ut_divide(number, base, &number,
  136. &digit_index);
  137. *(pos++) = digits[digit_index];
  138. }
  139. }
  140. /* *(Pos++) = '0'; */
  141. return (pos);
  142. }
  143. /*******************************************************************************
  144. *
  145. * FUNCTION: acpi_ut_scan_number
  146. *
  147. * PARAMETERS: string - String buffer
  148. * number_ptr - Where the number is returned
  149. *
  150. * RETURN: Updated position for next valid character
  151. *
  152. * DESCRIPTION: Scan a string for a decimal integer.
  153. *
  154. ******************************************************************************/
  155. const char *acpi_ut_scan_number(const char *string, u64 *number_ptr)
  156. {
  157. u64 number = 0;
  158. while (ACPI_IS_DIGIT(*string)) {
  159. number *= 10;
  160. number += *(string++) - '0';
  161. }
  162. *number_ptr = number;
  163. return (string);
  164. }
  165. /*******************************************************************************
  166. *
  167. * FUNCTION: acpi_ut_print_number
  168. *
  169. * PARAMETERS: string - String buffer
  170. * number - The number to be converted
  171. *
  172. * RETURN: Updated position for next valid character
  173. *
  174. * DESCRIPTION: Print a decimal integer into a string.
  175. *
  176. ******************************************************************************/
  177. const char *acpi_ut_print_number(char *string, u64 number)
  178. {
  179. char ascii_string[20];
  180. const char *pos1;
  181. char *pos2;
  182. pos1 = acpi_ut_put_number(ascii_string, number, 10, FALSE);
  183. pos2 = string;
  184. while (pos1 != ascii_string) {
  185. *(pos2++) = *(--pos1);
  186. }
  187. *pos2 = 0;
  188. return (string);
  189. }
  190. /*******************************************************************************
  191. *
  192. * FUNCTION: acpi_ut_format_number
  193. *
  194. * PARAMETERS: string - String buffer with boundary
  195. * end - Boundary of the string
  196. * number - The number to be converted
  197. * base - Base of the integer
  198. * width - Field width
  199. * precision - Precision of the integer
  200. * type - Special printing flags
  201. *
  202. * RETURN: Updated position for next valid character
  203. *
  204. * DESCRIPTION: Print an integer into a string with any base and any precision.
  205. *
  206. ******************************************************************************/
  207. static char *acpi_ut_format_number(char *string,
  208. char *end,
  209. u64 number,
  210. u8 base, s32 width, s32 precision, u8 type)
  211. {
  212. char *pos;
  213. char sign;
  214. char zero;
  215. u8 need_prefix;
  216. u8 upper;
  217. s32 i;
  218. char reversed_string[66];
  219. /* Parameter validation */
  220. if (base < 2 || base > 16) {
  221. return (NULL);
  222. }
  223. if (type & ACPI_FORMAT_LEFT) {
  224. type &= ~ACPI_FORMAT_ZERO;
  225. }
  226. need_prefix = ((type & ACPI_FORMAT_PREFIX)
  227. && base != 10) ? TRUE : FALSE;
  228. upper = (type & ACPI_FORMAT_UPPER) ? TRUE : FALSE;
  229. zero = (type & ACPI_FORMAT_ZERO) ? '0' : ' ';
  230. /* Calculate size according to sign and prefix */
  231. sign = '\0';
  232. if (type & ACPI_FORMAT_SIGN) {
  233. if ((s64) number < 0) {
  234. sign = '-';
  235. number = -(s64) number;
  236. width--;
  237. } else if (type & ACPI_FORMAT_SIGN_PLUS) {
  238. sign = '+';
  239. width--;
  240. } else if (type & ACPI_FORMAT_SIGN_PLUS_SPACE) {
  241. sign = ' ';
  242. width--;
  243. }
  244. }
  245. if (need_prefix) {
  246. width--;
  247. if (base == 16) {
  248. width--;
  249. }
  250. }
  251. /* Generate full string in reverse order */
  252. pos = acpi_ut_put_number(reversed_string, number, base, upper);
  253. i = ACPI_PTR_DIFF(pos, reversed_string);
  254. /* Printing 100 using %2d gives "100", not "00" */
  255. if (i > precision) {
  256. precision = i;
  257. }
  258. width -= precision;
  259. /* Output the string */
  260. if (!(type & (ACPI_FORMAT_ZERO | ACPI_FORMAT_LEFT))) {
  261. while (--width >= 0) {
  262. string = acpi_ut_bound_string_output(string, end, ' ');
  263. }
  264. }
  265. if (sign) {
  266. string = acpi_ut_bound_string_output(string, end, sign);
  267. }
  268. if (need_prefix) {
  269. string = acpi_ut_bound_string_output(string, end, '0');
  270. if (base == 16) {
  271. string = acpi_ut_bound_string_output(string, end,
  272. upper ? 'X' : 'x');
  273. }
  274. }
  275. if (!(type & ACPI_FORMAT_LEFT)) {
  276. while (--width >= 0) {
  277. string = acpi_ut_bound_string_output(string, end, zero);
  278. }
  279. }
  280. while (i <= --precision) {
  281. string = acpi_ut_bound_string_output(string, end, '0');
  282. }
  283. while (--i >= 0) {
  284. string = acpi_ut_bound_string_output(string, end,
  285. reversed_string[i]);
  286. }
  287. while (--width >= 0) {
  288. string = acpi_ut_bound_string_output(string, end, ' ');
  289. }
  290. return (string);
  291. }
  292. /*******************************************************************************
  293. *
  294. * FUNCTION: acpi_ut_vsnprintf
  295. *
  296. * PARAMETERS: string - String with boundary
  297. * size - Boundary of the string
  298. * format - Standard printf format
  299. * args - Argument list
  300. *
  301. * RETURN: Number of bytes actually written.
  302. *
  303. * DESCRIPTION: Formatted output to a string using argument list pointer.
  304. *
  305. ******************************************************************************/
  306. int
  307. acpi_ut_vsnprintf(char *string,
  308. acpi_size size, const char *format, va_list args)
  309. {
  310. u8 base = 10;
  311. u8 type = 0;
  312. s32 width = -1;
  313. s32 precision = -1;
  314. char qualifier = 0;
  315. u64 number;
  316. char *pos;
  317. char *end;
  318. char c;
  319. const char *s;
  320. const void *p;
  321. s32 length;
  322. int i;
  323. pos = string;
  324. end = string + size;
  325. for (; *format; ++format) {
  326. if (*format != '%') {
  327. pos = acpi_ut_bound_string_output(pos, end, *format);
  328. continue;
  329. }
  330. /* Process sign */
  331. do {
  332. ++format;
  333. if (*format == '#') {
  334. type |= ACPI_FORMAT_PREFIX;
  335. } else if (*format == '0') {
  336. type |= ACPI_FORMAT_ZERO;
  337. } else if (*format == '+') {
  338. type |= ACPI_FORMAT_SIGN_PLUS;
  339. } else if (*format == ' ') {
  340. type |= ACPI_FORMAT_SIGN_PLUS_SPACE;
  341. } else if (*format == '-') {
  342. type |= ACPI_FORMAT_LEFT;
  343. } else {
  344. break;
  345. }
  346. } while (1);
  347. /* Process width */
  348. width = -1;
  349. if (ACPI_IS_DIGIT(*format)) {
  350. format = acpi_ut_scan_number(format, &number);
  351. width = (s32) number;
  352. } else if (*format == '*') {
  353. ++format;
  354. width = va_arg(args, int);
  355. if (width < 0) {
  356. width = -width;
  357. type |= ACPI_FORMAT_LEFT;
  358. }
  359. }
  360. /* Process precision */
  361. precision = -1;
  362. if (*format == '.') {
  363. ++format;
  364. if (ACPI_IS_DIGIT(*format)) {
  365. format = acpi_ut_scan_number(format, &number);
  366. precision = (s32) number;
  367. } else if (*format == '*') {
  368. ++format;
  369. precision = va_arg(args, int);
  370. }
  371. if (precision < 0) {
  372. precision = 0;
  373. }
  374. }
  375. /* Process qualifier */
  376. qualifier = -1;
  377. if (*format == 'h' || *format == 'l' || *format == 'L') {
  378. qualifier = *format;
  379. ++format;
  380. if (qualifier == 'l' && *format == 'l') {
  381. qualifier = 'L';
  382. ++format;
  383. }
  384. }
  385. switch (*format) {
  386. case '%':
  387. pos = acpi_ut_bound_string_output(pos, end, '%');
  388. continue;
  389. case 'c':
  390. if (!(type & ACPI_FORMAT_LEFT)) {
  391. while (--width > 0) {
  392. pos =
  393. acpi_ut_bound_string_output(pos,
  394. end,
  395. ' ');
  396. }
  397. }
  398. c = (char)va_arg(args, int);
  399. pos = acpi_ut_bound_string_output(pos, end, c);
  400. while (--width > 0) {
  401. pos =
  402. acpi_ut_bound_string_output(pos, end, ' ');
  403. }
  404. continue;
  405. case 's':
  406. s = va_arg(args, char *);
  407. if (!s) {
  408. s = "<NULL>";
  409. }
  410. length = acpi_ut_bound_string_length(s, precision);
  411. if (!(type & ACPI_FORMAT_LEFT)) {
  412. while (length < width--) {
  413. pos =
  414. acpi_ut_bound_string_output(pos,
  415. end,
  416. ' ');
  417. }
  418. }
  419. for (i = 0; i < length; ++i) {
  420. pos = acpi_ut_bound_string_output(pos, end, *s);
  421. ++s;
  422. }
  423. while (length < width--) {
  424. pos =
  425. acpi_ut_bound_string_output(pos, end, ' ');
  426. }
  427. continue;
  428. case 'o':
  429. base = 8;
  430. break;
  431. case 'X':
  432. type |= ACPI_FORMAT_UPPER;
  433. case 'x':
  434. base = 16;
  435. break;
  436. case 'd':
  437. case 'i':
  438. type |= ACPI_FORMAT_SIGN;
  439. case 'u':
  440. break;
  441. case 'p':
  442. if (width == -1) {
  443. width = 2 * sizeof(void *);
  444. type |= ACPI_FORMAT_ZERO;
  445. }
  446. p = va_arg(args, void *);
  447. pos = acpi_ut_format_number(pos, end,
  448. ACPI_TO_INTEGER(p), 16,
  449. width, precision, type);
  450. continue;
  451. default:
  452. pos = acpi_ut_bound_string_output(pos, end, '%');
  453. if (*format) {
  454. pos =
  455. acpi_ut_bound_string_output(pos, end,
  456. *format);
  457. } else {
  458. --format;
  459. }
  460. continue;
  461. }
  462. if (qualifier == 'L') {
  463. number = va_arg(args, u64);
  464. if (type & ACPI_FORMAT_SIGN) {
  465. number = (s64) number;
  466. }
  467. } else if (qualifier == 'l') {
  468. number = va_arg(args, unsigned long);
  469. if (type & ACPI_FORMAT_SIGN) {
  470. number = (s32) number;
  471. }
  472. } else if (qualifier == 'h') {
  473. number = (u16)va_arg(args, int);
  474. if (type & ACPI_FORMAT_SIGN) {
  475. number = (s16) number;
  476. }
  477. } else {
  478. number = va_arg(args, unsigned int);
  479. if (type & ACPI_FORMAT_SIGN) {
  480. number = (signed int)number;
  481. }
  482. }
  483. pos = acpi_ut_format_number(pos, end, number, base,
  484. width, precision, type);
  485. }
  486. if (size > 0) {
  487. if (pos < end) {
  488. *pos = '\0';
  489. } else {
  490. end[-1] = '\0';
  491. }
  492. }
  493. return (ACPI_PTR_DIFF(pos, string));
  494. }
  495. /*******************************************************************************
  496. *
  497. * FUNCTION: acpi_ut_snprintf
  498. *
  499. * PARAMETERS: string - String with boundary
  500. * size - Boundary of the string
  501. * Format, ... - Standard printf format
  502. *
  503. * RETURN: Number of bytes actually written.
  504. *
  505. * DESCRIPTION: Formatted output to a string.
  506. *
  507. ******************************************************************************/
  508. int acpi_ut_snprintf(char *string, acpi_size size, const char *format, ...)
  509. {
  510. va_list args;
  511. int length;
  512. va_start(args, format);
  513. length = acpi_ut_vsnprintf(string, size, format, args);
  514. va_end(args);
  515. return (length);
  516. }
  517. #ifdef ACPI_APPLICATION
  518. /*******************************************************************************
  519. *
  520. * FUNCTION: acpi_ut_file_vprintf
  521. *
  522. * PARAMETERS: file - File descriptor
  523. * format - Standard printf format
  524. * args - Argument list
  525. *
  526. * RETURN: Number of bytes actually written.
  527. *
  528. * DESCRIPTION: Formatted output to a file using argument list pointer.
  529. *
  530. ******************************************************************************/
  531. int acpi_ut_file_vprintf(ACPI_FILE file, const char *format, va_list args)
  532. {
  533. acpi_cpu_flags flags;
  534. int length;
  535. flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
  536. length = acpi_ut_vsnprintf(acpi_gbl_print_buffer,
  537. sizeof(acpi_gbl_print_buffer), format, args);
  538. (void)acpi_os_write_file(file, acpi_gbl_print_buffer, length, 1);
  539. acpi_os_release_lock(acpi_gbl_print_lock, flags);
  540. return (length);
  541. }
  542. /*******************************************************************************
  543. *
  544. * FUNCTION: acpi_ut_file_printf
  545. *
  546. * PARAMETERS: file - File descriptor
  547. * Format, ... - Standard printf format
  548. *
  549. * RETURN: Number of bytes actually written.
  550. *
  551. * DESCRIPTION: Formatted output to a file.
  552. *
  553. ******************************************************************************/
  554. int acpi_ut_file_printf(ACPI_FILE file, const char *format, ...)
  555. {
  556. va_list args;
  557. int length;
  558. va_start(args, format);
  559. length = acpi_ut_file_vprintf(file, format, args);
  560. va_end(args);
  561. return (length);
  562. }
  563. #endif