2
* Copyright (c) 2012 Alexander Prutkov
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
39
static const char *cmdname = "printf";
41
/* Dispays help for printf in various levels */
42
void help_cmd_printf(unsigned int level)
44
if (level == HELP_SHORT) {
45
printf("`%s' prints formatted data.\n", cmdname);
47
help_cmd_printf(HELP_SHORT);
49
"Usage: %s FORMAT [ARGS ...] \n"
50
"Prints ARGS according to FORMAT. Number of expected arguments in\n"
51
"FORMAT must be equals to the number of ARGS. Currently supported\n"
52
"format flags are:\n",
60
/** Print a formatted data with lib printf.
62
* Currently available format flags are:
64
* '%u' - unsigned integer.
65
* '%s' - null-terminated string.
67
* @param ch formatted flag.
68
* @param arg string with data to print.
70
static int print_arg(wchar_t ch, const char* arg)
74
printf("%d", (int)(strtol(arg, NULL, 10)));
77
printf("%u", (unsigned int)(strtoul(arg, NULL, 10)));
88
/** Process a control character.
90
* Currently available characters are:
93
* @param ch Control character.
95
static int process_ctl(wchar_t ch)
108
/** Prints formatted data.
110
* Accepted format flags:
111
* %d - print an integer
112
* %u - print an unsigned integer
113
* %s - print a null terminated string
115
* Accepted output controls:
118
int cmd_printf(char **argv)
124
bool esc_flag = false;
125
unsigned int carg; // Current argument
127
/* Count the arguments */
128
for (argc = 0; argv[argc] != NULL; argc ++);
131
printf("Usage: %s FORMAT [ARGS ...] \n", cmdname);
136
fmt_sz = str_size(fmt);
140
while ((ch = str_decode(fmt, &pos, fmt_sz))) {
152
ch = str_decode(fmt, &pos, fmt_sz);
158
printf("\nBad parameter number. Aborted.\n");
161
print_arg(ch, argv[carg]);