ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/lmainit/lmainit.c
Revision: 1.1
Committed: Sat Dec 7 21:49:49 2002 UTC (21 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include <unistd.h>
2     #include <sys/types.h>
3     #include <sys/stat.h>
4     #include <fcntl.h>
5     #include <sys/ioctl.h>
6     #include <sys/vt.h>
7    
8     #include <stdio.h>
9     #include <stdlib.h>
10    
11     #include "builtins.h"
12     #include "shell.h"
13     #include "bashgetopt.h"
14    
15     /* A builtin `xxx' is normally implemented with an `xxx_builtin' function.
16     If you're converting a command that uses the normal Unix argc/argv
17     calling convention, use argv = make_builtin_argv (list, &argc) and call
18     the original `main' something like `xxx_main'. Look at cat.c for an
19     example.
20    
21     Builtins should use internal_getopt to parse options. It is the same as
22     getopt(3), but it takes a WORD_LIST *. Look at print.c for an example
23     of its use.
24    
25     If the builtin takes no options, call no_options(list) before doing
26     anything else. If it returns a non-zero value, your builtin should
27     immediately return EX_USAGE. Look at logname.c for an example.
28    
29     A builtin command returns EXECUTION_SUCCESS for success and
30     EXECUTION_FAILURE to indicate failure. */
31     console_builtin (list)
32     WORD_LIST *list;
33     {
34     char *path = list->word->word;
35     int fd = open (path, O_RDWR);
36     unsigned int console;
37    
38     if (fd != -1)
39     {
40     dup2 (fd, 0);
41     dup2 (fd, 1);
42     dup2 (fd, 2);
43     if (fd > 2)
44     close (fd);
45    
46     if (sscanf (path, "/dev/tty%u%*c", &console) == 1)
47     {
48     if (console > 0 && console < 64)
49     {
50     ioctl (0, VT_ACTIVATE, console);
51     ioctl (0, VT_WAITACTIVE, console);
52     }
53     }
54    
55     return EXECUTION_SUCCESS;
56     }
57     else
58     return EXECUTION_FAILURE;
59     }
60    
61     /* An array of strings forming the `long' documentation for a builtin xxx,
62     which is printed by `help xxx'. It must end with a NULL. */
63     char *console_doc[] = {
64     "connects the current process to 'path'",
65     "no controlling terminal will be allocated",
66     "if the path is of the form /dev/ttyN, switches to that console",
67     (char *)NULL
68     };
69    
70     /* The standard structure describing a builtin command. bash keeps an array
71     of these structures. The flags must include BUILTIN_ENABLED so the
72     builtin can be used. */
73     struct builtin console_struct = {
74     "console", /* builtin name */
75     console_builtin, /* function implementing the builtin */
76     BUILTIN_ENABLED, /* initial flags for builtin */
77     console_doc, /* array of long documentation strings. */
78     "console path", /* usage synopsis; becomes short_doc */
79     0 /* reserved for internal use */
80     };
81    
82     setsid_builtin (list)
83     WORD_LIST *list;
84     {
85     int fd = open ("/dev/tty", O_RDWR);
86    
87     if (fd != -1)
88     {
89     ioctl (fd, TIOCNOTTY);
90     close (fd);
91     }
92    
93     return setsid () >= 0
94     ? EXECUTION_SUCCESS
95     : EXECUTION_FAILURE;
96     }
97    
98     char *setsid_doc[] = {
99     "calls setsid",
100     (char *)NULL
101     };
102    
103     struct builtin setsid_struct = {
104     "setsid",
105     setsid_builtin,
106     BUILTIN_ENABLED,
107     setsid_doc,
108     "setsid",
109     0
110     };
111    
112     winsize_builtin (list)
113     WORD_LIST *list;
114     {
115     struct winsize ws;
116    
117     if (ioctl (0, TIOCGWINSZ, &ws) != -1)
118     {
119     printf ("rows=%d; cols=%d\n", ws.ws_row, ws.ws_col);
120     return EXECUTION_SUCCESS;
121     }
122     else
123     {
124     printf ("rows=80; cols=25\n");
125     return EXECUTION_FAILURE;
126     }
127     }
128    
129     char *winsize_doc[] = {
130     "returns the window size as 'rows=<rows>; cols=<columns>'",
131     (char *)NULL
132     };
133    
134     struct builtin winsize_struct = {
135     "winsize",
136     winsize_builtin,
137     BUILTIN_ENABLED,
138     winsize_doc,
139     "winsize",
140     0
141     };
142    
143     renamefunc_builtin (list)
144     WORD_LIST *list;
145     {
146     if (list && list->next)
147     {
148     BUCKET_CONTENTS *elt = (BUCKET_CONTENTS *)hash_remove (list->word->word, shell_functions, 0);
149     if (elt)
150     {
151     SHELL_VAR *cmd = (SHELL_VAR *)elt->data;
152    
153     free (elt->key);
154     free (elt);
155    
156     free (cmd->name);
157     cmd->name = savestring (list->next->word->word);
158    
159     elt = hash_insert (cmd->name, shell_functions, 0);
160     elt->data = (char *)cmd;
161    
162     return EXECUTION_SUCCESS;
163     }
164     else
165     return EXECUTION_FAILURE;
166     }
167     else
168     return EX_USAGE;
169     }
170    
171     char *renamefunc_doc[] = {
172     "rename a function from oldname to newname. the new function mustn't exist",
173     (char *)NULL
174     };
175    
176     struct builtin renamefunc_struct = {
177     "renamefunc",
178     renamefunc_builtin,
179     BUILTIN_ENABLED,
180     renamefunc_doc,
181     "renamefunc oldname newname",
182     0
183     };
184    
185     usleep_builtin (list)
186     WORD_LIST *list;
187     {
188     usleep (atof (list->word->word) * 1e6);
189    
190     return EXECUTION_SUCCESS;
191     }
192    
193     char *usleep_doc[] = {
194     "sleep a (possibly fractional) number of seconds",
195     (char *)NULL
196     };
197    
198     struct builtin usleep_struct = {
199     "usleep",
200     usleep_builtin,
201     BUILTIN_ENABLED,
202     usleep_doc,
203     "usleep time-in-fractional-seconds",
204     0
205     };
206    
207     str2hex_builtin (list)
208     WORD_LIST *list;
209     {
210     char *p;
211    
212     for (p = list->word->word; *p; p++)
213     printf ("%02x", *p);
214     puts ("\n");
215    
216     return EXECUTION_SUCCESS;
217     }
218    
219     char *str2hex_doc[] = {
220     "convert the first argument to a hex string",
221     (char *)NULL
222     };
223    
224     struct builtin str2hex_struct = {
225     "str2hex",
226     str2hex_builtin,
227     BUILTIN_ENABLED,
228     str2hex_doc,
229     "str2hex string",
230     0
231     };
232    
233     #if 0
234     havefunc_builtin (list)
235     WORD_LIST *list;
236     {
237     if(list && find_hash_item(list->word->word, shell_functions))
238     return EXECUTION_SUCCESS;
239     return EXECUTION_FAILURE;
240     }
241    
242     char *havefunc_doc[] = {
243     "checks if argument is a shell function",
244     (char *)NULL
245     };
246    
247     struct builtin havefunc_struct = {
248     "havefunc",
249     havefunc_builtin,
250     BUILTIN_ENABLED,
251     havefunc_doc,
252     "havefunc name",
253     0
254     };
255     #endif
256