utprint.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /******************************************************************************
  2. *
  3. * Module Name: utprint - Formatted printing routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, 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. /*******************************************************************************
  63. *
  64. * FUNCTION: acpi_ut_bound_string_length
  65. *
  66. * PARAMETERS: string - String with boundary
  67. * count - Boundary of the string
  68. *
  69. * RETURN: Length of the string. Less than or equal to Count.
  70. *
  71. * DESCRIPTION: Calculate the length of a string with boundary.
  72. *
  73. ******************************************************************************/
  74. static acpi_size
  75. acpi_ut_bound_string_length(const char *string, acpi_size count)
  76. {
  77. u32 length = 0;
  78. while (*string && count) {
  79. length++;
  80. string++;
  81. count--;
  82. }
  83. return (length);
  84. }
  85. /*******************************************************************************
  86. *
  87. * FUNCTION: acpi_ut_bound_string_output
  88. *
  89. * PARAMETERS: string - String with boundary
  90. * end - Boundary of the string
  91. * c - Character to be output to the string
  92. *
  93. * RETURN: Updated position for next valid character
  94. *
  95. * DESCRIPTION: Output a character into a string with boundary check.
  96. *
  97. ******************************************************************************/
  98. static char *acpi_ut_bound_string_output(char *string, const char *end, char c)
  99. {
  100. if (string < end) {
  101. *string = c;
  102. }
  103. ++string;
  104. return (string);
  105. }
  106. /*******************************************************************************
  107. *
  108. * FUNCTION: acpi_ut_put_number
  109. *
  110. * PARAMETERS: string - Buffer to hold reverse-ordered string
  111. * number - Integer to be converted
  112. * base - Base of the integer
  113. * upper - Whether or not using upper cased digits
  114. *
  115. * RETURN: Updated position for next valid character
  116. *
  117. * DESCRIPTION: Convert an integer into a string, note that, the string holds a
  118. * reversed ordered number without the trailing zero.
  119. *
  120. ******************************************************************************/
  121. static char *acpi_ut_put_number(char *string, u64 number, u8 base, u8 upper)
  122. {
  123. const char *digits;
  124. u64 digit_index;
  125. char *pos;
  126. pos = string;
  127. digits = upper ? acpi_gbl_upper_hex_digits : acpi_gbl_lower_hex_digits;
  128. if (number == 0) {
  129. *(pos++) = '0';
  130. } else {
  131. while (number) {
  132. (void)acpi_ut_divide(number, base, &number,
  133. &digit_index);
  134. *(pos++) = digits[digit_index];
  135. }
  136. }
  137. /* *(Pos++) = '0'; */
  138. return (pos);
  139. }
  140. /*******************************************************************************
  141. *
  142. * FUNCTION: acpi_ut_scan_number
  143. *
  144. * PARAMETERS: string - String buffer
  145. * number_ptr - Where the number is returned
  146. *
  147. * RETURN: Updated position for next valid character
  148. *
  149. * DESCRIPTION: Scan a string for a decimal integer.
  150. *
  151. ******************************************************************************/
  152. const char *acpi_ut_scan_number(const char *string, u64 *number_ptr)
  153. {
  154. u64 number = 0;
  155. while (isdigit((int)*string)) {
  156. number *= 10;
  157. number += *(string++) - '0';
  158. }
  159. *number_ptr = number;
  160. return (string);
  161. }
  162. /*******************************************************************************
  163. *
  164. * FUNCTION: acpi_ut_print_number
  165. *
  166. * PARAMETERS: string - String buffer
  167. * number - The number to be converted
  168. *
  169. * RETURN: Updated position for next valid character
  170. *
  171. * DESCRIPTION: Print a decimal integer into a string.
  172. *
  173. ******************************************************************************/
  174. const char *acpi_ut_print_number(char *string, u64 number)
  175. {
  176. char ascii_string[20];
  177. const char *pos1;
  178. char *pos2;
  179. pos1 = acpi_ut_put_number(ascii_string, number, 10, FALSE);
  180. pos2 = string;
  181. while (pos1 != ascii_string) {
  182. *(pos2++) = *(--pos1);
  183. }
  184. *pos2 = 0;
  185. return (string);
  186. }
  187. /*******************************************************************************
  188. *
  189. * FUNCTION: acpi_ut_format_number
  190. *
  191. * PARAMETERS: string - String buffer with boundary
  192. * end - Boundary of the string
  193. * number - The number to be converted
  194. * base - Base of the integer
  195. * width - Field width
  196. * precision - Precision of the integer
  197. * type - Special printing flags
  198. *
  199. * RETURN: Updated position for next valid character
  200. *
  201. * DESCRIPTION: Print an integer into a string with any base and any precision.
  202. *
  203. ******************************************************************************/
  204. static char *acpi_ut_format_number(char *string,
  205. char *end,
  206. u64 number,
  207. u8 base, s32 width, s32 precision, u8 type)
  208. {
  209. char *pos;
  210. char sign;
  211. char zero;
  212. u8 need_prefix;
  213. u8 upper;
  214. s32 i;
  215. char reversed_string[66];
  216. /* Parameter validation */
  217. if (base < 2 || base > 16) {
  218. return (NULL);
  219. }
  220. if (type & ACPI_FORMAT_LEFT) {
  221. type &= ~ACPI_FORMAT_ZERO;
  222. }
  223. need_prefix = ((type & ACPI_FORMAT_PREFIX)
  224. && base != 10) ? TRUE : FALSE;
  225. upper = (type & ACPI_FORMAT_UPPER) ? TRUE : FALSE;
  226. zero = (type & ACPI_FORMAT_ZERO) ? '0' : ' ';
  227. /* Calculate size according to sign and prefix */
  228. sign = '\0';
  229. if (type & ACPI_FORMAT_SIGN) {
  230. if ((s64)number < 0) {
  231. sign = '-';
  232. number = -(s64)number;
  233. width--;
  234. } else if (type & ACPI_FORMAT_SIGN_PLUS) {
  235. sign = '+';
  236. width--;
  237. } else if (type & ACPI_FORMAT_SIGN_PLUS_SPACE) {
  238. sign = ' ';
  239. width--;
  240. }
  241. }
  242. if (need_prefix) {
  243. width--;
  244. if (base == 16) {
  245. width--;
  246. }
  247. }
  248. /* Generate full string in reverse order */
  249. pos = acpi_ut_put_number(reversed_string, number, base, upper);
  250. i = ACPI_PTR_DIFF(pos, reversed_string);
  251. /* Printing 100 using %2d gives "100", not "00" */
  252. if (i > precision) {
  253. precision = i;
  254. }
  255. width -= precision;
  256. /* Output the string */
  257. if (!(type & (ACPI_FORMAT_ZERO | ACPI_FORMAT_LEFT))) {
  258. while (--width >= 0) {
  259. string = acpi_ut_bound_string_output(string, end, ' ');
  260. }
  261. }
  262. if (sign) {
  263. string = acpi_ut_bound_string_output(string, end, sign);
  264. }
  265. if (need_prefix) {
  266. string = acpi_ut_bound_string_output(string, end, '0');
  267. if (base == 16) {
  268. string =
  269. acpi_ut_bound_string_output(string, end,
  270. upper ? 'X' : 'x');
  271. }
  272. }
  273. if (!(type & ACPI_FORMAT_LEFT)) {
  274. while (--width >= 0) {
  275. string = acpi_ut_bound_string_output(string, end, zero);
  276. }
  277. }
  278. while (i <= --precision) {
  279. string = acpi_ut_bound_string_output(string, end, '0');
  280. }
  281. while (--i >= 0) {
  282. string = acpi_ut_bound_string_output(string, end,
  283. reversed_string[i]);
  284. }
  285. while (--width >= 0) {
  286. string = acpi_ut_bound_string_output(string, end, ' ');
  287. }
  288. return (string);
  289. }
  290. /*******************************************************************************
  291. *
  292. * FUNCTION: vsnprintf
  293. *
  294. * PARAMETERS: string - String with boundary
  295. * size - Boundary of the string
  296. * format - Standard printf format
  297. * args - Argument list
  298. *
  299. * RETURN: Number of bytes actually written.
  300. *
  301. * DESCRIPTION: Formatted output to a string using argument list pointer.
  302. *
  303. ******************************************************************************/
  304. int vsnprintf(char *string, acpi_size size, const char *format, va_list args)
  305. {
  306. u8 base;
  307. u8 type;
  308. s32 width;
  309. s32 precision;
  310. char qualifier;
  311. u64 number;
  312. char *pos;
  313. char *end;
  314. char c;
  315. const char *s;
  316. const void *p;
  317. s32 length;
  318. int i;
  319. pos = string;
  320. end = string + size;
  321. for (; *format; ++format) {
  322. if (*format != '%') {
  323. pos = acpi_ut_bound_string_output(pos, end, *format);
  324. continue;
  325. }
  326. type = 0;
  327. base = 10;
  328. /* Process sign */
  329. do {
  330. ++format;
  331. if (*format == '#') {
  332. type |= ACPI_FORMAT_PREFIX;
  333. } else if (*format == '0') {
  334. type |= ACPI_FORMAT_ZERO;
  335. } else if (*format == '+') {
  336. type |= ACPI_FORMAT_SIGN_PLUS;
  337. } else if (*format == ' ') {
  338. type |= ACPI_FORMAT_SIGN_PLUS_SPACE;
  339. } else if (*format == '-') {
  340. type |= ACPI_FORMAT_LEFT;
  341. } else {
  342. break;
  343. }
  344. } while (1);
  345. /* Process width */
  346. width = -1;
  347. if (isdigit((int)*format)) {
  348. format = acpi_ut_scan_number(format, &number);
  349. width = (s32)number;
  350. } else if (*format == '*') {
  351. ++format;
  352. width = va_arg(args, int);
  353. if (width < 0) {
  354. width = -width;
  355. type |= ACPI_FORMAT_LEFT;
  356. }
  357. }
  358. /* Process precision */
  359. precision = -1;
  360. if (*format == '.') {
  361. ++format;
  362. if (isdigit((int)*format)) {
  363. format = acpi_ut_scan_number(format, &number);
  364. precision = (s32)number;
  365. } else if (*format == '*') {
  366. ++format;
  367. precision = va_arg(args, int);
  368. }
  369. if (precision < 0) {
  370. precision = 0;
  371. }
  372. }
  373. /* Process qualifier */
  374. qualifier = -1;
  375. if (*format == 'h' || *format == 'l' || *format == 'L') {
  376. qualifier = *format;
  377. ++format;
  378. if (qualifier == 'l' && *format == 'l') {
  379. qualifier = 'L';
  380. ++format;
  381. }
  382. }
  383. switch (*format) {
  384. case '%':
  385. pos = acpi_ut_bound_string_output(pos, end, '%');
  386. continue;
  387. case 'c':
  388. if (!(type & ACPI_FORMAT_LEFT)) {
  389. while (--width > 0) {
  390. pos =
  391. acpi_ut_bound_string_output(pos,
  392. end,
  393. ' ');
  394. }
  395. }
  396. c = (char)va_arg(args, int);
  397. pos = acpi_ut_bound_string_output(pos, end, c);
  398. while (--width > 0) {
  399. pos =
  400. acpi_ut_bound_string_output(pos, end, ' ');
  401. }
  402. continue;
  403. case 's':
  404. s = va_arg(args, char *);
  405. if (!s) {
  406. s = "<NULL>";
  407. }
  408. length = acpi_ut_bound_string_length(s, precision);
  409. if (!(type & ACPI_FORMAT_LEFT)) {
  410. while (length < width--) {
  411. pos =
  412. acpi_ut_bound_string_output(pos,
  413. end,
  414. ' ');
  415. }
  416. }
  417. for (i = 0; i < length; ++i) {
  418. pos = acpi_ut_bound_string_output(pos, end, *s);
  419. ++s;
  420. }
  421. while (length < width--) {
  422. pos =
  423. acpi_ut_bound_string_output(pos, end, ' ');
  424. }
  425. continue;
  426. case 'o':
  427. base = 8;
  428. break;
  429. case 'X':
  430. type |= ACPI_FORMAT_UPPER;
  431. case 'x':
  432. base = 16;
  433. break;
  434. case 'd':
  435. case 'i':
  436. type |= ACPI_FORMAT_SIGN;
  437. case 'u':
  438. break;
  439. case 'p':
  440. if (width == -1) {
  441. width = 2 * sizeof(void *);
  442. type |= ACPI_FORMAT_ZERO;
  443. }
  444. p = va_arg(args, void *);
  445. pos =
  446. acpi_ut_format_number(pos, end, ACPI_TO_INTEGER(p),
  447. 16, width, precision, type);
  448. continue;
  449. default:
  450. pos = acpi_ut_bound_string_output(pos, end, '%');
  451. if (*format) {
  452. pos =
  453. acpi_ut_bound_string_output(pos, end,
  454. *format);
  455. } else {
  456. --format;
  457. }
  458. continue;
  459. }
  460. if (qualifier == 'L') {
  461. number = va_arg(args, u64);
  462. if (type & ACPI_FORMAT_SIGN) {
  463. number = (s64)number;
  464. }
  465. } else if (qualifier == 'l') {
  466. number = va_arg(args, unsigned long);
  467. if (type & ACPI_FORMAT_SIGN) {
  468. number = (s32)number;
  469. }
  470. } else if (qualifier == 'h') {
  471. number = (u16)va_arg(args, int);
  472. if (type & ACPI_FORMAT_SIGN) {
  473. number = (s16)number;
  474. }
  475. } else {
  476. number = va_arg(args, unsigned int);
  477. if (type & ACPI_FORMAT_SIGN) {
  478. number = (signed int)number;
  479. }
  480. }
  481. pos = acpi_ut_format_number(pos, end, number, base,
  482. width, precision, type);
  483. }
  484. if (size > 0) {
  485. if (pos < end) {
  486. *pos = '\0';
  487. } else {
  488. end[-1] = '\0';
  489. }
  490. }
  491. return (ACPI_PTR_DIFF(pos, string));
  492. }
  493. /*******************************************************************************
  494. *
  495. * FUNCTION: snprintf
  496. *
  497. * PARAMETERS: string - String with boundary
  498. * size - Boundary of the string
  499. * Format, ... - Standard printf format
  500. *
  501. * RETURN: Number of bytes actually written.
  502. *
  503. * DESCRIPTION: Formatted output to a string.
  504. *
  505. ******************************************************************************/
  506. int snprintf(char *string, acpi_size size, const char *format, ...)
  507. {
  508. va_list args;
  509. int length;
  510. va_start(args, format);
  511. length = vsnprintf(string, size, format, args);
  512. va_end(args);
  513. return (length);
  514. }
  515. /*******************************************************************************
  516. *
  517. * FUNCTION: sprintf
  518. *
  519. * PARAMETERS: string - String with boundary
  520. * Format, ... - Standard printf format
  521. *
  522. * RETURN: Number of bytes actually written.
  523. *
  524. * DESCRIPTION: Formatted output to a string.
  525. *
  526. ******************************************************************************/
  527. int sprintf(char *string, const char *format, ...)
  528. {
  529. va_list args;
  530. int length;
  531. va_start(args, format);
  532. length = vsnprintf(string, ACPI_UINT32_MAX, format, args);
  533. va_end(args);
  534. return (length);
  535. }
  536. #ifdef ACPI_APPLICATION
  537. /*******************************************************************************
  538. *
  539. * FUNCTION: vprintf
  540. *
  541. * PARAMETERS: format - Standard printf format
  542. * args - Argument list
  543. *
  544. * RETURN: Number of bytes actually written.
  545. *
  546. * DESCRIPTION: Formatted output to stdout using argument list pointer.
  547. *
  548. ******************************************************************************/
  549. int vprintf(const char *format, va_list args)
  550. {
  551. acpi_cpu_flags flags;
  552. int length;
  553. flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
  554. length = vsnprintf(acpi_gbl_print_buffer,
  555. sizeof(acpi_gbl_print_buffer), format, args);
  556. (void)fwrite(acpi_gbl_print_buffer, length, 1, ACPI_FILE_OUT);
  557. acpi_os_release_lock(acpi_gbl_print_lock, flags);
  558. return (length);
  559. }
  560. /*******************************************************************************
  561. *
  562. * FUNCTION: printf
  563. *
  564. * PARAMETERS: Format, ... - Standard printf format
  565. *
  566. * RETURN: Number of bytes actually written.
  567. *
  568. * DESCRIPTION: Formatted output to stdout.
  569. *
  570. ******************************************************************************/
  571. int printf(const char *format, ...)
  572. {
  573. va_list args;
  574. int length;
  575. va_start(args, format);
  576. length = vprintf(format, args);
  577. va_end(args);
  578. return (length);
  579. }
  580. /*******************************************************************************
  581. *
  582. * FUNCTION: vfprintf
  583. *
  584. * PARAMETERS: file - File descriptor
  585. * format - Standard printf format
  586. * args - Argument list
  587. *
  588. * RETURN: Number of bytes actually written.
  589. *
  590. * DESCRIPTION: Formatted output to a file using argument list pointer.
  591. *
  592. ******************************************************************************/
  593. int vfprintf(FILE * file, const char *format, va_list args)
  594. {
  595. acpi_cpu_flags flags;
  596. int length;
  597. flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
  598. length = vsnprintf(acpi_gbl_print_buffer,
  599. sizeof(acpi_gbl_print_buffer), format, args);
  600. (void)fwrite(acpi_gbl_print_buffer, length, 1, file);
  601. acpi_os_release_lock(acpi_gbl_print_lock, flags);
  602. return (length);
  603. }
  604. /*******************************************************************************
  605. *
  606. * FUNCTION: fprintf
  607. *
  608. * PARAMETERS: file - File descriptor
  609. * Format, ... - Standard printf format
  610. *
  611. * RETURN: Number of bytes actually written.
  612. *
  613. * DESCRIPTION: Formatted output to a file.
  614. *
  615. ******************************************************************************/
  616. int fprintf(FILE * file, const char *format, ...)
  617. {
  618. va_list args;
  619. int length;
  620. va_start(args, format);
  621. length = vfprintf(file, format, args);
  622. va_end(args);
  623. return (length);
  624. }
  625. #endif