summaryrefslogtreecommitdiff
path: root/systems/msdos/tx/src/tx.c
blob: 542d3050e426a6797a137777465d3559d73c73a7 (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
#include <stdio.h> 
#include <conio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <ctype.h> 
#include <dir.h> 
#include "txdefs.h"

/* global scope */
char Progname[MAXPATH] ;
char Path[MAXPATH] = "\0" ;
char FileName[MAXFILE] = "\0" ;
char Extension[MAXEXT] = "\0" ;
char **menu[MAX_MENUS] ;
int NumMenus = 0;
int NumEntries[MAX_MENUS] ;
int CurrDrive ;
char ch_env_size[6] ; 
/* Text colors: 
   normal ->   yellow on blue 
   selected -> black on lightgray */
int color[2]={0x1E,0x70};
/* Alternative colors: */
/* int color[2]={0x30,0x0B};  */

void SplitPath(void) ;

/* File scope */

FILE *cnf ;
char ConfigFile[MAXPATH] = "\0" ;


void main(int argc, char *argv[])
{
  void ReadConfig(void) ;
  int i, env_size = ENV_SIZE ;
  extern void TxMain(void);
  strcpy(Progname,argv[0]);
  _wscroll = 0 ;            /* Disallow scrolling */
  textattr(color[0]) ;   /* Set menu colors */
  CurrDrive = getdisk() ;
  
  for ( i = 1 ; i < argc ; i++ ) {
    if ( argv[i][0] == CNF_COMLINE ) {  /* Configuration file name */
      ++argv[i] ;
      strcpy(ConfigFile,argv[i]) ;
    }
    else if ( !strcmp(argv[i],"-h") ) { /* help */
      fprintf(stderr,MSG_USAGE,Progname,CNF_ENVVAR);
      exit(0);
    }
    else if ( !strcmp(argv[i],"-m") ) { /* monochrome */
      color[0]=0x70;
      color[1]=0x0F;
      textattr(color[0]) ;
    }
    else if ( !strcmp(argv[i],"-e") ) { /* environment size */
      i++ ; /* get next argument */
      if ( ( i == argc ) ||  ( ( env_size = atoi(argv[i]) ) < 256 ) ) {
	fprintf(stderr,MSG_USAGE,Progname,CNF_ENVVAR);
	exit(0);
      }
    }
    else { /* Working file name */
      int OldDrive=CurrDrive;
      if (Path[0]) { /* We already have a working file name */
	fprintf(stderr,MSG_USAGE,Progname,CNF_ENVVAR);
	exit(0);
      }
      strcpy(Path,argv[i]) ;
      SplitPath() ;
      setdisk(CurrDrive) ;
      if ( chdir(Path) ) {
	CurrDrive=OldDrive;
	setdisk(CurrDrive);
	Path[0]='\0';
      }
    }
  }
  
/* Set environment size for child processes as a string to be passed
   to the command processor */
  itoa(env_size,ch_env_size,10);

/* Look for environmental variable CNF_ENVVAR */
  if (!ConfigFile[0]) {
    char *ppt;
    if ( (ppt=getenv(CNF_ENVVAR)) != NULL )
      strcpy(ConfigFile,ppt) ;
  }

/* Check for default file name, "tx.cnf" (or "progname.cnf" if the user
   renamed the program to "progname.exe") being present in directory in 
   which the executable resides. This should be the normal case. */

  if (!(ConfigFile[0])) {
    char drive[MAXDRIVE] , dir[MAXDIR] , file[MAXFILE], ext[MAXEXT] ;
    fnsplit(argv[0],drive,dir,file,ext) ;
    strcpy(ext,".cnf") ;
    fnmerge(ConfigFile,drive,dir,file,ext) ;
  }

/* Print usage message and exit if no config file exists */
  if ( (cnf = fopen(ConfigFile,"r")) == NULL )  {
    fprintf(stderr,MSG_NOCONFIG,Progname);
    fprintf(stderr,MSG_USAGE,Progname,CNF_ENVVAR);
    exit(1) ;
  }

  ReadConfig() ;

/* Set working file name if not supplied by user */
  if (!Path[0]) {
    getcwd(Path,MAXPATH) ;
#ifdef __GO32__
    strcat(Path,"/") ;
#else
    strcat(Path,"\\") ;
#endif
    strcat(Path,DEF_WFN) ;
    strcat(Path,DEF_WFEXT) ;
    SplitPath();
  }
  
  TxMain() ;  /* Branch to main body of tx */
}



void ReadConfig(void)
{
  char line[1+MAX_ACTION], **cp ;
  int len , iscommand = 0 ;
  const int limit[2] = { MAX_WIDTH , MAX_ACTION } ;
  int  ReadLine(char *,int) ;
  
  NumEntries[0] = 0 ;
  menu[0]= (char **) malloc( 2 * MAX_ENTRIES * sizeof(char *) );
  cp = menu[0] ;          /* Position current pointer */
  
  while ( (len = ReadLine(line,limit[iscommand])) > 0 ) {
    if ( line[0] == NEW_MENU_SIGN ) { /* Start new menu */
      if (!NumEntries[0])
	continue ;   /* Ignore if menu 0 */
      if (iscommand) {  /* Error; were expecting a command */
	exit(3) ;
      }
      ++NumMenus ;
      if (NumMenus >= MAX_MENUS)
	break ;
      NumEntries[NumMenus] = 0 ;
      menu[NumMenus]= (char **) malloc(2*MAX_ENTRIES*sizeof(char *));
      cp = menu[NumMenus] ;           /* Position current pointer */
      continue ;
    }
    if ( NumEntries[NumMenus] >= MAX_ENTRIES )
      continue ;                       /* Ignore excess entries */
    *cp = (char *) malloc(len+1);
    strcpy(*cp,line) ;
    ++cp ;
    NumEntries[NumMenus] += iscommand ; /* New entry if it was command */
    iscommand = 1 - iscommand ;         /* Switch text/command */
  }
  fclose(cnf) ;
  return ;
}



int ReadLine(char *line,int max)
{
  int c , notin = 1 , count = 0 , wasspace = 0 ;
  
  while ( (c = fgetc(cnf)) != EOF && count < max ) {
    if ( notin ){ /* Skip whitespace while not reading a line */
      while ( isspace(c) && (c = fgetc(cnf)) != EOF ) ;
      if ( c == EOF )
	return -1 ;
      if ( c == NEW_MENU_SIGN ) { /* New menu */
	*line++ = c ;
	++count ;
	break ;   /* Do not allow any more characters */
      }
      notin = 0 ;
    }
    if ( c == COMMENT_SIGN ){  /* Skip till eol if COMMENT_SIGN */
      while ( (c = fgetc(cnf)) != EOF && c != '\n' ) ;
      if (count) /* We have read some valid characters */
	break ;
      notin = 1 ;  /* We have not read anything. Restart */
      continue ;
    }
    if ( c == '\r' )
      continue ;
    if ( c == '\n' )     /* reached end of line */
      break ;
    if ( isspace(c) ) {
      if (wasspace)
	continue ;   /* ignore extra spaces */
      wasspace = 1 ;   /* set space flag */
      c = ' ' ;        /* convert tabs to spaces */
    }
    else
      wasspace = 0 ;    /* clear space flag */
    *line++ = c ;
    ++count ;
  }
  if ( count ) {
    if (wasspace) {
      --count ;  /* Remove trailing spaces */
      --line ;
    }
    *line = '\0' ;
    return count ;
  }
  return -1 ;
}


void SplitPath(void) { 
  int flags ; 
  char drive[MAXDRIVE] , dir[MAXDIR]; 
  void StripBsl(char *) ; 
  
  flags = fnsplit(Path,drive,dir,FileName,Extension) ; 
 
  if ( flags & DRIVE ) { 
    drive[0] = toupper(drive[0]) ; 
    CurrDrive = ( (int) drive[0] ) - 'A' ; 
  } 
  else { 
    drive[0] = CurrDrive + 'A' ; 
    drive[1] = ':' ; 
    drive[2] = '\0' ; 
  } 
  
  if ( flags & DIRECTORY ) { 
    strcpy(Path,drive) ; 
    if ( dir[0] != BACKSLASH )
#ifdef __GO32__
      strcat(Path,"/");
#else
      strcat(Path,"\\");
#endif
    strcat(Path,dir) ; 
    StripBsl(Path);
  }
  else {
    Path[0]='\0';
    getcwd(Path,MAXPATH) ;
  }
  
  if ( !(flags & FILENAME) ) { 
    strcpy(FileName,DEF_WFN) ; 
    strcpy(Extension,DEF_WFEXT) ; 
  } 
} 
 
void StripBsl(char *Pname) 
{ 
  for ( ; *Pname ; Pname++);
  if ( *--Pname == BACKSLASH )
    *Pname-- = '\0';
  if ( *Pname == ':' )
    *++Pname = BACKSLASH ;
  *++Pname = '\0';
}