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.15 1999/11/25 07:28:54 kris Exp $
79 * $DragonFly: src/usr.bin/chat/chat.c,v 1.2 2003/06/17 04:29:25 dillon Exp $
91 #include <sys/types.h>
122 #define __V(x) (va_alist) va_dcl
128 #define O_NONBLOCK O_NDELAY
133 extern char *sys_errlist[];
134 #define memmove(to, from, n) bcopy(from, to, n)
135 #define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
139 /*************** Micro getopt() *********************************************/
140 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
141 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
142 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
143 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
144 (_O=4,(char*)0):(char*)0)
145 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
146 #define ARG(c,v) (c?(--c,*v++):(char*)0)
148 static int _O = 0; /* Internal state */
149 /*************** Micro getopt() *********************************************/
151 #define MAX_ABORTS 50
152 #define MAX_REPORTS 50
153 #define DEFAULT_CHAT_TIMEOUT 45
163 FILE* report_fp = (FILE *) 0;
164 char *report_file = (char *) 0;
165 char *chat_file = (char *) 0;
166 char *phone_num = (char *) 0;
167 char *phone_num2 = (char *) 0;
168 int timeout = DEFAULT_CHAT_TIMEOUT;
170 int have_tty_parameters = 0;
173 #define term_parms struct termio
174 #define get_term_param(param) ioctl(0, TCGETA, param)
175 #define set_term_param(param) ioctl(0, TCSETA, param)
176 struct termio saved_tty_parameters;
180 #define term_parms struct termios
181 #define get_term_param(param) tcgetattr(0, param)
182 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
183 struct termios saved_tty_parameters;
186 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
188 int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
189 int clear_abort_next = 0;
191 char *report_string[MAX_REPORTS] ;
192 char report_buffer[50] ;
193 int n_reports = 0, report_next = 0, report_gathering = 0 ;
194 int clear_report_next = 0;
196 int say_next = 0, hup_next = 0;
198 void *dup_mem __P((void *b, size_t c));
199 void *copy_of __P((char *s));
200 static void usage __P((void));
201 void logf __P((const char *fmt, ...));
202 void fatal __P((int code, const char *fmt, ...));
203 SIGTYPE sigalrm __P((int signo));
204 SIGTYPE sigint __P((int signo));
205 SIGTYPE sigterm __P((int signo));
206 SIGTYPE sighup __P((int signo));
207 void unalarm __P((void));
208 void init __P((void));
209 void set_tty_parameters __P((void));
210 void echo_stderr __P((int));
211 void break_sequence __P((void));
212 void terminate __P((int status));
213 void do_file __P((char *chat_file));
214 int get_string __P((register char *string));
215 int put_string __P((register char *s));
216 int write_char __P((int c));
217 int put_char __P((int c));
218 int get_char __P((void));
219 void chat_send __P((register char *s));
220 char *character __P((int c));
221 void chat_expect __P((register char *s));
222 char *clean __P((register char *s, int sending));
223 void break_sequence __P((void));
224 void terminate __P((int status));
225 void pack_array __P((char **array, int end));
226 char *expect_strtok __P((char *, char *));
227 int vfmtmsg __P((char *, int, const char *, va_list)); /* vsprintf++ */
229 int main __P((int, char *[]));
235 void *ans = malloc (c);
237 fatal(2, "memory error!");
246 return dup_mem (s, strlen (s) + 1);
250 * chat [ -v ] [-T number] [-U number] [ -t timeout ] [ -f chat-file ] \
251 * [ -r report-file ] \
252 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
254 * Perform a UUCP-dialer-like chat script on stdin and stdout.
266 while ((option = OPTION(argc, argv)) != 0) {
289 if ((arg = OPTARG(argc, argv)) != NULL)
290 chat_file = copy_of(arg);
296 if ((arg = OPTARG(argc, argv)) != NULL)
303 arg = OPTARG (argc, argv);
305 if (report_fp != NULL)
307 report_file = copy_of (arg);
308 report_fp = fopen (report_file, "a");
309 if (report_fp != NULL) {
311 fprintf (report_fp, "Opening \"%s\"...\n",
319 if ((arg = OPTARG(argc, argv)) != NULL)
320 phone_num = copy_of(arg);
326 if ((arg = OPTARG(argc, argv)) != NULL)
327 phone_num2 = copy_of(arg);
338 * Default the report file to the stderr location
340 if (report_fp == NULL)
345 openlog("chat", LOG_PID);
347 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
350 setlogmask(LOG_UPTO(LOG_INFO));
352 setlogmask(LOG_UPTO(LOG_WARNING));
358 if (chat_file != NULL) {
359 arg = ARG(argc, argv);
365 while ((arg = ARG(argc, argv)) != NULL) {
368 if ((arg = ARG(argc, argv)) != NULL)
378 * Process a chat script when read from a file.
381 void do_file (chat_file)
385 char *sp, *arg, quote;
389 cfp = fopen (chat_file, "r");
391 fatal(1, "%s -- open failed: %m", chat_file);
396 while (fgets(buf, STR_LEN, cfp) != NULL) {
397 sp = strchr (buf, '\n');
404 /* lines starting with '#' are comments. If a real '#'
405 is to be expected, it should be quoted .... */
409 while (*sp != '\0') {
410 if (*sp == ' ' || *sp == '\t') {
415 if (*sp == '"' || *sp == '\'') {
418 while (*sp != quote) {
420 fatal(1, "unterminated quote (line %d)", linect);
430 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
448 * We got an error parsing the command line.
454 Usage: chat [-e] [-v] [-V] [-t timeout] [-r report-file] [-T phone-number]\n\
455 [-U phone-number2] {-f chat-file | chat-script}\n");
462 * Send a message to syslog and/or stderr.
464 void logf __V((const char *fmt, ...))
473 fmt = va_arg(args, char *);
476 vfmtmsg(line, sizeof(line), fmt, args);
478 syslog(LOG_INFO, "%s", line);
480 fprintf(stderr, "%s\n", line);
484 * Print an error message and terminate.
487 void fatal __V((int code, const char *fmt, ...))
497 code = va_arg(args, int);
498 fmt = va_arg(args, char *);
501 vfmtmsg(line, sizeof(line), fmt, args);
503 syslog(LOG_ERR, "%s", line);
505 fprintf(stderr, "%s\n", line);
511 SIGTYPE sigalrm(signo)
517 alarmed = 1; /* Reset alarm to avoid race window */
518 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
520 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
521 fatal(2, "Can't get file mode flags on stdin: %m");
523 if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
524 fatal(2, "Can't set file mode flags on stdin: %m");
534 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
535 fatal(2, "Can't get file mode flags on stdin: %m");
537 if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
538 fatal(2, "Can't set file mode flags on stdin: %m");
541 SIGTYPE sigint(signo)
547 SIGTYPE sigterm(signo)
553 SIGTYPE sighup(signo)
561 signal(SIGINT, sigint);
562 signal(SIGTERM, sigterm);
563 signal(SIGHUP, sighup);
565 set_tty_parameters();
566 signal(SIGALRM, sigalrm);
571 void set_tty_parameters()
573 #if defined(get_term_param)
576 if (get_term_param (&t) < 0)
577 fatal(2, "Can't get terminal parameters: %m");
579 saved_tty_parameters = t;
580 have_tty_parameters = 1;
582 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
590 if (set_term_param (&t) < 0)
591 fatal(2, "Can't set terminal parameters: %m");
595 void break_sequence()
602 void terminate(status)
606 if (report_file != (char *) 0 && report_fp != (FILE *) NULL) {
608 * Allow the last of the report string to be gathered before we terminate.
610 if (report_gathering) {
613 rep_len = strlen(report_buffer);
614 while (rep_len + 1 <= sizeof(report_buffer)) {
618 if (c < 0 || iscntrl(c))
620 report_buffer[rep_len] = c;
623 report_buffer[rep_len] = 0;
624 fprintf (report_fp, "chat: %s\n", report_buffer);
627 fprintf (report_fp, "Closing \"%s\".\n", report_file);
629 report_fp = (FILE *) NULL;
632 #if defined(get_term_param)
633 if (have_tty_parameters) {
634 if (set_term_param (&saved_tty_parameters) < 0)
635 fatal(2, "Can't restore terminal parameters: %m");
643 * 'Clean up' this string.
645 char *clean(s, sending)
647 int sending; /* set to 1 when sending (putting) this string. */
649 char temp[STR_LEN], cur_chr;
650 register char *s1, *phchar;
651 int add_return = sending;
652 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
655 /* Don't overflow buffer, leave room for chars we append later */
656 while (*s && s1 - temp < sizeof(temp) - 2 - add_return) {
658 if (cur_chr == '^') {
660 if (cur_chr == '\0') {
671 if (cur_chr != '\\') {
677 if (cur_chr == '\0') {
691 if (sending && *s == '\0')
708 if (sending && phone_num) {
709 for ( phchar = phone_num; *phchar != '\0'; phchar++)
719 if (sending && phone_num2) {
720 for ( phchar = phone_num2; *phchar != '\0'; phchar++)
759 if (isoctal (cur_chr)) {
763 cur_chr |= *s++ - '0';
766 cur_chr |= *s++ - '0';
770 if (cur_chr != 0 || sending) {
771 if (sending && (cur_chr == '\\' || cur_chr == 0))
788 *s1++ = '\0'; /* guarantee closure */
789 *s1++ = '\0'; /* terminate the string */
790 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
794 * A modified version of 'strtok'. This version skips \ sequences.
797 char *expect_strtok (s, term)
800 static char *str = "";
805 * If a string was specified then do initial processing.
811 * If this is the escape flag then reset it and ignore the character.
832 * If this is not in the termination string, continue.
834 if (strchr (term, *str) == (char *) 0) {
840 * This is the terminator. Mark the end of the string and stop.
849 * Process the expect string
858 if (strcmp(s, "HANGUP") == 0) {
863 if (strcmp(s, "ABORT") == 0) {
868 if (strcmp(s, "CLR_ABORT") == 0) {
873 if (strcmp(s, "REPORT") == 0) {
878 if (strcmp(s, "CLR_REPORT") == 0) {
883 if (strcmp(s, "TIMEOUT") == 0) {
888 if (strcmp(s, "ECHO") == 0) {
893 if (strcmp(s, "SAY") == 0) {
899 * Fetch the expect and reply string.
902 expect = expect_strtok (s, "-");
905 if (expect == (char *) 0)
908 reply = expect_strtok (s, "-");
911 * Handle the expect string. If successful then exit.
913 if (get_string (expect))
917 * If there is a sub-reply string then send it. Otherwise any condition
920 if (reply == (char *) 0 || exit_code != 3)
927 * The expectation did not occur. This is terminal.
930 logf("Failed (%s)", fail_reason);
933 terminate(exit_code);
937 * Translate the input character to the appropriate string for printing
944 static char string[10];
947 meta = (c & 0x80) ? "M-" : "";
951 sprintf(string, "%s^%c", meta, (int)c + '@');
953 sprintf(string, "%s^?", meta);
955 sprintf(string, "%s%c", meta, c);
961 * process the reply string
969 write(2, s, strlen(s));
976 if (strcmp(s, "OFF") == 0)
977 signal(SIGHUP, SIG_IGN);
979 signal(SIGHUP, sighup);
985 echo = (strcmp(s, "ON") == 0);
994 if (n_aborts >= MAX_ABORTS)
995 fatal(2, "Too many ABORT strings");
999 if (strlen(s1) > strlen(s)
1000 || strlen(s1) + 1 > sizeof(fail_buffer))
1001 fatal(1, "Illegal or too-long ABORT string ('%v')", s);
1003 abort_string[n_aborts++] = s1;
1006 logf("abort on (%v)", s);
1010 if (clear_abort_next) {
1016 clear_abort_next = 0;
1020 if (strlen(s1) > strlen(s)
1021 || strlen(s1) + 1 > sizeof(fail_buffer))
1022 fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s);
1025 for (i=0; i < n_aborts; i++) {
1026 if ( strcmp(s1,abort_string[i]) == 0 ) {
1027 free(abort_string[i]);
1028 abort_string[i] = NULL;
1032 logf("clear abort on (%v)", s);
1037 pack_array(abort_string,old_max);
1045 if (n_reports >= MAX_REPORTS)
1046 fatal(2, "Too many REPORT strings");
1050 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1051 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1053 report_string[n_reports++] = s1;
1056 logf("report (%v)", s);
1060 if (clear_report_next) {
1066 clear_report_next = 0;
1070 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1071 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1073 old_max = n_reports;
1074 for (i=0; i < n_reports; i++) {
1075 if ( strcmp(s1,report_string[i]) == 0 ) {
1076 free(report_string[i]);
1077 report_string[i] = NULL;
1081 logf("clear report (%v)", s);
1086 pack_array(report_string,old_max);
1096 timeout = DEFAULT_CHAT_TIMEOUT;
1099 logf("timeout set to %d seconds", timeout);
1104 if (strcmp(s, "EOT") == 0)
1106 else if (strcmp(s, "BREAK") == 0)
1118 status = read(0, &c, 1);
1122 return ((int)c & 0x7F);
1125 logf("warning: read() on stdin returned %d", status);
1128 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1129 fatal(2, "Can't get file mode flags on stdin: %m");
1131 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1132 fatal(2, "Can't set file mode flags on stdin: %m");
1144 usleep(10000); /* inter-character typing delay (?) */
1146 status = write(1, &ch, 1);
1153 logf("warning: write() on stdout returned %d", status);
1156 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1157 fatal(2, "Can't get file mode flags on stdin, %m");
1159 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1160 fatal(2, "Can't set file mode flags on stdin: %m");
1169 if (alarmed || put_char(c) < 0) {
1174 if (errno == EINTR || errno == EWOULDBLOCK)
1175 logf(" -- write timed out");
1177 logf(" -- write failed: %m");
1192 logf("send (??????)");
1194 logf("send (%v)", s);
1197 alarm(timeout); alarmed = 0;
1200 register char c = *s++;
1203 if (!write_char (c))
1219 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1223 if (!write_char (c))
1235 * Echo a character to stderr.
1236 * When called with -1, a '\n' character is generated when
1237 * the cursor is not at the beginning of a line.
1246 case '\r': /* ignore '\r' */
1258 write(2, s, strlen(s));
1265 * 'Wait for' this string to appear on this file descriptor.
1267 int get_string(string)
1268 register char *string;
1271 int c, printed = 0, len, minlen;
1272 register char *s = temp, *end = s + STR_LEN;
1273 char *logged = temp;
1275 fail_reason = (char *)0;
1277 if (strlen(string) > STR_LEN) {
1278 logf("expect string is too long");
1283 string = clean(string, 0);
1284 len = strlen(string);
1285 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1288 logf("expect (%v)", string);
1299 while ( ! alarmed && (c = get_char()) >= 0) {
1300 int n, abort_len, report_len;
1304 if (verbose && c == '\n') {
1306 logf(""); /* blank line */
1308 logf("%0.*v", s - logged, logged);
1314 if (verbose && s >= logged + 80) {
1315 logf("%0.*v", s - logged, logged);
1321 fputc( '\n', stderr );
1323 fprintf( stderr, "%s", character(c) );
1326 if (!report_gathering) {
1327 for (n = 0; n < n_reports; ++n) {
1328 if ((report_string[n] != (char*) NULL) &&
1329 s - temp >= (report_len = strlen(report_string[n])) &&
1330 strncmp(s - report_len, report_string[n], report_len) == 0) {
1331 time_t time_now = time ((time_t*) NULL);
1332 struct tm* tm_now = localtime (&time_now);
1334 strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1335 strcat (report_buffer, report_string[n]);
1337 report_string[n] = (char *) NULL;
1338 report_gathering = 1;
1345 int rep_len = strlen (report_buffer);
1346 report_buffer[rep_len] = c;
1347 report_buffer[rep_len + 1] = '\0';
1350 report_gathering = 0;
1351 fprintf (report_fp, "chat: %s\n", report_buffer);
1355 if (s - temp >= len &&
1356 c == string[len - 1] &&
1357 strncmp(s - len, string, len) == 0) {
1360 logf("%0.*v", s - logged, logged);
1361 logf(" -- got it\n");
1369 for (n = 0; n < n_aborts; ++n) {
1370 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1371 strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
1374 logf("%0.*v", s - logged, logged);
1381 strcpy(fail_reason = fail_buffer, abort_string[n]);
1387 if (logged < s - minlen) {
1388 logf("%0.*v", s - logged, logged);
1392 memmove(temp, s, minlen);
1393 logged = temp + (logged - s);
1397 if (alarmed && verbose)
1398 logf("warning: alarm synchronization problem");
1403 if (verbose && printed) {
1405 logf(" -- read timed out");
1407 logf(" -- read failed: %m");
1416 * Gross kludge to handle Solaris versions >= 2.6 having usleep.
1419 #include <sys/param.h>
1420 #if MAXUID > 65536 /* then this is Solaris 2.6 or later */
1426 #include <sys/types.h>
1427 #include <sys/time.h>
1430 usleep -- support routine for 4.2BSD system call emulations
1431 last edit: 29-Oct-1984 D A Gwyn
1434 extern int select();
1437 usleep( usec ) /* returns 0 if ok, else -1 */
1438 long usec; /* delay in microseconds */
1440 static struct { /* `timeval' */
1441 long tv_sec; /* seconds */
1442 long tv_usec; /* microsecs */
1443 } delay; /* _select() timeout */
1445 delay.tv_sec = usec / 1000000L;
1446 delay.tv_usec = usec % 1000000L;
1448 return select(0, (long *)0, (long *)0, (long *)0, &delay);
1453 pack_array (array, end)
1454 char **array; /* The address of the array of string pointers */
1455 int end; /* The index of the next free entry before CLR_ */
1459 for (i = 0; i < end; i++) {
1460 if (array[i] == NULL) {
1461 for (j = i+1; j < end; ++j)
1462 if (array[j] != NULL)
1463 array[i++] = array[j];
1464 for (; i < end; ++i)
1472 * vfmtmsg - format a message into a buffer. Like vsprintf except we
1473 * also specify the length of the output buffer, and we handle the
1474 * %m (error message) format.
1475 * Doesn't do floating-point formats.
1476 * Returns the number of chars put into buf.
1478 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
1481 vfmtmsg(buf, buflen, fmt, args)
1488 int width, prec, fillch;
1489 int base, len, neg, quoted;
1490 unsigned long val = 0;
1495 static char hexchars[] = "0123456789abcdef";
1499 while (buflen > 0) {
1500 for (f = fmt; *f != '%' && *f != 0; ++f)
1506 memcpy(buf, fmt, len);
1521 width = va_arg(args, int);
1524 while (isdigit(c)) {
1525 width = width * 10 + c - '0';
1532 prec = va_arg(args, int);
1535 while (isdigit(c)) {
1536 prec = prec * 10 + c - '0';
1547 i = va_arg(args, int);
1556 val = va_arg(args, unsigned int);
1560 val = va_arg(args, unsigned int);
1564 val = (unsigned long) va_arg(args, void *);
1569 str = va_arg(args, char *);
1572 num[0] = va_arg(args, int);
1577 str = strerror(errno);
1579 case 'v': /* "visible" string */
1580 case 'q': /* quoted string */
1582 p = va_arg(args, unsigned char *);
1583 if (fillch == '0' && prec > 0) {
1586 n = strlen((char *)p);
1587 if (prec > 0 && prec < n)
1590 while (n > 0 && buflen > 0) {
1593 if (!quoted && c >= 0x80) {
1598 if (quoted && (c == '"' || c == '\\'))
1600 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
1604 case '\t': OUTCHAR('t'); break;
1605 case '\n': OUTCHAR('n'); break;
1606 case '\b': OUTCHAR('b'); break;
1607 case '\f': OUTCHAR('f'); break;
1610 OUTCHAR(hexchars[c >> 4]);
1611 OUTCHAR(hexchars[c & 0xf]);
1628 --fmt; /* so %z outputs %z etc. */
1633 str = num + sizeof(num);
1635 while (str > num + neg) {
1636 *--str = hexchars[val % base];
1638 if (--prec <= 0 && val == 0)
1650 len = num + sizeof(num) - 1 - str;
1653 if (prec > 0 && len > prec)
1659 if ((n = width - len) > 0) {
1667 memcpy(buf, str, len);