2015-02-15 08:46:58 -05:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-09-28 10:10:27 -04:00
|
|
|
#include <ctype.h>
|
2016-02-29 11:34:34 -05:00
|
|
|
#include <errno.h>
|
2015-02-15 08:46:58 -05:00
|
|
|
#include <limits.h>
|
2014-09-28 10:10:27 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-02-15 08:46:58 -05:00
|
|
|
#include "utf.h"
|
|
|
|
#include "util.h"
|
2014-09-28 10:10:27 -04:00
|
|
|
|
2015-02-15 11:00:32 -05:00
|
|
|
static void
|
|
|
|
usage(void)
|
2015-02-15 08:46:58 -05:00
|
|
|
{
|
2015-04-03 05:41:38 -04:00
|
|
|
eprintf("usage: %s format [arg ...]\n", argv0);
|
2014-09-28 10:10:27 -04:00
|
|
|
}
|
|
|
|
|
2015-02-15 11:00:32 -05:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2014-09-28 10:10:27 -04:00
|
|
|
{
|
2015-02-15 08:46:58 -05:00
|
|
|
Rune *rarg;
|
2016-02-29 09:35:13 -05:00
|
|
|
size_t i, j, argi, lastargi, formatlen;
|
2015-02-15 08:46:58 -05:00
|
|
|
long long num;
|
|
|
|
double dou;
|
2016-02-29 11:34:34 -05:00
|
|
|
int cooldown = 0, width, precision, ret = 0;
|
2015-04-27 14:27:37 -04:00
|
|
|
char *format, *tmp, *arg, *fmt, flag;
|
2015-02-15 08:46:58 -05:00
|
|
|
|
|
|
|
argv0 = argv[0];
|
|
|
|
if (argc < 2)
|
2014-09-28 10:10:27 -04:00
|
|
|
usage();
|
|
|
|
|
2015-02-15 08:46:58 -05:00
|
|
|
format = argv[1];
|
|
|
|
if ((tmp = strstr(format, "\\c"))) {
|
|
|
|
*tmp = 0;
|
|
|
|
cooldown = 1;
|
|
|
|
}
|
|
|
|
formatlen = unescape(format);
|
2015-02-15 09:20:32 -05:00
|
|
|
if (formatlen == 0)
|
|
|
|
return 0;
|
2015-02-15 08:46:58 -05:00
|
|
|
lastargi = 0;
|
|
|
|
for (i = 0, argi = 2; !cooldown || i < formatlen; i++, i = cooldown ? i : (i % formatlen)) {
|
|
|
|
if (i == 0) {
|
|
|
|
if (lastargi == argi)
|
2014-09-28 10:10:27 -04:00
|
|
|
break;
|
2015-02-15 08:46:58 -05:00
|
|
|
lastargi = argi;
|
|
|
|
}
|
|
|
|
if (format[i] != '%') {
|
|
|
|
putchar(format[i]);
|
|
|
|
continue;
|
|
|
|
}
|
2014-09-28 10:10:27 -04:00
|
|
|
|
2015-04-27 14:27:37 -04:00
|
|
|
/* flag */
|
2015-06-21 09:52:21 -04:00
|
|
|
for (flag = '\0', i++; strchr("#-+ 0", format[i]); i++) {
|
2015-06-21 09:57:32 -04:00
|
|
|
flag = format[i];
|
2015-04-27 14:27:37 -04:00
|
|
|
}
|
|
|
|
|
2015-02-15 08:46:58 -05:00
|
|
|
/* field width */
|
2015-03-26 18:00:02 -04:00
|
|
|
width = -1;
|
2015-02-15 08:46:58 -05:00
|
|
|
if (format[i] == '*') {
|
|
|
|
if (argi < argc)
|
|
|
|
width = estrtonum(argv[argi++], 0, INT_MAX);
|
|
|
|
else
|
|
|
|
cooldown = 1;
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
j = i;
|
|
|
|
for (; strchr("+-0123456789", format[i]); i++);
|
|
|
|
if (j != i) {
|
|
|
|
tmp = estrndup(format + j, i - j);
|
|
|
|
width = estrtonum(tmp, 0, INT_MAX);
|
|
|
|
free(tmp);
|
2015-03-26 18:00:02 -04:00
|
|
|
} else {
|
|
|
|
width = 0;
|
2014-09-28 10:10:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 08:46:58 -05:00
|
|
|
/* field precision */
|
2015-03-26 18:00:02 -04:00
|
|
|
precision = -1;
|
2015-02-15 08:46:58 -05:00
|
|
|
if (format[i] == '.') {
|
|
|
|
if (format[++i] == '*') {
|
|
|
|
if (argi < argc)
|
|
|
|
precision = estrtonum(argv[argi++], 0, INT_MAX);
|
|
|
|
else
|
|
|
|
cooldown = 1;
|
2015-03-26 18:00:02 -04:00
|
|
|
i++;
|
2014-09-28 10:10:27 -04:00
|
|
|
} else {
|
2015-02-15 08:46:58 -05:00
|
|
|
j = i;
|
|
|
|
for (; strchr("+-0123456789", format[i]); i++);
|
|
|
|
if (j != i) {
|
|
|
|
tmp = estrndup(format + j, i - j);
|
|
|
|
precision = estrtonum(tmp, 0, INT_MAX);
|
|
|
|
free(tmp);
|
2015-03-26 18:00:02 -04:00
|
|
|
} else {
|
|
|
|
precision = 0;
|
2015-02-15 08:46:58 -05:00
|
|
|
}
|
2014-09-28 10:10:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 08:46:58 -05:00
|
|
|
if (format[i] != '%') {
|
|
|
|
if (argi < argc)
|
|
|
|
arg = argv[argi++];
|
|
|
|
else {
|
|
|
|
arg = "";
|
|
|
|
cooldown = 1;
|
|
|
|
}
|
2016-06-25 13:11:09 -04:00
|
|
|
} else {
|
2015-02-15 08:46:58 -05:00
|
|
|
putchar('%');
|
2016-06-25 13:11:09 -04:00
|
|
|
continue;
|
|
|
|
}
|
2015-02-15 08:46:58 -05:00
|
|
|
|
|
|
|
switch (format[i]) {
|
|
|
|
case 'b':
|
|
|
|
if ((tmp = strstr(arg, "\\c"))) {
|
|
|
|
*tmp = 0;
|
2015-02-16 04:30:19 -05:00
|
|
|
unescape(arg);
|
|
|
|
fputs(arg, stdout);
|
|
|
|
return 0;
|
2015-02-15 08:46:58 -05:00
|
|
|
}
|
|
|
|
unescape(arg);
|
|
|
|
fputs(arg, stdout);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
unescape(arg);
|
2015-03-11 05:50:18 -04:00
|
|
|
rarg = ereallocarray(NULL, utflen(arg) + 1, sizeof(*rarg));
|
2015-02-15 08:46:58 -05:00
|
|
|
utftorunestr(arg, rarg);
|
|
|
|
efputrune(rarg, stdout, "<stdout>");
|
|
|
|
free(rarg);
|
|
|
|
break;
|
|
|
|
case 's':
|
2015-03-26 18:00:02 -04:00
|
|
|
printf("%*.*s", width, precision, arg);
|
2015-02-15 08:46:58 -05:00
|
|
|
break;
|
|
|
|
case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
|
2016-02-29 09:35:13 -05:00
|
|
|
for (j = 0; isspace(arg[j]); j++);
|
2015-02-15 08:46:58 -05:00
|
|
|
if (arg[j] == '\'' || arg[j] == '\"') {
|
|
|
|
arg += j + 1;
|
|
|
|
unescape(arg);
|
2015-03-11 05:50:18 -04:00
|
|
|
rarg = ereallocarray(NULL, utflen(arg) + 1, sizeof(*rarg));
|
2015-02-15 08:46:58 -05:00
|
|
|
utftorunestr(arg, rarg);
|
|
|
|
num = rarg[0];
|
2016-02-29 11:34:34 -05:00
|
|
|
} else if (arg[0]) {
|
|
|
|
errno = 0;
|
|
|
|
if (format[i] == 'd' || format[i] == 'i')
|
|
|
|
num = strtol(arg, &tmp, 0);
|
|
|
|
else
|
|
|
|
num = strtoul(arg, &tmp, 0);
|
|
|
|
|
|
|
|
if (tmp == arg || *tmp != '\0') {
|
|
|
|
ret = 1;
|
|
|
|
weprintf("%%%c %s: conversion error\n",
|
|
|
|
format[i], arg);
|
|
|
|
}
|
|
|
|
if (errno == ERANGE) {
|
|
|
|
ret = 1;
|
|
|
|
weprintf("%%%c %s: out of range\n",
|
|
|
|
format[i], arg);
|
|
|
|
}
|
2015-06-21 09:52:21 -04:00
|
|
|
} else {
|
2016-02-29 11:34:34 -05:00
|
|
|
num = 0;
|
2015-06-21 09:52:21 -04:00
|
|
|
}
|
|
|
|
fmt = estrdup(flag ? "%#*.*ll#" : "%*.*ll#");
|
|
|
|
if (flag)
|
|
|
|
fmt[1] = flag;
|
|
|
|
fmt[flag ? 7 : 6] = format[i];
|
|
|
|
printf(fmt, width, precision, num);
|
2015-02-15 08:46:58 -05:00
|
|
|
free(fmt);
|
|
|
|
break;
|
|
|
|
case 'a': case 'A': case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
|
2015-06-21 09:52:21 -04:00
|
|
|
fmt = estrdup(flag ? "%#*.*#" : "%*.*#");
|
|
|
|
if (flag)
|
|
|
|
fmt[1] = flag;
|
|
|
|
fmt[flag ? 5 : 4] = format[i];
|
2015-02-15 08:46:58 -05:00
|
|
|
dou = (strlen(arg) > 0) ? estrtod(arg) : 0;
|
|
|
|
printf(fmt, width, precision, dou);
|
|
|
|
free(fmt);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf("Invalid format specifier '%c'.\n", format[i]);
|
2014-09-28 10:10:27 -04:00
|
|
|
}
|
2015-02-15 08:46:58 -05:00
|
|
|
if (argi >= argc)
|
|
|
|
cooldown = 1;
|
2014-09-28 10:10:27 -04:00
|
|
|
}
|
2015-03-17 16:43:01 -04:00
|
|
|
|
2016-02-29 11:34:34 -05:00
|
|
|
return fshut(stdout, "<stdout>") | ret;
|
2014-09-28 10:10:27 -04:00
|
|
|
}
|