File: readcmd.c

package info (click to toggle)
vmfs-tools 0.2.5-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 532 kB
  • sloc: ansic: 6,487; sh: 22; makefile: 3
file content (178 lines) | stat: -rw-r--r-- 4,586 bytes parent folder | download | duplicates (4)
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
/*
 * vmfs-tools - Tools to access VMFS filesystems
 * Copyright (C) 2009 Christophe Fillot <cf@utc.fr>
 * Copyright (C) 2009,2010 Mike Hommey <mh@glandium.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <dlfcn.h>
#include "readcmd.h"

static const cmd_t empty_cmd = { 0, };

static char *init_readline(const char *prompt);

static char *(*readline)(const char *prompt) = init_readline;
static void (*add_history)(const char *string);

/* local_readline() buffer allocation increment */
#define READLINE_INCR 256

char *local_readline(const char *prompt) {
   char *buf = NULL, *buf2;
   size_t len, alloc = READLINE_INCR;

   if (prompt)
      fprintf(stdout, "%s", prompt);

   buf = malloc(alloc);
   if (!buf)
      return NULL;
   buf2 = buf;
   do {
      if (!fgets(buf2, READLINE_INCR, stdin))
         break;

      len = strlen(buf2);
      if ((len > 0) && (buf2[len-1] == '\n')) {
         buf2[--len] = 0;
         return buf;
      }
      len += (buf2 - buf);
      alloc += READLINE_INCR;
      if (!(buf2 = realloc(buf, alloc)))
         break;
      buf = buf2;
      buf2 = &buf[len];
   } while (1);

   free(buf);
   return NULL;
}

static void local_add_history(const char *string)
{
}

/* Argv allocation increment */
#define ARGV_INCR 16

/* Return a command after having prompted for it */
const cmd_t *readcmd(const char *prompt)
{
   char *buf;
   int i;
   cmd_t *cmd = NULL;
   if (!isatty(fileno(stdin)))
      prompt = NULL;
   if (!(buf = readline(prompt))) {
      if (prompt)
         fprintf(stdout, "\n");
      return NULL;
   }
   for(i=strlen(buf)-1;(i>=0)&&(buf[i]==' ');buf[i--]=0);
   if (buf[0]==0) {
      free(buf);
      return &empty_cmd;
   }
   add_history(buf);

   cmd = calloc(sizeof(cmd_t),1);
   cmd->buf = buf;
   if ((cmd->redir = index(buf, '|')))
      cmd->piped = 1;
   else
      cmd->redir = index(buf, '>');
   if (cmd->redir && (cmd->redir != buf) && (cmd->redir[-1] == ' ')) {
      char *s;
      for(s=cmd->redir-1;(s>=buf)&&(*s==' ');*(s--)=0);
      *(cmd->redir++) = 0;
      if (!cmd->piped && *cmd->redir == '>') {
         cmd->append = 1;
         if (*(++cmd->redir) == '>') {
            fprintf(stderr,"Unexpected token '>'\n");
            free(cmd);
            free(buf);
            return &empty_cmd;
         }
      }
      while (*cmd->redir == ' ')
         cmd->redir++;
   } else
      cmd->redir = NULL;
   cmd->argv = malloc(ARGV_INCR * sizeof(char *));
   cmd->argv[0] = buf;
   do {
      while (*(cmd->argv[cmd->argc]) == ' ')
         *(cmd->argv[cmd->argc]++) = 0;
      if (!(++cmd->argc % ARGV_INCR)) {
         char **newargv = realloc(cmd->argv,
                                  (cmd->argc + ARGV_INCR) * sizeof(char *));
         if (newargv) {
            cmd->argv = newargv;
         } else {
            freecmd(cmd);
            fprintf(stderr, "Not enough memory\n");
            return NULL;
         }
      }
   } while((cmd->argv[cmd->argc] = strchr(cmd->argv[cmd->argc - 1], ' ')));

   return cmd;
}

/* Free a command */
void freecmd(const cmd_t *cmd)
{
   if (!cmd || cmd == &empty_cmd)
      return;

   free(cmd->argv);
   free(cmd->buf);
   free((cmd_t *)cmd);
}

static void *dlhandle = NULL;

static char *init_readline(const char *prompt)
{
   if (isatty(fileno(stdin))) {
      dlhandle = dlopen("libreadline.so", RTLD_NOW);
      if (!dlhandle)
         dlhandle = dlopen("libreadline.so.6", RTLD_NOW);
      if (!dlhandle)
         dlhandle = dlopen("libreadline.so.5", RTLD_NOW);
      if (dlhandle) {
         readline = dlsym(dlhandle, "readline");
         add_history = dlsym(dlhandle, "add_history");
         if (readline && add_history)
            goto readline;
      }
   }
   readline = local_readline;
   add_history = local_add_history;
readline:
   return readline(prompt);
}

static __attribute__((destructor)) void close_readline(void)
{
   if (dlhandle)
      dlclose(dlhandle);
}