Has been moved to src/tests/kernel/boot_floppy.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@11942 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2005-03-21 18:02:15 +00:00
parent ea0bca7cd2
commit f7a818f980
65 changed files with 0 additions and 6133 deletions

View File

@ -1,39 +0,0 @@
SubDir OBOS_TOP src kernel apps ;
KernelObjects
exec_test.c
false_main.c
fibo_main.c
fork_test.c
init.c
monitor_test.c
pipe_test.c
true_main.c
sig_test.c
select_test.c
tls_test.c
on_exit_thread.c
;
UsePrivateHeaders kernel ;
# UsePrivateHeaders [ FDirName kernel arch ] ;
UsePrivateHeaders [ FDirName kernel arch $(OBOS_ARCH) ] ;
UsePrivateHeaders [ FDirName kernel boot platform $(OBOS_BOOT_PLATFORM) ] ;
if $(OBOS_ARCH) = x86 {
SubInclude OBOS_TOP src kernel apps cpuinfo ;
}
SubInclude OBOS_TOP src kernel apps echo ;
SubInclude OBOS_TOP src kernel apps filetest ;
SubInclude OBOS_TOP src kernel apps fortune ;
SubInclude OBOS_TOP src kernel apps hostname ;
SubInclude OBOS_TOP src kernel apps ls ;
SubInclude OBOS_TOP src kernel apps symlink ;
SubInclude OBOS_TOP src kernel apps ps ;
SubInclude OBOS_TOP src kernel apps shell ;
SubInclude OBOS_TOP src kernel apps envtest ;
#SubInclude OBOS_TOP src kernel apps sockettest ;
SubInclude OBOS_TOP src kernel apps testapp ;
SubInclude OBOS_TOP src kernel apps tests ;
SubInclude OBOS_TOP src kernel apps uname ;

View File

@ -1,20 +0,0 @@
SubDir OBOS_TOP src kernel apps cpuinfo ;
KernelObjects
<$(SOURCE_GRIST)>main.c
:
-fpic
;
KernelLd cpuinfo :
libglue2.o
<$(SOURCE_GRIST)>main.o
libroot.so
:
$(OBOS_TOP)/src/kernel/ldscripts/$(OBOS_ARCH)/app.ld
:
:
:
bin/cpuinfo
;

View File

@ -1,105 +0,0 @@
// cache-type codes
enum cache_types {
Unused,
Inst_TLB,
Data_TLB,
L1_Inst_Cache,
L1_Data_Cache,
L2_Cache,
L3_Cache,
No_L2_Or_L3,
Trace_Cache
};
// an entry in the TLB/Cache table
typedef struct {
uint8 descriptor;
uint8 cache_type;
char text[70];
} tlbc_info;
//
// the global TLB/Cache table
// technical reference:
// "Intel Processor Identification and the CPUID instruction"
// (ftp://ftp.intel.com/docs/24161821.pdf)
// Page 18
static tlbc_info
Intel_TLB_Cache_Table[] = {
{0x0, 0, ""},
{0x1, Inst_TLB, "4KB pages, 4-way set associative, 32 entries"},
{0x2, Inst_TLB, "4MB pages, fully associative, 2 entries"},
{0x50, Inst_TLB, "4KB, 2MB or 4MB pages, fully associative, 64 entries"},
{0x51, Inst_TLB, "4KB, 2MB or 4MB pages, fully associative, 128 entries"},
{0x52, Inst_TLB, "4KB, 2MB or 4MB pages, fully associative, 256 entries"},
{0x3, Data_TLB, "4KB pages, 4-way set associative, 64 entries"},
{0x4, Data_TLB, "4MB pages, 4-way set associative, 8 entries"},
{0x5b, Data_TLB, "4KB or 4MB pages, fully associative, 64 entries"},
{0x5c, Data_TLB, "4KB or 4MB pages, fully associative, 128 entries"},
{0x5d, Data_TLB, "4KB or 4MB pages, fully associative, 256 entries"},
{0x6, L1_Inst_Cache, "8KB pages, 4-way set associative, 32 byte line size"},
{0x8, L1_Inst_Cache, "16KB pages, 4-way set associative, 32 byte line size"},
{0xa, L1_Data_Cache, "8KB pages, 2-way set associative, 32 byte line size"},
{0xc, L1_Data_Cache, "16KB pages, 4-way set associative, 32 byte line size"},
{0x66, L1_Data_Cache, "8KB pages, 4-way set associative, 64 byte line size"},
{0x67, L1_Data_Cache, "16KB pages, 4-way set associative, 64 byte line size"},
{0x68, L1_Data_Cache, "32KB pages, 4-way set associative, 64 byte line size"},
{0x39, L2_Cache, "128KB pages, 4-way set associative, sectored, 64 byte line size"},
{0x3c, L2_Cache, "256KB pages, 4-way set associative, sectored, 64 byte line size"},
{0x41, L2_Cache, "128KB pages, 4-way set associative, 32 byte line size"},
{0x42, L2_Cache, "256KB pages, 4-way set associative, 32 byte line size"},
{0x43, L2_Cache, "512KB pages, 4-way set associative, 32 byte line size"},
{0x44, L2_Cache, "1MB pages, 4-way set associative, 32 byte line size"},
{0x45, L2_Cache, "2MB pages, 4-way set associative, 32 byte line size"},
{0x79, L2_Cache, "128KB pages, 8-way set associative, sectored, 64 byte line size"},
{0x7a, L2_Cache, "256KB pages, 8-way set associative, sectored, 64 byte line size"},
{0x7b, L2_Cache, "512KB pages, 8-way set associative, sectored, 64 byte line size"},
{0x7c, L2_Cache, "1MB pages, 8-way set associative, sectored, 64 byte line size"},
{0x82, L2_Cache, "256KB pages, 8-way set associative, 32 byte line size"},
{0x83, L2_Cache, "512KB pages, 8-way set associative, 32 byte line size"},
{0x84, L2_Cache, "1MB pages, 8-way set associative, 32 byte line size"},
{0x85, L2_Cache, "2MB pages, 8-way set associative, 32 byte line size"},
{0x40, No_L2_Or_L3, "No 2nd-level cache, or if 2nd-level cache exists, no 3rd-level cache\n"},
{0x22, L3_Cache, "512KB pages, 4-way set associative, sectored, 64 byte line size"},
{0x23, L3_Cache, "1MB pages, 8-way set associative, sectored, 64 byte line size"},
{0x25, L3_Cache, "2MB pages, 8-way set associative, sectored, 64 byte line size"},
{0x29, L3_Cache, "4MB pages, 8-way set associative, sectored, 64 byte line size"},
{0x70, Trace_Cache, "Trace cache: 12K-uops, 8-way set associative\n"},
{0x71, Trace_Cache, "Trace cache: 16K-uops, 8-way set associative\n"},
{0x72, Trace_Cache, "Trace cache: 32K-uops, 8-way set associative\n"},
};
void AMD_identify (cpuid_info *);
void AMD_features (cpuid_info *);
void AMD_TLB_cache (cpuid_info *);
void Cyrix_identify (cpuid_info *);
void Cyrix_features (cpuid_info *);
void Cyrix_TLB_cache (cpuid_info *);
void Intel_identify (cpuid_info *);
void Intel_features (cpuid_info *);
void Intel_TLB_cache (cpuid_info *);
const char *Intel_brand_string (int, int);
void dump_regs (cpuid_info *, const char *, uint32, uint32);
char getoption (char *);
void insert (int *, int, int);
void opt_identify (cpuid_info *, int);
void opt_features (cpuid_info *, int);
void opt_TLB_cache (cpuid_info *, int);
void opt_dump_calls (cpuid_info *, uint32);
void print_regs (cpuid_info *);
void usage (void);

View File

@ -1,178 +0,0 @@
static char *Intel_feature_flags[32] = {
"FPU Floating Point Unit",
"VME Virtual 8086 Mode Enhancements",
"DE Debugging Extensions",
"PSE Page Size Extensions",
"TSC Time Stamp Counter",
"MSR Model Specific Registers",
"PAE Physical Address Extension",
"MCE Machine Check Exception",
"CX8 COMPXCHG8B Instruction",
"APIC On-chip Advanced Programmable Interrupt Controller",
"10 Reserved",
"SEP Fast System Call",
"MTRR Memory Type Range Registers",
"PGE PTE Global Flag",
"MCA Machine Check Architecture",
"CMOV Conditional Move and Compare Instructions",
"FGPAT Page Attribute Table",
"PSE-36 36-bit Page Size Extension",
"PN Processor Serial Number present and enabled",
"CLFSH CFLUSH instruction",
"20 reserved",
"DS Debug store",
"ACPI Thermal Monitor and Clock Ctrl",
"MMX MMX instruction set",
"FXSR Fast FP/MMX Streaming SIMD Extensions save/restore",
"SSE Streaming SIMD Extensions instruction set",
"SSE2 SSE2 extensions",
"SS Self Snoop",
"HT Hyper Threading",
"TM Thermal monitor",
"30 reserved",
"31 reserved"
};
#if 0
static char *AMD_feature_flags[32] = {
"Floating Point Unit",
"Virtual Mode Extensions",
"Debugging Extensions",
"Page Size Extensions",
"Time Stamp Counter (with RDTSC and CR4 disable bit)",
"Model Specific Registers with RDMSR & WRMSR",
"PAE - Page Address Extensions",
"Machine Check Exception",
"COMPXCHG8B Instruction",
"APIC",
"10 - Reserved",
"SYSCALL/SYSRET or SYSENTER/SYSEXIT instructions",
"MTRR - Memory Type Range Registers",
"Global paging extension",
"Machine Check Architecture",
"Conditional Move Instruction",
"PAT - Page Attribute Table",
"PSE-36 - Page Size Extensions",
"18 - reserved",
"19 - reserved",
"20 - reserved",
"21 - reserved",
"AMD MMX Instruction Extensions",
"MMX instructions",
"FXSAVE/FXRSTOR",
"25 - reserved",
"26 - reserved",
"27 - reserved",
"28 - reserved",
"29 - reserved",
"3DNow! Instruction Extensions",
"3DNow instructions"
};
static char *Cyrix_standard_feature_flags_5[32] = {
"FPU Floating Point Unit",
"V86 Virtual Mode Extensions",
"Debug Extension",
"4MB Page Size",
"Time Stamp Counter",
"RDMSR/WRMSR (Model Specific Registers)",
"PAE",
"Machine Check Exception",
"COMPXCHG8B Instruction",
"APIC - On-chip Advanced Programmable Interrupt Controller",
"10 - Reserved",
"11 - Reserved",
"MTRR Memory Type Range Registers",
"13 - reserved",
"Machine Check",
"CMOV Conditional Move Instruction",
"16 - reserved",
"17 - reserved",
"18 - reserved",
"19 - reserved",
"20 - reserved",
"21 - reserved",
"22 - reserved",
"MMX instructions",
"24 - reserved",
"25 - reserved",
"26 - reserved",
"27 - reserved",
"28 - reserved",
"29 - reserved",
"30 - reserved"
};
static char *Cyrix_standard_feature_flags_not5[32] = {
"FPU Floating Point Unit",
"V86 Virtual Mode Extensions",
"Debug Extension",
"4MB Page Size",
"Time Stamp Counter",
"RDMSR/WRMSR (Model Specific Registers)",
"PAE",
"Machine Check Exception",
"COMPXCHG8B Instruction",
"APIC - On-chip Advanced Programmable Interrupt Controller",
"10 - Reserved",
"11 - Reserved",
"MTRR Memory Type Range Registers",
"Global Paging Extension",
"Machine Check",
"CMOV Conditional Move Instruction",
"16 - reserved",
"17 - reserved",
"18 - reserved",
"19 - reserved",
"20 - reserved",
"21 - reserved",
"22 - reserved",
"MMX instructions",
"24 - reserved",
"25 - reserved",
"26 - reserved",
"27 - reserved",
"28 - reserved",
"29 - reserved",
"30 - reserved"
};
static char *Cyrix_extended_feature_flags[32] = {
"FPU Floating Point Unit",
"V86 Virtual Mode Extensions",
"Debug Extension",
"Page Size Extensions",
"Time Stamp Counter",
"Cyrix MSR",
"PAE",
"MC Exception",
"COMPXCHG8B",
"APIC on chip",
"SYSCALL/SYSRET",
"11 - reserved",
"MTRR",
"Global bit",
"Machine Check",
"CMOV",
"FPU CMOV",
"17 - reserved",
"18 - reserved",
"19 - reserved",
"20 - reserved",
"21 - reserved",
"22 - reserved",
"MMX",
"Extended MMX",
"25 - reserved",
"26 - reserved",
"27 - reserved",
"28 - reserved",
"29 - reserved",
"30 - reserved",
"3DNow instructions"
};
#endif

View File

