summaryrefslogtreecommitdiff
path: root/parse.cpp
blob: 875ad406360c476f4202186c6ad908462f663b5b (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
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include <led.h>
#include <morse.h>
#include <unistd.h>
void pish_parse(const string& str)
{
  stringstream ss(str);
  string cmd;
  ss>>cmd;
  vector<string> args;
  while(ss)
    {
      string arg;
      ss >> arg;
      args.push_back(arg);
    }
  string argstr;
  for(int i=0;i<args.size();++i)
    {
      argstr+=args[i]+' ';
    }
  if(cmd.length()==0)
    return; // exit early
  if(cmd=="exit" or cmd=="quit")
    {
      dash();
      exit(0);
    }
  else if(cmd=="morse")
    {
      if(argstr!="")
	{
	  dash();
	  usleep(250000);
	  transmit(argstr);
	  usleep(250000);
	  dash();
	}
      else
	{
	  cerr << "morse requires an argument" << endl;
	  dot();
	  usleep(250000);
	  dot();
	}
    }
  else if(cmd=="exec")
    {
      if(argstr=="")
	{
	  dot();
	  usleep(250000);
	  dot();
	}
      else
	{
	  dash();
	  usleep(250000);
	  int ret=system(argstr.c_str());
	  if(ret!=EXIT_SUCCESS)
	    {
	      cerr << "Command exited with return value " << ret << endl;
	      dot();
	      usleep(250000);
	      dot();
	    }
	  else
	    {
	      cerr << "Command exited normally." << endl;
	      dash();
	    }
	}
    }
  else if(cmd=="cd")
    {
      if(argstr!="")
	{
	  dash();
	  usleep(250000);
	  chdir(argstr.c_str());
	  dash();
	}
      else
	{
	  cerr << "Need an argument" << endl;
	  dot();
	  usleep(250000);
	  dot();
	}
    }
  else if(cmd=="pwd")
    {
      dash();
      usleep(250000);
      char* pwd=getcwd(0,0); // is this extension Linux only?
      cout << pwd << endl;
      transmit(string(pwd));
      free(pwd);
      usleep(25000);
      dash();
    }
  else
    {
      cerr << "Invalid command." << endl;
      dot();
      usleep(250000);
      dot();
    }
}