2 * Chat -- a program for automatic session establishment (i.e. dial
3 * the phone and log in).
5 * Standard termination codes:
6 * 0 - successful completion of the script
7 * 1 - invalid argument, expect string too large, etc.
8 * 2 - error on an I/O operation or fatal error condition.
9 * 3 - timeout waiting for a simple string.
10 * 4 - the first string declared as "ABORT"
11 * 5 - the second string declared as "ABORT"
12 * 6 - ... and so on for successive ABORT strings.
14 * This software is in the public domain.
17 * added -T and -U option and \T and \U substitution to pass a phone
18 * number into chat script. Two are needed for some ISDN TA applications.
19 * Keith Dart <kdart@cisco.com>
22 * Added SAY keyword to send output to stderr.
23 * This allows to turn ECHO OFF and to output specific, user selected,
24 * text to give progress messages. This best works when stderr
25 * exists (i.e.: pppd in nodetach mode).
27 * Added HANGUP directives to allow for us to be called
28 * back. When HANGUP is set to NO, chat will not hangup at HUP signal.
29 * We rely on timeouts in that case.
31 * Added CLR_ABORT to clear previously set ABORT string. This has been
32 * dictated by the HANGUP above as "NO CARRIER" (for example) must be
33 * an ABORT condition until we know the other host is going to close
34 * the connection for call back. As soon as we have completed the
35 * first stage of the call back sequence, "NO CARRIER" is a valid, non
36 * fatal string. As soon as we got called back (probably get "CONNECT"),
37 * we should re-arm the ABORT "NO CARRIER". Hence the CLR_ABORT command.
38 * Note that CLR_ABORT packs the abort_strings[] array so that we do not
39 * have unused entries not being reclaimed.
41 * In the same vein as above, added CLR_REPORT keyword.
43 * Allow for comments. Line starting with '#' are comments and are
44 * ignored. If a '#' is to be expected as the first character, the
45 * expect string must be quoted.
48 * Francis Demierre <Francis@SwissMail.Com>
49 * Thu May 15 17:15:40 MET DST 1997
52 * Added -r "report file" switch & REPORT keyword.
53 * Robert Geer <bgeer@xmission.com>
55 * Added -s "use stderr" and -S "don't use syslog" switches.
57 * Karl O. Pinc <kop@meme.com>
60 * Added -e "echo" switch & ECHO keyword
61 * Dick Streefland <dicks@tasking.nl>
64 * Considerable updates and modifications by
65 * Al Longyear <longyear@pobox.com>
66 * Paul Mackerras <paulus@cs.anu.edu.au>
69 * The original author is:
71 * Karl Fox <karl@MorningStar.Com>
72 * Morning Star Technologies, Inc.
78 * $FreeBSD: src/usr.bin/chat/chat.c,v 1.19 2002/08/25 13:22:33 charnier Exp $
79 * $DragonFly: src/usr.bin/chat/chat.c,v 1.7 2004/12/31 19:53:51 cpressey Exp $
91 #include <sys/types.h>
120 #define __V(x) (va_alist) va_dcl
125 #define O_NONBLOCK O_NDELAY
130 extern char *sys_errlist[];
131 #define memmove(to, from, n) bcopy(from, to, n)
132 #define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
136 /*************** Micro getopt() *********************************************/
137 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
138 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
139 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
140 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
141 (_O=4,(char*)0):(char*)0)
142 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
143 #define ARG(c,v) (c?(--c,*v++):(char*)0)
145 static int _O = 0; /* Internal state */
146 /*************** Micro getopt() *********************************************/
148 #define MAX_ABORTS 50
149 #define MAX_REPORTS 50
150 #define DEFAULT_CHAT_TIMEOUT 45
160 FILE* report_fp = (FILE *) 0;
161 char *report_file = (char *) 0;
162 char *chat_file = (char *) 0;
163 char *phone_num = (char *) 0;
164 char *phone_num2 = (char *) 0;
165 int timeout = DEFAULT_CHAT_TIMEOUT;
167 int have_tty_parameters = 0;
170 #define term_parms struct termio
171 #define get_term_param(param) ioctl(0, TCGETA, param)
172 #define set_term_param(param) ioctl(0, TCSETA, param)
173 struct termio saved_tty_parameters;
177 #define term_parms struct termios
178 #define get_term_param(param) tcgetattr(0, param)
179 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
180 struct termios saved_tty_parameters;
183 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
185 int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
186 int clear_abort_next = 0;
188 char *report_string[MAX_REPORTS] ;
189 char report_buffer[50] ;
190 int n_reports = 0, report_next = 0, report_gathering = 0 ;
191 int clear_report_next = 0;
193 int say_next = 0, hup_next = 0;
195 void *dup_mem(void *b, size_t c);
196 void *copy_of(char *s);
197 static void usage(void);
198 void logf(const char *fmt, ...);
199 void fatal(int code, const char *fmt, ...);
200 SIGTYPE sigalrm(int signo);
201 SIGTYPE sigint(int signo);
202 SIGTYPE sigterm(int signo);
203 SIGTYPE sighup(int signo);
206 void set_tty_parameters(void);
207 void echo_stderr(int);
208 void break_sequence(void);
209 void terminate(int status);
210 void do_file(char *chat_file);
211 int get_string(register char *string);
212 int put_string(register char *s);
213 int write_char(int c);
216 void chat_send(register char *s);
217 char *character(int c);
218 void chat_expect(register char *s);
219 char *clean(register char *s, int sending);
220 void break_sequence(void);
221 void terminate(int status);
222 void pack_array(char **array, int end);
223 char *expect_strtok(char *, char *);
224 int vfmtmsg(char *, int, const char *, va_list); /* vsprintf++ */
230 void *ans = malloc (c);
232 fatal(2, "memory error!");
238 void *copy_of (char *s)
240 return dup_mem (s, strlen (s) + 1);
244 * chat [ -v ] [-T number] [-U number] [ -t timeout ] [ -f chat-file ] \
245 * [ -r report-file ] \
246 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
248 * Perform a UUCP-dialer-like chat script on stdin and stdout.
251 main(int argc, char **argv)
258 while ((option = OPTION(argc, argv)) != 0) {
281 if ((arg = OPTARG(argc, argv)) != NULL)
282 chat_file = copy_of(arg);
288 if ((arg = OPTARG(argc, argv)) != NULL)
295 arg = OPTARG (argc, argv);
297 if (report_fp != NULL)
299 report_file = copy_of (arg);
300 report_fp = fopen (report_file, "a");
301 if (report_fp != NULL) {
303 fprintf (report_fp, "Opening \"%s\"...\n",
311 if ((arg = OPTARG(argc, argv)) != NULL)
312 phone_num = copy_of(arg);
318 if ((arg = OPTARG(argc, argv)) != NULL)
319 phone_num2 = copy_of(arg);
330 * Default the report file to the stderr location
332 if (report_fp == NULL)
337 openlog("chat", LOG_PID);
339 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
342 setlogmask(LOG_UPTO(LOG_INFO));
344 setlogmask(LOG_UPTO(LOG_WARNING));
350 if (chat_file != NULL) {
351 arg = ARG(argc, argv);
357 while ((arg = ARG(argc, argv)) != NULL) {
360 if ((arg = ARG(argc, argv)) != NULL)
370 * Process a chat script when read from a file.
373 void do_file (char *chat_file)
376 char *sp, *arg, quote;
380 cfp = fopen (chat_file, "r");
382 fatal(1, "%s -- open failed: %m", chat_file);
387 while (fgets(buf, STR_LEN, cfp) != NULL) {
388 sp = strchr (buf, '\n');
395 /* lines starting with '#' are comments. If a real '#'
396 is to be expected, it should be quoted .... */
400 while (*sp != '\0') {
401 if (*sp == ' ' || *sp == '\t') {
406 if (*sp == '"' || *sp == '\'') {
409 while (*sp != quote) {
411 fatal(1, "unterminated quote (line %d)", linect);
421 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
439 * We got an error parsing the command line.
445 Usage: chat [-e] [-v] [-V] [-t timeout] [-r report-file] [-T phone-number]\n\
446 [-U phone-number2] {-f chat-file | chat-script}\n");
453 * Send a message to syslog and/or stderr.
455 void logf __V((const char *fmt, ...))
461 vfmtmsg(line, sizeof(line), fmt, args);
463 syslog(LOG_INFO, "%s", line);
465 fprintf(stderr, "%s\n", line);
469 * Print an error message and terminate.
472 void fatal __V((int code, const char *fmt, ...))
478 vfmtmsg(line, sizeof(line), fmt, args);
480 syslog(LOG_ERR, "%s", line);
482 fprintf(stderr, "%s\n", line);
488 SIGTYPE sigalrm(int signo)
493 alarmed = 1; /* Reset alarm to avoid race window */
494 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
496 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
497 fatal(2, "Can't get file mode flags on stdin: %m");
499 if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
500 fatal(2, "Can't set file mode flags on stdin: %m");
510 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
511 fatal(2, "Can't get file mode flags on stdin: %m");
513 if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
514 fatal(2, "Can't set file mode flags on stdin: %m");
517 SIGTYPE sigint(int signo)
522 SIGTYPE sigterm(int signo)
527 SIGTYPE sighup(int signo)
534 signal(SIGINT, sigint);
535 signal(SIGTERM, sigterm);
536 signal(SIGHUP, sighup);
538 set_tty_parameters();
539 signal(SIGALRM, sigalrm);
544 void set_tty_parameters(void)
546 #if defined(get_term_param)
549 if (get_term_param (&t) < 0)
550 fatal(2, "Can't get terminal parameters: %m");
552 saved_tty_parameters = t;
553 have_tty_parameters = 1;
555 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
563 if (set_term_param (&t) < 0)
564 fatal(2, "Can't set terminal parameters: %m");
568 void break_sequence(void)
575 void terminate(int status)
578 if (report_file != (char *) 0 && report_fp != (FILE *) NULL) {
580 * Allow the last of the report string to be gathered before we terminate.
582 if (report_gathering) {
585 rep_len = strlen(report_buffer);
586 while (rep_len + 1 <= sizeof(report_buffer)) {
590 if (c < 0 || iscntrl(c))
592 report_buffer[rep_len] = c;
595 report_buffer[rep_len] = 0;
596 fprintf (report_fp, "chat: %s\n", report_buffer);
599 fprintf (report_fp, "Closing \"%s\".\n", report_file);
601 report_fp = (FILE *) NULL;
604 #if defined(get_term_param)
605 if (have_tty_parameters) {
606 if (set_term_param (&saved_tty_parameters) < 0)
607 fatal(2, "Can't restore terminal parameters: %m");
615 * 'Clean up' this string.
617 /* sending: set to 1 when sending (putting) this string. */
618 char *clean(register char *s, int sending)
620 char temp[STR_LEN], cur_chr;
621 register char *s1, *phchar;
622 int add_return = sending;
623 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
626 /* Don't overflow buffer, leave room for chars we append later */
627 while (*s && s1 - temp < sizeof(temp) - 2 - add_return) {
629 if (cur_chr == '^') {
631 if (cur_chr == '\0') {
642 if (cur_chr != '\\') {
648 if (cur_chr == '\0') {
662 if (sending && *s == '\0')
679 if (sending && phone_num) {
680 for ( phchar = phone_num; *phchar != '\0'; phchar++)
690 if (sending && phone_num2) {
691 for ( phchar = phone_num2; *phchar != '\0'; phchar++)
730 if (isoctal (cur_chr)) {
734 cur_chr |= *s++ - '0';
737 cur_chr |= *s++ - '0';
741 if (cur_chr != 0 || sending) {
742 if (sending && (cur_chr == '\\' || cur_chr == 0))
759 *s1++ = '\0'; /* guarantee closure */
760 *s1++ = '\0'; /* terminate the string */
761 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
765 * A modified version of 'strtok'. This version skips \ sequences.
768 char *expect_strtok (char *s, char *term)
770 static char *str = "";
775 * If a string was specified then do initial processing.
781 * If this is the escape flag then reset it and ignore the character.
802 * If this is not in the termination string, continue.
804 if (strchr (term, *str) == (char *) 0) {
810 * This is the terminator. Mark the end of the string and stop.
819 * Process the expect string
822 void chat_expect (char *s)
827 if (strcmp(s, "HANGUP") == 0) {
832 if (strcmp(s, "ABORT") == 0) {
837 if (strcmp(s, "CLR_ABORT") == 0) {
842 if (strcmp(s, "REPORT") == 0) {
847 if (strcmp(s, "CLR_REPORT") == 0) {
852 if (strcmp(s, "TIMEOUT") == 0) {
857 if (strcmp(s, "ECHO") == 0) {
862 if (strcmp(s, "SAY") == 0) {
868 * Fetch the expect and reply string.
871 expect = expect_strtok (s, "-");
874 if (expect == (char *) 0)
877 reply = expect_strtok (s, "-");
880 * Handle the expect string. If successful then exit.
882 if (get_string (expect))
886 * If there is a sub-reply string then send it. Otherwise any condition
889 if (reply == (char *) 0 || exit_code != 3)
896 * The expectation did not occur. This is terminal.
899 logf("Failed (%s)", fail_reason);
902 terminate(exit_code);
906 * Translate the input character to the appropriate string for printing
910 char *character(int c)
912 static char string[10];
915 meta = (c & 0x80) ? "M-" : "";
919 sprintf(string, "%s^%c", meta, (int)c + '@');
921 sprintf(string, "%s^?", meta);
923 sprintf(string, "%s%c", meta, c);
929 * process the reply string
931 void chat_send (register char *s)
936 write(STDERR_FILENO, s, strlen(s));
943 if (strcmp(s, "OFF") == 0)
944 signal(SIGHUP, SIG_IGN);
946 signal(SIGHUP, sighup);
952 echo = (strcmp(s, "ON") == 0);
961 if (n_aborts >= MAX_ABORTS)
962 fatal(2, "Too many ABORT strings");
966 if (strlen(s1) > strlen(s)
967 || strlen(s1) + 1 > sizeof(fail_buffer))
968 fatal(1, "Illegal or too-long ABORT string ('%v')", s);
970 abort_string[n_aborts++] = s1;
973 logf("abort on (%v)", s);
977 if (clear_abort_next) {
983 clear_abort_next = 0;
987 if (strlen(s1) > strlen(s)
988 || strlen(s1) + 1 > sizeof(fail_buffer))
989 fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s);
992 for (i=0; i < n_aborts; i++) {
993 if ( strcmp(s1,abort_string[i]) == 0 ) {
994 free(abort_string[i]);
995 abort_string[i] = NULL;
999 logf("clear abort on (%v)", s);
1004 pack_array(abort_string,old_max);
1012 if (n_reports >= MAX_REPORTS)
1013 fatal(2, "Too many REPORT strings");
1017 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1018 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1020 report_string[n_reports++] = s1;
1023 logf("report (%v)", s);
1027 if (clear_report_next) {
1033 clear_report_next = 0;
1037 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1038 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1040 old_max = n_reports;
1041 for (i=0; i < n_reports; i++) {
1042 if ( strcmp(s1,report_string[i]) == 0 ) {
1043 free(report_string[i]);
1044 report_string[i] = NULL;
1048 logf("clear report (%v)", s);
1053 pack_array(report_string,old_max);
1063 timeout = DEFAULT_CHAT_TIMEOUT;
1066 logf("timeout set to %d seconds", timeout);
1071 if (strcmp(s, "EOT") == 0)
1073 else if (strcmp(s, "BREAK") == 0)
1085 status = read(STDIN_FILENO, &c, 1);
1089 return ((int)c & 0x7F);
1092 logf("warning: read() on stdin returned %d", status);
1095 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1096 fatal(2, "Can't get file mode flags on stdin: %m");
1098 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1099 fatal(2, "Can't set file mode flags on stdin: %m");
1110 usleep(10000); /* inter-character typing delay (?) */
1112 status = write(STDOUT_FILENO, &ch, 1);
1119 logf("warning: write() on stdout returned %d", status);
1122 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1123 fatal(2, "Can't get file mode flags on stdin, %m");
1125 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1126 fatal(2, "Can't set file mode flags on stdin: %m");
1132 int write_char (int c)
1134 if (alarmed || put_char(c) < 0) {
1139 if (errno == EINTR || errno == EWOULDBLOCK)
1140 logf(" -- write timed out");
1142 logf(" -- write failed: %m");
1149 int put_string (register char *s)
1156 logf("send (??????)");
1158 logf("send (%v)", s);
1161 alarm(timeout); alarmed = 0;
1164 register char c = *s++;
1167 if (!write_char (c))
1183 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1187 if (!write_char (c))
1199 * Echo a character to stderr.
1200 * When called with -1, a '\n' character is generated when
1201 * the cursor is not at the beginning of a line.
1203 void echo_stderr(int n)
1209 case '\r': /* ignore '\r' */
1216 write(STDERR_FILENO, "\n", 1);
1221 write(STDERR_FILENO, s, strlen(s));
1228 * 'Wait for' this string to appear on this file descriptor.
1230 int get_string(register char *string)
1233 int c, printed = 0, len, minlen;
1234 register char *s = temp, *end = s + STR_LEN;
1235 char *logged = temp;
1237 fail_reason = (char *)0;
1239 if (strlen(string) > STR_LEN) {
1240 logf("expect string is too long");
1245 string = clean(string, 0);
1246 len = strlen(string);
1247 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1250 logf("expect (%v)", string);
1261 while ( ! alarmed && (c = get_char()) >= 0) {
1262 int n, abort_len, report_len;
1266 if (verbose && c == '\n') {
1268 logf(""); /* blank line */
1270 logf("%0.*v", s - logged, logged);
1276 if (verbose && s >= logged + 80) {
1277 logf("%0.*v", s - logged, logged);
1283 fputc( '\n', stderr );
1285 fprintf( stderr, "%s", character(c) );
1288 if (!report_gathering) {
1289 for (n = 0; n < n_reports; ++n) {
1290 if ((report_string[n] != (char*) NULL) &&
1291 s - temp >= (report_len = strlen(report_string[n])) &&
1292 strncmp(s - report_len, report_string[n], report_len) == 0) {
1293 time_t time_now = time ((time_t*) NULL);
1294 struct tm* tm_now = localtime (&time_now);
1296 strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1297 strcat (report_buffer, report_string[n]);
1299 report_string[n] = (char *) NULL;
1300 report_gathering = 1;
1307 int rep_len = strlen (report_buffer);
1308 report_buffer[rep_len] = c;
1309 report_buffer[rep_len + 1] = '\0';
1312 report_gathering = 0;
1313 fprintf (report_fp, "chat: %s\n", report_buffer);
1317 if (s - temp >= len &&
1318 c == string[len - 1] &&
1319 strncmp(s - len, string, len) == 0) {
1322 logf("%0.*v", s - logged, logged);
1323 logf(" -- got it\n");
1331 for (n = 0; n < n_aborts; ++n) {
1332 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1333 strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
1336 logf("%0.*v", s - logged, logged);
1343 strcpy(fail_reason = fail_buffer, abort_string[n]);
1349 if (logged < s - minlen) {
1350 logf("%0.*v", s - logged, logged);
1354 memmove(temp, s, minlen);
1355 logged = temp + (logged - s);
1359 if (alarmed && verbose)
1360 logf("warning: alarm synchronization problem");
1365 if (verbose && printed) {
1367 logf(" -- read timed out");
1369 logf(" -- read failed: %m");
1378 * Gross kludge to handle Solaris versions >= 2.6 having usleep.
1381 #include <sys/param.h>
1382 #if MAXUID > 65536 /* then this is Solaris 2.6 or later */
1388 #include <sys/types.h>
1389 #include <sys/time.h>
1392 usleep -- support routine for 4.2BSD system call emulations
1393 last edit: 29-Oct-1984 D A Gwyn
1396 extern int select();
1398 /* usec: delay in microseconds */
1400 usleep( long usec ) /* returns 0 if ok, else -1 */
1402 static struct { /* `timeval' */
1403 long tv_sec; /* seconds */
1404 long tv_usec; /* microsecs */
1405 } delay; /* _select() timeout */
1407 delay.tv_sec = usec / 1000000L;
1408 delay.tv_usec = usec % 1000000L;
1410 return select(0, (long *)0, (long *)0, (long *)0, &delay);
1415 pack_array (char **array, int end)
1419 for (i = 0; i < end; i++) {
1420 if (array[i] == NULL) {
1421 for (j = i+1; j < end; ++j)
1422 if (array[j] != NULL)
1423 array[i++] = array[j];
1424 for (; i < end; ++i)
1432 * vfmtmsg - format a message into a buffer. Like vsprintf except we
1433 * also specify the length of the output buffer, and we handle the
1434 * %m (error message) format.
1435 * Doesn't do floating-point formats.
1436 * Returns the number of chars put into buf.
1438 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
1441 vfmtmsg(char *buf, int buflen, const char *fmt, va_list args)
1444 int width, prec, fillch;
1445 int base, len, neg, quoted;
1446 unsigned long val = 0;
1451 static char hexchars[] = "0123456789abcdef";
1455 while (buflen > 0) {
1456 for (f = fmt; *f != '%' && *f != 0; ++f)
1462 memcpy(buf, fmt, len);
1477 width = va_arg(args, int);
1480 while (isdigit(c)) {
1481 width = width * 10 + c - '0';
1488 prec = va_arg(args, int);
1491 while (isdigit(c)) {
1492 prec = prec * 10 + c - '0';
1503 i = va_arg(args, int);
1512 val = va_arg(args, unsigned int);
1516 val = va_arg(args, unsigned int);
1520 val = (unsigned long) va_arg(args, void *);
1525 str = va_arg(args, char *);
1528 num[0] = va_arg(args, int);
1533 str = strerror(errno);
1535 case 'v': /* "visible" string */
1536 case 'q': /* quoted string */
1538 p = va_arg(args, unsigned char *);
1539 if (fillch == '0' && prec > 0) {
1542 n = strlen((char *)p);
1543 if (prec > 0 && prec < n)
1546 while (n > 0 && buflen > 0) {
1549 if (!quoted && c >= 0x80) {
1554 if (quoted && (c == '"' || c == '\\'))
1556 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
1560 case '\t': OUTCHAR('t'); break;
1561 case '\n': OUTCHAR('n'); break;
1562 case '\b': OUTCHAR('b'); break;
1563 case '\f': OUTCHAR('f'); break;
1566 OUTCHAR(hexchars[c >> 4]);
1567 OUTCHAR(hexchars[c & 0xf]);
1584 --fmt; /* so %z outputs %z etc. */
1589 str = num + sizeof(num);
1591 while (str > num + neg) {
1592 *--str = hexchars[val % base];
1594 if (--prec <= 0 && val == 0)
1606 len = num + sizeof(num) - 1 - str;
1609 if (prec > 0 && len > prec)
1615 if ((n = width - len) > 0) {
1623 memcpy(buf, str, len);