summaryrefslogtreecommitdiff
path: root/src/commands.c
blob: db261300955917232107184b0fc821e2a4e926d8 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <limits.h>
#include <unistd.h>
#include <time.h>

#include <readline/readline.h>
#include <readline/history.h>

#include <main.h>
#include <commands.h>
#include <completion.h>
#include <utils.h>

/* Function which tells you that you can't do this. */
void too_dangerous( char *caller )
{
  fprintf( stderr,
           "%s: Too dangerous for me to distribute.  Write it yourself.\n",
           caller );
}

/* Return non-zero if ARG is a valid argument for CALLER, else print
   an error message and return zero. */
int valid_argument( char *caller, char *arg )
{
  if( !arg || !*arg )
  {
    fprintf( stderr, "%s: Argument required.\n", caller );
    return( 0 );
  }

  return( 1 );
}

/* **************************************************************** */
/*                                                                  */
/*                       SILA Shell Commands                        */
/*                                                                  */
/* **************************************************************** */

/* String to pass to system ().  This is for the LIST, VIEW and RENAME
   commands. */
static char syscom[PATH_MAX];

/* List the file(s) named in arg. */
int com_ls( char *arg )
{
  if( !arg )
    arg = "";

  sprintf( syscom, "ls -FClg %s", arg );
  return( system( syscom ) );
}

int com_id( char *arg )
{
  if( !arg )
    arg = "";

  sprintf (syscom, "id %s", arg);
  return( system( syscom ) );
}

int com_more( char *arg )
{
  if( !valid_argument( "more", arg ) )
    return( 1 );

  sprintf (syscom, "more %s", arg);
  return( system( syscom ) );
}

int com_vi( char *arg )
{
  if( !valid_argument( "vi", arg ) )
    return( 1 );

  sprintf (syscom, "vi %s", arg);
  return( system( syscom ) );
}

int com_rename( char *arg )
{
  too_dangerous( "rename" );
  return( 1 );
}

int com_stat( char *arg )
{
  struct stat finfo;

  if( !valid_argument( "stat", arg ) )
    return( 1 );

  if( stat( arg, &finfo ) == -1 )
  {
    perror( arg );
    return( 1 );
  }

  printf( "Statistics for `%s':\n", arg );

  printf( "%s has %ld link%s, and is %ld byte%s in length.\n", arg,
          finfo.st_nlink,
          (finfo.st_nlink == 1) ? "" : "s",
          finfo.st_size,
          (finfo.st_size == 1) ? "" : "s");
  printf( "Inode Last Change at: %s", ctime( &finfo.st_ctime ) );
  printf( "      Last access at: %s", ctime( &finfo.st_atime ) );
  printf( "    Last modified at: %s", ctime( &finfo.st_mtime ) );
  return( 0 );
}

int com_delete( char *arg )
{
  too_dangerous( "delete" );
  return( 1 );
}

/* Print out help for ARG, or for all of the commands if ARG is
   not present. */
int com_help( char *arg )
{
  register int i;
  int printed = 0;

  for( i = 0; current->list[i].name; ++i )
  {
    if( !*arg || (strcmp( arg, current->list[i].name ) == 0) )
    {
      printf( "  %-12s\t- %s.\n", current->list[i].name, current->list[i].doc );
      printed++;
    }
  }

  if( printed )
    printf( "\n" );

  if( !printed )
  {
    printf( "No commands match `%s'.  Possibilties are:\n", arg );

    for( i = 0; current->list[i].name; ++i )
    {
      /* Print in six columns. */
      if( printed == 6 )
      {
        printed = 0;
        printf( "\n" );
      }

      printf( "%s\t", current->list[i].name );
      printed++;
    }

    if( printed )
      printf( "\n" );
  }
  return( 0 );
}

/* Change to the directory ARG. */
int com_cd( char *arg )
{
  if( !strcmp( arg, "~" ) )
  {
    arg = getenv( "HOME" );
  }

  if( chdir( arg ) == -1 )
  {
    perror( arg );
    return( 1 );
  }

  com_pwd( "" );
  return( 0 );
}

/* Print out the current working directory. */
int com_pwd( char *ignore )
{
  char dir[PATH_MAX], *s;

  s = getcwd( dir, PATH_MAX );
  if( s == 0 )
  {
    printf( "Error getting pwd: %s\n", dir );
    return( 1 );
  }

  printf( "Current directory is: %s\n", dir );
  return( 0 );
}

int com_ping( char *arg )
{
  if( !valid_argument( "ping", arg ) )
    return( 1 );

  sprintf (syscom, "ping %s", arg);
  return( system( syscom ) );
}

int com_useradd( char *arg )
{
  /* temporary STUB. Should use busctl or REST */
  if( !valid_argument( "useradd", arg ) )
    return( 1 );

  sprintf (syscom, "useradd %s", arg);
  return( system( syscom ) );
}

int com_userdel( char *arg )
{
  /* temporary STUB. Should use busctl or REST */
  if( !valid_argument( "userdel", arg ) )
    return( 1 );

  sprintf (syscom, "userdel %s", arg);
  return( system( syscom ) );
}

int com_userlist( char *arg )
{
  /* temporary STUB. Should use busctl or REST */
  if( !arg ) arg = "";

  sprintf (syscom, "cat /etc/passwd | cut -f1 -d':' %s", arg);
  return( system( syscom ) );
}


/* The user wishes to quit using this program.  Just set DONE non-zero. */
int com_quit( char *arg )
{
  done = 1;
  return( 0 );
}



int com_shell( char *arg )
{
  if( !arg ) arg = "";

  current = &shell;

  initialize_readline();
  return( 0 );
}

int com_users( char *arg )
{
  if( !arg ) arg = "";

  current = &users;

  initialize_readline();
  return( 0 );
}

int com_top( char *arg )
{
  if( !arg ) arg = "";

  current = &top;

  initialize_readline();
  return( 0 );
}