aboutsummaryrefslogtreecommitdiffstats
path: root/erts/etc/vxworks/vxcall.c
blob: 3362d05fc545aa91e4278cc6f4cbc8ebe2c17685 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 1997-2009. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 */
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <vxWorks.h>
#include <symLib.h>
#include <sysSymTbl.h>
#include <a_out.h>

extern char *malloc();
static STATUS lookup();

/*
   Little utility to convert from Unix' argv,argv calling conventions to
   VxWorks' arg0,arg1,arg2,...
   Will do limited argument parsing - no parenthesis around nor commas
   between the args, which may be "-enclosed strings (without \ escapes),
   '-enclosed characters (also no \ escapes), integers, or symbols.
*/

int vxcall(argc, argv)
int argc;
char **argv;
{
    int vxarg[10];		/* Max 10 args can be passed */
    FUNCPTR entry;
    SYM_TYPE type;
    int i, l;

#ifdef DEBUG
    fdprintf(2, "vxcall:");
    for (i = 1; i < argc; i++)
	fdprintf(2, " %s", argv[i]);
    fdprintf(2, "\n");
#endif
    if (lookup(argv[1], N_EXT | N_TEXT, (char **)&entry) != OK)
	return(ERROR);
    /* Do limited "C" parsing of the args */
    for (i = 0; i < 10; i++) {
	if (i < argc - 2) {
	    switch (argv[i+2][0]) {
	    case '"':
		l = strlen(argv[i+2]) - 1;
		if (argv[i+2][l] != '"')
		    return(ERROR);
		/* just strip the quotes - should do \escapes within... */
		vxarg[i] = (int)&argv[i+2][1];
		argv[i+2][l] = '\0';
		break;
	    case '\'':
		if (argv[i+2][2] != '\'')
		    return(ERROR);
		vxarg[i] = argv[i+2][1]; /* should do \escapes... */
		break;
	    case '1': case '2': case '3': case '4':
	    case '5': case '6': case '7': case '8': case '9':
		vxarg[i] = atoi(argv[i+2]); /* should do octal, hex, float.. */
		break;
	    default:
		if (lookup(argv[i+2], 0, (char **)&vxarg[i]) != OK)
		    return(ERROR);
	    }
	} else
	    vxarg[i] = 0;
    }
#ifdef DEBUG
    fdprintf(2, "calling 0x%x(0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x)\n",
	     entry, vxarg[0], vxarg[1], vxarg[2], vxarg[3], vxarg[4],
		    vxarg[5], vxarg[6], vxarg[7], vxarg[8], vxarg[9]);
#endif
    return((*entry)(vxarg[0], vxarg[1], vxarg[2], vxarg[3], vxarg[4],
		    vxarg[5], vxarg[6], vxarg[7], vxarg[8], vxarg[9]));
}

/* Entry point for unix:cmd in post-4.1 erlang - uses "sh -c 'cmd...'" */
int sh(argc, argv)
int argc;
char **argv;
{
    int ll = strlen(argv[argc-1]) - 1;

#ifdef DEBUG
    int i;
    fdprintf(2, "sh:");
    for (i = 1; i < argc; i++)
	fdprintf(2, " %s", argv[i]);
    fdprintf(2, "\n");
#endif
    if (strcmp(argv[1], "-c") != 0 ||
	argv[2][0] != '\'' || argv[argc-1][ll] != '\'')
	return(ERROR);
    argv[argc-1][ll] = '\0';	/* delete trailing ' */
    argv[2]++;			/* skip leading ' (*after* the above!) */
    return(vxcall(argc-1, argv+1));
}

/* Lookup symbol; get address for text symbols, value (assuming int)
   otherwise; return OK or ERROR on failure
   Symbol name is null-terminated and without the leading '_' */
STATUS
lookup(sym, stype, value)
char *sym, **value;
int stype;
{
    char buf[256];
    char *symname = buf;
    int len, ret;
    SYM_TYPE type;

    len = strlen(sym);
    if (len > 254 && (symname = malloc(len+2)) == NULL)
	return(ERROR);
#if defined _ARCH_PPC || defined SIMSPARCSOLARIS
    /* GCC for PPC or SIMSPARC doesn't add a leading _ to symbols */
    strcpy(symname, sym);
#else
    sprintf(symname, "_%s", sym);
#endif
    ret = (stype != 0) ?
	symFindByNameAndType(sysSymTbl, symname, value, &type, stype, stype) :
	    symFindByName(sysSymTbl, symname, value, &type);
    if (symname != buf)
	free(symname);
    if (ret == OK && (type & N_TEXT) == 0) /* get value */
	*value = (char *)*((int *)*value);
    return(ret);
}