Link with C++ linker
[oweals/cde.git] / cde / programs / dtksh / misc.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: misc.c /main/5 1996/10/04 16:21:08 drk $ */
24 /*      Copyright (c) 1991, 1992 UNIX System Laboratories, Inc. */
25 /*      All Rights Reserved     */
26
27 /*      THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF    */
28 /*      UNIX System Laboratories, Inc.                  */
29 /*      The copyright notice above does not evidence any       */
30 /*      actual or intended publication of such source code.    */
31
32 #include        "name.h"
33 #include        "shell.h"
34 #include "stdio.h"
35 #include <X11/X.h>
36 #include <X11/Intrinsic.h>
37 #include <X11/IntrinsicP.h>
38 #define NO_AST
39 #include "dtksh.h"
40 #undef NO_AST
41 #include "exksh.h" /* which includes sys/types.h */
42 /* #include <sys/param.h> */
43 #include <string.h>
44 /* #include <search.h> */
45 #include <ctype.h>
46 #include "docall.h"
47 #include "msgs.h"
48
49 static char use[] = "0x%lx";
50
51 int
52 symcomp(
53         void *sym1,
54         void *sym2 )
55
56 {
57         return(strcmp(((struct symarray *) sym1)->str, ((struct symarray *) sym2)->str));
58 }
59
60
61 void *
62 getaddr(
63         char *str )
64 {
65         if (isdigit(str[0]))
66                 return((void *) strtoul(str, NULL, 0));
67         else
68                 return((void *) fsym(str, -1));
69 }
70
71 int
72 do_deref(
73         int argc,
74         char **argv )
75 {
76         unsigned char *ptr;
77         long i, len = 0;
78         short longwise = -1;
79         char printit = 0;
80         static char xk_ret_buffer[1024];
81         char * errmsg;
82
83         for (i = 1; (i < argc) && argv[i] != NULL && argv[i][0] == '-'; i++) {
84                 if (isdigit(argv[i][1])) {
85                         if (longwise < 0)
86                                 longwise = 0;
87                         ptr = (unsigned char *) argv[i] + 1;
88                         xk_par_int(&ptr, &len, NULL);
89                         if (!len) {
90                            errmsg = strdup(GETMESSAGE(10,1, 
91                               "An invalid length parameter was specified: %s"));
92                            printerrf(argv[0], errmsg, 
93                                     argv[i], NULL, NULL, NULL, NULL, NULL,
94                                     NULL, NULL);
95                            free(errmsg);
96                            return(SH_FAIL);
97                         }
98                 }
99                 else if (argv[i][1] == 'l')
100                         longwise = 1;
101                 else if (argv[i][1] == 'p')
102                         printit = 1;
103         }
104         if (longwise < 0)
105                 longwise = 1;
106         if (!len)
107                 len = sizeof(long);
108         if (i >= argc) {
109                 XK_USAGE(argv[0]);
110         }       
111         ptr = (unsigned char *) getaddr(argv[i++]);
112         if (ptr) {
113                 if ( ((i < argc) && argv[i]) || printit) {
114                         char *dbuf, *p;
115                         int totlen;
116                         char buf[10 * BUFSIZ];
117                         int incr;
118
119                         if (printit)
120                                 totlen = len + 1 + 1;
121                         else
122                                 totlen = len + strlen(argv[i]) + 1 + 1;
123                         dbuf = (char *) (totlen < (10 * BUFSIZ - 1)) ? buf : malloc(totlen);
124                         if (printit)
125                                 strcpy(dbuf, "0x");
126                         else
127                                 sprintf(dbuf, "%s=0x", argv[i]);
128                         p = dbuf + strlen(dbuf);
129                         incr = longwise ? sizeof(long) : sizeof(char);
130                         for (i=0; i < len; i += incr, p += 2 * incr)
131                                 sprintf(p, "%*.*x", incr * 2, incr * 2, longwise ? *((unsigned long *) (ptr + i)) : (unsigned long) (ptr[i]));
132                         if (printit)
133                                 ALTPUTS(dbuf);
134                         else
135                                 env_set(dbuf);
136                         if (dbuf != buf)
137                                 free(dbuf);
138                 }
139                 else {
140                         if (len > sizeof(unsigned long)) {
141                            char tmpBuf[30];
142
143                            sprintf(tmpBuf, "%ld", (long)sizeof(unsigned long));
144                            errmsg = strdup(GETMESSAGE(10,2, 
145                               "To set RET, the length must be less than: %s"));
146                            printerrf(argv[0], errmsg,
147                                  tmpBuf, NULL, NULL, NULL, NULL, NULL, NULL,
148                                  NULL);
149                            free(errmsg);
150                            return(SH_FAIL);
151                         }
152                         sprintf(xk_ret_buffer, use, *((unsigned long *) ptr));
153                         xk_ret_buf = xk_ret_buffer;
154                 }
155                 return(SH_SUCC);
156         }
157         errmsg = strdup(GetSharedMsg(DT_UNDEF_SYMBOL));
158         printerrf(argv[0], errmsg, argv[--i], NULL, NULL, NULL,
159                   NULL, NULL, NULL, NULL);
160         free(errmsg);
161         return(SH_FAIL);
162 }
163
164 void *
165 nop(
166         void *var )
167 {
168         return(var);
169 }
170
171 void *
172 save_alloc(
173         void *var )
174 {
175         return(var);
176 }