summaryrefslogtreecommitdiff
path: root/other/Kermit/src/load.cpp
blob: 2d40735721ce09cc60e1c73264157357a15ec713 (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
/*
 * load.cpp:
 * written by palmers / teso
 */
#include <Kermit>
#include <string>
#include <iostream>
#include <fstream>
#include <stack>

#define PROGRAM		"load"
#define VERSION		"0.0.1"
#define AUTHOR		"palmers / teso"

#define LINE_LENGTH	16384


void usage (char *s)
{
  cout << PROGRAM << VERSION << " by " << AUTHOR << endl;
  cout << "Usage: " << s << " [options] <file>" << endl;
  cout << "Options:" << endl;
  cout << "\t-v:\t be verbose" << endl;
  cout << "\t-E:\t proceed even if errors occour" << endl;
  cout << endl;
  exit (0);
}


int main (int argc, char **argv)
{
  bool		verbose = false,
		err_continue = false;
  int		x = 0;
  ifstream	fs;
  char		line[LINE_LENGTH + 1];
  Patch		*tp = NULL;
  rwKernel	*rw = NULL;
  SymbolTable	*st = NULL;
  Addr2AddrList	*a2a = NULL;

  if (argc < 2)
    usage (argv[0]);

  for (x = 1; x < argc; x++)
    {
      if (argv[x][0] == '-')
	{
	  switch (argv[x][1])
	    {
	      case 'v':
		verbose = true;
		break;
	      case 'E':
		err_continue = true;
		break;
	      default:
		cout << "unknow option: " << argv[x] << endl;
		usage (argv[0]);
		break;
	    }
	}
    }

  rw = new rwKernel ();
  genDummyValMap ();
  st = new SymbolTable (rw);
  a2a = genReplaceValMap (st);

  fs.open (argv[argc - 1]);
  if (!fs.is_open ())
    {
      cerr << "failed to open \"" << argv[argc - 1] << "\"" << endl;
      abort ();
    }

  if (verbose)
    cout << "done." << endl;

  while (!fs.eof ())
    {
      fs.getline (line, LINE_LENGTH, '\n');
      tp = new Patch (string (line), rw);

      if (tp->isClean ())
        tp->link (a2a);

      if (tp->isLinked ())
        tp->apply ();
      else if (verbose)
	{
          cout << "#" << x << ": Linking Failed" << endl;
	  if (err_continue)
	    continue;
	  break;
	}
      if (tp->isApplied () && verbose)
         cout << "#" << x << ": Success" << endl;
      else
	{
	  cout << "#" << x << ": Applying Failed" << endl;
	  if (err_continue)
	    continue;
	  break;
	}
      delete tp;
    }
  if (verbose)
    cout << "done." << endl;
  fs.close ();
  return 0;
}