ReactOS  0.4.15-dev-1201-gb2cf5a4
man.c
Go to the documentation of this file.
1 /*
2  * FILE : man.c
3  * NATIVE NAME: ReactOS manual browser
4  * AUTHOR : Semyon Novikov (tappak)
5  * PROJECT : ReactOS Operating System
6  * DESCRIPTION: manual file browser (Use Linux man file syntax)
7  * DATE : 2004-03-29
8  * LICENSE : GPL
9  */
10 
11  /* Known issues.
12  * 1. Scroll screen
13  * 2. Non ENVIRONMENT manpath varrible
14  * 3. At the end of man page must be ./" tag!
15  */
16 
17 
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <windows.h>
22 #include <wincon.h>
23 
24 #define MAXLINE 256
25 #define BOLD 7|9
26 #define ITALIC 3|9
27 #define NORMAL 7|0
28 
29 /*===[functions]===*/
30 void SetCl(WORD cl);
31 int OpenF(char* name);
32 int CloseF(void);
33 void Usage(void);
34 int AnalyzeArgv(char *);
35 int AnalyzeFile();
36 /*=================*/
37 
38 /*====[Globals]====*/
40 char OpenFlag=0;
42 /*=================*/
43 
44 void
46 {
48 }
49 
50 int
51 OpenF(char* name)
52 {
53  int ret = 0;
54  char *cp;
55 
56  /* C:\man\\... */
57  cp = getenv("SystemDrive");
58  if (cp && *cp)
59  {
60  strcpy(manpath, cp);
61  strcat(manpath, "\\man\\");
62  }
63  else
64  {
65  strcpy(manpath, "C:\\man\\");
66  }
68 
69  manfile = fopen(manpath, "r");
70  if (manfile != NULL)
71  {
72  OpenFlag = 1;
73  AnalyzeFile();
74  }
75  else
76  {
77  ret = -1;
78  }
79 
80  return ret;
81 }
82 
83 int
85 {
86  int retval=0;
87 
88  if(fclose(manfile))
89  OpenFlag=0;
90  else retval=-1;
91 
92  return retval;
93 }
94 
95 void
97 {
98  puts("usage: man [command]");
99  puts("see \"man man\" for details");
100 }
101 
102 int
103 AnalyzeArgv(char *argument)
104 {
105  int element;
106  char HelpFlag=0;
107  char *keys[]={"--help","/h","/?","-h"};
108  char *sections[]={".1",".2",".3",".4",".5",".6",".7",".8",".9"};
109  char *filename=(char*)malloc(sizeof(char)*MAXLINE);
110 
111  strcpy(filename,argument); //save argument value
112 
113  for(element=0;element<_countof(keys);element++)
114  {
115  if(!strcmp(keys[element],argument))
116  {
117  Usage();
118  HelpFlag=1;
119  }
120  }
121 
122  element = 0;
123 
124  if(!HelpFlag)
125  {
126 
127  if(OpenF(filename))
128  {
130  {
131  strcpy(filename,argument);
132  element++;
133  }
134 
135  if(element>=_countof(sections)) printf("No manual for %s\n",argument);
136  }
137 
138  }
139 
140  return element;
141 }
142 
143 void sh_outp(char *cur_string)
144 {
145  int symbol;
146  putchar('\n');
147  putchar('\n');
148  for(symbol=3;putchar(cur_string[symbol]); symbol++);
149 }
150 
151 void th_outp(char *cur_string, char *THtag)
152 {
153  int symbol;
154  putchar('\n');
155  putchar('\n');
156  putchar('\t');
157  putchar('\t');
158  SetCl(ITALIC);
159  for(symbol=3;putchar(THtag[symbol]); symbol++);
160  putchar('\n');
161  SetCl(NORMAL);
162 }
163 
164 void text_outp(char *cur_string)
165 {
166  int symbol=0;
167 
168  if(cur_string[0]=='.')
169  while(cur_string[symbol]!=' ')
170  symbol++;
171 
172  for(;cur_string[symbol]!='\n'; symbol++)
173  putchar(cur_string[symbol]);
174 
175  putchar(' ');
176 }
177 
178 int
180 {
181  char *cur_string=(char*)malloc(sizeof(char)*MAXLINE);
182  char *THtag=(char*)malloc(sizeof(char)*MAXLINE);
183 
184 
185 
186 
187  while(fgets(cur_string,MAXLINE,manfile))
188 
189 /* TAGs processing */
190  if((cur_string[0]=='.')&&(cur_string[1]=='S')&&
191  (cur_string[2]=='H')) // .SH tag
192  {
193  SetCl(BOLD);
194  sh_outp(cur_string);
195  SetCl(NORMAL);
196  }
197  else
198  if((cur_string[0]=='.')&&(cur_string[1]=='I')&&
199  (cur_string[2]==' ')) // .I tag
200  {
201  SetCl(ITALIC);
202  text_outp(cur_string);
203  SetCl(NORMAL);
204  }
205  else
206  if((cur_string[0]=='.')&&(cur_string[1]=='/')&&
207  (cur_string[2]=='\"')); // ./" tag (comment)
208 
209  else
210  if((cur_string[0]=='.')&&(cur_string[1]=='T')&&
211  (cur_string[2]=='H')) // .TH tag
212  {
213  strcpy(THtag,cur_string);
214  }
215  else
216  if((cur_string[0]=='.')&&(cur_string[1]=='B')&&
217  (cur_string[2]==' ')) // .B tag
218  {
219  SetCl(BOLD);
220  text_outp(cur_string);
221  SetCl(NORMAL);
222  }
223  else
224  if((cur_string[0]=='.')&&(cur_string[1]=='N')&&
225  (cur_string[2]=='L'))
226  {
227  putchar('\n');
228  putchar(' ');
229  }
230 
231  else
232  {
233  text_outp(cur_string); // print plane text
234  }
235  th_outp(cur_string, THtag);
236  /* END of TAGs processing */
237 
238  free(cur_string);
239  free(THtag);
240  return 0;
241 }
242 
243 
244 int
245 main(int argc, char *argv[])
246 {
247 
248  switch(argc)
249  {
250  case 1: Usage(); break;
251  case 2: AnalyzeArgv(argv[1]);break;
252  default: Usage();break;
253  }
254 
255  if(OpenFlag)CloseF();
256  SetCl(NORMAL);
257  return 0;
258 }
259 
static int argc
Definition: ServiceArgs.c:12
int CloseF(void)
Definition: man.c:84
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
void th_outp(char *cur_string, char *THtag)
Definition: man.c:151
#define free
Definition: debug_ros.c:5
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:200
char manpath[MAX_PATH]
Definition: man.c:41
#define argv
Definition: mplay32.c:18
const char * filename
Definition: ioapi.h:135
FILE * manfile
Definition: man.c:39
int AnalyzeArgv(char *)
Definition: man.c:103
struct section sections[2]
Definition: diskspace.c:792
int putchar(int c)
Definition: crtsupp.c:12
#define MAXLINE
Definition: man.c:24
smooth NULL
Definition: ftsmooth.c:416
BOOL WINAPI SetConsoleTextAttribute(IN HANDLE hConsoleOutput, IN WORD wAttributes)
Definition: console.c:669
#define _countof(array)
Definition: sndvol32.h:68
#define MAX_PATH
Definition: compat.h:34
unsigned short WORD
Definition: ntddk_ex.h:93
int main(int argc, char *argv[])
Definition: man.c:245
void SetCl(WORD cl)
Definition: man.c:45
int ret
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
#define STD_OUTPUT_HANDLE
Definition: winbase.h:265
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
int puts(const char *string)
Definition: crtsupp.c:23
void sh_outp(char *cur_string)
Definition: man.c:143
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
int OpenF(char *name)
Definition: man.c:51
_Check_return_ char *__cdecl getenv(_In_z_ const char *_VarName)
void Usage(void)
Definition: man.c:96
int AnalyzeFile()
Definition: man.c:179
#define NORMAL
Definition: man.c:27
#define ITALIC
Definition: man.c:26
POINT cp
Definition: magnifier.c:59
Definition: name.c:38
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define malloc
Definition: debug_ros.c:4
char OpenFlag
Definition: man.c:40
void text_outp(char *cur_string)
Definition: man.c:164
#define BOLD
Definition: man.c:25
#define printf
Definition: config.h:203