2003-12-12 04:21:32 +00:00
|
|
|
/*
|
|
|
|
* ac - an AppleCommander command line utility
|
|
|
|
* Copyright (C) 2002 by Robert Greene
|
|
|
|
* robgreene at users.sourceforge.net
|
2006-04-16 03:28:36 +00:00
|
|
|
* Copyright (C) 2003, 2004 by John B. Matthews
|
2008-05-16 21:23:19 +00:00
|
|
|
* matthewsj at users.sourceforge.net
|
2003-12-12 04:21:32 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
package com.webcodepro.applecommander.ui;
|
|
|
|
|
2006-04-16 03:28:36 +00:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2004-06-09 03:58:00 +00:00
|
|
|
import java.io.IOException;
|
2006-04-16 03:28:36 +00:00
|
|
|
import java.util.Iterator;
|
2004-06-09 03:58:00 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2003-12-12 04:21:32 +00:00
|
|
|
import com.webcodepro.applecommander.storage.DirectoryEntry;
|
|
|
|
import com.webcodepro.applecommander.storage.Disk;
|
|
|
|
import com.webcodepro.applecommander.storage.DiskFullException;
|
|
|
|
import com.webcodepro.applecommander.storage.FileEntry;
|
|
|
|
import com.webcodepro.applecommander.storage.FileFilter;
|
|
|
|
import com.webcodepro.applecommander.storage.FormattedDisk;
|
2006-04-16 03:28:36 +00:00
|
|
|
import com.webcodepro.applecommander.storage.FormattedDisk.DiskInformation;
|
|
|
|
import com.webcodepro.applecommander.storage.os.dos33.DosFormatDisk;
|
|
|
|
import com.webcodepro.applecommander.storage.os.pascal.PascalFormatDisk;
|
2004-06-09 03:58:00 +00:00
|
|
|
import com.webcodepro.applecommander.storage.os.prodos.ProdosFormatDisk;
|
2003-12-26 21:40:10 +00:00
|
|
|
import com.webcodepro.applecommander.storage.physical.ByteArrayImageLayout;
|
2006-04-16 03:28:36 +00:00
|
|
|
import com.webcodepro.applecommander.storage.physical.DosOrder;
|
2003-12-26 21:40:10 +00:00
|
|
|
import com.webcodepro.applecommander.storage.physical.ImageOrder;
|
|
|
|
import com.webcodepro.applecommander.storage.physical.ProdosOrder;
|
2008-05-18 18:58:13 +00:00
|
|
|
import com.webcodepro.applecommander.util.AppleUtil;
|
2004-07-11 15:07:56 +00:00
|
|
|
import com.webcodepro.applecommander.util.TextBundle;
|
2003-12-12 04:21:32 +00:00
|
|
|
|
|
|
|
public class ac {
|
2004-07-11 15:07:56 +00:00
|
|
|
private static TextBundle textBundle = UiBundle.getInstance();
|
2003-12-12 04:21:32 +00:00
|
|
|
|
2004-07-04 03:34:35 +00:00
|
|
|
public static void main(String[] args) {
|
2003-12-12 04:21:32 +00:00
|
|
|
try {
|
|
|
|
if (args.length == 0) {
|
|
|
|
help();
|
2008-05-17 13:48:58 +00:00
|
|
|
} else if ("-ls".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2006-04-16 03:28:36 +00:00
|
|
|
showDirectory(args[1], FormattedDisk.FILE_DISPLAY_STANDARD);
|
2008-05-17 13:48:58 +00:00
|
|
|
} else if ("-l".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2006-04-16 03:28:36 +00:00
|
|
|
showDirectory(args[1], FormattedDisk.FILE_DISPLAY_NATIVE);
|
2008-05-17 13:48:58 +00:00
|
|
|
} else if ("-ll".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2006-04-16 03:28:36 +00:00
|
|
|
showDirectory(args[1], FormattedDisk.FILE_DISPLAY_DETAIL);
|
2004-07-04 03:34:35 +00:00
|
|
|
} else if ("-e".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2003-12-12 04:21:32 +00:00
|
|
|
getFile(args[1], args[2], true);
|
2004-07-04 03:34:35 +00:00
|
|
|
} else if ("-g".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2003-12-12 04:21:32 +00:00
|
|
|
getFile(args[1], args[2], false);
|
2004-07-04 03:34:35 +00:00
|
|
|
} else if ("-p".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2008-05-19 02:43:43 +00:00
|
|
|
putFile(args[1], args[2], args[3],
|
|
|
|
(args.length > 4 ? args[4] : "0x2000"));
|
2004-07-04 03:34:35 +00:00
|
|
|
} else if ("-d".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2003-12-12 04:21:32 +00:00
|
|
|
deleteFile(args[1], args[2]);
|
2008-05-17 13:48:58 +00:00
|
|
|
} else if ("-i".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2006-04-16 03:28:36 +00:00
|
|
|
getDiskInfo(args[1]);
|
2008-05-17 13:48:58 +00:00
|
|
|
} else if ("-cc65".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
|
|
|
putCC65(args[1], args[2], args[3]);
|
|
|
|
} else if ("-dos140".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2006-04-22 04:13:01 +00:00
|
|
|
createDosDisk(args[1], Disk.APPLE_140KB_DISK);
|
2008-05-17 13:48:58 +00:00
|
|
|
} else if ("-pas140".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2006-04-22 04:13:01 +00:00
|
|
|
createPasDisk(args[1], args[2], Disk.APPLE_140KB_DISK);
|
2008-05-21 12:14:56 +00:00
|
|
|
} else if ("-pas800".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
|
|
|
createPasDisk(args[1], args[2], Disk.APPLE_800KB_DISK);
|
2008-05-17 13:48:58 +00:00
|
|
|
} else if ("-pro140".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2006-04-22 04:13:01 +00:00
|
|
|
createProDisk(args[1], args[2], Disk.APPLE_140KB_DISK);
|
2008-05-17 13:48:58 +00:00
|
|
|
} else if ("-pro800".equalsIgnoreCase(args[0])) { //$NON-NLS-1$
|
2006-04-22 04:13:01 +00:00
|
|
|
createProDisk(args[1], args[2], Disk.APPLE_800KB_DISK);
|
2003-12-12 04:21:32 +00:00
|
|
|
} else {
|
|
|
|
help();
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
2004-07-04 03:34:35 +00:00
|
|
|
System.err.println(textBundle.format("CommandLineErrorMessage", //$NON-NLS-1$
|
|
|
|
ex.getLocalizedMessage()));
|
2003-12-12 04:21:32 +00:00
|
|
|
ex.printStackTrace();
|
|
|
|
help();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put <stdin> into the file named fileName on the disk named imageName;
|
2006-04-16 03:28:36 +00:00
|
|
|
* Note: only volume level supported; input size unlimited.
|
2003-12-12 04:21:32 +00:00
|
|
|
*/
|
2008-05-18 21:03:25 +00:00
|
|
|
static void putFile(String imageName, String fileName, String fileType, String address)
|
2003-12-12 04:21:32 +00:00
|
|
|
throws IOException, DiskFullException {
|
|
|
|
|
2006-04-16 03:28:36 +00:00
|
|
|
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
|
|
|
byte[] inb = new byte[1024];
|
|
|
|
int byteCount = 0;
|
|
|
|
while ((byteCount = System.in.read(inb)) > 0) {
|
|
|
|
buf.write(inb, 0, byteCount);
|
|
|
|
}
|
2003-12-12 04:21:32 +00:00
|
|
|
Disk disk = new Disk(imageName);
|
|
|
|
FormattedDisk[] formattedDisks = disk.getFormattedDisks();
|
|
|
|
FormattedDisk formattedDisk = formattedDisks[0];
|
|
|
|
FileEntry entry = formattedDisk.createFile();
|
|
|
|
entry.setFilename(fileName);
|
|
|
|
entry.setFiletype(fileType);
|
2008-05-18 06:15:02 +00:00
|
|
|
entry.setFileData(buf.toByteArray());
|
2003-12-12 04:21:32 +00:00
|
|
|
if (entry.needsAddress()) {
|
|
|
|
entry.setAddress(stringToInt(address));
|
|
|
|
}
|
|
|
|
formattedDisk.save();
|
|
|
|
}
|
|
|
|
|
2008-05-17 13:48:58 +00:00
|
|
|
/**
|
|
|
|
* Put <stdin> into the file named fileName on the disk named imageName;
|
|
|
|
* Assume a cc65 style four-byte header with start address in bytes 0-1.
|
|
|
|
*/
|
2008-05-18 21:03:25 +00:00
|
|
|
static void putCC65(String imageName, String fileName, String fileType)
|
2008-05-17 13:48:58 +00:00
|
|
|
throws IOException, DiskFullException {
|
|
|
|
|
|
|
|
byte[] header = new byte[4];
|
2008-05-19 02:43:43 +00:00
|
|
|
if (System.in.read(header, 0, 4) == 4) {
|
|
|
|
int address = AppleUtil.getWordValue(header, 0);
|
|
|
|
putFile(imageName, fileName, fileType, Integer.toString(address));
|
|
|
|
}
|
2008-05-17 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
2003-12-12 04:21:32 +00:00
|
|
|
/**
|
|
|
|
* Delete the file named fileName from the disk named imageName.
|
|
|
|
*/
|
2008-05-18 21:03:25 +00:00
|
|
|
static void deleteFile(String imageName, String fileName) throws IOException {
|
2003-12-12 04:21:32 +00:00
|
|
|
Disk disk = new Disk(imageName);
|
|
|
|
FormattedDisk[] formattedDisks = disk.getFormattedDisks();
|
|
|
|
for (int i=0; i<formattedDisks.length; i++) {
|
|
|
|
FormattedDisk formattedDisk = formattedDisks[i];
|
|
|
|
List files = formattedDisk.getFiles();
|
|
|
|
FileEntry entry = getEntry(files, fileName);
|
|
|
|
if (entry != null) {
|
|
|
|
entry.delete();
|
|
|
|
disk.save();
|
|
|
|
} else {
|
2004-07-04 03:34:35 +00:00
|
|
|
System.err.println(textBundle.format(
|
|
|
|
"CommandLineNoMatchMessage", fileName)); //$NON-NLS-1$
|
2003-12-12 04:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file named filename from the disk named imageName;
|
|
|
|
* the file is filtered according to its type and sent to <stdout>.
|
|
|
|
*/
|
2008-05-18 21:03:25 +00:00
|
|
|
static void getFile(String imageName, String fileName, boolean filter) throws IOException {
|
2003-12-12 04:21:32 +00:00
|
|
|
Disk disk = new Disk(imageName);
|
|
|
|
FormattedDisk[] formattedDisks = disk.getFormattedDisks();
|
|
|
|
for (int i=0; i<formattedDisks.length; i++) {
|
|
|
|
FormattedDisk formattedDisk = formattedDisks[i];
|
|
|
|
List files = formattedDisk.getFiles();
|
|
|
|
FileEntry entry = getEntry(files, fileName);
|
|
|
|
if (entry != null) {
|
|
|
|
if (filter) {
|
|
|
|
FileFilter ff = entry.getSuggestedFilter();
|
|
|
|
byte[] buf = ff.filter(entry);
|
|
|
|
System.out.write(buf, 0, buf.length);
|
|
|
|
} else {
|
|
|
|
byte[] buf = entry.getFileData();
|
|
|
|
System.out.write(buf, 0, buf.length);
|
|
|
|
}
|
|
|
|
} else {
|
2004-07-04 03:34:35 +00:00
|
|
|
System.err.println(textBundle.format(
|
|
|
|
"CommandLineNoMatchMessage", fileName)); //$NON-NLS-1$
|
2003-12-12 04:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursive routine to locate a specific file by filename;
|
|
|
|
* In the instance of a system with directories (e.g. ProDOS),
|
|
|
|
* this really returns the first file with the given filename.
|
|
|
|
*/
|
|
|
|
static FileEntry getEntry(List files, String fileName) {
|
|
|
|
FileEntry theEntry = null;
|
|
|
|
if (files != null) {
|
|
|
|
for (int i=0; i<files.size(); i++) {
|
|
|
|
FileEntry entry = (FileEntry) files.get(i);
|
|
|
|
String entryName = entry.getFilename();
|
|
|
|
if (!entry.isDeleted() &&
|
|
|
|
fileName.equalsIgnoreCase(entryName)) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
if (entry.isDirectory()) {
|
|
|
|
theEntry = getEntry(((DirectoryEntry) entry).
|
|
|
|
getFiles(), fileName);
|
|
|
|
if (theEntry != null) {return theEntry;}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a directory listing of the disk named imageName.
|
|
|
|
*/
|
2006-04-16 03:28:36 +00:00
|
|
|
static void showDirectory(String imageName, int display) throws IOException {
|
2003-12-12 04:21:32 +00:00
|
|
|
Disk disk = new Disk(imageName);
|
|
|
|
FormattedDisk[] formattedDisks = disk.getFormattedDisks();
|
|
|
|
for (int i=0; i<formattedDisks.length; i++) {
|
|
|
|
FormattedDisk formattedDisk = formattedDisks[i];
|
|
|
|
System.out.println();
|
|
|
|
System.out.println(formattedDisk.getDiskName());
|
|
|
|
List files = formattedDisk.getFiles();
|
|
|
|
if (files != null) {
|
2006-04-16 03:28:36 +00:00
|
|
|
showFiles(files, "", display); //$NON-NLS-1$
|
2003-12-12 04:21:32 +00:00
|
|
|
}
|
2004-07-04 03:34:35 +00:00
|
|
|
System.out.println(textBundle.format(
|
|
|
|
"CommandLineStatus", //$NON-NLS-1$
|
|
|
|
new Object[] {
|
|
|
|
formattedDisk.getFormat(),
|
|
|
|
new Integer(formattedDisk.getFreeSpace()),
|
|
|
|
new Integer(formattedDisk.getUsedSpace())
|
|
|
|
}));
|
2003-12-12 04:21:32 +00:00
|
|
|
System.out.println();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursive routine to display directory entries.
|
|
|
|
* In the instance of a system with directories (e.g. ProDOS),
|
|
|
|
* this really returns the first file with the given filename.
|
|
|
|
*/
|
2006-04-16 03:28:36 +00:00
|
|
|
static void showFiles(List files, String indent, int display) {
|
2003-12-12 04:21:32 +00:00
|
|
|
for (int i=0; i<files.size(); i++) {
|
|
|
|
FileEntry entry = (FileEntry) files.get(i);
|
|
|
|
if (!entry.isDeleted()) {
|
2006-04-16 03:28:36 +00:00
|
|
|
List data = entry.getFileColumnData(display);
|
2003-12-12 04:21:32 +00:00
|
|
|
System.out.print(indent);
|
|
|
|
for (int d=0; d<data.size(); d++) {
|
|
|
|
System.out.print(data.get(d));
|
2004-07-04 03:34:35 +00:00
|
|
|
System.out.print(" "); //$NON-NLS-1$
|
2003-12-12 04:21:32 +00:00
|
|
|
}
|
|
|
|
System.out.println();
|
|
|
|
}
|
|
|
|
if (entry.isDirectory()) {
|
|
|
|
showFiles(((DirectoryEntry)entry).getFiles(),
|
2006-04-16 03:28:36 +00:00
|
|
|
indent + " ", display); //$NON-NLS-1$
|
2003-12-12 04:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-04-16 03:28:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display information about the given disk image.
|
|
|
|
*/
|
|
|
|
static void getDiskInfo(String imageName) throws IOException {
|
|
|
|
Disk disk = new Disk(imageName);
|
|
|
|
FormattedDisk[] formattedDisks = disk.getFormattedDisks();
|
|
|
|
for (int i=0; i<formattedDisks.length; i++) {
|
|
|
|
FormattedDisk formattedDisk = formattedDisks[i];
|
|
|
|
Iterator iterator = formattedDisk.getDiskInformation().iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
DiskInformation diskinfo = (DiskInformation) iterator.next();
|
|
|
|
System.out.println(diskinfo.getLabel() + ": " + diskinfo.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a DOS disk image.
|
|
|
|
*/
|
|
|
|
static void createDosDisk(String fileName, int imageSize)
|
|
|
|
throws IOException {
|
|
|
|
ByteArrayImageLayout layout = new ByteArrayImageLayout(imageSize);
|
|
|
|
ImageOrder imageOrder = new DosOrder(layout);
|
|
|
|
FormattedDisk[] disks =
|
|
|
|
DosFormatDisk.create(fileName, imageOrder);
|
|
|
|
disks[0].save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Pascal disk image.
|
|
|
|
*/
|
|
|
|
static void createPasDisk(String fileName, String volName, int imageSize)
|
|
|
|
throws IOException {
|
|
|
|
ByteArrayImageLayout layout = new ByteArrayImageLayout(imageSize);
|
|
|
|
ImageOrder imageOrder = new ProdosOrder(layout);
|
|
|
|
FormattedDisk[] disks =
|
|
|
|
PascalFormatDisk.create(fileName, volName, imageOrder);
|
|
|
|
disks[0].save();
|
|
|
|
}
|
2003-12-12 04:21:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a ProDOS disk image.
|
|
|
|
*/
|
2006-04-16 03:28:36 +00:00
|
|
|
static void createProDisk(String fileName, String volName, int imageSize)
|
2003-12-12 04:21:32 +00:00
|
|
|
throws IOException {
|
2003-12-26 21:40:10 +00:00
|
|
|
ByteArrayImageLayout layout = new ByteArrayImageLayout(imageSize);
|
|
|
|
ImageOrder imageOrder = new ProdosOrder(layout);
|
2003-12-12 04:21:32 +00:00
|
|
|
FormattedDisk[] disks =
|
2003-12-26 21:40:10 +00:00
|
|
|
ProdosFormatDisk.create(fileName, volName, imageOrder);
|
2003-12-12 04:21:32 +00:00
|
|
|
disks[0].save();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stringToInt(String s) {
|
|
|
|
int i = 0;
|
|
|
|
try {
|
2008-05-18 21:03:25 +00:00
|
|
|
s = s.trim().toLowerCase();
|
2008-05-22 17:30:27 +00:00
|
|
|
if (s.startsWith("$")) { // 6502, Motorola
|
2008-05-18 20:00:16 +00:00
|
|
|
i = Integer.parseInt(s.substring(1), 0x10);
|
2008-05-18 21:03:25 +00:00
|
|
|
} else if (s.startsWith("0x")) { // Java, C
|
|
|
|
i = Integer.parseInt(s.substring(2), 0x10);
|
2008-05-18 20:00:16 +00:00
|
|
|
} else {
|
|
|
|
i = Integer.parseInt(s);
|
|
|
|
}
|
|
|
|
} catch (NumberFormatException nfe) {
|
|
|
|
i = 0x2000;
|
2003-12-12 04:21:32 +00:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void help() {
|
2006-04-16 03:28:36 +00:00
|
|
|
System.err.println(textBundle.format("CommandLineHelp", AppleCommander.VERSION)); //$NON-NLS-1$
|
2003-12-12 04:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|