@ -1,663 +0,0 @@
#include <OS.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpuinfo.h"
#include "flag_arrays.c"
extern char *__progname;
static cpuid_info CPU_Data;
void
usage()
{
printf("usage: %s [-option]\n\n"
"Prints information about your CPU.\n"
" -i CPU identification\n"
" -f supported processor features\n"
" -t TLB and cache info\n"
" -d dump registers from CPUID calls\n", __progname);
exit(0);
}
char
getoption(char *optstr)
{
if ((optstr[0] != '-')
|| (strlen(optstr) > 2)
|| (strchr("iftd", optstr[1]) == NULL))
// invalid option
usage();
return optstr[1];
}
int
main(int argc, char *argv[])
{
// this program prints out everything you would ever want to know about
// your computer's processor, or your money back, in full (30 days, no hassle)
//
// technical reference:
// "Intel Processor Identification and the CPUID Instruction"
// (ftp://download.intel.com/design/Xeon/applnots/24161821.pdf)
const char *no_support = "Sorry, your processor does not support this feature\n";
char option = 0;
int vendor_tag;
uint32 max_level;
cpuid_info *info = &CPU_Data;
if (argc == 2)
option = getoption(argv[1]);
else
usage();
// get initial info (max_level and vendor_tag)
get_cpuid(info, 0, 0);
max_level = info->eax_0.max_eax;
vendor_tag = info->regs.ebx;
switch (option) {
case 'i':
printf("CPU identification:\n\n");
if (max_level < 1)
printf(no_support);
else
opt_identify(info, vendor_tag);
break;
case 'f':
printf("Supported processor features:\n\n");
if (max_level < 1)
printf(no_support);
else
opt_features(info, vendor_tag);
break;
case 't':
printf("TLB and cache information:\n\n");
if (max_level < 2)
printf(no_support);
else
opt_TLB_cache(info, vendor_tag);
break;
case 'd':
printf("CPUID instruction call dump:\n\n");
opt_dump_calls(info, max_level);
break;
}
return 0;
}
void
opt_identify(cpuid_info *info, int vendor_tag)
{
// gosh, these CPU manufacturers have really outdone
// themselves on creating cutesy vendorID strings, no?
// (that was sarcasm, btw...)
char vendorID[12+1] = {0};
// the 'vendorid' field of the info struct is not null terminated,
// so it is copied into a properly terminated local string buffer
memcpy(vendorID, info->eax_0.vendor_id, 12);
printf("%12s '%s'\n", "Vendor ID:", vendorID);
switch (vendor_tag) {
case 'uneG': // "GenuineIntel"
Intel_identify(info);
break;
case 'htuA': // "AuthenticAMD"
AMD_identify(info);
break;
case 'iryC': // "CyrixInstead"
Cyrix_identify(info);
break;
}
}
void
opt_features(cpuid_info *info, int vendor_tag)
{
switch (vendor_tag) {
case 'uneG': // "GenuineIntel"
Intel_features(info);
break;
case 'htuA': // "AuthenticAMD"
AMD_features(info);
break;
case 'iryC': // "CyrixInstead"
Cyrix_features(info);
break;
}
}
void
opt_TLB_cache(cpuid_info *info, int vendor_tag)
{
switch (vendor_tag) {
case 'uneG': // "GenuineIntel"
Intel_TLB_cache(info);
break;
case 'htuA': // "AuthenticAMD"
AMD_TLB_cache(info);
break;
case 'iryC': // "CyrixInstead"
Cyrix_TLB_cache(info);
break;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Intel specs
void
Intel_identify(cpuid_info *info)
{
// the code for this function looks very boring and tedious
// (that's because it's very boring and tedious)
// but I think it's correct anyway (there's always that possibility!)
int type, family, model, sig;
get_cpuid(info, 1, 0);
type = info->eax_1.type;
family = info->eax_1.family;
model = info->eax_1.model;
sig = info->regs.eax;
printf("%12s 0x%08X\n", "Signature:", sig);
printf("%12s %2d '", "Type:", type);
if (type == 0) printf("Original OEM");
else if (type == 1) printf("Overdrive");
else if (type == 2) printf("Dual-capable");
else if (type == 3) printf("Reserved");
printf("'\n");
printf("%12s %2d '", "Family:", family);
if (family == 3) printf("i386");
else if (family == 4) printf("i486");
else if (family == 5) printf("Pentium");
else if (family == 6) printf("Pentium Pro");
else if (family == 15) printf("Pentium 4");
printf("'\n");
if (family == 15) {
int efamily = (info->regs.eax >> 20) & 0xff;
printf("Extended family: %d\n", efamily);
}
printf("%12s %2d '", "Model:", model);
switch (family) {
case 3:
break;
case 4:
if (model == 0) printf("DX");
else if (model == 1) printf("DX");
else if (model == 2) printf("SX");
else if (model == 3) printf("487/DX2");
else if (model == 4) printf("SL");
else if (model == 5) printf("SX2");
else if (model == 7) printf("write-back enhanced DX2");
else if (model == 8) printf("DX4");
break;
case 5:
if (model == 1) printf("60/66");
else if (model == 2) printf("75-200");
else if (model == 3) printf("for 486 system");
else if (model == 4) printf("MMX");
break;
case 6:
if (model == 1) printf("Pentium Pro");
else if (model == 3) printf("Pentium II Model 3");
else if (model == 5) printf("Pentium II Model 5/Xeon/Celeron");
else if (model == 6) printf("Celeron");
else if (model == 7) printf("Pentium III/Pentium III Xeon - external L2 cache");
else if (model == 8) printf("Pentium III/Pentium III Xeon - internal L2 cache");
break;
case 15:
break;
}
printf("'\n");
if (model == 15) {
int emodel = (info->regs.eax >> 16) & 0xf;
printf("Extended model: %d\n", emodel);
}
printf("%12s 0x%08lX\n", "Features:", info->eax_1.features);
{
int id = info->regs.ebx & 0xff;
printf("%12s %2d '%s'\n", "Brand ID:", id, Intel_brand_string(id, sig));
}
printf("%12s %2d\n", "Stepping:", info->eax_1.stepping);
printf("%12s %2d\n", "Reserved:", info->eax_1.reserved_0);
get_cpuid(info, 0x80000000, 0);
if (info->regs.eax & 0x80000000) {
// extended feature/signature bits supported
if (info->regs.eax >= 0x80000004) {
uint32 i;
printf("\nExtended brand string:\n'");
for (i = 0x80000002; i <= 0x80000004; ++i) {
get_cpuid(info, i, 0);
print_regs(info);
}
printf("'\n");
}
}
}
const char *
Intel_brand_string(int id, int processor_signature)
{
int sig = processor_signature;
switch (id) {
case 0x0: return "Unsupported";
case 0x1: return "Intel(R) Celeron(R) processor";
case 0x2: return "Intel(R) Pentium(R) III processor";
case 0x3: return (sig == 0x6B1) ? "Intel(R) Celeron(R) processor" : "Intel(R) Pentium(R) III Xeon(TM) processor";
case 0x4: return "Intel(R) Pentium(R) III processor";
case 0x5: return "";
case 0x6: return "Mobile Intel(R) Pentium(R) III Processor-M";
case 0x7: return "Mobile Intel(R) Celeron(R) processor";
case 0x8: return (sig >= 0xF13) ? "Intel(R) Genuine processor" : "Intel(R) Pentium(R) 4 processor";
case 0x9: return "Intel(R) Pentium(R) 4 processor";
case 0xA: return "Intel(R) Celeron(R) processor";
case 0xB: return (sig < 0xF13) ? "Intel(R) Xeon(TM) processor MP" : "Intel(R) Xeon(TM) processor";
case 0xC: return "Intel(R) Xeon(TM) processor MP";
case 0xD: return "";
case 0xE: return (sig < 0xF13) ? "Intel(R) Xeon(TM) processor" : "Mobile Intel(R) Pentium(R) 4 Processor-M";
case 0xF: return "Mobile Intel(R) Celeron(R) processor";
default:
return "";
}
}
void
Intel_features(cpuid_info *info)
{
// for each bit set in the features flag, print out the
// corresponding index in the flags array (too easy!)
int i;
int fflags;
get_cpuid(info, 1, 0);
fflags = info->eax_1.features;
for (i = 0; i < 32; ++i)
if (fflags & (1<<i))
printf(" %s\n", Intel_feature_flags[i]);
printf("\n");
}
void
insert(int *a, int elem, int max_index)
{
// inserts a new element into an ordered integer array
// using (what else) the trusty old binary search algorithm...
// assumes the array has sufficient space to do this
// (i.e. it's the caller's problem to check for bounds overflow)
int i;
int lo = 0;
int hi = max_index;
int mid;
// find insert index (will end up in lo)
while (lo < hi) {
mid = (lo + hi) / 2;
if (elem < a[mid])
hi = mid;
else
lo = mid + 1;
}
// bump up the max index (to make room for the new element)
hi = max_index + 1;
// move up all the items after the insert point by one
for (i = hi; i > lo; --i)
a[i] = a[i-1];
// insert the new guy
a[lo] = elem;
}
void
Intel_TLB_cache(cpuid_info *info)
{
// displays technical info for the CPU's various instruction and data pipeline caches
// and the TLBs (Translation Lookaside Buffers).
//
// The method for extracting and displaying this info might be less than obvious,
// so here's an explanation:
//
// The TLB/Cache info is stored in a global table. Each entry is uniquely identified
// by a descriptor byte. Additionally, each entry is catagorized according to the
// cache type. A textual string contains the information to display.
//
// To retrieve the CPUs capabilities, the pass loop repeatedly (maybe) calls
// get_cpuid() which fills the registers. Each register is a 4-byte datum,
// capable of holding up to 4 descriptor bytes. The descriptor bytes are extracted
// (thru shifting and masking) and then inserted into a slot array. The slot array
// stores indexes into the table. Since descriptor bytes are not indexes themselves,
// an index array is created and used to convert them. The slot array is filled so
// as to keep the indexes in sorted order -- this guarantees than all entries for
// a particular cache type will display together.
//
// After the pass loop is finished, the slot array contains the indexes of all the
// table entries that apply to the host processor. Displaying the info is merely a matter
// of spinning thru the slot array and printing the text. The output, however, is organized
// by the cache types -- this technique *only* works because the TLB/Cache table has been
// carefully arranged in that order.
#define BIT_31_MASK (1 << 31)
uint32 pass, matches; // counters
uint32 reg; // value of an individual register (eax, ebx, ecx, edx)
uint8 db; // descriptor byte
int indexOf[256] = {0}; // converts descriptor bytes to table indexes
int slot[256] = {0}; // indexes into the TLB/Cache table (for entries found)
tlbc_info *tab = Intel_TLB_Cache_Table;
int tabsize = sizeof Intel_TLB_Cache_Table / sizeof(Intel_TLB_Cache_Table[0]);
// fill the index conversion array
int i = 0;
int n = 0;
while (n < tabsize)
indexOf[tab[n++].descriptor] = i++;
// pass loop: insert relevant table indexes into the slot array
i = 0;
for (pass = 0; ; ++pass) {
get_cpuid(info, 2, 0);
// low byte of eax register holds maximum iterations
reg = info->regs.eax;
if (pass >= (reg & 0xff))
break;
reg >>= 8; // skip low byte
for (; reg; reg >>= 8)
if ((db = (reg & 0xff)))
insert(slot, indexOf[db], i++);
reg = info->regs.ebx; // ebx
if ((reg & BIT_31_MASK) == 0)
for (; reg; reg >>= 8)
if ((db = (reg & 0xff)))
insert(slot, indexOf[db], i++);
reg = info->regs.ecx; // ecx
if ((reg & BIT_31_MASK) == 0)
for (; reg; reg >>= 8)
if ((db = (reg & 0xff)))
insert(slot, indexOf[db], i++);
reg = info->regs.edx; // edx
if ((reg & BIT_31_MASK) == 0)
for (; reg; reg >>= 8)
if ((db = (reg & 0xff)))
insert(slot, indexOf[db], i++);
}
// reset slot index for output loop
i = 0;
// eeiuw! an icky macro (but it really does prevent much repetitive code)
#define display_matching_entries(type_name,type_code) \
printf(type_name ":\n"); \
matches = 0; \
while (tab[n = slot[i]].cache_type == type_code) { \
printf(" %s\n", tab[n].text); \
++matches; \
++i; \
} \
if (matches == 0) \
printf(" None\n")
// at long last... output the data
display_matching_entries("Instruction TLB", Inst_TLB);
display_matching_entries("Data TLB", Data_TLB);
display_matching_entries("L1 Instruction Cache", L1_Inst_Cache);
display_matching_entries("L1 Data Cache", L1_Data_Cache);
display_matching_entries("L2 Cache", L2_Cache);
if (tab[slot[i]].cache_type == No_L2_Or_L3)
// no need to print the text on this one, just skip it
++i;
display_matching_entries("L3 Cache", L3_Cache);
display_matching_entries("Trace Cache", Trace_Cache);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// AMD specs
// Brennan says: I'm just digging around and trying to find AMD documentation
// on product signatures and CPU types...not easy to find. This is a dirty hack
// until I can find proper docs...
static const char *
AMD_brand_string(int family, int model, int processor_signature)
{
int sig = processor_signature;
switch (family) {
//K5 and K6 models
case 0x5:
if(model >= 0x0 && model <= 0x3) {
return "AMD K5";
}
else if(model == 0x8){
return "AMD K6-2";
}
else if(model == 0x9) {
return "AMD K6-III";
}
else {
return "AMD K6";
}
//Athlon and Duron models, very general
case 0x6:
//Hack. I own one, so I know this is the processor signature!
if(sig == 1634) {
return "AMD Mobile Athlon";
}
else if(model == 0x1 || model == 0x2 || model == 0x4) {
return "AMD Athlon";
}
else if(model == 0x3 ||model == 0x7) {
return "AMD Duron";
}
else if(model == 0x6){
return "AMD Duron/AMD Athlon XP/AMD Athlon MP";
}
case 0xf:
if(model == 0x5) {
return "AMD Athlon 64";
}
if(model == 0x6) {
return "AMD Opteron";
}
//This is the new 266FSB Duron model
else if(model == 0x8) {
return "AMD Duron (266 FSB)";
}
default:
return "Unknown AMD Processor";
}
}
void
AMD_identify(cpuid_info *info)
{
int type, family, model, sig;
const char *id_string;
get_cpuid(info, 1, 0);
type = info->eax_1.type;
family = info->eax_1.family;
model = info->eax_1.model;
sig = info->regs.eax;
id_string = AMD_brand_string(family, model, sig);
printf("%s, Model: %d\n", id_string, model);
printf("Family: %d\n", family);
printf("Signature: %d\n", sig);
}
void
AMD_features(cpuid_info *info)
{
printf("Sorry! Info about AMD CPU's not implemented yet :(\n");
}
void
AMD_TLB_cache(cpuid_info *info)
{
printf("Sorry! Info about AMD CPU's not implemented yet :(\n");
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Cyrix specs
void
Cyrix_identify(cpuid_info *info)
{
printf("Sorry! Info about Cyrix CPU's not implemented yet :(\n");
}
void
Cyrix_features(cpuid_info *info)
{
printf("Sorry! Info about Cyrix CPU's not implemented yet :(\n");
}
void
Cyrix_TLB_cache(cpuid_info *info)
{
printf("Sorry! Info about Cyrix CPU's not implemented yet :(\n");
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// dumps
void
opt_dump_calls(cpuid_info *info, uint32 max_level)
{
// dumps the registers for standard (and if supported) extended
// calls to the CPUID instruction
uint32 max_extended;
dump_regs(info, "Standard Calls", 0, max_level);
get_cpuid(info, 0x80000000, 0);
max_extended = info->regs.eax;
if (max_extended == 0)
printf("\nNo extended calls available for this CPU\n");
else
dump_regs(info, "Extended Calls", 0x80000000, max_extended);
}
void
dump_regs(cpuid_info *info, const char *heading, uint32 min_level, uint32 max_level)
{
// gosh, it's so perty (in a text-based output kind of way)
uint32 i;
printf("%s (max eax level = %lx)\n", heading, max_level);
printf("----------------------------------------------\n");
printf(" Input | Output\n");
printf("----------------------------------------------\n");
printf(" eax | eax ebx ecx edx\n");
for (i = min_level; i <= max_level; ++i) {
get_cpuid(info, i, 0);
printf("%08lx | %08lx %08lx %08lx %08lx\n",
i,
info->regs.eax,
info->regs.ebx,
info->regs.ecx,
info->regs.edx);
}
printf("\n");
}
void
print_regs(cpuid_info *info)
{
// this function either prints the contents of all the
// registers as a single character string, or it does
// something else entirely (you decide)
int i;
char s[17] = {0};
for (i = 0; i < 4; ++i)
s[i] = info->regs.eax >> (8*i),
s[i+4] = info->regs.ebx >> (8*i),
s[i+8] = info->regs.ecx >> (8*i),
s[i+12] = info->regs.edx >> (8*i);
printf("%s", s);
}

View File

@ -1,3 +0,0 @@
SubDir OBOS_TOP src kernel apps echo ;
KernelObjects <$(SOURCE_GRIST)>main.c : -fpic -Wno-unused ;

View File

@ -1,79 +0,0 @@
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc , char *argv[])
{
int nflag; /* if not set, output a trailing newline. */
/* This utility may NOT do getopt(3) option parsing. */
if (*++argv && !strcmp(*argv, "-n")) {
++argv;
nflag = 1;
} else
nflag = 0;
while (argv[0] != NULL) {
/*
* If the next argument is NULL then this is this
* the last argument, therefore we need to check
* for a trailing \c.
*/
if (argv[1] == NULL) {
size_t len;
len = strlen(argv[0]);
/* is there room for a '\c' and is there one? */
if (len >= 2 &&
argv[0][len - 2] == '\\' &&
argv[0][len - 1] == 'c') {
/* chop it and set the no-newline flag. */
argv[0][len - 2] = '\0';
nflag = 1;
}
}
(void)printf("%s", argv[0]);
if (*++argv)
putchar(' ');
}
if (!nflag)
putchar('\n');
return 0;
}

View File

@ -1,3 +0,0 @@
SubDir OBOS_TOP src kernel apps envtest ;
KernelObjects <$(SOURCE_GRIST)>main.c : -fpic -Wno-unused ;

View File

@ -1,66 +0,0 @@
/*
** Copyright 2003-2004, The Haiku Team. All rights reserved.
** Distributed under the terms of the Haiku License.
*/
#include <image.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
int i;
thread_id pid;
status_t rc;
char temp[16];
char *var;
printf("Setting some env vars...\n");
if (setenv("TEST", "This is a test!", 0)) {
printf("setenv() error\n");
return -1;
}
if (setenv("HOME", "/boot", 1)) {
printf("setenv() error\n");
return -1;
}
printf("Getting VAR_1: ");
var = getenv("VAR_1");
if (var)
printf("found set to: \"%s\"\n", var);
else
printf("not found\n");
printf("List of env variables set:\n");
for (i = 0; environ[i]; i++)
printf("%s\n", environ[i]);
if (argc > 1) {
char buffer[16];
const char *_argv[] = { argv[0], buffer, NULL };
int val = atoi(argv[1]) - 1;
sprintf(temp, "VAR_%d", val);
if (setenv(temp, "dummy", 0)) {
printf("setenv() error\n");
return -1;
}
if (val > 0) {
printf("Spawning test (%d left)\n", val);
sprintf(buffer, "%d", val);
pid = load_image(2, _argv, (const char **)environ);
wait_for_thread(pid, &rc);
}
}
return 0;
}

View File

@ -1,52 +0,0 @@
/*
** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the Haiku License.
*/
#include <OS.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
static void
print_process_info(const char *text)
{
puts(text);
printf("\tsession_id = %ld\n", getsid(0));
printf("\tgroup_id = %ld\n", getpgid(0));
printf("\tprocess_id = %ld\n", getpid());
printf("\tparent_id = %ld\n", getppid());
}
int
main(int argc, char **argv)
{
printf("exec_test: this is thread %ld\n", find_thread(NULL));
if (argc < 2) {
print_process_info("before exec():");
puts("going to execl()...");
execl("/bin/exec_test", "/bin/exec_test", "argument 1", NULL);
printf("execl() returned: %s\n", strerror(errno));
} else {
int i;
print_process_info("after exec():");
puts("got arguments:");
for (i = 0; i < argc; i++)
printf("%d: \"%s\"\n", i, argv[i]);
setsid();
print_process_info("after setsid():");
}
return 0;
}

View File

@ -1,13 +0,0 @@
/*
** Copyright 2002, Manuel J. Petit. All rights reserved.
** Contains portions of:
**
** /boot/bin/true, Copyright 2001 Travis K. Geiselbrecht
**
** Distributed under the terms of the NewOS License.
*/
int main(void)
{
return 1;
}

View File

@ -1,72 +0,0 @@
/*
** Copyright 2002, Manuel J. Petit. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <image.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
static void
usage(char const *app)
{
printf("usage: %s [-s] ###\n", app);
exit(-1);
}
int
main(int argc, char *argv[])
{
int num = 0;
int silent = 0;
int result;
switch (argc) {
case 2:
num = atoi(argv[1]);
break;
case 3:
if (strcmp(argv[1], "-s") == 0) {
num = atoi(argv[2]);
silent = 1;
} else {
usage(argv[0]);
}
break;
default:
usage(argv[0]);
break;
}
if (num < 2) {
result = 1;
} else {
thread_id pid;
status_t retcode;
char buffer[64];
const char *aaargv[] = { "/boot/bin/fibo", "-s", buffer, NULL };
int aaargc = 3;
sprintf(buffer, "%d", num-1);
pid = load_image(aaargc, aaargv, (const char **)environ);
wait_for_thread(pid, &retcode);
result = retcode;
sprintf(buffer, "%d", num-2);
pid = load_image(aaargc, aaargv, (const char **)environ);
wait_for_thread(pid, &retcode);
result += retcode;
}
if (silent) {
return result;
} else {
printf("%d\n", result);
return 0;
}
}

View File

@ -1,3 +0,0 @@
SubDir OBOS_TOP src kernel apps filetest ;
KernelObjects <$(SOURCE_GRIST)>main.c : -fpic -Wno-unused ;

View File

@ -1,52 +0,0 @@
/*
** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/resource.h>
#include <Errors.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define FORTUNES "/boot/etc/fortunes"
int main(int argc, char **argv)
{
int rc = 0, fd[3];
int cnt, i;
int on = 1;
printf("File Test!!!\n");
for (i=0;i<3;i++) {
printf("%d : ", i + 1);
fd[i] = open(FORTUNES, O_RDONLY, 0);
printf("fd %d\n", fd[i]);
}
printf("closing the 3 fd's\n");
for (i=0;i<3;i++) {
close(fd[i]);
}
printf("\nNow we have no open fd's so next socket should be 3\n");
fd[0] = open(FORTUNES, O_RDONLY, 0);
printf("new fd %d\n", fd[0]);
printf("Trying ioctl to set non-blocking: ");
rc = ioctl(fd[0], FIONBIO, &on, sizeof(on));
if (rc == EINVAL) {
printf("OK\n");
} else {
printf("failed\n");
printf("error was %s\n", strerror(errno));
}
close(fd[0]);
return 0;
}

View File

@ -1,33 +0,0 @@
/*
** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the Haiku License.
*/
#include <OS.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int
main(int argc, char **argv)
{
pid_t child = fork();
if (child == 0) {
printf("CHILD: we're the child!\n");
snooze(500000); // .5 sec
printf("CHILD: exit!\n");
} else if (child > 0) {
printf("PARENT: we're the parent, our child has pid %ld\n", child);
waitpid(-1, NULL, 0);
} else
fprintf(stderr, "fork() failed: %s\n", strerror(errno));
return 0;
}

View File

@ -1,7 +0,0 @@
SubDir OBOS_TOP src kernel apps fortune ;
KernelObjects
<$(SOURCE_GRIST)>main.c
:
-fpic
;

View File

@ -1,9 +0,0 @@
#@#
Magic is real... unless declared integer.
#@#
System is up and running.
#@#
Open source thesis that thousand of eyes looking thu the code
increases software quality is completely wrong. The amount of
bugs per line of code is an universal constant. The bazaar
model only achieves a quick bug turnover.

View File

@ -1,80 +0,0 @@
/*
** Copyright 2002-2004, The OpenBeOS Team. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
*/
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <syscalls.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#define FORTUNES "/etc/fortunes"
int
main(void)
{
int fd;
int rc;
char *buf;
unsigned i;
unsigned found;
struct stat stat;
fd = open(FORTUNES, O_RDONLY, 0);
if (fd < 0) {
printf("Couldn't open %s: %s\n", FORTUNES, strerror(errno));
return -1;
}
rc = fstat(fd, &stat);
if (rc < 0) {
printf("stat() failed: %s\n", strerror(errno));
return -1;
}
buf = malloc(stat.st_size + 1);
rc = read(fd, buf, stat.st_size);
if (rc < 0) {
printf("Could not read from fortune file: %s\n", strerror(errno));
return -1;
}
buf[stat.st_size] = 0;
close(fd);
found = 0;
for (i = 0; i < stat.st_size; i++) {
if (!strncmp(buf + i, "#@#", 3))
found += 1;
}
if (found > 0)
found = 1 + ((system_time() + 3) % found);
else {
printf("Out of cookies...\n");
return -1;
}
for (i = 0; i < stat.st_size; i++) {
if (!strncmp(buf + i, "#@#", 3))
found -= 1;
if (found == 0) {
unsigned j;
for (j = i + 1; j < stat.st_size; j++) {
if (!strncmp(buf + j, "#@#", 3))
buf[j] = 0;
}
printf("%s\n", buf + i + 3);
break;
}
}
return 0;
}

View File

@ -1,3 +0,0 @@
SubDir OBOS_TOP src kernel apps hostname ;
KernelObjects <$(SOURCE_GRIST)>main.c : -fpic -Wno-unused ;

View File

@ -1,42 +0,0 @@
/* naive implementation of hostname
*
* This mainly serves as a testbed for sysctl, part of the kernel
* just added.
*/
#include <syscalls.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sysctl.h>
#define MAXHOSTNAMELEN 256
int main(int argc, char *argv[])
{
char buffer[MAXHOSTNAMELEN];
size_t buflen = MAXHOSTNAMELEN;
int mib[2];
int rc = -1;
char *newname = NULL;
size_t newnamelen = 0;
if (argc >= 2) {
newname = argv[1];
newnamelen = strlen(newname);
}
buffer[0] = '\0';
mib[0] = CTL_KERN;
mib[1] = KERN_HOSTNAME;
rc = sysctl(mib, 2, &buffer, &buflen, newname, newnamelen);
if (rc == 0 && !newname) {
printf("%s\n", buffer);
}
return 0;
}

View File

@ -1,52 +0,0 @@
/*
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <image.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
static void
setup_io(void)
{
int i;
for (i = 0; i< 256; i++) {
close(i);
}
open("/dev/keyboard", O_RDONLY, 0); /* stdin */
open("/dev/console", O_WRONLY, 0); /* stdout */
open("/dev/console", O_WRONLY, 0); /* stderr */
}
int
main(int argc, char **argv)
{
setup_io();
printf("Welcome to Haiku!\r\n");
{
const char *args[] = {"consoled", NULL};
thread_id thread;
thread = load_image(1, args, (const char **)environ);
if (thread >= B_OK) {
status_t returnCode;
wait_for_thread(thread, &returnCode);
} else
printf("Failed to create a team for fortune.\n");
}
printf("init exiting\n");
return 0;
}

View File

@ -1,8 +0,0 @@
SubDir OBOS_TOP src kernel apps ls ;
KernelObjects
<$(SOURCE_GRIST)>main.c
:
-fpic -Wno-unused
;

View File

@ -1,163 +0,0 @@
/*
** Copyright 2002, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <syscalls.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
extern char *__progname;
void (*disp_func)(const char *, const char *, struct stat *) = NULL;
static int show_all = 0;
mode_t perms [9] = {
S_IRUSR,
S_IWUSR,
S_IXUSR,
S_IRGRP,
S_IWGRP,
S_IXGRP,
S_IROTH,
S_IWOTH,
S_IXOTH
};
static void
display_l(const char *path, const char *filename, struct stat *stat)
{
char perm[11];
uint32 i;
memset(perm, '-', 10);
perm[10] = '\0';
for (i = 0; i < sizeof(perms) / sizeof(mode_t); i++) {
if (stat->st_mode & perms[i]) {
switch(i % 3) {
case 0:
perm[i + 1] = 'r';
break;
case 1:
perm[i+1] = 'w';
break;
case 2:
perm[i+1] = 'x';
}
}
}
if (S_ISDIR(stat->st_mode))
perm[0] = 'd';
else if (S_ISLNK(stat->st_mode))
perm[0] = 'l';
else if (S_ISCHR(stat->st_mode))
perm[0] = 'c';
printf("%10s %12lld %s" ,perm ,stat->st_size ,filename);
if (S_ISLNK(stat->st_mode)) {
char buffer[1024];
if (readlink(path, buffer, sizeof(buffer)) < 0)
strcpy(buffer, "???");
printf(" (-> %s)\n", buffer);
} else
putchar('\n');
}
static void
display(const char *path, const char *filename, struct stat *stat)
{
printf("%s\n", filename);
}
int
main(int argc, char *argv[])
{
int rc;
int rc2;
int count = 0;
struct stat st;
char *arg;
int ch;
uint64 totbytes = 0;
disp_func = display;
while ((ch = getopt(argc, argv, "al")) != -1) {
switch (ch) {
case 'a':
show_all = 1;
break;
case 'l':
disp_func = display_l;
break;
}
}
argc -= optind;
argv += optind;
if (*argv == NULL)
arg = ".";
else
arg = *argv;
rc = stat(arg, &st);
if (rc < 0) {
printf("%s: %s: %s\n", __progname,
arg, strerror(rc));
goto err_ls;
}
if (S_ISDIR(st.st_mode)) {
DIR *thedir = opendir(arg);
if (!thedir) {
printf("ls: %s: %s\n", arg, strerror(errno));
} else {
if (show_all) {
/* process the '.' entry */
rc = stat(arg, &st);
if (rc == 0) {
(*disp_func)(arg, ".", &st);
totbytes += st.st_size;
}
}
for(;;) {
char buf2[1024];
struct dirent *de = readdir(thedir);
if (!de)
break;
memset(buf2, 0, sizeof(buf2));
if (strcmp(arg, ".") != 0) {
strlcpy(buf2, arg, sizeof(buf2));
strlcat(buf2, "/", sizeof(buf2));
}
strlcat(buf2, de->d_name, sizeof(buf2));
rc2 = lstat(buf2, &st);
if (rc2 == 0) {
(*disp_func)(buf2, de->d_name, &st);
totbytes += st.st_size;
}
count++;
}
closedir(thedir);
}
printf("%lld bytes in %d files\n", totbytes, count);
} else {
(*disp_func)(arg, arg, &st);
}
err_ls:
return 0;
}

