OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [expect/] [exp_printify.c] - Blame information for rev 1776

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* exp_printify - printable versions of random ASCII strings
2
 
3
Written by: Don Libes, NIST, 2/6/90
4
 
5
Design and implementation of this program was paid for by U.S. tax
6
dollars.  Therefore it is public domain.  However, the author and NIST
7
would appreciate credit if this program or parts of it are used.
8
 
9
*/
10
 
11
#include "expect_cf.h"
12
#include "tcl.h"
13
#ifdef NO_STDLIB_H
14
#include "../compat/stdlib.h"
15
#else
16
#include <stdlib.h>             /* for malloc */
17
#endif
18
#include <ctype.h>
19
 
20
/* generate printable versions of random ASCII strings.  Primarily used */
21
/* by cmdExpect when -d forces it to print strings it is examining. */
22
char *
23
exp_printify(s)
24
char *s;
25
{
26
        static int destlen = 0;
27
        static char *dest = 0;
28
        char *d;                /* ptr into dest */
29
        unsigned int need;
30
 
31
        if (s == 0) return("<null>");
32
 
33
        /* worst case is every character takes 4 to printify */
34
        need = strlen(s)*4 + 1;
35
        if (need > destlen) {
36
                if (dest) ckfree(dest);
37
                dest = ckalloc(need);
38
                destlen = need;
39
        }
40
 
41
        for (d = dest;*s;s++) {
42
                if (*s == '\r') {
43
                        strcpy(d,"\\r");                d += 2;
44
                } else if (*s == '\n') {
45
                        strcpy(d,"\\n");                d += 2;
46
                } else if (*s == '\t') {
47
                        strcpy(d,"\\t");                d += 2;
48
                } else if (isascii(*s) && isprint(*s)) {
49
                        *d = *s;                        d += 1;
50
                } else {
51
                        sprintf(d,"\\x%02x",*s & 0xff); d += 4;
52
                }
53
        }
54
        *d = '\0';
55
        return(dest);
56
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.