revert between 56095 -> 55830 in arch
[AROS.git] / workbench / utilities / Help / main.c
blobaf8596cabe1507336db32187ac89d4128decc30a
1 /*
2 Copyright © 2013, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 /******************************************************************************
8 NAME
10 Help name (section)
12 SYNOPSIS
14 NAME, SECTION
16 LOCATION
18 SYS:Utilities
20 FUNCTION
22 Shows help information for commands and system applications.
23 The help files are stored in HELP:English with a sub-directory
24 for each section. The utility "Multiview" is used to show the
25 guide files. If neither NAME nor SECTION are given an index page
26 will be shown.
28 INPUTS
30 NAME -- the name of the command or application whose help
31 you want to view. The name is case-insensitive.
32 SECTION -- the section where the help document will be searched.
33 Currently available are "commands" and "system". If you
34 don't specify the section all available sections will
35 be searched. The section is case-insensitive.
37 RESULT
39 NOTES
41 EXAMPLE
43 Help dir
44 Help dir commands
45 Help help
47 BUGS
49 SEE ALSO
51 INTERNALS
53 ******************************************************************************/
55 #include <proto/dos.h>
57 #include <string.h>
58 #include <stdio.h>
59 #include <stdlib.h>
61 //#define DEBUG 1
62 #include <aros/debug.h>
64 #define ARG_TEMPLATE "NAME,SECTION"
66 const char *ver = "$VER: Help 1.1 (09.03.2013)";
68 enum
70 ARG_NAME,
71 ARG_SECTION,
72 ARG_COUNT
75 struct RDArgs *rda;
76 static STRPTR name;
77 static STRPTR section;
78 static TEXT docpath[100];
79 static TEXT viewpath[256];
81 static CONST_STRPTR section_arr[] = {"Commands", "System", NULL};
84 static void clean_exit(CONST_STRPTR s)
86 if (s) PutStr(s);
87 if (rda) FreeArgs(rda);
88 exit(0);
92 static LONG show_file(STRPTR buffer, ULONG buflen, CONST_STRPTR path)
94 strlcpy(buffer, "SYS:Utilities/Multiview ", buflen);
95 strlcat(buffer, path, buflen);
96 D(bug("[Help/show_file] SystemTags path %s\n", buffer));
97 // FIXME: running asynchron?
98 LONG error = SystemTags
100 buffer,
101 TAG_DONE
103 return error;
107 static STRPTR find_file
109 STRPTR buffer, ULONG buflen,
110 CONST_STRPTR name, CONST_STRPTR section
113 // TODO: support different suffixes
115 STRPTR retval = NULL;
116 struct FileInfoBlock *fib = AllocDosObjectTagList(DOS_FIB, NULL);
117 if (fib)
119 snprintf(buffer, buflen, "HELP:English/%s/%s.guide", section, name);
120 D(bug("[Help/find_file] search for %s\n", buffer));
121 BPTR lock = Lock(buffer, ACCESS_READ);
122 if (lock)
124 BOOL success = Examine(lock, fib);
125 if (success)
127 if (fib->fib_DirEntryType < 0) // is file?
129 retval = buffer;
132 UnLock(lock);
134 FreeDosObject(DOS_FIB, fib);
136 return retval;
140 int main(int argc, char **argv)
142 if (argc) // started from Shell
144 IPTR args[ARG_COUNT] = {0};
146 rda = ReadArgs(ARG_TEMPLATE, args, NULL);
147 if (!rda)
149 PrintFault(IoErr(), argv[0]);
150 clean_exit("ReadArgs() failed.\n");
153 if (args[ARG_NAME])
155 name = (STRPTR)args[ARG_NAME];
156 if (strpbrk(name, ":/()#?~"))
158 clean_exit("Illegal characters in argument 'name'.\n");
162 if (args[ARG_SECTION])
164 section = (STRPTR)args[ARG_SECTION];
165 if (strpbrk(section, ":/()#?~"))
167 clean_exit("Illegal characters in argument 'section'.\n");
171 D(bug("[Help] name %s section %s\n", name, section));
173 if (name == NULL && section == NULL)
175 strcpy(docpath, "HELP:English/Index.guide");
176 show_file(viewpath, sizeof(viewpath), docpath);
178 else if (section == NULL || *section == '\0')
180 CONST_STRPTR *sect;
181 BOOL found = FALSE;
182 for (sect = section_arr ; (found == FALSE) && (*sect) ; sect++)
184 if (find_file(docpath, sizeof(docpath), name, *sect))
186 show_file(viewpath, sizeof(viewpath), docpath);
187 found = TRUE;
190 if (found == FALSE)
192 clean_exit("Can't find help document.\n");
195 else
197 if (find_file(docpath, sizeof(docpath), name, section))
199 show_file(viewpath, sizeof(viewpath), docpath);
201 else
203 clean_exit("Can't find help document.\n");
207 else // started from Wanderer
209 strcpy(docpath, "HELP:English/Index.guide");
210 show_file(viewpath, sizeof(viewpath), docpath);
213 clean_exit(NULL);
214 return 0;