View File

@ -1,44 +0,0 @@
/* tests node monitor functionality (very basic test!) */
/*
** Copyright 2003-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the Haiku License.
*/
#include <OS.h>
#include <NodeMonitor.h>
#include <syscalls.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int
main(int argc, char **argv)
{
struct stat st;
if (stat("/", &st) < 0) {
fprintf(stderr, "Could not stat root!\n");
return -1;
}
printf("watch file: device = %ld, node = %Ld\n", st.st_dev, st.st_ino);
if (_kern_start_watching(st.st_dev, st.st_ino, B_WATCH_DIRECTORY, 1, 2) < B_OK) {
fprintf(stderr, "Could not start watching!\n");
return -1;
}
mkdir("/temp_test", 0755);
rmdir("/temp_test");
if (_kern_stop_watching(st.st_dev, st.st_ino, 0, 1, 2) < B_OK) {
fprintf(stderr, "Could not stop watching!\n");
return -1;
}
return 0;
}

View File

@ -1,43 +0,0 @@
#include <OS.h>
#include <stdio.h>
static void
first(void *data)
{
printf("first on_exit_thread(): %ld\n", (int32)data);
}
static void
second(void *data)
{
printf("second on_exit_thread(): %ld\n", (int32)data);
}
static int32
thread(void *data)
{
puts("Nurse Stimpy for the rescue!");
on_exit_thread(first, (void *)11);
on_exit_thread(second, (void *)22);
return 0;
}
int
main(int argc, char **argv)
{
status_t status;
thread_id id;
on_exit_thread(first, (void *)666);
id = spawn_thread(thread, "mythread", B_NORMAL_PRIORITY, (void *)42);
resume_thread(id);
wait_for_thread(id, &status);
return 0;
}

View File

@ -1,97 +0,0 @@
/* tests basic pipes functionality */
/*
** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
*/
#include <OS.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
static int32
reader_func(void *data)
{
char buffer[1024];
int input = (int)data;
int bytes;
while ((bytes = read(input, buffer, sizeof(buffer) - 1)) > 0) {
buffer[bytes] = '\0';
// null-terminate, just in case
printf("reader: (%d) %s\n", bytes, buffer);
if (buffer[0] == '2') {
puts("reader: wait a second and let the writer lead (the pipe has to be buffered)");
snooze(1000000);
}
if (strstr(buffer, "QUIT"))
break;
}
puts("reader quits");
return 0;
}
static int32
writer_func(void *data)
{
int output = (int)data;
int i;
const char *strings[] = {
"1. If you read this ",
"2. the pipe implementation ",
"3. seems to work ",
"4. at least a bit ",
"5. QUIT",
NULL};
for (i = 0; strings[i] != NULL; i++) {
snooze(100000);
// make sure the reader is waiting for us...
// (needed by the current pipefs implementation :)
printf("writer: (%ld)\n", write(output, strings[i], strlen(strings[i])));
}
puts("writer quits");
return 0;
}
int
main(int argc, char **argv)
{
thread_id reader, writer;
int stream[2];
status_t returnCode;
if (pipe(stream) < 0) {
fprintf(stderr, "pipe creation failed!\n");
return -1;
}
reader = spawn_thread(reader_func, "Reader" , B_NORMAL_PRIORITY, (void *)stream[0]);
resume_thread(reader);
writer = spawn_thread(writer_func, "Writer" , B_NORMAL_PRIORITY, (void *)stream[1]);
resume_thread(writer);
puts("reader & writer started.");
// wait until they quit
wait_for_thread(reader, &returnCode);
wait_for_thread(writer, &returnCode);
close(stream[0]);
close(stream[1]);
return 0;
}

View File

@ -1,3 +0,0 @@
SubDir OBOS_TOP src kernel apps ps ;
KernelObjects <$(SOURCE_GRIST)>main.c ;

View File

@ -1,72 +0,0 @@
/*
* PS command
* Rewritten for OpenBeOS by Angelo Mottola, Aug 2002.
*/
#include <OS.h>
#include <Errors.h>
#include <syscalls.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
const char *state(thread_state state);
const char *state(thread_state state)
{
switch (state) {
case B_THREAD_RUNNING:
return "run";
case B_THREAD_READY:
return "rdy";
case B_THREAD_SUSPENDED:
return "sus";
case B_THREAD_WAITING:
return "sem";
case B_THREAD_ASLEEP:
return "zzz";
case B_THREAD_RECEIVING:
return "msg";
default:
return "???";
}
}
int main(int argc, char **argv)
{
int32 thread_num;
int32 team_num = 0;
thread_info thread;
team_info team;
sem_info sem;
char sem_name[B_OS_NAME_LENGTH * 2];
char buffer[B_OS_NAME_LENGTH];
printf("\n thread name state prio user kernel semaphore\n");
printf("-----------------------------------------------------------------------\n");
while (get_next_team_info(&team_num, &team) == B_OK) {
printf("%s (team %ld) (uid %d) (gid %d)\n",
team.args, team.team, team.uid, team.gid);
thread_num = 0;
while (get_next_thread_info(team.team, &thread_num, &thread) == B_OK) {
sem_name[0] = '\0';
if (thread.state == B_THREAD_WAITING) {
if (get_sem_info(thread.sem, &sem) == B_OK) {
strcpy(sem_name, sem.name);
sprintf(buffer, "(%ld)", sem.sem);
strcat(sem_name, buffer);
}
}
printf(" %6ld %20s %s %3ld %7d %7d %s\n",
thread.thread, thread.name, state(thread.state), thread.priority,
(int)thread.user_time, (int)thread.kernel_time, sem_name);
}
}
printf("\n");
return 0;
}

View File

@ -1,48 +0,0 @@
/* tests basic select() and poll() functionality */
/*
** Copyright 2002, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
*/
#include <sys/select.h>
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define FILE_NAME "/boot"
int
main(int argc, char **argv)
{
fd_set readSet;
struct pollfd pollfd;
int count;
int file = open(FILE_NAME, O_RDONLY);
if (file < 0) {
fprintf(stderr, "Could not open \"%s\": %s\n", FILE_NAME, strerror(file));
return -1;
}
FD_ZERO(&readSet);
FD_SET(file, &readSet);
puts("selecting...");
count = select(file + 1, &readSet, NULL, NULL, NULL);
printf("\tselect returned: %d (read set = %ld)\n", count, FD_ISSET(file, &readSet));
pollfd.fd = file;
pollfd.events = POLLOUT | POLLERR;
puts("polling...");
count = poll(&pollfd, 1, -1);
printf("\tpoll returned: %d (revents = 0x%x)\n", count, pollfd.revents);
close(file);
return 0;
}

View File

@ -1,12 +0,0 @@
SubDir OBOS_TOP src kernel apps shell ;
KernelObjects <$(SOURCE_GRIST)>main.c
<$(SOURCE_GRIST)>args.c
<$(SOURCE_GRIST)>commands.c
<$(SOURCE_GRIST)>file_utils.c
<$(SOURCE_GRIST)>parse.c
<$(SOURCE_GRIST)>script.c
<$(SOURCE_GRIST)>shell_vars.c
<$(SOURCE_GRIST)>statements.c
<$(SOURCE_GRIST)>shell_history.c
: -fpic ;

