]> asedeno.scripts.mit.edu Git - pssh.git/blob - util/stdlib.c
My 2006-07-10 release.
[pssh.git] / util / stdlib.c
1 /**********
2  * Copyright (c) 2003-2004 Greg Parker.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY GREG PARKER ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  **********/
24
25 #include "stdlib.h"
26
27 #include "ctype.h"
28 #include "assert.h"
29 #include "ssh/ssh.h"
30 #include "rsrc/rsrc.h"
31
32 uint8_t *memchr(uint8_t *buf, uint8_t ch, uint16_t bufLen)
33 {
34     uint8_t *end = buf + bufLen;
35     for ( ; buf < end; buf++) {
36         if (*buf == ch) return buf;
37     }
38     return NULL;
39 }
40
41
42 char *strsep(char **stringp, const char *delim)
43 {
44     char *pos;
45     char *str = *stringp;
46     if (!str) return NULL;
47
48     // GrP only implemented for single-character delim
49     assert(strlen(delim) == 1);
50
51     pos = strchr(str, *delim);
52     if (pos) {
53         *pos = '\0';
54         *stringp = pos+1;
55     } else {
56         *stringp = NULL;
57     }
58     return str;
59 }
60
61
62 int strcasecmp(const char *s1, const char *s2)
63 {
64     return StrCaselessCompare(s1, s2);
65 }
66
67
68 char *strcasestr(const char *big, const char *little)
69 {
70     int littlelen = strlen(little);
71
72     const char *s = big;
73     for (s = big; *s != '\0'; s++) {
74         if (0 == memcmp(s, little, littlelen)) return (char *)s;
75     }
76     return NULL;
77 }
78
79
80 void printf(const char *format, ...)
81 {
82     char buf[320];
83     char *c;
84     va_list args;
85     struct ssh_session_t *ss = ssh_get_current();
86
87     va_start(args, format);
88     vsnprintf(buf, sizeof(buf), format, args);
89     va_end(args);
90
91     // Filter out nonprintable chars
92     for (c = buf; *c != '\0'; c++) {
93         if (!isprint(*c)  &&  !isspace(*c)) *c = '?';
94     }
95
96     if (ss) ssh_receive_bytes(ss, buf, strlen(buf));
97     // ssh_update(ss);
98     // DON'T update here - it can cause a crash 
99 }
100
101 void debug_printf(const char *format, ...)
102 {
103     char buf[320];
104     va_list args;
105     RectangleType r = {{0, 0}, {160, 14}};
106     WinHandle drawWindow;
107     int len;
108
109     len = snprintf(buf, sizeof(buf), "%lu ", TimGetTicks());
110
111     va_start(args, format);
112     vsnprintf(buf+len, sizeof(buf)-len, format, args);
113     va_end(args);
114
115     // print on top of program title box
116     // will be visible even with vt100 problems and alerts
117     // fixme this causes problems with hi-res drawing
118     // drawWindow = WinSetDrawWindow(NULL);
119     WinPushDrawState();
120     WinEraseRectangle(&r, 0);
121     WinDrawChars(buf, strlen(buf), 0, 0);
122     WinPopDrawState();
123     // WinSetDrawWindow(drawWindow);
124 }
125
126 int fatal(const char *format, ...)
127 {
128     char buf[320];
129     va_list args;
130
131     va_start(args, format);
132     vsnprintf(buf, sizeof(buf), format, args);
133     va_end(args);
134
135     // release version: display alert, then kill program
136     FrmCustomAlert(FatalAlertID, buf, " ", " ");
137     SysReset();  // fixme don't see a better way to kill the program cleanly
138
139     // development version: don't obscure debug_printf output or vt100
140     /*
141       {
142       struct ssh_session_t *ss = ssh_get_current();
143       if (ss) {
144       ssh_receive_bytes(ss, "\nFATAL:", 7);
145       ssh_receive_bytes(ss, buf, strlen(buf));
146       } else {
147       debug_printf("%s", buf);
148       while(1) { }
149       }
150     */
151
152     return 0;
153 }
154
155
156
157 void assert_failed(uint32_t which)
158 {
159     char intbuf[20];
160     FrmCustomAlert(AlertFormID, "arm assert failed!", StrIToA(intbuf, which), " ");
161 }
162