/src/c4group/c4group_cmdl.cpp
C++ | 580 lines | 410 code | 46 blank | 124 comment | 109 complexity | 37a974817288293658177809e031863a MD5 | raw file
Possible License(s): WTFPL, 0BSD, LGPL-2.1, CC-BY-3.0
- /*
- * OpenClonk, http://www.openclonk.org
- *
- * Copyright (c) 2002, 2006, 2008 Sven Eberhardt
- * Copyright (c) 2003-2004, 2007 Matthes Bender
- * Copyright (c) 2004, 2007-2008 Peter Wortmann
- * Copyright (c) 2005 G?nther Brammer
- * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
- *
- * Portions might be copyrighted by other authors who have contributed
- * to OpenClonk.
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- * See isc_license.txt for full license and disclaimer.
- *
- * "Clonk" is a registered trademark of Matthes Bender.
- * See clonk_trademark_license.txt for full license.
- */
- /* C4Group command line executable */
- // Version 1.0 November 1997
- // 1.1 November 1997
- // 1.2 February 1998
- // 1.3 March 1998
- // 1.4 April 1998
- // 1.5 May 1998
- // 1.6 November 1998
- // 1.7 December 1998
- // 1.8 February 1999
- // 1.9 May 1999
- // 2.0 June 1999
- // 2.6 March 2001
- // 2.7 June 2001
- // 2.8 June 2002
- // 4.95.0 November 2003
- // 4.95.4 July 2005 PORT/HEAD mixmax
- #include <C4Include.h>
- #include <C4ConfigShareware.h>
- #include <StdRegistry.h>
- #include <C4Group.h>
- #include <C4Version.h>
- #include <C4Update.h>
- #include <shellapi.h>
- #include <conio.h>
- int globalArgC;
- char **globalArgV;
- int iFirstCommand = -1;
- bool fQuiet = false;
- bool fRecursive = false;
- bool fRegisterShell = false;
- bool fUnregisterShell = false;
- bool fPromptAtEnd = false;
- char strExecuteAtEnd[_MAX_PATH + 1] = "";
- int iResult = 0;
- C4ConfigShareware Config;
- C4Config *GetCfg() { return &Config; }
- CDDrawCfg DDrawCfg; // to satisfy the linker
- #ifdef _WIN32
- #ifdef _DEBUG
- int dbg_printf(const char *strMessage, ...)
- {
- va_list args; va_start(args, strMessage);
- // Compose formatted message
- StdStrBuf Buf;
- Buf.FormatV(strMessage, args);
- // Log
- OutputDebugString(Buf.getData());
- return printf(Buf.getData());
- }
- #define printf dbg_printf
- #endif
- #endif
- bool ProcessGroup(const char *szFilename)
- {
- C4Group hGroup;
- int iArg;
- bool fDeleteGroup = false;
- hGroup.SetStdOutput(true);
- int argc = globalArgC;
- char **argv = globalArgV;
- // Current filename
- if (!fQuiet)
- printf("Group: %s\n",szFilename);
- // Open group file
- if (hGroup.Open(szFilename, true && Config.Registered()))
- {
- // No commands: display contents
- if (iFirstCommand<0)
- {
- if (Config.Registered())
- hGroup.View("*");
- }
- // Process commands
- else
- for (iArg=iFirstCommand; iArg<argc; iArg++)
- {
- // This argument is a command
- if (argv[iArg][0]=='-')
- {
- // Block unregistered commands
- if (!Config.Registered() && (SCharPos(argv[iArg][1], "yw") < 0))
- {
- printf("Command -%c not allowed in unregistered version: %s\n", argv[iArg][1], Config.GetRegistrationError());
- continue;
- }
- // Handle commands
- switch (argv[iArg][1])
- {
- // Add
- case 'a':
- if ((iArg+1>=argc) || (argv[iArg+1][0]=='-'))
- printf("Missing argument for add command\n");
- else
- {
- if ((argv[iArg][2]=='s') || (argv[iArg][2] && (argv[iArg][3]=='s')) )
- {
- if ((iArg+2>=argc) || (argv[iArg+2][0]=='-'))
- printf("Missing argument for add as command\n");
- else
- { hGroup.Add(argv[iArg+1],argv[iArg+2]); iArg+=2; }
- }
- else
- #ifdef _WIN32
- { hGroup.Add(argv[iArg+1]); iArg++; }
- #else
- { hGroup.Add(argv[iArg+1], argv[iArg+1]); iArg++; }
- #endif
- }
- break;
- // Move
- case 'm':
- if ((iArg+1>=argc) || (argv[iArg+1][0]=='-'))
- printf("Missing argument for move command\n");
- else
- #ifdef _WIN32
- { hGroup.Move(argv[iArg+1]); iArg++; }
- #else
- { hGroup.Move(argv[iArg+1], argv[iArg+1]); iArg++; }
- #endif
- break;
- // Extract
- case 'e':
- if ((iArg+1>=argc) || (argv[iArg+1][0]=='-'))
- printf("Missing argument for extract command\n");
- else
- {
- if ((argv[iArg][2]=='t') || (argv[iArg][2] && (argv[iArg][3]=='s')) )
- {
- if ((iArg+2>=argc) || (argv[iArg+2][0]=='-'))
- printf("Missing argument for extract as command\n");
- else
- { hGroup.Extract(argv[iArg+1],argv[iArg+2]); iArg+=2; }
- }
- else
- { hGroup.Extract(argv[iArg+1]); iArg++; }
- }
- break;
- // Delete
- case 'd':
- if ((iArg+1>=argc) || (argv[iArg+1][0]=='-'))
- printf("Missing argument for delete command\n");
- else
- { hGroup.Delete(argv[iArg+1], fRecursive); iArg++; }
- break;
- // Sort
- case 's':
- // First sort parameter overrides default Clonk sort list
- C4Group_SetSortList(NULL);
- // Missing argument
- if ((iArg+1>=argc) || (argv[iArg+1][0]=='-'))
- printf("Missing argument for sort command\n");
- // Sort, advance to next argument
- else
- { hGroup.Sort(argv[iArg+1]); iArg++; }
- break;
- // Rename
- case 'r':
- if ((iArg+2>=argc) || (argv[iArg+1][0]=='-') || (argv[iArg+2][0]=='-'))
- printf("Missing argument(s) for rename command\n");
- else
- { hGroup.Rename(argv[iArg+1],argv[iArg+2]); iArg+=2; }
- break;
- // View
- case 'v':
- if ((iArg+1>=argc) || (argv[iArg+1][0]=='-'))
- { hGroup.View("*"); }
- else
- { hGroup.View(argv[iArg+1]); iArg++; }
- break;
- // Make original
- case 'o':
- hGroup.MakeOriginal(true);
- break;
- // Pack
- case 'p':
- printf("Packing...\n");
- // Close
- if (!hGroup.Close()) printf("Closing failed: %s\n",hGroup.GetError());
- // Pack
- else if (!C4Group_PackDirectory(szFilename)) printf("Pack failed\n");
- // Reopen
- else if (!hGroup.Open(szFilename)) printf("Reopen failed: %s\n",hGroup.GetError());
- break;
- // Unpack
- case 'u':
- printf("Unpacking...\n");
- // Close
- if (!hGroup.Close()) printf("Closing failed: %s\n",hGroup.GetError());
- // Unpack
- else if (!C4Group_UnpackDirectory(szFilename)) printf("Unpack failed\n");
- // Reopen
- else if (!hGroup.Open(szFilename)) printf("Reopen failed: %s\n",hGroup.GetError());
- break;
- // Unpack
- case 'x':
- printf("Exploding...\n");
- // Close
- if (!hGroup.Close()) printf("Closing failed: %s\n",hGroup.GetError());
- // Explode
- else if (!C4Group_ExplodeDirectory(szFilename)) printf("Unpack failed\n");
- // Reopen
- else if (!hGroup.Open(szFilename)) printf("Reopen failed: %s\n",hGroup.GetError());
- break;
- // Print maker
- case 'k':
- printf("%s\n",hGroup.GetMaker());
- break;
- // Generate update
- case 'g':
- if ((iArg + 3 >= argc) || (argv[iArg+1][0] == '-') || (argv[iArg+2][0] == '-') || (argv[iArg+3][0] == '-'))
- printf("Update generation failed: too few arguments\n");
-