View File

@ -1,80 +0,0 @@
//#include <types.h>
#include <string.h>
#include <ctype.h>
#include <syscalls.h>
#include <stdio.h>
#include <stdlib.h>
#include "args.h"
#include "shell_defs.h"
#include "shell_vars.h"
bool af_exit_after_script;
char *af_script_file_name;
char *gCommandToExecute = NULL;
static const char *kUsage =
"Usage: shell [ -s ] [ -c <command> ] [ <script> ... ]\n";
static
void
print_usage(bool error)
{
fprintf((error ? stderr : stdout), kUsage);
}
void
init_arguments(int argc,char **argv)
{
int cnt = 1;
int shell_argc = 0;
char name[255];
af_exit_after_script = true;
af_script_file_name = NULL;
while((cnt < argc) && (argv[cnt][0] == '-')){
const char *option = argv[cnt];
switch(option[1]){
case 's': // setup script
if (option[2] == 0)
af_exit_after_script = false;
break;
case 'c': // execute command
cnt++;
if (cnt >= argc) {
print_usage(true);
exit(1);
}
gCommandToExecute = argv[cnt];
// ignore further args
return;
case '-':
// ignore standard bash option
if (!strcmp(option, "--login"))
break;
default:
print_usage(true);
exit(1);
}
cnt++;
}
if(cnt < argc){
af_script_file_name = argv[cnt];
cnt++;
while(cnt < argc){
sprintf(name,NAME_PAR_PRFX,cnt-1);
shell_var_set_text(name,argv[cnt]);
shell_argc++;
cnt++;
}
shell_var_set_number(NAME_VAR_ARGC,shell_argc);
}
}

View File

@ -1,10 +0,0 @@
#ifndef _args_h_
#define _args_h_
extern bool af_exit_after_script;
extern char *af_script_file_name;
extern char *gCommandToExecute;
void init_arguments(int argc,char **argv);
#endif

View File

