2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
14 #include <afs/param.h>
18 #include "../afsapplib/afsapplib.h"
27 * VARIABLES __________________________________________________________________
44 TCHAR szSwitch[ cchRESOURCE ];
47 TCHAR szValue[ cchRESOURCE ];
49 { TEXT("cell"), TRUE, FALSE, TEXT("") },
50 { TEXT("subset"), TRUE, FALSE, TEXT("") },
51 { TEXT("server"), TRUE, FALSE, TEXT("") },
52 { TEXT("reset"), FALSE, FALSE, TEXT("") },
53 { TEXT("confirm"), FALSE, FALSE, TEXT("") },
54 { TEXT("user"), TRUE, FALSE, TEXT("") },
55 { TEXT("password"), TRUE, FALSE, TEXT("") },
56 { TEXT("lookup"), FALSE, FALSE, TEXT("") },
57 { TEXT("useexisting"), FALSE, FALSE, TEXT("") }
60 #define nSWITCHES (sizeof(aSWITCHES) / sizeof(aSWITCHES[0]))
64 * PROTOTYPES _________________________________________________________________
68 #define iswhite(_ch) ( ((_ch)==TEXT(' ')) || ((_ch)==TEXT('\t')) )
70 void cdecl CommandLineHelp (int ids, LPTSTR pszFormat = NULL, ...);
74 * ROUTINES ___________________________________________________________________
78 CMDLINEOP ParseCommandLine (LPTSTR pszCmdLine)
81 for (ii = 0; ii < nSWITCHES; ++ii)
82 aSWITCHES[ ii ].fPresent = FALSE;
84 // Search through pszCmdLine for switches; each switch must be
85 // preceeded by "/" or "-".
87 while (pszCmdLine && *pszCmdLine)
89 while (iswhite(*pszCmdLine))
94 if ( (*pszCmdLine != '-') && (*pszCmdLine != '/') )
96 CommandLineHelp (IDS_CMDLINE_SYNTAX);
102 // Okay, we've found what is probably the start of a switch.
103 // See if it matches anything.
105 for (ii = 0; ii < nSWITCHES; ++ii)
107 size_t cch = lstrlen(aSWITCHES[ ii ].szSwitch);
108 if (lstrncmpi (pszCmdLine, aSWITCHES[ ii ].szSwitch, cch))
111 // If the switch wants a value, it must be followed by ":"
112 // or whitespace; if it doesn't, it must be followed by "/"
115 LPTSTR pszAfter = &pszCmdLine[ cch ];
116 if (iswhite (*pszAfter) || (!*pszAfter) ||
117 ((*pszAfter == TEXT(':')) && (aSWITCHES[ ii ].fGetsValue)) ||
118 ((*pszAfter == TEXT('/')) && (!aSWITCHES[ ii ].fGetsValue)) )
120 break; // found a switch!
126 TCHAR szCopy[ cchRESOURCE ];
127 lstrcpy (szCopy, pszCmdLine);
130 *pch && !iswhite(*pch) && !(*pch == TEXT('/')) && !(*pch == TEXT(':'));
134 CommandLineHelp (IDS_CMDLINE_UNRECOGNIZED, TEXT("%s"), szCopy);
137 if (aSWITCHES[ ii ].fPresent)
139 CommandLineHelp (IDS_CMDLINE_DUPLICATE, TEXT("%s"), aSWITCHES[ ii ].szSwitch);
143 // Woo hoo! Found what appears to be a valid switch. Parse it now.
145 aSWITCHES[ ii ].fPresent = TRUE;
146 pszCmdLine += lstrlen( aSWITCHES[ ii ].szSwitch );
147 while (iswhite (*pszCmdLine))
150 if (*pszCmdLine == TEXT(':'))
152 if (!aSWITCHES[ ii ].fGetsValue)
154 CommandLineHelp (IDS_CMDLINE_UNEXPECTVALUE, TEXT("%s"), aSWITCHES[ ii ].szSwitch);
157 for (++pszCmdLine; iswhite (*pszCmdLine); ++pszCmdLine)
161 if (aSWITCHES[ ii ].fGetsValue)
163 if ( (*pszCmdLine == TEXT('/')) || (*pszCmdLine == TEXT('\0')) )
165 CommandLineHelp (IDS_CMDLINE_MISSINGVAL, TEXT("%s"), aSWITCHES[ ii ].szSwitch);
168 BOOL fQuoted = FALSE;
170 for (pszTarget = aSWITCHES[ ii ].szValue;
171 *pszCmdLine && !(*pszCmdLine == TEXT('/') && !fQuoted)
172 && !(iswhite(*pszCmdLine) && !fQuoted); )
174 if (*pszCmdLine == TEXT('"'))
182 *pszTarget++ = *pszCmdLine++;
184 *pszTarget++ = TEXT('\0');
188 // Was the -CONFIRM switch given? It works with any other switch
191 if (aSWITCHES[ swCONFIRM ].fPresent)
193 Action_ShowConfirmations (TRUE);
196 // Now test the command-line for syntactical correctness.
197 // First test: if the SUBSET switch is given, the CELL switch must
200 if ( (aSWITCHES[ swSUBSET ].fPresent) &&
201 (!aSWITCHES[ swCELL ].fPresent) )
203 CommandLineHelp (IDS_CMDLINE_SUBSETNOTCELL);
207 // Similarly, if the SERVER switch is given, the CELL switch must
210 if ( (aSWITCHES[ swSERVER ].fPresent) &&
211 (!aSWITCHES[ swCELL ].fPresent) )
213 CommandLineHelp (IDS_CMDLINE_SERVERNOTCELL);
217 // And if the USER or PASSWORD switch is given, the other is required.
219 if ( (aSWITCHES[ swUSER ].fPresent && !aSWITCHES[ swPASSWORD ].fPresent) ||
220 (aSWITCHES[ swPASSWORD ].fPresent && !aSWITCHES[ swUSER ].fPresent) )
222 CommandLineHelp (IDS_CMDLINE_USERPASSWORD);
226 // Implement the command-line switches.
228 if (aSWITCHES[ swRESET ].fPresent)
230 if (aSWITCHES[ swSERVER ].fPresent)
232 ErasePreferences (aSWITCHES[ swCELL ].szValue, aSWITCHES[ swSERVER ].szValue);
234 else if (aSWITCHES[ swCELL ].fPresent)
236 ErasePreferences (aSWITCHES[ swCELL ].szValue);
238 else // neither cell nor server specified--kill just the general stuff
240 EraseSettings (REGSTR_SETTINGS_BASE, REGSTR_SETTINGS_PATH, REGVAL_SETTINGS);
243 Message (MB_OK | MB_ICONINFORMATION, IDS_CMDLINE_RESET_TITLE, IDS_CMDLINE_RESET_DESC);
247 if (aSWITCHES[ swUSER ].fPresent)
249 LPTSTR pszCell = (aSWITCHES[ swCELL ].fPresent) ? (aSWITCHES[ swCELL ].szValue) : NULL;
252 if (!AfsAppLib_SetCredentials (pszCell, aSWITCHES[ swUSER ].szValue, aSWITCHES[ swPASSWORD ].szValue, &status))
254 ImmediateErrorDialog (status, IDS_SVR_ERROR_BAD_CREDENTIALS);
259 if (aSWITCHES[ swLOOKUP ].fPresent)
261 return opLOOKUPERRORCODE;
264 if (aSWITCHES[ swUSEEXISTING ].fPresent)
267 TCHAR szDefCell[ cchNAME ];
269 if (aSWITCHES[ swCELL ].fPresent)
271 lstrcpy(szDefCell,aSWITCHES[ swCELL ].szValue);
275 AfsAppLib_GetLocalCell(szDefCell);
277 g.hCreds = AfsAppLib_GetCredentials(szDefCell,&ulStatus);
278 if (g.hCreds != NULL)
280 LPOPENCELL_PACKET lpocp = New (OPENCELL_PACKET);
282 memset(lpocp,0x00,sizeof(OPENCELL_PACKET));
283 lstrcpy(lpocp->szCell,szDefCell);
284 lpocp->fCloseAppOnFail = TRUE;
285 lpocp->hCreds = g.hCreds;
287 StartTask(taskOPENCELL,NULL,lpocp);
288 return opNOCELLDIALOG;
294 if (aSWITCHES[ swCELL ].fPresent)
296 LPOPENCELL_PACKET lpp = New (OPENCELL_PACKET);
297 memset (lpp, 0x00, sizeof(OPENCELL_PACKET));
299 lstrcpy (lpp->szCell, aSWITCHES[ swCELL ].szValue);
300 lpp->fCloseAppOnFail = TRUE;
302 if (aSWITCHES[ swSUBSET ].fPresent)
304 lpp->sub = Subsets_LoadSubset (lpp->szCell, aSWITCHES[ swSUBSET ].szValue);
305 if (lpp->sub == NULL)
310 CommandLineHelp (IDS_CMDLINE_INVALIDSUBSET, TEXT("%s%s"), aSWITCHES[ swCELL ].szValue, aSWITCHES[ swSUBSET ].szValue);
314 else if (aSWITCHES[ swSERVER ].fPresent)
316 lpp->sub = New (SUBSET);
317 memset (lpp->sub, 0x0, sizeof(SUBSET));
318 FormatMultiString (&lpp->sub->pszMonitored, TRUE, TEXT("%1"), TEXT("%s"), aSWITCHES[ swSERVER ].szValue);
321 StartTask (taskOPENCELL, NULL, lpp);
322 return opNOCELLDIALOG;
325 // Okay--nothing sufficiently special took place to prevent us
326 // from running the tool normally.
332 void cdecl CommandLineHelp (int ids, LPTSTR pszFormat, ...)
335 va_start (arg, pszFormat);
336 vMessage (MB_OK | MB_ICONHAND, IDS_CMDLINE_TITLE, ids, pszFormat, arg);