annotate toys/pending/printf.c @ 1645:2ffee259f519 draft

Another cleanup pass on printf.
author Rob Landley <rob@landley.net>
date Sat, 03 Jan 2015 20:31:41 -0600
parents 54c092c3ee38
children 35c035b7e3e0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
1 /* printf.c - Format and Print the data.
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
2 *
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
3 * Copyright 2014 Sandeep Sharma <sandeep.jack2756@gmail.com>
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
4 * Copyright 2014 Kyungwan Han <asura321@gmail.com>
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
5 *
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
6 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
7
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
8 USE_PRINTF(NEWTOY(printf, "<1", TOYFLAG_USR|TOYFLAG_BIN))
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
9
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
10 config PRINTF
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
11 bool "printf"
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
12 default n
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
13 help
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
14 usage: printf FORMAT [ARGUMENT...]
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
15
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
16 Format and print ARGUMENT(s) according to FORMAT, using C printf syntax
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
17 (percent escapes for cdeEfgGiosuxX, slash escapes for \abefnrtv0 or
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
18 \0OCT or 0xHEX).
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
19 */
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
20
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
21 #define FOR_printf
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
22 #include "toys.h"
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
23
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
24 GLOBALS(
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
25 char *hv_w;
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
26 char *hv_p;
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
27 int encountered;
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
28 )
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
29
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
30 // Detect matching character (return true/valse) and advance pointer if match.
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
31 static int eat(char **s, char c)
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
32 {
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
33 int x = (**s == c);
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
34
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
35 if (x) ++*s;
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
36
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
37 return x;
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
38 }
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
39
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
40 // Add ll and L to Interger and floating point formats respectively.
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
41 static char *get_format(char *f)
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
42 {
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
43 int len = strlen(f);
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
44 char last = f[--len], *post = "";
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
45
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
46 f[len] = 0;
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
47 if (strchr("diouxX", last)) post = "ll"; // add ll to integer modifier.
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
48 else if (strchr("feEgG", last)) post = "L"; // add L to float modifier.
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
49 return xmprintf("%s%s%c", f, post, last);
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
50 }
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
51
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
52 // Print arguments with corresponding conversion and width and precision.
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
53 static void print(char *fmt, int w, int p)
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
54 {
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
55 char *ptr = fmt, *ep = 0, *format = 0, *arg = *toys.optargs;
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
56
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
57 errno = 0;
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
58 if (strchr("diouxX", *ptr)) {
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
59 long long val = 0;
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
60
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
61 if (arg) {
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
62 if (*arg == '\'' || *arg == '"') val = arg[1];
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
63 else {
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
64 val = strtoll(arg, &ep, 0);
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
65 if (errno || (ep && (*ep || ep == arg))) {
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
66 perror_msg("Invalid num %s", arg);
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
67 val = 0;
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
68 }
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
69 }
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
70 }
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
71 format = get_format(fmt);
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
72 TT.hv_w ? (TT.hv_p ? printf(format, w, p, val) : printf(format, w, val))
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
73 : (TT.hv_p ? printf(format, p, val) : printf(format, val));
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
74 } else if (strchr("gGeEf", *ptr)) {
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
75 long double dval = 0;
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
76
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
77 if (arg) {
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
78 dval = strtold(arg, &ep);
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
79 if (errno || (ep && (*ep || ep == arg))) {
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
80 perror_msg("Invalid num %s", arg);
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
81 dval = 0;
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
82 }
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
83 }
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
84 format = get_format(fmt);
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
85 TT.hv_w ? (TT.hv_p ? printf(format, w, p, dval) : printf(format, w, dval))
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
86 : (TT.hv_p ? printf(format, p, dval) : printf(format, dval));
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
87 } else if (*ptr == 's') {
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
88 char *str = arg;
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
89
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
90 if (!str) str = "";
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
91
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
92 TT.hv_w ? (TT.hv_p ? printf(fmt,w,p,str): printf(fmt, w, str))
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
93 : (TT.hv_p ? printf(fmt, p, str) : printf(fmt, str));
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
94 } else if (*ptr == 'c') printf(fmt, arg ? *arg : 0);
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
95
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
96 if (format) free(format);
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
97 }
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
98
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
99 // Parse escape sequences.
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
100 static int handle_slash(char **esc_val)
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
101 {
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
102 char *ptr = *esc_val;
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
103 int len = 1, base = 0;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
104 unsigned result = 0;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
105
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
106 if (*ptr == 'c') xexit();
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
107
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
108 // 0x12 hex escapes have 1-2 digits, \123 octal escapes have 1-3 digits.
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
109 if (eat(&ptr, 'x')) base = 16;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
110 else if (*ptr >= '0' && *ptr <= '8') base = 8;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
111 len += (base-8)/8;
1643
54c092c3ee38 Cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1256
diff changeset
112
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
113 // Not a hex or octal escape? (This catches trailing \)
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
114 if (!len) {
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
115 if (!(result = unescape(*ptr))) result = '\\';
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
116 else ++*esc_val;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
117
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
118 return result;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
119 }
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
120
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
121 while (len) {
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
122 unsigned num = tolower(*ptr)-'0';
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
123
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
124 if (num > 10) num += '0'-'a'+10;
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
125 if (num >= base) {
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
126 // Don't parse invalid hex value ala "\xvd", print it verbatim
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
127 if (base == 16 && len == 2) {
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
128 ptr--;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
129 result = '\\';
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
130 }
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
131 break;
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
132 }
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
133 result = (result*base)+num;
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
134 ptr++;
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
135 len--;
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
136 }
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
137 *esc_val = ptr;
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
138
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
139 return (char)result;
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
140 }
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
141
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
142 void printf_main(void)
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
143 {
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
144 char *format = *toys.optargs, **arg = toys.optargs+1, *f, *p;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
145
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
146 for (f = format; *f; f++) {
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
147 if (eat(&f, '\\')) putchar(handle_slash(&f));
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
148 else if (*f != '%' || *++f == '%') xputc(*f);
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
149 else if (*f == 'b')
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
150 for (p = *arg ? *(arg++) : ""; *p; p++)
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
151 putchar(eat(&p, '\\') ? handle_slash(&p) : *p);
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
152 else {
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
153 char *start = f;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
154 int wp[2], i;
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
155
1645
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
156 // todo: we currently ignore these?
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
157 if (strchr("-+# ", *f)) f++;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
158 memset(wp, 0, 8);
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
159 for (i=0; i<2; i++) {
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
160 if (eat(&f, '*')) {
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
161 if (*arg) wp[i] = atolx(*(arg++));
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
162 } else while (isdigit(*f)) f++;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
163 if (!eat(&f, '.')) break;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
164 }
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
165 if (!(p = strchr("diouxXfeEgGcs", *f)))
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
166 perror_exit("bad format@%ld", f-format);
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
167 else {
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
168 int len = f-start;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
169
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
170 TT.hv_p = strstr(start, ".*");
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
171 TT.hv_w = strchr(start, '*');
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
172 //pitfall: handle diff b/w * and .*
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
173 if ((TT.hv_w-1) == TT.hv_p) TT.hv_w = NULL;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
174 memcpy((p = xzalloc(len+1)), start, len);
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
175 print(p+len-1, wp[0], wp[1]);
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
176 if (*arg) arg++;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
177 free(p);
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
178 p = NULL;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
179 }
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
180 TT.encountered = 1;
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
181 }
2ffee259f519 Another cleanup pass on printf.
Rob Landley <rob@landley.net>
parents: 1643
diff changeset
182 }
1256
2fe64bc16a61 An implementation of __printf__ is attached.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
183 }