@ -1,238 +0,0 @@
/*
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <syscalls.h>
#include <image.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "commands.h"
#include "file_utils.h"
#include "shell_defs.h"
struct command cmds[] = {
{"exec", &cmd_exec},
{"stat", &cmd_stat},
{"mkdir", &cmd_mkdir},
{"cat", &cmd_cat},
{"cd", &cmd_cd},
{"pwd", &cmd_pwd},
{"kill", &cmd_kill},
{"help", &cmd_help},
{NULL, NULL}
};
int
cmd_exec(int argc, char *argv[])
{
return cmd_create_proc(argc - 1,argv+1);
}
int
cmd_create_proc(int argc, char *argv[])
{
bool must_wait=true;
team_id pid;
int arg_len;
char *tmp;
char filename[SCAN_SIZE+1];
if (argc < 1) {
printf("not enough args to exec\n");
return 0;
}
tmp = argv[argc - 1];
if (!find_file_in_path(argv[0], filename, SCAN_SIZE)) {
printf("can't find '%s' \n",argv[0]);
return 0;
}
// a hack to support the unix '&'
if(argc >= 1) {
arg_len = strlen(tmp);
if(arg_len > 0){
tmp += arg_len -1;
if(*tmp == '&'){
if(arg_len == 1){
argc --;
} else {
*tmp = 0;
}
must_wait = false;
}
}
}
pid = load_image(argc, (const char **)argv, (const char **)environ);
if (pid >= 0) {
if (must_wait)
wait_for_thread(pid, NULL);
} else {
printf("Error: cannot execute '%s'\n", filename);
return 0; // should be -1, but the shell would exit
}
return 0;
}
int
cmd_mkdir(int argc, char *argv[])
{
int rc;
if (argc < 2) {
printf("not enough arguments to mkdir\n");
return 0;
}
rc = _kern_create_dir(-1, argv[1], 0755);
if (rc < 0) {
printf("sys_mkdir() returned error: %s\n", strerror(rc));
} else {
printf("%s successfully created.\n", argv[1]);
}
return 0;
}
int
cmd_cat(int argc, char *argv[])
{
int rc;
int fd;
char buf[257];
if (argc < 2) {
printf("not enough arguments to cat\n");
return 0;
}
fd = open(argv[1], 0);
if (fd < 0) {
printf("cat: sys_open() returned error: %s!\n", strerror(fd));
goto done_cat;
}
for(;;) {
rc = read_pos(fd, -1, buf, sizeof(buf) - 1);
if (rc <= 0)
break;
buf[rc] = '\0';
printf("%s", buf);
}
close(fd);
done_cat:
return 0;
}
int
cmd_cd(int argc, char *argv[])
{
int rc;
if (argc < 2) {
printf("not enough arguments to cd\n");
return 0;
}
rc = chdir(argv[1]);
if (rc < 0)
printf("cd: sys_setcwd() returned error: %s!\n", strerror(errno));
return 0;
}
int
cmd_pwd(int argc, char *argv[])
{
char buffer[B_PATH_NAME_LENGTH];
if (getcwd(buffer, sizeof(buffer)) == NULL) {
printf("cd: sys_getcwd() returned error: %s!\n", strerror(errno));
return 0;
}
printf("%s\n", buffer);
return 0;
}
int
cmd_stat(int argc, char *argv[])
{
int rc;
struct stat st;
if (argc < 2) {
printf("not enough arguments to stat\n");
return 0;
}
rc = stat(argv[1], &st);
if (rc >= 0) {
printf("stat of file '%s': \n", argv[1]);
printf("vnid 0x%x\n", (unsigned int)st.st_ino);
printf("type %d\n", st.st_type);
printf("size %d\n", (int)st.st_size);
} else
printf("stat failed for file '%s'\n", argv[1]);
return 0;
}
int
cmd_kill(int argc, char *argv[])
{
int rc;
if (argc < 3) {
printf("not enough arguments to kill\n");
return 0;
}
rc = send_signal(atoi(argv[2]), atoi(argv[1]));
if (rc)
printf("kill failed\n");
return 0;
}
int
cmd_help(int argc, char *argv[])
{
printf("command list:\n\n");
printf("exit : quits this copy of the shell\n");
printf("exec <file> : load this file as a binary and run it\n");
printf("mkdir <path> : makes a directory at <path>\n");
printf("cd <path> : sets the current working directory at <path>\n");
printf("ls <path> : directory list of <path>. If no path given it lists the current dir\n");
printf("stat <file> : gives detailed file statistics of <file>\n");
printf("help : this command\n");
printf("cat <file> : dumps the file to stdout\n");
printf("mount <path> <device> <fsname> : tries to mount <device> at <path>\n");
printf("unmount <path> : tries to unmount at <path>\n");
printf("kill <sig> <tid> : sends signal <sig> to thread <tid>\n");
return 0;
}

View File

@ -1,22 +0,0 @@
#ifndef _COMMANDS_H
#define _COMMANDS_H
int cmd_exit(int argc, char *argv[]);
int cmd_exec(int argc, char *argv[]);
int cmd_stat(int argc, char *argv[]);
int cmd_mkdir(int argc, char *argv[]);
int cmd_cat(int argc, char *argv[]);
int cmd_cd(int argc, char *argv[]);
int cmd_pwd(int argc, char *argv[]);
int cmd_kill(int argc, char *argv[]);
int cmd_help(int argc, char *argv[]);
int cmd_create_proc(int argc,char *argv[]);
typedef int(cmd_handler_proc)(int argc,char *argv[]);
struct command {
char *cmd_text;
cmd_handler_proc *cmd_handler;
};
extern struct command cmds[];
#endif

View File

@ -1,129 +0,0 @@
#include <image.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <Errors.h>
#include <fcntl.h>
#include <unistd.h>
#include "file_utils.h"
#include "statements.h"
#include "shell_defs.h"
bool
combine_path(const char *path1,const char *path2,char *out,unsigned int max_len)
{
unsigned int cur_len = 0;
cur_len = strlen(path1);
if(cur_len > max_len) return false;
strcpy(out,path1);
if(*path1 != 0){
if(out[cur_len-1] != '/'){
if(cur_len >= max_len) return false;
out[cur_len] = '/';
out[cur_len+1] = 0;
cur_len++;
}
}
if (strlen(path2)+cur_len > max_len)
return false;
strcat(out,path2);
return true;
}
bool
exists_file(const char *file_name)
{
int handle = open(file_name, 0);
bool exists;
exists = (handle >= 0);
if (exists)
close(handle);
return exists;
}
bool
find_file_in_path(const char *file_name, char *found_name, unsigned int max_size)
{
char path[SCAN_SIZE + 1];
int cnt = 0;
if (strchr(file_name, '/') != NULL) {
strncpy(found_name,file_name,max_size);
found_name[max_size] = 0;
if (exists_file(found_name))
return true;
found_name[0] = 0;
return false;
}
while (get_path(cnt, path, SCAN_SIZE)) {
if (combine_path(path, file_name, found_name, max_size)) {
if (exists_file(found_name))
return true;
}
cnt++;
}
found_name[0] = 0;
return false;
}
int
exec_file(int argc, char *argv[], status_t *retcode)
{
char filename[255];
int pid;
if (!find_file_in_path(argv[0], filename, SCAN_SIZE))
return SHE_FILE_NOT_FOUND;
pid = load_image(argc, (const char **)argv, (const char **)environ);
if (pid < 0)
return SHE_CANT_EXECUTE;
wait_for_thread(pid, retcode);
return SHE_NO_ERROR;
}
int
read_file_in_buffer(const char *filename,char **buffer)
{
struct stat stat;
int file_no;
int err;
int size;
*buffer = NULL;
file_no = open(filename, O_RDONLY);
if (file_no < 0)
return file_no;
err = fstat(file_no,&stat);
if (err < 0)
return err;
*buffer = malloc(stat.st_size + 1);
if (*buffer == NULL)
return ENOMEM;
size = read(file_no, *buffer, stat.st_size);
close(file_no);
if (size < 0)
free(*buffer);
return size;
}

View File

@ -1,10 +0,0 @@
#ifndef _file_utils_h_
#define _file_utils_h_
bool combine_path(const char *path1,const char *path2,char *out,unsigned int max_len);
bool exists_file(const char *file_name);
bool find_file_in_path(const char *file_name,char *found_name,unsigned int max_size);
int exec_file(int argc,char *argv[], status_t *retcode);
int read_file_in_buffer(const char *filename,char **buffer);
#endif

View File

@ -1,117 +0,0 @@
/*
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <string.h>
#include <ctype.h>
#include <syscalls.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "commands.h"
#include "parse.h"
#include "statements.h"
#include "shell_defs.h"
#include "shell_vars.h"
#include "args.h"
#include "shell_history.h"
#define ECHO 0
// echoes input when unequal 0
static int
readline(char *buf, int len)
{
int i = 0;
char ch;
while (true) {
ch = getchar();
switch (ch) {
case 27:
// ESC -- used to display command history:
// erase all characters on the current line,
// then display the previously saved command
#if ECHO
while (i--) {
printf("\b");
}
#endif
strcpy (buf, fetch_history_command());
i = printf("%s", buf);
break;
case '\b':
// backspace:
// erase the last character written
if (i > 0) {
#if ECHO
printf("\b");
#endif
--i;
}
break;
case '\n':
buf[i] = 0;
store_history_command(buf);
#if ECHO
printf("\n");
#endif
return i;
default:
buf[i] = ch;
#if ECHO
printf("%c", ch);
#endif
i++;
}
}
}
int
main(int argc, char *argv[])
{
char buf[1024];
init_vars();
init_statements();
init_arguments(argc, argv);
if (af_script_file_name) {
run_script(af_script_file_name);
if (af_exit_after_script)
exit(0);
}
if (gCommandToExecute) {
if (strlen(gCommandToExecute) > 0)
parse_string(gCommandToExecute);
exit(0);
}
printf("Welcome to the Haiku shell\n");
for (;;) {
int chars_read;
printf("$ ");
chars_read = readline(buf, sizeof(buf));
if (chars_read > 0)
parse_string(buf);
}
printf("shell exiting\n");
return 0;
}

View File

@ -1,583 +0,0 @@
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "parse.h"
#include "commands.h"
#include "shell_defs.h"
#include "shell_vars.h"
#include "script.h"
struct token_info {
char *token;
int code;
};
struct token_info tokens[] = {
{"echo",SVO_ECHO},
{"exec",SVO_EXEC},
{"exit",SVO_EXIT},
{"if",SVO_IF},
{"goto",SVO_GOTO},
{"number",SVO_NUMBER_CAST},
{"string",SVO_STRING_CAST},
{"+",SVO_ADD},
{"-",SVO_SUB},
{"*",SVO_MUL},
{"/",SVO_DIV},
{"=",SVO_LOAD},
{"==",SVO_EQUAL},
{"!=",SVO_NOT_EQUAL},
{">",SVO_BIGGER},
{">=",SVO_BIGGER_E},
{"<",SVO_LESS},
{"<=",SVO_LESS_E},
{"$",SVO_DOLLAR},
{",",SVO_COMMA},
{"(",SVO_PARENL},
{")",SVO_PARENR},
{":",SVO_DOTDOT},
{NULL,SVO_NONE}
};
static int token_to_id(const char *token)
{
int cnt = 0;
while((tokens[cnt].token != NULL) && (strcmp(tokens[cnt].token,token) != 0)) cnt++;
return tokens[cnt].code;
}
char *id_to_token(int id)
{
int cnt = 0;
switch (id) {
case SVO_IDENT :return "ident";
case SVO_SQ_STRING :
case SVO_DQ_STRING :return "string";
case SVO_NUMBER :return "number";
case SVO_END :return "end";
}
while((tokens[cnt].token != NULL) && (tokens[cnt].code != id)) cnt++;
if(tokens[cnt].token != NULL){
return tokens[cnt].token;
} else {
return "unkown";
}
}
static bool scan_string(scan_info *info,int max_len)
{
const char **in=&(info->scanner);
char ch=**in;
char *scan=info->token;
int len_cnt = max_len;
bool err = false;
(*in)++;
while((**in != 0) && (**in != ch)){
if(len_cnt > 0){
*scan = **in;
scan++;
len_cnt--;
}
(*in)++;
}
if(**in != 0) {
(*in)++;
} else {
info->scan_error = SHE_MISSING_QOUTE;
err = true;
}
*scan = 0;
return err;
}
int parse_line(const char *buf, char *argv[], int max_args, char *redirect_in, char *redirect_out)
{
const char *scan=buf;
const char *type_char;
const char *start;
char out[SCAN_SIZE+1];
char tmp[SCAN_SIZE+1];
char *arg_tmp;
int arg_cnt = 0;
int len = 0;
bool replace;
bool param;
redirect_in[0] = 0;
redirect_out[0] = 0;
while (true) {
while ((*scan != 0) && (isspace(*scan))) scan++;
if (*scan == 0) break;
replace = true;
type_char = scan;
start = scan;
param = true;
switch(*type_char){
case '\'':
replace = false;
case '"':
scan++;
start++;
while((*scan != 0) && (*scan != *start)) scan++;
break;
case '>':
case '<':
start++;
while((*start != 0) && (isspace(*start))) start++;
scan = start;
default:
while((*scan != 0) && (!isspace(*scan))) scan++;
}
len = scan - start;
if(*scan != 0) scan++;
if(replace){
memcpy(tmp,start,len);
tmp[len]=0;
parse_vars_in_string(tmp,out,SCAN_SIZE);
} else {
memcpy(out,start,len);
out[len]=0;
}
switch(*type_char){
case '>':
strcpy(redirect_out,out);
break;
case '<':
strcpy(redirect_in,out);
break;
default:
if(arg_cnt < max_args){
arg_tmp = shell_strdup(out);
if(arg_tmp != NULL) argv[arg_cnt++] = arg_tmp;
}
}
}
return arg_cnt;
}
static void scan_ident(const char **in,char *out,int max_len)
{
char *scan = out;
int len_cnt = max_len;
while ((**in != 0) && (IS_IDENT_CHAR(**in))){
if(len_cnt > 0){
*scan = **in;
scan++;
len_cnt--;
}
(*in)++;
}
*scan = 0;
}
static void scan_number(const char **in,char *out,int max_len)
{
char *scan = out;
int len_cnt = max_len;
while ( (**in != 0) && (isdigit(**in)) ){
if(len_cnt > 0){
*scan = **in;
scan++;
len_cnt--;
}
(*in)++;
}
*scan = 0;
}
static void scan_comp(const char **in,char *out,int max_len)
{
int num_cnt = max_len;
char *out_scan = out;
*out_scan = **in;
out_scan++;
(*in)++;
num_cnt--;
if(num_cnt > 0) {
if(**in == '='){
*out_scan = **in;
out_scan++;
(*in)++;
num_cnt--;
}
}
*out_scan = 0;
}
bool expect(scan_info *info,int check)
{
if(info->sym_code == check){
scan(info);
return false;
} else {
return true;
}
}
bool scan_info_next_line(scan_info *info)
{
if(info->current == NULL) return false;
info->current = info->current->next;
if(info->current == NULL) return false;
info->line_no++;
return set_scan_info_line(info);
}
bool scan_info_home(scan_info *info)
{
info->current = info->data.list;
info->line_no = 1;
return set_scan_info_line(info);
}
int init_scan_info_by_file(const char *file_name,scan_info *info)
{
int err = SHE_NO_ERROR;
err = read_text_file(file_name,&(info->data));
if(err != SHE_NO_ERROR) return err;
info->scanner = NULL;
info->scan_error = SHE_NO_ERROR;
if(scan_info_home(info)) err = SHE_SCAN_ERROR;
return err;
}
bool set_scan_info_line(scan_info *info)
{
if(info->current == NULL) return true;
strcpy(info->input_line,info->current->text);
info->scanner = (info->input_line);
return scan(info);
}
// fetch next token
// **in scan string
// *out token
// return token type (STY_<> vars)
void init_scan_info(const char*in,scan_info *info){
strncpy(info->input_line,in,SCAN_SIZE);
info->input_line[SCAN_SIZE] = 0;
info->scanner=(const char*)info->input_line;
info->scan_error = SHE_NO_ERROR;
info->current = NULL;
info->line_no = 1;
scan(info);
}
bool scan(scan_info *info)
{
bool err = false;
int sym_code = SVO_NONE;
const char **in = &(info->scanner);
char tmp[SCAN_SIZE+1];
while((**in != 0) && (isspace(**in))) (*in)++;
if(isalpha(**in)){
scan_ident(in,info->token,SCAN_SIZE);
sym_code = token_to_id(info->token);
if(sym_code == SVO_NONE) sym_code = SVO_IDENT;
} else if(isdigit(**in)){
scan_number(in,info->token,SCAN_SIZE);
sym_code = SVO_NUMBER;
} else{
switch(**in){
case '#':
sym_code = SVO_END;
break;
case '>':
case '<':
case '=':
case '!':
scan_comp(in,info->token,SCAN_SIZE);
break;
case '"':
err = scan_string(info,SCAN_SIZE);
if(err != SHE_NO_ERROR){
parse_vars_in_string(info->token,tmp,SCAN_SIZE);
strncpy(info->token,tmp,SCAN_SIZE);
info->token[SCAN_SIZE] = 0;
}
sym_code = SVO_DQ_STRING;
break;
case '\'':
sym_code = SVO_SQ_STRING;
err =scan_string(info,SCAN_SIZE);
break;
case 0:
*(info->token) = 0;
sym_code = SVO_END;
break;
default:
info->token[0] = **in;
info->token[1] = 0;
(*in)++;
break;
}
if(sym_code == SVO_NONE) sym_code = token_to_id(info->token);
}
info->sym_code = sym_code;
return err;
}
void parse_vars_in_string(const char *string,char *out,int max_len)
{
const char *scan= string;
char buf[SCAN_SIZE + 1];
char *dest;
char *out_scan = out;
shell_value *value;
int out_len = 0;
int len_cnt;
int out_max_len= max_len;
int can_move;
char *text;
while((*scan != 0) && (*scan != '\'') && (out_max_len > 0)){
if(*scan == '$'){
scan++;
dest = buf;
len_cnt = SCAN_SIZE;
while ((*scan != 0) && (IS_IDENT_CHAR(*scan)) &&(len_cnt > 0)){
*dest = *scan;
dest++;
scan++;
len_cnt--;
}
*dest = 0;
value = get_value_by_name(buf);
if(value != NULL){
text = shell_value_to_char(value);
can_move = strlen(text);
if(can_move > out_max_len) can_move = out_max_len;
memcpy(out_scan,text,can_move);
out_max_len -= can_move;
out_scan += can_move;
free(text);
}
} else{
*out_scan = *scan;
out_len++;
out_scan++;
scan ++;
out_max_len--;
}
}
*out_scan= 0;
}
static int
launch(int (*cmd)(int, char **), int argc, char **argv, char *r_in, char *r_out)
{
int saved_in;
int saved_out;
int new_in;
int new_out;
int retval= 0;
int err;
if (strcmp(r_in, "")!= 0) {
new_in = open(r_in, 0);
if (new_in < 0)
new_in = creat(r_in, 0644);
} else {
new_in = dup(0);
}
if (new_in < 0) {
err = new_in;
goto err_1;
}
if (strcmp(r_out, "")!= 0) {
new_out = open(r_out, 0);
if (new_out < 0)
new_out = creat(r_out, 0644);
} else {
new_out = dup(1);
}
if (new_out < 0) {
err = new_out;
goto err_2;
}
saved_in = dup(0);
saved_out= dup(1);
dup2(new_in, 0);
dup2(new_out, 1);
close(new_in);
close(new_out);
retval = cmd(argc, argv);
dup2(saved_in, 0);
dup2(saved_out, 1);
close(saved_in);
close(saved_out);
return 0;
err_2:
close(new_in);
err_1:
return err;
}
int shell_parse(const char *buf, int len)
{
int i;
// bool found_command = false;
int argc;
char *argv[64];
char redirect_in[256];
char redirect_out[256];
cmd_handler_proc *handler = NULL;
int cnt;
int err;
// search for the command
argc = parse_line(buf, argv, 64, redirect_in, redirect_out);
if(argc == 0) return 0;
handler = &cmd_create_proc;
for(i=0; cmds[i].cmd_handler != NULL; i++) {
if(strcmp(cmds[i].cmd_text,argv[0]) == 0){
handler = cmds[i].cmd_handler;
break;
}
}
err = launch(handler, argc, argv, redirect_in, redirect_out);
for(cnt = 0;cnt <argc;cnt++) free(argv[cnt]);
return err;
}

View File

@ -1,22 +0,0 @@
#ifndef _parse_h_
#define _parse_h_
#include "shell_defs.h"
#define IS_IDENT_CHAR(c) isalnum(c) || (c == '_')
int shell_parse(const char *buf, int len);
bool scan(scan_info *info);
int init_scan_info_by_file(const char *file_name,scan_info *info);
void init_scan_info(const char*in,scan_info *info);
bool scan_info_home(scan_info *info);
bool scan_info_next_line(scan_info *info);
bool expect(scan_info *info,int check);
void parse_vars_in_string(const char *string,char *out,int max_len);
int parse_line(const char *buf, char *argv[], int max_args, char *redirect_in, char *redirect_out);
bool set_scan_info_line(scan_info *info);
char *id_to_token(int id);
#endif

View File

@ -1,124 +0,0 @@
#include <syscalls.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <Errors.h>
#include <stdlib.h>
#include "statements.h"
#include "file_utils.h"
#include "shell_defs.h"
#include "script.h"
#include "parse.h"
#include "statements.h"
static int add_string_to_list(text_file *data,char *text)
{
text_item *item;
item = malloc(sizeof(text_item));
if(item == NULL) return SHE_NO_MEMORY;
item->text = text;
item->next = NULL;
if(data->top != NULL) data->top->next = item;
if(data->list == NULL) data->list = item;
data->top = item;
return SHE_NO_ERROR;
}
void free_text_file(text_file *data)
{
text_item *current;
text_item *next;
current = data->list;
while(current != NULL){
next = current->next;
free(current);
current = next;
}
free(data->buffer);
data->buffer = NULL;
data->list = NULL;
data->top = NULL;
}
int read_text_file(const char *filename,text_file *data)
{
char *scan;
char *old;
int err;
int size;
char realfilename[256];
if( !find_file_in_path(filename,realfilename,SCAN_SIZE)){
printf("can't find '%s' \n",filename);
return SHE_FILE_NOT_FOUND;
}
size = read_file_in_buffer(realfilename,&(data->buffer));
if(size < 0) return size;
scan = data->buffer;
data->top = NULL;
data->list = NULL;
while(size >0){
err = add_string_to_list(data, scan);
if(err <0) goto err;
old = scan;
while((size > 0) && (*scan != 10)) {
scan++;
size--;
}
*scan=0;
scan++;
size--;
}
return B_NO_ERROR;
err:
printf("error\n");
free_text_file(data);
return err;
}
int run_script(const char *file_name)
{
scan_info info;
int err = init_scan_info_by_file(file_name,&info);
if(err != SHE_NO_ERROR) return err;
while(info.current != NULL){
err = parse_info(&info);
if(err != SHE_NO_ERROR) break;
if(scan_info_next_line(&info)) {
err = SHE_SCAN_ERROR;
break;
}
}
//err:
free_text_file(&(info.data));
return err;
}

View File

@ -1,27 +0,0 @@
#ifndef _script_h_
#define _script_h_
#include <ctype.h>
struct _text_item{
struct _text_item *next;
char *text;
};
typedef struct _text_item text_item;
struct _text_file{
char *buffer;
text_item *list;
text_item *top;
};
typedef struct _text_file text_file;
int read_text_file(const char *filename,text_file *data);
void free_text_file(text_file *data);
int run_script(const char *file_name);
#endif

View File

@ -1,71 +0,0 @@
#ifndef _shell_defs_h_
#define _shell_defs_h_
#include <SupportDefs.h>
#include "script.h"
#define NAME_VAR_PATH "path"
#define NAME_VAR_ARGC "argc"
#define NAME_PAR_PRFX "p%d"
#define SVO_ADD 0
#define SVO_SUB 1
#define SVO_MUL 2
#define SVO_DIV 3
#define SVO_BIGGER 4
#define SVO_EQUAL 5
#define SVO_LESS 6
#define SVO_LESS_E 7
#define SVO_BIGGER_E 8
#define SVO_NOT_EQUAL 9
#define SVO_PARENL 10
#define SVO_PARENR 11
#define SVO_COMMA 12
#define SVO_LOAD 13
#define SVO_EXEC 14
#define SVO_ECHO 15
#define SVO_DOLLAR 16
#define SVO_IDENT 17
#define SVO_SQ_STRING 18
#define SVO_DQ_STRING 19
#define SVO_NUMBER 20
#define SVO_CHAR 21
#define SVO_END 22
#define SVO_NONE 23
#define SVO_EXIT 24
#define SVO_IF 25
#define SVO_DOTDOT 26
#define SVO_GOTO 27
#define SVO_NUMBER_CAST 28
#define SVO_STRING_CAST 29
#define SVO_MAX 30
#define SHE_NO_ERROR 0 // no error
#define SHE_NO_MEMORY SVO_MAX+2 // not enough memory
#define SHE_PARSE_ERROR SVO_MAX+3 // parse error
#define SHE_FILE_NOT_FOUND SVO_MAX+4
#define SHE_CANT_EXECUTE SVO_MAX+5
#define SHE_INVALID_TYPE SVO_MAX+6
#define SHE_INVALID_NUMBER SVO_MAX+7
#define SHE_INVALID_OPERATION SVO_MAX+8
#define SHE_SCAN_ERROR SVO_MAX+9
#define SHE_MISSING_QOUTE SVO_MAX+10
#define SHE_LABEL_NOT_FOUND SVO_MAX+11
#define SCAN_SIZE 255 // size of output strings in parser
struct _scan_info{
char input_line[SCAN_SIZE+1];
char token[SCAN_SIZE+1];
const char *scanner;
int sym_code;
int scan_error;
int line_no;
text_item *current;
text_file data;
};
typedef struct _scan_info scan_info;
#endif

View File

@ -1,69 +0,0 @@
#include <stdio.h>
#include <string.h>
#include "shell_history.h"
static char Table[MAX_HISTORY_DEPTH][120]; // holds MAX_HISTORY_DEPTH slots
// each slot up to 120 chars long
static int NumEntries = 0; // current number of filled entries in Table
static int Slot = 0; // index of next slot in Table to look at
void store_history_command(char *cmd_string)
{
// store the command string in the next available slot
char *next = Table[0];
int i;
// find the next empty slot (if there is one)
//
for (i = 0; i < MAX_HISTORY_DEPTH; ++i) {
if (Slot == MAX_HISTORY_DEPTH)
// wrap around
Slot = 0;
next = Table[Slot++];
if (*next == '\0')
// found empty slot
break;
}
// copy in the command string
strcpy (next, cmd_string);
// update count
++NumEntries;
if (NumEntries > MAX_HISTORY_DEPTH)
NumEntries = MAX_HISTORY_DEPTH;
}
char *fetch_history_command(void)
{
// fetch the previous command string
char *prev = "";
int i;
for (i = 0; i < NumEntries; ++i) {
if (Slot == 0)
// wrap around
Slot = NumEntries;
prev = Table[--Slot];
if (*prev)
// found non-blank entry
break;
}
return prev;
}

View File

@ -1,16 +0,0 @@
#ifndef _SHELL_HISTORY_H_
#define _SHELL_HISTORY_H_
// you can store up to this many commands in the history table
#define MAX_HISTORY_DEPTH 20
// the only two history operations:
void store_history_command(char *cmd_string);
char *fetch_history_command(void);
#endif

View File

@ -1,372 +0,0 @@
//#include <types.h>
#include <string.h>
#include <ctype.h>
#include <syscalls.h>
#include <stdio.h>
#include <stdlib.h>
#include "shell_vars.h"
#include "statements.h"
#include "shell_defs.h"
struct _shell_var{
struct _shell_var *next;
char *name;
shell_value *value;
}
;
typedef struct _shell_var shell_var;
static shell_var *var_list;
char *shell_strdup(const char *some_str)
{
char *result;
result = malloc(strlen(some_str) + 1);
if(result != NULL){
strcpy(result,some_str);
}
return result;
}
bool string_to_long_int(const char *str,long int *out)
{
const char *scanner = str;
bool do_neg = false;
*out = 0;
if(*scanner == '-'){
do_neg = true;
scanner++;
}
while ((*scanner != 0) && (isdigit(*scanner))){
(*out) = (*out)*10 + (*scanner)-48;
scanner++;
}
if(do_neg) *out = -(*out);
return (*scanner != 0);
}
void shell_value_free(shell_value *value)
{
if(!value->isnumber) free(value->value.val_text);
free(value);
}
char *shell_value_to_char(shell_value *value)
{
if(value->isnumber){
char tmp[255];
sprintf(tmp,"%ld",value->value.val_number);
return shell_strdup(tmp);
} else {
return shell_strdup(value->value.val_text);
}
}
int shell_value_to_number(shell_value *value,long int *number)
{
if(value->isnumber){
*number = value->value.val_number;
return SHE_NO_ERROR;
} else {
return string_to_long_int(value->value.val_text,number);
}
}
int shell_value_get_text(shell_value * value,char **text){
if(value->isnumber) return SHE_INVALID_TYPE;
*text = value->value.val_text;
return SHE_NO_ERROR;
}
int shell_value_get_number(shell_value *value,long int *number)
{
if(!value->isnumber) return SHE_INVALID_TYPE;
*number = value->value.val_number;
return SHE_NO_ERROR;
}
/* XXX - unused and static
static void shell_var_free(shell_var *var)
{
shell_value_free(var->value);
free(var->name);
free(var);
}
*/
shell_value *shell_value_init_number(long int value)
{
shell_value *current;
current = malloc(sizeof(shell_value));
if(current == NULL) return NULL;
current->isnumber = true;
current->value.val_number = value;
return current;
}
shell_value *shell_value_init_text(const char *value)
{
shell_value *current;
current = malloc(sizeof(shell_value));
if(current == NULL) goto err;
current->value.val_text = shell_strdup(value);
if(current->value.val_text == NULL) goto err1;
current->isnumber = false;
return current;
err1:
free(current);
err:
return NULL;
}
int shell_value_set_text(shell_value *value,const char *new_value)
{
char *val = shell_strdup(new_value);
if(val == NULL) return SHE_NO_MEMORY;
if(!value->isnumber) free(value->value.val_text);
value->value.val_text = val;
value->isnumber = false;
return SHE_NO_ERROR;
}
int shell_value_set_number(shell_value *value,long int new_value)
{
if(!value->isnumber) free(value->value.val_text);
value->value.val_number = new_value;
value->isnumber = true;
return SHE_NO_ERROR;
}
shell_value *shell_value_clone(shell_value *var)
{
if(var->isnumber){
return shell_value_init_number(var->value.val_number);
} else {
return shell_value_init_text(var->value.val_text);
}
}
int shell_value_neg(shell_value *out)
{
long int value;
int err;
err = shell_value_get_number(out,&value);
if(err != SHE_NO_ERROR) return err;
err = shell_value_set_number(out,-value);
return err;
}
// out = out .op. out
int shell_value_do_operation(shell_value *out,const shell_value *other,int oper_type)
{
long int i1;
long int i2;
long int i3;
int err;
if(out->isnumber != other->isnumber) return SHE_INVALID_TYPE;
if(out->isnumber){
i1 = out->value.val_number;
i2 = other->value.val_number;
printf("%ld %ld %d",i1,i2,oper_type);
switch(oper_type){
case SVO_ADD : i3 = i1+i2 ;break;
case SVO_SUB : i3 = i1-i2 ;break;
case SVO_MUL : i3 = i1*i2 ;break;
case SVO_DIV : i3 = i1/i2 ;break;
case SVO_BIGGER : i3 = i1>i2 ;break;
case SVO_LESS : i3 = i1<i2 ;break;
case SVO_EQUAL : i3 = i1 == i2 ;break;
case SVO_BIGGER_E : i3 = i1 >=i2 ;break;
case SVO_LESS_E : i3 = i1 <= i2 ;break;
case SVO_NOT_EQUAL : i3 = i1 != i2 ;break;
default: return SHE_INVALID_OPERATION;
}
} else {
i1 = strcmp(out->value.val_text,other->value.val_text);
switch(oper_type){
case SVO_EQUAL : i3 = i1 == 0;break;
case SVO_BIGGER : i3 = i1 > 0;break;
case SVO_LESS : i3 = i1 < 0;break;
case SVO_BIGGER_E : i3 = i1 >= 0;break;
case SVO_LESS_E : i3 = i1 <= 0;break;
case SVO_NOT_EQUAL: i3 = i1 != 0;break;
default : return SHE_INVALID_OPERATION;
}
}
err = shell_value_set_number(out,i3);
return err;
}
//
// init shell var
//
static shell_var *shell_var_init_value(const char *name,shell_value *value)
{
shell_var *current;
current = malloc(sizeof(shell_var));
if(current == NULL) goto err;
current->name = shell_strdup(name);
if(current->name == NULL) goto err2;
current->value = value;
return current;
free(current->name);
err2:
free(current);
err:
return NULL;
}
//
// Add shell variable
//
static int shell_var_add(const char *var_name,shell_value *value)
{
shell_var *current;
current = shell_var_init_value(var_name,value);
current->next = var_list;
var_list = current;
return SHE_NO_ERROR;
free(current->name);
free(current);
return SHE_NO_MEMORY;
}
static shell_var * find_shell_var_by_name(const char *name)
{
shell_var *current;
current = var_list;
while((current != NULL) && (strcmp(current->name,name) != 0)) current = current->next;
return current;
}
static int shell_var_set(const char *var_name,shell_value *value)
{
shell_var *current;
current = find_shell_var_by_name(var_name);
if(current == NULL){
return shell_var_add(var_name,value);
}
shell_value_free(current->value);
current->value = value;
return SHE_NO_ERROR;
}
int shell_var_set_number(const char *var_name,long int number)
{
shell_value *value;
value = shell_value_init_number(number);
if(value == NULL) return SHE_NO_MEMORY;
return shell_var_set(var_name,value);
}
int shell_var_set_text(const char *var_name,const char *text)
{
shell_value *value;
value = shell_value_init_text(text);
if(value == NULL) return SHE_NO_MEMORY;
return shell_var_set(var_name,value);
}
int set_shell_var_with_value(const char *var_name,shell_value *value)
{
return shell_var_set(var_name,shell_value_clone(value));
}
shell_value *get_value_by_name(const char *name)
{
shell_var *current;
shell_value *result = NULL;
current = find_shell_var_by_name(name);
if(current != NULL) result = current->value;
return result;
}
void init_vars(void){
var_list = NULL;
}

