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
11 #include <afs/param.h>
15 #include "../afsapplib/afsapplib.h"
24 * VARIABLES __________________________________________________________________
41 TCHAR szSwitch[ cchRESOURCE ];
44 TCHAR szValue[ cchRESOURCE ];
46 { TEXT("cell"), TRUE, FALSE, TEXT("") },
47 { TEXT("subset"), TRUE, FALSE, TEXT("") },
48 { TEXT("server"), TRUE, FALSE, TEXT("") },
49 { TEXT("reset"), FALSE, FALSE, TEXT("") },
50 { TEXT("confirm"), FALSE, FALSE, TEXT("") },
51 { TEXT("user"), TRUE, FALSE, TEXT("") },
52 { TEXT("password"), TRUE, FALSE, TEXT("") },
53 { TEXT("lookup"), FALSE, FALSE, TEXT("") },
54 { TEXT("useexisting"), FALSE, FALSE, TEXT("") }
57 #define nSWITCHES (sizeof(aSWITCHES) / sizeof(aSWITCHES[0]))
61 * PROTOTYPES _________________________________________________________________
65 #define iswhite(_ch) ( ((_ch)==TEXT(' ')) || ((_ch)==TEXT('\t')) )
67 void cdecl CommandLineHelp (int ids, LPTSTR pszFormat = NULL, ...);
71 * ROUTINES ___________________________________________________________________
75 CMDLINEOP ParseCommandLine (LPTSTR pszCmdLine)
77 for (size_t ii = 0; ii < nSWITCHES; ++ii)
78 aSWITCHES[ ii ].fPresent = FALSE;
80 // Search through pszCmdLine for switches; each switch must be
81 // preceeded by "/" or "-".
83 while (pszCmdLine && *pszCmdLine)
85 while (iswhite(*pszCmdLine))
90 if ( (*pszCmdLine != '-') && (*pszCmdLine != '/') )
92 CommandLineHelp (IDS_CMDLINE_SYNTAX);
98 // Okay, we've found what is probably the start of a switch.
99 // See if it matches anything.
101 for (ii = 0; ii < nSWITCHES; ++ii)
103 size_t cch = lstrlen(aSWITCHES[ ii ].szSwitch);
104 if (lstrncmpi (pszCmdLine, aSWITCHES[ ii ].szSwitch, cch))
107 // If the switch wants a value, it must be followed by ":"
108 // or whitespace; if it doesn't, it must be followed by "/"
111 LPTSTR pszAfter = &pszCmdLine[ cch ];
112 if (iswhite (*pszAfter) || (!*pszAfter) ||
113 ((*pszAfter == TEXT(':')) && (aSWITCHES[ ii ].fGetsValue)) ||
114 ((*pszAfter == TEXT('/')) && (!aSWITCHES[ ii ].fGetsValue)) )
116 break; // found a switch!
122 TCHAR szCopy[ cchRESOURCE ];
123 lstrcpy (szCopy, pszCmdLine);
124 for (LPTSTR pch = szCopy;
125 *pch && !iswhite(*pch) && !(*pch == TEXT('/')) && !(*pch == TEXT(':'));
129 CommandLineHelp (IDS_CMDLINE_UNRECOGNIZED, TEXT("%s"), szCopy);
132 if (aSWITCHES[ ii ].fPresent)
134 CommandLineHelp (IDS_CMDLINE_DUPLICATE, TEXT("%s"), aSWITCHES[ ii ].szSwitch);
138 // Woo hoo! Found what appears to be a valid switch. Parse it now.
140 aSWITCHES[ ii ].fPresent = TRUE;
141 pszCmdLine += lstrlen( aSWITCHES[ ii ].szSwitch );
142 while (iswhite (*pszCmdLine))
145 if (*pszCmdLine == TEXT(':'))
147 if (!aSWITCHES[ ii ].fGetsValue)
149 CommandLineHelp (IDS_CMDLINE_UNEXPECTVALUE, TEXT("%s"), aSWITCHES[ ii ].szSwitch);
152 for (++pszCmdLine; iswhite (*pszCmdLine); ++pszCmdLine)
156 if (aSWITCHES[ ii ].fGetsValue)
158 if ( (*pszCmdLine == TEXT('/')) || (*pszCmdLine == TEXT('\0')) )
160 CommandLineHelp (IDS_CMDLINE_MISSINGVAL, TEXT("%s"), aSWITCHES[ ii ].szSwitch);
163 BOOL fQuoted = FALSE;
164 for (LPTSTR pszTarget = aSWITCHES[ ii ].szValue;
165 *pszCmdLine && !(*pszCmdLine == TEXT('/') && !fQuoted)
166 && !(iswhite(*pszCmdLine) && !fQuoted); )
168 if (*pszCmdLine == TEXT('"'))
176 *pszTarget++ = *pszCmdLine++;
178 *pszTarget++ = TEXT('\0');
182 // Was the -CONFIRM switch given? It works with any other switch
185 if (aSWITCHES[ swCONFIRM ].fPresent)
187 Action_ShowConfirmations (TRUE);
190 // Now test the command-line for syntactical correctness.
191 // First test: if the SUBSET switch is given, the CELL switch must
194 if ( (aSWITCHES[ swSUBSET ].fPresent) &&
195 (!aSWITCHES[ swCELL ].fPresent) )
197 CommandLineHelp (IDS_CMDLINE_SUBSETNOTCELL);
201 // Similarly, if the SERVER switch is given, the CELL switch must
204 if ( (aSWITCHES[ swSERVER ].fPresent) &&
205 (!aSWITCHES[ swCELL ].fPresent) )
207 CommandLineHelp (IDS_CMDLINE_SERVERNOTCELL);
211 // And if the USER or PASSWORD switch is given, the other is required.
213 if ( (aSWITCHES[ swUSER ].fPresent && !aSWITCHES[ swPASSWORD ].fPresent) ||
214 (aSWITCHES[ swPASSWORD ].fPresent && !aSWITCHES[ swUSER ].fPresent) )
216 CommandLineHelp (IDS_CMDLINE_USERPASSWORD);
220 // Implement the command-line switches.
222 if (aSWITCHES[ swRESET ].fPresent)
224 if (aSWITCHES[ swSERVER ].fPresent)
226 ErasePreferences (aSWITCHES[ swCELL ].szValue, aSWITCHES[ swSERVER ].szValue);
228 else if (aSWITCHES[ swCELL ].fPresent)
230 ErasePreferences (aSWITCHES[ swCELL ].szValue);
232 else // neither cell nor server specified--kill just the general stuff
234 EraseSettings (REGSTR_SETTINGS_BASE, REGSTR_SETTINGS_PATH, REGVAL_SETTINGS);
237 Message (MB_OK | MB_ICONINFORMATION, IDS_CMDLINE_RESET_TITLE, IDS_CMDLINE_RESET_DESC);
241 if (aSWITCHES[ swUSER ].fPresent)
243 LPTSTR pszCell = (aSWITCHES[ swCELL ].fPresent) ? (aSWITCHES[ swCELL ].szValue) : NULL;
246 if (!AfsAppLib_SetCredentials (pszCell, aSWITCHES[ swUSER ].szValue, aSWITCHES[ swPASSWORD ].szValue, &status))
248 ImmediateErrorDialog (status, IDS_ERROR_BAD_CREDENTIALS);
253 if (aSWITCHES[ swLOOKUP ].fPresent)
255 return opLOOKUPERRORCODE;
258 if (aSWITCHES[ swUSEEXISTING ].fPresent)
261 TCHAR szDefCell[ cchNAME ];
263 if (aSWITCHES[ swCELL ].fPresent)
265 lstrcpy(szDefCell,aSWITCHES[ swCELL ].szValue);
269 AfsAppLib_GetLocalCell(szDefCell);
271 g.hCreds = AfsAppLib_GetCredentials(szDefCell,&ulStatus);
272 if (g.hCreds != NULL)
274 LPOPENCELL_PACKET lpocp = New (OPENCELL_PACKET);
276 memset(lpocp,0x00,sizeof(OPENCELL_PACKET));
277 lstrcpy(lpocp->szCell,szDefCell);
278 lpocp->fCloseAppOnFail = TRUE;
279 lpocp->hCreds = g.hCreds;
281 StartTask(taskOPENCELL,NULL,lpocp);
282 return opNOCELLDIALOG;
288 if (aSWITCHES[ swCELL ].fPresent)
290 LPOPENCELL_PACKET lpp = New (OPENCELL_PACKET);
291 memset (lpp, 0x00, sizeof(OPENCELL_PACKET));
293 lstrcpy (lpp->szCell, aSWITCHES[ swCELL ].szValue);
294 lpp->fCloseAppOnFail = TRUE;
296 if (aSWITCHES[ swSUBSET ].fPresent)
298 lpp->sub = Subsets_LoadSubset (lpp->szCell, aSWITCHES[ swSUBSET ].szValue);
299 if (lpp->sub == NULL)
304 CommandLineHelp (IDS_CMDLINE_INVALIDSUBSET, TEXT("%s%s"), aSWITCHES[ swCELL ].szValue, aSWITCHES[ swSUBSET ].szValue);
308 else if (aSWITCHES[ swSERVER ].fPresent)
310 lpp->sub = New (SUBSET);
311 memset (lpp->sub, 0x0, sizeof(SUBSET));
312 FormatMultiString (&lpp->sub->pszMonitored, TRUE, TEXT("%1"), TEXT("%s"), aSWITCHES[ swSERVER ].szValue);
315 StartTask (taskOPENCELL, NULL, lpp);
316 return opNOCELLDIALOG;
319 // Okay--nothing sufficiently special took place to prevent us
320 // from running the tool normally.
326 void cdecl CommandLineHelp (int ids, LPTSTR pszFormat, ...)
329 va_start (arg, pszFormat);
330 vMessage (MB_OK | MB_ICONHAND, IDS_CMDLINE_TITLE, ids, pszFormat, arg);