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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1433ecda was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 4.2 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
[a35b458]57
[1ffd74e]58 return;
59}
60
[087c27f6]61/** Print a formatted data with lib printf.
[1b20da0]62 *
[087c27f6]63 * Currently available format flags are:
64 * '%d' - integer.
65 * '%u' - unsigned integer.
66 * '%s' - null-terminated string.
[1b20da0]67 *****
[087c27f6]68 * @param ch formatted flag.
69 * @param arg string with data to print.
70 */
[1433ecda]71static int print_arg(wchar_t ch, const char *arg)
[1ffd74e]72{
[1433ecda]73 switch (ch) {
[1ffd74e]74 case 'd':
75 printf("%d", (int)(strtol(arg, NULL, 10)));
76 break;
77 case 'u':
78 printf("%u", (unsigned int)(strtoul(arg, NULL, 10)));
79 break;
80 case 's':
81 printf("%s", arg);
82 break;
83 default:
[8e81a7e]84 return CMD_FAILURE;
[1ffd74e]85 }
[8e81a7e]86 return CMD_SUCCESS;
[1ffd74e]87}
88
[087c27f6]89/** Process a control character.
[1b20da0]90 *
[087c27f6]91 * Currently available characters are:
92 * '\n' - new line.
[1b20da0]93 *****
[087c27f6]94 * @param ch Control character.
95 */
96static int process_ctl(wchar_t ch)
[1ffd74e]97{
[1433ecda]98 switch (ch) {
[1ffd74e]99 case 'n':
100 printf("\n");
101 break;
102 default:
[8e81a7e]103 return CMD_FAILURE;
[1ffd74e]104 }
[8e81a7e]105 return CMD_SUCCESS;
[1ffd74e]106}
107
108
[1b20da0]109/** Prints formatted data.
[087c27f6]110 *
[8e81a7e]111 * Accepted format flags:
112 * %d - print an integer
113 * %u - print an unsigned integer
114 * %s - print a null terminated string
115 *****
116 * Accepted output controls:
117 * \n - new line
118 */
[1ffd74e]119int cmd_printf(char **argv)
120{
121 unsigned int argc;
[1433ecda]122 char *fmt;
[1ffd74e]123 size_t pos, fmt_sz;
124 wchar_t ch;
125 bool esc_flag = false;
[8e81a7e]126 unsigned int carg; // Current argument
[1ffd74e]127
128 /* Count the arguments */
[eb748a0]129 argc = 0;
130 while (argv[argc] != NULL)
131 argc++;
[1ffd74e]132
133 if (argc < 2) {
134 printf("Usage: %s FORMAT [ARGS ...] \n", cmdname);
135 return CMD_SUCCESS;
136 }
137
138 fmt = argv[1];
139 fmt_sz = str_size(fmt);
140 pos = 0;
141 carg = 2;
142
143 while ((ch = str_decode(fmt, &pos, fmt_sz))) {
[1433ecda]144 switch (ch) {
[1ffd74e]145
146 case '\\':
[1b20da0]147 if (esc_flag)
[1ffd74e]148 goto emit;
149 esc_flag = true;
150 break;
151
152 case '%':
[1b20da0]153 if (esc_flag)
[1ffd74e]154 goto emit;
155 ch = str_decode(fmt, &pos, fmt_sz);
[1b20da0]156 if (!ch) {
[1ffd74e]157 putchar('%');
158 break;
159 }
[8e81a7e]160 if (carg == argc) {
161 printf("\nBad parameter number. Aborted.\n");
162 return CMD_FAILURE;
163 }
[1ffd74e]164 print_arg(ch, argv[carg]);
165 ++carg;
166 break;
167
168 default:
169 if (esc_flag) {
[087c27f6]170 process_ctl(ch);
[1ffd74e]171 esc_flag = false;
172 break;
173 }
[1b20da0]174 putchar(ch);
[1ffd74e]175 break;
176
[d9f53877]177 emit:
178 putchar(ch);
179 esc_flag = false;
[1ffd74e]180 }
[1b20da0]181 }
[1ffd74e]182
183 return CMD_SUCCESS;
184}
Note: See TracBrowser for help on using the repository browser.