View File

@ -1,45 +0,0 @@
#ifndef _shell_var_h_
#define _shell_vars_h_
//#include <types.h>
struct _shell_value{
bool isnumber;
union{
char *val_text;
long int val_number;
} value;
};
typedef struct _shell_value shell_value;
char *shell_strdup(const char *some_str);
shell_value *get_value_by_name(const char *name);
int shell_var_set_number(const char *var_name,long int number);
int shell_var_set_text(const char *var_name,const char *text);
int set_shell_var_with_value(const char *var_name,shell_value *value);
void init_vars(void);
void shell_value_free(shell_value *value);
int shell_value_do_operation(shell_value *out,const shell_value *other,int oper_type);
int shell_value_neg(shell_value *out);
char *shell_value_to_char(shell_value *value);
shell_value *shell_value_init_number(long int value);
shell_value *shell_value_init_text(const char *value);
int shell_value_to_number(shell_value *value,long int *number);
int shell_value_set_text(shell_value *value,const char *new_value);
int shell_value_set_number(shell_value *value,long int new_value);
int shell_value_get_number(shell_value *value,long int *number);
int shell_value_get_text(shell_value * value,char **text);
shell_value *shell_value_clone(shell_value *var);
bool string_to_long_int(const char *str,long int *out);
#endif

View File

@ -1,741 +0,0 @@
#include <string.h>
#include <ctype.h>
#include <syscalls.h>
#include <stdio.h>
#include <stdlib.h>
#include "parse.h"
#include "statements.h"
#include "shell_vars.h"
#include "shell_defs.h"
#include "file_utils.h"
static int parse_value(scan_info *info,shell_value **out);
static int handle_exec(scan_info *info,shell_value **out);
static int parse_rvl_expr(scan_info *info,shell_value **out);
struct _oper_level{
int sym_code;
int level;
};
typedef struct _oper_level oper_level;
#define MAX_LEVEL 3
oper_level oper_levels[] = {
{SVO_EQUAL,3},
{SVO_LESS,3},
{SVO_LESS_E,3},
{SVO_BIGGER,3},
{SVO_BIGGER_E,3},
{SVO_NOT_EQUAL,3},
{SVO_ADD,2},
{SVO_SUB,2},
{SVO_MUL,1},
{SVO_DIV,1},
{0,-1}
};
static int
get_oper_level(int oper)
{
int cnt = 0;
while ((oper_levels[cnt].level != 0) && (oper_levels[cnt].sym_code != oper))
cnt++;
return oper_levels[cnt].level;
}
static int
parse_neg(scan_info *info,shell_value **out)
{
int err;
bool do_neg = false;
if (info->sym_code == SVO_SUB){
do_neg = true;
if (scan(info))
return SHE_SCAN_ERROR;
}
err = parse_value(info,out);
if (err != SHE_NO_ERROR)
return err;
if (do_neg) {
err = shell_value_neg(*out);
if (err != SHE_NO_ERROR) {
shell_value_free(*out);
*out = NULL;
}
}
return err;
}
static int
parse_oper(scan_info *info,shell_value **out,int level)
{
int err;
int oper_code;
shell_value *other;
if (level > 1)
err = parse_oper(info, out, level - 1);
else
err = parse_neg(info, out);
if (err != SHE_NO_ERROR)
return err;
oper_code = info->sym_code;
while (get_oper_level(info->sym_code) == level) {
if (scan(info)) {
err = SHE_SCAN_ERROR;
goto err;
}
if (level > 1)
err = parse_oper(info,&other,level - 1);
else
err = parse_neg(info,&other);
if (err != SHE_NO_ERROR)
goto err;
err = shell_value_do_operation(*out,other,oper_code);
if (err != SHE_NO_ERROR)
goto err_2;
shell_value_free(other);
}
return SHE_NO_ERROR;
err_2:
shell_value_free(other);
err:
shell_value_free(*out);
*out = NULL;
return err;
}
static int
parse_expr(scan_info *info,shell_value **out)
{
(*out) = NULL;
return parse_oper(info,out,MAX_LEVEL);
}
static int
parse_cast(scan_info * info,shell_value **out)
{
int err;
long int dummy;
char *text;
bool to_number = info->sym_code == SVO_NUMBER_CAST;
if (scan(info))
return SHE_SCAN_ERROR;
err = parse_rvl_expr(info,out);
if (err != SHE_NO_ERROR)
return err;
// place cast in shell_vars=>change type direct
if (to_number) {
err = shell_value_to_number(*out,&dummy);
if (err != SHE_NO_ERROR)
goto err;
err = shell_value_set_number(*out,dummy);
if (err != SHE_NO_ERROR)
goto err;
} else {
text = shell_value_to_char(*out);
err = shell_value_set_text(*out,text);
free(text);
}
return SHE_NO_ERROR;
err:
shell_value_free(*out);
*out = NULL;
return err;
}
static int
parse_value(scan_info *info,shell_value **out)
{
char buf2[SCAN_SIZE+1];
int err = SHE_NO_ERROR;
long int val;
shell_value *value;
*out = NULL;
switch (info->sym_code){
case SVO_DQ_STRING:
parse_vars_in_string(info->token,buf2,SCAN_SIZE);
*out = shell_value_init_text(buf2);
break;
case SVO_NUMBER:
err = string_to_long_int(info->token,&val);
if(err == SHE_NO_ERROR) *out = shell_value_init_number(val);
break;
case SVO_SQ_STRING:
*out = shell_value_init_text(info->token);
break;
case SVO_EXEC :
err = handle_exec(info,out);
break;
case SVO_NUMBER_CAST:
case SVO_STRING_CAST:
err = parse_cast(info,out);
break;
case SVO_PARENL:
err = parse_rvl_expr(info,out);
if (err)
return err;
break;
case SVO_DOLLAR:
if (scan(info))
return SHE_SCAN_ERROR;
if (info->sym_code == SVO_IDENT) {
value = get_value_by_name(info->token);
if (value != NULL)
*out = shell_value_clone(value);
else
*out = shell_value_init_text("");
} else
err = SVO_IDENT;
break;
default:
return SHE_PARSE_ERROR;
}
if (err == SHE_NO_ERROR) {
if (scan(info)) {
if (*out != NULL)
shell_value_free(*out);
return SHE_SCAN_ERROR;
}
if (*out == NULL)
err = SHE_NO_MEMORY;
}
return err;
}
static int
handle_exit(scan_info *info)
{
long int exit_value = 0;
int err = SHE_NO_ERROR;
shell_value *expr;
if (info->sym_code == SVO_PARENL) {
if (scan(info))
return SHE_SCAN_ERROR;
err = parse_expr(info, &expr);
if (err != SHE_NO_ERROR)
return err;
if (!(expr->isnumber)) {
err = SHE_INVALID_TYPE;
goto err;
}
err = shell_value_get_number(expr, &exit_value);
if (err != SHE_NO_ERROR)
goto err;
if (expect(info, SVO_PARENR)) {
err = SVO_PARENR;
goto err;
}
}
exit(exit_value);
err:
shell_value_free(expr);
return err;
}
static int
parse_rvl_expr(scan_info *info,shell_value **out)
{
int err = SHE_NO_ERROR;
*out = NULL;
if (expect(info, SVO_PARENL))
return SVO_PARENL;
err = parse_expr(info, out);
if (err != SHE_NO_ERROR)
return err;
if (expect(info, SVO_PARENR)) {
err = SVO_PARENR;
goto err;
}
return SHE_NO_ERROR;
err:
shell_value_free(*out);
return err;
}
static int
handle_if(scan_info *info)
{
int err = SHE_NO_ERROR;
long int value;
shell_value *expr;
err = parse_rvl_expr(info,&expr);
if (err != SHE_NO_ERROR)
return err;
if (!expr->isnumber) {
err = SHE_INVALID_TYPE;
goto err;
}
err = shell_value_get_number(expr,&value);
if (err != SHE_NO_ERROR)
goto err;
if (value != 0)
err = parse_info(info);
err:
shell_value_free(expr);
return err;
}
//
// Parse exec function
//
static int
handle_exec(scan_info *info,shell_value **out)
{
shell_value *state;
int argc;
char *argv[64];
char redirect_in[SCAN_SIZE+1];
char redirect_out[SCAN_SIZE+1];
int err = SHE_NO_ERROR;
status_t retcode;
char *statement;
*out = NULL;
if (scan(info))
return SHE_SCAN_ERROR;
err = parse_rvl_expr(info, &state);
if (err != SHE_NO_ERROR)
return err;
err = shell_value_get_text(state, &statement);
if (err != SHE_NO_ERROR)
return err;
argc = parse_line(statement, argv, 64, redirect_in, redirect_out);
err = exec_file(argc, argv, &retcode);
if (err != SHE_NO_ERROR)
goto err;
*out = shell_value_init_number(retcode);
err:
shell_value_free(state);
return err;
}
//
// Parse load statement
//
static int
handle_load(scan_info *info)
{
char var_name[SCAN_SIZE+1];
shell_value *value;
int err = SHE_NO_ERROR;
if(info->sym_code != SVO_IDENT) return SVO_IDENT;
strcpy(var_name,info->token);
if(scan(info)) return SHE_SCAN_ERROR;
if(expect(info,SVO_LOAD)) return SVO_LOAD;
err = parse_expr(info,&value);
if(err != SHE_NO_ERROR) return err;
set_shell_var_with_value(var_name,value);
shell_value_free(value);
return SHE_NO_ERROR;
}
//
// Parse echo statement
//
static int handle_echo(scan_info *info)
{
shell_value *value;
int err = SHE_NO_ERROR;
char *text;
while(info->sym_code != SVO_END){
err = parse_expr(info,&value);
if(err != SHE_NO_ERROR) return err;
text = shell_value_to_char(value);
printf("%s",text);
shell_value_free(value);
free(text);
if(info->sym_code == SVO_END) break;
if(expect(info,SVO_COMMA)) {
err = SHE_SCAN_ERROR;
break;
}
}
printf("\n");
return err;
}
//
// Convert error code too string.
//
void ste_error_to_str(int error,char *descr)
{
char *err_txt;
if(error < 0){
strcpy(descr,strerror(error));
return;
}
if(error <= SVO_MAX){
sprintf(descr,"'%s' expected",id_to_token(error));
return;
}
switch(error){
case SHE_NO_ERROR:
err_txt = "No error";
break;
case SHE_NO_MEMORY:
err_txt = "Not enough memory";
break;
case SHE_PARSE_ERROR:
err_txt = "Parse error";
break;
case SHE_FILE_NOT_FOUND:
err_txt = "File not found";
break;
case SHE_CANT_EXECUTE:
err_txt = "Can't execute";
break;
case SHE_INVALID_TYPE:
err_txt = "Invalid type";
break;
case SHE_INVALID_NUMBER:
err_txt = "Invalid number";
break;
case SHE_INVALID_OPERATION:
err_txt = "Invalid operation";
break;
case SHE_MISSING_QOUTE:
err_txt = "Missing Qoute";
break;
case SHE_LABEL_NOT_FOUND:
err_txt = "Label not found";
break;
default:
err_txt = "Unkown error";
}
strcpy(descr,err_txt);
}
// get an directory from path shell variable
// no : number of path
// name : return buffer
// max_len : max size of text in return buffer
bool get_path(int no,char *name,int max_len)
{
int cnt=no;
int len_cnt = max_len;
shell_value *var_value=get_value_by_name(NAME_VAR_PATH);
char *scan=name;
char *value;
int err;
if(var_value == NULL) return false;
err = shell_value_get_text(var_value,&value);
if(err != SHE_NO_ERROR) return false;
while((cnt > 0) && (*value != 0)){
while (*value != ':'){
if(*value == 0){
*name = 0;
return false;
}
value++;
}
value++;
cnt --;
}
if(*value == 0) return false;
while((*value != 0) && (*value != ':') && (len_cnt > 0)){
*scan = *value;
scan ++;
value++;
len_cnt--;
}
*scan = 0;
return true;
}
static int handle_ident(scan_info *info,bool *is_label)
{
char ch = *(info->scanner);
*is_label = false;
if(ch == ':'){
*is_label = true;
if(scan(info)) return SHE_SCAN_ERROR;
if(scan(info)) return SHE_SCAN_ERROR;
if(expect(info,SVO_END)) return SVO_END;
}
return SHE_NO_ERROR;
}
static int is_label_with_name(scan_info *info,char *check_label ,bool *is_label)
{
char *begin = info->input_line;
char *scanner = begin;
*is_label = false;
if(*scanner == 0) return SHE_NO_ERROR;
scanner += strlen(scanner) -1;
while((scanner != begin) && (isspace(*scanner))) scanner--;
if(*scanner == ':'){
if(info->sym_code != SVO_IDENT) return SVO_IDENT;
if(strcmp(check_label , info->token) == 0) *is_label = true;
}
return SHE_NO_ERROR;
}
static int scan_until_label(scan_info *info,char *label_name)
{
int err;
bool is_label;
if(scan_info_home(info)) return SHE_SCAN_ERROR;
while(info->current != NULL){
err = is_label_with_name(info,label_name,&is_label);
if(is_label) return SHE_NO_ERROR;
if(scan_info_next_line(info)) return SHE_SCAN_ERROR;
}
return SHE_LABEL_NOT_FOUND;
}
static int handle_goto(scan_info *info)
{
int err;
char label_name[SCAN_SIZE + 1];
if(scan(info)) return SHE_SCAN_ERROR;
if(info->sym_code != SVO_IDENT) return SVO_IDENT;
strcpy(label_name,info->token);
err = scan_until_label(info,label_name);
return err;
}
int parse_string(char *in)
{
scan_info info;
init_scan_info(in,&info);
return parse_info(&info);
}
int parse_info(scan_info *info)
{
char err_txt[255];
int err = SHE_NO_ERROR;
bool is_label;
switch(info->sym_code){
case SVO_DOLLAR :
if(scan(info)){
err = SHE_SCAN_ERROR;
break;
}
err = handle_load(info);
break;
case SVO_GOTO:
err = handle_goto(info);
break;
case SVO_IDENT:
err = handle_ident(info,&is_label);
if(!is_label){
err = shell_parse(info->input_line,strlen(info->input_line));
}
break;
case SVO_ECHO :
if(scan(info)){
err = SHE_SCAN_ERROR;
break;
}
err = handle_echo(info);
break;
case SVO_IF :
if(scan(info)){
err = SHE_SCAN_ERROR;
} else {
err = handle_if(info);
}
break;
case SVO_EXIT :
if (scan(info))
err = SHE_SCAN_ERROR;
else
err = handle_exit(info);
break;
default:
return shell_parse(info->input_line, strlen(info->input_line));
}
if (err != SHE_NO_ERROR) {
if (err == SHE_SCAN_ERROR)
err = info->scan_error;
ste_error_to_str(err, err_txt);
printf("error :%d:%ld, %d-%s \n", info->line_no,
info->scanner - info->input_line - strlen(info->token),
err, err_txt);
}
return err;
}
void
init_statements(void)
{
shell_var_set_text(NAME_VAR_PATH, ".:/:/boot:/boot/bin:/boot/beos/bin");
}

