source: mainline/uspace/app/bdsh/cmds/modules/printf/printf.c

Last change on this file was 28a5ebd, checked in by Martin Decky <martin@…>, 5 years ago

Use char32_t instead of wchat_t to represent UTF-32 strings

The intention of the native HelenOS string API has been always to
support Unicode in the UTF-8 and UTF-32 encodings as the sole character
representations and ignore the obsolete mess of older single-byte and
multibyte character encodings. Before C11, the wchar_t type has been
slightly misused for the purpose of the UTF-32 strings. The newer
char32_t type is obviously a much more suitable option. The standard
defines char32_t as uint_least32_t, thus we can take the liberty to fix
it to uint32_t.

To maintain compatilibity with the C Standard, the putwchar(wchar_t)
functions has been replaced by our custom putuchar(char32_t) functions
where appropriate.

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[1ffd74e]1/*
2 * Copyright (c) 2012 Alexander Prutkov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
[582a0b8]31#include <stddef.h>
[1ffd74e]32#include "config.h"
33#include "util.h"
34#include "errors.h"
35#include "entry.h"
36#include "printf.h"
37#include "cmds.h"
38#include "str.h"
39
40static const char *cmdname = "printf";
41
42/* Dispays help for printf in various levels */
43void help_cmd_printf(unsigned int level)
44{
45 if (level == HELP_SHORT) {
46 printf("`%s' prints formatted data.\n", cmdname);
47 } else {
48 help_cmd_printf(HELP_SHORT);
49 printf(
[d9f53877]50 "Usage: %s FORMAT [ARGS ...] \n"
51 "Prints ARGS according to FORMAT. Number of expected arguments in\n"
52 "FORMAT must be equals to the number of ARGS. Currently supported\n"
53 "format flags are:\n",
54 cmdname);
[1ffd74e]55 }
56
57 return;
58}
59
[087c27f6]60/** Print a formatted data with lib printf.
[1b20da0]61 *
[087c27f6]62 * Currently available format flags are:
63 * '%d' - integer.
64 * '%u' - unsigned integer.
65 * '%s' - null-terminated string.
[47e00b83]66 *
[087c27f6]67 * @param ch formatted flag.
68 * @param arg string with data to print.
69 */
[28a5ebd]70static int print_arg(char32_t ch, const char *arg)
[1ffd74e]71{
[1433ecda]72 switch (ch) {
[1ffd74e]73 case 'd':
74 printf("%d", (int)(strtol(arg, NULL, 10)));
75 break;
76 case 'u':
77 printf("%u", (unsigned int)(strtoul(arg, NULL, 10)));
78 break;
79 case 's':
80 printf("%s", arg);
81 break;
82 default:
[8e81a7e]83 return CMD_FAILURE;
[1ffd74e]84 }
[8e81a7e]85 return CMD_SUCCESS;
[1ffd74e]86}
87
[087c27f6]88/** Process a control character.
[1b20da0]89 *
[087c27f6]90 * Currently available characters are:
91 * '\n' - new line.
[47e00b83]92 *
[087c27f6]93 * @param ch Control character.
94 */
[28a5ebd]95static int process_ctl(char32_t ch)
[1ffd74e]96{
[1433ecda]97 switch (ch) {
[1ffd74e]98 case 'n':
99 printf("\n");
100 break;
101 default:
[8e81a7e]102 return CMD_FAILURE;
[1ffd74e]103 }
[8e81a7e]104 return CMD_SUCCESS;
[1ffd74e]105}
106
[1b20da0]107/** Prints formatted data.
[087c27f6]108 *
[8e81a7e]109 * Accepted format flags:
110 * %d - print an integer
111 * %u - print an unsigned integer
112 * %s - print a null terminated string
[47e00b83]113 *
[8e81a7e]114 * Accepted output controls:
115 * \n - new line
116 */
[1ffd74e]117int cmd_printf(char **argv)
118{
119 unsigned int argc;
[1433ecda]120 char *fmt;
[1ffd74e]121 size_t pos, fmt_sz;
[28a5ebd]122 char32_t ch;
[1ffd74e]123 bool esc_flag = false;
[8e81a7e]124 unsigned int carg; // Current argument
[1ffd74e]125
126 /* Count the arguments */
[eb748a0]127 argc = 0;
128 while (argv[argc] != NULL)
129 argc++;
[1ffd74e]130
131 if (argc < 2) {
132 printf("Usage: %s FORMAT [ARGS ...] \n", cmdname);
133 return CMD_SUCCESS;
134 }
135
136 fmt = argv[1];
137 fmt_sz = str_size(fmt);
138 pos = 0;
139 carg = 2;
140
141 while ((ch = str_decode(fmt, &pos, fmt_sz))) {
[1433ecda]142 switch (ch) {
[1ffd74e]143
144 case '\\':
[1b20da0]145 if (esc_flag)
[1ffd74e]146 goto emit;
147 esc_flag = true;
148 break;
149
150 case '%':
[1b20da0]151 if (esc_flag)
[1ffd74e]152 goto emit;
153 ch = str_decode(fmt, &pos, fmt_sz);
[1b20da0]154 if (!ch) {
[1ffd74e]155 putchar('%');
156 break;
157 }
[8e81a7e]158 if (carg == argc) {
159 printf("\nBad parameter number. Aborted.\n");
160 return CMD_FAILURE;
161 }
[1ffd74e]162 print_arg(ch, argv[carg]);
163 ++carg;
164 break;
165
166 default:
167 if (esc_flag) {
[087c27f6]168 process_ctl(ch);
[1ffd74e]169 esc_flag = false;
170 break;
171 }
[28a5ebd]172 putuchar(ch);
[1ffd74e]173 break;
174
[d9f53877]175 emit:
[28a5ebd]176 putuchar(ch);
[d9f53877]177 esc_flag = false;
[1ffd74e]178 }
[1b20da0]179 }
[1ffd74e]180
181 return CMD_SUCCESS;
182}
Note: See TracBrowser for help on using the repository browser.