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>
22 * VARIABLES __________________________________________________________________
38 TCHAR szSwitch[ cchRESOURCE ];
41 TCHAR szValue[ cchRESOURCE ];
43 { TEXT("cell"), TRUE },
44 { TEXT("subset"), TRUE },
45 { TEXT("server"), TRUE },
46 { TEXT("reset"), FALSE },
47 { TEXT("confirm"), FALSE },
48 { TEXT("user"), TRUE },
49 { TEXT("password"), TRUE },
50 { TEXT("lookup"), FALSE }
53 #define nSWITCHES (sizeof(aSWITCHES) / sizeof(aSWITCHES[0]))
57 * PROTOTYPES _________________________________________________________________
61 #define iswhite(_ch) ( ((_ch)==TEXT(' ')) || ((_ch)==TEXT('\t')) )
63 void cdecl CommandLineHelp (int ids, LPTSTR pszFormat = NULL, ...);
67 * ROUTINES ___________________________________________________________________
71 CMDLINEOP ParseCommandLine (LPTSTR pszCmdLine)
73 for (size_t ii = 0; ii < nSWITCHES; ++ii)
74 aSWITCHES[ ii ].fPresent = FALSE;
76 // Search through pszCmdLine for switches; each switch must be
77 // preceeded by "/" or "-".
79 while (pszCmdLine && *pszCmdLine)
81 while (iswhite(*pszCmdLine))
86 if ( (*pszCmdLine != '-') && (*pszCmdLine != '/') )
88 CommandLineHelp (IDS_CMDLINE_SYNTAX);
94 // Okay, we've found what is probably the start of a switch.
95 // See if it matches anything.
97 for (ii = 0; ii < nSWITCHES; ++ii)
99 size_t cch = lstrlen(aSWITCHES[ ii ].szSwitch);
100 if (lstrncmpi (pszCmdLine, aSWITCHES[ ii ].szSwitch, cch))
103 // If the switch wants a value, it must be followed by ":"
104 // or whitespace; if it doesn't, it must be followed by "/"
107 LPTSTR pszAfter = &pszCmdLine[ cch ];
108 if (iswhite (*pszAfter) || (!*pszAfter) ||
109 ((*pszAfter == TEXT(':')) && (aSWITCHES[ ii ].fGetsValue)) ||
110 ((*pszAfter == TEXT('/')) && (!aSWITCHES[ ii ].fGetsValue)) )
112 break; // found a switch!
118 TCHAR szCopy[ cchRESOURCE ];
119 lstrcpy (szCopy, pszCmdLine);
120 for (LPTSTR pch = szCopy;
121 *pch && !iswhite(*pch) && !(*pch == TEXT('/')) && !(*pch == TEXT(':'));
125 CommandLineHelp (IDS_CMDLINE_UNRECOGNIZED, TEXT("%s"), szCopy);
128 if (aSWITCHES[ ii ].fPresent)
130 CommandLineHelp (IDS_CMDLINE_DUPLICATE, TEXT("%s"), aSWITCHES[ ii ].szSwitch);
134 // Woo hoo! Found what appears to be a valid switch. Parse it now.
136 aSWITCHES[ ii ].fPresent = TRUE;
137 pszCmdLine += lstrlen( aSWITCHES[ ii ].szSwitch );
138 while (iswhite (*pszCmdLine))
141 if (*pszCmdLine == TEXT(':'))
143 if (!aSWITCHES[ ii ].fGetsValue)
145 CommandLineHelp (IDS_CMDLINE_UNEXPECTVALUE, TEXT("%s"), aSWITCHES[ ii ].szSwitch);
148 for (++pszCmdLine; iswhite (*pszCmdLine); ++pszCmdLine)
152 if (aSWITCHES[ ii ].fGetsValue)
154 if ( (*pszCmdLine == TEXT('/')) || (*pszCmdLine == TEXT('\0')) )
156 CommandLineHelp (IDS_CMDLINE_MISSINGVAL, TEXT("%s"), aSWITCHES[ ii ].szSwitch);
159 BOOL fQuoted = FALSE;
160 for (LPTSTR pszTarget = aSWITCHES[ ii ].szValue;
161 *pszCmdLine && !(*pszCmdLine == TEXT('/') && !fQuoted)
162 && !(iswhite(*pszCmdLine) && !fQuoted); )
164 if (*pszCmdLine == TEXT('"'))
172 *pszTarget++ = *pszCmdLine++;
174 *pszTarget++ = TEXT('\0');
178 // Was the -CONFIRM switch given? It works with any other switch
181 if (aSWITCHES[ swCONFIRM ].fPresent)
183 Action_ShowConfirmations (TRUE);
186 // Now test the command-line for syntactical correctness.
187 // First test: if the SUBSET switch is given, the CELL switch must
190 if ( (aSWITCHES[ swSUBSET ].fPresent) &&
191 (!aSWITCHES[ swCELL ].fPresent) )
193 CommandLineHelp (IDS_CMDLINE_SUBSETNOTCELL);
197 // Similarly, if the SERVER switch is given, the CELL switch must
200 if ( (aSWITCHES[ swSERVER ].fPresent) &&
201 (!aSWITCHES[ swCELL ].fPresent) )
203 CommandLineHelp (IDS_CMDLINE_SERVERNOTCELL);
207 // And if the USER or PASSWORD switch is given, the other is required.
209 if ( (aSWITCHES[ swUSER ].fPresent && !aSWITCHES[ swPASSWORD ].fPresent) ||
210 (aSWITCHES[ swPASSWORD ].fPresent && !aSWITCHES[ swUSER ].fPresent) )
212 CommandLineHelp (IDS_CMDLINE_USERPASSWORD);
216 // Implement the command-line switches.
218 if (aSWITCHES[ swRESET ].fPresent)
220 if (aSWITCHES[ swSERVER ].fPresent)
222 ErasePreferences (aSWITCHES[ swCELL ].szValue, aSWITCHES[ swSERVER ].szValue);
224 else if (aSWITCHES[ swCELL ].fPresent)
226 ErasePreferences (aSWITCHES[ swCELL ].szValue);
228 else // neither cell nor server specified--kill just the general stuff
230 EraseSettings (REGSTR_SETTINGS_BASE, REGSTR_SETTINGS_PATH, REGVAL_SETTINGS);
233 Message (MB_OK | MB_ICONINFORMATION, IDS_CMDLINE_RESET_TITLE, IDS_CMDLINE_RESET_DESC);
237 if (aSWITCHES[ swUSER ].fPresent)
239 LPTSTR pszCell = (aSWITCHES[ swCELL ].fPresent) ? (aSWITCHES[ swCELL ].szValue) : NULL;
242 if (!AfsAppLib_SetCredentials (pszCell, aSWITCHES[ swUSER ].szValue, aSWITCHES[ swPASSWORD ].szValue, &status))
244 ImmediateErrorDialog (status, IDS_ERROR_BAD_CREDENTIALS);
249 if (aSWITCHES[ swLOOKUP ].fPresent)
251 return opLOOKUPERRORCODE;
254 if (aSWITCHES[ swCELL ].fPresent)
256 LPOPENCELL_PACKET lpp = New (OPENCELL_PACKET);
257 memset (lpp, 0x00, sizeof(OPENCELL_PACKET));
259 lstrcpy (lpp->szCell, aSWITCHES[ swCELL ].szValue);
260 lpp->fCloseAppOnFail = TRUE;
262 if (aSWITCHES[ swSUBSET ].fPresent)
264 lpp->sub = Subsets_LoadSubset (lpp->szCell, aSWITCHES[ swSUBSET ].szValue);
265 if (lpp->sub == NULL)
270 CommandLineHelp (IDS_CMDLINE_INVALIDSUBSET, TEXT("%s%s"), aSWITCHES[ swCELL ].szValue, aSWITCHES[ swSUBSET ].szValue);
274 else if (aSWITCHES[ swSERVER ].fPresent)
276 lpp->sub = New (SUBSET);
277 memset (lpp->sub, 0x0, sizeof(SUBSET));
278 FormatMultiString (&lpp->sub->pszMonitored, TRUE, TEXT("%1"), TEXT("%s"), aSWITCHES[ swSERVER ].szValue);
281 StartTask (taskOPENCELL, NULL, lpp);
282 return opNOCELLDIALOG;
285 // Okay--nothing sufficiently special took place to prevent us
286 // from running the tool normally.
292 void cdecl CommandLineHelp (int ids, LPTSTR pszFormat, ...)
295 va_start (arg, pszFormat);
296 vMessage (MB_OK | MB_ICONHAND, IDS_CMDLINE_TITLE, ids, pszFormat, arg);