View File

@ -1,18 +0,0 @@
#ifndef __STATEMENTS_H_
#define __STATEMENTS_H_
//#include <types.h>
#include "shell_defs.h"
void init_statements(void);
bool get_path(int no,char *name,int max_len);
void ste_error_to_str(int error,char *descr);
int parse_string(char *in);
int parse_info(scan_info *info);
#endif

View File

@ -1,122 +0,0 @@
/*
** Small test for signals handling
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <syscalls.h>
typedef void (*beos_signal_func_ptr)(int, void *, struct vregs *);
void sig_handler(int, void *data, struct vregs *regs);
void alarm_handler(int, void *data, struct vregs *regs);
void install_handler(int, beos_signal_func_ptr);
int32 thread(void *);
void usage(void);
void
sig_handler(int sig, void *data, struct vregs *regs)
{
int i;
char buffer[10];
memcpy(buffer, data, 10);
buffer[9] = '\0';
printf("sig_test (sig_handler): Received signal #%d...\n", sig);
printf("User data at %p (contents: \"%s\"), vregs at %p\n", data, buffer, regs);
printf("Counting for fun: ");
for (i=0; i<10; i++)
printf("%d ", i);
printf("\n");
}
void
alarm_handler(int sig, void *data, struct vregs *regs)
{
printf("Alarm!\n");
}
void
install_handler(int sig, beos_signal_func_ptr handler)
{
struct sigaction newa;
struct sigaction olda;
memset(&newa, 0, sizeof(newa));
newa.sa_handler = (__signal_func_ptr)handler;
newa.sa_flags = SA_NOMASK | SA_RESTART;
newa.sa_userdata = "test!";
if (sigaction(sig, &newa, &olda) < 0) {
printf("Failed installing handler for sig #%d!\n", sig);
}
}
int32
thread(void *data)
{
printf("Started snoozing thread...\n");
while (1) {
if (snooze(1000000000L) == B_INTERRUPTED)
printf("sig_test (thread): snooze was interrupted!\n");
}
return 0;
}
void
usage()
{
printf("usage: sig_test <mode>\n"
"where `mode' is one of:\n"
"\t1\tInstalls one handler for all signals and spawns 5 threads\n"
"\t2\tSame as 1 but does not spawn additional threads\n"
"\t3\tSets an alarm via set_alarm\n"
"\n");
exit(1);
}
int
main(int argc, char **argv)
{
int i;
if (argc != 2)
usage();
switch (argv[1][0]) {
case '1':
printf("Spawning some threads...\n");
for (i=0; i<5; i++)
resume_thread(spawn_thread(thread, "sig_test aux thread", B_NORMAL_PRIORITY, NULL));
case '2':
printf("Installing signal handlers...\n");
for (i=1; i<=NSIG; i++)
install_handler(i, sig_handler);
break;
case '3':
install_handler(SIGALRM, alarm_handler);
set_alarm(5000000, B_PERIODIC_ALARM);
break;
}
printf("Done. Entering sleep mode...\n");
while (1) {
if (snooze(1000000000L) == B_INTERRUPTED)
// this never gets called as the syscall is always restarted
printf("sig_test (main): snooze was interrupted!\n");
}
return 0;
}

View File

@ -1,3 +0,0 @@
SubDir OBOS_TOP src kernel apps sockettest ;
KernelObjects <$(SOURCE_GRIST)>main.c : -fpic -Wno-unused ;

View File

@ -1,57 +0,0 @@
/*
** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/resource.h>
#include <Errors.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
int main(int argc, char **argv)
{
int rc = 0, fd[3];
int cnt, i;
int on = 1;
printf("Socket Test!!!\n");
printf("This test doesn't do much but test that we can create a socket descriptor\n\n");
for (i=0;i<3;i++) {
printf("%d : ", i + 1);
fd[i] = socket(PF_INET, SOCK_DGRAM, 0);
printf("socket is fd %d\n", fd[i]);
if (fd[i] > 0) {
rc = read(fd[i], NULL, 0);
printf(" read on fd %d gave %d (expecting 0)\n", fd[i], rc);
}
}
printf("closing the 3 fd's\n");
for (i=0;i<3;i++) {
close(fd[i]);
}
printf("\nNow we have no open fd's so next socket should be 3\n");
fd[0] = socket(PF_INET, SOCK_DGRAM, 0);
printf("new socket = fd %d\n", fd[0]);
printf("trying ioctl to set non blocking: ");
rc = ioctl(fd[0], FIONBIO, &on, sizeof(on));
if (rc == 0) {
printf("OK\n");
} else {
printf("failed\n");
printf("Error was %s\n", strerror(errno));
}
close(fd[0]);
return 0;
}

View File

@ -1,3 +0,0 @@
SubDir OBOS_TOP src kernel apps symlink ;
KernelObjects <$(SOURCE_GRIST)>main.c : -fpic -Wno-unused ;

View File

@ -1,22 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int
main(int argc, char **argv)
{
if (argc < 3) {
fprintf(stderr, "usage: symlink <target> <link-name>\n");
return -1;
}
if (symlink(argv[2], argv[1]) < 0) {
fprintf(stderr, "symlink failed: %s\n", strerror(errno));
return -1;
}
return 0;
}

View File

@ -1,3 +0,0 @@
SubDir OBOS_TOP src kernel apps testapp ;
KernelObjects <$(SOURCE_GRIST)>main.c : -fpic -Wno-unused ;

View File

@ -1,521 +0,0 @@
/*
** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <syscalls.h>
#include <sys/resource.h>
#include <Errors.h>
#include <OS.h>
static void port_test(void);
static int32 port_test_thread_func(void* arg);
#if 0
static int32
test_thread(void *args)
{
int i = (int)args;
snooze(1000000);
for(;;) {
printf("%c", 'a' + i);
}
return 0;
}
#endif
static int32
dummy_thread(void *args)
{
return 1;
}
#if 0
static int32
cpu_eater_thread(void *args)
{
for(;;)
;
}
#endif
static int32
fpu_cruncher_thread(void *args)
{
double y = *(double *)args;
double z;
for(;;) {
z = y * 1.47;
y = z / 1.47;
if(y != *(double *)args)
printf("error: y %f\n", y);
}
}
int main(int argc, char **argv)
{
int rc = 0;
int cnt;
printf("testapp\n");
for(cnt = 0; cnt< argc; cnt++) {
printf("arg %d = %s \n",cnt,argv[cnt]);
}
printf("my thread id is %ld\n", find_thread(NULL));
#if 0
{
char c;
printf("enter something: ");
for(;;) {
c = getchar();
printf("%c", c);
}
}
#endif
#if 0
for(;;) {
snooze(100000);
printf("booyah!");
}
for(;;);
#endif
#if 0
printf("waiting 5 seconds\n");
snooze(5000000);
#endif
#if 0
fd = sys_open("/dev/net/rtl8139/0", "", STREAM_TYPE_DEVICE);
if(fd >= 0) {
for(;;) {
size_t len;
static char buf[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0x0F, 0x00, 0x0F, 0x00, 0x0F,
0x00, 0x08, 0x00, 0x45, 0x00, 0x00, 0x28, 0xF9, 0x55, 0x40, 0x00,
0x40, 0x06, 0xC0, 0x02, 0xC0, 0xA8, 0x00, 0x01, 0xC0, 0xA8, 0x00,
0x26, 0x00, 0x50, 0x0B, 0x5C, 0x81, 0xD6, 0xFA, 0x48, 0xBB, 0x17,
0x03, 0xC9, 0x50, 0x10, 0x7B, 0xB0, 0x6C, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
len = sizeof(buf);
sys_write(fd, buf, 0, &len);
}
}
#endif
#if 0
fd = sys_open("/dev/net/rtl8139/0", "", STREAM_TYPE_DEVICE);
if(fd >= 0) {
int foo = 0;
for(;;) {
size_t len;
char buf[1500];
len = sizeof(buf);
sys_read(fd, buf, 0, &len);
printf("%d read %d bytes\n", foo++, len);
}
}
#endif
// XXX dangerous! This overwrites the beginning of your hard drive
#if 0
{
char buf[512];
size_t bytes_read;
printf("opening /dev/bus/ide/0/0/raw\n");
fd = sys_open("/dev/bus/ide/0/0/raw", "", STREAM_TYPE_DEVICE);
printf("fd = %d\n", fd);
bytes_read = 512;
rc = sys_read(fd, buf, 0, &bytes_read);
printf("rc = %d, bytes_read = %d\n", rc, bytes_read);
buf[0] = 'f';
buf[1] = 'o';
buf[2] = 'o';
buf[3] = '2';
buf[4] = 0;
bytes_read = 512;
rc = sys_write(fd, buf, 1024, &bytes_read);
printf("rc = %d, bytes_read = %d\n", rc, bytes_read);
sys_close(fd);
}
#endif
#if 0
{
thread_id tids[10];
int i;
for(i=0; i<10; i++) {
tids[i] = spawn_thread(&test_thread, "foo", B_NORMAL_PRIORITY, (void *)i);
resume_thread(tids[i]);
}
snooze(5000000);
sys_kill_team(sys_get_current_team_id());
/*
sys_snooze(3000000);
for(i=0; i<10; i++) {
kill_thread(tids[i]);
}
printf("thread_is dead\n");
sys_snooze(5000000);
*/
}
#endif
#if 0
{
for(;;)
sys_create_team("/bin/true", "true", 32);
}
#endif
#if 0
{
void *buf = (void *)0x60000000;
int fd;
int rc;
int len = 512;
fd = sys_open("/bin/testapp", "", STREAM_TYPE_FILE);
rc = sys_read(fd, buf, 0, &len);
printf("rc from read = 0x%x\n", rc);
sys_close(fd);
}
#endif
#if 0
{
char data;
int fd;
fd = sys_open("/dev/audio/pcbeep/1", STREAM_TYPE_DEVICE, 0);
if(fd >= 0) {
printf("writing to the speaker\n");
data = 3;
sys_write(fd, &data, 0, 1);
snooze(1000000);
data = 0;
sys_write(fd, &data, 0, 1);
sys_close(fd);
}
}
#endif
#if 1
{
port_test();
}
#endif
#if 0
{
int fd, bytes_read;
char buf[3];
fd = sys_open("/dev/ps2mouse", STREAM_TYPE_DEVICE, 0);
if(fd < 0) {
printf("failed to open device\n");
return -1;
}
bytes_read = sys_read(fd, buf, -1, 3);
if(bytes_read < 3) {
printf("failed to read device\n");
return -1;
}
printf("Status: %X\nDelta X: %d\nDelta Y: %d\n", buf[0], buf[1], buf[2]);
}
#endif
#if 0
{
int fd;
int buf[512/4];
int i, j;
fd = sys_open("/dev/disk/netblock/0/raw", STREAM_TYPE_DEVICE, 0);
if(fd < 0) {
printf("could not open netblock\n");
return -1;
}
sys_ioctl(fd, 90001, NULL, 0);
for(i=0; i<1*1024*1024; i += 512) {
for(j=0; j<512/4; j++)
buf[j] = i + j*4;
sys_write(fd, buf, -1, sizeof(buf));
}
// sys_read(fd, buf, 0, sizeof(buf));
// sys_write(fd, buf, 512, sizeof(buf));
}
#endif
#if 0
#define NUM_FDS 150
{
int fds[NUM_FDS], i;
struct rlimit rl;
rc = getrlimit(RLIMIT_NOFILE, &rl);
if (rc < 0) {
printf("error in getrlimit\n");
return -1;
}
printf("RLIMIT_NOFILE = %lu\n", rl.rlim_cur);
for(i = 0; i < NUM_FDS; i++) {
fds[i] = open("/bin/testapp", 0);
if (fds[i] < 0) {
break;
}
}
printf("opened %d files\n", i);
rl.rlim_cur += 15;
printf("Setting RLIMIT_NOFILE to %lu\n", rl.rlim_cur);
rc = setrlimit(RLIMIT_NOFILE, &rl);
if (rc < 0) {
printf("error in setrlimit\n");
return -1;
}
rl.rlim_cur = 0;
rc = getrlimit(RLIMIT_NOFILE, &rl);
if (rc < 0) {
printf("error in getrlimit\n");
return -1;
}
printf("RLIMIT_NOFILE = %lu\n", rl.rlim_cur);
for(;i < NUM_FDS; i++) {
fds[i] = open("/bin/testapp", 0);
if (fds[i] < 0) {
break;
}
}
printf("opened a total of %d files\n", i);
}
#endif
#if 0
{
region_id rid;
void *ptr;
rid = sys_vm_map_file("netblock", &ptr, REGION_ADDR_ANY_ADDRESS, 16*1024, LOCK_RW,
REGION_NO_PRIVATE_MAP, "/dev/disk/netblock/0/raw", 0);
if(rid < 0) {
printf("error mmaping device\n");
return -1;
}
// play around with it
printf("mmaped device at %p\n", ptr);
printf("%d\n", *(int *)ptr);
printf("%d\n", *((int *)ptr + 1));
printf("%d\n", *((int *)ptr + 2));
printf("%d\n", *((int *)ptr + 3));
}
#endif
#if 1
{
int i;
printf("spawning %d copies of true\n", 100);
for(i=0; i<100; i++) {
const char *args[] = {"true", NULL};
thread_id id;
bigtime_t t;
printf("%d...", i);
t = system_time();
id = load_image(1, args, (const char **)environ);
if (id <= 0x2) {
printf("new team returned 0x%lx!\n", id);
return -1;
}
wait_for_thread(id, NULL);
printf("done (%Ld usecs)\n", system_time() - t);
}
}
#endif
#if 1
{
int i;
printf("spawning two cpu eaters\n");
// resume_thread(spawn_thread("cpu eater 1", &cpu_eater_thread, 0));
// resume_thread(spawn_thread(&cpu_eater_thread, "cpu eater 2", &cpu_eater_thread, 0));
printf("spawning %d threads\n", 100);
for (i = 0; i < 100; i++) {
thread_id id;
bigtime_t t;
printf("%d...", i);
t = system_time();
id = spawn_thread(&dummy_thread, "testthread", B_NORMAL_PRIORITY, NULL);
if (id > 0)
resume_thread(id);
wait_for_thread(id, NULL);
printf("done (%Ld usecs)\n", system_time() - t);
}
}
#endif
#if 1
{
thread_id id;
static double f[5] = { 2.43, 5.23, 342.34, 234123.2, 1.4 };
printf("spawning a few floating point crunchers (press key to continue)\n");
id = spawn_thread(&fpu_cruncher_thread, "fpu thread0", B_NORMAL_PRIORITY, &f[0]);
resume_thread(id);
id = spawn_thread(&fpu_cruncher_thread, "fpu thread1", B_NORMAL_PRIORITY, &f[1]);
resume_thread(id);
id = spawn_thread(&fpu_cruncher_thread, "fpu thread2", B_NORMAL_PRIORITY, &f[2]);
resume_thread(id);
id = spawn_thread(&fpu_cruncher_thread, "fpu thread3", B_NORMAL_PRIORITY, &f[3]);
resume_thread(id);
id = spawn_thread(&fpu_cruncher_thread, "fpu thread4", B_NORMAL_PRIORITY, &f[4]);
resume_thread(id);
getchar();
printf("passed the test\n");
}
#endif
printf("exiting w/return code %d\n", rc);
return rc;
}
/*
* testcode ports
*/
port_id test_p1, test_p2, test_p3, test_p4;
static void
port_test(void)
{
char testdata[5];
thread_id t;
int res;
int32 dummy;
int32 dummy2;
strcpy(testdata, "abcd");
printf("porttest: create_port()\n");
test_p1 = create_port(1, "test port #1");
test_p2 = create_port(10, "test port #2");
test_p3 = create_port(1024, "test port #3");
test_p4 = create_port(1024, "test port #4");
printf("porttest: find_port()\n");
printf("'test port #1' has id %ld (should be %ld)\n", find_port("test port #1"), test_p1);
printf("porttest: write_port() on 1, 2 and 3\n");
write_port(test_p1, 1, &testdata, sizeof(testdata));
write_port(test_p2, 666, &testdata, sizeof(testdata));
write_port(test_p3, 999, &testdata, sizeof(testdata));
printf("porttest: port_count(test_p1) = %ld\n", port_count(test_p1));
printf("porttest: write_port() on 1 with timeout of 1 sec (blocks 1 sec)\n");
write_port_etc(test_p1, 1, &testdata, sizeof(testdata), B_TIMEOUT, 1000000);
printf("porttest: write_port() on 2 with timeout of 1 sec (wont block)\n");
res = write_port_etc(test_p2, 777, &testdata, sizeof(testdata), B_TIMEOUT, 1000000);
printf("porttest: res=%d, %s\n", res, res == 0 ? "ok" : "BAD");
printf("porttest: read_port() on empty port 4 with timeout of 1 sec (blocks 1 sec)\n");
res = read_port_etc(test_p4, &dummy, &dummy2, sizeof(dummy2), B_TIMEOUT, 1000000);
printf("porttest: res=%d, %s\n", res, res == ETIMEDOUT ? "ok" : "BAD");
printf("porttest: spawning thread for port 1\n");
t = spawn_thread(port_test_thread_func, "port_test", B_NORMAL_PRIORITY, NULL);
// resume thread
resume_thread(t);
printf("porttest: write\n");
write_port(test_p1, 1, &testdata, sizeof(testdata));
// now we can write more (no blocking)
printf("porttest: write #2\n");
write_port(test_p1, 2, &testdata, sizeof(testdata));
printf("porttest: write #3\n");
write_port(test_p1, 3, &testdata, sizeof(testdata));
printf("porttest: waiting on spawned thread\n");
wait_for_thread(t, NULL);
printf("porttest: close p1\n");
close_port(test_p2);
printf("porttest: attempt write p1 after close\n");
res = write_port(test_p2, 4, &testdata, sizeof(testdata));
printf("porttest: write_port ret %s (should give Bad port id)\n", strerror(res));
printf("porttest: testing delete p2\n");
delete_port(test_p2);
printf("porttest: end test main thread\n");
}
static int32
port_test_thread_func(void* arg)
{
int32 msg_code;
int n;
char buf[5];
printf("porttest: port_test_thread_func()\n");
n = read_port(test_p1, &msg_code, &buf, 3);
printf("read_port #1 code %ld len %d buf %3s\n", msg_code, n, buf);
n = read_port(test_p1, &msg_code, &buf, 4);
printf("read_port #1 code %ld len %d buf %4s\n", msg_code, n, buf);
buf[4] = 'X';
n = read_port(test_p1, &msg_code, &buf, 5);
printf("read_port #1 code %ld len %d buf %5s\n", msg_code, n, buf);
printf("porttest: testing delete p1 from other thread\n");
delete_port(test_p1);
printf("porttest: end port_test_thread_func()\n");
return 0;
}

