[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>
|
---|
| 31 | #include "config.h"
|
---|
| 32 | #include "util.h"
|
---|
| 33 | #include "errors.h"
|
---|
| 34 | #include "entry.h"
|
---|
| 35 | #include "printf.h"
|
---|
| 36 | #include "cmds.h"
|
---|
| 37 | #include "str.h"
|
---|
| 38 |
|
---|
| 39 | static const char *cmdname = "printf";
|
---|
| 40 |
|
---|
| 41 | /* Dispays help for printf in various levels */
|
---|
| 42 | void help_cmd_printf(unsigned int level)
|
---|
| 43 | {
|
---|
| 44 | if (level == HELP_SHORT) {
|
---|
| 45 | printf("`%s' prints formatted data.\n", cmdname);
|
---|
| 46 | } else {
|
---|
| 47 | help_cmd_printf(HELP_SHORT);
|
---|
| 48 | printf(
|
---|
[d9f53877] | 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",
|
---|
| 53 | cmdname);
|
---|
[1ffd74e] | 54 | }
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | return;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[087c27f6] | 60 | /** Print a formatted data with lib printf.
|
---|
| 61 | *
|
---|
| 62 | * Currently available format flags are:
|
---|
| 63 | * '%d' - integer.
|
---|
| 64 | * '%u' - unsigned integer.
|
---|
| 65 | * '%s' - null-terminated string.
|
---|
| 66 | *****
|
---|
| 67 | * @param ch formatted flag.
|
---|
| 68 | * @param arg string with data to print.
|
---|
| 69 | */
|
---|
[8e81a7e] | 70 | static int print_arg(wchar_t ch, const char* arg)
|
---|
[1ffd74e] | 71 | {
|
---|
| 72 | switch(ch) {
|
---|
| 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.
|
---|
| 89 | *
|
---|
| 90 | * Currently available characters are:
|
---|
| 91 | * '\n' - new line.
|
---|
| 92 | *****
|
---|
| 93 | * @param ch Control character.
|
---|
| 94 | */
|
---|
| 95 | static int process_ctl(wchar_t ch)
|
---|
[1ffd74e] | 96 | {
|
---|
| 97 | switch(ch) {
|
---|
| 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 |
|
---|
| 107 |
|
---|
[087c27f6] | 108 | /** Prints formatted data.
|
---|
| 109 | *
|
---|
[8e81a7e] | 110 | * Accepted format flags:
|
---|
| 111 | * %d - print an integer
|
---|
| 112 | * %u - print an unsigned integer
|
---|
| 113 | * %s - print a null terminated string
|
---|
| 114 | *****
|
---|
| 115 | * Accepted output controls:
|
---|
| 116 | * \n - new line
|
---|
| 117 | */
|
---|
[1ffd74e] | 118 | int cmd_printf(char **argv)
|
---|
| 119 | {
|
---|
| 120 | unsigned int argc;
|
---|
| 121 | char* fmt;
|
---|
| 122 | size_t pos, fmt_sz;
|
---|
| 123 | wchar_t ch;
|
---|
| 124 | bool esc_flag = false;
|
---|
[8e81a7e] | 125 | unsigned int carg; // Current argument
|
---|
[1ffd74e] | 126 |
|
---|
| 127 | /* Count the arguments */
|
---|
| 128 | for (argc = 0; argv[argc] != NULL; argc ++);
|
---|
| 129 |
|
---|
| 130 | if (argc < 2) {
|
---|
| 131 | printf("Usage: %s FORMAT [ARGS ...] \n", cmdname);
|
---|
| 132 | return CMD_SUCCESS;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | fmt = argv[1];
|
---|
| 136 | fmt_sz = str_size(fmt);
|
---|
| 137 | pos = 0;
|
---|
| 138 | carg = 2;
|
---|
| 139 |
|
---|
| 140 | while ((ch = str_decode(fmt, &pos, fmt_sz))) {
|
---|
| 141 | switch(ch) {
|
---|
| 142 |
|
---|
| 143 | case '\\':
|
---|
| 144 | if (esc_flag)
|
---|
| 145 | goto emit;
|
---|
| 146 | esc_flag = true;
|
---|
| 147 | break;
|
---|
| 148 |
|
---|
| 149 | case '%':
|
---|
| 150 | if (esc_flag)
|
---|
| 151 | goto emit;
|
---|
| 152 | ch = str_decode(fmt, &pos, fmt_sz);
|
---|
| 153 | if (!ch) {
|
---|
| 154 | putchar('%');
|
---|
| 155 | break;
|
---|
| 156 | }
|
---|
[8e81a7e] | 157 | if (carg == argc) {
|
---|
| 158 | printf("\nBad parameter number. Aborted.\n");
|
---|
| 159 | return CMD_FAILURE;
|
---|
| 160 | }
|
---|
[1ffd74e] | 161 | print_arg(ch, argv[carg]);
|
---|
| 162 | ++carg;
|
---|
| 163 | break;
|
---|
| 164 |
|
---|
| 165 | default:
|
---|
| 166 | if (esc_flag) {
|
---|
[087c27f6] | 167 | process_ctl(ch);
|
---|
[1ffd74e] | 168 | esc_flag = false;
|
---|
| 169 | break;
|
---|
| 170 | }
|
---|
| 171 | putchar(ch);
|
---|
| 172 | break;
|
---|
| 173 |
|
---|
[d9f53877] | 174 | emit:
|
---|
| 175 | putchar(ch);
|
---|
| 176 | esc_flag = false;
|
---|
[1ffd74e] | 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | return CMD_SUCCESS;
|
---|
| 181 | }
|
---|