Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronryank committed Mar 10, 2017
0 parents commit 6823a4f
Show file tree
Hide file tree
Showing 12 changed files with 506 additions and 0 deletions.
6 changes: 6 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@echo off
gcc pfunc/*.c -c -Wall -Wextra -Werror
gcc portable.c -c -Wall -Wextra -Werror
ar cr libportable.a *.o
ranlib libportable.a
ERASE *.o
4 changes: 4 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cc pfunc/*.c -c -Wall -Wextra -Werror
cc portability.c -c -Wall -Wextra -Werror
ar ruv portable.a *.o
ranlib portable.a
16 changes: 16 additions & 0 deletions headers/apple.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* apple.h | libportable's APPLE header
* Copyright (C) 2017 Aaron Ryan Klingler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
21 changes: 21 additions & 0 deletions headers/unix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* unix.h | libportable's UNIX header
* Copyright (C) 2017 Aaron Ryan Klingler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <unistd.h>
#include <termios.h>

#define PERR(success, api) {if(!(success)) printf("%s: Error in %s on line %d\n", __FILE__, api, (int) __LINE__);}
24 changes: 24 additions & 0 deletions headers/windows.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* windows.h | libportable's WINDOWS header
* Copyright (C) 2017 Aaron Ryan Klingler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <windows.h>

#ifndef __CYGWIN__
#include <conio.h>
#endif

#define PERR(bSuccess, api) {if(!(bSuccess)) printf("%s: Error %d from %s on line %d\n", __FILE__, (int) GetLastError(), api, (int) __LINE__);}
22 changes: 22 additions & 0 deletions pfunc/cursor.home.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* PORTABLY(cursor, home) | home the cursor (put it at 0x0y)
* Copyright (C) 2017 Aaron Ryan Klingler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../portable.h"
#include <stdio.h>

#define POSIX__home_cursor() POSIX__set_cursor(0, 0)
#define WINDOWS__home_cursor() WINDOWS__set_cursor(0, 0)
35 changes: 35 additions & 0 deletions pfunc/cursor.set.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* PORTABLE(cursor, set, x, y) | set the cursor to {x, y}
* Copyright (C) 2017 Aaron Ryan Klingler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../portable.h"
#include <stdio.h>

int POSIX__set_cursor(int x, int y)
{
printf("\e[%d;%dH", y, x);
return 0; // always succeeds
}

int WINDOWS__set_cursor(int x, int y)
{
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
COORD coordScreen = { x, y };

int bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );
PERR( bSuccess, "SetConsoleCursorPosition" );
return bSuccess;
}
76 changes: 76 additions & 0 deletions pfunc/screen.clear.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* PORTABLY(screen, clear) | clear the screen
* Copyright (C) 2017 Aaron Ryan Klingler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include "../portable.h"

void WINDOWS__clear_screen(void)
{
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
COORD coordScreen = { 0, 0 }; /* here's where we'll home the
cursor */
BOOL bSuccess;
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
DWORD dwConSize; /* number of character cells in
the current buffer */

/* get the number of character cells in the current buffer */

bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
PERR( bSuccess, "GetConsoleScreenBufferInfo" );
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

/* fill the entire screen with blanks */

bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
dwConSize, coordScreen, &cCharsWritten );
PERR( bSuccess, "FillConsoleOutputCharacter" );

/* get the current text attribute */

bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
PERR( bSuccess, "ConsoleScreenBufferInfo" );

/* now set the buffer's attributes accordingly */

bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten );
PERR( bSuccess, "FillConsoleOutputAttribute" );

/* put the cursor at (0, 0) */

bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );
PERR( bSuccess, "SetConsoleCursorPosition" );
return;
}

void POSIX__clear_screen(void)
{
// 2J is ignored on Apple, and 3J is ignored on Unix.
printf("\033[3J\033[2J");
}

// this is super crappy. doesn't really get the job done. but YOLO.
void SYS_UNDEFINED__clear_screen(void)
{
int i, j;

for (i = 0; i < 1000; i++)
for (j = 0; j < 1000; j++)
putchar(' ');
}
27 changes: 27 additions & 0 deletions pfunc/text.color.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* PORTABLY(text, color, COLOR) | set terminal output color to COLOR
* Copyright (C) 2017 Aaron Ryan Klingler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../portable.h"
#include <stdio.h>

int POSIX__color_text(int color)
{
printf("\e[0m\e[%dm", color);
return 0; // always succeeds
}

#define WINDOWS__color_text() // impossible to color text in Windows normally.
56 changes: 56 additions & 0 deletions portable.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* porable.c | libportable's base functions
* Copyright (C) 2017 Aaron Ryan Klingler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "portable.h"

char *
strlower(char *t)
{
int i;
for (i = 0; t[i]; i++)
t[i] = tolower(t[i]);

return t;
}

// avoid GCC telling us we're returning the address of a local variable
char *
strlower_handlerfunc(char *t)
{
char *s = malloc(strlen(t)); // can't free this, so allocate the minimum amount.
memset(s, 0, 50);
memcpy(s, t, strlen(t));

return strlower(s);
}

#define LOWERCASE3(x) strlower_handlerfunc(x)
#define LOWERCASE2(x) LOWERCASE3(#x)
#define LOWERCASE(x) LOWERCASE2(x)

char *
get_system_name(void)
{
if (SYSTEM == SYS_UNDEFINED)
return "";
else
return LOWERCASE(SYSTEM);
}
Loading

0 comments on commit 6823a4f

Please sign in to comment.