View File

@ -1,3 +0,0 @@
SubDir OBOS_TOP src kernel apps testdigit ;
KernelObjects <$(SOURCE_GRIST)>main.c : -fpic -Wno-unused ;

View File

@ -1,47 +0,0 @@
/*
** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/resource.h>
#include <Errors.h>
#include <fcntl.h>
#define DIGIT "/dev/misc/digit"
int main(int argc, char **argv)
{
int rc = 0, fd, i;
char buffer[128];
size_t len = 128;
printf("Digit Test!!!\n");
printf("\nDigit was one of the example device drivers that Be\n"
"provided, so this test basically justs runs a simple test\n"
"to make sure it works with OpenBeOS.\n"
"\nNB This is a simple test :)\n\n");
fd = open(DIGIT, O_RDONLY, 0);
if (fd < 0) {
printf("Failed at first hurdle :( Couldn't open the device!\n");
return -1;
}
rc = read(fd, buffer, len);
printf("Read on device gave %d\n", rc);
for (i = 0; i < rc; i++) {
printf("%02x ", buffer[i]);
if ((i & 15) == 15)
printf("\n");
}
printf("\n");
close(fd);
printf("closed device.\n");
return 0;
}

View File

@ -1,45 +0,0 @@
SubDir OBOS_TOP src kernel apps tests ;
KernelObjects
<$(SOURCE_GRIST)>thread_test.c
<$(SOURCE_GRIST)>fops_test.c
<$(SOURCE_GRIST)>random_test.c
:
-fpic -Wno-unused
;
KernelLd thread_test :
libglue2.o
<$(SOURCE_GRIST)>thread_test.o
libroot.so
:
$(OBOS_TOP)/src/kernel/ldscripts/$(OBOS_ARCH)/app.ld
:
:
:
bin/thread_test
;
KernelLd fops_test :
libglue2.o
<$(SOURCE_GRIST)>fops_test.o
libroot.so
:
$(OBOS_TOP)/src/kernel/ldscripts/$(OBOS_ARCH)/app.ld
:
:
:
bin/fops_test
;
KernelLd random_test :
libglue2.o
<$(SOURCE_GRIST)>random_test.o
libroot.so
:
$(OBOS_TOP)/src/kernel/ldscripts/$(OBOS_ARCH)/app.ld
:
:
:
bin/random_test
;

View File

@ -1,69 +0,0 @@
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <syscalls.h>
#include <sys/resource.h>
#include <Errors.h>
#include <errno.h>
#include <OS.h>
#include <stdlib.h>
#define TEST_FILE "/boot/etc/fortunes"
#define RD_LINES 5
#define NEW_FORTUNE "The path has been shown - we are the ones who must walk it"
static void
read_file(FILE *f)
{
char buffer[2048];
int lines = 0;
while ((fgets(buffer, sizeof(buffer), f)) != NULL) {
size_t bytes = strlen(buffer);
if (buffer[0] == '#')
continue;
if (buffer[bytes - 1] != '\n')
buffer[bytes - 1] = '\n';
fprintf(stdout, "%d: %s", ++lines, buffer);
}
}
int
main(int argc, char **argv)
{
FILE *f;
f = fopen(TEST_FILE, "rw");
if (!f) {
printf("Failed to open %s :( [%s]\n", TEST_FILE,
strerror(errno));
return -1;
}
read_file(f);
fputs("#@#\n", f);
fputs(NEW_FORTUNE, f);
fprintf(stdout, "File added to.\n");
fclose(f);
printf("File opened and closed\n");
f = fopen(TEST_FILE, "r");
if (!f) {
printf("Failed to open %s :( [%s]\n", TEST_FILE,
strerror(errno));
return -1;
}
read_file(f);
fclose(f);
printf("File opened and closed\n");
return 0;
}

View File

@ -1,43 +0,0 @@
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <syscalls.h>
#include <sys/resource.h>
#include <Errors.h>
#include <errno.h>
#include <OS.h>
#include <stdlib.h>
#include <fcntl.h>
#define RANDOM_DEVICE "/dev/urandom"
#define RANDOM_CHECKS 5
#define STR_LEN 16
int main(int argc, char **argv)
{
int fd;
char data[STR_LEN];
int rv, i, j;
printf("/dev/urandom test\n"
"=================\n\n");
fd = open(RANDOM_DEVICE, O_RDONLY);
if (fd < 0) {
printf("Failed to open %s\n", RANDOM_DEVICE);
return -1;
}
for (i=0; i < RANDOM_CHECKS; i++) {
rv = read(fd, data, STR_LEN);
if (rv < STR_LEN)
break;
printf ("%d: ", i);
for (j=0; j < sizeof(data);j++) {
printf(" %02x ", (uint8)data[j]);
}
printf("\n");
}
close(fd);
return 0;
}

View File

@ -1,136 +0,0 @@
/*
** Copyright 2002-2004, The OpenBeOS Team. All rights reserved.
** Distributed under the terms of the MIT License.
*/
#include <OS.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define THREADS 5
#define START_VAL 1
struct the_test {
int *current_val;
int thread_mult;
sem_id the_lock;
};
static int32
counter_thread(void *data)
{
struct the_test *tt = (struct the_test*)data;
acquire_sem(tt->the_lock);
*(tt->current_val) *= tt->thread_mult;
release_sem(tt->the_lock);
free(data);
return 0;
}
static int32
priority_test(void *data)
{
int i;
for (i = 0; i < 6; i++) {
printf("%s: %d\n", (char *)data, i);
// sys_snooze(1000);
}
return 0;
}
static int32
communication_test(void *data)
{
int times = (int)data;
int i, code;
char buffer[1024];
thread_id sender;
for (i = 0; i < times; i++) {
code = receive_data(&sender, (void *)buffer, sizeof(buffer));
printf(" -> received (%d): \"%s\"\n", code, buffer);
}
return 0;
}
int
main(int argc, char **argv)
{
thread_id t[THREADS];
sem_id lock;
int i;
int expected = START_VAL, current_val = START_VAL;
char *comm_test[] = { "This is a test",
"of the send_data",
"and receive_data",
"syscalls implementation",
"for OpenBeOS" };
printf("OpenBeOS Thread Test - Simple\n"
"=============================\n");
lock = create_sem(1, "test_lock");
for (i = 0; i < THREADS; i++) {
struct the_test *my_test = (struct the_test*)malloc(sizeof(struct the_test));
my_test->the_lock = lock;
my_test->current_val = &current_val;
my_test->thread_mult = i + 1;
t[i] = spawn_thread(counter_thread, "counter", B_NORMAL_PRIORITY, my_test);
if (t[i] > 0) {
resume_thread(t[i]);
expected *= (i + 1);
}
}
wait_for_thread(t[THREADS - 1], NULL);
printf("Test value = %d vs expected of %d\n", current_val, expected);
delete_sem(lock);
t[0] = spawn_thread(priority_test, "thread0", 11, "thread0");
t[1] = spawn_thread(priority_test, "thread1", 12, "thread1");
t[2] = spawn_thread(priority_test, "thread2", 13, "thread2");
resume_thread(t[0]);
resume_thread(t[1]);
resume_thread(t[2]);
printf("Snoozing...\n");
snooze(100000);
printf("Waiting for threads...");
wait_for_thread(t[0], NULL);
printf("1, ");
wait_for_thread(t[1], NULL);
printf("2, ");
wait_for_thread(t[2], NULL);
printf("3.\nDone. Spawning commthread...\n");
t[0] = spawn_thread(communication_test, "commthread", B_NORMAL_PRIORITY, (void *)5);
printf("Spawned. Starting communication...\n");
resume_thread(t[0]);
for (i = 0; i < 5; i++) {
printf("sending");
send_data(t[0], i, comm_test[i], strlen(comm_test[i]) + 1);
// Give time to the commthread to display info
snooze(10000);
}
wait_for_thread(t[0], NULL);
return 0;
}

View File

@ -1,97 +0,0 @@
/* tests TLS (thread locale storage) functionality */
/*
** Copyright 2002, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
*/
#include <OS.h>
#include <TLS.h>
#include <stdio.h>
#include <string.h>
sem_id gWait;
int32 gSlot;
#if __INTEL__
static void *
read_fs_register(void)
{
void *fs;
asm("movl %%fs, %0" : "=r" (fs));
return fs;
}
#endif
static int32
thread1_func(void *data)
{
printf("1. stack at %p, thread id = %ld\n", &data, find_thread(NULL));
printf("1. TLS address = %p\n", tls_address(gSlot));
tls_set(gSlot, (void *)"thread 1");
printf("1. TLS get = \"%s\" (should be \"thread 1\")\n", (char *)tls_get(gSlot));
acquire_sem(gWait);
printf("1. TLS get = \"%s\" (should be \"thread 1\")\n", (char *)tls_get(gSlot));
return 0;
}
static int32
thread2_func(void *data)
{
printf("2. stack at %p, thread id = %ld\n", &data, find_thread(NULL));
printf("2. TLS address = %p\n", tls_address(gSlot));
printf("2. TLS get = \"%s\" (should be NULL)\n", (char *)tls_get(gSlot));
acquire_sem(gWait);
printf("2. TLS get = \"%s\" (should be NULL)\n", (char *)tls_get(gSlot));
return 0;
}
int
main(int argc, char **argv)
{
thread_id thread1, thread2;
status_t returnCode;
gWait = create_sem(0, "waiter");
gSlot = tls_allocate();
printf("got slot %ld, stack at %p, thread id = %ld\n", gSlot, &thread1, find_thread(NULL));
#if __INTEL__
printf("FS register is %p\n", read_fs_register());
#endif
printf("TLS address in main = %p\n", tls_address(gSlot));
tls_set(gSlot, (void *)"main thread");
thread1 = spawn_thread(thread1_func, "Thread 1" , B_NORMAL_PRIORITY, NULL);
resume_thread(thread1);
thread2 = spawn_thread(thread2_func, "Thread 2" , B_NORMAL_PRIORITY, NULL);
resume_thread(thread2);
printf("TLS in main = \"%s\" (should be \"main thread\")\n", (char *)tls_get(gSlot));
// shut down
snooze(100000);
delete_sem(gWait);
wait_for_thread(thread1, &returnCode);
wait_for_thread(thread2, &returnCode);
return 0;
}

View File

@ -1,13 +0,0 @@
/*
** Copyright 2002, Manuel J. Petit. All rights reserved.
** Contains portions of:
**
** /boot/bin/true, Copyright 2001 Travis K. Geiselbrecht
**
** Distributed under the terms of the NewOS License.
*/
int main(void)
{
return 1;
}

View File

@ -1,3 +0,0 @@
SubDir OBOS_TOP src kernel apps uname ;
KernelObjects <$(SOURCE_GRIST)>main.c : -fpic -Wno-unused ;

View File

@ -1,188 +0,0 @@
/* Copyright (c) 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
//#include <sys/param.h>
#include <sysctl.h>
//#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <Errors.h>
void usage(void);
int
main(int argc, char **argv)
{
#define MFLAG 0x01
#define NFLAG 0x02
#define PFLAG 0x04
#define RFLAG 0x08
#define SFLAG 0x10
#define VFLAG 0x20
u_int flags;
int ch, mib[2];
size_t len, tlen;
char *p, buf[1024];
const char *prefix;
flags = 0;
while ((ch = getopt(argc, argv, "amnprsv")) != -1)
switch(ch) {
case 'a':
flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
break;
case 'm':
flags |= MFLAG;
break;
case 'n':
flags |= NFLAG;
break;
case 'p':
flags |= PFLAG;
break;
case 'r':
flags |= RFLAG;
break;
case 's':
flags |= SFLAG;
break;
case 'v':
flags |= VFLAG;
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc)
usage();
if (!flags)
flags |= SFLAG;
prefix = "";
if (flags & SFLAG) {
mib[0] = CTL_KERN;
mib[1] = KERN_OSTYPE;
len = sizeof(buf);
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) {
// err(1, "sysctl");
printf("An error occured\n");
return -1;
}
(void)printf("%s%.*s", prefix, (int)len, buf);
prefix = " ";
}
if (flags & NFLAG) {
mib[0] = CTL_KERN;
mib[1] = KERN_HOSTNAME;
len = sizeof(buf);
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) {
// err(1, "sysctl");
printf("An error occured\n");
return -1;
}
(void)printf("%s%.*s", prefix, (int)len, buf);
prefix = " ";
}
if (flags & RFLAG) {
mib[0] = CTL_KERN;
mib[1] = KERN_OSRELEASE;
len = sizeof(buf);
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) {
// err(1, "sysctl");
printf("An error occured\n");
return -1;
}
(void)printf("%s%.*s", prefix, (int)len, buf);
prefix = " ";
}
if (flags & VFLAG) {
mib[0] = CTL_KERN;
mib[1] = KERN_VERSION;
len = sizeof(buf);
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1){
// err(1, "sysctl");
printf("An error occured\n");
return -1;
}
for (p = buf, tlen = len; tlen--; ++p)
if (*p == '\n' || *p == '\t')
*p = ' ';
(void)printf("%s%.*s", prefix, (int)len, buf);
prefix = " ";
}
if (flags & MFLAG) {
mib[0] = CTL_HW;
mib[1] = HW_MACHINE;
len = sizeof(buf);
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) {
// err(1, "sysctl");
printf("An error occured\n");
return -1;
}
(void)printf("%s%.*s", prefix, (int)len, buf);
prefix = " ";
}
if (flags & PFLAG) {
mib[0] = CTL_HW;
mib[1] = HW_MODEL;
len = sizeof(buf);
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) {
// err(1, "sysctl");
printf("An error occured\n");
return -1;
}
(void)printf("%s%.*s", prefix, (int)len, buf);
prefix = " ";
}
(void)printf("\n");
//exit (0);
return B_NO_ERROR;
}
void
usage()
{
(void)fprintf(stderr, "usage: uname [-amnprsv]\n");
//exit(1);
}