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 * 22-May-99 added environment substitutuion, enabled with -E switch.
18 * Andreas Arens <andras@cityweb.de>.
20 * 12-May-99 added a feature to read data to be sent from a file,
21 * if the send string starts with @. Idea from gpk <gpk@onramp.net>.
23 * added -T and -U option and \T and \U substitution to pass a phone
24 * number into chat script. Two are needed for some ISDN TA applications.
25 * Keith Dart <kdart@cisco.com>
28 * Added SAY keyword to send output to stderr.
29 * This allows to turn ECHO OFF and to output specific, user selected,
30 * text to give progress messages. This best works when stderr
31 * exists (i.e.: pppd in nodetach mode).
33 * Added HANGUP directives to allow for us to be called
34 * back. When HANGUP is set to NO, chat will not hangup at HUP signal.
35 * We rely on timeouts in that case.
37 * Added CLR_ABORT to clear previously set ABORT string. This has been
38 * dictated by the HANGUP above as "NO CARRIER" (for example) must be
39 * an ABORT condition until we know the other host is going to close
40 * the connection for call back. As soon as we have completed the
41 * first stage of the call back sequence, "NO CARRIER" is a valid, non
42 * fatal string. As soon as we got called back (probably get "CONNECT"),
43 * we should re-arm the ABORT "NO CARRIER". Hence the CLR_ABORT command.
44 * Note that CLR_ABORT packs the abort_strings[] array so that we do not
45 * have unused entries not being reclaimed.
47 * In the same vein as above, added CLR_REPORT keyword.
49 * Allow for comments. Line starting with '#' are comments and are
50 * ignored. If a '#' is to be expected as the first character, the
51 * expect string must be quoted.
54 * Francis Demierre <Francis@SwissMail.Com>
55 * Thu May 15 17:15:40 MET DST 1997
58 * Added -r "report file" switch & REPORT keyword.
59 * Robert Geer <bgeer@xmission.com>
61 * Added -s "use stderr" and -S "don't use syslog" switches.
63 * Karl O. Pinc <kop@meme.com>
66 * Added -e "echo" switch & ECHO keyword
67 * Dick Streefland <dicks@tasking.nl>
70 * Considerable updates and modifications by
71 * Al Longyear <longyear@pobox.com>
72 * Paul Mackerras <paulus@cs.anu.edu.au>
75 * The original author is:
77 * Karl Fox <karl@MorningStar.Com>
78 * Morning Star Technologies, Inc.
94 #include <sys/types.h>
118 #define O_NONBLOCK O_NDELAY
123 extern char *sys_errlist[];
124 #define memmove(to, from, n) bcopy(from, to, n)
125 #define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
129 /*************** Micro getopt() *********************************************/
130 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
131 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
132 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
133 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
134 (_O=4,(char*)0):(char*)0)
135 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
136 #define ARG(c,v) (c?(--c,*v++):(char*)0)
138 static int _O = 0; /* Internal state */
139 /*************** Micro getopt() *********************************************/
143 #define MAX_ABORTS 50
144 #define MAX_REPORTS 50
145 #define DEFAULT_CHAT_TIMEOUT 45
156 FILE* report_fp = (FILE *) 0;
157 char *report_file = (char *) 0;
158 char *chat_file = (char *) 0;
159 char *phone_num = (char *) 0;
160 char *phone_num2 = (char *) 0;
161 int timeout = DEFAULT_CHAT_TIMEOUT;
163 int have_tty_parameters = 0;
166 #define term_parms struct termio
167 #define get_term_param(param) ioctl(0, TCGETA, param)
168 #define set_term_param(param) ioctl(0, TCSETA, param)
169 struct termio saved_tty_parameters;
173 #define term_parms struct termios
174 #define get_term_param(param) tcgetattr(0, param)
175 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
176 struct termios saved_tty_parameters;
179 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
181 int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
182 int clear_abort_next = 0;
184 char *report_string[MAX_REPORTS] ;
185 char report_buffer[4096] ;
186 int n_reports = 0, report_next = 0, report_gathering = 0 ;
187 int clear_report_next = 0;
189 int say_next = 0, hup_next = 0;
191 void *dup_mem (void *b, size_t c);
192 void *copy_of (char *s);
193 char *grow (char *s, char **p, size_t len);
195 void msgf (const char *fmt, ...);
196 void fatal (int code, const char *fmt, ...);
197 SIGTYPE sigalrm (int signo);
198 SIGTYPE sigint (int signo);
199 SIGTYPE sigterm (int signo);
200 SIGTYPE sighup (int signo);
201 void checksigs(void);
203 void set_tty_parameters (void);
204 int echo_stderr (int);
205 void break_sequence (void);
206 void terminate (int status);
207 void do_file (char *chat_file);
208 int get_string (register char *string);
209 int put_string (register char *s);
210 int write_char (int c);
211 int put_char (int c);
213 int chat_send (register char *s);
214 char *character (int c);
215 void chat_expect (register char *s);
216 char *clean (register char *s, int sending);
217 void break_sequence (void);
218 void pack_array (char **array, int end);
219 char *expect_strtok (char *, char *);
220 int vfmtmsg (char *, int, const char *, va_list); /* vsprintf++ */
222 int main (int, char *[]);
224 void *dup_mem(void *b, size_t c)
226 void *ans = malloc (c);
228 fatal(2, "memory error!");
234 void *copy_of (char *s)
236 return dup_mem (s, strlen (s) + 1);
239 /* grow a char buffer and keep a pointer offset */
240 char *grow(char *s, char **p, size_t len)
242 size_t l = *p - s; /* save p as distance into s */
246 fatal(2, "memory error!");
247 *p = s + l; /* restore p */
252 * chat [ -v ] [ -E ] [ -T number ] [ -U number ] [ -t timeout ] [ -f chat-file ] \
253 * [ -r report-file ] \
254 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
256 * Perform a UUCP-dialer-like chat script on stdin and stdout.
259 main(int argc, char **argv)
264 program_name = *argv;
267 while ((option = OPTION(argc, argv)) != 0) {
294 if ((arg = OPTARG(argc, argv)) != NULL)
295 chat_file = copy_of(arg);
301 if ((arg = OPTARG(argc, argv)) != NULL)
308 arg = OPTARG (argc, argv);
310 if (report_fp != NULL)
312 report_file = copy_of (arg);
313 report_fp = fopen (report_file, "a");
314 if (report_fp != NULL) {
316 fprintf (report_fp, "Opening \"%s\"...\n",
324 if ((arg = OPTARG(argc, argv)) != NULL)
325 phone_num = copy_of(arg);
331 if ((arg = OPTARG(argc, argv)) != NULL)
332 phone_num2 = copy_of(arg);
343 * Default the report file to the stderr location
345 if (report_fp == NULL)
349 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
352 setlogmask(LOG_UPTO(LOG_INFO));
354 setlogmask(LOG_UPTO(LOG_WARNING));
359 if (chat_file != NULL) {
360 arg = ARG(argc, argv);
366 while ((arg = ARG(argc, argv)) != NULL) {
369 if ((arg = ARG(argc, argv)) != NULL)
380 * Process a chat script when read from a file.
383 void do_file (char *chat_file)
386 char *sp, *arg, quote;
390 cfp = fopen (chat_file, "r");
392 fatal(1, "%s -- open failed: %m", chat_file);
397 while (fgets(buf, STR_LEN, cfp) != NULL) {
398 sp = strchr (buf, '\n');
405 /* lines starting with '#' are comments. If a real '#'
406 is to be expected, it should be quoted .... */
410 while (*sp != '\0') {
411 if (*sp == ' ' || *sp == '\t') {
416 if (*sp == '"' || *sp == '\'') {
419 while (*sp != quote) {
421 fatal(1, "unterminated quote (line %d)", linect);
431 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
451 * We got an error parsing the command line.
456 Usage: %s [-e] [-E] [-v] [-V] [-t timeout] [-r report-file]\n\
457 [-T phone-number] [-U phone-number2] {-f chat-file | chat-script}\n", program_name);
464 * Send a message to syslog and/or stderr.
466 void msgf(const char *fmt, ...)
472 vfmtmsg(line, sizeof(line), fmt, args);
474 syslog(LOG_INFO, "%s", line);
476 fprintf(stderr, "%s\n", line);
481 * Print an error message and terminate.
484 void fatal(int code, const char *fmt, ...)
490 vfmtmsg(line, sizeof(line), fmt, args);
492 syslog(LOG_ERR, "%s", line);
494 fprintf(stderr, "%s\n", line);
502 SIGTYPE sigalrm(int signo)
511 const char *fatalsig = NULL;
513 SIGTYPE sigint(int signo)
518 SIGTYPE sigterm(int signo)
520 fatalsig = "SIGTERM";
523 SIGTYPE sighup(int signo)
539 if (alarmsig && verbose) {
551 memset(&sa, 0, sizeof(sa));
552 sa.sa_handler = sigint;
553 sigaction(SIGINT, &sa, NULL);
554 sa.sa_handler = sigterm;
555 sigaction(SIGTERM, &sa, NULL);
556 sa.sa_handler = sighup;
557 sigaction(SIGHUP, &sa, NULL);
559 set_tty_parameters();
560 sa.sa_handler = sigalrm;
561 sigaction(SIGALRM, &sa, NULL);
566 void set_tty_parameters(void)
568 #if defined(get_term_param)
571 if (get_term_param (&t) < 0)
572 fatal(2, "Can't get terminal parameters: %m");
574 saved_tty_parameters = t;
575 have_tty_parameters = 1;
577 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
585 if (set_term_param (&t) < 0)
586 fatal(2, "Can't set terminal parameters: %m");
590 void break_sequence(void)
597 void terminate(int status)
599 static int terminating = 0;
606 * Allow the last of the report string to be gathered before we terminate.
608 if (report_gathering) {
611 rep_len = strlen(report_buffer);
612 while (rep_len + 1 <= sizeof(report_buffer)) {
616 if (c < 0 || iscntrl(c))
618 report_buffer[rep_len] = c;
621 report_buffer[rep_len] = 0;
622 fprintf (report_fp, "chat: %s\n", report_buffer);
624 if (report_file != (char *) 0 && report_fp != (FILE *) NULL) {
626 fprintf (report_fp, "Closing \"%s\".\n", report_file);
628 report_fp = (FILE *) NULL;
631 #if defined(get_term_param)
632 if (have_tty_parameters) {
633 if (set_term_param (&saved_tty_parameters) < 0)
634 fatal(2, "Can't restore terminal parameters: %m");
642 * 'Clean up' this string.
644 char *clean(register char *s,
645 int sending) /* set to 1 when sending (putting) this string. */
648 char *s1, *p, *phchar;
649 int add_return = sending;
650 size_t len = strlen(s) + 3; /* see len comments below */
652 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
653 #define isalnumx(chr) ((((chr) >= '0') && ((chr) <= '9')) \
654 || (((chr) >= 'a') && ((chr) <= 'z')) \
655 || (((chr) >= 'A') && ((chr) <= 'Z')) \
658 p = s1 = malloc(len);
660 fatal(2, "memory error!");
663 if (cur_chr == '^') {
665 if (cur_chr == '\0') {
676 if (use_env && cur_chr == '$') { /* ARI */
684 phchar = getenv(phchar);
685 *s = c; /* restore */
687 len += strlen(phchar);
688 s1 = grow(s1, &p, len);
695 if (cur_chr != '\\') {
701 if (cur_chr == '\0') {
704 *p++ = '\\'; /* +1 for len */
715 if (sending && *s == '\0')
731 if (sending && phone_num) {
732 len += strlen(phone_num);
733 s1 = grow(s1, &p, len);
734 for (phchar = phone_num; *phchar != '\0'; phchar++)
744 if (sending && phone_num2) {
745 len += strlen(phone_num2);
746 s1 = grow(s1, &p, len);
747 for (phchar = phone_num2; *phchar != '\0'; phchar++)
793 if (isoctal (cur_chr)) {
797 cur_chr |= *s++ - '0';
800 cur_chr |= *s++ - '0';
804 if (cur_chr != 0 || sending) {
805 if (sending && (cur_chr == '\\' || cur_chr == 0))
820 *p++ = '\r'; /* +2 for len */
822 *p = '\0'; /* +3 for len */
827 * A modified version of 'strtok'. This version skips \ sequences.
830 char *expect_strtok (char *s, char *term)
832 static char *str = "";
837 * If a string was specified then do initial processing.
843 * If this is the escape flag then reset it and ignore the character.
864 * If this is not in the termination string, continue.
866 if (strchr (term, *str) == (char *) 0) {
872 * This is the terminator. Mark the end of the string and stop.
881 * Process the expect string
884 void chat_expect (char *s)
889 if (strcmp(s, "HANGUP") == 0) {
894 if (strcmp(s, "ABORT") == 0) {
899 if (strcmp(s, "CLR_ABORT") == 0) {
904 if (strcmp(s, "REPORT") == 0) {
909 if (strcmp(s, "CLR_REPORT") == 0) {
914 if (strcmp(s, "TIMEOUT") == 0) {
919 if (strcmp(s, "ECHO") == 0) {
924 if (strcmp(s, "SAY") == 0) {
930 * Fetch the expect and reply string.
933 expect = expect_strtok (s, "-");
936 if (expect == (char *) 0)
939 reply = expect_strtok (s, "-");
942 * Handle the expect string. If successful then exit.
944 if (get_string (expect))
948 * If there is a sub-reply string then send it. Otherwise any condition
951 if (reply == (char *) 0 || exit_code != 3)
959 * The expectation did not occur. This is terminal.
962 msgf("Failed (%s)", fail_reason);
965 terminate(exit_code);
969 * Translate the input character to the appropriate string for printing
973 char *character(int c)
975 static char string[10];
978 meta = (c & 0x80) ? "M-" : "";
982 sprintf(string, "%s^%c", meta, (int)c + '@');
984 sprintf(string, "%s^?", meta);
986 sprintf(string, "%s%c", meta, c);
992 * process the reply string
994 int chat_send (register char *s)
996 char file_data[STR_LEN];
1004 ret = write(2, s, len) != len;
1011 memset(&sa, 0, sizeof(sa));
1013 if (strcmp(s, "OFF") == 0)
1014 sa.sa_handler = SIG_IGN;
1016 sa.sa_handler = sighup;
1017 sigaction(SIGHUP, &sa, NULL);
1023 echo = (strcmp(s, "ON") == 0);
1032 if (n_aborts >= MAX_ABORTS)
1033 fatal(2, "Too many ABORT strings");
1037 if (strlen(s1) + 1 > sizeof(fail_buffer))
1038 fatal(1, "Illegal or too-long ABORT string ('%v')", s);
1040 abort_string[n_aborts++] = s1;
1043 msgf("abort on (%v)", s1);
1047 if (clear_abort_next) {
1053 clear_abort_next = 0;
1057 if (strlen(s1) + 1 > sizeof(fail_buffer))
1058 fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s);
1061 for (i=0; i < n_aborts; i++) {
1062 if ( strcmp(s1,abort_string[i]) == 0 ) {
1063 free(abort_string[i]);
1064 abort_string[i] = NULL;
1068 msgf("clear abort on (%v)", s1);
1073 pack_array(abort_string,old_max);
1081 if (n_reports >= MAX_REPORTS)
1082 fatal(2, "Too many REPORT strings");
1085 if (strlen(s1) + 1 > sizeof(fail_buffer))
1086 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1088 report_string[n_reports++] = s1;
1091 msgf("report (%v)", s1);
1095 if (clear_report_next) {
1101 clear_report_next = 0;
1105 if (strlen(s1) + 1 > sizeof(fail_buffer))
1106 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1108 old_max = n_reports;
1109 for (i=0; i < n_reports; i++) {
1110 if ( strcmp(s1,report_string[i]) == 0 ) {
1111 free(report_string[i]);
1112 report_string[i] = NULL;
1116 msgf("clear report (%v)", s1);
1121 pack_array(report_string,old_max);
1132 timeout = DEFAULT_CHAT_TIMEOUT;
1135 msgf("timeout set to %d seconds", timeout);
1141 * The syntax @filename means read the string to send from the
1145 /* skip the @ and any following white-space */
1147 while (*++fn == ' ' || *fn == '\t')
1154 /* open the file and read until STR_LEN-1 bytes or end-of-file */
1157 fatal(1, "%s -- open failed: %m", fn);
1158 while (n < STR_LEN - 1) {
1159 int nr = fread(&file_data[n], 1, STR_LEN - 1 - n, f);
1161 fatal(1, "%s -- read error", fn);
1168 /* use the string we got as the string to send,
1169 but trim off the final newline if any. */
1170 if (n > 0 && file_data[n-1] == '\n')
1177 if (strcmp(s, "EOT") == 0)
1179 else if (strcmp(s, "BREAK") == 0)
1193 status = read(0, &c, 1);
1198 return ((int)c & 0x7F);
1201 msgf("warning: read() on stdin returned %d", status);
1213 usleep(10000); /* inter-character typing delay (?) */
1216 status = write(1, &ch, 1);
1224 msgf("warning: write() on stdout returned %d", status);
1231 int write_char(int c)
1233 if (alarmed || put_char(c) < 0) {
1238 if (errno == EINTR || errno == EWOULDBLOCK)
1239 msgf(" -- write timed out");
1241 msgf(" -- write failed: %m");
1248 int put_string(register char *s)
1258 msgf("send (?????\?)");
1260 msgf("send (%v)", s);
1263 alarm(timeout); alarmed = 0;
1266 register char c = *s++;
1269 if (!write_char (c)) {
1287 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1291 if (!write_char (c)) {
1307 * Echo a character to stderr.
1308 * When called with -1, a '\n' character is generated when
1309 * the cursor is not at the beginning of a line.
1311 int echo_stderr(int n)
1318 case '\r': /* ignore '\r' */
1325 ret = write(2, "\n", 1) != 1;
1331 ret = write(2, s, len) != len;
1340 * 'Wait for' this string to appear on this file descriptor.
1342 int get_string(register char *string)
1345 int c, printed = 0, len, minlen;
1346 register char *s = temp, *end = s + STR_LEN;
1347 char *s1, *logged = temp;
1349 fail_reason = (char *)0;
1350 string = s1 = clean(string, 0);
1351 len = strlen(string);
1352 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1355 msgf("expect (%v)", string);
1357 if (len > STR_LEN) {
1358 msgf("expect string is too long");
1374 while ( ! alarmed && (c = get_char()) >= 0) {
1375 int n, abort_len, report_len;
1378 if (echo_stderr(c) != 0) {
1379 fatal(2, "Could not write to stderr, %m");
1382 if (verbose && c == '\n') {
1384 msgf(""); /* blank line */
1386 msgf("%0.*v", s - logged, logged);
1392 if (verbose && s >= logged + 80) {
1393 msgf("%0.*v", s - logged, logged);
1399 fputc( '\n', stderr );
1401 fprintf( stderr, "%s", character(c) );
1404 if (!report_gathering) {
1405 for (n = 0; n < n_reports; ++n) {
1406 if ((report_string[n] != (char*) NULL) &&
1407 s - temp >= (report_len = strlen(report_string[n])) &&
1408 strncmp(s - report_len, report_string[n], report_len) == 0) {
1409 time_t time_now = time ((time_t*) NULL);
1410 struct tm* tm_now = localtime (&time_now);
1412 strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1413 strcat (report_buffer, report_string[n]);
1415 free(report_string[n]);
1416 report_string[n] = (char *) NULL;
1417 report_gathering = 1;
1424 int rep_len = strlen (report_buffer);
1425 if ((rep_len + 1) < sizeof(report_buffer)) {
1426 report_buffer[rep_len] = c;
1427 report_buffer[rep_len + 1] = '\0';
1431 report_gathering = 0;
1432 fprintf (report_fp, "chat: %s\n", report_buffer);
1436 if (s - temp >= len &&
1437 c == string[len - 1] &&
1438 strncmp(s - len, string, len) == 0) {
1441 msgf("%0.*v", s - logged, logged);
1442 msgf(" -- got it\n");
1451 for (n = 0; n < n_aborts; ++n) {
1452 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1453 strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
1456 msgf("%0.*v", s - logged, logged);
1463 strcpy(fail_reason = fail_buffer, abort_string[n]);
1470 if (logged < s - minlen) {
1472 msgf("%0.*v", s - logged, logged);
1476 memmove(temp, s, minlen);
1477 logged = temp + (logged - s);
1481 if (alarmed && verbose)
1482 msgf("warning: alarm synchronization problem");
1487 if (verbose && printed) {
1489 msgf(" -- read timed out");
1491 msgf(" -- read failed: %m");
1501 * Gross kludge to handle Solaris versions >= 2.6 having usleep.
1504 #include <sys/param.h>
1505 #if MAXUID > 65536 /* then this is Solaris 2.6 or later */
1511 #include <sys/types.h>
1512 #include <sys/time.h>
1515 usleep -- support routine for 4.2BSD system call emulations
1516 last edit: 29-Oct-1984 D A Gwyn
1519 extern int select();
1521 /* returns 0 if ok, else -1 */
1522 int usleep(long usec) /* delay in microseconds */
1524 static struct { /* `timeval' */
1525 long tv_sec; /* seconds */
1526 long tv_usec; /* microsecs */
1527 } delay; /* _select() timeout */
1529 delay.tv_sec = usec / 1000000L;
1530 delay.tv_usec = usec % 1000000L;
1532 return select(0, (long *)0, (long *)0, (long *)0, &delay);
1537 char **array, /* The address of the array of string pointers */
1538 int end) /* The index of the next free entry before CLR_ */
1542 for (i = 0; i < end; i++) {
1543 if (array[i] == NULL) {
1544 for (j = i+1; j < end; ++j)
1545 if (array[j] != NULL)
1546 array[i++] = array[j];
1547 for (; i < end; ++i)
1555 * vfmtmsg - format a message into a buffer. Like vsprintf except we
1556 * also specify the length of the output buffer, and we handle the
1557 * %m (error message) format.
1558 * Doesn't do floating-point formats.
1559 * Returns the number of chars put into buf.
1561 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
1564 vfmtmsg(char *buf, int buflen, const char *fmt, va_list args)
1567 int width, prec, fillch;
1568 int base, len, neg, quoted;
1569 unsigned long val = 0;
1574 static char hexchars[] = "0123456789abcdef";
1578 while (buflen > 0) {
1579 for (f = fmt; *f != '%' && *f != 0; ++f)
1585 memcpy(buf, fmt, len);
1600 width = va_arg(args, int);
1603 while (isdigit(c)) {
1604 width = width * 10 + c - '0';
1611 prec = va_arg(args, int);
1614 while (isdigit(c)) {
1615 prec = prec * 10 + c - '0';
1626 i = va_arg(args, int);
1635 val = va_arg(args, unsigned int);
1639 val = va_arg(args, unsigned int);
1643 val = (unsigned long) va_arg(args, void *);
1648 str = va_arg(args, char *);
1651 num[0] = va_arg(args, int);
1656 str = strerror(errno);
1658 case 'v': /* "visible" string */
1659 case 'q': /* quoted string */
1661 p = va_arg(args, unsigned char *);
1662 if (fillch == '0' && prec > 0) {
1665 n = strlen((char *)p);
1666 if (prec > 0 && prec < n)
1669 while (n > 0 && buflen > 0) {
1672 if (!quoted && c >= 0x80) {
1677 if (quoted && (c == '"' || c == '\\'))
1679 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
1683 case '\t': OUTCHAR('t'); break;
1684 case '\n': OUTCHAR('n'); break;
1685 case '\b': OUTCHAR('b'); break;
1686 case '\f': OUTCHAR('f'); break;
1689 OUTCHAR(hexchars[c >> 4]);
1690 OUTCHAR(hexchars[c & 0xf]);
1707 --fmt; /* so %z outputs %z etc. */
1712 str = num + sizeof(num);
1714 while (str > num + neg) {
1715 *--str = hexchars[val % base];
1717 if (--prec <= 0 && val == 0)
1729 len = num + sizeof(num) - 1 - str;
1732 if (prec > 0 && len > prec)
1738 if ((n = width - len) > 0) {
1746 memcpy(buf, str, len);