/* Reads test.sh and format a menu showing the various commands */ #include #include #include #include #include #include #include #include #include using namespace std; struct MENU { string verb; string catego; string descrip; MENU (const string &_verb, const string &_catego, const string &_descrip){ for (unsigned i=0; i<_verb.size(); i++){ if (_verb[i] != '"') verb += _verb[i]; } catego = _catego; descrip = _descrip; if (descrip.size()==0) descrip = "z"; } bool operator < (const MENU &s) const{ bool ret = false; int ok = catego.compare(s.catego); if (ok < 0){ ret = true; }else if (ok == 0){ ok = descrip.compare (s.descrip); if (ok < 0){ ret = true; }else if (ok == 0){ ret = verb < s.verb; } } return ret; } }; int main (int argc, char *argv[]) { glocal int ret = -1; glocal const char *scriptfile = "test.sh"; glocal.ret = (argc,argv); setproginfo ("","0.0","..."); int ret = -1; glocal map sections; glocal vector menus; (glocal.scriptfile,true); if (strncmp(line,"##",2)==0){ vector tb; int n = str_splitline(line,' ',tb); if (n >= 3){ string rest = tb[2]; for (int i=3; i tb; int n = str_splitline (line,' ',tb); if ( n > 3 && strcmp(tb[2].c_str(),"\"$1\"")==0){ string &verb = tb[4]; bool found = false; for (int i=4; i 0) rest += string(" "); rest += tb[j]; } string catego = tb[i+1]; map::iterator it = glocal.sections.find(catego); if (it != glocal.sections.end()) catego = it->second; glocal.menus.push_back(MENU(verb,catego,rest)); found = true; } } if (!found){ glocal.menus.push_back(MENU(verb,"z","")); } } } return 0; sort (glocal.menus.begin(), glocal.menus.end()); string last; for (vector::iterator it = glocal.menus.begin(); it != glocal.menus.end(); it++){ if (last != it->catego){ last = it->catego; printf ("\n%s\n",last.c_str()); } const char *descrip = it->descrip.c_str(); if (strcmp(descrip,"z")==0) descrip = ""; const char *catego = it->catego.c_str(); if (strcmp(catego,"z")==0) catego = " "; printf ("\t%-25s %s\n",it->verb.c_str(),descrip); } return ret; return glocal.ret; }