Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/cs/nam.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1990-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
/*
22
* Glenn Fowler
23
* AT&T Research
24
*
25
* open cs paths and return to sender
26
* the requests are
27
*
28
* request
29
*
30
* <op> <mount> <path> [pwd=<pwd>] [<name>=<value>] ...
31
*
32
* <mount> == /dev/fd translates fd to path if possible
33
*/
34
35
static const char id[] = "@(#)$Id: cs.nam (AT&T Research) 1996-02-29 $\0\n";
36
37
#include <cs.h>
38
#include <hashkey.h>
39
#include <ctype.h>
40
#include <error.h>
41
#include <tok.h>
42
43
#define DEVFD "/dev/fd\n"
44
45
typedef struct
46
{
47
int active;
48
int dormant;
49
} State_t;
50
51
static int
52
svc_connect(void* handle, int fd, CSID* id, int clone, char** args)
53
{
54
register State_t* state = (State_t*)handle;
55
56
NoP(fd);
57
NoP(id);
58
NoP(clone);
59
NoP(args);
60
state->active++;
61
state->dormant = 0;
62
return(0);
63
}
64
65
/*
66
* service a request
67
*/
68
69
static int
70
svc_read(void* handle, int fd)
71
{
72
State_t* state = (State_t*)handle;
73
register char* b;
74
register int n;
75
int getfd;
76
int ud;
77
char* msg;
78
char* op;
79
char* logical;
80
char* path;
81
char* name;
82
char* value;
83
84
static char buf[(3 * PATH_MAX) / 2 + 1];
85
86
if ((n = csread(fd, buf, sizeof(buf), CS_LINE)) <= 1)
87
goto drop;
88
buf[n - 1] = 0;
89
msg = buf;
90
if (tokscan(msg, &msg, " %s %s %s ", &op, &logical, &path) < 1)
91
goto nope;
92
switch (strkey(op))
93
{
94
case HASHKEY5('d','e','b','u','g'):
95
error_info.trace = -strtol(logical, NiL, 10);
96
goto nope;
97
case HASHKEY4('o','p','e','n'):
98
getfd = 1;
99
break;
100
case HASHKEY4('p','a','t','h'):
101
if (csrecv(fd, NiL, &ud, 1) != 1)
102
goto drop;
103
b = cspath(ud, CS_PATH_NAME);
104
close(ud);
105
if (!b)
106
goto nope;
107
n = strlen(b);
108
b[n++] = '\n';
109
if (cswrite(fd, b, n) != n)
110
goto drop;
111
return(0);
112
case HASHKEY4('q','u','i','t'):
113
exit(0);
114
break;
115
case HASHKEY4('s','t','a','t'):
116
getfd = 0;
117
break;
118
default:
119
goto nope;
120
}
121
while (tokscan(msg, &msg, " %s=%s ", &name, &value) == 2)
122
switch (strkey(name))
123
{
124
case HASHKEY3('p','w','d'):
125
chdir(value);
126
break;
127
}
128
if (!getfd)
129
goto nope;
130
if (*path)
131
do
132
{
133
n = *--path;
134
*path = '/';
135
} while (n);
136
if ((ud = csopen(logical, CS_OPEN_READ)) >= 0)
137
{
138
if (cswrite(fd, DEVFD, sizeof(DEVFD) - 1) != sizeof(DEVFD) - 1 || cssend(fd, &ud, 1))
139
{
140
close(ud);
141
goto drop;
142
}
143
close(ud);
144
return(0);
145
}
146
nope:
147
if (cswrite(fd, "\n", 1) == 1)
148
return(0);
149
drop:
150
state->active--;
151
return(-1);
152
}
153
154
/*
155
* exit if inactive on timeout
156
*/
157
158
static int
159
svc_timeout(void* handle)
160
{
161
State_t* state = (State_t*)handle;
162
163
if (!state->active)
164
{
165
if (state->dormant)
166
exit(0);
167
state->dormant = 1;
168
}
169
return(0);
170
}
171
172
int
173
main(int argc, char** argv)
174
{
175
static State_t state;
176
177
NoP(argc);
178
cstimeout(CS_SVC_DORMANT * 1000L);
179
csserve(&state, argv[1], NiL, NiL, svc_connect, svc_read, NiL, svc_timeout);
180
exit(1);
181
}
182
183